source: trunk/Mars/datacenter/macros/filldotraw.C@ 18401

Last change on this file since 18401 was 8061, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 8.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): Thomas Bretz, 08/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
19! Author(s): Daniela Dorner, 08/2004 <mailto:dorner@astro.uni-wuerzburg.de>
20!
21! Copyright: MAGIC Software Development, 2000-2006
22!
23!
24\* ======================================================================== */
25
26/////////////////////////////////////////////////////////////////////////////
27//
28// filldotraw.C
29// ============
30//
31// This macro is used to read a merpped raw data file or a raw data file
32// directly. The descision is taken by the file-name extension (".root" or
33// ".raw")
34//
35// Usage:
36// .x filldotraw.C("/data/MAGIC/Period014/filename.raw", kTRUE)
37//
38// The second argument is the 'dummy-mode'. If it is kTRUE dummy-mode is
39// switched on and nothing will be written into the database. This is usefull
40// for tests.
41//
42// Filling the database is done with 'UPADTE' for _all_ columns
43// matching the Run-Number!
44//
45// The macro can also be run without ACLiC but this is a lot slower...
46//
47// Remark: Running it from the commandline looks like this:
48// root -q -l -b filldotraw.C+\(\"filename\"\,kFALSE\) 2>&1 | tee filldotraw.log
49//
50// Make sure, that database and password are corretly set in a resource
51// file called sql.rc and the resource file is found.
52//
53// Returns 2 in case of failure, 1 in case of success and 0 if the connection
54// to the database is not working.
55//
56/////////////////////////////////////////////////////////////////////////////
57
58#include <fstream>
59#include <iostream>
60
61#include <TEnv.h>
62#include <TFile.h>
63#include <TTree.h>
64#include <TRegexp.h>
65
66#include <TSQLRow.h>
67#include <TSQLResult.h>
68
69#include "MZlib.h"
70#include "MSQLMagic.h"
71#include "MRawRunHeader.h"
72#include "MDirIter.h"
73
74using namespace std;
75
76Int_t QueryFromName(MSQLMagic &serv, const char *col, const char *val)
77{
78 const TString query1 = Form("SELECT f%sKEY FROM %s WHERE f%s='%s'",
79 col, col, col, val);
80
81 TSQLResult *res1 = serv.Query(query1);
82 if (!res1)
83 {
84 cout << "ERROR - Query has failed: " << query1 << endl;
85 return -1;
86 }
87
88 TSQLRow *row=res1->Next();
89
90 const Int_t rc1 = row && (*row)[0] ? atoi((*row)[0]) : -1;
91 delete res1;
92 return rc1;
93}
94
95//get key for a magic number
96Int_t MagicNumber(MSQLMagic &serv, const MRawRunHeader &h)
97{
98 return QueryFromName(serv, "MagicNumber", Form("%d", h.GetMagicNumber()));
99}
100
101Bool_t ReadRaw(TString fname, MRawRunHeader &h)
102{
103 MZlib fin(fname);
104 if (!fin)
105 {
106 cout << "ERROR - Couldn't open file " << fname << endl;
107 return kFALSE;
108 }
109
110 if (!h.ReadEvt(fin))
111 {
112 cout << "ERROR - Reading header from file " << fname << endl;
113 return kFALSE;
114 }
115 return kTRUE;
116}
117
118Bool_t ReadRoot(TString fname, MRawRunHeader *h)
119{
120 TFile file(fname, "READ");
121 if (file.IsZombie())
122 {
123 cout << "ERROR - Cannot open file " << fname << endl;
124 return kFALSE;
125 }
126
127 TTree *t = (TTree*)file.Get("RunHeaders");
128 if (!t)
129 {
130 cout << "ERROR - Tree RunHeaders not found." << endl;
131 return kFALSE;
132 }
133
134 t->SetBranchAddress("MRawRunHeader.", &h);
135 t->GetEntry(0);
136
137 return kTRUE;
138}
139
140Bool_t CheckRunNumber(MSQLMagic &serv, Int_t num)
141{
142 TString query(Form("SELECT fRunNumber from RunData where fRunNumber=%d", num));
143
144 TSQLResult *res = serv.Query(query);
145 if (!res)
146 {
147 cout << "ERROR - Query failed: " << query << endl;
148 return kFALSE;
149 }
150
151 TSQLRow *row = res->Next();
152
153 Bool_t rc = row && (*row)[0] ? atoi((*row)[0])==num : kFALSE;
154 delete res;
155 return rc;
156
157}
158
159Bool_t InsertEntry(MSQLMagic &serv, MRawRunHeader &h)
160{
161 const Int_t magickey = MagicNumber(serv, h);
162 const Int_t runkey = QueryFromName(serv, "RunType", h.GetRunTypeStr());
163 const Int_t projkey = serv.QueryKeyOfName("Project", h.GetProjectName());
164 const Int_t sourcekey = serv.QueryKeyOfName("Source", h.GetSourceName());
165 const Int_t modekey = serv.QueryKeyOfName("ObservationMode", h.GetObservationMode());
166
167 if (magickey<0 || runkey<0 || projkey<0 || sourcekey<0 || modekey<0)
168 return -1;
169
170 TString query;
171
172 query += Form("fRunNumber=%d, ", h.GetRunNumber());
173 query += Form("fMagicNumberKEY=%d, ", magickey);
174 query += Form("fFormatVersion=%d, ", h.GetFormatVersion());
175 query += Form("fRunTypeKEY=%d, ", runkey);
176 query += Form("fProjectKEY=%d, ", projkey);
177 query += Form("fSourceKEY=%d, ", sourcekey);
178 query += Form("fNumEvents=%d, ", h.GetNumEvents());
179 query += Form("fRunStart='%s', ", h.GetRunStart().GetSqlDateTime().Data());
180 query += Form("fRunStop='%s', ", h.GetRunEnd().GetSqlDateTime().Data());
181 query += Form("fObservationModeKEY=%d, ", modekey);
182
183 query += "fExcludedFDAKEY=1, fTestFlagKEY=1, fLightConditionsKEY=1, ";
184 query += "fCalibrationScriptKEY=1, fDiscriminatorThresholdTableKEY=1, ";
185 query += "fTriggerDelayTableKEY=1, fL1TriggerTableKEY=1, fL2TriggerTableKEY=1, ";
186 query += "fHvSettingsKEY=1, fZenithDistance=0, fAzimuth=0, ";
187 query += "fDaqStoreRate=0, fDaqTriggerRate=0, fMeanTRiggerRate=0, ";
188 query += "fL2RatePresc=0, fL2RateUnpresc=0 ";
189
190 return serv.Insert("RunData", query);
191}
192
193Int_t UpdateEntry(MSQLMagic &serv, MRawRunHeader &h)
194{
195 //get key for the magic number
196 const Int_t key = MagicNumber(serv, h);
197 if (key<0)
198 return -1;
199
200 TString vars(Form("fMagicNumberKEY=%d, fFormatVersion=%d",
201 key, h.GetFormatVersion()));
202 TString where(Form("fRunNumber=%d", h.GetRunNumber()));
203
204 return serv.Update("RunData", vars, where);
205}
206
207
208int Process(MSQLMagic &serv, TString fname, Bool_t dummy)
209{
210 MRawRunHeader h;
211
212 //read header either from root or from raw file
213 if (fname.EndsWith(".root"))
214 ReadRoot(fname, &h);
215 if (fname.EndsWith(".raw"))
216 ReadRaw(fname, h);
217 if (fname.EndsWith(".raw.gz"))
218 ReadRaw(fname, h);
219
220 if (dummy)
221 h.Print("header");
222
223 Int_t rc = CheckRunNumber(serv, h.GetRunNumber()) ?
224 UpdateEntry(serv, h) : InsertEntry(serv, h);
225
226 return rc==0 ? 2 : 1;
227}
228
229int filldotraw(TString fname, Bool_t dummy=kTRUE)
230{
231 TEnv env("sql.rc");
232
233 MSQLMagic serv(env);
234 if (!serv.IsConnected())
235 {
236 cout << "ERROR - Connection to database failed." << endl;
237 return 0;
238 }
239
240 serv.SetIsDummy(dummy);
241
242 cout << "filldotraw" << endl;
243 cout << "----------" << endl;
244 cout << endl;
245 cout << "Connected to " << serv.GetName() << endl;
246 cout << "File: " << fname << endl;
247 cout << endl;
248
249 return Process(serv, fname, dummy);
250}
251
252int filldotraw(Int_t runno, Bool_t dummy=kTRUE)
253{
254 TEnv env("sql.rc");
255
256 MSQLMagic serv(env);
257 if (!serv.IsConnected())
258 {
259 cout << "ERROR - Connection to database failed." << endl;
260 return 0;
261 }
262
263 serv.SetIsDummy(dummy);
264
265 cout << "filldotraw" << endl;
266 cout << "----------" << endl;
267 cout << endl;
268 cout << "Connected to " << serv.GetName() << endl;
269 cout << "Run: " << runno << endl;
270 cout << endl;
271
272 //get date for the run to build path of the file
273 TString query(Form("SELECT DATE_FORMAT(ADDDATE(fRunStart, Interval 13 HOUR), '%%Y/%%m/%%d') FROM RunData WHERE fRunNumber=%d",
274 runno));
275
276 TSQLResult *res = serv.Query(query);
277 if (!res)
278 {
279 cout << "ERROR - Query failed: " << query << endl;
280 return 2;
281 }
282
283 TSQLRow *row = 0;
284 row = res->Next();
285 TString date=(*row)[0];
286 cout << "date: " << date << endl;
287 TString path(Form("/magic/data/rawfiles/%s", date.Data()));
288 TString file(Form("*%d_*_*_E.raw.?g?z?", runno));
289
290 delete res;
291
292 cout << "path: " << path << " - file : " << file << endl;
293 TString fname;
294 TString name;
295
296 Int_t count=0;
297 MDirIter Next(path, file, -1);
298 while (1)
299 {
300 name = Next();
301 if (name.IsNull())
302 break;
303 fname=name;
304 cout << "filename: " << fname << endl;
305 count++;
306 }
307
308 //check if there's only one file with this runno
309 if (count!=1)
310 {
311 cout << "ERROR - there's are " << count << " files. " << endl;
312 return 2;
313 }
314
315 return Process(serv, fname, dummy);
316}
Note: See TracBrowser for help on using the repository browser.