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

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