source: trunk/MagicSoft/Mars/macros/sql/filldotrun.C@ 5691

Last change on this file since 5691 was 4879, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 13.2 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
37// 040514-0
38// 040518-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 {
179 cout << filename << ": File Version unknown - please update the macro!" << endl;
180 cout << "Second Line: " << strng << endl;
181 cout << endl;
182 return -1;
183 }
184
185 cout << " V" << version << " " << flush;
186
187 Int_t cnt=0;
188 while (1)
189 {
190 // ========== Col 1: Run Number =========
191 //Reading the line
192 //and converting some strings to ints/floats
193 strng.ReadToDelim(fin, ' ');
194 if (!fin)
195 break;
196
197 Int_t runnumber = atoi(strng.Data());
198
199 //runnumber=0 means no valid dataset
200 //-> continue
201 if (runnumber == 0)
202 {
203 strng.ReadLine(fin);
204 cout << "Runnumber == 0" << endl;
205 continue;
206 }
207
208 //cout << "RunNo: " << runnumber << " ";
209
210 if (ExistStr(serv, "fRunNumber", "MyMagic.RunData", strng.Data()))
211 {
212 // FIXME: Maybe we can implement an switch to update mode?
213 cout << "Run #" << runnumber << " already existing... skipped." << endl;
214 strng.ReadLine(fin);
215 continue;
216 }
217
218 // ========== Col 1: Run Type =========
219 strng.ReadToDelim(fin, ' ');
220 if (strng.Contains("???"))
221 strng="n/a";
222
223 Int_t runtype = QueryNameKEY(serv, dummy, "RunType", strng.Data(), kFALSE);
224 if (runtype<0)
225 {
226 cout << "ERROR - RunType " << strng << " not available." << endl;
227 strng.ReadLine(fin);
228 continue;
229 }
230
231 //cout << runtype << " ";
232
233 // ========== Col 2,3: Start Time =========
234 TString startdate, starttime;
235 startdate.ReadToDelim(fin, ' ');
236 starttime.ReadToDelim(fin, ' ');
237 //cout << startdate << " " << starttime << " ";
238
239 // ========== Col 4,5: Stop Time =========
240 TString stopdate, stoptime;
241 stopdate.ReadToDelim(fin, ' ');
242 stoptime.ReadToDelim(fin, ' ');
243 //cout << stopdate << " " << stoptime << " ";
244
245 if (startdate.Contains("???"))
246 startdate="0000-00-00";
247 if (starttime.Contains("???"))
248 starttime="00:00:00";
249 if (stopdate.Contains("???"))
250 stopdate="0000-00-00";
251 if (stoptime.Contains("???"))
252 stoptime="00:00:00";
253
254 // ========== Col 6: Source Name =========
255 strng.ReadToDelim(fin, ' ');
256 if (strng.Contains("???"))
257 strng="Unavailable";
258
259 Int_t sourcekey = QueryNameKEY(serv, dummy, "Source", strng.Data());
260 if (sourcekey<0)
261 {
262 strng.ReadLine(fin);
263 continue;
264 }
265 //cout << sourcekey << " ";
266
267 // ========== Col 7, 8: Local source position =========
268 strng.ReadToDelim(fin, ' ');
269 Float_t zd = atof(strng.Data());
270
271 strng.ReadToDelim(fin, ' ');
272 Float_t az = atof(strng.Data());
273
274 //cout << zd << " " << az << " ";
275
276 // ========== Col 9: Number of Events =========
277 strng.ReadToDelim(fin, ' ');
278 Int_t evtno = atoi(strng.Data());
279
280 //cout << evtno << " ";
281
282 // ========== Col 10: Project Name =========
283 strng.ReadToDelim(fin, ' ');
284 if (strng.Contains("???"))
285 strng="Unavailable";
286
287 Int_t projkey = QueryNameKEY(serv, dummy, "Project", strng.Data());
288 if (projkey<0)
289 {
290 strng.ReadLine(fin);
291 continue;
292 }
293 //cout << projkey << " ";
294
295 // ========== Col 10: Trigger Table Name =========
296 strng.ReadToDelim(fin, ' ');
297 if (strng.Contains("???"))
298 strng="n/a";
299
300 Int_t triggerkey = QueryNameKEY(serv, dummy, "TriggerTable", strng.Data());
301 if (triggerkey<0)
302 {
303 strng.ReadLine(fin);
304 continue;
305 }
306 //cout << triggerkey << " ";
307
308 // ========== Col 11-13: TrigRate, L2 UnPresc Rate, L2 Presc Rate ==========
309 strng.ReadToDelim(fin, ' ');
310 Float_t trigrate = atof(strng.Data());
311
312 strng.ReadToDelim(fin, ' ');
313 Float_t l2uprate = atof(strng.Data());
314
315 strng.ReadToDelim(fin, ' ');
316 Float_t l2prrate = atof(strng.Data());
317
318 // ========== Col 14,15: DaqRate, Storage Rate ==========
319 strng.ReadToDelim(fin, ' ');
320 Float_t daqrate = atof(strng.Data());
321
322 strng.ReadToDelim(fin, ' ');
323 Float_t storerate = atof(strng.Data());
324
325 // ========== Col 16: HV table =========
326 if (version==200405050 || version==200405140)
327 strng.ReadToDelim(fin, '\n');
328 else
329 strng.ReadToDelim(fin, ' ');
330 if (strng.Contains("???"))
331 strng="n/a";
332
333 Int_t hvkey = QueryNameKEY(serv, dummy, "HvSettings", strng.Data());
334 if (hvkey<0)
335 {
336 //strng.ReadLine(fin);
337 continue;
338 }
339
340 if (version==200405180)
341 strng.ReadLine(fin);
342
343 //continue;
344
345 //cout << endl;
346
347 // ================================================================
348 // ========== Data read from file now access the database =========
349 // ================================================================
350
351 //assemlbe the query that is needed to insert the values of this run
352 TString query;
353 query += "INSERT MyMagic.RunData SET ";
354
355 query += Form("fRunNumber=%d, ", runnumber);
356 query += Form("fRunTypeKEY=%d, ", runtype);
357 query += Form("fProjectKEY=%d, ", projkey);
358 query += Form("fSourceKEY=%d, ", sourcekey);
359 query += Form("fNumEvents=%d, ", evtno);
360 query += Form("fRunStart=\"%s %s\", ", startdate.Data(), starttime.Data());
361 query += Form("fRunStop=\"%s %s\", ", stopdate.Data(), stoptime.Data());
362 query += Form("fTriggerTableKEY=%d, ", triggerkey);
363 query += Form("fHvSettingsKEY=%d, ", hvkey);
364 if (!TMath::IsNaN(zd) && TMath::Finite(zd))
365 query += Form("fZenithDistance=%d, ", TMath::Nint(zd));
366 if (!TMath::IsNaN(az) && TMath::Finite(az))
367 query += Form("fAzimuth=%d, ", TMath::Nint(az));
368 if (!TMath::IsNaN(storerate) && TMath::Finite(storerate))
369 query += Form("fDaqStoreRate=%d, ", TMath::Nint(storerate));
370 if (!TMath::IsNaN(daqrate) && TMath::Finite(daqrate))
371 query += Form("fDaqTriggerRate=%d, ", TMath::Nint(daqrate));
372 if (!TMath::IsNaN(trigrate) && TMath::Finite(trigrate))
373 query += Form("fMeanTriggerRate=%d, ", TMath::Nint(trigrate));
374 if (!TMath::IsNaN(l2prrate) && TMath::Finite(l2prrate))
375 query += Form("fL2RatePresc=%d, ", TMath::Nint(l2prrate));
376 if (!TMath::IsNaN(l2uprate) && TMath::Finite(l2uprate))
377 query += Form("fL2RateUnpresc=%d, ", TMath::Nint(l2uprate));
378 query += "fMagicNumberKEY=1, fExcludedFDAKEY=1";
379
380 //cout << query << endl;
381 cnt++;
382
383 //cout << query << endl;
384 //continue;
385
386 if (dummy)
387 continue;
388
389 //send query, add dataset to DB
390 TSQLResult *res = serv.Query(query);
391 if (!res)
392 return -1;
393
394 delete res;
395 }
396
397 return cnt;
398
399}
400
401// This tool will work from Period017 (2004_05_17) on...
402int filldotrun(const TString path="/data/MAGIC/Period018/ccdata", Bool_t dummy=kTRUE)
403{
404 TEnv env("sql.rc");
405
406 MSQLServer serv(env);
407 if (!serv.IsConnected())
408 {
409 cout << "ERROR - Connection to database failed." << endl;
410 return;
411 }
412 cout << "filldotrun" << endl;
413 cout << "----------" << endl;
414 cout << endl;
415 cout << "Connected to " << serv.GetName() << endl;
416 cout << "Search Path: " << path << endl;
417 cout << endl;
418
419 if (path.EndsWith(".run"))
420 {
421 cout << path(TRegexp("CC_.*.run", kFALSE)) << flush;
422 Int_t n = insert(serv, dummy, path);
423 cout << " <" << n << "> " << (dummy?"DUMMY":"") << endl;
424
425 return n<0 ? 0 : 1;
426 }
427
428 MDirIter Next(path, "CC_*.run", -1);
429 while (1)
430 {
431 TString name = Next();
432 if (name.IsNull())
433 break;
434
435 cout << name(TRegexp("CC_.*.run", kFALSE)) << flush;
436 Int_t n = insert(serv, dummy, name);
437 cout << " <" << n << "> " << (dummy?"DUMMY":"") << endl;
438
439 if (n<0)
440 return 0;
441 }
442
443 return 1;
444}
Note: See TracBrowser for help on using the repository browser.