source: trunk/MagicSoft/Mars/mcalib/MCalibCalcFromPast.cc@ 8142

Last change on this file since 8142 was 8140, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 15.6 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 fNumFails(0)
97{
98
99 fName = name ? name : "MCalibCalcFromPast";
100 fTitle = title ? title : "Task to steer the processing of interlaced calibration events";
101
102 SetNumEventsDump();
103 SetNumPhesDump ();
104}
105
106// -----------------------------------------------------------------------------------
107//
108Int_t MCalibCalcFromPast::PreProcess(MParList *pList)
109{
110
111 fGeom = (MGeomCam*)pList->FindObject("MGeomCam");
112 if (!fGeom)
113 {
114 *fLog << err << "MGeomCam not found... abort." << endl;
115 return kFALSE;
116 }
117
118 fParList = pList;
119 if (!fParList)
120 {
121 *fLog << err << "MParList not found... abort." << endl;
122 return kFALSE;
123 }
124
125 fRunHeader = (MRawRunHeader*)pList->FindObject("MRawRunHeader");
126 if (!fRunHeader)
127 {
128 *fLog << err << "MRawRunHeader not found... abort." << endl;
129 return kFALSE;
130 }
131
132 //
133 // Look for the MBadPixels Intensity Cam
134 //
135 fIntensBad = (MBadPixelsIntensityCam*)pList->FindCreateObj("MBadPixelsIntensityCam");
136 if (fIntensBad)
137 *fLog << inf << "Found MBadPixelsIntensityCam ... " << flush;
138 else
139 return kFALSE;
140
141 //
142 // Look for the MFillH-class "MHCalibrationBlindCam". In case yes, initialize the
143 // corresponding IntensityCam
144 //
145 if (pList->FindObject(AddSerialNumber("MHCalibrationBlindCam")))
146 {
147
148 *fLog << inf << "Found MHCalibrationBlindCam ... " << flush;
149
150 fIntensBlind = (MCalibrationIntensityBlindCam*)pList->FindCreateObj("MCalibrationIntensityBlindCam");
151 if (!fIntensBlind)
152 {
153 *fLog << err << "Could not find nor create MCalibrationIntensitBlindCam abort... " << endl;
154 return kFALSE;
155 }
156 }
157
158 //
159 // Look for the MFillH-class "MHCalibrationChargeCam". In case yes, initialize the
160 // corresponding IntensityCam
161 //
162 if (pList->FindObject(AddSerialNumber("MHCalibrationChargeCam")))
163 {
164
165 fIntensCharge = (MCalibrationIntensityChargeCam*)pList->FindCreateObj("MCalibrationIntensityChargeCam");
166 fIntensQE = (MCalibrationIntensityQECam*) pList->FindCreateObj("MCalibrationIntensityQECam");
167 fIntensConst = (MCalibrationIntensityConstCam*) pList->FindCreateObj("MCalibrationIntensityConstCam");
168
169 if (!fIntensCharge)
170 return kFALSE;
171 if (!fIntensQE)
172 return kFALSE;
173 if (!fIntensConst)
174 return kFALSE;
175
176
177 MCalibrationChargeCam *chargeinit = (MCalibrationChargeCam*)pList->FindObject("MCalibrationChargeCam");
178 MCalibrationQECam *qeinit = (MCalibrationQECam*) pList->FindObject("MCalibrationQECam");
179
180 if (chargeinit)
181 fIntensCharge->SetCam(chargeinit,0);
182 else
183 *fLog << "Could not find initial MCalibrationChargeCam, cannot initialize intensity cam" << endl;
184
185 if (qeinit)
186 fIntensQE->SetCam(qeinit,0);
187 else
188 *fLog << "Could not find initial MCalibrationQECam, cannot initialize intensity cam" << endl;
189
190 fIntensConst->GetCam()->Init(*fGeom);
191
192 if (!fChargeCalc)
193 fChargeCalc = (MCalibrationChargeCalc*)pList->FindObject("MCalibrationChargeCalc");
194
195 if (!fCalibrate)
196 fCalibrate = (MCalibrateData*)pList->FindObject("MCalibrateData");
197
198 *fLog << inf << "Found MHCalibrationChargeCam ... " << flush;
199
200 if (!fChargeCalc)
201 {
202 *fLog << err << "Could not find MCalibrationChargeCalc abort... " << endl;
203 return kFALSE;
204 }
205
206 if (!fCalibrate)
207 {
208 *fLog << err << "Could not find MCalibrateData abort... " << endl;
209 return kFALSE;
210 }
211 }
212
213 //
214 // Look for the MFillH name "FillRelTimeCam". In case yes, initialize the
215 // corresponding IntensityCam
216 //
217 if (pList->FindObject(AddSerialNumber("MHCalibrationRelTimeCam")))
218 {
219
220 fIntensRelTime = (MCalibrationIntensityRelTimeCam*)pList->FindCreateObj("MCalibrationIntensityRelTimeCam");
221 if (!fRelTimeCalc)
222 fRelTimeCalc = (MCalibrationRelTimeCalc*)pList->FindObject(AddSerialNumber("MCalibrationRelTimeCalc"));
223
224 *fLog << inf << "Found MHCalibrationRelTimeCam ... " << flush;
225
226 if (!fIntensRelTime)
227 {
228 *fLog << err << "Could not find nor create MCalibrationIntensityRelTimeCam abort... " << endl;
229 return kFALSE;
230 }
231
232 if (!fRelTimeCalc)
233 {
234 *fLog << err << "Could not find MCalibrationRelTimeCalc abort... " << endl;
235 return kFALSE;
236 }
237 }
238
239 fNumCam = 0;
240 fNumEvents = 0;
241 fNumPhes = 0;
242
243 fChargeCalc->SetUseExternalNumPhes(kFALSE);
244
245 if (fUpdateNumPhes)
246 {
247 fPhes.Set(fNumPhesDump);
248 fPhesVar.Set(fNumPhesDump);
249 }
250
251 fNumFails = 0;
252
253 return kTRUE;
254}
255
256// --------------------------------------------------------------------------
257//
258// Set fNumEvents=0
259//
260// This is necessary because the calibration histograms do reset themself
261// if ReInit is called, so they are empty. MCalibCalcFromPast errornously
262// ignores how many events are in the histograms but count the number
263// itself.
264//
265Bool_t MCalibCalcFromPast::ReInit(MParList *pList)
266{
267 if (fNumEvents>0)
268 *fLog << inf << fNumEvents << " calibration events at the end of the last file have been skipped." << endl;
269
270 fNumEvents = 0;
271
272 return kTRUE;
273}
274
275// --------------------------------------------------------------------------
276//
277// - Initializes new containers in the
278// - Intensity Cams, if the number of calibration events has reach fNumEventsDump.
279// - Executes Finalize() of the MCalibration*Calc classes in that case.
280// - Sets the latest MCalibrationChargeCam as update class into MCalibrateData
281// - Initialize new MCalibration*Cams into the intensity cams.
282//
283Int_t MCalibCalcFromPast::Process()
284{
285 if (fNumEvents++ < fNumEventsDump)
286 return kTRUE;
287
288 fNumEvents = 0;
289 ReInitialize();
290
291 //
292 // Finalize Possible calibration histogram classes...
293 //
294 *fLog << inf << GetDescriptor() << ": Finalize calibration histograms: " << flush;
295
296 if (Finalize("MHCalibrationChargeCam")) *fLog << "MHCalibrationChargeCam..." << flush;
297 if (Finalize("MHCalibrationChargeBlindCam")) *fLog << "MHCalibrationChargeBlindCam..." << flush;
298 if (Finalize("MHCalibrationRelTimeCam")) *fLog << "MHCalibrationRelTimeCam..." << flush;
299
300 //
301 // Finalize possible calibration calculation tasks
302 //
303 *fLog << endl;
304 *fLog << inf << "Finalize calibration calculations..." << endl;
305 if (fChargeCalc)
306 {
307 if (!fChargeCalc->Finalize())
308 {
309 fNumFails++;
310 *fLog << warn << "WARNING - Finalization of charges failed the " << fNumFails << ". time..." << endl;
311 return kTRUE;
312 }
313
314 if (fUpdateNumPhes)
315 {
316 MCalibrationChargePix &avpix =(MCalibrationChargePix&)fIntensCharge->GetCam()->GetAverageArea(0);
317 fPhes [fNumPhes] = avpix.GetPheFFactorMethod();
318 fPhesVar[fNumPhes] = avpix.GetPheFFactorMethodVar();
319
320 fNumPhes++;
321
322 if (fNumPhes == fNumPhesDump)
323 {
324 fNumPhes = 0;
325 if (!UpdateMeanPhes())
326 {
327 *fLog << warn << "Could not update mean number of photo-electrons. "
328 << "Skip it until next update" << endl;
329 fChargeCalc->SetUseExternalNumPhes(kFALSE);
330 }
331 else
332 {
333 *fLog << inf << "New averaged number photo-electrons: " << fMeanPhes << endl;
334 fChargeCalc->SetExternalNumPhes ( fMeanPhes );
335 fChargeCalc->SetExternalNumPhesRelVar( fMeanPhesRelVar );
336 fChargeCalc->SetUseExternalNumPhes();
337 }
338 }
339 }
340 }
341
342 if (fRelTimeCalc)
343 fRelTimeCalc->Finalize();
344
345 if (fCalibrate)
346 fCalibrate->UpdateConversionFactors(fUpdateWithFFactorMethod ? NULL
347 : (MCalibrationChargeCam*)fIntensCharge->GetCam() );
348
349 return kTRUE;
350}
351
352
353// --------------------------------------------------------------------------
354//
355// Searches for name in the MParList and calls, if existing:
356// - MHCalibrationCam::Finalize()
357// - MHCalibrationCam::ResetHists()
358//
359Bool_t MCalibCalcFromPast::Finalize(const char* name, Bool_t finalize)
360{
361 MHCalibrationCam *hist = (MHCalibrationCam*)fParList->FindObject(name, "MHCalibrationCam");
362 if (!hist)
363 return kFALSE;
364
365 if (finalize)
366 hist->Finalize();
367
368 hist->ResetHists();
369 return kTRUE;
370}
371
372// --------------------------------------------------------------------------
373//
374// Re-Intitializes new containers inside the Intensity Cams.
375// From now on, a call to the IntensityCam functions returns pointers
376// to the newly created Containers.
377//
378Bool_t MCalibCalcFromPast::ReInitialize()
379{
380 fNumCam++;
381
382 *fLog << inf << "MCalibCalcFromPast::ReInitialize #" << fNumCam << ": ";
383
384 const Int_t runnumber = fRunHeader->GetRunNumber();
385
386 // The "DeleteOldCam" function must not delete the first entry in
387 // the array because it is a special cam from the MParList. (see above)
388
389 if (fIntensBad)
390 {
391 *fLog << "MBadPixelsCam...";
392 MBadPixelsCam *cold = fIntensBad->GetCam();
393 fIntensBad->AddToList(Form("MBadPixelsCam%04d",fNumCam),*fGeom);
394 if (cold)
395 fIntensBad->DeleteOldCam(cold);
396 }
397
398 if (fIntensCharge)
399 {
400 *fLog << "MCalibrationChargeCam...";
401 MCalibrationChargeCam *cold = (MCalibrationChargeCam*)fIntensCharge->GetCam();
402
403 fIntensCharge->AddToList(Form("MCalibrationChargeCam%04d",fNumCam),*fGeom);
404
405 MCalibrationChargeCam *cnew = (MCalibrationChargeCam*)fIntensCharge->GetCam();
406 cnew->SetRunNumber(runnumber);
407
408 if (cold)
409 {
410 cnew->MergeHiLoConversionFactors(*cold);
411 fIntensCharge->DeleteOldCam(cold);
412 }
413 }
414
415 if (fIntensQE)
416 {
417 *fLog << "MCalibrationQECam...";
418 MCalibrationCam *cold = fIntensQE->GetCam();
419 fIntensQE->AddToList(Form("MCalibrationQECam%04d",fNumCam),*fGeom);
420 fIntensQE->GetCam()->SetRunNumber(runnumber);
421 if (cold)
422 fIntensQE->DeleteOldCam(cold);
423 }
424 if (fIntensBlind)
425 {
426 *fLog << "MCalibrationBlindCam...";
427 MCalibrationCam *cold = fIntensBlind->GetCam();
428 fIntensBlind->AddToList(Form("MCalibrationBlindCam%04d",fNumCam),*fGeom);
429 fIntensBlind->GetCam()->SetRunNumber(runnumber);
430 if (cold)
431 fIntensBlind->DeleteOldCam(cold);
432 }
433
434 *fLog << endl;
435
436 return kTRUE;
437
438}
439
440Int_t MCalibCalcFromPast::PostProcess()
441{
442 if (GetNumExecutions()==0)
443 return kTRUE;
444
445 // Now we reset all histograms to make sure that the PostProcess
446 // of the following tasks doesn't try to finalize a partly empty
447 // histogram!
448 Finalize("MHCalibrationChargeCam", kFALSE);
449 Finalize("MHCalibrationChargeBlindCam", kFALSE);
450 Finalize("MHCalibrationRelTimeCam", kFALSE);
451
452 if (fChargeCalc)
453 fChargeCalc->ResetNumProcessed();
454
455 if (fNumCam==0)
456 return kTRUE;
457
458 *fLog << inf << endl;
459 *fLog << GetDescriptor() << " execution statistics:" << endl;
460 *fLog << " " << setfill(' ') << setw(7) << fNumFails << " (" << Form("%5.1f", 100.*fNumFails/fNumCam) << "%) updates failed." << endl;
461 *fLog << endl;
462
463 return kTRUE;
464}
465
466
467Bool_t MCalibCalcFromPast::UpdateMeanPhes()
468{
469 Float_t sumw = 0.;
470 Float_t sum = 0.;
471
472 for (Int_t i=0; i<fPhes.GetSize(); i++)
473 {
474 const Float_t weight = 1./fPhesVar[i];
475 sum += fPhes[i]*weight;
476 sumw += weight;
477 }
478
479 if (sumw < 0.000001)
480 return kFALSE;
481
482 if (sum < 0.000001)
483 return kFALSE;
484
485 fMeanPhes = sum/sumw;
486 fMeanPhesRelVar = sumw/sum/sum;
487
488 return kTRUE;
489}
490
491// --------------------------------------------------------------------------
492//
493// Read the setup from a TEnv, eg:
494// MCalibCalcFromPast.UpdateWithFFactorMethod: Off, On
495// MCalibCalcFromPast.NumEventsDump: 500
496// MCalibCalcFromPast.UpdateNumPhes: yes/no
497// MCalibCalcFromPast.NumPhesDump: 10
498//
499Int_t MCalibCalcFromPast::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
500{
501 Bool_t rc = kFALSE;
502 if (IsEnvDefined(env, prefix, "UpdateWithFFactorMethod", print))
503 {
504 rc = kTRUE;
505 SetUpdateWithFFactorMethod(GetEnvValue(env, prefix, "UpdateWithFFactorMethod", fUpdateWithFFactorMethod));
506 }
507
508 if (IsEnvDefined(env, prefix, "NumEventsDump", print))
509 {
510 SetNumEventsDump(GetEnvValue(env, prefix, "NumEventsDump", (Int_t)fNumEventsDump));
511 rc = kTRUE;
512 }
513
514 if (IsEnvDefined(env, prefix, "UpdateNumPhes", print))
515 {
516 TString s = GetEnvValue(env, prefix, "UpdateNumPhes", "");
517 s.ToLower();
518 if (s.BeginsWith("no"))
519 SetUpdateNumPhes(kFALSE);
520 if (s.BeginsWith("yes"))
521 SetUpdateNumPhes(kTRUE);
522 rc = kTRUE;
523 }
524
525 if (IsEnvDefined(env, prefix, "NumPhesDump", print))
526 {
527 SetNumPhesDump(GetEnvValue(env, prefix, "NumPhesDump", (Int_t)fNumPhesDump));
528 rc = kTRUE;
529 }
530
531 return rc;
532}
Note: See TracBrowser for help on using the repository browser.