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

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