source: trunk/MagicSoft/Mars/datacenter/macros/fillcalib.C@ 7503

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