source: trunk/MagicSoft/Mars/datacenter/macros/buildsequenceentries.C@ 7503

Last change on this file since 7503 was 7460, checked in by Daniela Dorner, 19 years ago
*** empty log message ***
File size: 29.3 KB
Line 
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-2006
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// if sequence is okay:
39// - check if in the range of the runnumbers of this sequence other sequences
40// exist in the database
41// if there are no sequences, insert the new sequence, else:
42// - delete overlaping sequences
43// if there's only one sequence in the same runnumber range:
44// - check if the new and the old sequence are identical
45// if they are identical, do nothing, if not, delete the old sequence and
46// insert the new one
47//
48// remark: deleting sequences includes the following steps:
49// - delete entries from the tables Sequences, SequenceProcessStatus,
50// Calibration and Star
51// - updating the sequence number (fSequenceFirst) in the table RunData
52// - remove the Sequence File, the calibrated data and the image files from
53// the disk
54//
55// the macro can be executed either for all nights or for one single night
56// .x buildsequenceentries.C+( "datapath", "sequpath", Bool_t dummy=kTRUE)
57// .x buildsequenceentries.C+( "night", "datapath", "sequpath")
58//
59// the Bool_t dummy:
60// kTRUE: dummy-mode, i.e. nothing is inserted into the database, but the
61// commands, that would be executed are returned
62// kFALSE: the information is inserted into the database and the files of
63// removed sequences is deleted
64// be careful with this option - for tests use always kTRUE
65//
66// TString datapath, TString sequpath:
67// datapath: path, where the processed data is stored in the datacenter
68// sequpath: path, where the sequence files are stored in the datacenter
69// the datapath (standard: /magic/data/) and the sequencepath (standard:
70// /magic/sequences) have to be given, that the sequence file, the
71// calibrated data and the star files can be removed, when an old sequence
72// has to be removed from the database
73//
74// If nothing failes 1 is returned. In the case of an error 0 is returned.
75// This is needed for the scripts that execute the macro.
76//
77/////////////////////////////////////////////////////////////////////////////
78#include <iostream>
79#include <iomanip>
80#include <fstream>
81
82#include <MSQLServer.h>
83#include <TSQLRow.h>
84#include <TSQLResult.h>
85
86#include <TEnv.h>
87#include <TMath.h>
88#include <TExMap.h>
89#include <TArrayI.h>
90#include <TRegexp.h>
91#include <TSystem.h>
92#include <TObjString.h>
93
94#include <MTime.h>
95#include <MDirIter.h>
96
97using namespace std;
98
99int debug = 0;
100
101Bool_t DeleteSequence(MSQLServer &serv, TString datapath, TString sequpath, Int_t sequ, Bool_t dummy)
102{
103 //queries to delete information from the database
104 TString query1(Form("DELETE FROM Calibration WHERE fSequenceFirst=%d", sequ));
105 TString query2(Form("DELETE FROM Star WHERE fSequenceFirst=%d", sequ));
106 TString query3(Form("DELETE FROM SequenceProcessStatus WHERE fSequenceFirst=%d", sequ));
107 TString query4(Form("UPDATE RunData SET fSequenceFirst=0 WHERE fSequenceFirst=%d", sequ));
108 TString query5(Form("DELETE FROM Sequences WHERE fSequenceFirst=%d AND fManuallyChangedKEY=1", sequ));
109
110 //commands to delete files from the disk
111 TString fname(Form("%s/%04d/sequence%08d.txt", sequpath.Data(),sequ/10000, sequ));
112 TString command(Form("rm -r %s/callisto/%04d/%08d/", datapath.Data(), sequ/10000, sequ));
113 TString command2(Form("rm -r %s/star/%04d/%08d/", datapath.Data(), sequ/10000, sequ));
114
115 if (dummy)
116 {
117 cout << "not using dummy=kTRUE the following commands would be executed: " << endl;
118 cout << "queries: " << endl;
119 cout << query1 << endl;
120 cout << query2 << endl;
121 cout << query3 << endl;
122 cout << query4 << endl;
123 cout << query5 << endl;
124 cout << "removing files:" << endl;
125 cout << "unlink " << fname << endl;
126 cout << command << endl;
127 cout << command2 << endl;
128 return kTRUE;
129 }
130
131 TSQLResult *res = serv.Query(query1);
132 if (!res)
133 return kFALSE;
134 delete res;
135
136 res = serv.Query(query2);
137 if (!res)
138 return kFALSE;
139 delete res;
140
141 res = serv.Query(query3);
142 if (!res)
143 return kFALSE;
144 delete res;
145
146 res = serv.Query(query4);
147 if (!res)
148 return kFALSE;
149 delete res;
150
151 res = serv.Query(query5);
152 if (!res)
153 return kFALSE;
154 delete res;
155
156 gSystem->Unlink(fname);
157
158 gSystem->Exec(command);
159 gSystem->Exec(command2);
160
161 return kTRUE;
162}
163
164Int_t DoCheck(TSQLResult &res)
165{
166 TArrayI data(5);
167 Int_t n = 0;
168
169 TSQLRow *row=0;
170 while ((row=res.Next()))
171 {
172 n++;
173
174 if (data[0]==0)
175 {
176 for (int i=0; i<data.GetSize(); i++)
177 data[i] = atoi((*row)[i]);
178 continue;
179 }
180
181 for (int i=1; i<data.GetSize(); i++)
182 {
183 if (data[i] != atoi((*row)[i]))
184 return i+1;
185 }
186 }
187 return n==0 ? 0 : -1;
188}
189
190
191//check values, that can be different for different runtypes
192Bool_t CheckRuns(MSQLServer &serv, Int_t from, Int_t to, Int_t type)
193{
194 TString query("SELECT fRunNumber, fL1TriggerTableKEY, fL2TriggerTableKEY,"
195 " fCalibrationScriptKEY, fProjectKEY FROM RunData");
196 query += Form(" WHERE fRunTypeKEY=%d AND fExcludedFDAKEY=1 AND "
197 " (fRunNumber BETWEEN %d AND %d)"
198 " ORDER BY fRunNumber", type, from, to);
199
200 TSQLResult *res = serv.Query(query);
201 if (!res)
202 return kFALSE;
203
204 Int_t rc = DoCheck(*res);
205 delete res;
206
207 switch (rc)
208 {
209 case 0: cout << "ERROR - No runs found for check!" << endl; break;
210 case 1: cout << "ERROR - fRunNumber doesn't match!" << endl; break;
211 case 2: cout << "ERROR - fL1TriggerTableKEY doesn't match!" << endl; break;
212 case 3: cout << "ERROR - fL2TriggerTableKEY doesn't match!" << endl; break;
213 case 4: cout << "ERROR - fCalibrationScriptKEY doesn't match!" << endl; break;
214 case 5: cout << "ERROR - fProjectKEY doesn't match!" << endl; break;
215 }
216
217 return rc<0;
218}
219
220Int_t CheckSequence(MSQLServer &serv, TString datapath, TString sequpath, Int_t from, Int_t to, Bool_t dummy)
221{
222 Int_t rc=0;
223 //rc=0 means sequence not found -> insert
224 //rc=1 means deleting sequence(s) worked -> insert
225 //rc=2 means sequence is still the same -> insert not neccessary
226 //if deleting sequence doesn't work -> return -1
227
228
229 //getting # of sequence (in sequDB) between from and to
230 TString query("SELECT fSequenceFirst FROM Sequences WHERE (fSequenceFirst ");
231 query += Form("BETWEEN %d and %d OR fSequenceLast BETWEEN %d and %d) AND "
232 "fManuallyChangedKEY=1 ", from, to, from, to);
233
234 TSQLResult *res = serv.Query(query);
235 if (!res)
236 return -1;
237
238 TArrayI sequences;
239 Int_t numsequ=0;
240
241 TSQLRow *row=0;
242 while ((row=res->Next()))
243 {
244 numsequ++;
245 sequences.Set(numsequ);
246 sequences.AddAt(atoi((*row)[0]), numsequ-1);
247 }
248 delete res;
249
250 //if there's no sequence in the table Sequences -> check other tables
251 //if there's one sequence -> check if the sequence is identical
252 //if there are more sequences -> delete them
253 switch (numsequ)
254 {
255 case 0:
256 //FIXME: like this the check is of no use, but when doing it
257 // without the check for manuallychanged, all manually
258 // changed sequences would be deleted
259 cout << "found no sequence in Sequ-DB -> check other tables" << endl;
260 cout << " deleting every sequence found in Calibration, Star or SequenceProcessStatus between "
261 << from << " and " << to << endl;
262
263 //calibration table
264 query=Form("SELECT Calibration.fSequenceFirst FROM Calibration "
265 " LEFT JOIN Sequences ON Calibration.fSequenceFirst=Sequences.fSequenceFirst "
266 " WHERE fManuallyChangedKEY=1 AND Calibration.fSequenceFirst BETWEEN %d and %d",
267 from, to);
268 res = serv.Query(query);
269 if (!res)
270 return -1;
271 row=0;
272 while ((row=res->Next()))
273 {
274 if (!DeleteSequence(serv, datapath, sequpath, atoi((*row)[0]), dummy))
275 return -1;
276 rc=1;
277 }
278 delete res;
279
280 //Star table
281 query=Form("SELECT Star.fSequenceFirst FROM Star "
282 " LEFT JOIN Sequences ON Star.fSequenceFirst=Sequences.fSequenceFirst "
283 " WHERE fManuallyChangedKEY=1 AND Star.fSequenceFirst BETWEEN %d and %d",
284 from, to);
285 res = serv.Query(query);
286 if (!res)
287 return -1;
288 row=0;
289 while ((row=res->Next()))
290 {
291 if (!DeleteSequence(serv, datapath, sequpath, atoi((*row)[0]), dummy))
292 return -1;
293 rc=1;
294 }
295 delete res;
296
297 //SequenceProcessStatus table
298 query=Form("SELECT SequenceProcessStatus.fSequenceFirst FROM SequenceProcessStatus "
299 " LEFT JOIN Sequences ON SequenceProcessStatus.fSequenceFirst=Sequences.fSequenceFirst "
300 " WHERE fManuallyChangedKEY=1 AND SequenceProcessStatus.fSequenceFirst BETWEEN %d and %d",
301 from, to);
302 res = serv.Query(query);
303 if (!res)
304 return -1;
305 row=0;
306 while ((row=res->Next()))
307 {
308 if (!DeleteSequence(serv, datapath, sequpath, atoi((*row)[0]), dummy))
309 return -1;
310 rc=1;
311 }
312 delete res;
313 break;
314
315 case 1:
316 cout << "found 1 sequence: " << sequences.At(0) << " -> check sequ# " << endl;
317 if (sequences.At(0)!=from)
318 {
319 if (!DeleteSequence(serv, datapath, sequpath, sequences.At(0), dummy))
320 return -1;
321 rc=1;
322 }
323 else
324 {
325 cout << "sequence# is the same -> checking the runs " << endl;
326
327 //getting olf runs
328 query=Form("SELECT fRunNumber FROM RunData WHERE fSequenceFirst=%d ", from);
329 res = serv.Query(query);
330 if (!res)
331 return -1;
332
333 TArrayI oldruns;
334 Int_t count=0;
335 row=0;
336 while ((row=res->Next()))
337 {
338 count++;
339 oldruns.Set(count);
340 oldruns.AddAt(atoi((*row)[0]), count-1);
341 }
342 delete res;
343
344 //getting new runs
345 query=Form("SELECT fRunNumber FROM RunData WHERE fRunNumber BETWEEN %d and %d AND fExcludedFDAKEY=1", from, to);
346 res = serv.Query(query);
347 if (!res)
348 return -1;
349 TArrayI newruns;
350 count=0;
351 row=0;
352 while ((row=res->Next()))
353 {
354 count++;
355 newruns.Set(count);
356 newruns.AddAt(atoi((*row)[0]), count-1);
357 }
358 delete res;
359
360 //comparing old and new runs (first the # of runs, if it is the same, also the single runnumbers
361 if (oldruns.GetSize()!=newruns.GetSize())
362 {
363// cout << " number of runs (" << oldruns.GetSize() << " - " << newruns.GetSize()
364// << ") is not the same -> deleting sequence " << sequences.At(0) << endl;
365 if (!DeleteSequence(serv, datapath, sequpath, sequences.At(0), dummy))
366 return -1;
367 rc=1;
368 }
369 else
370 {
371 cout << " number of runs is the same -> checking the single runnumbers " << endl;
372
373 for (Int_t i=0;i<newruns.GetSize();i++)
374 {
375// cout << "i: " << i << " - new: " << newruns.At(i) << " - old: " << oldruns.At(i) << endl;
376 if (newruns.At(i)==oldruns.At(i))
377 continue;
378
379 cout << i << ". run is not the same ( " << oldruns.At(i) << " -- " << newruns.At(i)
380 << ") -> deleting sequence " << sequences.At(0) << endl;
381 if (!DeleteSequence(serv, datapath, sequpath, sequences.At(0), dummy))
382 return -1;
383 rc=1;
384 break;
385 }
386 }
387 }
388 rc=2;
389 break;
390
391 default:
392 cout << "found " << numsequ << " sequences -> deleting them " << endl;
393
394 for (Int_t i=0;i<sequences.GetSize();i++)
395 {
396 cout << "deleting sequence " << sequences.At(i) << "... <" << i << ">" << endl;
397 if (!DeleteSequence(serv, datapath, sequpath, sequences.At(i), dummy))
398 return -1;
399 rc=1;
400 }
401 }
402
403 return rc;
404}
405
406Bool_t InsertSequence(MSQLServer &serv, Int_t from, Int_t to)
407{
408
409 cout << "Inserting sequence " << from << " ... " << endl;
410
411 // ========== Request number of events ==========
412 TString query("SELECT SUM(fNumEvents), "
413 " SUM(if(TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)<0,"
414 " TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)+24*60*60,"
415 " TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart))), ");
416 query += " MIN(fZenithDistance), MAX(fZenithDistance), ";
417 query += " MIN(fAzimuth), MAX(fAzimuth) ";
418 query += Form(" FROM RunData WHERE fRunTypeKEY=2 AND "
419 " (fRunNumber BETWEEN %d AND %d) AND fExcludedFDAKEY=1",
420 from, to);
421
422 TSQLResult *res = serv.Query(query);
423 if (!res)
424 return kFALSE;
425
426 TSQLRow *row = res->Next();
427 if (!row || !(*row)[0])
428 {
429 cout << "ERROR - No result from query: " << query << endl;
430 return kFALSE;
431 }
432
433 TString nevts = (*row)[0];
434 TString secs = (*row)[1];
435 TString zdmin = (*row)[2];
436 TString zdmax = (*row)[3];
437 TString azmin = (*row)[4];
438 TString azmax = (*row)[5];
439
440 delete res;
441
442 // ========== Request start time of sequence ==========
443 query = Form("SELECT fRunStart FROM RunData WHERE fRunNumber=%d AND fExcludedFDAKEY=1", from);
444
445 res = serv.Query(query);
446 if (!res)
447 return kFALSE;
448
449 row = res->Next();
450 if (!row || !(*row)[0])
451 {
452 cout << "ERROR - No result from query: " << query << endl;
453 return kFALSE;
454 }
455
456 TString start((*row)[0]);
457
458 delete res;
459
460 // ========== Request data of sequence ==========
461 query = Form("SELECT fSourceKEY, fProjectKEY, "
462 " fL1TriggerTableKEY, fL2TriggerTableKEY,"
463 " fHvSettingsKEY, fDiscriminatorThresholdTableKEY,"
464 " fTriggerDelayTableKEY, fLightConditionsKEY, fTestFlagKEY"
465 " FROM RunData"
466 " WHERE fRunTypeKEY=2 AND fExcludedFDAKEY=1 AND (fRunNumber BETWEEN %d AND %d)"
467 " LIMIT 1", from, to);
468
469 res = serv.Query(query);
470 if (!res)
471 return kFALSE;
472
473 row = res->Next();
474 if (!row)
475 {
476 cout << "ERROR - No result from query: " << query << endl;
477 return kFALSE;
478 }
479
480 TString query1("INSERT Sequences SET");
481 query1+=Form(" fSequenceFirst=%d, fSequenceLast=%d,", from, to);
482 query1+=Form(" fSourceKEY=%s,", (*row)[0]);
483 query1+=Form(" fProjectKEY=%s,", (*row)[1]);
484 query1+=Form(" fNumEvents=%s,", nevts.Data());
485 query1+=Form(" fRunTime=%s,", secs.Data());
486 query1+=Form(" fRunStart=\"%s\",", start.Data());
487 query1+=Form(" fZenithDistanceMin=%s,", zdmin.Data());
488 query1+=Form(" fZenithDistanceMax=%s,", zdmax.Data());
489 query1+=Form(" fAzimuthMin=%s,", azmin.Data());
490 query1+=Form(" fAzimuthMax=%s,", azmax.Data());
491 query1+=Form(" fL1TriggerTableKEY=%s,", (*row)[2]);
492 query1+=Form(" fL2TriggerTableKEY=%s,", (*row)[3]);
493 query1+=Form(" fHvSettingsKEY=%s,", (*row)[4]);
494 query1+=Form(" fDiscriminatorThresholdTableKEY=%s,", (*row)[5]);
495 query1+=Form(" fTriggerDelayTableKEY=%s,", (*row)[6]);
496 query1+=Form(" fLightConditionsKEY=%s,", (*row)[7]);
497 query1+=Form(" fTestFlagKEY=%s, fManuallyChangedKEY=1", (*row)[8]);
498
499
500 TString query2 = Form("UPDATE RunData SET fSequenceFirst=%d WHERE"
501 " (fRunNumber BETWEEN %d AND %d) AND"
502 " (fRunTypeKEY BETWEEN 2 AND 4) AND"
503 " fSourceKEY=%s AND fHvSettingsKEY=%s AND fExcludedFDAKEY=1",
504 from, from, to, (*row)[0], (*row)[4]);
505
506 TString query3 = Form("INSERT SequenceProcessStatus SET fSequenceFirst=%d ", from);
507
508 delete res;
509
510 cout << "q1: " << query1 << endl;
511 cout << "q2: " << query2 << endl;
512 cout << "q3: " << query3 << endl;
513
514 res = serv.Query(query1);
515 if (!res)
516 {
517 cout << "ERROR - Could not insert Sequence into Sequences." << endl;
518 return kFALSE;
519 }
520 delete res;
521
522 res = serv.Query(query2);
523 if (!res)
524 {
525 cout << "ERROR - Could not update RunData." << endl;
526 return kFALSE;
527 }
528 delete res;
529
530 res = serv.Query(query3);
531 if (!res)
532 {
533 cout << "ERROR - Could not insert Sequence into SequenceProcessStatus." << endl;
534 return kFALSE;
535 }
536 delete res;
537
538 return kTRUE;
539}
540
541//
542// Handling new sequence (checking runs; checking for old sequence; inserting sequence, if everything is okay)
543//
544Bool_t NewSequence(MSQLServer &serv, TString datapath, TString sequpath, Int_t from, Int_t to, TList &sequlist, Bool_t dummy)
545{
546 cout << "Found Sequence (" << from << ", " << to << ") ... checking runs..." << flush;
547
548 if (!CheckRuns(serv, from, to, 2))
549 {
550 cout << "Warning - Found inconsistency in data-runs (" << from << ", " << to << ")" << endl;
551 //sequence is not built, but kTRUE is returned, to allow
552 //the automatic processing of the other sequences of this day
553 return kTRUE;
554 }
555 if (!CheckRuns(serv, from, to, 3))
556 {
557 cout << "Warning - Found inconsistency in ped-runs (" << from << ", " << to << ")" << endl;
558 //sequence is not built, but kTRUE is returned, to allow
559 //the automatic processing of the other sequences of this day
560 return kTRUE;
561 }
562
563 cout << "ok." << endl;
564
565
566 cout << "checking Sequence..." << endl;
567
568 TObject *sequ;
569 Bool_t rc=kFALSE;
570 switch (CheckSequence(serv, datapath, sequpath, from, to, dummy))
571 {
572 case 0:
573 cout << " sequence not found -> inserting " << from << flush ;
574 if (dummy)
575 {
576 cout << " <dummy> " << endl;
577 return kTRUE;
578 }
579 cout << endl;
580 if ((sequ=sequlist.Remove(sequlist.FindObject(Form("%d", from)))))
581 delete sequ;
582 return InsertSequence(serv, from, to);
583
584 case 1:
585 cout << " deleting successfully finished -> inserting sequence " << from << flush;
586 if (dummy)
587 {
588 cout << " <dummy> " << endl;
589 return kTRUE;
590 }
591 cout << endl;
592 if ((sequ=sequlist.Remove(sequlist.FindObject(Form("%d", from)))))
593 delete sequ;
594 return InsertSequence(serv, from, to);
595
596 case 2:
597 cout << " sequence " << from << " is already existing -> inserting not necessary" << endl;
598 if ((sequ=sequlist.Remove(sequlist.FindObject(Form("%d", from)))))
599 delete sequ;
600 return kTRUE;
601
602 case -1:
603 cout << " deleting went wrong " << endl;
604 return kFALSE;
605 }
606
607
608 return rc;
609}
610
611//
612// Build Sequences in range of runs
613//
614Bool_t Process(MSQLServer &serv, TString datapath, TString sequpath, Int_t from, Int_t to, TList &sequlist, Bool_t dummy)
615{
616
617 TString query(Form("SELECT fRunNumber, fRunTypeKEY, fRunStart, fRunStop"
618 " FROM RunData"
619 " WHERE fRunNumber BETWEEN %d AND %d AND "
620 " fExcludedFDAKEY=1 AND (fRunTypeKEY BETWEEN 2 AND 4)"
621 " ORDER BY fRunNumber", from, to));
622
623 TSQLResult *res = serv.Query(query);
624 if (!res)
625 return kFALSE;
626
627 TExMap map;
628
629 Int_t start=0;
630 Int_t stop=0;
631 Int_t last=0;
632 Int_t first=0;
633
634 MTime lasttime;
635
636 TSQLRow *row=0;
637
638 enum { UNKNOWN, PED=3, CAL=4, DATA=2 };
639 Char_t status = UNKNOWN;
640
641 Int_t nblocks = 0;
642
643 while ((row=res->Next()))
644 {
645 if (!(*row)[1])
646 continue;
647
648 if (start==0)
649 {
650 first = atoi((*row)[0]);
651 if (debug)
652 cout << "First Run: " << first << endl;
653 }
654
655 switch (atoi((*row)[1]))
656 {
657 case CAL: // ---------- CALIBRATION ----------
658 if (status!=CAL)
659 {
660 start = stop = atoi((*row)[0]);
661 if (!(*row)[2])
662 cout << "No time available... skipped." << endl;
663 else
664 {
665 MTime *tm = new MTime;
666 tm->SetSqlDateTime((*row)[2]);
667 map.Add((ULong_t)map.GetSize(), (Long_t)tm, (Long_t)nblocks);
668 }
669 }
670 status = CAL;
671 break;
672 default:
673 if (status==CAL)
674 {
675 MTime *tm = new MTime(lasttime);
676 map.Add((ULong_t)map.GetSize(), (Long_t)tm, (Long_t)nblocks);
677
678 stop = last;
679 nblocks++;
680 if (debug)
681 cout << "Cal Block #" << nblocks << " from " << start << " to " << last << endl;
682 }
683 status = UNKNOWN;
684 break;
685 }
686 last = atoi((*row)[0]);
687 lasttime.SetSqlDateTime((*row)[3]);
688 }
689 if (status==CAL)
690 {
691 stop = last;
692 nblocks++;
693 if (debug)
694 cout << "Cal Block #" << nblocks << " from " << start << " to " << stop << endl;
695 }
696
697 if (debug)
698 cout << "Last Run: " << last << endl;
699 delete res;
700
701 if (debug)
702 cout << "Found " << nblocks << " calibration blocks" << endl;
703
704 res = serv.Query(query);
705 if (!res)
706 return kFALSE;
707
708 Int_t n = -1;
709
710 Bool_t rc = kTRUE;
711
712 start = first;
713 while ((row=res->Next()))
714 {
715 if (!(*row)[1])
716 continue;
717
718 MTime tstart, tstop;
719 tstart.SetSqlDateTime((*row)[2]);
720 tstop.SetSqlDateTime((*row)[3]);
721
722 MTime min;
723 Int_t nmin = -1;
724 Double_t dmin = 1e35;
725
726 Long_t key, val;
727 TExMapIter nmap(&map);
728 while (nmap.Next(key, val))
729 {
730 MTime *t = (MTime*)key;
731
732 if (nmin==-1)
733 {
734 nmin = val;
735 min = *(MTime*)key;
736 dmin = fabs((Double_t)*t-(Double_t)tstart);
737 }
738
739 if (fabs((Double_t)*t-(Double_t)tstart) < dmin)
740 {
741 nmin = val;
742 dmin = fabs((Double_t)*t-(Double_t)tstart);
743 min = *t;
744 }
745 if (fabs((Double_t)*t-(Double_t)tstop) < dmin)
746 {
747 nmin = val;
748 dmin = fabs((Double_t)*t-(Double_t)tstop);
749 min = *t;
750 }
751 }
752
753 if (n!=nmin)
754 {
755 if (n!=-1)
756 {
757 if (!NewSequence(serv, datapath, sequpath, start, last, sequlist, dummy))
758 {
759 rc = kFALSE;
760 //continue;
761 }
762 }
763 n = nmin;
764 start = atoi((*row)[0]);
765 }
766 last = atoi((*row)[0]);
767 }
768
769 delete res;
770
771 if (n!=-1 && start!=last)
772 {
773 if (!NewSequence(serv, datapath, sequpath, start, last, sequlist, dummy))
774 rc = kFALSE;
775 }
776
777 if (debug)
778 cout << endl;
779
780 return rc;
781}
782
783
784
785//
786// Build Sequences for the night given by TString day
787//
788int buildsequenceentries(TString day, TString datapath, TString sequpath, Bool_t dummy=kTRUE)
789{
790 TEnv env("sql.rc");
791
792 MSQLServer serv(env);
793 if (!serv.IsConnected())
794 {
795 cout << "ERROR - Connection to database failed." << endl;
796 return 0;
797 }
798
799 cout << "buildsequences" << endl;
800 cout << "--------------" << endl;
801 cout << endl;
802 cout << "Connected to " << serv.GetName() << endl;
803 cout << "Night of sunrise at: " << day << endl;
804 cout << endl;
805
806 day += " 13:00:00";
807 const TString cond(Form("(fRunStart>ADDDATE(\"%s\", INTERVAL -1 DAY) AND fRunStart<\"%s\")",
808 day.Data(), day.Data()));
809
810 //get all values from the database, that are relevant for building sequences
811 TString query(Form("SELECT fRunNumber, fSourceKEY, fProjectKEY, fHvSettingsKEY, fLightConditionsKEY, fDiscriminatorThresholdTableKEY, fTriggerDelayTableKEY FROM RunData WHERE %s AND fExcludedFDAKEY=1 order by fRunNumber", cond.Data()));
812
813 TSQLResult *res = serv.Query(query);
814 if (!res)
815 return 0;
816
817 //build blocks of runs, which have the same values
818 //for each block the first and the last run are stored in a TExMap
819 //the values are checked with the help of an array of TStrings
820 TString keys[6]= { "NULL", "NULL", "NULL", "NULL", "NULL", "NULL" };
821 TString stop = "NULL";
822 TString runstart = "NULL";
823 TString runstop = "NULL";
824 Int_t count = 0;
825 TExMap blocks;
826 Int_t runbegin;
827 Int_t runend;
828
829 TSQLRow *row=0;
830 while ((row=res->Next()))
831 {
832 if (count==0)
833 {
834 for (Int_t i=1 ; i<7 ; i++)
835 keys[i-1]=(*row)[i];
836 runstart=(*row)[0];
837 }
838
839 for (Int_t i=1 ; i<7 ; i++)
840 {
841 runbegin=atoi(runstart.Data());
842 runend=atoi(runstop.Data());
843 if (i==2 && runbegin>20100 && runend<45100)
844 continue;
845
846 TString value=(*row)[i];
847 TString key=keys[i-1];
848 if (!value.CompareTo(key))
849 continue;
850
851 keys[i-1]=value;
852 //fill values into TExMap
853 blocks.Add((ULong_t)blocks.GetSize(), (Long_t)runbegin, (Long_t)runend);
854 runstart=(*row)[0];
855 for (Int_t i=1 ; i<7 ; i++)
856 keys[i-1]=(*row)[i];
857 break;
858 }
859 runstop=(*row)[0];
860 count++;
861 }
862
863 //fill values into TExMap (last value)
864 runbegin=atoi(runstart.Data());
865 runend=atoi(runstop.Data());
866 blocks.Add((ULong_t)blocks.GetSize(), (Long_t)runbegin, (Long_t)runend);
867
868
869 //get list of current sequence of this night from the database and store it in sequlist
870 TList sequlist;
871 query=Form("SELECT fSequenceFirst FROM Sequences WHERE fManuallyChangedKEY=1 AND %s order by fSequenceFirst",
872 cond.Data());
873// cout << "Q: " << query << endl;
874
875 res = serv.Query(query);
876 if (!res)
877 return 0;
878
879 cout << "old sequences: " << flush;
880 while ((row=res->Next()))
881 {
882 const char *str = (*row)[0];
883 cout << str << " " << flush;
884 sequlist.Add(new TObjString(str));
885 }
886 cout << endl;
887
888 Bool_t rc = kTRUE;
889
890 //build sequences in each block of runs
891 Long_t key, val;
892 TExMapIter nblocks(&blocks);
893 while (nblocks.Next(key, val))
894 {
895 Int_t runstart2 = (Int_t)key;
896 Int_t runstop2 = (Int_t)val;
897 cout << endl << "datablock from " << runstart2 << " to " << runstop2 << endl;
898
899 if (!Process(serv, datapath, sequpath, runstart2, runstop2, sequlist, dummy))
900 rc = kFALSE;
901
902 }
903
904 //newly build or remaining sequences are removed from sequlist while building sequences
905 //delete sequences that remained in the list
906 //this is necessary if e.g. all runs of one old sequence have been excluded in the meantime
907 TIter Next(&sequlist);
908 TObject *obj = 0;
909 while ((obj=Next()))
910 {
911 cout << "sequ: " << obj->GetName() << " deleting... " << endl;
912 if (!DeleteSequence(serv, datapath, sequpath, atoi(obj->GetName()), dummy))
913 return 0;
914 }
915
916 return rc ? 1 : 0;
917}
918
919//
920// Build Sequences for all Nights
921//
922int buildsequenceentries(TString datapath, TString sequpath, Bool_t dummy=kTRUE)
923{
924 TEnv env("sql.rc");
925
926 MSQLServer serv(env);
927 if (!serv.IsConnected())
928 {
929 cout << "ERROR - Connection to database failed." << endl;
930 return 0;
931 }
932
933 //get all dates from the database
934 TString query="SELECT fDate FROM SequenceBuildStatus";
935
936 TSQLResult *res = serv.Query(query);
937 if (!res)
938 return 0;
939
940 //execute buildsequenceentries for all dates
941 TString date;
942 TSQLRow *row=0;
943 while ((row=res->Next()))
944 {
945 date=(*row)[0];
946 cout << "date: " << date << endl;
947 buildsequenceentries(date, datapath, sequpath, dummy);
948 }
949
950 return 1;
951}
Note: See TracBrowser for help on using the repository browser.