1 | /* ======================================================================== *\
|
---|
2 | !
|
---|
3 | ! *
|
---|
4 | ! * This file is part of MARS, the MAGIC Analysis and Reconstruction
|
---|
5 | ! * Software. It is distributed to you in the hope that it can be a useful
|
---|
6 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
|
---|
7 | ! * It is distributed WITHOUT ANY WARRANTY.
|
---|
8 | ! *
|
---|
9 | ! * Permission to use, copy, modify and distribute this software and its
|
---|
10 | ! * documentation for any purpose is hereby granted without fee,
|
---|
11 | ! * provided that the above copyright notice appear in all copies and
|
---|
12 | ! * that both that copyright notice and this permission notice appear
|
---|
13 | ! * in supporting documentation. It is provided "as is" without express
|
---|
14 | ! * or implied warranty.
|
---|
15 | ! *
|
---|
16 | !
|
---|
17 | !
|
---|
18 | ! Author(s): Thomas Bretz, 08/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
|
---|
19 | ! Author(s): Daniela Dorner, 08/2004 <mailto:dorner@astro.uni-wuerzburg.de>
|
---|
20 | !
|
---|
21 | ! Copyright: MAGIC Software Development, 2000-2004
|
---|
22 | !
|
---|
23 | !
|
---|
24 | \* ======================================================================== */
|
---|
25 |
|
---|
26 | /////////////////////////////////////////////////////////////////////////////
|
---|
27 | //
|
---|
28 | // writesequencefile.C
|
---|
29 | // ===================
|
---|
30 | //
|
---|
31 | // reads the sequence information from the database
|
---|
32 | // and writes it into a txt file
|
---|
33 | //
|
---|
34 | /////////////////////////////////////////////////////////////////////////////
|
---|
35 | #include <iostream>
|
---|
36 | #include <iomanip>
|
---|
37 | #include <fstream>
|
---|
38 |
|
---|
39 | #include <MSQLServer.h>
|
---|
40 | #include <TSQLRow.h>
|
---|
41 | #include <TSQLResult.h>
|
---|
42 |
|
---|
43 | #include <TEnv.h>
|
---|
44 | #include <TMath.h>
|
---|
45 | #include <TRegexp.h>
|
---|
46 |
|
---|
47 | #include <MAstro.h>
|
---|
48 | #include <MTime.h>
|
---|
49 | #include <MDirIter.h>
|
---|
50 |
|
---|
51 | using namespace std;
|
---|
52 |
|
---|
53 | Bool_t GetAllRuns(MSQLServer &serv, ofstream &fout, TString query)
|
---|
54 | {
|
---|
55 | TSQLResult *res = serv.Query(query);
|
---|
56 | if (!res)
|
---|
57 | return kFALSE;
|
---|
58 |
|
---|
59 | TSQLRow *row=0;
|
---|
60 |
|
---|
61 | Int_t cnt = 0;
|
---|
62 |
|
---|
63 | fout << "Runs:";
|
---|
64 | while ((row = res->Next()))
|
---|
65 | {
|
---|
66 | fout << " " << (*row)[0];
|
---|
67 | cnt++;
|
---|
68 | }
|
---|
69 | fout << endl;
|
---|
70 |
|
---|
71 | delete res;
|
---|
72 |
|
---|
73 | if (cnt==0)
|
---|
74 | {
|
---|
75 | cout << "ERROR - No runs belonging to this sequence found." << endl;
|
---|
76 | return kFALSE;
|
---|
77 | }
|
---|
78 |
|
---|
79 | return kTRUE;
|
---|
80 | }
|
---|
81 |
|
---|
82 | Bool_t GetCalRuns(MSQLServer &serv, ofstream &fout, TString query, MTime *t)
|
---|
83 | {
|
---|
84 | TSQLResult *res = serv.Query(query);
|
---|
85 | if (!res)
|
---|
86 | return kFALSE;
|
---|
87 |
|
---|
88 | Int_t first = 0;
|
---|
89 | Int_t cnt = 0;
|
---|
90 |
|
---|
91 | fout << "CalRuns:";
|
---|
92 | TSQLRow *row=0;
|
---|
93 | while ((row = res->Next()))
|
---|
94 | if ((*row)[1][0]=='4')
|
---|
95 | {
|
---|
96 | fout << " " << (*row)[0];
|
---|
97 | cnt++;
|
---|
98 |
|
---|
99 | if (!first)
|
---|
100 | {
|
---|
101 | t[0].SetSqlDateTime((*row)[2]);
|
---|
102 | first = 1;
|
---|
103 | }
|
---|
104 | t[1].SetSqlDateTime((*row)[3]);
|
---|
105 | }
|
---|
106 | fout << endl;
|
---|
107 |
|
---|
108 | delete res;
|
---|
109 |
|
---|
110 | if (cnt==0)
|
---|
111 | {
|
---|
112 | cout << "ERROR - No calibration runs belonging to this sequence found." << endl;
|
---|
113 | return kFALSE;
|
---|
114 | }
|
---|
115 |
|
---|
116 | return kTRUE;
|
---|
117 | }
|
---|
118 |
|
---|
119 | Bool_t GetPedRuns(MSQLServer &serv, ofstream &fout, TString query, MTime *t)
|
---|
120 | {
|
---|
121 | Int_t cnt = 0;
|
---|
122 |
|
---|
123 | fout << "PedRuns:";
|
---|
124 |
|
---|
125 | Int_t tot = 0;
|
---|
126 |
|
---|
127 | while (tot<1000)
|
---|
128 | {
|
---|
129 | TSQLResult *res = serv.Query(query);
|
---|
130 | if (!res)
|
---|
131 | return kFALSE;
|
---|
132 |
|
---|
133 | Int_t idx = 0;
|
---|
134 | Int_t n = 0;
|
---|
135 | Double_t diff = 1e35;
|
---|
136 | MTime start, stop;
|
---|
137 |
|
---|
138 | TSQLRow *row=0;
|
---|
139 | while ((row=res->Next()))
|
---|
140 | {
|
---|
141 | if ((*row)[1][0]=='3' || (cnt>1 && idx==0 && (*row)[1][0]=='2'))
|
---|
142 | {
|
---|
143 | MTime t0, t1;
|
---|
144 | t0.SetSqlDateTime((*row)[2]);
|
---|
145 | t1.SetSqlDateTime((*row)[3]);
|
---|
146 |
|
---|
147 | if ((Double_t)t[0]-(Double_t)t1<diff && t[0]>t1)
|
---|
148 | {
|
---|
149 | diff = (Double_t)t[0]-(Double_t)t1;
|
---|
150 | idx = atoi((*row)[0]);
|
---|
151 | n = atoi((*row)[4]);
|
---|
152 | start = t0;
|
---|
153 | stop = t1;
|
---|
154 | }
|
---|
155 | if ((Double_t)t0-(Double_t)t[1]<diff && t0>t[1])
|
---|
156 | {
|
---|
157 | diff = (Double_t)t0-(Double_t)t[1];
|
---|
158 | idx = atoi((*row)[0]);
|
---|
159 | n = atoi((*row)[4]);
|
---|
160 | start = t0;
|
---|
161 | stop = t1;
|
---|
162 | }
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | tot += n;
|
---|
167 | if (idx!=0)
|
---|
168 | fout << " " << idx;
|
---|
169 | cnt ++;
|
---|
170 |
|
---|
171 | delete res;
|
---|
172 |
|
---|
173 | if (start<t[0])
|
---|
174 | t[0] = start;
|
---|
175 | if (stop>t[1])
|
---|
176 | t[1] = stop;
|
---|
177 | }
|
---|
178 |
|
---|
179 | fout << endl;
|
---|
180 |
|
---|
181 | if (cnt==0)
|
---|
182 | {
|
---|
183 | cout << "ERROR - No pedestal (data) runs belonging to this sequence found." << endl;
|
---|
184 | return kFALSE;
|
---|
185 | }
|
---|
186 |
|
---|
187 | return kTRUE;
|
---|
188 | }
|
---|
189 |
|
---|
190 | Bool_t GetDatRuns(MSQLServer &serv, ofstream &fout, TString query)
|
---|
191 | {
|
---|
192 | TSQLResult *res = serv.Query(query);
|
---|
193 | if (!res)
|
---|
194 | return kFALSE;
|
---|
195 |
|
---|
196 | Int_t cnt=0;
|
---|
197 |
|
---|
198 | fout << "DatRuns:";
|
---|
199 | TSQLRow *row=0;
|
---|
200 | while ((row = res->Next()))
|
---|
201 | if ((*row)[1][0]=='2')
|
---|
202 | {
|
---|
203 | fout << " " << (*row)[0];
|
---|
204 | cnt++;
|
---|
205 | }
|
---|
206 | fout << endl;
|
---|
207 |
|
---|
208 | delete res;
|
---|
209 |
|
---|
210 | if (cnt==0)
|
---|
211 | {
|
---|
212 | cout << "ERROR - No data runs belonging to this sequence found." << endl;
|
---|
213 | return kFALSE;
|
---|
214 | }
|
---|
215 |
|
---|
216 | return kTRUE;
|
---|
217 | }
|
---|
218 |
|
---|
219 | TString GetName(MSQLServer &serv, const char *col, const char *n)
|
---|
220 | {
|
---|
221 | TString query(Form("SELECT f%sName FROM %s WHERE f%sKEY=%s",
|
---|
222 | col, col, col, n));
|
---|
223 |
|
---|
224 | TSQLResult *res = serv.Query(query);
|
---|
225 | if (!res)
|
---|
226 | {
|
---|
227 | cout << "ERROR - Resolving " << col << " failed! " << endl;
|
---|
228 | return "";
|
---|
229 | }
|
---|
230 |
|
---|
231 | TSQLRow *row = res->Next();
|
---|
232 | return (*row)[0];
|
---|
233 | }
|
---|
234 |
|
---|
235 | Bool_t GetSequence(MSQLServer &serv, TSQLRow &data)
|
---|
236 | {
|
---|
237 | UShort_t y;
|
---|
238 | Byte_t m, d;
|
---|
239 |
|
---|
240 | MTime time;
|
---|
241 | time.SetSqlDateTime(data[8]);
|
---|
242 | time.GetDateOfSunrise(y, m, d);
|
---|
243 |
|
---|
244 | TString date = Form("%04d-%02d-%02d", y, (int)m, (int)d);
|
---|
245 |
|
---|
246 | Int_t period = MAstro::GetMagicPeriod(time.GetMjd());
|
---|
247 |
|
---|
248 | TString str[6];
|
---|
249 | str[0] = GetName(serv, "Project", data[2]);
|
---|
250 | str[1] = GetName(serv, "Source", data[3]);
|
---|
251 | str[2] = GetName(serv, "L1TriggerTable", data[4]);
|
---|
252 | str[3] = GetName(serv, "L2TriggerTable", data[5]);
|
---|
253 | str[4] = GetName(serv, "HvSettings", data[6]);
|
---|
254 | str[5] = GetName(serv, "LightConditions", data[7]);
|
---|
255 |
|
---|
256 | if (str[0].IsNull() || str[1].IsNull() || str[2].IsNull() || str[3].IsNull() || str[4].IsNull() || str[5].IsNull())
|
---|
257 | return kFALSE;
|
---|
258 |
|
---|
259 | TString fname(Form("/mnt/stk01/sequences/%04d/sequence%08d.txt", atoi(data[0])/10000, atoi(data[0])));
|
---|
260 | cout << "Creating " << fname << "..." << flush;
|
---|
261 |
|
---|
262 | ofstream fout(fname);
|
---|
263 | if (!fout)
|
---|
264 | {
|
---|
265 | cout << "ERROR - Cannot open file." << endl;
|
---|
266 | return kFALSE;
|
---|
267 | }
|
---|
268 |
|
---|
269 | fout << "Sequence: " << data[0] << endl;
|
---|
270 | fout << "Period: " << period << endl;
|
---|
271 | fout << "Night: " << date << endl;
|
---|
272 | fout << "LightConditions: " << str[5] << endl;
|
---|
273 | fout << endl;
|
---|
274 | fout << "Start: " << data[8] << endl;
|
---|
275 | fout << "LastRun: " << data[1] << endl;
|
---|
276 | fout << "Project: " << str[0] << endl;
|
---|
277 | fout << "Source: " << str[1] << endl;
|
---|
278 | fout << "L1TriggerTable: " << str[2] << endl;
|
---|
279 | fout << "L2TriggerTable: " << str[3] << endl;
|
---|
280 | fout << "HvSettings: " << str[4] << endl;
|
---|
281 | fout << "NumEvents: " << data[9] << endl;
|
---|
282 | fout << endl;
|
---|
283 |
|
---|
284 | TString query(Form("SELECT fRunNumber, fRunTypeKEY, fRunStart, fRunStop, fNumEvents"
|
---|
285 | " FROM RunData WHERE fSequenceFirst=%s AND fExcludedFDAKEY=1"
|
---|
286 | " ORDER BY fRunNumber",
|
---|
287 | data[0]));
|
---|
288 |
|
---|
289 | if (!GetAllRuns(serv, fout, query))
|
---|
290 | return kFALSE;
|
---|
291 |
|
---|
292 | fout << endl;
|
---|
293 |
|
---|
294 | MTime t[2];
|
---|
295 | if (!GetCalRuns(serv, fout, query, t))
|
---|
296 | return kFALSE;
|
---|
297 | if (!GetPedRuns(serv, fout, query, t))
|
---|
298 | return kFALSE;
|
---|
299 | if (!GetDatRuns(serv, fout, query))
|
---|
300 | return kFALSE;
|
---|
301 |
|
---|
302 | fout << endl;
|
---|
303 |
|
---|
304 | cout << " done <Nevts=" << data[9] << ">" << endl;
|
---|
305 |
|
---|
306 | return kTRUE;
|
---|
307 | }
|
---|
308 |
|
---|
309 | // This tool will work from Period017 (2004_05_17) on...
|
---|
310 | int writesequencefile(Int_t sequno)
|
---|
311 | {
|
---|
312 | TEnv env("sql.rc");
|
---|
313 |
|
---|
314 | MSQLServer serv(env);
|
---|
315 | if (!serv.IsConnected())
|
---|
316 | {
|
---|
317 | cout << "ERROR - Connection to database failed." << endl;
|
---|
318 | return 0;
|
---|
319 | }
|
---|
320 |
|
---|
321 | cout << "writesequencefile" << endl;
|
---|
322 | cout << "-----------------" << endl;
|
---|
323 | cout << endl;
|
---|
324 | cout << "Connected to " << serv.GetName() << endl;
|
---|
325 | cout << endl;
|
---|
326 |
|
---|
327 | TString query(Form("SELECT fSequenceFirst, fSequenceLast, fProjectKEY, fSourceKEY,"
|
---|
328 | " fL1TriggerTableKEY, fL2TriggerTableKEY, fHvSettingsKEY, "
|
---|
329 | " fLightConditionsKEY, fRunStart, fNumEvents"
|
---|
330 | " FROM Sequences WHERE fSequenceFirst=%d", sequno));
|
---|
331 | TSQLResult *res = serv.Query(query);
|
---|
332 |
|
---|
333 | TSQLRow *row = 0;
|
---|
334 | while ((row = res->Next()))
|
---|
335 | if (!GetSequence(serv, *row))
|
---|
336 | return 0;
|
---|
337 |
|
---|
338 | delete res;
|
---|
339 |
|
---|
340 | cout << endl;
|
---|
341 |
|
---|
342 | return 1;
|
---|
343 | }
|
---|