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

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