source: tags/Mars-V0.9.5/datacenter/macros/filldotrun.C

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