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

Last change on this file since 4635 was 4601, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 12.8 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//
50// The second argument is the 'dummy-mode'. If it is kTRUE dummy-mode is
51// switched on and nothing will be written into the database. Instead
52// informations about the subtables are displayed. This is usefull for tests
53// when adding a new arehucas version support. If it is kFALSE the information
54// are written into the subtables and the runs info is written into the
55// rundatabase.
56//
57// In the automatic case it makes sense to check the logfiles to make sure
58// that everything is fine...
59//
60// The macro can also be run without ACLiC but this is a lot slower...
61//
62// Remark: Running it from the commandline looks like this:
63// root -q -l -b filldotrun.C+\(\"path\"\,kFALSE\) 2>&1 | tee filldotrun.log
64//
65/////////////////////////////////////////////////////////////////////////////
66#include <iostream>
67#include <iomanip>
68#include <fstream>
69
70#include <MSQLServer.h>
71#include <TSQLRow.h>
72#include <TSQLResult.h>
73
74#include <TMath.h>
75#include <TRegexp.h>
76
77#include <MTime.h>
78#include <MDirIter.h>
79
80using namespace std;
81
82Bool_t ExistStr(MSQLServer &serv, const char *column, const char *table, const char *test)
83{
84 TString query(Form("SELECT %s FROM %s WHERE %s='%s'", column, table, column, test));
85 TSQLResult *res = serv.Query(query);
86 if (!res)
87 return kFALSE;
88
89 Bool_t rc = kFALSE;
90
91 TSQLRow *row=res->Next();
92 if (row && (*row)[0])
93 rc=kTRUE;
94
95 delete res;
96 return rc;
97}
98
99Int_t QueryNameKEY(MSQLServer &serv, Bool_t dummy, const char *col, const char *name, Bool_t insert=kTRUE)
100{
101 TString query;
102
103 query = Form("SELECT f%sKEY FROM MyMagic.%s WHERE f%sName='%s'", col, col, col, name);
104 TSQLResult *res = serv.Query(query);
105 if (!res)
106 {
107 cout << "ERROR - Query: " << query << endl;
108 return -1;
109 }
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 {
134 cout << "ERROR - Query: " << query << endl;
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
155 TString strng;
156 strng.ReadLine(fin);
157 if (strng!=TString("[CC Plain Run Summary File]"))
158 {
159 cout << filename << ": No Plain Run Summary File" << endl;
160 cout << "First Line: " << strng << endl;
161 cout << endl;
162 return 0;
163 }
164
165 strng.ReadLine(fin);
166 TRegexp reg("[0-9][0-9][0-9][0-9][0-9][0-9]-[0-9]");
167 TString arehucas = strng(reg);
168 arehucas.Prepend("20");
169 arehucas.ReplaceAll("-", "");
170
171 Int_t version = atoi(arehucas.Data());
172 if (version!=200405050 && version!=200405140 && version!=200405180)
173 {
174 cout << filename << ": File Version unknown - please update the macro!" << endl;
175 cout << "Second Line: " << strng << endl;
176 cout << endl;
177 return 0;
178 }
179
180 cout << " V" << version << " " << flush;
181
182 Int_t cnt=0;
183 while (1)
184 {
185 // ========== Col 1: Run Number =========
186 //Reading the line
187 //and converting some strings to ints/floats
188 strng.ReadToDelim(fin, ' ');
189 if (!fin)
190 break;
191
192 Int_t runnumber = atoi(strng.Data());
193
194 //runnumber=0 means no valid dataset
195 //-> continue
196 if (runnumber == 0)
197 {
198 strng.ReadLine(fin);
199 cout << "Runnumber == 0" << endl;
200 continue;
201 }
202
203 //cout << "RunNo: " << runnumber << " ";
204
205 if (ExistStr(serv, "fRunNumber", "MyMagic.RunData", strng.Data()))
206 {
207 // FIXME: Maybe we can implement an switch to update mode?
208 cout << "Run #" << runnumber << " already existing... skipped." << endl;
209 strng.ReadLine(fin);
210 continue;
211 }
212
213 // ========== Col 1: Run Type =========
214 strng.ReadToDelim(fin, ' ');
215 if (strng.Contains("???"))
216 strng="n/a";
217
218 Int_t runtype = QueryNameKEY(serv, dummy, "RunType", strng.Data(), kFALSE);
219 if (runtype<0)
220 {
221 cout << "ERROR - RunType " << strng << " not available." << endl;
222 strng.ReadLine(fin);
223 continue;
224 }
225
226 //cout << runtype << " ";
227
228 // ========== Col 2,3: Start Time =========
229 TString startdate, starttime;
230 startdate.ReadToDelim(fin, ' ');
231 starttime.ReadToDelim(fin, ' ');
232 //cout << startdate << " " << starttime << " ";
233
234 // ========== Col 4,5: Stop Time =========
235 TString stopdate, stoptime;
236 stopdate.ReadToDelim(fin, ' ');
237 stoptime.ReadToDelim(fin, ' ');
238 //cout << stopdate << " " << stoptime << " ";
239
240 if (startdate.Contains("???"))
241 startdate="0000-00-00";
242 if (starttime.Contains("???"))
243 starttime="00:00:00";
244 if (stopdate.Contains("???"))
245 stopdate="0000-00-00";
246 if (stoptime.Contains("???"))
247 stoptime="00:00:00";
248
249 // ========== Col 6: Source Name =========
250 strng.ReadToDelim(fin, ' ');
251 if (strng.Contains("???"))
252 strng="Unavailable";
253
254 Int_t sourcekey = QueryNameKEY(serv, dummy, "Source", strng.Data());
255 if (sourcekey<0)
256 {
257 strng.ReadLine(fin);
258 continue;
259 }
260 //cout << sourcekey << " ";
261
262 // ========== Col 7, 8: Local source position =========
263 strng.ReadToDelim(fin, ' ');
264 Float_t zd = atof(strng.Data());
265
266 strng.ReadToDelim(fin, ' ');
267 Float_t az = atof(strng.Data());
268
269 //cout << zd << " " << az << " ";
270
271 // ========== Col 9: Number of Events =========
272 strng.ReadToDelim(fin, ' ');
273 Int_t evtno = atoi(strng.Data());
274
275 //cout << evtno << " ";
276
277 // ========== Col 10: Project Name =========
278 strng.ReadToDelim(fin, ' ');
279 if (strng.Contains("???"))
280 strng="Unavailable";
281
282 Int_t projkey = QueryNameKEY(serv, dummy, "Project", strng.Data());
283 if (projkey<0)
284 {
285 strng.ReadLine(fin);
286 continue;
287 }
288 //cout << projkey << " ";
289
290 // ========== Col 10: Trigger Table Name =========
291 strng.ReadToDelim(fin, ' ');
292 if (strng.Contains("???"))
293 strng="n/a";
294
295 Int_t triggerkey = QueryNameKEY(serv, dummy, "TriggerTable", strng.Data());
296 if (triggerkey<0)
297 {
298 strng.ReadLine(fin);
299 continue;
300 }
301 //cout << triggerkey << " ";
302
303 // ========== Col 11-13: TrigRate, L2 UnPresc Rate, L2 Presc Rate ==========
304 strng.ReadToDelim(fin, ' ');
305 Float_t trigrate = atof(strng.Data());
306
307 strng.ReadToDelim(fin, ' ');
308 Float_t l2uprate = atof(strng.Data());
309
310 strng.ReadToDelim(fin, ' ');
311 Float_t l2prrate = atof(strng.Data());
312
313 // ========== Col 14,15: DaqRate, Storage Rate ==========
314 strng.ReadToDelim(fin, ' ');
315 Float_t daqrate = atof(strng.Data());
316
317 strng.ReadToDelim(fin, ' ');
318 Float_t storerate = atof(strng.Data());
319
320 // ========== Col 16: HV table =========
321 if (version==200405050 || version==200405140)
322 strng.ReadToDelim(fin, '\n');
323 else
324 strng.ReadToDelim(fin, ' ');
325 if (strng.Contains("???"))
326 strng="n/a";
327
328 Int_t hvkey = QueryNameKEY(serv, dummy, "HvSettings", strng.Data());
329 if (hvkey<0)
330 {
331 //strng.ReadLine(fin);
332 continue;
333 }
334
335 if (version==200405180)
336 strng.ReadLine(fin);
337
338 //continue;
339
340 //cout << endl;
341
342 // ================================================================
343 // ========== Data read from file now access the database =========
344 // ================================================================
345
346 //assemlbe the query that is needed to insert the values of this run
347 TString query;
348 query += "INSERT MyMagic.RunData SET ";
349
350 query += Form("fRunNumber=%d, ", runnumber);
351 query += Form("fRunTypeKEY=%d, ", runtype);
352 query += Form("fProjectKEY=%d, ", projkey);
353 query += Form("fSourceKEY=%d, ", sourcekey);
354 query += Form("fNumEvents=%d, ", evtno);
355 query += Form("fRunStart=\"%s %s\", ", startdate.Data(), starttime.Data());
356 query += Form("fRunStop=\"%s %s\", ", stopdate.Data(), stoptime.Data());
357 query += Form("fTriggerTableKEY=%d, ", triggerkey);
358 query += Form("fHvSettingsKEY=%d, ", hvkey);
359 if (!TMath::IsNaN(zd) && TMath::Finite(zd))
360 query += Form("fZenithDistance=%d, ", TMath::Nint(zd));
361 if (!TMath::IsNaN(az) && TMath::Finite(az))
362 query += Form("fAzimuth=%d, ", TMath::Nint(az));
363 if (!TMath::IsNaN(storerate) && TMath::Finite(storerate))
364 query += Form("fDaqStoreRate=%d, ", TMath::Nint(storerate));
365 if (!TMath::IsNaN(daqrate) && TMath::Finite(daqrate))
366 query += Form("fDaqTriggerRate=%d, ", TMath::Nint(daqrate));
367 if (!TMath::IsNaN(trigrate) && TMath::Finite(trigrate))
368 query += Form("fMeanTriggerRate=%d, ", TMath::Nint(trigrate));
369 if (!TMath::IsNaN(l2prrate) && TMath::Finite(l2prrate))
370 query += Form("fL2RatePresc=%d, ", TMath::Nint(l2prrate));
371 if (!TMath::IsNaN(l2uprate) && TMath::Finite(l2uprate))
372 query += Form("fL2RateUnpresc=%d, ", TMath::Nint(l2uprate));
373 query += "fMagicNumberKEY=1";
374
375 //cout << query << endl;
376 cnt++;
377
378 //cout << query << endl;
379 //continue;
380
381 if (dummy)
382 continue;
383
384 //send query, add dataset to DB
385 TSQLResult *res = serv.Query(query);
386 if (!res)
387 {
388 cout << query << " - FAILED!" << endl;
389 continue;
390 }
391
392 delete res;
393 }
394
395 return cnt;
396
397}
398
399// This tool will work from Period017 (2004_05_17) on...
400void filldotrun(const char *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 MDirIter Next(path, "CC_*.run", -1);
416 while (1)
417 {
418 TString name = Next();
419 if (name.IsNull())
420 break;
421
422 cout << name(TRegexp("CC_.*.run", kFALSE)) << flush;
423
424 Int_t n = insert(serv, dummy, name);
425
426 cout << " <" << n << "> " << (dummy?"DUMMY":"") << endl;
427 }
428}
Note: See TracBrowser for help on using the repository browser.