source: tags/Mars-V0.9/macros/sql/filldotrun.C

Last change on this file was 5830, checked in by Daniela Dorner, 20 years ago
*** empty log message ***
File size: 19.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-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//
40// Usage:
41// .x filldotrun.C+("/data/MAGIC/Period019/ccdata", kTRUE)
42//
43// While the first argument is the directory in which all subdirectories where
44// searches for CC_*.run files. All these files were analysed and the run
45// info will be put into the DB, eg:
46// "/data/MAGIC" would do it for all data
47// "/data/MAGIC/Period019/ccdata" would do it for one Period
48// "/data/MAGIC/Period019/ccdata/2004_05_17" would do it for a single day
49// "/data/MAGIC/Period019/ccdata/file.run" would do it for a single file
50//
51// The second argument is the 'dummy-mode'. If it is kTRUE dummy-mode is
52// switched on and nothing will be written into the database. Instead
53// informations about the subtables are displayed. This is usefull for tests
54// when adding a new arehucas version support. If it is kFALSE the information
55// are written into the subtables and the runs info is written into the
56// rundatabase.
57//
58// In the automatic case it makes sense to check the logfiles to make sure
59// that everything is fine...
60//
61// Make sure, that database and password are corretly set in a resource
62// file called sql.rc and the resource file is found.
63//
64// Remark: Running it from the commandline looks like this:
65// root -q -l -b filldotrun.C+\(\"path\"\,kFALSE\) 2>&1 | tee filldotrun.log
66//
67// Returns 0 in case of failure and 1 in case of success.
68//
69/////////////////////////////////////////////////////////////////////////////
70#include <iostream>
71#include <iomanip>
72#include <fstream>
73
74#include <TEnv.h>
75#include <TMath.h>
76#include <TRegexp.h>
77
78#include <TSQLRow.h>
79#include <TSQLResult.h>
80
81#include "MTime.h"
82#include "MDirIter.h"
83#include "MSQLServer.h"
84
85using namespace std;
86
87Bool_t ExistStr(MSQLServer &serv, const char *column, const char *table, const char *test)
88{
89 TString query(Form("SELECT %s FROM %s WHERE %s='%s'", column, table, column, test));
90 TSQLResult *res = serv.Query(query);
91 if (!res)
92 return kFALSE;
93
94 Bool_t rc = kFALSE;
95
96 TSQLRow *row=res->Next();
97 if (row && (*row)[0])
98 rc=kTRUE;
99
100 delete res;
101 return rc;
102}
103
104Int_t QueryNameKEY(MSQLServer &serv, Bool_t dummy, const char *col, const char *name, Bool_t insert=kTRUE)
105{
106 TString query;
107
108 query = Form("SELECT f%sKEY FROM MyMagic.%s WHERE f%sName='%s'", col, col, col, name);
109 TSQLResult *res = serv.Query(query);
110 if (!res)
111 return -1;
112
113 TSQLRow *row=res->Next();
114
115 Int_t rc = row && (*row)[0] ? atoi((*row)[0]) : -1;
116
117 delete res;
118
119 if (rc>=0)
120 return rc;
121
122 if (!insert)
123 return -1;
124
125 query = Form("INSERT MyMagic.%s (f%sName) VALUES (\"%s\");", col, col, name);
126
127 if (dummy)
128 {
129 cout << query << endl;
130 return 0;
131 }
132
133 res=serv.Query(query);
134 if (!res)
135 return -1;
136
137 delete res;
138
139 Int_t key = QueryNameKEY(serv, dummy, col, name, kFALSE);
140 if (key>0)
141 {
142 cout << "New " << col << ": " << name << endl;
143 return key;
144 }
145
146 cout << "ERROR: " << query << endl;
147 return kFALSE;
148}
149
150
151Int_t insert(MSQLServer &serv, Bool_t dummy, TString filename)
152{
153 ifstream fin(filename);
154 if (!fin)
155 {
156 cout << "Could not open file " << filename << endl;
157 return -1;
158 }
159
160 TString strng;
161 strng.ReadLine(fin);
162 if (strng!=TString("[CC Plain Run Summary File]"))
163 {
164 cout << filename << ": No Plain Run Summary File" << endl;
165 cout << "First Line: " << strng << endl;
166 cout << endl;
167 return -1;
168 }
169
170 strng.ReadLine(fin);
171 TRegexp reg("[0-9][0-9][0-9][0-9][0-9][0-9]-[0-9]");
172 TString arehucas = strng(reg);
173 arehucas.Prepend("20");
174 arehucas.ReplaceAll("-", "");
175
176 Int_t version = atoi(arehucas.Data());
177 if (version!=200405050 && version!=200405140 && version!=200405180
178 && version!=200407270 && version!=200411130 &&
179 version!=200412090 && version!=200412210)
180 {
181 cout << filename << ": File Version unknown - please update the macro!" << endl;
182 cout << "Second Line: " << strng << endl;
183 cout << endl;
184 return -1;
185 }
186
187 cout << " V" << version << " " << flush;
188
189 Int_t cnt=0;
190 while (1)
191 {
192 // ========== Col 1: Run Number =========
193 //Reading the line
194 //and converting some strings to ints/floats
195 strng.ReadToDelim(fin, ' ');
196 if (!fin)
197 break;
198
199 Int_t runnumber = atoi(strng.Data());
200
201 //runnumber=0 means no valid dataset
202 //-> continue
203 if (runnumber == 0)
204 {
205 strng.ReadLine(fin);
206 cout << "Runnumber == 0" << endl;
207 continue;
208 }
209
210 //cout << "RunNo: " << runnumber << " ";
211
212 if (ExistStr(serv, "fRunNumber", "MyMagic.RunData", strng.Data()))
213 {
214 // FIXME: Maybe we can implement an switch to update mode?
215 cout << "Run #" << runnumber << " 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 = QueryNameKEY(serv, dummy, "RunType", strng.Data(), 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 = QueryNameKEY(serv, dummy, "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 = QueryNameKEY(serv, dummy, "Project", strng.Data());
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 if (version >=200411130)
304 {
305 Int_t l1triggerkey = QueryNameKEY(serv, dummy, "L1TriggerTable", strng.Data());
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 Int_t l2triggerkey = QueryNameKEY(serv, dummy, "L2TriggerTable", strng.Data());
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 Int_t l2triggerkey = QueryNameKEY(serv, dummy, "L2TriggerTable", strng.Data());
341 if (l2triggerkey<0)
342 {
343 strng.ReadLine(fin);
344 continue;
345 }
346
347 strng="n/a";
348 Int_t 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 Int_t l1triggerkey = QueryNameKEY(serv, dummy, "L1TriggerTable", L1TT.Data());
359 if (l1triggerkey<0)
360 {
361 strng.ReadLine(fin);
362 continue;
363 }
364
365 Int_t l2triggerkey = QueryNameKEY(serv, dummy, "L2TriggerTable", L2TT.Data());
366 if (l2triggerkey<0)
367 {
368 strng.ReadLine(fin);
369 continue;
370 }
371
372 break;
373 }
374 case 2:
375 {
376 Int_t l1triggerkey = QueryNameKEY(serv, dummy, "L1TriggerTable", strng.Data());
377 if (l1triggerkey<0)
378 {
379 strng.ReadLine(fin);
380 continue;
381 }
382
383 strng="n/a";
384 Int_t l2triggerkey = 1;
385
386 break;
387 }
388 case 3:
389 {
390 Int_t l1triggerkey = 1;
391 Int_t l2triggerkey = 1;
392 break;
393 }
394 default:
395 {
396 cout << "WARNING: neiter 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 = QueryNameKEY(serv, dummy, "HvSettings", strng.Data());
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 if (version==200411130 || version==200412090 || version==200412210)
438 {
439 // ========== Col 19-35: DC and HV-values, mjd =========
440 for (int i=0 ; i<17 ; i++)
441 {
442 strng.ReadToDelim(fin, ' ');
443 }
444
445 // ========== Col 36: test-flag =========
446 strng.ReadToDelim(fin, ' ');
447 if (strng.Contains("???"))
448 strng="n/a";
449
450 Int_t testflagkey = QueryNameKEY(serv, dummy, "TestFlag", strng.Data());
451 if (testflagkey<0)
452 {
453 strng.ReadLine(fin);
454 continue;
455 }
456
457 // ========== Col 37: light conditions =========
458 strng.ReadToDelim(fin, ' ');
459 if (strng.Contains("???"))
460 strng="n/a";
461
462 Int_t lightcondkey = QueryNameKEY(serv, dummy, "LightConditions", strng.Data());
463 if (lightcondkey<0)
464 {
465 strng.ReadLine(fin);
466 continue;
467 }
468
469 // ========== Col 38: discriminator threshold table =========
470 strng.ReadToDelim(fin, ' ');
471 if (strng.Contains("???"))
472 strng="n/a";
473
474 Int_t dttablekey = QueryNameKEY(serv, dummy, "DiscriminatorThresholdTable", strng.Data());
475 if (dttablekey<0)
476 {
477 strng.ReadLine(fin);
478 continue;
479 }
480
481 // ========== Col 39: trigger delay table =========
482 strng.ReadToDelim(fin, ' ');
483 if (strng.Contains("???"))
484 strng="n/a";
485
486 Int_t triggerdelaytablekey = QueryNameKEY(serv, dummy, "TriggerDelayTable", strng.Data());
487 if (triggerdelaytablekey<0)
488 {
489 strng.ReadLine(fin);
490 continue;
491 }
492
493 // ========== Col 40,41: RA and Dec sent to drive =========
494 strng.ReadToDelim(fin, ' ');
495 strng.ReadToDelim(fin, ' ');
496
497 // ========== Col 42: Calibration Script =========
498 strng.ReadToDelim(fin, ' ');
499 if (strng.Contains("???"))
500 strng="n/a";
501
502 Int_t calibrationscriptkey = QueryNameKEY(serv, dummy, "CalibrationScript", strng.Data());
503 if (calibrationscriptkey<0)
504 {
505 strng.ReadLine(fin);
506 continue;
507 }
508
509 }
510 else
511 {
512 Int_t testflagkey, lightcondkey, dttablekey, triggerdelaytablekey, calibrationscriptkey=1;
513 }
514
515
516 //continue;
517
518 //cout << endl;
519
520 // ================================================================
521 // ========== Data read from file now access the database =========
522 // ================================================================
523
524 //assemlbe the query that is needed to insert the values of this run
525 TString query;
526 query += "INSERT MyMagic.RunData SET ";
527
528 query += Form("fRunNumber=%d, ", runnumber);
529 query += Form("fRunTypeKEY=%d, ", runtype);
530 query += Form("fProjectKEY=%d, ", projkey);
531 query += Form("fSourceKEY=%d, ", sourcekey);
532 query += Form("fNumEvents=%d, ", evtno);
533 query += Form("fRunStart=\"%s %s\", ", startdate.Data(), starttime.Data());
534 query += Form("fRunStop=\"%s %s\", ", stopdate.Data(), stoptime.Data());
535 query += Form("fL1TriggerTableKEY=%d, ", l1triggerkey);
536 query += Form("fL2TriggerTableKEY=%d, ", l2triggerkey);
537 query += Form("fTestFlagKEY=%d, ", testflagkey);
538 query += Form("fCalibrationScriptKEY=%d, ", calibrationscriptkey);
539 query += Form("fTriggerDelayTableKEY=%d, ", triggerdelaytablekey);
540 query += Form("fDiscrimintorThresholdTableKEY=%d, ", dttablekey);
541 query += Form("fLightConditionsKEY=%d, ", lightcondkey);
542 query += Form("fHvSettingsKEY=%d, ", hvkey);
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 //cout << query << endl;
560 cnt++;
561
562 //cout << query << endl;
563 //continue;
564
565 if (dummy)
566 continue;
567
568 //send query, add dataset to DB
569 TSQLResult *res = serv.Query(query);
570 if (!res)
571 return -1;
572
573 delete res;
574 }
575
576 return cnt;
577
578}
579
580// This tool will work from Period017 (2004_05_17) on...
581int filldotrun(const TString path="/data/MAGIC/Period018/ccdata", Bool_t dummy=kTRUE)
582{
583 TEnv env("sql.rc");
584
585 MSQLServer serv(env);
586 if (!serv.IsConnected())
587 {
588 cout << "ERROR - Connection to database failed." << endl;
589 return;
590 }
591 cout << "filldotrun" << endl;
592 cout << "----------" << endl;
593 cout << endl;
594 cout << "Connected to " << serv.GetName() << endl;
595 cout << "Search Path: " << path << endl;
596 cout << endl;
597
598 if (path.EndsWith(".run"))
599 {
600 cout << path(TRegexp("CC_.*.run", kFALSE)) << flush;
601 Int_t n = insert(serv, dummy, path);
602 cout << " <" << n << "> " << (dummy?"DUMMY":"") << endl;
603
604 return n<0 ? 0 : 1;
605 }
606
607 MDirIter Next(path, "CC_*.run", -1);
608 while (1)
609 {
610 TString name = Next();
611 if (name.IsNull())
612 break;
613
614 cout << name(TRegexp("CC_.*.run", kFALSE)) << flush;
615 Int_t n = insert(serv, dummy, name);
616 cout << " <" << n << "> " << (dummy?"DUMMY":"") << endl;
617
618 if (n<0)
619 return 0;
620 }
621
622 return 1;
623}
Note: See TracBrowser for help on using the repository browser.