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

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