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

Last change on this file since 7189 was 7189, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 13.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{
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 fChargeCalc->Finalize();
291 if (fUpdateNumPhes)
292 {
293 MCalibrationChargePix &avpix =(MCalibrationChargePix&)fIntensCharge->GetCam()->GetAverageArea(0);
294 fPhes [fNumPhes] = avpix.GetPheFFactorMethod();
295 fPhesVar[fNumPhes] = avpix.GetPheFFactorMethodVar();
296
297 fNumPhes++;
298
299 if (fNumPhes == fNumPhesDump)
300 {
301 fNumPhes = 0;
302 if (!UpdateMeanPhes())
303 {
304 *fLog << warn << "Could not update mean number of photo-electrons. "
305 << "Skip it until next update" << endl;
306 fChargeCalc->SetUseExternalNumPhes(kFALSE);
307 }
308 else
309 {
310 *fLog << inf << "New averaged number photo-electrons: " << fMeanPhes << endl;
311 fChargeCalc->SetExternalNumPhes ( fMeanPhes );
312 fChargeCalc->SetExternalNumPhesRelVar( fMeanPhesRelVar );
313 fChargeCalc->SetUseExternalNumPhes();
314 }
315 }
316 }
317 }
318
319 if (fRelTimeCalc)
320 fRelTimeCalc->Finalize();
321
322 if (fCalibrate)
323 fCalibrate->UpdateConversionFactors(fUpdateWithFFactorMethod ? NULL
324 : (MCalibrationChargeCam*)fIntensCharge->GetCam() );
325
326 return kTRUE;
327}
328
329
330// --------------------------------------------------------------------------
331//
332// Searches for name in the MParList and calls, if existing:
333// - MHCalibrationCam::Finalize()
334// - MHCalibrationCam::ResetHists()
335//
336Bool_t MCalibCalcFromPast::Finalize(const char* name)
337{
338
339 MHCalibrationCam *hist = (MHCalibrationCam*)fParList->FindObject(name);
340 if (hist)
341 {
342 hist->Finalize();
343 hist->ResetHists();
344 return kTRUE;
345 }
346
347 return kFALSE;
348
349}
350
351// --------------------------------------------------------------------------
352//
353// Re-Intitializes new containers inside the Intensity Cams.
354// From now on, a call to the IntensityCam functions returns pointers
355// to the newly created Containers.
356//
357Bool_t MCalibCalcFromPast::ReInitialize()
358{
359 fNumCam++;
360
361 *fLog << inf << "MCalibCalcFromPast::ReInitialize #" << fNumCam << ": ";
362
363 const Int_t runnumber = fRunHeader->GetRunNumber();
364
365 if (fIntensBad)
366 {
367 fIntensBad->AddToList(Form("MBadPixelsCam%04d",fNumCam),*fGeom);
368 *fLog << "MBadPixelsCam...";
369 }
370
371 if (fIntensCharge)
372 {
373 fIntensCharge->AddToList(Form("MCalibrationChargeCam%04d",fNumCam),*fGeom);
374 fIntensCharge->GetCam()->SetRunNumber(runnumber);
375 *fLog << "MCalibrationChargeCam...";
376 }
377 if (fIntensQE)
378 {
379 fIntensQE->AddToList(Form("MCalibrationQECam%04d",fNumCam),*fGeom);
380 fIntensQE->GetCam()->SetRunNumber(runnumber);
381 *fLog << "MCalibrationQECam...";
382 }
383 if (fIntensBlind)
384 {
385 fIntensBlind->AddToList(Form("MCalibrationBlindCam%04d",fNumCam),*fGeom);
386 fIntensBlind->GetCam()->SetRunNumber(runnumber);
387 *fLog << "MCalibrationBlindCam...";
388 }
389
390 *fLog << endl;
391
392 return kTRUE;
393
394}
395
396Int_t MCalibCalcFromPast::PostProcess()
397{
398 *fLog << inf << "Number of Calibration Cams: " << fNumCam << endl;
399 return kTRUE;
400
401}
402
403
404Bool_t MCalibCalcFromPast::UpdateMeanPhes()
405{
406 Float_t sumw = 0.;
407 Float_t sum = 0.;
408
409 for (Int_t i=0; i<fPhes.GetSize(); i++)
410 {
411 const Float_t weight = 1./fPhesVar[i];
412 sum += fPhes[i]*weight;
413 sumw += weight;
414 }
415
416 if (sumw < 0.000001)
417 return kFALSE;
418
419 if (sum < 0.000001)
420 return kFALSE;
421
422 fMeanPhes = sum/sumw;
423 fMeanPhesRelVar = sumw/sum/sum;
424
425 return kTRUE;
426}
427
428// --------------------------------------------------------------------------
429//
430// Read the setup from a TEnv, eg:
431// MCalibCalcFromPast.UpdateWithFFactorMethod: Off, On
432// MCalibCalcFromPast.NumEventsDump: 500
433// MCalibCalcFromPast.UpdateNumPhes: yes/no
434// MCalibCalcFromPast.NumPhesDump: 10
435//
436Int_t MCalibCalcFromPast::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
437{
438 Bool_t rc = kFALSE;
439 if (IsEnvDefined(env, prefix, "UpdateWithFFactorMethod", print))
440 {
441 rc = kTRUE;
442 SetUpdateWithFFactorMethod(GetEnvValue(env, prefix, "UpdateWithFFactorMethod", fUpdateWithFFactorMethod));
443 }
444
445 if (IsEnvDefined(env, prefix, "NumEventsDump", print))
446 {
447 SetNumEventsDump(GetEnvValue(env, prefix, "NumEventsDump", (Int_t)fNumEventsDump));
448 rc = kTRUE;
449 }
450
451 if (IsEnvDefined(env, prefix, "UpdateNumPhes", print))
452 {
453 TString s = GetEnvValue(env, prefix, "UpdateNumPhes", "");
454 s.ToLower();
455 if (s.BeginsWith("no"))
456 SetUpdateNumPhes(kFALSE);
457 if (s.BeginsWith("yes"))
458 SetUpdateNumPhes(kTRUE);
459 rc = kTRUE;
460 }
461
462 if (IsEnvDefined(env, prefix, "NumPhesDump", print))
463 {
464 SetNumPhesDump(GetEnvValue(env, prefix, "NumPhesDump", (Int_t)fNumPhesDump));
465 rc = kTRUE;
466 }
467
468
469
470 return rc;
471}
Note: See TracBrowser for help on using the repository browser.