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

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