source: tags/Mars-V0.9.6/datacenter/macros/filldotraw.C

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