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

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