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

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