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

Last change on this file since 4680 was 4680, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 13.4 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 <MSQLServer.h>
74#include <TSQLRow.h>
75#include <TSQLResult.h>
76
77#include <TMath.h>
78#include <TRegexp.h>
79
80#include <MTime.h>
81#include <MDirIter.h>
82
83using namespace std;
84
85Bool_t ExistStr(MSQLServer &serv, const char *column, const char *table, const char *test)
86{
87 TString query(Form("SELECT %s FROM %s WHERE %s='%s'", column, table, column, test));
88 TSQLResult *res = serv.Query(query);
89 if (!res)
90 return kFALSE;
91
92 Bool_t rc = kFALSE;
93
94 TSQLRow *row=res->Next();
95 if (row && (*row)[0])
96 rc=kTRUE;
97
98 delete res;
99 return rc;
100}
101
102Int_t QueryNameKEY(MSQLServer &serv, Bool_t dummy, const char *col, const char *name, Bool_t insert=kTRUE)
103{
104 TString query;
105
106 query = Form("SELECT f%sKEY FROM MyMagic.%s WHERE f%sName='%s'", col, col, col, name);
107 TSQLResult *res = serv.Query(query);
108 if (!res)
109 {
110 cout << "ERROR - Query: " << query << endl;
111 return -1;
112 }
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 MyMagic.%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 {
137 cout << "ERROR - Query: " << query << endl;
138 return -1;
139 }
140 delete res;
141
142 Int_t key = QueryNameKEY(serv, dummy, col, name, kFALSE);
143 if (key>0)
144 {
145 cout << "New " << col << ": " << name << endl;
146 return key;
147 }
148
149 cout << "ERROR: " << query << endl;
150 return kFALSE;
151}
152
153
154Int_t insert(MSQLServer &serv, Bool_t dummy, TString filename)
155{
156 ifstream fin(filename);
157 if (!fin)
158 {
159 cout << "Could not open file " << fname << endl;
160 return -1;
161 }
162
163 TString strng;
164 strng.ReadLine(fin);
165 if (strng!=TString("[CC Plain Run Summary File]"))
166 {
167 cout << filename << ": No Plain Run Summary File" << endl;
168 cout << "First Line: " << strng << endl;
169 cout << endl;
170 return -1;
171 }
172
173 strng.ReadLine(fin);
174 TRegexp reg("[0-9][0-9][0-9][0-9][0-9][0-9]-[0-9]");
175 TString arehucas = strng(reg);
176 arehucas.Prepend("20");
177 arehucas.ReplaceAll("-", "");
178
179 Int_t version = atoi(arehucas.Data());
180 if (version!=200405050 && version!=200405140 && version!=200405180)
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 1: 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 2,3: Start Time =========
237 TString startdate, starttime;
238 startdate.ReadToDelim(fin, ' ');
239 starttime.ReadToDelim(fin, ' ');
240 //cout << startdate << " " << starttime << " ";
241
242 // ========== Col 4,5: 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 6: 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 7, 8: 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 9: Number of Events =========
280 strng.ReadToDelim(fin, ' ');
281 Int_t evtno = atoi(strng.Data());
282
283 //cout << evtno << " ";
284
285 // ========== Col 10: 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 10: Trigger Table Name =========
299 strng.ReadToDelim(fin, ' ');
300 if (strng.Contains("???"))
301 strng="n/a";
302
303 Int_t triggerkey = QueryNameKEY(serv, dummy, "TriggerTable", strng.Data());
304 if (triggerkey<0)
305 {
306 strng.ReadLine(fin);
307 continue;
308 }
309 //cout << triggerkey << " ";
310
311 // ========== Col 11-13: TrigRate, L2 UnPresc Rate, L2 Presc Rate ==========
312 strng.ReadToDelim(fin, ' ');
313 Float_t trigrate = atof(strng.Data());
314
315 strng.ReadToDelim(fin, ' ');
316 Float_t l2uprate = atof(strng.Data());
317
318 strng.ReadToDelim(fin, ' ');
319 Float_t l2prrate = atof(strng.Data());
320
321 // ========== Col 14,15: DaqRate, Storage Rate ==========
322 strng.ReadToDelim(fin, ' ');
323 Float_t daqrate = atof(strng.Data());
324
325 strng.ReadToDelim(fin, ' ');
326 Float_t storerate = atof(strng.Data());
327
328 // ========== Col 16: HV table =========
329 if (version==200405050 || version==200405140)
330 strng.ReadToDelim(fin, '\n');
331 else
332 strng.ReadToDelim(fin, ' ');
333 if (strng.Contains("???"))
334 strng="n/a";
335
336 Int_t hvkey = QueryNameKEY(serv, dummy, "HvSettings", strng.Data());
337 if (hvkey<0)
338 {
339 //strng.ReadLine(fin);
340 continue;
341 }
342
343 if (version==200405180)
344 strng.ReadLine(fin);
345
346 //continue;
347
348 //cout << endl;
349
350 // ================================================================
351 // ========== Data read from file now access the database =========
352 // ================================================================
353
354 //assemlbe the query that is needed to insert the values of this run
355 TString query;
356 query += "INSERT MyMagic.RunData SET ";
357
358 query += Form("fRunNumber=%d, ", runnumber);
359 query += Form("fRunTypeKEY=%d, ", runtype);
360 query += Form("fProjectKEY=%d, ", projkey);
361 query += Form("fSourceKEY=%d, ", sourcekey);
362 query += Form("fNumEvents=%d, ", evtno);
363 query += Form("fRunStart=\"%s %s\", ", startdate.Data(), starttime.Data());
364 query += Form("fRunStop=\"%s %s\", ", stopdate.Data(), stoptime.Data());
365 query += Form("fTriggerTableKEY=%d, ", triggerkey);
366 query += Form("fHvSettingsKEY=%d, ", hvkey);
367 if (!TMath::IsNaN(zd) && TMath::Finite(zd))
368 query += Form("fZenithDistance=%d, ", TMath::Nint(zd));
369 if (!TMath::IsNaN(az) && TMath::Finite(az))
370 query += Form("fAzimuth=%d, ", TMath::Nint(az));
371 if (!TMath::IsNaN(storerate) && TMath::Finite(storerate))
372 query += Form("fDaqStoreRate=%d, ", TMath::Nint(storerate));
373 if (!TMath::IsNaN(daqrate) && TMath::Finite(daqrate))
374 query += Form("fDaqTriggerRate=%d, ", TMath::Nint(daqrate));
375 if (!TMath::IsNaN(trigrate) && TMath::Finite(trigrate))
376 query += Form("fMeanTriggerRate=%d, ", TMath::Nint(trigrate));
377 if (!TMath::IsNaN(l2prrate) && TMath::Finite(l2prrate))
378 query += Form("fL2RatePresc=%d, ", TMath::Nint(l2prrate));
379 if (!TMath::IsNaN(l2uprate) && TMath::Finite(l2uprate))
380 query += Form("fL2RateUnpresc=%d, ", TMath::Nint(l2uprate));
381 query += "fMagicNumberKEY=1";
382
383 //cout << query << endl;
384 cnt++;
385
386 //cout << query << endl;
387 //continue;
388
389 if (dummy)
390 continue;
391
392 //send query, add dataset to DB
393 TSQLResult *res = serv.Query(query);
394 if (!res)
395 {
396 cout << query << " - FAILED!" << endl;
397 return -1;
398 }
399
400 delete res;
401 }
402
403 return cnt;
404
405}
406
407// This tool will work from Period017 (2004_05_17) on...
408void filldotrun(const char *path="/data/MAGIC/Period018/ccdata", Bool_t dummy=kTRUE)
409{
410 MSQLServer serv("mysql://hercules:d99swMT!@localhost");
411 if (!serv.IsConnected())
412 {
413 cout << "ERROR - Connection to database failed." << endl;
414 return;
415 }
416 cout << "filldotrun" << endl;
417 cout << "----------" << endl;
418 cout << endl;
419 cout << "Connected to " << serv.GetName() << endl;
420 cout << "Search Path: " << path << endl;
421 cout << endl;
422
423 if (path.EndsWith(".run"))
424 {
425 cout << name(TRegexp("CC_.*.run", kFALSE)) << flush;
426 Int_t n = insert(serv, dummy, name);
427 cout << " <" << n << "> " << (dummy?"DUMMY":"") << endl;
428
429 return n<0 ? 0 : 1;
430 }
431
432 MDirIter Next(path, "CC_*.run", -1);
433 while (1)
434 {
435 TString name = Next();
436 if (name.IsNull())
437 break;
438
439 cout << name(TRegexp("CC_.*.run", kFALSE)) << flush;
440 Int_t n = insert(serv, dummy, name);
441 cout << " <" << n << "> " << (dummy?"DUMMY":"") << endl;
442
443 if (n<0)
444 return 0;
445 }
446
447 return 1;
448}
Note: See TracBrowser for help on using the repository browser.