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

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