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

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