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

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