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

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