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

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