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

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