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

Last change on this file since 9573 was 9062, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 11.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-2008
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 2 in case of failure, 1 in case of success and 0 if the connection
58// to the database is not working.
59//
60/////////////////////////////////////////////////////////////////////////////
61#include <iostream>
62
63#include <TH1.h>
64
65#include <TFile.h>
66#include <TSQLResult.h>
67#include <TSQLRow.h>
68
69#include "MSQLMagic.h"
70
71#include "MStatusArray.h"
72#include "MHCamera.h"
73#include "MSequence.h"
74#include "MGeomCamMagic.h"
75#include "MBadPixelsCam.h"
76
77using namespace std;
78
79int Process(MSQLMagic &serv, TString fname)
80{
81 //getting number of unsuitable, unreliable and isolated pixel
82 MBadPixelsCam badpix;
83
84 TFile file(fname, "READ");
85 if (!file.IsOpen())
86 {
87 cout << "ERROR - Could not find file " << fname << endl;
88 return 2;
89 }
90
91 if (badpix.Read("MBadPixelsCam")<=0)
92 {
93 cout << "ERROR - Reading of MBadPixelsCam failed." << endl;
94 return 2;
95 }
96
97 MGeomCamMagic def;
98
99 MGeomCam *geom = (MGeomCam*)file.Get("MGeomCam");
100 if (!geom)
101 {
102 cout << "WARNING - Reading of MGeomCam failed... using default <MGeomCamMagic>" << endl;
103 geom = &def;
104 }
105
106 cout << "Camera Geometry: " << geom->ClassName() << endl;
107
108 const Short_t unsin = badpix.GetNumUnsuitable(MBadPixelsPix::kUnsuitableRun, geom, 0);
109 const Short_t unsout = badpix.GetNumUnsuitable(MBadPixelsPix::kUnsuitableRun, geom, 1);
110
111 const Short_t unrin = badpix.GetNumUnsuitable(MBadPixelsPix::kUnreliableRun, geom, 0);
112 const Short_t unrout = badpix.GetNumUnsuitable(MBadPixelsPix::kUnreliableRun, geom, 1);
113
114 const Short_t isoin = badpix.GetNumIsolated(*geom, 0);
115 const Short_t isoout = badpix.GetNumIsolated(*geom, 1);
116
117 const Short_t clumax = badpix.GetNumMaxCluster(*geom);
118
119 if (unsin<0 || unsout<0 || unrin<0 || unrout<0 || isoin<0 || isoout<0 || clumax<0)
120 {
121 cout << "ERROR - one of the pixel values < 0." << endl;
122 return 2;
123 }
124
125 // MHCamera hist(geom);
126 // hist.SetCamContent(badpix, 1);
127 // hist.DrawCopy();
128 // hist.SetCamContent(badpix, 3);
129 // hist.DrawCopy();
130
131 //Getting values from the status display
132 MStatusArray arr;
133 if (arr.Read()<=0)
134 {
135 cout << "ERROR - could not read MStatusArray." << endl;
136 return 2;
137 }
138
139 TH1 *h;
140
141 //getting the mean and rms from the arrival time (inner cam)
142 h = (TH1*)arr.FindObjectInCanvas("HRelTimeHiGainArea0", "TH1F", "Time");
143 if (!h)
144 {
145 cout << "ERROR - Could not find histogram HRelTimeHiGainArea0 in Time." << endl;
146 return 2;
147 }
148
149 TString meaninner = Form("%5.1f", h->GetMean());
150 TString rmsinner = Form("%6.2f", h->GetRMS());
151
152 //getting the mean and rms from the arrival time (outer cam)
153 h = (TH1*)arr.FindObjectInCanvas("HRelTimeHiGainArea1", "TH1F", "Time");
154 if (!h)
155 {
156 cout << "ERROR - Could not find histogram HRelTimeHiGainArea1 in Time." << endl;
157 return 2;
158 }
159
160 TString meanouter = Form("%5.1f", h->GetMean());
161 TString rmsouter = Form("%6.2f", h->GetRMS());
162
163 //Getting conversion factors
164 MHCamera *c = (MHCamera*)arr.FindObjectInCanvas("TotalConvPhe", "MHCamera", "Conversion");
165 if (!c)
166 {
167 cout << "ERROR - Could not find MHCamera TotalConv in Conversion." << endl;
168 return 2;
169 }
170
171 TArrayI inner(1), outer(1);
172 inner[0] = 0;
173 outer[0] = 1;
174
175 Int_t s0[] = { 1, 2, 3, 4, 5, 6 };
176
177 Stat_t meanconvi = c->GetMeanSectors(TArrayI(6, s0), inner);
178 Stat_t meanconvo = c->GetMeanSectors(TArrayI(6, s0), outer);
179 TString meanconvinner=Form("%6.3f", meanconvi);
180 TString meanconvouter=Form("%6.3f", meanconvo);
181
182 //Getting relative charge rms
183 c = (MHCamera*)arr.FindObjectInCanvas("RMSperMean", "MHCamera", "FitCharge");
184 if (!c)
185 {
186 cout << "ERROR - Could not find MHCamera RMSperMean in FitCharge." << endl;
187 return 2;
188 }
189
190 Stat_t relrmsi = c->GetMedianSectors(TArrayI(6, s0), inner);
191 Stat_t relrmso = c->GetMedianSectors(TArrayI(6, s0), outer);
192 TString relrmsinner=Form("%6.3f", relrmsi);
193 TString relrmsouter=Form("%6.3f", relrmso);
194
195 //Getting relative charge rms
196 c = (MHCamera*)arr.FindObjectInCanvas("NumPhes", "MHCamera", "FitCharge");
197 if (!c)
198 {
199 cout << "ERROR - Could not find MHCamera NumPhes in FitCharge." << endl;
200 return 2;
201 }
202
203 Stat_t numphei = c->GetMedianSectors(TArrayI(6, s0), inner);
204 Stat_t numpheo = c->GetMedianSectors(TArrayI(6, s0), outer);
205 TString numpheinner=Form("%5.1f", numphei);
206 TString numpheouter=Form("%5.1f", numpheo);
207
208 MSequence seq;
209 if (seq.Read("sequence[0-9]{8}[.]txt|MSequence")<=0)
210 {
211 cout << "ERROR - Could not find sequence in file: " << fname << endl;
212 return 2;
213 }
214 if (!seq.IsValid())
215 {
216 cout << "ERROR - Sequence read from file inavlid: " << fname << endl;
217 return 2;
218 }
219
220 //getting the ratio of calibration events used
221 h = (TH1*)arr.FindObjectInCanvas("ArrTm;avg", "MHCamera", "ArrTm");
222 if (!h)
223 {
224 cout << "ERROR - Could not find histogram ArrTime;avg." << endl;
225 return 2;
226 }
227
228 UInt_t nevts = TMath::Nint(h->GetEntries());
229
230 TString query;
231 query = Form("SELECT SUM(fNumEvents) FROM RunData "
232 "LEFT JOIN RunType USING (fRunTypeKEY) "
233 "WHERE fSequenceFirst=%d AND fTelescopeNumber=%d AND "
234 "RunType.fRunTypeName='Calibration'",
235 seq.GetSequence(), seq.GetTelescope());
236
237 TSQLResult *res = serv.Query(query);
238 if (!res)
239 {
240 cout << "ERROR - Query failed: " << query << endl;
241 return 2;
242 }
243
244 TSQLRow *row = res->Next();
245 if (!row)
246 {
247 cout << "ERROR - Query failed: " << query << endl;
248 return 2;
249 }
250
251 Float_t ratiocalib = 100.*nevts/atof((*row)[0]);
252
253 TString ratiocal = Form("%.1f", ratiocalib);
254
255 delete res;
256
257 cout << "Sequence M" << seq.GetTelescope() << ":" << seq.GetSequence() << endl;
258 cout << " Unsuitable: (i/o) " << Form("%3d %3d", (int)unsin, (int)unsout) << endl; // Unbrauchbar
259 cout << " Unreliable: (i/o) " << Form("%3d %3d", (int)unrin, (int)unrout) << endl; // Unzuverlaessig
260 cout << " Isolated: (i/o) " << Form("%3d %3d", (int)isoin, (int)isoout) << endl; // Isolated (unbrauchbar)
261 cout << " Max.Cluster: " << Form("%3d", (int)clumax) << endl; // Max Cluster
262 cout << " Arr Time inner: " << meaninner << " +- " << rmsinner << endl;
263 cout << " Arr Time outer: " << meanouter << " +- " << rmsouter << endl;
264 cout << " Mean Conv inner: " << meanconvinner << endl;
265 cout << " Mean Conv outer: " << meanconvouter << endl;
266 cout << " Rel.charge rms in: " << relrmsinner << endl;
267 cout << " Rel.charge rms out: " << relrmsouter << endl;
268 cout << " Med. num phe inner: " << numpheinner << endl;
269 cout << " Med. num phe outer: " << numpheouter << endl;
270 cout << " Ratio Calib Evts: " << ratiocal << endl;
271
272 // FIXME: Fill calibration charge
273
274 //inserting or updating the information in the database
275 TString vars =
276 Form(" fSequenceFirst=%d, "
277 " fTelescopeNumber=%d, "
278 " fUnsuitableInner=%d, "
279 " fUnsuitableOuter=%d, "
280 " fUnreliableInner=%d, "
281 " fUnreliableOuter=%d, "
282 " fIsolatedInner=%d, "
283 " fIsolatedOuter=%d, "
284 " fIsolatedMaxCluster=%d, "
285 " fArrTimeMeanInner=%s, "
286 " fArrTimeRmsInner=%s, "
287 " fArrTimeMeanOuter=%s, "
288 " fArrTimeRmsOuter=%s, "
289 " fConvFactorInner=%s, "
290 " fConvFactorOuter=%s, "
291 " fRatioCalEvents=%s, "
292 " fRelChargeRmsInner=%s, "
293 " fRelChargeRmsOuter=%s, "
294 " fMedNumPheInner=%s, "
295 " fMedNumPheOuter=%s ",
296 seq.GetSequence(), seq.GetTelescope(),
297 (int)unsin, (int)unsout, (int)unrin,
298 (int)unrout, (int)isoin, (int)isoout, (int)clumax,
299 meaninner.Data(), rmsinner.Data(),
300 meanouter.Data(), rmsouter.Data(),
301 meanconvinner.Data(), meanconvouter.Data(),
302 ratiocal.Data(),
303 relrmsinner.Data(), relrmsouter.Data(),
304 numpheinner.Data(), numpheouter.Data()
305 );
306
307 TString where = Form("fSequenceFirst=%d AND fTelescopeNumber=%d",
308 seq.GetSequence(), seq.GetTelescope());
309
310 return serv.InsertUpdate("Calibration", vars, where) ? 1 : 2;
311}
312
313int fillcalib(TString fname, Bool_t dummy=kTRUE)
314{
315 MSQLMagic serv("sql.rc");
316 if (!serv.IsConnected())
317 {
318 cout << "ERROR - Connection to database failed." << endl;
319 return 0;
320 }
321
322 cout << "fillcalib" << endl;
323 cout << "---------" << endl;
324 cout << endl;
325 cout << "Connected to " << serv.GetName() << endl;
326 cout << "File: " << fname << endl;
327 cout << endl;
328
329 serv.SetIsDummy(dummy);
330
331 return Process(serv, fname);
332}
Note: See TracBrowser for help on using the repository browser.