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

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