source: tags/Mars-V0.9.4/mcalib/MCalibCalcFromPast.cc

Last change on this file was 7190, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 13.7 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): Markus Gaug, 12/2004 <mailto:markus@ifae.es>
19!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24//////////////////////////////////////////////////////////////////////////////
25//
26// MCalibCalcFromPast
27//
28// Steers the occurrance of interlaced calibration events in one data run
29//
30// Input Containers:
31// MParList
32// MCalibrationIntensityChargeCam
33// MCalibrationIntensityRelTimeCam
34// MCalibrationIntensityConstCam
35// MBadPixelsIntensityCam
36//
37// Output Containers:
38// MCalibrationIntensityChargeCam
39// MCalibrationIntensityRelTimeCam
40// MBadPixelsIntensityCam
41//
42// Class version 2:
43// + UInt_t fNumPhesDump; // Number of cams after which the number of phes gets averaged
44// + Float_t fMeanPhes;
45// + Float_t fMeanPhesRelVar;
46// + Bool_t fUpdateNumPhes; // Update the number of photo-electrons only after fNumPhesDump number of Cams
47// + TArrayF fPhes;
48// + TArrayF fPhesVar;
49//
50//////////////////////////////////////////////////////////////////////////////
51#include "MCalibCalcFromPast.h"
52
53#include "MLog.h"
54#include "MLogManip.h"
55
56#include "MParList.h"
57
58#include "MRawRunHeader.h"
59
60#include "MHCalibrationCam.h"
61
62#include "MCalibrationIntensityChargeCam.h"
63#include "MCalibrationIntensityBlindCam.h"
64#include "MCalibrationIntensityQECam.h"
65#include "MCalibrationIntensityRelTimeCam.h"
66#include "MCalibrationIntensityConstCam.h"
67
68#include "MBadPixelsIntensityCam.h"
69
70#include "MCalibrationChargePix.h"
71#include "MCalibrationChargeCalc.h"
72#include "MCalibrationRelTimeCalc.h"
73#include "MCalibrateData.h"
74
75ClassImp(MCalibCalcFromPast);
76
77using namespace std;
78
79const UInt_t MCalibCalcFromPast::fgNumEventsDump = 500;
80const UInt_t MCalibCalcFromPast::fgNumPhesDump = 10;
81
82// --------------------------------------------------------------------------
83//
84// Default constructor.
85//
86// Sets:
87// - fNumEventsDump to fgNumEventsDump
88// - fNumPhesDump to fgNumPhesDump
89//
90MCalibCalcFromPast::MCalibCalcFromPast(const char *name, const char *title)
91 : fGeom(NULL), fParList(NULL), fRunHeader(NULL),
92 fIntensCharge(NULL), fIntensBlind(NULL), fIntensRelTime(NULL), fIntensConst(NULL),
93 fIntensBad(NULL),
94 fChargeCalc(NULL), fRelTimeCalc(NULL), fCalibrate(NULL),
95 fNumCam(0), fNumEvents(0), fUpdateWithFFactorMethod(kTRUE), fUpdateNumPhes(kTRUE)
96{
97
98 fName = name ? name : "MCalibCalcFromPast";
99 fTitle = title ? title : "Task to steer the processing of interlaced calibration events";
100
101 SetNumEventsDump();
102 SetNumPhesDump ();
103}
104
105// -----------------------------------------------------------------------------------
106//
107Int_t MCalibCalcFromPast::PreProcess(MParList *pList)
108{
109
110 fGeom = (MGeomCam*)pList->FindObject("MGeomCam");
111 if (!fGeom)
112 {
113 *fLog << err << "MGeomCam not found... abort." << endl;
114 return kFALSE;
115 }
116
117 fParList = pList;
118 if (!fParList)
119 {
120 *fLog << err << "MParList not found... abort." << endl;
121 return kFALSE;
122 }
123
124 fRunHeader = (MRawRunHeader*)pList->FindObject("MRawRunHeader");
125 if (!fRunHeader)
126 {
127 *fLog << err << "MRawRunHeader not found... abort." << endl;
128 return kFALSE;
129 }
130
131 //
132 // Look for the MBadPixels Intensity Cam
133 //
134 fIntensBad = (MBadPixelsIntensityCam*)pList->FindCreateObj("MBadPixelsIntensityCam");
135 if (fIntensBad)
136 *fLog << inf << "Found MBadPixelsIntensityCam ... " << flush;
137 else
138 return kFALSE;
139
140 //
141 // Look for the MFillH-class "MHCalibrationBlindCam". In case yes, initialize the
142 // corresponding IntensityCam
143 //
144 if (pList->FindObject(AddSerialNumber("MHCalibrationBlindCam")))
145 {
146
147 *fLog << inf << "Found MHCalibrationBlindCam ... " << flush;
148
149 fIntensBlind = (MCalibrationIntensityBlindCam*)pList->FindCreateObj("MCalibrationIntensityBlindCam");
150 if (!fIntensBlind)
151 {
152 *fLog << err << "Could not find nor create MCalibrationIntensitBlindCam abort... " << endl;
153 return kFALSE;
154 }
155 }
156
157 //
158 // Look for the MFillH-class "MHCalibrationChargeCam". In case yes, initialize the
159 // corresponding IntensityCam
160 //
161 if (pList->FindObject(AddSerialNumber("MHCalibrationChargeCam")))
162 {
163
164 fIntensCharge = (MCalibrationIntensityChargeCam*)pList->FindCreateObj("MCalibrationIntensityChargeCam");
165 fIntensQE = (MCalibrationIntensityQECam*) pList->FindCreateObj("MCalibrationIntensityQECam");
166 fIntensConst = (MCalibrationIntensityConstCam*) pList->FindCreateObj("MCalibrationIntensityConstCam");
167
168 MCalibrationChargeCam *chargeinit = (MCalibrationChargeCam*)pList->FindObject("MCalibrationChargeCam");
169 MCalibrationQECam *qeinit = (MCalibrationQECam*) pList->FindObject("MCalibrationQECam");
170
171 if (chargeinit)
172 fIntensCharge->SetCam(chargeinit,0);
173 else
174 *fLog << "Could not find initial MCalibrationChargeCam, cannot initialize intensity cam" << endl;
175
176 if (qeinit)
177 fIntensQE->SetCam(qeinit,0);
178 else
179 *fLog << "Could not find initial MCalibrationQECam, cannot initialize intensity cam" << endl;
180
181 fIntensConst->GetCam()->Init(*fGeom);
182
183 if (!fChargeCalc)
184 fChargeCalc = (MCalibrationChargeCalc*)pList->FindObject("MCalibrationChargeCalc");
185
186 if (!fCalibrate)
187 fCalibrate = (MCalibrateData*)pList->FindObject("MCalibrateData");
188
189 *fLog << inf << "Found MHCalibrationChargeCam ... " << flush;
190
191 if (!fIntensCharge)
192 {
193 *fLog << err << "Could not find nor create MCalibrationIntensityChargeCam abort... " << endl;
194 return kFALSE;
195 }
196
197 if (!fIntensQE)
198 {
199 *fLog << err << "Could not find nor create MCalibrationIntensityQECam abort... " << endl;
200 return kFALSE;
201 }
202
203 if (!fChargeCalc)
204 {
205 *fLog << err << "Could not find MCalibrationChargeCalc abort... " << endl;
206 return kFALSE;
207 }
208
209 if (!fCalibrate)
210 {
211 *fLog << err << "Could not find MCalibrateData abort... " << endl;
212 return kFALSE;
213 }
214 }
215
216 //
217 // Look for the MFillH name "FillRelTimeCam". In case yes, initialize the
218 // corresponding IntensityCam
219 //
220 if (pList->FindObject(AddSerialNumber("MHCalibrationRelTimeCam")))
221 {
222
223 fIntensRelTime = (MCalibrationIntensityRelTimeCam*)pList->FindCreateObj("MCalibrationIntensityRelTimeCam");
224 if (!fRelTimeCalc)
225 fRelTimeCalc = (MCalibrationRelTimeCalc*)pList->FindObject(AddSerialNumber("MCalibrationRelTimeCalc"));
226
227 *fLog << inf << "Found MHCalibrationRelTimeCam ... " << flush;
228
229 if (!fIntensRelTime)
230 {
231 *fLog << err << "Could not find nor create MCalibrationIntensityRelTimeCam abort... " << endl;
232 return kFALSE;
233 }
234
235 if (!fRelTimeCalc)
236 {
237 *fLog << err << "Could not find MCalibrationRelTimeCalc abort... " << endl;
238 return kFALSE;
239 }
240 }
241
242 fNumCam = 0;
243 fNumEvents = 0;
244 fNumPhes = 0;
245
246 fChargeCalc->SetUseExternalNumPhes(kFALSE);
247
248 if (fUpdateNumPhes)
249 {
250 fPhes.Set(fNumPhesDump);
251 fPhesVar.Set(fNumPhesDump);
252 }
253
254 return kTRUE;
255}
256
257// --------------------------------------------------------------------------
258//
259// - Initializes new containers in the
260// - Intensity Cams, if the number of calibration events has reach fNumEventsDump.
261// - Executes Finalize() of the MCalibration*Calc classes in that case.
262// - Sets the latest MCalibrationChargeCam as update class into MCalibrateData
263// - Initialize new MCalibration*Cams into the intensity cams.
264//
265Int_t MCalibCalcFromPast::Process()
266{
267
268 if (fNumEvents++ < fNumEventsDump)
269 return kTRUE;
270
271 fNumEvents = 0;
272 ReInitialize();
273
274 //
275 // Finalize Possible calibration histogram classes...
276 //
277 *fLog << inf << GetDescriptor() << ": Finalize calibration histograms: " << flush;
278
279 if (Finalize("MHCalibrationChargeCam")) *fLog << "MHCalibrationChargeCam..." << flush;
280 if (Finalize("MHCalibrationChargeBlindCam")) *fLog << "MHCalibrationChargeBlindCam..." << flush;
281 if (Finalize("MHCalibrationRelTimeCam")) *fLog << "MHCalibrationRelTimeCam..." << flush;
282
283 //
284 // Finalize possible calibration calculation tasks
285 //
286 *fLog << endl;
287 *fLog << inf << "Finalize calibration calculations..." << endl;
288 if (fChargeCalc)
289 {
290 if (!fChargeCalc->Finalize())
291 return kERROR;
292
293 if (fUpdateNumPhes)
294 {
295 MCalibrationChargePix &avpix =(MCalibrationChargePix&)fIntensCharge->GetCam()->GetAverageArea(0);
296 fPhes [fNumPhes] = avpix.GetPheFFactorMethod();
297 fPhesVar[fNumPhes] = avpix.GetPheFFactorMethodVar();
298
299 fNumPhes++;
300
301 if (fNumPhes == fNumPhesDump)
302 {
303 fNumPhes = 0;
304 if (!UpdateMeanPhes())
305 {
306 *fLog << warn << "Could not update mean number of photo-electrons. "
307 << "Skip it until next update" << endl;
308 fChargeCalc->SetUseExternalNumPhes(kFALSE);
309 }
310 else
311 {
312 *fLog << inf << "New averaged number photo-electrons: " << fMeanPhes << endl;
313 fChargeCalc->SetExternalNumPhes ( fMeanPhes );
314 fChargeCalc->SetExternalNumPhesRelVar( fMeanPhesRelVar );
315 fChargeCalc->SetUseExternalNumPhes();
316 }
317 }
318 }
319 }
320
321 if (fRelTimeCalc)
322 fRelTimeCalc->Finalize();
323
324 if (fCalibrate)
325 fCalibrate->UpdateConversionFactors(fUpdateWithFFactorMethod ? NULL
326 : (MCalibrationChargeCam*)fIntensCharge->GetCam() );
327
328 return kTRUE;
329}
330
331
332// --------------------------------------------------------------------------
333//
334// Searches for name in the MParList and calls, if existing:
335// - MHCalibrationCam::Finalize()
336// - MHCalibrationCam::ResetHists()
337//
338Bool_t MCalibCalcFromPast::Finalize(const char* name)
339{
340
341 MHCalibrationCam *hist = (MHCalibrationCam*)fParList->FindObject(name);
342 if (hist)
343 {
344 hist->Finalize();
345 hist->ResetHists();
346 return kTRUE;
347 }
348
349 return kFALSE;
350
351}
352
353// --------------------------------------------------------------------------
354//
355// Re-Intitializes new containers inside the Intensity Cams.
356// From now on, a call to the IntensityCam functions returns pointers
357// to the newly created Containers.
358//
359Bool_t MCalibCalcFromPast::ReInitialize()
360{
361 fNumCam++;
362
363 *fLog << inf << "MCalibCalcFromPast::ReInitialize #" << fNumCam << ": ";
364
365 const Int_t runnumber = fRunHeader->GetRunNumber();
366
367 if (fIntensBad)
368 {
369 fIntensBad->AddToList(Form("MBadPixelsCam%04d",fNumCam),*fGeom);
370 *fLog << "MBadPixelsCam...";
371 }
372
373 if (fIntensCharge)
374 {
375 fIntensCharge->AddToList(Form("MCalibrationChargeCam%04d",fNumCam),*fGeom);
376 fIntensCharge->GetCam()->SetRunNumber(runnumber);
377 *fLog << "MCalibrationChargeCam...";
378 }
379 if (fIntensQE)
380 {
381 fIntensQE->AddToList(Form("MCalibrationQECam%04d",fNumCam),*fGeom);
382 fIntensQE->GetCam()->SetRunNumber(runnumber);
383 *fLog << "MCalibrationQECam...";
384 }
385 if (fIntensBlind)
386 {
387 fIntensBlind->AddToList(Form("MCalibrationBlindCam%04d",fNumCam),*fGeom);
388 fIntensBlind->GetCam()->SetRunNumber(runnumber);
389 *fLog << "MCalibrationBlindCam...";
390 }
391
392 *fLog << endl;
393
394 return kTRUE;
395
396}
397
398Int_t MCalibCalcFromPast::PostProcess()
399{
400 *fLog << inf << "Number of Calibration Cams: " << fNumCam << endl;
401 return kTRUE;
402
403}
404
405
406Bool_t MCalibCalcFromPast::UpdateMeanPhes()
407{
408 Float_t sumw = 0.;
409 Float_t sum = 0.;
410
411 for (Int_t i=0; i<fPhes.GetSize(); i++)
412 {
413 const Float_t weight = 1./fPhesVar[i];
414 sum += fPhes[i]*weight;
415 sumw += weight;
416 }
417
418 if (sumw < 0.000001)
419 return kFALSE;
420
421 if (sum < 0.000001)
422 return kFALSE;
423
424 fMeanPhes = sum/sumw;
425 fMeanPhesRelVar = sumw/sum/sum;
426
427 return kTRUE;
428}
429
430// --------------------------------------------------------------------------
431//
432// Read the setup from a TEnv, eg:
433// MCalibCalcFromPast.UpdateWithFFactorMethod: Off, On
434// MCalibCalcFromPast.NumEventsDump: 500
435// MCalibCalcFromPast.UpdateNumPhes: yes/no
436// MCalibCalcFromPast.NumPhesDump: 10
437//
438Int_t MCalibCalcFromPast::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
439{
440 Bool_t rc = kFALSE;
441 if (IsEnvDefined(env, prefix, "UpdateWithFFactorMethod", print))
442 {
443 rc = kTRUE;
444 SetUpdateWithFFactorMethod(GetEnvValue(env, prefix, "UpdateWithFFactorMethod", fUpdateWithFFactorMethod));
445 }
446
447 if (IsEnvDefined(env, prefix, "NumEventsDump", print))
448 {
449 SetNumEventsDump(GetEnvValue(env, prefix, "NumEventsDump", (Int_t)fNumEventsDump));
450 rc = kTRUE;
451 }
452
453 if (IsEnvDefined(env, prefix, "UpdateNumPhes", print))
454 {
455 TString s = GetEnvValue(env, prefix, "UpdateNumPhes", "");
456 s.ToLower();
457 if (s.BeginsWith("no"))
458 SetUpdateNumPhes(kFALSE);
459 if (s.BeginsWith("yes"))
460 SetUpdateNumPhes(kTRUE);
461 rc = kTRUE;
462 }
463
464 if (IsEnvDefined(env, prefix, "NumPhesDump", print))
465 {
466 SetNumPhesDump(GetEnvValue(env, prefix, "NumPhesDump", (Int_t)fNumPhesDump));
467 rc = kTRUE;
468 }
469
470
471
472 return rc;
473}
Note: See TracBrowser for help on using the repository browser.