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

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