source: tags/Mars-V0.9.5/datacenter/macros/buildsequenceentries.C

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