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

Last change on this file since 4878 was 4878, checked in by tbretz, 21 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// The macro can also be run without ACLiC but this is a lot slower...
62//
63// Remark: Running it from the commandline looks like this:
64// root -q -l -b filldotrun.C+\(\"path\"\,kFALSE\) 2>&1 | tee filldotrun.log
65//
66// Returns 0 in case of failure and 1 in case of success.
67//
68/////////////////////////////////////////////////////////////////////////////
69#include <iostream>
70#include <iomanip>
71#include <fstream>
72
73#include <TEnv.h>
74#include <TMath.h>
75#include <TRegexp.h>
76
77#include <TSQLRow.h>
78#include <TSQLResult.h>
79
80#include "MTime.h"
81#include "MDirIter.h"
82#include "MSQLServer.h"
83
84using namespace std;
85
86Bool_t ExistStr(MSQLServer &serv, const char *column, const char *table, const char *test)
87{
88 TString query(Form("SELECT %s FROM %s WHERE %s='%s'", column, table, column, test));
89 TSQLResult *res = serv.Query(query);
90 if (!res)
91 return kFALSE;
92
93 Bool_t rc = kFALSE;
94
95 TSQLRow *row=res->Next();
96 if (row && (*row)[0])
97 rc=kTRUE;
98
99 delete res;
100 return rc;
101}
102
103Int_t QueryNameKEY(MSQLServer &serv, Bool_t dummy, const char *col, const char *name, Bool_t insert=kTRUE)
104{
105 TString query;
106
107 query = Form("SELECT f%sKEY FROM MyMagic.%s WHERE f%sName='%s'", col, col, col, name);
108 TSQLResult *res = serv.Query(query);
109 if (!res)
110 return -1;
111
112 TSQLRow *row=res->Next();
113
114 Int_t rc = row && (*row)[0] ? atoi((*row)[0]) : -1;
115
116 delete res;
117
118 if (rc>=0)
119 return rc;
120
121 if (!insert)
122 return -1;
123
124 query = Form("INSERT MyMagic.%s (f%sName) VALUES (\"%s\");", col, col, name);
125
126 if (dummy)
127 {
128 cout << query << endl;
129 return 0;
130 }
131
132 res=serv.Query(query);
133 if (!res)
134 return -1;
135
136 delete res;
137
138 Int_t key = QueryNameKEY(serv, dummy, col, name, kFALSE);
139 if (key>0)
140 {
141 cout << "New " << col << ": " << name << endl;
142 return key;
143 }
144
145 cout << "ERROR: " << query << endl;
146 return kFALSE;
147}
148
149
150Int_t insert(MSQLServer &serv, Bool_t dummy, TString filename)
151{
152 ifstream fin(filename);
153 if (!fin)
154 {
155 cout << "Could not open file " << filename << endl;
156 return -1;
157 }
158
159 TString strng;
160 strng.ReadLine(fin);
161 if (strng!=TString("[CC Plain Run Summary File]"))
162 {
163 cout << filename << ": No Plain Run Summary File" << endl;
164 cout << "First Line: " << strng << endl;
165 cout << endl;
166 return -1;
167 }
168
169 strng.ReadLine(fin);
170 TRegexp reg("[0-9][0-9][0-9][0-9][0-9][0-9]-[0-9]");
171 TString arehucas = strng(reg);
172 arehucas.Prepend("20");
173 arehucas.ReplaceAll("-", "");
174
175 Int_t version = atoi(arehucas.Data());
176 if (version!=200405050 && version!=200405140 && version!=200405180)
177 {
178 cout << filename << ": File Version unknown - please update the macro!" << endl;
179 cout << "Second Line: " << strng << endl;
180 cout << endl;
181 return -1;
182 }
183
184 cout << " V" << version << " " << flush;
185
186 Int_t cnt=0;
187 while (1)
188 {
189 // ========== Col 1: Run Number =========
190 //Reading the line
191 //and converting some strings to ints/floats
192 strng.ReadToDelim(fin, ' ');
193 if (!fin)
194 break;
195
196 Int_t runnumber = atoi(strng.Data());
197
198 //runnumber=0 means no valid dataset
199 //-> continue
200 if (runnumber == 0)
201 {
202 strng.ReadLine(fin);
203 cout << "Runnumber == 0" << endl;
204 continue;
205 }
206
207 //cout << "RunNo: " << runnumber << " ";
208
209 if (ExistStr(serv, "fRunNumber", "MyMagic.RunData", strng.Data()))
210 {
211 // FIXME: Maybe we can implement an switch to update mode?
212 cout << "Run #" << runnumber << " already existing... skipped." << endl;
213 strng.ReadLine(fin);
214 continue;
215 }
216
217 // ========== Col 1: Run Type =========
218 strng.ReadToDelim(fin, ' ');
219 if (strng.Contains("???"))
220 strng="n/a";
221
222 Int_t runtype = QueryNameKEY(serv, dummy, "RunType", strng.Data(), kFALSE);
223 if (runtype<0)
224 {
225 cout << "ERROR - RunType " << strng << " not available." << endl;
226 strng.ReadLine(fin);
227 continue;
228 }
229
230 //cout << runtype << " ";
231
232 // ========== Col 2,3: Start Time =========
233 TString startdate, starttime;
234 startdate.ReadToDelim(fin, ' ');
235 starttime.ReadToDelim(fin, ' ');
236 //cout << startdate << " " << starttime << " ";
237
238 // ========== Col 4,5: Stop Time =========
239 TString stopdate, stoptime;
240 stopdate.ReadToDelim(fin, ' ');
241 stoptime.ReadToDelim(fin, ' ');
242 //cout << stopdate << " " << stoptime << " ";
243
244 if (startdate.Contains("???"))
245 startdate="0000-00-00";
246 if (starttime.Contains("???"))
247 starttime="00:00:00";
248 if (stopdate.Contains("???"))
249 stopdate="0000-00-00";
250 if (stoptime.Contains("???"))
251 stoptime="00:00:00";
252
253 // ========== Col 6: Source Name =========
254 strng.ReadToDelim(fin, ' ');
255 if (strng.Contains("???"))
256 strng="Unavailable";
257
258 Int_t sourcekey = QueryNameKEY(serv, dummy, "Source", strng.Data());
259 if (sourcekey<0)
260 {
261 strng.ReadLine(fin);
262 continue;
263 }
264 //cout << sourcekey << " ";
265
266 // ========== Col 7, 8: Local source position =========
267 strng.ReadToDelim(fin, ' ');
268 Float_t zd = atof(strng.Data());
269
270 strng.ReadToDelim(fin, ' ');
271 Float_t az = atof(strng.Data());
272
273 //cout << zd << " " << az << " ";
274
275 // ========== Col 9: Number of Events =========
276 strng.ReadToDelim(fin, ' ');
277 Int_t evtno = atoi(strng.Data());
278
279 //cout << evtno << " ";
280
281 // ========== Col 10: Project Name =========
282 strng.ReadToDelim(fin, ' ');
283 if (strng.Contains("???"))
284 strng="Unavailable";
285
286 Int_t projkey = QueryNameKEY(serv, dummy, "Project", strng.Data());
287 if (projkey<0)
288 {
289 strng.ReadLine(fin);
290 continue;
291 }
292 //cout << projkey << " ";
293
294 // ========== Col 10: Trigger Table Name =========
295 strng.ReadToDelim(fin, ' ');
296 if (strng.Contains("???"))
297 strng="n/a";
298
299 Int_t triggerkey = QueryNameKEY(serv, dummy, "TriggerTable", strng.Data());
300 if (triggerkey<0)
301 {
302 strng.ReadLine(fin);
303 continue;
304 }
305 //cout << triggerkey << " ";
306
307 // ========== Col 11-13: TrigRate, L2 UnPresc Rate, L2 Presc Rate ==========
308 strng.ReadToDelim(fin, ' ');
309 Float_t trigrate = atof(strng.Data());
310
311 strng.ReadToDelim(fin, ' ');
312 Float_t l2uprate = atof(strng.Data());
313
314 strng.ReadToDelim(fin, ' ');
315 Float_t l2prrate = atof(strng.Data());
316
317 // ========== Col 14,15: DaqRate, Storage Rate ==========
318 strng.ReadToDelim(fin, ' ');
319 Float_t daqrate = atof(strng.Data());
320
321 strng.ReadToDelim(fin, ' ');
322 Float_t storerate = atof(strng.Data());
323
324 // ========== Col 16: HV table =========
325 if (version==200405050 || version==200405140)
326 strng.ReadToDelim(fin, '\n');
327 else
328 strng.ReadToDelim(fin, ' ');
329 if (strng.Contains("???"))
330 strng="n/a";
331
332 Int_t hvkey = QueryNameKEY(serv, dummy, "HvSettings", strng.Data());
333 if (hvkey<0)
334 {
335 //strng.ReadLine(fin);
336 continue;
337 }
338
339 if (version==200405180)
340 strng.ReadLine(fin);
341
342 //continue;
343
344 //cout << endl;
345
346 // ================================================================
347 // ========== Data read from file now access the database =========
348 // ================================================================
349
350 //assemlbe the query that is needed to insert the values of this run
351 TString query;
352 query += "INSERT MyMagic.RunData SET ";
353
354 query += Form("fRunNumber=%d, ", runnumber);
355 query += Form("fRunTypeKEY=%d, ", runtype);
356 query += Form("fProjectKEY=%d, ", projkey);
357 query += Form("fSourceKEY=%d, ", sourcekey);
358 query += Form("fNumEvents=%d, ", evtno);
359 query += Form("fRunStart=\"%s %s\", ", startdate.Data(), starttime.Data());
360 query += Form("fRunStop=\"%s %s\", ", stopdate.Data(), stoptime.Data());
361 query += Form("fTriggerTableKEY=%d, ", triggerkey);
362 query += Form("fHvSettingsKEY=%d, ", hvkey);
363 if (!TMath::IsNaN(zd) && TMath::Finite(zd))
364 query += Form("fZenithDistance=%d, ", TMath::Nint(zd));
365 if (!TMath::IsNaN(az) && TMath::Finite(az))
366 query += Form("fAzimuth=%d, ", TMath::Nint(az));
367 if (!TMath::IsNaN(storerate) && TMath::Finite(storerate))
368 query += Form("fDaqStoreRate=%d, ", TMath::Nint(storerate));
369 if (!TMath::IsNaN(daqrate) && TMath::Finite(daqrate))
370 query += Form("fDaqTriggerRate=%d, ", TMath::Nint(daqrate));
371 if (!TMath::IsNaN(trigrate) && TMath::Finite(trigrate))
372 query += Form("fMeanTriggerRate=%d, ", TMath::Nint(trigrate));
373 if (!TMath::IsNaN(l2prrate) && TMath::Finite(l2prrate))
374 query += Form("fL2RatePresc=%d, ", TMath::Nint(l2prrate));
375 if (!TMath::IsNaN(l2uprate) && TMath::Finite(l2uprate))
376 query += Form("fL2RateUnpresc=%d, ", TMath::Nint(l2uprate));
377 query += "fMagicNumberKEY=1, fExcludedFDAKEY=1";
378
379 //cout << query << endl;
380 cnt++;
381
382 //cout << query << endl;
383 //continue;
384
385 if (dummy)
386 continue;
387
388 //send query, add dataset to DB
389 TSQLResult *res = serv.Query(query);
390 if (!res)
391 return -1;
392
393 delete res;
394 }
395
396 return cnt;
397
398}
399
400// This tool will work from Period017 (2004_05_17) on...
401int filldotrun(const TString path="/data/MAGIC/Period018/ccdata", Bool_t dummy=kTRUE)
402{
403 TEnv env("sql.rc");
404
405 MSQLServer serv(env);
406 if (!serv.IsConnected())
407 {
408 cout << "ERROR - Connection to database failed." << endl;
409 return;
410 }
411 cout << "filldotrun" << endl;
412 cout << "----------" << endl;
413 cout << endl;
414 cout << "Connected to " << serv.GetName() << endl;
415 cout << "Search Path: " << path << endl;
416 cout << endl;
417
418 if (path.EndsWith(".run"))
419 {
420 cout << path(TRegexp("CC_.*.run", kFALSE)) << flush;
421 Int_t n = insert(serv, dummy, path);
422 cout << " <" << n << "> " << (dummy?"DUMMY":"") << endl;
423
424 return n<0 ? 0 : 1;
425 }
426
427 MDirIter Next(path, "CC_*.run", -1);
428 while (1)
429 {
430 TString name = Next();
431 if (name.IsNull())
432 break;
433
434 cout << name(TRegexp("CC_.*.run", kFALSE)) << flush;
435 Int_t n = insert(serv, dummy, name);
436 cout << " <" << n << "> " << (dummy?"DUMMY":"") << endl;
437
438 if (n<0)
439 return 0;
440 }
441
442 return 1;
443}
Note: See TracBrowser for help on using the repository browser.