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

Last change on this file was 7112, checked in by Daniela Dorner, 20 years ago
*** empty log message ***
File size: 10.1 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// fillcalib.C
29// ===========
30//
31// This macro is used to read the calibartion-/callisto-output files.
32// These files are automatically called calib00000.root.
33//
34// From this file the MBadPixelsCam and the MGeomCam is extracted. If
35// the geometry isn't found MGeomCamMagic is used as a default.
36//
37// Usage:
38// .x fillcalib.C("/data/MAGIC/Period014/calib00000.root", kTRUE)
39//
40// The second argument is the 'dummy-mode'. If it is kTRUE dummy-mode is
41// switched on and nothing will be written into the database. This is usefull
42// for tests.
43//
44// The corresponding sequence number is extracted from the filename...
45// FIXME: MSeqeuence should be stored in the calib-file?
46//
47// The macro can also be run without ACLiC but this is a lot slower...
48//
49// Remark: Running it from the commandline looks like this:
50// root -q -l -b fillcalib.C+\(\"filename\"\,kFALSE\) 2>&1 | tee fillcalib.log
51//
52// Make sure, that database and password are corretly set in a resource
53// file called sql.rc and the resource file is found.
54//
55// Returns 0 in case of failure and 1 in case of success.
56//
57/////////////////////////////////////////////////////////////////////////////
58#include <iostream>
59
60#include <TEnv.h>
61#include <TRegexp.h>
62
63#include <TH1.h>
64
65#include <TFile.h>
66#include <TSQLResult.h>
67#include <TSQLRow.h>
68
69#include "MSQLServer.h"
70
71#include "MStatusArray.h"
72#include "MHCamera.h"
73#include "MGeomCamMagic.h"
74#include "MBadPixelsCam.h"
75
76using namespace std;
77
78// --------------------------------------------------------------------------
79//
80// Checks whether an entry is already existing
81//
82Bool_t ExistStr(MSQLServer &serv, const char *column, const char *table, Int_t test)
83{
84 TString query(Form("SELECT %s FROM %s WHERE %s='%d'", column, table, column, test));
85 TSQLResult *res = serv.Query(query);
86 if (!res)
87 return kFALSE;
88
89 TSQLRow *row;
90
91 Bool_t rc = kFALSE;
92 while ((row=res->Next()))
93 {
94 if ((*row)[0])
95 {
96 rc = kTRUE;
97 break;
98 }
99 }
100
101 delete res;
102
103 return rc;
104}
105
106int Process(MSQLServer &serv, TString fname, Bool_t dummy)
107{
108 MBadPixelsCam badpix;
109
110 TFile file(fname, "READ");
111 if (!file.IsOpen())
112 {
113 cout << "ERROR - Could not find file " << fname << endl;
114 return 0;
115 }
116
117 if (badpix.Read("MBadPixelsCam")<=0)
118 {
119 cout << "ERROR - Reading of MBadPixelsCam failed." << endl;
120 return 0;
121 }
122
123 MGeomCamMagic def;
124
125 MGeomCam *geom = (MGeomCam*)file.Get("MGeomCam");
126 if (!geom)
127 {
128 cout << "WARNING - Reading of MGeomCam failed... using default <MGeomCamMagic>" << endl;
129 geom = &def;
130 }
131
132 cout << "Camera Geometry: " << geom->ClassName() << endl;
133
134 const Short_t unsin = badpix.GetNumUnsuitable(MBadPixelsPix::kUnsuitableRun, geom, 0);
135 const Short_t unsout = badpix.GetNumUnsuitable(MBadPixelsPix::kUnsuitableRun, geom, 1);
136
137 const Short_t unrin = badpix.GetNumUnsuitable(MBadPixelsPix::kUnreliableRun, geom, 0);
138 const Short_t unrout = badpix.GetNumUnsuitable(MBadPixelsPix::kUnreliableRun, geom, 1);
139
140 const Short_t isoin = badpix.GetNumIsolated(*geom, 0);
141 const Short_t isoout = badpix.GetNumIsolated(*geom, 1);
142
143 const Short_t clumax = badpix.GetNumMaxCluster(*geom);
144
145 if (unsin<0 || unsout<0 || unrin<0 || unrout<0 || isoin<0 || isoout<0 || clumax<0)
146 return 0;
147
148 // MHCamera hist(geom);
149 // hist.SetCamContent(badpix, 1);
150 // hist.DrawCopy();
151 // hist.SetCamContent(badpix, 3);
152 // hist.DrawCopy();
153
154 //Getting values from the status display
155 MStatusArray arr;
156 if (arr.Read()<=0)
157 return 0;
158
159 TH1 *h;
160
161 //getting the mean and rms from the arrival time (inner cam)
162 h = (TH1*)arr.FindObjectInCanvas("HRelTimeHiGainArea0", "TH1F", "Time");
163 if (!h)
164 {
165 cout << "WARNING - Could not find histogram HRelTimeHiGainArea0." << endl;
166 return 0;
167 }
168
169 Float_t meani = h->GetMean();
170 Float_t rmsi = h->GetRMS();
171 meani = TMath::Nint(meani*10)/10.;
172 rmsi = TMath::Nint(rmsi*10)/10.;
173 TString meaninner = Form("%5.1f", meani);
174 TString rmsinner = Form("%5.1f", rmsi);
175
176
177 //getting the mean and rms from the arrival time (outer cam)
178 h = (TH1*)arr.FindObjectInCanvas("HRelTimeHiGainArea1", "TH1F", "Time");
179 if (!h)
180 {
181 cout << "WARNING - Could not find histogram HRelTimeHiGainArea1." << endl;
182 return 0;
183 }
184
185 Float_t meano = h->GetMean();
186 Float_t rmso = h->GetRMS();
187 meano = TMath::Nint(meano*10)/10.;
188 rmso = TMath::Nint(rmso*10)/10.;
189 TString meanouter = Form("%5.1f", meano);
190 TString rmsouter = Form("%5.1f", rmso);
191
192
193 //Getting conversion factors
194 MHCamera *c = (MHCamera*)arr.FindObjectInCanvas("TotalConvPhe", "MHCamera", "Conversion");
195 if (!c)
196 {
197 cout << "WARNING - Could not find MHCamera TotalConv." << endl;
198 return 0;
199 }
200
201 TArrayI inner(1), outer(1);
202 inner[0] = 0;
203 outer[0] = 1;
204
205 Int_t s0[] = { 1, 2, 3, 4, 5, 6 };
206
207 Stat_t meanconvi = c->GetMeanSectors(TArrayI(6, s0), inner);
208 Stat_t meanconvo = c->GetMeanSectors(TArrayI(6, s0), outer);
209 meanconvi = TMath::Nint(meanconvi*1000)/1000.;
210 meanconvo = TMath::Nint(meanconvo*1000)/1000.;
211 TString meanconvinner=Form("%6.3f", meanconvi);
212 TString meanconvouter=Form("%6.3f", meanconvo);
213
214
215 //Getting sequ# from filename
216 TString sequence = fname(TRegexp("calib[0-9]+[.]root$"));
217 if (sequence.IsNull())
218 {
219 cout << "WARNING - Could get sequence# from filename: " << fname << endl;
220 return 0;
221 }
222
223 Int_t seq = atoi(sequence.Data()+5);
224
225
226 cout << "Sequence #" << seq << endl;
227 cout << " Unsuitable: (i/o) " << Form("%3d %3d", (int)unsin, (int)unsout) << endl; // Unbrauchbar
228 cout << " Unreliable: (i/o) " << Form("%3d %3d", (int)unrin, (int)unrout) << endl; // Unzuverlaessig
229 cout << " Isolated: (i/o) " << Form("%3d %3d", (int)isoin, (int)isoout) << endl; // Isolated (unbrauchbar)
230 cout << " Max.Cluster: " << Form("%3d", (int)clumax) << endl; // Max Cluster
231 cout << " Arr Time inner: " << meaninner << " +- " << rmsinner << endl;
232 cout << " Arr Time outer: " << meanouter << " +- " << rmsouter << endl;
233 cout << " Mean Conv inner: " << meanconvinner << endl;
234 cout << " Mean Conv outer: " << meanconvouter << endl;
235
236 TString query;
237 if (!ExistStr(serv, "fSequenceFirst", "Calibration", seq))
238 {
239 query = Form("INSERT Calibration SET"
240 " fSequenceFirst=%d,"
241 " fUnsuitableInner=%d, "
242 " fUnsuitableOuter=%d, "
243 " fUnreliableInner=%d, "
244 " fUnreliableOuter=%d, "
245 " fIsolatedInner=%d, "
246 " fIsolatedOuter=%d, "
247 " fIsolatedMaxCluster=%d, "
248 " fArrTimeMeanInner=%s, "
249 " fArrTimeRmsInner=%s, "
250 " fArrTimeMeanOuter=%s, "
251 " fArrTimeRmsOuter=%s, "
252 " fConvFactorInner=%s, "
253 " fConvFactorOuter=%s ",
254 seq, (int)unsin, (int)unsout, (int)unrin,
255 (int)unrout, (int)isoin, (int)isoout,
256 (int)clumax,
257 meaninner.Data(), rmsinner.Data(),
258 meanouter.Data(), rmsouter.Data(),
259 meanconvinner.Data(), meanconvouter.Data());
260 }
261 else
262 {
263 query = Form("UPDATE Calibration SET"
264 " fUnsuitableInner=%d, "
265 " fUnsuitableOuter=%d, "
266 " fUnreliableInner=%d, "
267 " fUnreliableOuter=%d, "
268 " fIsolatedInner=%d, "
269 " fIsolatedOuter=%d, "
270 " fIsolatedMaxCluster=%d, "
271 " fArrTimeMeanInner=%s, "
272 " fArrTimeRmsInner=%s, "
273 " fArrTimeMeanOuter=%s, "
274 " fArrTimeRmsOuter=%s, "
275 " fConvFactorInner=%s, "
276 " fConvFactorOuter=%s "
277 " WHERE fSequenceFirst=%d ",
278 (int)unsin, (int)unsout, (int)unrin,(int)unrout,
279 (int)isoin, (int)isoout, (int)clumax,
280 meaninner.Data(), rmsinner.Data(),
281 meanouter.Data(), rmsouter.Data(),
282 meanconvinner.Data(), meanconvouter.Data(),
283 seq);
284 }
285
286 if (dummy)
287 return 0;
288
289 TSQLResult *res = serv.Query(query);
290 if (!res)
291 {
292 cout << "ERROR - Query failed: " << query << endl;
293 return 0;
294 }
295 delete res;
296
297 return 1;
298}
299
300int fillcalib(TString fname, Bool_t dummy=kTRUE)
301{
302 TEnv env("sql.rc");
303
304 MSQLServer serv(env);
305 if (!serv.IsConnected())
306 {
307 cout << "ERROR - Connection to database failed." << endl;
308 return 0;
309 }
310
311 cout << "fillcalib" << endl;
312 cout << "---------" << endl;
313 cout << endl;
314 cout << "Connected to " << serv.GetName() << endl;
315 cout << "File: " << fname << endl;
316 cout << endl;
317
318 return Process(serv, fname, dummy);
319}
Note: See TracBrowser for help on using the repository browser.