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 "MSQLServer.h"
|
---|
71 | #include "MRawRunHeader.h"
|
---|
72 | #include "MDirIter.h"
|
---|
73 |
|
---|
74 | using namespace std;
|
---|
75 |
|
---|
76 | //get key for a magic number
|
---|
77 | Int_t MagicNumber(MSQLServer &serv, const MRawRunHeader &h)
|
---|
78 | {
|
---|
79 | TString query(Form("SELECT fMagicNumberKEY FROM MagicNumber WHERE fMagicNumber=%d",
|
---|
80 | h.GetMagicNumber()));
|
---|
81 |
|
---|
82 | TSQLResult *res = serv.Query(query);
|
---|
83 | if (!res)
|
---|
84 | {
|
---|
85 | cout << "ERROR - Query failed: " << query << endl;
|
---|
86 | return -1;
|
---|
87 | }
|
---|
88 |
|
---|
89 | TSQLRow *row = res->Next();
|
---|
90 |
|
---|
91 | TString rc = row ? (*row)[0] : "";
|
---|
92 |
|
---|
93 | delete res;
|
---|
94 |
|
---|
95 | if (rc.IsNull())
|
---|
96 | {
|
---|
97 | cout << "ERROR - No result from query: " << query << endl;
|
---|
98 | return -1;
|
---|
99 | }
|
---|
100 |
|
---|
101 | return rc.Atoi();
|
---|
102 | }
|
---|
103 |
|
---|
104 | Bool_t ReadRaw(TString fname, MRawRunHeader &h)
|
---|
105 | {
|
---|
106 | MZlib fin(fname);
|
---|
107 | if (!fin)
|
---|
108 | {
|
---|
109 | cout << "ERROR - Couldn't open file " << fname << endl;
|
---|
110 | return kFALSE;
|
---|
111 | }
|
---|
112 |
|
---|
113 | if (!h.ReadEvt(fin))
|
---|
114 | {
|
---|
115 | cout << "ERROR - Reading header from file " << fname << endl;
|
---|
116 | return kFALSE;
|
---|
117 | }
|
---|
118 | return kTRUE;
|
---|
119 | }
|
---|
120 |
|
---|
121 | Bool_t ReadRoot(TString fname, MRawRunHeader *h)
|
---|
122 | {
|
---|
123 | TFile file(fname, "READ");
|
---|
124 | if (file.IsZombie())
|
---|
125 | {
|
---|
126 | cout << "ERROR - Cannot open file " << fname << endl;
|
---|
127 | return kFALSE;
|
---|
128 | }
|
---|
129 |
|
---|
130 | TTree *t = (TTree*)file.Get("RunHeaders");
|
---|
131 | if (!t)
|
---|
132 | {
|
---|
133 | cout << "ERROR - Tree RunHeaders not found." << endl;
|
---|
134 | return kFALSE;
|
---|
135 | }
|
---|
136 |
|
---|
137 | t->SetBranchAddress("MRawRunHeader.", &h);
|
---|
138 | t->GetEntry(0);
|
---|
139 |
|
---|
140 | return kTRUE;
|
---|
141 | }
|
---|
142 |
|
---|
143 | int Process(MSQLServer &serv, TString fname, Bool_t dummy)
|
---|
144 | {
|
---|
145 | MRawRunHeader h;
|
---|
146 |
|
---|
147 | //read header either from root or from raw file
|
---|
148 | if (fname.EndsWith(".root"))
|
---|
149 | ReadRoot(fname, &h);
|
---|
150 | if (fname.EndsWith(".raw"))
|
---|
151 | ReadRaw(fname, h);
|
---|
152 | if (fname.EndsWith(".raw.gz"))
|
---|
153 | ReadRaw(fname, h);
|
---|
154 |
|
---|
155 | if (dummy)
|
---|
156 | {
|
---|
157 | h.Print("header");
|
---|
158 | return 1;
|
---|
159 | }
|
---|
160 |
|
---|
161 | //get key for the magic number
|
---|
162 | const Int_t key = MagicNumber(serv, h);
|
---|
163 | if (key<0)
|
---|
164 | return 2;
|
---|
165 |
|
---|
166 | TString query(Form("UPDATE RunData SET fMagicNumberKEY=%d, fFormatVersion=%d WHERE fRunNumber=%d",
|
---|
167 | key, h.GetFormatVersion(), h.GetRunNumber()));
|
---|
168 |
|
---|
169 | TSQLResult *res = serv.Query(query);
|
---|
170 | if (!res)
|
---|
171 | {
|
---|
172 | cout << "ERROR - Query failed: " << query << endl;
|
---|
173 | return 2;
|
---|
174 | }
|
---|
175 | delete res;
|
---|
176 | return 1;
|
---|
177 | }
|
---|
178 |
|
---|
179 | int filldotraw(TString fname, Bool_t dummy=kTRUE)
|
---|
180 | {
|
---|
181 | TEnv env("sql.rc");
|
---|
182 |
|
---|
183 | MSQLServer serv(env);
|
---|
184 | if (!serv.IsConnected())
|
---|
185 | {
|
---|
186 | cout << "ERROR - Connection to database failed." << endl;
|
---|
187 | return 0;
|
---|
188 | }
|
---|
189 |
|
---|
190 | cout << "filldotraw" << endl;
|
---|
191 | cout << "----------" << endl;
|
---|
192 | cout << endl;
|
---|
193 | cout << "Connected to " << serv.GetName() << endl;
|
---|
194 | cout << "File: " << fname << endl;
|
---|
195 | cout << endl;
|
---|
196 |
|
---|
197 | return Process(serv, fname, dummy);
|
---|
198 | }
|
---|
199 |
|
---|
200 | int filldotraw(Int_t runno, Bool_t dummy=kTRUE)
|
---|
201 | {
|
---|
202 | TEnv env("sql.rc");
|
---|
203 |
|
---|
204 | MSQLServer serv(env);
|
---|
205 | if (!serv.IsConnected())
|
---|
206 | {
|
---|
207 | cout << "ERROR - Connection to database failed." << endl;
|
---|
208 | return 0;
|
---|
209 | }
|
---|
210 |
|
---|
211 | cout << "filldotraw" << endl;
|
---|
212 | cout << "----------" << endl;
|
---|
213 | cout << endl;
|
---|
214 | cout << "Connected to " << serv.GetName() << endl;
|
---|
215 | cout << "Run: " << runno << endl;
|
---|
216 | cout << endl;
|
---|
217 |
|
---|
218 | //get date for the run to build path of the file
|
---|
219 | TString query(Form("SELECT DATE_FORMAT(ADDDATE(fRunStart, Interval 13 HOUR), '%%Y/%%m/%%d') FROM RunData WHERE fRunNumber=%d",
|
---|
220 | runno));
|
---|
221 |
|
---|
222 | TSQLResult *res = serv.Query(query);
|
---|
223 | if (!res)
|
---|
224 | {
|
---|
225 | cout << "ERROR - Query failed: " << query << endl;
|
---|
226 | return 2;
|
---|
227 | }
|
---|
228 |
|
---|
229 | TSQLRow *row = 0;
|
---|
230 | row = res->Next();
|
---|
231 | TString date=(*row)[0];
|
---|
232 | cout << "date: " << date << endl;
|
---|
233 | TString path(Form("/magic/data/rawfiles/%s", date.Data()));
|
---|
234 | TString file(Form("*%d_*_*_E.raw.?g?z?", runno));
|
---|
235 |
|
---|
236 | delete res;
|
---|
237 |
|
---|
238 | cout << "path: " << path << " - file : " << file << endl;
|
---|
239 | TString fname;
|
---|
240 | TString name;
|
---|
241 |
|
---|
242 | Int_t count=0;
|
---|
243 | MDirIter Next(path, file, -1);
|
---|
244 | while (1)
|
---|
245 | {
|
---|
246 | name = Next();
|
---|
247 | if (name.IsNull())
|
---|
248 | break;
|
---|
249 | fname=name;
|
---|
250 | cout << "filename: " << fname << endl;
|
---|
251 | count++;
|
---|
252 | }
|
---|
253 |
|
---|
254 | //check if there's only one file with this runno
|
---|
255 | if (count!=1)
|
---|
256 | {
|
---|
257 | cout << "ERROR - there's are " << count << " files. " << endl;
|
---|
258 | return 2;
|
---|
259 | }
|
---|
260 |
|
---|
261 | return Process(serv, fname, dummy);
|
---|
262 | }
|
---|