source: trunk/MagicSoft/Mars/datacenter/macros/filldotraw.C@ 7777

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