source: trunk/MagicSoft/Mars/datacenter/macros/filldotrun.C@ 9212

Last change on this file since 9212 was 9197, checked in by hoehne, 16 years ago
*** empty log message ***
File size: 21.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): Daniela Dorner, 08/2004 <mailto:dorner@astro.uni-wuerzburg.de>
19! Author(s): Thomas Bretz, 08/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
20!
21! Copyright: MAGIC Software Development, 2000-2008
22!
23!
24\* ======================================================================== */
25
26/////////////////////////////////////////////////////////////////////////////
27//
28// filldotrun.C
29// ============
30//
31// This macro is used in the datacenter to automatically fill the run-database
32// with the information stored in the .run-files written by the central
33// control.
34//
35// To following Arehucas versions are Currently supported:
36// 040505-0, 040514-0,
37// 040518-0, 040727-0,
38// 041113-0, 041209-0, 041221-0
39// 050224-0, 050317-0, 050322-0, 050401-0, 050413-0, 050415-0, 050714-0,
40// 050719-0, 050829-0, 051025-0,
41// 060330-0, 060401-0, 060808-0
42// 070416-0,
43// 080220-0, 080519-0, 080912-0, 081204-0, 081214-0
44//
45// Usage:
46// .x filldotrun.C+("/data/MAGIC/Period019/ccdata", kTRUE)
47//
48// While the first argument is the directory in which all subdirectories where
49// searches for CC_*.run files. All these files were analysed and the run
50// info will be put into the DB, eg:
51// "/magic/subsystemdata/cc" would do it for all data
52// "/magic/subsystemdata/cc/2005" for one year
53// "/magic/subsystemdata/cc/2005/11" for one month
54// "/magic/subsystemdata/cc/2005/11/11" for a single day
55// "/magic/subsystemdata/cc/2005/11/11/file.run" for a single file
56//
57// The second argument is the 'dummy-mode'. If it is kTRUE dummy-mode is
58// switched on and nothing will be written into the database. Instead
59// informations about the subtables are displayed. This is usefull for tests
60// when adding a new arehucas version support. If it is kFALSE the information
61// are written into the subtables and the runs info is written into the
62// rundatabase.
63//
64// In the automatic case it makes sense to check the logfiles to make sure
65// that everything is fine...
66//
67// Make sure, that database and password are corretly set in a resource
68// file called sql.rc and the resource file is found.
69//
70// Remark: Running it from the commandline looks like this:
71// root -q -l -b filldotrun.C+\(\"path\"\,kFALSE\) 2>&1 | tee filldotrun.log
72//
73// Returns 0 in case of failure and 1 in case of success.
74//
75/////////////////////////////////////////////////////////////////////////////
76#include <iostream>
77#include <iomanip>
78#include <fstream>
79
80#include <TMath.h>
81#include <TRegexp.h>
82
83#include "MTime.h"
84#include "MDirIter.h"
85#include "MSQLMagic.h"
86
87using namespace std;
88
89
90Int_t insert(MSQLMagic &serv, Bool_t dummy, TString filename)
91{
92 ifstream fin(filename);
93 if (!fin)
94 {
95 cout << "Could not open file " << filename << endl;
96 return -1;
97 }
98
99 TString strng;
100 strng.ReadLine(fin);
101 if (strng!=TString("[CC Plain Run Summary File]"))
102 {
103 cout << filename << ": No Plain Run Summary File" << endl;
104 cout << "First Line: " << strng << endl;
105 cout << endl;
106 return -1;
107 }
108
109 strng.ReadLine(fin);
110 TRegexp reg("[0-9][0-9][0-9][0-9][0-9][0-9]-[0-9]");
111 TString arehucas = strng(reg);
112 arehucas.Prepend("20");
113 arehucas.ReplaceAll("-", "");
114
115 Int_t version = atoi(arehucas.Data());
116 if (version!=200405050 && version!=200405140 && version!=200405180 &&
117 version!=200407270 && version!=200411130 && version!=200412090 &&
118 version!=200412210 &&
119 version!=200502240 && version!=200503170 && version!=200503220 &&
120 version!=200504010 && version!=200504130 && version!=200504150 &&
121 version!=200507140 && version!=200507190 && version!=200508290 &&
122 version!=200510250 &&
123 version!=200603300 && version!=200604010 && version!=200608080 &&
124 version!=200704160 &&
125 version!=200802200 && version!=200805190 && version!=200809120 &&
126 version!=200812040 && version!=200812140)
127 {
128 cout << filename << ": File Version unknown - please update the macro!" << endl;
129 cout << "Second Line: " << strng << endl;
130 cout << endl;
131 return -1;
132 }
133
134 if (version >= 200805190)
135 {
136 strng.ReadLine(fin);
137 if (!strng.BeginsWith("Telescope M"))
138 {
139 cout << "WARNING - Line 3 doesn't start with 'Telescope M'." << endl;
140 cout << strng << endl;
141 }
142 }
143
144 if (version >= 200411130)
145 {
146 strng.ReadLine(fin);
147 if (strng[0]!='#')
148 {
149 cout << "WARNING - '#' expected." << endl;
150 cout << strng << endl;
151 }
152 }
153
154 cout << " * V" << version << " " << endl;
155
156 Int_t cnt=0;
157 while (1)
158 {
159 Int_t telnumber = 1; // FIXME: "NULL"?
160 if (version >=200805190)
161 {
162 strng.ReadToDelim(fin, ' ');
163 if (!fin)
164 break;
165 if (strng[0]!='M')
166 {
167 cout << "WARNING - First character is not an M." << endl;
168 cout << strng << endl;
169 strng.ReadLine(fin);
170 continue;
171 }
172 if (strng[1]!='1')
173 {
174 cout << "WARNING - Only MAGIC 1 implemented so far." << endl;
175 cout << strng << endl;
176 strng.ReadLine(fin);
177 continue;
178 }
179
180 telnumber = atoi(strng.Data()+1);
181 }
182
183 // ========== Col 1: Run Number =========
184 //Reading the line
185 //and converting some strings to ints/floats
186 strng.ReadToDelim(fin, ' ');
187 if (!fin)
188 break;
189
190 Int_t runnumber = atoi(strng.Data());
191
192 //runnumber=0 means no valid dataset
193 //-> continue
194 if (runnumber == 0)
195 {
196 strng.ReadLine(fin);
197 cout << "WARNING - Runnumber == 0" << endl;
198 cout << strng << endl;
199 continue;
200 }
201
202 Int_t filenumber = 0; // FIXME: "NULL"?
203 if (version >=200805190)
204 {
205 strng.ReadToDelim(fin, ' ');
206 filenumber = atoi(strng.Data());
207 }
208
209 TString where = Form("fTelescopeNumber=%d AND fFileNumber=%d",
210 telnumber, filenumber);
211 if (serv.ExistStr("fRunNumber", "RunData", Form("%d", runnumber), where))
212 {
213 // FIXME: Maybe we can implement a switch to update mode?
214 cout << "WARNING - Entry M" << telnumber << ":" << runnumber << "/" << filenumber << " already existing... skipped." << endl;
215 strng.ReadLine(fin);
216 continue;
217 }
218
219 // ========== Col 2: Run Type =========
220 strng.ReadToDelim(fin, ' ');
221 if (strng.Contains("???"))
222 strng="n/a";
223
224 Int_t runtype = serv.QueryKeyOfName("RunType", strng, kFALSE);
225 if (runtype<0)
226 {
227 cout << "ERROR - RunType " << strng << " not available." << endl;
228 strng.ReadLine(fin);
229 continue;
230 }
231
232 //cout << runtype << " ";
233
234 // ========== Col 3,4: Start Time =========
235 TString startdate, starttime;
236 startdate.ReadToDelim(fin, ' ');
237 starttime.ReadToDelim(fin, ' ');
238 //cout << startdate << " " << starttime << " ";
239
240 // ========== Col 5,6: Stop Time =========
241 TString stopdate, stoptime;
242 stopdate.ReadToDelim(fin, ' ');
243 stoptime.ReadToDelim(fin, ' ');
244 //cout << stopdate << " " << stoptime << " ";
245
246 if (startdate.Contains("???"))
247 startdate="0000-00-00";
248 if (starttime.Contains("???"))
249 starttime="00:00:00";
250 if (stopdate.Contains("???"))
251 stopdate="0000-00-00";
252 if (stoptime.Contains("???"))
253 stoptime="00:00:00";
254
255 // ========== Col 7: Source Name =========
256 strng.ReadToDelim(fin, ' ');
257 if (strng.Contains("???"))
258 strng="Unavailable";
259
260 Int_t sourcekey = serv.QueryKeyOfName("Source", strng.Data());
261 if (sourcekey<0)
262 {
263 strng.ReadLine(fin);
264 continue;
265 }
266 //cout << sourcekey << " ";
267
268 // ========== Col 8,9: Local source position =========
269 strng.ReadToDelim(fin, ' ');
270 Float_t zd = atof(strng.Data());
271
272 strng.ReadToDelim(fin, ' ');
273 Float_t az = atof(strng.Data());
274
275 //cout << zd << " " << az << " ";
276
277 // ========== Col 10: Number of Events =========
278 strng.ReadToDelim(fin, ' ');
279 Int_t evtno = atoi(strng.Data());
280
281 //cout << evtno << " ";
282
283 // ========== Col 11: Project Name =========
284 strng.ReadToDelim(fin, ' ');
285 if (strng.Contains("???"))
286 strng="Unavailable";
287
288 Int_t projkey = serv.QueryKeyOfName("Project", strng);
289 if (projkey<0)
290 {
291 strng.ReadLine(fin);
292 continue;
293 }
294 //cout << projkey << " ";
295
296 // ========== Col 12: Trigger Table Name =========
297 // starting from version 200411130: Col 12,13: Trigger Table Name =========
298 strng.ReadToDelim(fin, ' ');
299 if (strng.Contains("???"))
300 strng="n/a";
301
302 Int_t l1triggerkey=1;
303 Int_t l2triggerkey=1;
304 if (version >=200411130)
305 {
306 l1triggerkey = serv.QueryKeyOfName("L1TriggerTable", strng);
307 if (l1triggerkey<0)
308 {
309 strng.ReadLine(fin);
310 continue;
311 }
312
313 strng.ReadToDelim(fin, ' ');
314 if (strng.Contains("???"))
315 strng="n/a";
316
317 l2triggerkey = serv.QueryKeyOfName("L2TriggerTable", strng);
318 if (l2triggerkey<0)
319 {
320 strng.ReadLine(fin);
321 continue;
322 }
323 }
324 else
325 {
326 Int_t c=0;
327
328 if (strng.Contains(":"))
329 c=1;
330
331 if (strng.Contains("L1_") && !(strng.Contains(":")))
332 c=2;
333
334 if (strng.Contains("n/a"))
335 c=3;
336
337 switch (c)
338 {
339 case 0:
340 {
341 l2triggerkey = serv.QueryKeyOfName("L2TriggerTable", strng);
342 if (l2triggerkey<0)
343 {
344 strng.ReadLine(fin);
345 continue;
346 }
347
348 strng="n/a";
349 l1triggerkey = 1;
350
351 break;
352 }
353 case 1:
354 {
355 TString L1TT, L2TT;
356 L2TT=strng(7,12);
357 L1TT=strng(0,6);
358
359 l1triggerkey = serv.QueryKeyOfName("L1TriggerTable", L1TT);
360 if (l1triggerkey<0)
361 {
362 strng.ReadLine(fin);
363 continue;
364 }
365
366 l2triggerkey = serv.QueryKeyOfName("L2TriggerTable", L2TT);
367 if (l2triggerkey<0)
368 {
369 strng.ReadLine(fin);
370 continue;
371 }
372
373 break;
374 }
375 case 2:
376 {
377 l1triggerkey = serv.QueryKeyOfName("L1TriggerTable", strng);
378 if (l1triggerkey<0)
379 {
380 strng.ReadLine(fin);
381 continue;
382 }
383
384 strng="n/a";
385 l2triggerkey = 1;
386
387 break;
388 }
389 case 3:
390 {
391 l1triggerkey = 1;
392 l2triggerkey = 1;
393 break;
394 }
395 default:
396 {
397 cout << "WARNING: neither L1 nor L2 Trigger table - please check what is happening." << strng << endl;
398 break;
399 }
400 }
401 }
402
403 // ========== Col 13-15: TrigRate, L2 UnPresc Rate, L2 Presc Rate ==========
404 strng.ReadToDelim(fin, ' ');
405 Float_t trigrate = atof(strng.Data());
406
407 strng.ReadToDelim(fin, ' ');
408 Float_t l2uprate = atof(strng.Data());
409
410 strng.ReadToDelim(fin, ' ');
411 Float_t l2prrate = atof(strng.Data());
412
413 // ========== Col 16,17: DaqRate, Storage Rate ==========
414 strng.ReadToDelim(fin, ' ');
415 Float_t daqrate = atof(strng.Data());
416
417 strng.ReadToDelim(fin, ' ');
418 Float_t storerate = atof(strng.Data());
419
420 // ========== Col 18: HV table =========
421 if (version==200405050 || version==200405140)
422 strng.ReadToDelim(fin, '\n');
423 else
424 strng.ReadToDelim(fin, ' ');
425 if (strng.Contains("???"))
426 strng="n/a";
427
428 Int_t hvkey = serv.QueryKeyOfName("HvSettings", strng);
429 if (hvkey<0)
430 {
431 //strng.ReadLine(fin);
432 continue;
433 }
434
435 if (version==200405180 || version==200407270)
436 strng.ReadLine(fin);
437
438 Int_t testflagkey=1;
439 Int_t lightcondkey=1;
440 Int_t dttablekey=1;
441 Int_t triggerdelaytablekey=1;
442 Int_t calibrationscriptkey=1;
443 if (version>=200411130)
444 {
445 // ========== Col 19-35: DC and HV-values, mjd =========
446 for (int i=0 ; i<17 ; i++)
447 {
448 strng.ReadToDelim(fin, ' ');
449 }
450
451 // ========== Col 36: test-flag =========
452 strng.ReadToDelim(fin, ' ');
453 if (strng.Contains("???"))
454 strng="n/a";
455
456 testflagkey = serv.QueryKeyOfName("TestFlag", strng);
457 if (testflagkey<0)
458 {
459 strng.ReadLine(fin);
460 continue;
461 }
462
463 // ========== Col 37: light conditions =========
464 strng.ReadToDelim(fin, ' ');
465 if (strng.Contains("???"))
466 strng="n/a";
467
468 lightcondkey = serv.QueryKeyOfName("LightConditions", strng);
469 if (lightcondkey<0)
470 {
471 strng.ReadLine(fin);
472 continue;
473 }
474
475 // ========== Col 38: discriminator threshold table =========
476 strng.ReadToDelim(fin, ' ');
477 if (strng.Contains("???"))
478 strng="n/a";
479
480 dttablekey = serv.QueryKeyOfName("DiscriminatorThresholdTable", strng);
481 if (dttablekey<0)
482 {
483 strng.ReadLine(fin);
484 continue;
485 }
486
487 // ========== Col 39: trigger delay table =========
488 strng.ReadToDelim(fin, ' ');
489 if (strng.Contains("???"))
490 strng="n/a";
491
492 triggerdelaytablekey = serv.QueryKeyOfName("TriggerDelayTable", strng);
493 if (triggerdelaytablekey<0)
494 {
495 strng.ReadLine(fin);
496 continue;
497 }
498
499 // ========== Col 40,41: Telescope RA and Dec sent to drive =========
500 strng.ReadToDelim(fin, ' ');
501 strng.ReadToDelim(fin, ' ');
502
503 // ========== Col 42: Calibration Script =========
504 if (version>=200411130 && version<=200510250)
505 strng.ReadToDelim(fin, '\n');
506 else
507 strng.ReadToDelim(fin, ' ');
508 if (strng.Contains("???"))
509 strng="n/a";
510
511 calibrationscriptkey = serv.QueryKeyOfName("CalibrationScript", strng);
512 if (calibrationscriptkey<0)
513 {
514 strng.ReadLine(fin);
515 continue;
516 }
517
518 }
519
520 Int_t observationmodekey=1;
521 if (version>=200603300)
522 {
523 // ========== Col 43: Observation Mode =========
524 strng.ReadToDelim(fin, ' ');
525 if (strng.Contains("???"))
526 strng="n/a";
527
528 observationmodekey = serv.QueryKeyOfName("ObservationMode", strng);
529 if (observationmodekey<0)
530 {
531 strng.ReadLine(fin);
532 continue;
533 }
534
535 // ========== Col 44-51: Source RA and Dec, DT's and IPR =========
536 for (int i=0 ; i<7 ; i++)
537 {
538 strng.ReadToDelim(fin, ' ');
539 }
540 if (version<=200809120)
541 strng.ReadToDelim(fin, '\n');
542 else
543 strng.ReadToDelim(fin, ' ');
544 }
545
546 Int_t sumtriggerflagkey=1;
547 if (version>=200812040)
548 {
549 // ========= Col 52: SumTrigger flag =========
550 strng.ReadToDelim(fin, '\n');
551 if (strng.Contains("???"))
552 strng="n/a";
553
554 sumtriggerflagkey = serv.QueryKeyOfName("SumTriggerFlag", strng);
555 if (sumtriggerflagkey<0)
556 {
557 strng.ReadLine(fin);
558 continue;
559 }
560
561 }
562
563
564 // ================================================================
565 // ========== Data read from file now access the database =========
566 // ================================================================
567
568 //assemble the query that is needed to insert the values of this run
569 TString query;
570 query += Form("fTelescopeNumber=%d, ", telnumber);
571 query += Form("fRunNumber=%d, ", runnumber);
572 query += Form("fFileNumber=%d, ", filenumber);
573 query += Form("fRunTypeKEY=%d, ", runtype);
574 query += Form("fProjectKEY=%d, ", projkey);
575 query += Form("fSourceKEY=%d, ", sourcekey);
576 query += Form("fNumEvents=%d, ", evtno);
577 query += Form("fRunStart=\"%s %s\", ", startdate.Data(), starttime.Data());
578 query += Form("fRunStop=\"%s %s\", ", stopdate.Data(), stoptime.Data());
579 query += Form("fL1TriggerTableKEY=%d, ", l1triggerkey);
580 query += Form("fL2TriggerTableKEY=%d, ", l2triggerkey);
581 query += Form("fTestFlagKEY=%d, ", testflagkey);
582 query += Form("fCalibrationScriptKEY=%d, ", calibrationscriptkey);
583 query += Form("fTriggerDelayTableKEY=%d, ", triggerdelaytablekey);
584 query += Form("fDiscriminatorThresholdTableKEY=%d, ", dttablekey);
585 query += Form("fLightConditionsKEY=%d, ", lightcondkey);
586 query += Form("fHvSettingsKEY=%d, ", hvkey);
587 query += Form("fObservationModeKEY=%d, ", observationmodekey);
588 query += Form("fSumTriggerFlagKEY=%d, ", sumtriggerflagkey);
589 if (!TMath::IsNaN(zd) && TMath::Finite(zd))
590 query += Form("fZenithDistance=%d, ", TMath::Nint(zd));
591 if (!TMath::IsNaN(az) && TMath::Finite(az))
592 query += Form("fAzimuth=%d, ", TMath::Nint(az));
593 if (!TMath::IsNaN(storerate) && TMath::Finite(storerate))
594 query += Form("fDaqStoreRate=%d, ", TMath::Nint(storerate));
595 if (!TMath::IsNaN(daqrate) && TMath::Finite(daqrate))
596 query += Form("fDaqTriggerRate=%d, ", TMath::Nint(daqrate));
597 if (!TMath::IsNaN(trigrate) && TMath::Finite(trigrate))
598 query += Form("fMeanTriggerRate=%d, ", TMath::Nint(trigrate));
599 if (!TMath::IsNaN(l2prrate) && TMath::Finite(l2prrate))
600 query += Form("fL2RatePresc=%d, ", TMath::Nint(l2prrate));
601 if (!TMath::IsNaN(l2uprate) && TMath::Finite(l2uprate))
602 query += Form("fL2RateUnpresc=%d, ", TMath::Nint(l2uprate));
603 query += "fMagicNumberKEY=1, fExcludedFDAKEY=1";
604
605 cnt++;
606
607 //send query, add dataset to DB
608 if (serv.Insert("RunData", query)==kFALSE)
609 return -1;
610
611 TString query2=Form("fTelescopeNumber=%d, fRunNumber=%d, fFileNumber=%d, "
612 "fPriority=%d, fTimingCorrection='1970-01-01 00:00:00', fCompmux='1970-01-01 00:00:00'",
613 telnumber, runnumber, filenumber, runnumber);
614 if (testflagkey==3)
615 query2+=" , fDataCheckDone='1970-01-01 00:00:00'";
616
617 //create entry in table RunProcessStatus for this runnumber
618 if (serv.Insert("RunProcessStatus", query2)==kFALSE)
619 return -1;
620 }
621
622 return cnt;
623}
624
625// This tool will work from Period017 (2004_05_17) on...
626int filldotrun(const TString path="/home/lapalma/transfer/ccdata", Bool_t dummy=kTRUE)
627{
628 MSQLMagic serv("sql.rc");
629 if (!serv.IsConnected())
630 {
631 cout << "ERROR - Connection to database failed." << endl;
632 return 0;
633 }
634
635 cout << "filldotrun" << endl;
636 cout << "----------" << endl;
637 cout << endl;
638 cout << "Connected to " << serv.GetName() << endl;
639 cout << "Search Path: " << path << endl;
640 cout << endl;
641
642 serv.SetIsDummy(dummy);
643
644 if (path.EndsWith(".run"))
645 {
646 cout << path(TRegexp("CC_.*.run", kFALSE)) << flush;
647 Int_t n = insert(serv, dummy, path);
648 cout << " <" << n << "> " << (dummy?"DUMMY":"") << endl;
649
650 return n<0 ? 2 : 1;
651 }
652
653 MDirIter Next(path, "CC_*.run", -1);
654 while (1)
655 {
656 TString name = Next();
657 if (name.IsNull())
658 break;
659
660 cout << " * " << name(TRegexp("CC_.*.run", kFALSE)) << endl;
661 Int_t n = insert(serv, dummy, name);
662 cout << " <" << n << "> " << (dummy?"DUMMY":"") << endl;
663
664 if (n<0)
665 return 2;
666 }
667
668 return 1;
669}
Note: See TracBrowser for help on using the repository browser.