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

Last change on this file since 7629 was 7629, checked in by Daniela Dorner, 18 years ago
*** empty log message ***
File size: 29.5 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, "
466 " fTestFlagKEY, fObservationModeKEY "
467 " FROM RunData"
468 " WHERE fRunTypeKEY=2 AND fExcludedFDAKEY=1 AND (fRunNumber BETWEEN %d AND %d)"
469 " LIMIT 1", from, to);
470
471 res = serv.Query(query);
472 if (!res)
473 return kFALSE;
474
475 row = res->Next();
476 if (!row)
477 {
478 cout << "ERROR - No result from query: " << query << endl;
479 return kFALSE;
480 }
481
482 TString query1("INSERT Sequences SET");
483 query1+=Form(" fSequenceFirst=%d, fSequenceLast=%d,", from, to);
484 query1+=Form(" fSourceKEY=%s,", (*row)[0]);
485 query1+=Form(" fProjectKEY=%s,", (*row)[1]);
486 query1+=Form(" fNumEvents=%s,", nevts.Data());
487 query1+=Form(" fRunTime=%s,", secs.Data());
488 query1+=Form(" fRunStart=\"%s\",", start.Data());
489 query1+=Form(" fZenithDistanceMin=%s,", zdmin.Data());
490 query1+=Form(" fZenithDistanceMax=%s,", zdmax.Data());
491 query1+=Form(" fAzimuthMin=%s,", azmin.Data());
492 query1+=Form(" fAzimuthMax=%s,", azmax.Data());
493 query1+=Form(" fL1TriggerTableKEY=%s,", (*row)[2]);
494 query1+=Form(" fL2TriggerTableKEY=%s,", (*row)[3]);
495 query1+=Form(" fHvSettingsKEY=%s,", (*row)[4]);
496 query1+=Form(" fDiscriminatorThresholdTableKEY=%s,", (*row)[5]);
497 query1+=Form(" fTriggerDelayTableKEY=%s,", (*row)[6]);
498 query1+=Form(" fLightConditionsKEY=%s,", (*row)[7]);
499 query1+=Form(" fTestFlagKEY=%s, ", (*row)[8]);
500 query1+=Form(" fObservationModeKEY=%s, fManuallyChangedKEY=1", (*row)[9]);
501
502
503 TString query2 = Form("UPDATE RunData SET fSequenceFirst=%d WHERE"
504 " (fRunNumber BETWEEN %d AND %d) AND"
505 " (fRunTypeKEY BETWEEN 2 AND 4) AND"
506 " fSourceKEY=%s AND fHvSettingsKEY=%s AND fExcludedFDAKEY=1",
507 from, from, to, (*row)[0], (*row)[4]);
508
509 TString query3 = Form("INSERT SequenceProcessStatus SET fSequenceFirst=%d ", from);
510
511 delete res;
512
513 cout << "q1: " << query1 << endl;
514 cout << "q2: " << query2 << endl;
515 cout << "q3: " << query3 << endl;
516
517 res = serv.Query(query1);
518 if (!res)
519 {
520 cout << "ERROR - Could not insert Sequence into Sequences." << endl;
521 return kFALSE;
522 }
523 delete res;
524
525 res = serv.Query(query2);
526 if (!res)
527 {
528 cout << "ERROR - Could not update RunData." << endl;
529 return kFALSE;
530 }
531 delete res;
532
533 res = serv.Query(query3);
534 if (!res)
535 {
536 cout << "ERROR - Could not insert Sequence into SequenceProcessStatus." << endl;
537 return kFALSE;
538 }
539 delete res;
540
541 return kTRUE;
542}
543
544//
545// Handling new sequence (checking runs; checking for old sequence; inserting sequence, if everything is okay)
546//
547Bool_t NewSequence(MSQLServer &serv, TString datapath, TString sequpath, Int_t from, Int_t to, TList &sequlist, Bool_t dummy)
548{
549 cout << "Found Sequence (" << from << ", " << to << ") ... checking runs..." << flush;
550
551 if (!CheckRuns(serv, from, to, 2))
552 {
553 cout << "Warning - Found inconsistency in data-runs (" << from << ", " << to << ")" << endl;
554 //sequence is not built, but kTRUE is returned, to allow
555 //the automatic processing of the other sequences of this day
556 return kTRUE;
557 }
558 if (!CheckRuns(serv, from, to, 3))
559 {
560 cout << "Warning - Found inconsistency in ped-runs (" << from << ", " << to << ")" << endl;
561 //sequence is not built, but kTRUE is returned, to allow
562 //the automatic processing of the other sequences of this day
563 return kTRUE;
564 }
565
566 cout << "ok." << endl;
567
568
569 cout << "checking Sequence..." << endl;
570
571 TObject *sequ;
572 Bool_t rc=kFALSE;
573 switch (CheckSequence(serv, datapath, sequpath, from, to, dummy))
574 {
575 case 0:
576 cout << " sequence not found -> inserting " << from << flush ;
577 if (dummy)
578 {
579 cout << " <dummy> " << endl;
580 return kTRUE;
581 }
582 cout << endl;
583 if ((sequ=sequlist.Remove(sequlist.FindObject(Form("%d", from)))))
584 delete sequ;
585 return InsertSequence(serv, from, to);
586
587 case 1:
588 cout << " deleting successfully finished -> inserting sequence " << from << flush;
589 if (dummy)
590 {
591 cout << " <dummy> " << endl;
592 return kTRUE;
593 }
594 cout << endl;
595 if ((sequ=sequlist.Remove(sequlist.FindObject(Form("%d", from)))))
596 delete sequ;
597 return InsertSequence(serv, from, to);
598
599 case 2:
600 cout << " sequence " << from << " is already existing -> inserting not necessary" << endl;
601 if ((sequ=sequlist.Remove(sequlist.FindObject(Form("%d", from)))))
602 delete sequ;
603 return kTRUE;
604
605 case -1:
606 cout << " deleting went wrong " << endl;
607 return kFALSE;
608 }
609
610
611 return rc;
612}
613
614//
615// Build Sequences in range of runs
616//
617Bool_t Process(MSQLServer &serv, TString datapath, TString sequpath, Int_t from, Int_t to, TList &sequlist, Bool_t dummy)
618{
619
620 TString query(Form("SELECT fRunNumber, fRunTypeKEY, fRunStart, fRunStop"
621 " FROM RunData"
622 " WHERE fRunNumber BETWEEN %d AND %d AND "
623 " fExcludedFDAKEY=1 AND (fRunTypeKEY BETWEEN 2 AND 4)"
624 " ORDER BY fRunNumber", from, to));
625
626 TSQLResult *res = serv.Query(query);
627 if (!res)
628 return kFALSE;
629
630 TExMap map;
631
632 Int_t start=0;
633 Int_t stop=0;
634 Int_t last=0;
635 Int_t first=0;
636
637 MTime lasttime;
638
639 TSQLRow *row=0;
640
641 enum { UNKNOWN, PED=3, CAL=4, DATA=2 };
642 Char_t status = UNKNOWN;
643
644 Int_t nblocks = 0;
645
646 while ((row=res->Next()))
647 {
648 if (!(*row)[1])
649 continue;
650
651 if (start==0)
652 {
653 first = atoi((*row)[0]);
654 if (debug)
655 cout << "First Run: " << first << endl;
656 }
657
658 switch (atoi((*row)[1]))
659 {
660 case CAL: // ---------- CALIBRATION ----------
661 if (status!=CAL)
662 {
663 start = stop = atoi((*row)[0]);
664 if (!(*row)[2])
665 cout << "No time available... skipped." << endl;
666 else
667 {
668 MTime *tm = new MTime;
669 tm->SetSqlDateTime((*row)[2]);
670 map.Add((ULong_t)map.GetSize(), (Long_t)tm, (Long_t)nblocks);
671 }
672 }
673 status = CAL;
674 break;
675 default:
676 if (status==CAL)
677 {
678 MTime *tm = new MTime(lasttime);
679 map.Add((ULong_t)map.GetSize(), (Long_t)tm, (Long_t)nblocks);
680
681 stop = last;
682 nblocks++;
683 if (debug)
684 cout << "Cal Block #" << nblocks << " from " << start << " to " << last << endl;
685 }
686 status = UNKNOWN;
687 break;
688 }
689 last = atoi((*row)[0]);
690 lasttime.SetSqlDateTime((*row)[3]);
691 }
692 if (status==CAL)
693 {
694 stop = last;
695 nblocks++;
696 if (debug)
697 cout << "Cal Block #" << nblocks << " from " << start << " to " << stop << endl;
698 }
699
700 if (debug)
701 cout << "Last Run: " << last << endl;
702 delete res;
703
704 if (debug)
705 cout << "Found " << nblocks << " calibration blocks" << endl;
706
707 res = serv.Query(query);
708 if (!res)
709 return kFALSE;
710
711 Int_t n = -1;
712
713 Bool_t rc = kTRUE;
714
715 start = first;
716 while ((row=res->Next()))
717 {
718 if (!(*row)[1])
719 continue;
720
721 MTime tstart, tstop;
722 tstart.SetSqlDateTime((*row)[2]);
723 tstop.SetSqlDateTime((*row)[3]);
724
725 MTime min;
726 Int_t nmin = -1;
727 Double_t dmin = 1e35;
728
729 Long_t key, val;
730 TExMapIter nmap(&map);
731 while (nmap.Next(key, val))
732 {
733 MTime *t = (MTime*)key;
734
735 if (nmin==-1)
736 {
737 nmin = val;
738 min = *(MTime*)key;
739 dmin = fabs((Double_t)*t-(Double_t)tstart);
740 }
741
742 if (fabs((Double_t)*t-(Double_t)tstart) < dmin)
743 {
744 nmin = val;
745 dmin = fabs((Double_t)*t-(Double_t)tstart);
746 min = *t;
747 }
748 if (fabs((Double_t)*t-(Double_t)tstop) < dmin)
749 {
750 nmin = val;
751 dmin = fabs((Double_t)*t-(Double_t)tstop);
752 min = *t;
753 }
754 }
755
756 if (n!=nmin)
757 {
758 if (n!=-1)
759 {
760 if (!NewSequence(serv, datapath, sequpath, start, last, sequlist, dummy))
761 {
762 rc = kFALSE;
763 //continue;
764 }
765 }
766 n = nmin;
767 start = atoi((*row)[0]);
768 }
769 last = atoi((*row)[0]);
770 }
771
772 delete res;
773
774 if (n!=-1 && start!=last)
775 {
776 if (!NewSequence(serv, datapath, sequpath, start, last, sequlist, dummy))
777 rc = kFALSE;
778 }
779
780 if (debug)
781 cout << endl;
782
783 return rc;
784}
785
786
787
788//
789// Build Sequences for the night given by TString day
790//
791int buildsequenceentries(TString day, TString datapath, TString sequpath, Bool_t dummy=kTRUE)
792{
793 TEnv env("sql.rc");
794
795 MSQLServer serv(env);
796 if (!serv.IsConnected())
797 {
798 cout << "ERROR - Connection to database failed." << endl;
799 return 0;
800 }
801
802 cout << "buildsequences" << endl;
803 cout << "--------------" << endl;
804 cout << endl;
805 cout << "Connected to " << serv.GetName() << endl;
806 cout << "Night of sunrise at: " << day << endl;
807 cout << endl;
808
809 day += " 13:00:00";
810 const TString cond(Form("(fRunStart>ADDDATE(\"%s\", INTERVAL -1 DAY) AND fRunStart<\"%s\")",
811 day.Data(), day.Data()));
812
813 //get all values from the database, that are relevant for building sequences
814 TString query(Form("SELECT fRunNumber, fSourceKEY, fProjectKEY, fHvSettingsKEY, fLightConditionsKEY, fDiscriminatorThresholdTableKEY, fTriggerDelayTableKEY, fObservationModeKEY FROM RunData WHERE %s AND fExcludedFDAKEY=1 order by fRunNumber", cond.Data()));
815
816 TSQLResult *res = serv.Query(query);
817 if (!res)
818 return 2;
819
820 //build blocks of runs, which have the same values
821 //for each block the first and the last run are stored in a TExMap
822 //the values are checked with the help of an array of TStrings
823 TString keys[7]= { "NULL", "NULL", "NULL", "NULL", "NULL", "NULL" , "NULL" };
824 TString stop = "NULL";
825 TString runstart = "NULL";
826 TString runstop = "NULL";
827 Int_t count = 0;
828 TExMap blocks;
829 Int_t runbegin;
830 Int_t runend;
831
832 TSQLRow *row=0;
833 while ((row=res->Next()))
834 {
835 if (count==0)
836 {
837 for (Int_t i=1 ; i<8 ; i++)
838 keys[i-1]=(*row)[i];
839 runstart=(*row)[0];
840 }
841
842 for (Int_t i=1 ; i<8 ; i++)
843 {
844 runbegin=atoi(runstart.Data());
845 runend=atoi(runstop.Data());
846 if (i==2 && runbegin>20100 && runend<45100)
847 continue;
848
849 TString value=(*row)[i];
850 TString key=keys[i-1];
851 if (!value.CompareTo(key))
852 continue;
853
854 keys[i-1]=value;
855 //fill values into TExMap
856 blocks.Add((ULong_t)blocks.GetSize(), (Long_t)runbegin, (Long_t)runend);
857 runstart=(*row)[0];
858 for (Int_t i=1 ; i<8 ; i++)
859 keys[i-1]=(*row)[i];
860 break;
861 }
862 runstop=(*row)[0];
863 count++;
864 }
865
866 //fill values into TExMap (last value)
867 runbegin=atoi(runstart.Data());
868 runend=atoi(runstop.Data());
869 blocks.Add((ULong_t)blocks.GetSize(), (Long_t)runbegin, (Long_t)runend);
870
871
872 //get list of current sequence of this night from the database and store it in sequlist
873 TList sequlist;
874 query=Form("SELECT fSequenceFirst FROM Sequences WHERE fManuallyChangedKEY=1 AND %s order by fSequenceFirst",
875 cond.Data());
876// cout << "Q: " << query << endl;
877
878 res = serv.Query(query);
879 if (!res)
880 return 2;
881
882 cout << "old sequences: " << flush;
883 while ((row=res->Next()))
884 {
885 const char *str = (*row)[0];
886 cout << str << " " << flush;
887 sequlist.Add(new TObjString(str));
888 }
889 cout << endl;
890
891 Bool_t rc = kTRUE;
892
893 //build sequences in each block of runs
894 Long_t key, val;
895 TExMapIter nblocks(&blocks);
896 while (nblocks.Next(key, val))
897 {
898 Int_t runstart2 = (Int_t)key;
899 Int_t runstop2 = (Int_t)val;
900 cout << endl << "datablock from " << runstart2 << " to " << runstop2 << endl;
901
902 if (!Process(serv, datapath, sequpath, runstart2, runstop2, sequlist, dummy))
903 rc = kFALSE;
904
905 }
906
907 //newly build or remaining sequences are removed from sequlist while building sequences
908 //delete sequences that remained in the list
909 //this is necessary if e.g. all runs of one old sequence have been excluded in the meantime
910 TIter Next(&sequlist);
911 TObject *obj = 0;
912 while ((obj=Next()))
913 {
914 cout << "sequ: " << obj->GetName() << " deleting... " << endl;
915 if (!DeleteSequence(serv, datapath, sequpath, atoi(obj->GetName()), dummy))
916 return 2;
917 }
918
919 return rc ? 1 : 2;
920}
921
922//
923// Build Sequences for all Nights
924//
925int buildsequenceentries(TString datapath, TString sequpath, Bool_t dummy=kTRUE)
926{
927 TEnv env("sql.rc");
928
929 MSQLServer serv(env);
930 if (!serv.IsConnected())
931 {
932 cout << "ERROR - Connection to database failed." << endl;
933 return 0;
934 }
935
936 //get all dates from the database
937 TString query="SELECT fDate FROM SequenceBuildStatus";
938
939 TSQLResult *res = serv.Query(query);
940 if (!res)
941 return 2;
942
943 //execute buildsequenceentries for all dates
944 TString date;
945 TSQLRow *row=0;
946 while ((row=res->Next()))
947 {
948 date=(*row)[0];
949 cout << "date: " << date << endl;
950 buildsequenceentries(date, datapath, sequpath, dummy);
951 }
952
953 return 1;
954}
Note: See TracBrowser for help on using the repository browser.