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