source: trunk/Mars/mcalib/MCalibCalcFromPast.cc@ 10075

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