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 | Int_t rc=0;
|
---|
180 | //rc=0 means sequence not found -> insert
|
---|
181 | //rc=1 means deleting sequence(s) worked -> insert
|
---|
182 | //rc=2 means sequence is still the same -> insert not neccessary
|
---|
183 | //if deleting sequence doesn't work -> return -1
|
---|
184 |
|
---|
185 |
|
---|
186 | //getting # of sequence (in sequDB) between from and to
|
---|
187 | TString query("SELECT fSequenceFirst FROM Sequences WHERE fSequenceFirst ");
|
---|
188 | query += Form("BETWEEN %d and %d OR fSequenceLast BETWEEN %d and %d",
|
---|
189 | from, to, from, to);
|
---|
190 |
|
---|
191 | TSQLResult *res = serv.Query(query);
|
---|
192 | if (!res)
|
---|
193 | return -1;
|
---|
194 |
|
---|
195 | TArrayI sequences;
|
---|
196 | Int_t numsequ=0;
|
---|
197 |
|
---|
198 | TSQLRow *row=0;
|
---|
199 | while ((row=res->Next()))
|
---|
200 | {
|
---|
201 | numsequ++;
|
---|
202 | sequences.Set(numsequ);
|
---|
203 | sequences.AddAt(atoi((*row)[0]), numsequ-1);
|
---|
204 | }
|
---|
205 | delete res;
|
---|
206 |
|
---|
207 | //if there's no sequence in the table Sequences -> check other tables
|
---|
208 | //if there's one sequence -> check if the sequence is identical
|
---|
209 | //if there are more sequences -> delete them
|
---|
210 | switch (numsequ)
|
---|
211 | {
|
---|
212 | case 0:
|
---|
213 | cout << "found no sequence in Sequ-DB -> check other tables" << endl;
|
---|
214 | cout << " deleting every sequence found in Calibration, Star or SequenceProcessStatus between "
|
---|
215 | << from << " and " << to << endl;
|
---|
216 |
|
---|
217 | //calibration table
|
---|
218 | query(Form("SELECT fSequenceFirst FROM Calibration WHERE fSequenceFirst BETWEEN %d and %d", from, to));
|
---|
219 | res = serv.Query(query);
|
---|
220 | if (!res)
|
---|
221 | return -1;
|
---|
222 | row=0;
|
---|
223 | while ((row=res->Next()))
|
---|
224 | {
|
---|
225 | if (!DeleteSequence(serv, datapath, sequpath, atoi((*row)[0]), dummy))
|
---|
226 | return -1;
|
---|
227 | else
|
---|
228 | rc=1;
|
---|
229 | }
|
---|
230 | delete res;
|
---|
231 |
|
---|
232 | //Star table
|
---|
233 | query(Form("SELECT fSequenceFirst FROM Star WHERE fSequenceFirst BETWEEN %d and %d", from, to));
|
---|
234 | res = serv.Query(query);
|
---|
235 | if (!res)
|
---|
236 | return -1;
|
---|
237 | row=0;
|
---|
238 | while ((row=res->Next()))
|
---|
239 | {
|
---|
240 | if (!DeleteSequence(serv, datapath, sequpath, atoi((*row)[0]), dummy))
|
---|
241 | return -1;
|
---|
242 | else
|
---|
243 | rc=1;
|
---|
244 | }
|
---|
245 | delete res;
|
---|
246 |
|
---|
247 | //SequenceProcessStatus table
|
---|
248 | query(Form("SELECT fSequenceFirst FROM SequenceProcessStatus WHERE fSequenceFirst BETWEEN %d and %d", from, to));
|
---|
249 | res = serv.Query(query);
|
---|
250 | if (!res)
|
---|
251 | return -1;
|
---|
252 | row=0;
|
---|
253 | while ((row=res->Next()))
|
---|
254 | {
|
---|
255 | if (!DeleteSequence(serv, datapath, sequpath, atoi((*row)[0]), dummy))
|
---|
256 | return -1;
|
---|
257 | else
|
---|
258 | rc=1;
|
---|
259 | }
|
---|
260 | delete res;
|
---|
261 | break;
|
---|
262 |
|
---|
263 | case 1:
|
---|
264 | cout << "found 1 sequence: " << sequences.At(0) << " -> check sequ# " << endl;
|
---|
265 | if (sequences.At(0)!=from)
|
---|
266 | {
|
---|
267 | if (!DeleteSequence(serv, datapath, sequpath, sequences.At(0), dummy))
|
---|
268 | return -1;
|
---|
269 | else
|
---|
270 | rc=1;
|
---|
271 | }
|
---|
272 | else
|
---|
273 | {
|
---|
274 | cout << "sequence# is the same -> checking the runs " << endl;
|
---|
275 |
|
---|
276 | //getting olf runs
|
---|
277 | query(Form("SELECT fRunNumber FROM RunData WHERE fSequenceFirst=%d ", from));
|
---|
278 | res = serv.Query(query);
|
---|
279 | if (!res)
|
---|
280 | return -1;
|
---|
281 |
|
---|
282 | TArrayI oldruns;
|
---|
283 | Int_t count=0;
|
---|
284 | row=0;
|
---|
285 | while ((row=res->Next()))
|
---|
286 | {
|
---|
287 | count++;
|
---|
288 | oldruns.Set(count);
|
---|
289 | oldruns.AddAt(atoi((*row)[0]), count-1);
|
---|
290 | }
|
---|
291 | delete res;
|
---|
292 |
|
---|
293 | //getting new runs
|
---|
294 | query(Form("SELECT fRunNumber FROM RunData WHERE fRunNumber BETWEEN %d and %d AND fExcludedFDAKEY=1", from, to));
|
---|
295 | res = serv.Query(query);
|
---|
296 | if (!res)
|
---|
297 | return -1;
|
---|
298 | TArrayI newruns;
|
---|
299 | count=0;
|
---|
300 | row=0;
|
---|
301 | while ((row=res->Next()))
|
---|
302 | {
|
---|
303 | count++;
|
---|
304 | oldruns.Set(count);
|
---|
305 | oldruns.AddAt(atoi((*row)[0]), count-1);
|
---|
306 | }
|
---|
307 | delete res;
|
---|
308 |
|
---|
309 | //comparing old and new runs (first the # of runs, if it is the same, also the single runnumbers
|
---|
310 | if (oldruns.GetSize()!=newruns.GetSize())
|
---|
311 | {
|
---|
312 | cout << " number of runs is not the same -> deleting sequence " << sequences.At(0) << endl;
|
---|
313 | if (!DeleteSequence(serv, datapath, sequpath, sequences.At(0), dummy))
|
---|
314 | return -1;
|
---|
315 | else
|
---|
316 | rc=1;
|
---|
317 | }
|
---|
318 | else
|
---|
319 | {
|
---|
320 | cout << " number of runs is the same -> checking the single runnumbers " << endl;
|
---|
321 |
|
---|
322 | for (Int_t i=0;i<newruns.GetSize();i++)
|
---|
323 | {
|
---|
324 | if (newruns.At(i)==oldruns.At(i))
|
---|
325 | continue;
|
---|
326 |
|
---|
327 | cout << i << ". run is not the same ( " << oldruns.At(i) << " -- " << newruns.At(i)
|
---|
328 | << ") -> deleting sequence " << sequences.At(0) << endl;
|
---|
329 | if (!DeleteSequence(serv, datapath, sequpath, sequences.At(0), dummy))
|
---|
330 | return -1;
|
---|
331 | else
|
---|
332 | rc=1;
|
---|
333 | break;
|
---|
334 | }
|
---|
335 | }
|
---|
336 | }
|
---|
337 | rc=2;
|
---|
338 | break;
|
---|
339 |
|
---|
340 | default:
|
---|
341 | cout << "found " << numsequ << " sequences -> deleting them " << endl;
|
---|
342 |
|
---|
343 | for (Int_t i=0;i<sequences.GetSize();i++)
|
---|
344 | {
|
---|
345 | cout << "deleting sequence " << sequences.At(i) << "... <" << i << ">" << endl;
|
---|
346 | if (!DeleteSequence(serv, datapath, sequpath, sequences.At(i), dummy))
|
---|
347 | return -1;
|
---|
348 | else
|
---|
349 | rc=1;
|
---|
350 | }
|
---|
351 | }
|
---|
352 |
|
---|
353 | return rc;
|
---|
354 | }
|
---|
355 |
|
---|
356 | Bool_t InsertSequence(MSQLServer &serv, Int_t from, Int_t to)
|
---|
357 | {
|
---|
358 |
|
---|
359 | cout << "Inserting sequence " << from << " ... " << endl;
|
---|
360 |
|
---|
361 | // ========== Request number of events ==========
|
---|
362 | TString query("SELECT SUM(fNumEvents), "
|
---|
363 | " SUM(if(TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)<0,"
|
---|
364 | " TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)+24*60*60,"
|
---|
365 | " TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart))), ");
|
---|
366 | query += " MIN(fZenithDistance), MAX(fZenithDistance), ";
|
---|
367 | query += " MIN(fAzimuth), MAX(fAzimuth) ";
|
---|
368 | query += Form(" FROM RunData WHERE fRunTypeKEY=2 AND "
|
---|
369 | " (fRunNumber BETWEEN %d AND %d) AND fExcludedFDAKEY=1",
|
---|
370 | from, to);
|
---|
371 |
|
---|
372 | TSQLResult *res = serv.Query(query);
|
---|
373 | if (!res)
|
---|
374 | return kFALSE;
|
---|
375 |
|
---|
376 | TSQLRow *row = res->Next();
|
---|
377 | if (!row || !(*row)[0])
|
---|
378 | {
|
---|
379 | cout << "ERROR - No result from query: " << query << endl;
|
---|
380 | return kFALSE;
|
---|
381 | }
|
---|
382 |
|
---|
383 | TString nevts = (*row)[0];
|
---|
384 | TString secs = (*row)[1];
|
---|
385 | TString zdmin = (*row)[2];
|
---|
386 | TString zdmax = (*row)[3];
|
---|
387 | TString azmin = (*row)[4];
|
---|
388 | TString azmax = (*row)[5];
|
---|
389 |
|
---|
390 | delete res;
|
---|
391 |
|
---|
392 | // ========== Request start time of sequence ==========
|
---|
393 | query = Form("SELECT fRunStart FROM RunData WHERE fRunNumber=%d AND fExcludedFDAKEY=1", from);
|
---|
394 |
|
---|
395 | res = serv.Query(query);
|
---|
396 | if (!res)
|
---|
397 | return kFALSE;
|
---|
398 |
|
---|
399 | row = res->Next();
|
---|
400 | if (!row || !(*row)[0])
|
---|
401 | {
|
---|
402 | cout << "ERROR - No result from query: " << query << endl;
|
---|
403 | return kFALSE;
|
---|
404 | }
|
---|
405 |
|
---|
406 | TString start((*row)[0]);
|
---|
407 |
|
---|
408 | delete res;
|
---|
409 |
|
---|
410 | // ========== Request data of sequence ==========
|
---|
411 | query = Form("SELECT fSourceKEY, fProjectKEY, "
|
---|
412 | " fL1TriggerTableKEY, fL1TriggerTableKEY,"
|
---|
413 | " fHvSettingsKEY, fDiscriminatorThresholdTableKEY,"
|
---|
414 | " fTriggerDelayTableKEY, fLightConditionsKEY, fTestFlagKEY"
|
---|
415 | " FROM RunData"
|
---|
416 | " WHERE fRunTypeKEY=2 AND fExcludedFDAKEY=1 AND (fRunNumber BETWEEN %d AND %d)"
|
---|
417 | " LIMIT 1", from, to);
|
---|
418 |
|
---|
419 | res = serv.Query(query);
|
---|
420 | if (!res)
|
---|
421 | return kFALSE;
|
---|
422 |
|
---|
423 | row = res->Next();
|
---|
424 | if (!row)
|
---|
425 | {
|
---|
426 | cout << "ERROR - No result from query: " << query << endl;
|
---|
427 | return kFALSE;
|
---|
428 | }
|
---|
429 |
|
---|
430 | TString query1("INSERT Sequences SET");
|
---|
431 | query1+=Form(" fSequenceFirst=%d, fSequenceLast=%d,", from, to);
|
---|
432 | query1+=Form(" fSourceKEY=%s,", (*row)[0]);
|
---|
433 | query1+=Form(" fProjectKEY=%s,", (*row)[1]);
|
---|
434 | query1+=Form(" fNumEvents=%s,", nevts.Data());
|
---|
435 | query1+=Form(" fRunTime=%s,", secs.Data());
|
---|
436 | query1+=Form(" fRunStart=\"%s\",", start.Data());
|
---|
437 | query1+=Form(" fZenithDistanceMin=%s,", zdmin.Data());
|
---|
438 | query1+=Form(" fZenithDistanceMax=%s,", zdmax.Data());
|
---|
439 | query1+=Form(" fAzimuthMin=%s,", azmin.Data());
|
---|
440 | query1+=Form(" fAzimuthMax=%s,", azmax.Data());
|
---|
441 | query1+=Form(" fL1TriggerTableKEY=%s,", (*row)[2]);
|
---|
442 | query1+=Form(" fL2TriggerTableKEY=%s,", (*row)[3]);
|
---|
443 | query1+=Form(" fHvSettingsKEY=%s,", (*row)[4]);
|
---|
444 | query1+=Form(" fDiscriminatorThresholdTableKEY=%s,", (*row)[5]);
|
---|
445 | query1+=Form(" fTriggerDelayTableKEY=%s,", (*row)[6]);
|
---|
446 | query1+=Form(" fLightConditionsKEY=%s,", (*row)[7]);
|
---|
447 | query1+=Form(" fTestFlagKEY=%s, fManuallyChangedKEY=1", (*row)[8]);
|
---|
448 |
|
---|
449 |
|
---|
450 | TString query2 = Form("UPDATE RunData SET fSequenceFirst=%d WHERE"
|
---|
451 | " (fRunNumber BETWEEN %d AND %d) AND"
|
---|
452 | " (fRunTypeKEY BETWEEN 2 AND 4) AND"
|
---|
453 | " fSourceKEY=%s AND fHvSettingsKEY=%s AND fExcludedFDAKEY=1",
|
---|
454 | from, from, to, (*row)[1], (*row)[4]);
|
---|
455 |
|
---|
456 | TString query3 = Form("INSERT SequenceProcessStatus SET fSequenceFirst=%d ", from);
|
---|
457 |
|
---|
458 | delete res;
|
---|
459 |
|
---|
460 | cout << "q1: " << query1 << endl;
|
---|
461 | cout << "q2: " << query2 << endl;
|
---|
462 | cout << "q3: " << query3 << endl;
|
---|
463 |
|
---|
464 | res = serv.Query(query1);
|
---|
465 | if (!res)
|
---|
466 | {
|
---|
467 | cout << "ERROR - Could not insert Sequence into Sequences." << endl;
|
---|
468 | return kFALSE;
|
---|
469 | }
|
---|
470 | delete res;
|
---|
471 |
|
---|
472 | res = serv.Query(query2);
|
---|
473 | if (!res)
|
---|
474 | {
|
---|
475 | cout << "ERROR - Could not update RunData." << endl;
|
---|
476 | return kFALSE;
|
---|
477 | }
|
---|
478 | delete res;
|
---|
479 |
|
---|
480 | res = serv.Query(query3);
|
---|
481 | if (!res)
|
---|
482 | {
|
---|
483 | cout << "ERROR - Could not insert Sequence into SequenceProcessStatus." << endl;
|
---|
484 | return kFALSE;
|
---|
485 | }
|
---|
486 | delete res;
|
---|
487 |
|
---|
488 | return kTRUE;
|
---|
489 | }
|
---|
490 |
|
---|
491 | Bool_t NewSequence(MSQLServer &serv, TString datapath, TString sequpath, Int_t from, Int_t to, Bool_t dummy)
|
---|
492 | {
|
---|
493 | cout << "Found Sequence (" << from << ", " << to << ") ... checking runs..." << flush;
|
---|
494 |
|
---|
495 | if (!CheckRuns(serv, from, to, 2))
|
---|
496 | {
|
---|
497 | cout << "Warning - Found inconsistency in data-runs (" << from << ", " << to << ")" << endl;
|
---|
498 | //sequence is not built, but kTRUE is returned, to allow
|
---|
499 | //the automatic processing of the other sequences of this day
|
---|
500 | return kTRUE;
|
---|
501 | }
|
---|
502 | if (!CheckRuns(serv, from, to, 3))
|
---|
503 | {
|
---|
504 | cout << "Warning - Found inconsistency in ped-runs (" << from << ", " << to << ")" << endl;
|
---|
505 | //sequence is not built, but kTRUE is returned, to allow
|
---|
506 | //the automatic processing of the other sequences of this day
|
---|
507 | return kTRUE;
|
---|
508 | }
|
---|
509 |
|
---|
510 | cout << "ok." << endl;
|
---|
511 |
|
---|
512 |
|
---|
513 | cout << "checking Sequence..." << endl;
|
---|
514 |
|
---|
515 | Bool_t rc=kFALSE;
|
---|
516 | switch (CheckSequence(serv, datapath, sequpath, from, to, dummy))
|
---|
517 | {
|
---|
518 | case 0:
|
---|
519 | cout << " sequence not found -> inserting " << from << flush ;
|
---|
520 | if (dummy)
|
---|
521 | {
|
---|
522 | cout << " <dummy> " << endl;
|
---|
523 | return kTRUE;
|
---|
524 | }
|
---|
525 | cout << endl;
|
---|
526 | return InsertSequence(serv, from, to);
|
---|
527 |
|
---|
528 | case 1:
|
---|
529 | cout << " deleting successfully finished -> inserting sequence " << from << flush;
|
---|
530 | if (dummy)
|
---|
531 | {
|
---|
532 | cout << " <dummy> " << endl;
|
---|
533 | return kTRUE;
|
---|
534 | }
|
---|
535 | cout << endl;
|
---|
536 | return InsertSequence(serv, from, to);
|
---|
537 |
|
---|
538 | case 2:
|
---|
539 | cout << " sequence is already existing -> inserting not necessary" << endl;
|
---|
540 | return kTRUE;
|
---|
541 |
|
---|
542 | case -1:
|
---|
543 | cout << " deleting went wrong " << endl;
|
---|
544 | return kFALSE;
|
---|
545 | }
|
---|
546 |
|
---|
547 |
|
---|
548 | return rc;
|
---|
549 | }
|
---|
550 |
|
---|
551 | Bool_t Process(MSQLServer &serv, TString datapath, TString sequpath, Int_t from, Int_t to, Bool_t dummy)
|
---|
552 | {
|
---|
553 |
|
---|
554 | TString query(Form("SELECT fRunNumber, fRunTypeKEY, fRunStart, fRunStop"
|
---|
555 | " FROM RunData"
|
---|
556 | " WHERE fRunNumber BETWEEN %d AND %d AND "
|
---|
557 | " fExcludedFDAKEY=1 AND (fRunTypeKEY BETWEEN 2 AND 4)"
|
---|
558 | " ORDER BY fRunNumber", from, to));
|
---|
559 |
|
---|
560 | TSQLResult *res = serv.Query(query);
|
---|
561 | if (!res)
|
---|
562 | return kFALSE;
|
---|
563 |
|
---|
564 | TExMap map;
|
---|
565 |
|
---|
566 | Int_t start=0;
|
---|
567 | Int_t stop=0;
|
---|
568 | Int_t last=0;
|
---|
569 | Int_t first=0;
|
---|
570 |
|
---|
571 | MTime lasttime;
|
---|
572 |
|
---|
573 | TSQLRow *row=0;
|
---|
574 |
|
---|
575 | enum { UNKNOWN, PED=3, CAL=4, DATA=2 };
|
---|
576 | Char_t status = UNKNOWN;
|
---|
577 |
|
---|
578 | Int_t nblocks = 0;
|
---|
579 |
|
---|
580 | while ((row=res->Next()))
|
---|
581 | {
|
---|
582 | if (!(*row)[1])
|
---|
583 | continue;
|
---|
584 |
|
---|
585 | if (start==0)
|
---|
586 | {
|
---|
587 | first = atoi((*row)[0]);
|
---|
588 | if (debug)
|
---|
589 | cout << "First Run: " << first << endl;
|
---|
590 | }
|
---|
591 |
|
---|
592 | switch (atoi((*row)[1]))
|
---|
593 | {
|
---|
594 | case CAL: // ---------- CALIBRATION ----------
|
---|
595 | if (status!=CAL)
|
---|
596 | {
|
---|
597 | start = stop = atoi((*row)[0]);
|
---|
598 | if (!(*row)[2])
|
---|
599 | cout << "No time available... skipped." << endl;
|
---|
600 | else
|
---|
601 | {
|
---|
602 | MTime *tm = new MTime;
|
---|
603 | tm->SetSqlDateTime((*row)[2]);
|
---|
604 | map.Add((ULong_t)map.GetSize(), (Long_t)tm, (Long_t)nblocks);
|
---|
605 | }
|
---|
606 | }
|
---|
607 | status = CAL;
|
---|
608 | break;
|
---|
609 | default:
|
---|
610 | if (status==CAL)
|
---|
611 | {
|
---|
612 | MTime *tm = new MTime(lasttime);
|
---|
613 | map.Add((ULong_t)map.GetSize(), (Long_t)tm, (Long_t)nblocks);
|
---|
614 |
|
---|
615 | stop = last;
|
---|
616 | nblocks++;
|
---|
617 | if (debug)
|
---|
618 | cout << "Cal Block #" << nblocks << " from " << start << " to " << last << endl;
|
---|
619 | }
|
---|
620 | status = UNKNOWN;
|
---|
621 | break;
|
---|
622 | }
|
---|
623 | last = atoi((*row)[0]);
|
---|
624 | lasttime.SetSqlDateTime((*row)[3]);
|
---|
625 | }
|
---|
626 | if (status==CAL)
|
---|
627 | {
|
---|
628 | stop = last;
|
---|
629 | nblocks++;
|
---|
630 | if (debug)
|
---|
631 | cout << "Cal Block #" << nblocks << " from " << start << " to " << stop << endl;
|
---|
632 | }
|
---|
633 |
|
---|
634 | if (debug)
|
---|
635 | cout << "Last Run: " << last << endl;
|
---|
636 | delete res;
|
---|
637 |
|
---|
638 | if (debug)
|
---|
639 | cout << "Found " << nblocks << " calibration blocks" << endl;
|
---|
640 |
|
---|
641 | res = serv.Query(query);
|
---|
642 | if (!res)
|
---|
643 | return kFALSE;
|
---|
644 |
|
---|
645 | Int_t n = -1;
|
---|
646 |
|
---|
647 | Bool_t rc = kTRUE;
|
---|
648 |
|
---|
649 | start = first;
|
---|
650 | while ((row=res->Next()))
|
---|
651 | {
|
---|
652 | if (!(*row)[1])
|
---|
653 | continue;
|
---|
654 |
|
---|
655 | MTime tstart, tstop;
|
---|
656 | tstart.SetSqlDateTime((*row)[2]);
|
---|
657 | tstop.SetSqlDateTime((*row)[3]);
|
---|
658 |
|
---|
659 | MTime min;
|
---|
660 | Int_t nmin = -1;
|
---|
661 | Double_t dmin = 1e35;
|
---|
662 |
|
---|
663 | Long_t key, val;
|
---|
664 | TExMapIter nmap(&map);
|
---|
665 | while (nmap.Next(key, val))
|
---|
666 | {
|
---|
667 | MTime *t = (MTime*)key;
|
---|
668 |
|
---|
669 | if (nmin==-1)
|
---|
670 | {
|
---|
671 | nmin = val;
|
---|
672 | min = *(MTime*)key;
|
---|
673 | dmin = fabs((Double_t)*t-(Double_t)tstart);
|
---|
674 | }
|
---|
675 |
|
---|
676 | if (fabs((Double_t)*t-(Double_t)tstart) < dmin)
|
---|
677 | {
|
---|
678 | nmin = val;
|
---|
679 | dmin = fabs((Double_t)*t-(Double_t)tstart);
|
---|
680 | min = *t;
|
---|
681 | }
|
---|
682 | if (fabs((Double_t)*t-(Double_t)tstop) < dmin)
|
---|
683 | {
|
---|
684 | nmin = val;
|
---|
685 | dmin = fabs((Double_t)*t-(Double_t)tstop);
|
---|
686 | min = *t;
|
---|
687 | }
|
---|
688 | }
|
---|
689 |
|
---|
690 | if (n!=nmin)
|
---|
691 | {
|
---|
692 | if (n!=-1)
|
---|
693 | {
|
---|
694 | if (!NewSequence(serv, datapath, sequpath, start, last, dummy))
|
---|
695 | {
|
---|
696 | rc = kFALSE;
|
---|
697 | //continue;
|
---|
698 | }
|
---|
699 | }
|
---|
700 | n = nmin;
|
---|
701 | start = atoi((*row)[0]);
|
---|
702 | }
|
---|
703 | last = atoi((*row)[0]);
|
---|
704 | }
|
---|
705 |
|
---|
706 | delete res;
|
---|
707 |
|
---|
708 | if (n!=-1 && start!=last)
|
---|
709 | {
|
---|
710 | if (!NewSequence(serv, datapath, sequpath, start, last, dummy))
|
---|
711 | rc = kFALSE;
|
---|
712 | }
|
---|
713 |
|
---|
714 | if (debug)
|
---|
715 | cout << endl;
|
---|
716 |
|
---|
717 | return rc;
|
---|
718 | }
|
---|
719 |
|
---|
720 |
|
---|
721 |
|
---|
722 | int buildsequenceentries(TString day, TString datapath, TString sequpath, Bool_t dummy=kTRUE)
|
---|
723 | {
|
---|
724 | TEnv env("sql.rc");
|
---|
725 |
|
---|
726 | MSQLServer serv(env);
|
---|
727 | if (!serv.IsConnected())
|
---|
728 | {
|
---|
729 | cout << "ERROR - Connection to database failed." << endl;
|
---|
730 | return 0;
|
---|
731 | }
|
---|
732 |
|
---|
733 | cout << "buildsequences" << endl;
|
---|
734 | cout << "--------------" << endl;
|
---|
735 | cout << endl;
|
---|
736 | cout << "Connected to " << serv.GetName() << endl;
|
---|
737 | cout << "Night of sunrise at: " << day << endl;
|
---|
738 | cout << endl;
|
---|
739 |
|
---|
740 | day += " 13:00:00";
|
---|
741 | const TString cond(Form("(fRunStart>ADDDATE(\"%s\", INTERVAL -1 DAY) AND fRunStart<\"%s\")",
|
---|
742 | day.Data(), day.Data()));
|
---|
743 |
|
---|
744 | TString query(Form("SELECT fRunNumber, fSourceKEY, fProjectKEY, fHvSettingsKEY, fLightConditionsKEY, fDiscriminatorThresholdTableKEY, fTriggerDelayTableKEY FROM RunData WHERE %s AND fExcludedFDAKEY=1 order by fRunNumber", cond.Data()));
|
---|
745 |
|
---|
746 | TSQLResult *res = serv.Query(query);
|
---|
747 | if (!res)
|
---|
748 | return 0;
|
---|
749 |
|
---|
750 | TString keys[6]= { "NULL", "NULL", "NULL", "NULL", "NULL", "NULL" };
|
---|
751 | TString stop = "NULL";
|
---|
752 | TString runstart = "NULL";
|
---|
753 | TString runstop = "NULL";
|
---|
754 | Int_t count = 0;
|
---|
755 | TExMap blocks;
|
---|
756 | Int_t runbegin;
|
---|
757 | Int_t runend;
|
---|
758 |
|
---|
759 | TSQLRow *row=0;
|
---|
760 | while ((row=res->Next()))
|
---|
761 | {
|
---|
762 | if (count==0)
|
---|
763 | {
|
---|
764 | for (Int_t i=1 ; i<7 ; i++)
|
---|
765 | keys[i-1]=(*row)[i];
|
---|
766 | runstart=(*row)[0];
|
---|
767 | }
|
---|
768 |
|
---|
769 | for (Int_t i=1 ; i<7 ; i++)
|
---|
770 | {
|
---|
771 | runbegin=atoi(runstart.Data());
|
---|
772 | runend=atoi(runstop.Data());
|
---|
773 | if (i==2 && runbegin>20100 && runend<45100)
|
---|
774 | continue;
|
---|
775 |
|
---|
776 | TString value=(*row)[i];
|
---|
777 | TString key=keys[i-1];
|
---|
778 | if (!value.CompareTo(key))
|
---|
779 | continue;
|
---|
780 |
|
---|
781 | keys[i-1]=value;
|
---|
782 | //hier einfuellen
|
---|
783 | blocks.Add((ULong_t)blocks.GetSize(), (Long_t)runbegin, (Long_t)runend);
|
---|
784 | runstart=(*row)[0];
|
---|
785 | for (Int_t i=1 ; i<7 ; i++)
|
---|
786 | keys[i-1]=(*row)[i];
|
---|
787 | break;
|
---|
788 | }
|
---|
789 | runstop=(*row)[0];
|
---|
790 | count++;
|
---|
791 | }
|
---|
792 |
|
---|
793 | //und hier einfuellen (letzter wert)
|
---|
794 | runbegin=atoi(runstart.Data());
|
---|
795 | runend=atoi(runstop.Data());
|
---|
796 | blocks.Add((ULong_t)blocks.GetSize(), (Long_t)runbegin, (Long_t)runend);
|
---|
797 |
|
---|
798 |
|
---|
799 | Bool_t rc = kTRUE;
|
---|
800 |
|
---|
801 | Long_t key, val;
|
---|
802 | TExMapIter nblocks(&blocks);
|
---|
803 | while (nblocks.Next(key, val))
|
---|
804 | {
|
---|
805 | Int_t runstart2 = (Int_t)key;
|
---|
806 | Int_t runstop2 = (Int_t)val;
|
---|
807 | cout << endl << "datablock from " << runstart2 << " to " << runstop2 << endl;
|
---|
808 |
|
---|
809 | if (!Process(serv, datapath, sequpath, runstart2, runstop2, dummy))
|
---|
810 | rc = kFALSE;
|
---|
811 |
|
---|
812 | }
|
---|
813 | return rc ? 1 : 0;
|
---|
814 | }
|
---|
815 |
|
---|
816 |
|
---|
817 | int buildsequenceentries(TString datapath, TString sequpath, Bool_t dummy=kTRUE)
|
---|
818 | {
|
---|
819 | TEnv env("sql.rc");
|
---|
820 |
|
---|
821 | MSQLServer serv(env);
|
---|
822 | if (!serv.IsConnected())
|
---|
823 | {
|
---|
824 | cout << "ERROR - Connection to database failed." << endl;
|
---|
825 | return 0;
|
---|
826 | }
|
---|
827 |
|
---|
828 | TString query="SELECT fDate FROM SequenceBuildStatus";
|
---|
829 |
|
---|
830 | TSQLResult *res = serv.Query(query);
|
---|
831 | if (!res)
|
---|
832 | return 0;
|
---|
833 |
|
---|
834 | TSQLRow *row=0;
|
---|
835 | while ((row=res->Next()))
|
---|
836 | {
|
---|
837 | cout << "date: " << (*row)[0] << endl;
|
---|
838 | buildsequenceentries((*row)[0], dummy);
|
---|
839 | }
|
---|
840 |
|
---|
841 | return 1;
|
---|
842 | }
|
---|