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 | // buildsequenceentries.C
|
---|
29 | // ======================
|
---|
30 | //
|
---|
31 | // to group the runs of one night into sequences, this marco:
|
---|
32 | // - reads the runinformation of one night from the database
|
---|
33 | // - group the runs into sets of following runs with the same conditions
|
---|
34 | // - groups the runs in this sets to sequences such that each run belongs
|
---|
35 | // to the nearest (in time) calibration run
|
---|
36 | // - check if the runs with the same runtype have the same calibration script
|
---|
37 | // and the same trigger tables
|
---|
38 | //
|
---|
39 | /////////////////////////////////////////////////////////////////////////////
|
---|
40 | #include <iostream>
|
---|
41 | #include <iomanip>
|
---|
42 | #include <fstream>
|
---|
43 |
|
---|
44 | #include <MSQLServer.h>
|
---|
45 | #include <TSQLRow.h>
|
---|
46 | #include <TSQLResult.h>
|
---|
47 |
|
---|
48 | #include <TEnv.h>
|
---|
49 | #include <TMath.h>
|
---|
50 | #include <TExMap.h>
|
---|
51 | #include <TArrayI.h>
|
---|
52 | #include <TRegexp.h>
|
---|
53 | #include <TSystem.h>
|
---|
54 |
|
---|
55 | #include <MTime.h>
|
---|
56 | #include <MDirIter.h>
|
---|
57 |
|
---|
58 | using namespace std;
|
---|
59 |
|
---|
60 | int debug = 0;
|
---|
61 |
|
---|
62 | Bool_t DeleteSequence(MSQLServer &serv, TString datapath, TString sequpath, Int_t sequ, Bool_t dummy)
|
---|
63 | {
|
---|
64 | TString query1(Form("DELETE FROM Calibration WHERE fSequenceFirst=%d", sequ));
|
---|
65 | TString query2(Form("DELETE FROM Star WHERE fSequenceFirst=%d", sequ));
|
---|
66 | TString query3(Form("DELETE FROM SequenceProcessStatus WHERE fSequenceFirst=%d", sequ));
|
---|
67 | TString query4(Form("UPDATE RunData SET fSequenceFirst=0 WHERE fSequenceFirst=%d", sequ));
|
---|
68 | TString query5(Form("DELETE FROM Sequences WHERE fSequenceFirst=%d AND fManuallyChangedKEY=1", sequ));
|
---|
69 |
|
---|
70 | TString fname(Form("%s/%04d/sequence%08d.txt", sequpath.Data(),sequ/10000, sequ));
|
---|
71 | TString command(Form("rm -r %s/callisto/%04d/%08d/", datapath.Data(), sequ/10000, sequ));
|
---|
72 | TString command2(Form("rm -r %s/star/%04d/%08d/", datapath.Data(), sequ/10000, sequ));
|
---|
73 |
|
---|
74 | if (dummy)
|
---|
75 | {
|
---|
76 | cout << "not using dummy=kTRUE the following commands would be executed: " << endl;
|
---|
77 | cout << "queries: " << endl;
|
---|
78 | cout << query1 << endl;
|
---|
79 | cout << query2 << endl;
|
---|
80 | cout << query3 << endl;
|
---|
81 | cout << query4 << endl;
|
---|
82 | cout << query5 << endl;
|
---|
83 | cout << "removing files:" << endl;
|
---|
84 | cout << "unlink " << fname << endl;
|
---|
85 | cout << command << endl;
|
---|
86 | cout << command2 << endl;
|
---|
87 | return kTRUE;
|
---|
88 | }
|
---|
89 |
|
---|
90 | TSQLResult *res = serv.Query(query1);
|
---|
91 | if (!res)
|
---|
92 | return kFALSE;
|
---|
93 | delete res;
|
---|
94 |
|
---|
95 | res = serv.Query(query2);
|
---|
96 | if (!res)
|
---|
97 | return kFALSE;
|
---|
98 | delete res;
|
---|
99 |
|
---|
100 | res = serv.Query(query3);
|
---|
101 | if (!res)
|
---|
102 | return kFALSE;
|
---|
103 | delete res;
|
---|
104 |
|
---|
105 | res = serv.Query(query4);
|
---|
106 | if (!res)
|
---|
107 | return kFALSE;
|
---|
108 | delete res;
|
---|
109 |
|
---|
110 | res = serv.Query(query5);
|
---|
111 | if (!res)
|
---|
112 | return kFALSE;
|
---|
113 | delete res;
|
---|
114 |
|
---|
115 | gSystem->Unlink(fname);
|
---|
116 |
|
---|
117 | gSystem->Exec(command);
|
---|
118 | gSystem->Exec(command2);
|
---|
119 |
|
---|
120 | return kTRUE;
|
---|
121 | }
|
---|
122 |
|
---|
123 | Int_t DoCheck(TSQLResult &res)
|
---|
124 | {
|
---|
125 | TArrayI data(5);
|
---|
126 | Int_t n = 0;
|
---|
127 |
|
---|
128 | TSQLRow *row=0;
|
---|
129 | while ((row=res.Next()))
|
---|
130 | {
|
---|
131 | n++;
|
---|
132 |
|
---|
133 | if (data[0]==0)
|
---|
134 | {
|
---|
135 | for (int i=0; i<data.GetSize(); i++)
|
---|
136 | data[i] = atoi((*row)[i]);
|
---|
137 | continue;
|
---|
138 | }
|
---|
139 |
|
---|
140 | for (int i=1; i<data.GetSize(); i++)
|
---|
141 | {
|
---|
142 | if (data[i] != atoi((*row)[i]))
|
---|
143 | return i+1;
|
---|
144 | }
|
---|
145 | }
|
---|
146 | return n==0 ? 0 : -1;
|
---|
147 | }
|
---|
148 |
|
---|
149 | Bool_t CheckRuns(MSQLServer &serv, Int_t from, Int_t to, Int_t type)
|
---|
150 | {
|
---|
151 | TString query("SELECT fRunNumber, fL1TriggerTableKEY, fL2TriggerTableKEY,"
|
---|
152 | " fCalibrationScriptKEY, fProjectKEY FROM RunData");
|
---|
153 | query += Form(" WHERE fRunTypeKEY=%d AND fExcludedFDAKEY=1 AND "
|
---|
154 | " (fRunNumber BETWEEN %d AND %d)"
|
---|
155 | " ORDER BY fRunNumber", type, from, to);
|
---|
156 |
|
---|
157 | TSQLResult *res = serv.Query(query);
|
---|
158 | if (!res)
|
---|
159 | return kFALSE;
|
---|
160 |
|
---|
161 | Int_t rc = DoCheck(*res);
|
---|
162 | delete res;
|
---|
163 |
|
---|
164 | switch (rc)
|
---|
165 | {
|
---|
166 | case 0: cout << "ERROR - No runs found for check!" << endl; break;
|
---|
167 | case 1: cout << "ERROR - fRunNumber doesn't match!" << endl; break;
|
---|
168 | case 2: cout << "ERROR - fL1TriggerTableKEY doesn't match!" << endl; break;
|
---|
169 | case 3: cout << "ERROR - fL2TriggerTableKEY doesn't match!" << endl; break;
|
---|
170 | case 4: cout << "ERROR - fCalibrationScriptKEY doesn't match!" << endl; break;
|
---|
171 | case 5: cout << "ERROR - fProjectKEY doesn't match!" << endl; break;
|
---|
172 | }
|
---|
173 |
|
---|
174 | return rc<0;
|
---|
175 | }
|
---|
176 |
|
---|
177 | Bool_t CheckSequence(MSQLServer &serv, TString datapath, TString sequpath, Int_t from, Int_t to, Bool_t dummy)
|
---|
178 | {
|
---|
179 | Bool_t rc=kFALSE;
|
---|
180 |
|
---|
181 | TString query("SELECT fSequenceFirst, count(*) FROM RunData ");
|
---|
182 | query += Form(" WHERE fExcludedFDAKEY=1 AND (fRunNumber BETWEEN %d AND %d)"
|
---|
183 | " GROUP BY fSequenceFirst", from, to);
|
---|
184 |
|
---|
185 | TSQLResult *res = serv.Query(query);
|
---|
186 | if (!res)
|
---|
187 | return kFALSE;
|
---|
188 |
|
---|
189 | Int_t count=0;
|
---|
190 | Int_t nor=0;
|
---|
191 | TArrayI sequences;
|
---|
192 |
|
---|
193 | TSQLRow *row=0;
|
---|
194 | while ((row=res->Next()))
|
---|
195 | {
|
---|
196 | sequences.Set(count+1);
|
---|
197 | sequences.AddAt(atoi((*row)[0]), count);
|
---|
198 | // cout << "sequence: " << sequences.At(count) << endl;
|
---|
199 | nor=atoi((*row)[1]);
|
---|
200 | count++;
|
---|
201 | }
|
---|
202 |
|
---|
203 | delete res;
|
---|
204 |
|
---|
205 | if (sequences.GetSize()>1)
|
---|
206 | {
|
---|
207 | for (Int_t i=0;i<sequences.GetSize();i++)
|
---|
208 | {
|
---|
209 | rc=kTRUE;
|
---|
210 | if (sequences.At(i)!=0)
|
---|
211 | {
|
---|
212 | cout << "deleting sequence " << sequences.At(i) << "... <" << i << ">" << endl;
|
---|
213 | if(!DeleteSequence(serv, datapath, sequpath, sequences.At(i), dummy))
|
---|
214 | rc=kFALSE;
|
---|
215 | }
|
---|
216 | }
|
---|
217 | return rc;
|
---|
218 | }
|
---|
219 |
|
---|
220 | if (sequences.At(0)==0)
|
---|
221 | return kTRUE;
|
---|
222 |
|
---|
223 |
|
---|
224 | query="SELECT fRunNumber FROM RunData ";
|
---|
225 | query += Form(" WHERE fExcludedFDAKEY=1 AND (fRunNumber BETWEEN %d AND %d)"
|
---|
226 | " ORDER BY fRunNumber", from, to);
|
---|
227 |
|
---|
228 | res = serv.Query(query);
|
---|
229 | if (!res)
|
---|
230 | return kFALSE;
|
---|
231 |
|
---|
232 | TArrayI runs;
|
---|
233 | count=0;
|
---|
234 | row=0;
|
---|
235 | while ((row=res->Next()))
|
---|
236 | {
|
---|
237 | runs.Set(count+1);
|
---|
238 | runs.AddAt(atoi((*row)[0]), count);
|
---|
239 | count++;
|
---|
240 | }
|
---|
241 |
|
---|
242 | delete res;
|
---|
243 |
|
---|
244 |
|
---|
245 | if (nor==runs.GetSize() && sequences.GetSize()==1)
|
---|
246 | {
|
---|
247 | query="SELECT fRunNumber FROM RunData ";
|
---|
248 | query += Form(" WHERE fSequenceFirst=%d ", sequences.At(0));
|
---|
249 |
|
---|
250 | res = serv.Query(query);
|
---|
251 | if (!res)
|
---|
252 | return kFALSE;
|
---|
253 |
|
---|
254 | TArrayI oldruns;
|
---|
255 | count=0;
|
---|
256 | row=0;
|
---|
257 | while ((row=res->Next()))
|
---|
258 | {
|
---|
259 | oldruns.Set(count+1);
|
---|
260 | oldruns.AddAt(atoi((*row)[0]), count);
|
---|
261 | count++;
|
---|
262 | }
|
---|
263 |
|
---|
264 | if (runs.GetSize()!=oldruns.GetSize())
|
---|
265 | {
|
---|
266 | cout << "different sequsizes -> deleting sequence " << sequences.At(0) << endl;
|
---|
267 | return DeleteSequence(serv, datapath, sequpath, sequences.At(0), dummy);
|
---|
268 | }
|
---|
269 |
|
---|
270 | for (Int_t i=0; i<runs.GetSize(); i++)
|
---|
271 | {
|
---|
272 | if (runs.At(i)==oldruns.At(i))
|
---|
273 | continue;
|
---|
274 | cout << "different runs -> deleting sequence " << sequences.At(0) << endl;
|
---|
275 | return DeleteSequence(serv, datapath, sequpath, sequences.At(0), dummy);
|
---|
276 | }
|
---|
277 |
|
---|
278 | delete res;
|
---|
279 | }
|
---|
280 |
|
---|
281 | query="SELECT fSequenceFirst FROM Sequences ";
|
---|
282 | query += Form(" WHERE fSequenceFirst fRunNumber BETWEEN %d AND %d "
|
---|
283 | " ORDER BY fSequenceFirst", from, to);
|
---|
284 |
|
---|
285 | res = serv.Query(query);
|
---|
286 | if (!res)
|
---|
287 | return kFALSE;
|
---|
288 |
|
---|
289 | Int_t sequ=0;
|
---|
290 | while ((row=res->Next()))
|
---|
291 | {
|
---|
292 | sequ=atoi((*row)[0]);
|
---|
293 | cout << "deleting sequence " << sequ << endl;
|
---|
294 | if(!DeleteSequence(serv, datapath, sequpath, sequ, dummy))
|
---|
295 | rc=kFALSE;
|
---|
296 | }
|
---|
297 |
|
---|
298 | delete res;
|
---|
299 |
|
---|
300 |
|
---|
301 | cout << "same sequence -> nothing to do..." << endl;
|
---|
302 | return rc;
|
---|
303 | }
|
---|
304 |
|
---|
305 | Bool_t InsertSequence(MSQLServer &serv, Int_t from, Int_t to)
|
---|
306 | {
|
---|
307 |
|
---|
308 | // ========== Request number of events ==========
|
---|
309 | TString query("SELECT SUM(fNumEvents), "
|
---|
310 | " SUM(if(TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)<0,"
|
---|
311 | " TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)+24*60*60,"
|
---|
312 | " TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart))), ");
|
---|
313 | query += " MIN(fZenithDistance), MAX(fZenithDistance), ";
|
---|
314 | query += " MIN(fAzimuth), MAX(fAzimuth) ";
|
---|
315 | query += Form(" FROM RunData WHERE fRunTypeKEY=2 AND "
|
---|
316 | " (fRunNumber BETWEEN %d AND %d) AND fExcludedFDAKEY=1",
|
---|
317 | from, to);
|
---|
318 |
|
---|
319 | TSQLResult *res = serv.Query(query);
|
---|
320 | if (!res)
|
---|
321 | return kFALSE;
|
---|
322 |
|
---|
323 | TSQLRow *row = res->Next();
|
---|
324 | if (!row || !(*row)[0])
|
---|
325 | {
|
---|
326 | cout << "ERROR - No result from query: " << query << endl;
|
---|
327 | return kFALSE;
|
---|
328 | }
|
---|
329 |
|
---|
330 | TString nevts = (*row)[0];
|
---|
331 | TString secs = (*row)[1];
|
---|
332 | TString zdmin = (*row)[2];
|
---|
333 | TString zdmax = (*row)[3];
|
---|
334 | TString azmin = (*row)[4];
|
---|
335 | TString azmax = (*row)[5];
|
---|
336 |
|
---|
337 | delete res;
|
---|
338 |
|
---|
339 | // ========== Request start time of sequence ==========
|
---|
340 | query = Form("SELECT fRunStart FROM RunData WHERE fRunNumber=%d AND fExcludedFDAKEY=1", from);
|
---|
341 |
|
---|
342 | res = serv.Query(query);
|
---|
343 | if (!res)
|
---|
344 | return kFALSE;
|
---|
345 |
|
---|
346 | row = res->Next();
|
---|
347 | if (!row || !(*row)[0])
|
---|
348 | {
|
---|
349 | cout << "ERROR - No result from query: " << query << endl;
|
---|
350 | return kFALSE;
|
---|
351 | }
|
---|
352 |
|
---|
353 | TString start((*row)[0]);
|
---|
354 |
|
---|
355 | delete res;
|
---|
356 |
|
---|
357 | // ========== Request data of sequence ==========
|
---|
358 | query = Form("SELECT fSourceKEY, fProjectKEY, "
|
---|
359 | " fL1TriggerTableKEY, fL1TriggerTableKEY,"
|
---|
360 | " fHvSettingsKEY, fDiscriminatorThresholdTableKEY,"
|
---|
361 | " fTriggerDelayTableKEY, fLightConditionsKEY, fTestFlagKEY"
|
---|
362 | " FROM RunData"
|
---|
363 | " WHERE fRunTypeKEY=2 AND fExcludedFDAKEY=1 AND (fRunNumber BETWEEN %d AND %d)"
|
---|
364 | " LIMIT 1", from, to);
|
---|
365 |
|
---|
366 | res = serv.Query(query);
|
---|
367 | if (!res)
|
---|
368 | return kFALSE;
|
---|
369 |
|
---|
370 | row = res->Next();
|
---|
371 | if (!row)
|
---|
372 | {
|
---|
373 | cout << "ERROR - No result from query: " << query << endl;
|
---|
374 | return kFALSE;
|
---|
375 | }
|
---|
376 |
|
---|
377 | TString query1("INSERT Sequences SET");
|
---|
378 | query1+=Form(" fSequenceFirst=%d, fSequenceLast=%d,", from, to);
|
---|
379 | query1+=Form(" fProjectKEY=%s,", (*row)[0]);
|
---|
380 | query1+=Form(" fSourceKEY=%s,", (*row)[1]);
|
---|
381 | query1+=Form(" fNumEvents=%s,", nevts.Data());
|
---|
382 | query1+=Form(" fRunTime=%s,", secs.Data());
|
---|
383 | query1+=Form(" fRunStart=\"%s\",", start.Data());
|
---|
384 | query1+=Form(" fZenithDistanceMin=%s,", zdmin.Data());
|
---|
385 | query1+=Form(" fZenithDistanceMax=%s,", zdmax.Data());
|
---|
386 | query1+=Form(" fAzimuthMin=%s,", azmin.Data());
|
---|
387 | query1+=Form(" fAzimuthMax=%s,", azmax.Data());
|
---|
388 | query1+=Form(" fL1TriggerTableKEY=%s,", (*row)[2]);
|
---|
389 | query1+=Form(" fL2TriggerTableKEY=%s,", (*row)[3]);
|
---|
390 | query1+=Form(" fHvSettingsKEY=%s,", (*row)[4]);
|
---|
391 | query1+=Form(" fDiscriminatorThresholdTableKEY=%s,", (*row)[5]);
|
---|
392 | query1+=Form(" fTriggerDelayTableKEY=%s,", (*row)[6]);
|
---|
393 | query1+=Form(" fLightConditionsKEY=%s,", (*row)[7]);
|
---|
394 | query1+=Form(" fTestFlagKEY=%s, fManuallyChangedKEY=1", (*row)[8]);
|
---|
395 |
|
---|
396 |
|
---|
397 | TString query2 = Form("UPDATE RunData SET fSequenceFirst=%d WHERE"
|
---|
398 | " (fRunNumber BETWEEN %d AND %d) AND"
|
---|
399 | " (fRunTypeKEY BETWEEN 2 AND 4) AND"
|
---|
400 | " fSourceKEY=%d AND fHvSettingsKEY=%s AND fExcludedFDAKEY=1",
|
---|
401 | from, from, to, (*row)[1], (*row)[4]);
|
---|
402 |
|
---|
403 | TString query3 = Form("INSERT SequenceProcessStatus SET fSequenceFirst=%d ", from);
|
---|
404 |
|
---|
405 | delete res;
|
---|
406 |
|
---|
407 | res = serv.Query(query1);
|
---|
408 | if (!res)
|
---|
409 | return kFALSE;
|
---|
410 | delete res;
|
---|
411 |
|
---|
412 | res = serv.Query(query2);
|
---|
413 | if (!res)
|
---|
414 | return kFALSE;
|
---|
415 | delete res;
|
---|
416 |
|
---|
417 | res = serv.Query(query3);
|
---|
418 | if (!res)
|
---|
419 | return kFALSE;
|
---|
420 | delete res;
|
---|
421 |
|
---|
422 | return kTRUE;
|
---|
423 | }
|
---|
424 |
|
---|
425 | Bool_t NewSequence(MSQLServer &serv, TString datapath, TString sequpath, Int_t from, Int_t to, Bool_t dummy)
|
---|
426 | {
|
---|
427 | cout << "Found Sequence (" << from << ", " << to << ") ... checking runs..." << flush;
|
---|
428 |
|
---|
429 | if (!CheckRuns(serv, from, to, 2))
|
---|
430 | {
|
---|
431 | cout << "Warning - Found inconsistency in data-runs (" << from << ", " << to << ")" << endl;
|
---|
432 | //sequence is not built, but kTRUE is returned, to allow
|
---|
433 | //the automatic processing of the other sequences of this day
|
---|
434 | return kTRUE;
|
---|
435 | }
|
---|
436 | if (!CheckRuns(serv, from, to, 3))
|
---|
437 | {
|
---|
438 | cout << "Warning - Found inconsistency in ped-runs (" << from << ", " << to << ")" << endl;
|
---|
439 | //sequence is not built, but kTRUE is returned, to allow
|
---|
440 | //the automatic processing of the other sequences of this day
|
---|
441 | return kTRUE;
|
---|
442 | }
|
---|
443 |
|
---|
444 | cout << "ok." << endl;
|
---|
445 |
|
---|
446 |
|
---|
447 | cout << "checking Sequence..." << endl;
|
---|
448 |
|
---|
449 | if (!CheckSequence(serv, datapath, sequpath, from, to, dummy))
|
---|
450 | {
|
---|
451 | cout << " inserting sequence not necessary" << endl;
|
---|
452 | return kTRUE;
|
---|
453 | }
|
---|
454 |
|
---|
455 |
|
---|
456 | if (dummy)
|
---|
457 | return kTRUE;
|
---|
458 |
|
---|
459 | Bool_t rc = InsertSequence(serv, from, to);
|
---|
460 | if (!rc)
|
---|
461 | cout << "InsertSequence failed!" << endl;
|
---|
462 |
|
---|
463 | return rc;
|
---|
464 | }
|
---|
465 |
|
---|
466 | Bool_t Process(MSQLServer &serv, TString datapath, TString sequpath, Int_t from, Int_t to, Bool_t dummy)
|
---|
467 | {
|
---|
468 |
|
---|
469 | TString query(Form("SELECT fRunNumber, fRunTypeKEY, fRunStart, fRunStop"
|
---|
470 | " FROM RunData"
|
---|
471 | " WHERE fRunNumber BETWEEN %d AND %d AND "
|
---|
472 | " fExcludedFDAKEY=1 AND (fRunTypeKEY BETWEEN 2 AND 4)"
|
---|
473 | " ORDER BY fRunNumber", from, to));
|
---|
474 |
|
---|
475 | TSQLResult *res = serv.Query(query);
|
---|
476 | if (!res)
|
---|
477 | return kFALSE;
|
---|
478 |
|
---|
479 | TExMap map;
|
---|
480 |
|
---|
481 | Int_t start=0;
|
---|
482 | Int_t stop=0;
|
---|
483 | Int_t last=0;
|
---|
484 | Int_t first=0;
|
---|
485 |
|
---|
486 | MTime lasttime;
|
---|
487 |
|
---|
488 | TSQLRow *row=0;
|
---|
489 |
|
---|
490 | enum { UNKNOWN, PED=3, CAL=4, DATA=2 };
|
---|
491 | Char_t status = UNKNOWN;
|
---|
492 |
|
---|
493 | Int_t nblocks = 0;
|
---|
494 |
|
---|
495 | while ((row=res->Next()))
|
---|
496 | {
|
---|
497 | if (!(*row)[1])
|
---|
498 | continue;
|
---|
499 |
|
---|
500 | if (start==0)
|
---|
501 | {
|
---|
502 | first = atoi((*row)[0]);
|
---|
503 | if (debug)
|
---|
504 | cout << "First Run: " << first << endl;
|
---|
505 | }
|
---|
506 |
|
---|
507 | switch (atoi((*row)[1]))
|
---|
508 | {
|
---|
509 | case CAL: // ---------- CALIBRATION ----------
|
---|
510 | if (status!=CAL)
|
---|
511 | {
|
---|
512 | start = stop = atoi((*row)[0]);
|
---|
513 | if (!(*row)[2])
|
---|
514 | cout << "No time available... skipped." << endl;
|
---|
515 | else
|
---|
516 | {
|
---|
517 | MTime *tm = new MTime;
|
---|
518 | tm->SetSqlDateTime((*row)[2]);
|
---|
519 | map.Add((ULong_t)map.GetSize(), (Long_t)tm, (Long_t)nblocks);
|
---|
520 | }
|
---|
521 | }
|
---|
522 | status = CAL;
|
---|
523 | break;
|
---|
524 | default:
|
---|
525 | if (status==CAL)
|
---|
526 | {
|
---|
527 | MTime *tm = new MTime(lasttime);
|
---|
528 | map.Add((ULong_t)map.GetSize(), (Long_t)tm, (Long_t)nblocks);
|
---|
529 |
|
---|
530 | stop = last;
|
---|
531 | nblocks++;
|
---|
532 | if (debug)
|
---|
533 | cout << "Cal Block #" << nblocks << " from " << start << " to " << last << endl;
|
---|
534 | }
|
---|
535 | status = UNKNOWN;
|
---|
536 | break;
|
---|
537 | }
|
---|
538 | last = atoi((*row)[0]);
|
---|
539 | lasttime.SetSqlDateTime((*row)[3]);
|
---|
540 | }
|
---|
541 | if (status==CAL)
|
---|
542 | {
|
---|
543 | stop = last;
|
---|
544 | nblocks++;
|
---|
545 | if (debug)
|
---|
546 | cout << "Cal Block #" << nblocks << " from " << start << " to " << stop << endl;
|
---|
547 | }
|
---|
548 |
|
---|
549 | if (debug)
|
---|
550 | cout << "Last Run: " << last << endl;
|
---|
551 | delete res;
|
---|
552 |
|
---|
553 | if (debug)
|
---|
554 | cout << "Found " << nblocks << " calibration blocks" << endl;
|
---|
555 |
|
---|
556 | res = serv.Query(query);
|
---|
557 | if (!res)
|
---|
558 | return kFALSE;
|
---|
559 |
|
---|
560 | Int_t n = -1;
|
---|
561 |
|
---|
562 | Bool_t rc = kTRUE;
|
---|
563 |
|
---|
564 | start = first;
|
---|
565 | while ((row=res->Next()))
|
---|
566 | {
|
---|
567 | if (!(*row)[1])
|
---|
568 | continue;
|
---|
569 |
|
---|
570 | MTime tstart, tstop;
|
---|
571 | tstart.SetSqlDateTime((*row)[2]);
|
---|
572 | tstop.SetSqlDateTime((*row)[3]);
|
---|
573 |
|
---|
574 | MTime min;
|
---|
575 | Int_t nmin = -1;
|
---|
576 | Double_t dmin = 1e35;
|
---|
577 |
|
---|
578 | Long_t key, val;
|
---|
579 | TExMapIter nmap(&map);
|
---|
580 | while (nmap.Next(key, val))
|
---|
581 | {
|
---|
582 | MTime *t = (MTime*)key;
|
---|
583 |
|
---|
584 | if (nmin==-1)
|
---|
585 | {
|
---|
586 | nmin = val;
|
---|
587 | min = *(MTime*)key;
|
---|
588 | dmin = fabs((Double_t)*t-(Double_t)tstart);
|
---|
589 | }
|
---|
590 |
|
---|
591 | if (fabs((Double_t)*t-(Double_t)tstart) < dmin)
|
---|
592 | {
|
---|
593 | nmin = val;
|
---|
594 | dmin = fabs((Double_t)*t-(Double_t)tstart);
|
---|
595 | min = *t;
|
---|
596 | }
|
---|
597 | if (fabs((Double_t)*t-(Double_t)tstop) < dmin)
|
---|
598 | {
|
---|
599 | nmin = val;
|
---|
600 | dmin = fabs((Double_t)*t-(Double_t)tstop);
|
---|
601 | min = *t;
|
---|
602 | }
|
---|
603 | }
|
---|
604 |
|
---|
605 | if (n!=nmin)
|
---|
606 | {
|
---|
607 | if (n!=-1)
|
---|
608 | {
|
---|
609 | if (!NewSequence(serv, datapath, sequpath, start, last, dummy))
|
---|
610 | {
|
---|
611 | rc = kFALSE;
|
---|
612 | //continue;
|
---|
613 | }
|
---|
614 | }
|
---|
615 | n = nmin;
|
---|
616 | start = atoi((*row)[0]);
|
---|
617 | }
|
---|
618 | last = atoi((*row)[0]);
|
---|
619 | }
|
---|
620 |
|
---|
621 | delete res;
|
---|
622 |
|
---|
623 | if (n!=-1 && start!=last)
|
---|
624 | {
|
---|
625 | if (!NewSequence(serv, datapath, sequpath, start, last, dummy))
|
---|
626 | rc = kFALSE;
|
---|
627 | }
|
---|
628 |
|
---|
629 | if (debug)
|
---|
630 | cout << endl;
|
---|
631 |
|
---|
632 | return rc;
|
---|
633 | }
|
---|
634 |
|
---|
635 |
|
---|
636 |
|
---|
637 | int buildsequenceentries(TString day, TString datapath, TString sequpath, Bool_t dummy=kTRUE)
|
---|
638 | {
|
---|
639 | TEnv env("sql.rc");
|
---|
640 |
|
---|
641 | MSQLServer serv(env);
|
---|
642 | if (!serv.IsConnected())
|
---|
643 | {
|
---|
644 | cout << "ERROR - Connection to database failed." << endl;
|
---|
645 | return 0;
|
---|
646 | }
|
---|
647 |
|
---|
648 | cout << "buildsequences" << endl;
|
---|
649 | cout << "--------------" << endl;
|
---|
650 | cout << endl;
|
---|
651 | cout << "Connected to " << serv.GetName() << endl;
|
---|
652 | cout << "Night of sunrise at: " << day << endl;
|
---|
653 | cout << endl;
|
---|
654 |
|
---|
655 | day += " 13:00:00";
|
---|
656 | const TString cond(Form("(fRunStart>ADDDATE(\"%s\", INTERVAL -1 DAY) AND fRunStart<\"%s\")",
|
---|
657 | day.Data(), day.Data()));
|
---|
658 |
|
---|
659 | TString query(Form("SELECT fRunNumber, fSourceKEY, fProjectKEY, fHvSettingsKEY, fLightConditionsKEY, fDiscriminatorThresholdTableKEY, fTriggerDelayTableKEY FROM RunData WHERE %s AND fExcludedFDAKEY=1 order by fRunNumber", cond.Data()));
|
---|
660 |
|
---|
661 | TSQLResult *res = serv.Query(query);
|
---|
662 | if (!res)
|
---|
663 | return 0;
|
---|
664 |
|
---|
665 | TString keys[6]= { "NULL", "NULL", "NULL", "NULL", "NULL", "NULL" };
|
---|
666 | TString stop = "NULL";
|
---|
667 | TString runstart = "NULL";
|
---|
668 | TString runstop = "NULL";
|
---|
669 | Int_t count = 0;
|
---|
670 | TExMap blocks;
|
---|
671 | Int_t runbegin;
|
---|
672 | Int_t runend;
|
---|
673 |
|
---|
674 | TSQLRow *row=0;
|
---|
675 | while ((row=res->Next()))
|
---|
676 | {
|
---|
677 | if (count==0)
|
---|
678 | {
|
---|
679 | for (Int_t i=1 ; i<7 ; i++)
|
---|
680 | keys[i-1]=(*row)[i];
|
---|
681 | runstart=(*row)[0];
|
---|
682 | }
|
---|
683 |
|
---|
684 | for (Int_t i=1 ; i<7 ; i++)
|
---|
685 | {
|
---|
686 | runbegin=atoi(runstart.Data());
|
---|
687 | runend=atoi(runstop.Data());
|
---|
688 | if (i==2 && runbegin>20100 && runend<45100)
|
---|
689 | continue;
|
---|
690 |
|
---|
691 | TString value=(*row)[i];
|
---|
692 | TString key=keys[i-1];
|
---|
693 | if (!value.CompareTo(key))
|
---|
694 | continue;
|
---|
695 |
|
---|
696 | keys[i-1]=value;
|
---|
697 | //hier einfuellen
|
---|
698 | blocks.Add((ULong_t)blocks.GetSize(), (Long_t)runbegin, (Long_t)runend);
|
---|
699 | runstart=(*row)[0];
|
---|
700 | for (Int_t i=1 ; i<7 ; i++)
|
---|
701 | keys[i-1]=(*row)[i];
|
---|
702 | break;
|
---|
703 | }
|
---|
704 | runstop=(*row)[0];
|
---|
705 | count++;
|
---|
706 | }
|
---|
707 |
|
---|
708 | //und hier einfuellen (letzter wert)
|
---|
709 | runbegin=atoi(runstart.Data());
|
---|
710 | runend=atoi(runstop.Data());
|
---|
711 | blocks.Add((ULong_t)blocks.GetSize(), (Long_t)runbegin, (Long_t)runend);
|
---|
712 |
|
---|
713 |
|
---|
714 | Bool_t rc = kTRUE;
|
---|
715 |
|
---|
716 | Long_t key, val;
|
---|
717 | TExMapIter nblocks(&blocks);
|
---|
718 | while (nblocks.Next(key, val))
|
---|
719 | {
|
---|
720 | Int_t runstart2 = (Int_t)key;
|
---|
721 | Int_t runstop2 = (Int_t)val;
|
---|
722 | cout << endl << "datablock from " << runstart2 << " to " << runstop2 << endl;
|
---|
723 |
|
---|
724 | if (!Process(serv, datapath, sequpath, runstart2, runstop2, dummy))
|
---|
725 | rc = kFALSE;
|
---|
726 |
|
---|
727 | }
|
---|
728 | return rc ? 1 : 0;
|
---|
729 | }
|
---|
730 |
|
---|
731 |
|
---|
732 | int buildsequenceentries(TString datapath, TString sequpath, Bool_t dummy=kTRUE)
|
---|
733 | {
|
---|
734 | TEnv env("sql.rc");
|
---|
735 |
|
---|
736 | MSQLServer serv(env);
|
---|
737 | if (!serv.IsConnected())
|
---|
738 | {
|
---|
739 | cout << "ERROR - Connection to database failed." << endl;
|
---|
740 | return 0;
|
---|
741 | }
|
---|
742 |
|
---|
743 | TString query="SELECT fDate FROM SequenceBuildStatus";
|
---|
744 |
|
---|
745 | TSQLResult *res = serv.Query(query);
|
---|
746 | if (!res)
|
---|
747 | return 0;
|
---|
748 |
|
---|
749 | TSQLRow *row=0;
|
---|
750 | while ((row=res->Next()))
|
---|
751 | {
|
---|
752 | cout << "date: " << (*row)[0] << endl;
|
---|
753 | buildsequenceentries((*row)[0], dummy);
|
---|
754 | }
|
---|
755 |
|
---|
756 | return 1;
|
---|
757 | }
|
---|