source: trunk/MagicSoft/Mars/mcalib/MCalibrationRelTimeCalc.cc@ 8417

Last change on this file since 8417 was 8417, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 15.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 04/2004 <mailto:markus@ifae.es>
19!
20! Copyright: MAGIC Software Development, 2000-2004
21!
22!
23\* ======================================================================== */
24
25//////////////////////////////////////////////////////////////////////////////
26//
27// MCalibrationRelTimeCalc
28//
29// Task to finalize the relative time calibration obtained
30// from the fit results to the summed FADC slice distributions delivered by
31// MCalibrationRelTimeCam, calculated and filled by MHCalibrationRelTimeCam,
32//
33// PreProcess(): Initialize pointers to MCalibrationRelTimeCam,
34//
35// ReInit(): Initializes pointer to MBadPixelsCam
36//
37// Process(): Nothing to be done, histograms getting filled by MHCalibrationChargeCam
38//
39// PostProcess(): - FinalizeRelTimes()
40// - FinalizeBadPixels()
41//
42// Class Version 2:
43// + Byte_t fCheckFlags; // Bit-field to hold the possible check flags
44//
45// ClassVersionb 3:
46// - TString fOutputPath; // Path to the output file
47// - TString fOutputFile; // Name of the output file
48//
49//
50// Input Containers:
51// MCalibrationRelTimeCam
52// MBadPixelsCam
53// MGeomCam
54//
55// Output Containers:
56// MCalibrationRelTimeCam
57// MBadPixelsCam
58//
59//
60//////////////////////////////////////////////////////////////////////////////
61#include "MCalibrationRelTimeCalc.h"
62
63#include "MLog.h"
64#include "MLogManip.h"
65
66#include "MParList.h"
67
68#include "MStatusDisplay.h"
69
70#include "MGeomCam.h"
71#include "MGeomPix.h"
72
73#include "MCalibrationIntensityRelTimeCam.h"
74#include "MCalibrationRelTimeCam.h"
75#include "MCalibrationRelTimePix.h"
76
77#include "MBadPixelsCam.h"
78#include "MBadPixelsPix.h"
79
80ClassImp(MCalibrationRelTimeCalc);
81
82using namespace std;
83
84const Float_t MCalibrationRelTimeCalc::fgRelTimeResolutionLimit = 0.05;
85
86// --------------------------------------------------------------------------
87//
88// Default constructor.
89//
90// Sets all pointers to NULL
91//
92// Initializes:
93// - fRelTimeResolutionLimit to fgRelTimeResolutionimit
94//
95// Calls:
96// - Clear()
97//
98MCalibrationRelTimeCalc::MCalibrationRelTimeCalc(const char *name, const char *title)
99 : fGeom(NULL), fFlags(0)
100{
101
102 fName = name ? name : "MCalibrationRelTimeCalc";
103 fTitle = title ? title : "Task to finalize the relative time calibration";
104
105 SetCheckFitResults ( kFALSE );
106 SetCheckDeviatingBehavior( kFALSE );
107 SetCheckHistOverflow ( kFALSE );
108 SetCheckOscillations ( kFALSE );
109
110 SetRelTimeResolutionLimit();
111
112 Clear();
113
114}
115
116// --------------------------------------------------------------------------
117//
118// Sets:
119// - all flags to kFALSE
120// - all pointers to NULL
121//
122void MCalibrationRelTimeCalc::Clear(const Option_t *o)
123{
124
125 fBadPixels = NULL;
126 fCam = NULL;
127 fIntensCam = NULL;
128
129}
130
131// --------------------------------------------------------------------------
132//
133// Search for the following input containers and abort if not existing:
134// - MGeomCam
135// - MCalibrationIntensityRelTimeCam or MCalibrationRelTimeCam
136// - MBadPixelsCam
137//
138// It defines the PixId of every pixel in:
139//
140// - MCalibrationRelTimeCam
141//
142// It sets all pixels in excluded which have the flag fBadBixelsPix::IsBad() set in:
143//
144// - MCalibrationRelTimePix
145//
146Bool_t MCalibrationRelTimeCalc::ReInit(MParList *pList )
147{
148
149 fGeom = (MGeomCam*)pList->FindObject("MGeomCam");
150 if (!fGeom)
151 {
152 *fLog << err << "No MGeomCam found... aborting." << endl;
153 return kFALSE;
154 }
155
156 fBadPixels = (MBadPixelsCam*)pList->FindObject(AddSerialNumber("MBadPixelsCam"));
157 if (!fBadPixels)
158 {
159 *fLog << err << "Cannot find MBadPixelsCam ... abort." << endl;
160 return kFALSE;
161 }
162
163 fIntensCam = (MCalibrationIntensityRelTimeCam*)pList->FindObject(AddSerialNumber("MCalibrationIntensityRelTimeCam"));
164 if (fIntensCam)
165 *fLog << inf << "Found MCalibrationIntensityRelTimeCam ... " << endl;
166 else
167 {
168 fCam = (MCalibrationRelTimeCam*)pList->FindObject(AddSerialNumber("MCalibrationRelTimeCam"));
169 if (!fCam)
170 {
171 *fLog << err << "Cannot find MCalibrationRelTimeCam ... abort." << endl;
172 *fLog << err << "Maybe you forget to call an MFillH for the MHCalibrationRelTimeCam before..." << endl;
173 return kFALSE;
174 }
175 }
176
177 UInt_t npixels = fGeom->GetNumPixels();
178
179 MCalibrationRelTimeCam *relcam = fIntensCam
180 ? (MCalibrationRelTimeCam*)fIntensCam->GetCam() : fCam;
181
182 for (UInt_t i=0; i<npixels; i++)
183 {
184
185 MCalibrationRelTimePix &pix = (MCalibrationRelTimePix&)(*relcam)[i];
186
187 if ((*fBadPixels)[i].IsBad())
188 {
189 pix.SetExcluded();
190 continue;
191 }
192
193 if (IsDebug())
194 pix.SetDebug();
195 }
196
197 return kTRUE;
198}
199
200// -----------------------------------------------------------------------
201//
202// Return if number of executions is null.
203//
204Int_t MCalibrationRelTimeCalc::PostProcess()
205{
206
207 if (GetNumExecutions()==0)
208 return kTRUE;
209
210 return Finalize();
211}
212
213// -----------------------------------------------------------------------
214//
215// First loop over pixels, average areas and sectors, call:
216// - FinalizeRelTimes()
217// for every entry. Count number of valid pixels in loop and return kFALSE
218// if there are none (the "Michele check").
219//
220// Call FinalizeBadPixels()
221//
222// Call MParContainer::SetReadyToSave() for fCam
223//
224// Print out some statistics
225//
226Int_t MCalibrationRelTimeCalc::Finalize()
227{
228
229 //
230 // First loop over pixels, call FinalizePedestals and FinalizeRelTimes
231 //
232 FinalizeRelTimes();
233
234 //
235 // Finalize Bad Pixels
236 //
237 FinalizeBadPixels();
238
239 //
240 // Finalize calibration statistics
241 //
242 FinalizeUnsuitablePixels();
243
244 if (fIntensCam)
245 fIntensCam->SetReadyToSave();
246 else
247 fCam ->SetReadyToSave();
248
249 fBadPixels->SetReadyToSave();
250
251 *fLog << inf << endl;
252 *fLog << GetDescriptor() << ": Errors statistics:" << endl;
253
254 PrintUncalibrated(MBadPixelsPix::kDeviatingTimeResolution,
255 Form("%s%2.1f%s","Time resol. less than ", fRelTimeResolutionLimit, " FADC sl. from Mean:"));
256 PrintUncalibrated(MBadPixelsPix::kRelTimeOscillating,
257 "Pixels with changing Rel. Times over time:");
258 PrintUncalibrated(MBadPixelsPix::kRelTimeNotFitted,
259 "Pixels with unsuccesful Gauss fit to the times:");
260
261 return kTRUE;
262}
263
264
265// ----------------------------------------------------------------------------------------------------
266//
267//
268// First loop: Calculate a mean and mean RMS of time resolution per area index
269// Include only pixels which are not MBadPixelsPix::kUnsuitableRun or
270// MBadPixelsPix::kUnreliableRun (see FinalizeBadPixels())
271//
272// Second loop: Exclude those deviating by more than fRelTimeResolutionLimit FADC slices
273// from the mean (obtained in first loop). Set
274// MBadPixelsPix::kDeviatingTimeResolution if excluded.
275//
276void MCalibrationRelTimeCalc::FinalizeRelTimes()
277{
278
279 MCalibrationRelTimeCam *relcam = fIntensCam
280 ? (MCalibrationRelTimeCam*)fIntensCam->GetCam() : fCam;
281
282 const UInt_t npixels = fGeom->GetNumPixels();
283 const UInt_t nareas = fGeom->GetNumAreas();
284
285 TArrayF lowlim (nareas);
286 TArrayF upplim (nareas);
287 TArrayF areasum (nareas);
288 // Float_t areasum2 [nareas];
289 TArrayI numareavalid (nareas);
290 TArrayI useunreliable(nareas);
291
292 //
293 // Apero loop: Count number of unreliable pixels:
294 //
295 for (UInt_t i=0; i<npixels; i++)
296 {
297 MBadPixelsPix &bad = (*fBadPixels)[i];
298 const Int_t aidx = (*fGeom)[i].GetAidx();
299
300 if (bad.IsUnsuitable(MBadPixelsPix::kUnsuitableRun))
301 continue;
302
303 if (bad.IsUnsuitable(MBadPixelsPix::kUnreliableRun))
304 continue;
305
306 numareavalid[aidx] ++;
307 }
308
309 for (UInt_t aidx=0; aidx<nareas; aidx++)
310 if (numareavalid[aidx] < 100)
311 useunreliable[aidx] = 1;
312
313 numareavalid.Reset();
314 //
315 // First loop: Get mean time resolution the RMS
316 // The loop is only to recognize later pixels with very deviating numbers
317 //
318 for (UInt_t i=0; i<npixels; i++)
319 {
320
321 MCalibrationRelTimePix &pix = (MCalibrationRelTimePix&)(*relcam)[i];
322
323 if (pix.IsExcluded())
324 continue;
325
326 MBadPixelsPix &bad = (*fBadPixels)[i];
327 if (bad.IsUnsuitable(MBadPixelsPix::kUnsuitableRun))
328 continue;
329
330 const Int_t aidx = (*fGeom)[i].GetAidx();
331
332 if (!useunreliable[aidx])
333 if (bad.IsUnsuitable(MBadPixelsPix::kUnreliableRun))
334 continue;
335
336 const Float_t res = pix.GetTimePrecision();
337
338 areasum [aidx] += res;
339 // areasum2 [aidx] += res*res;
340 numareavalid[aidx] ++;
341 }
342
343
344 for (UInt_t aidx=0; aidx<nareas; aidx++)
345 {
346 if (numareavalid[aidx] < 20)
347 {
348 *fLog << warn << "Area " << setw(4) << aidx << ": Less than 20 pixels with valid time resolution found." << endl;
349 continue;
350 }
351
352 // Calculate the rms out of sum2:
353 /*
354 areasum2[aidx] = (areasum2[aidx] - areasum[aidx]*areasum[aidx]/numareavalid[aidx]);
355 areasum2[aidx] /= (numareavalid[aidx]-1.);
356 */
357 areasum [aidx] /= numareavalid[aidx];
358 lowlim [aidx] = 0;
359 upplim [aidx] = areasum [aidx] + fRelTimeResolutionLimit;
360
361 }
362 *fLog << endl;
363
364
365 for (UInt_t i=0; i<npixels; i++)
366 {
367
368 MCalibrationRelTimePix &pix = (MCalibrationRelTimePix&)(*relcam)[i];
369
370 if (pix.IsExcluded())
371 continue;
372
373 MBadPixelsPix &bad = (*fBadPixels)[i];
374 if (bad.IsUnsuitable(MBadPixelsPix::kUnsuitableRun))
375 continue;
376
377 const Float_t res = pix.GetTimePrecision();
378 const Int_t aidx = (*fGeom)[i].GetAidx();
379
380 if ( res < lowlim[aidx] || res > upplim[aidx] )
381 {
382 *fLog << warn << "Pixel " << setw(4) << i << ": Deviating time resolution: "
383 << Form("%4.2f",res) << " out of range ["
384 << Form("%4.2f,%4.2f",lowlim[aidx],upplim[aidx]) << "]" << endl;
385 bad.SetUncalibrated( MBadPixelsPix::kDeviatingTimeResolution);
386 pix.SetExcluded();
387 }
388 }
389}
390
391
392// -----------------------------------------------------------------------------------
393//
394// Sets pixel to MBadPixelsPix::kUnsuitableRun, if one of the following flags is set:
395// - MBadPixelsPix::kRelTimeIsPedestal
396// - MBadPixelsPix::kRelTimeErrNotValid
397// - MBadPixelsPix::kRelTimeRelErrNotValid
398//
399// - Call MCalibrationPix::SetExcluded() for the bad pixels
400//
401void MCalibrationRelTimeCalc::FinalizeBadPixels()
402{
403
404 MCalibrationRelTimeCam *relcam = fIntensCam
405 ? (MCalibrationRelTimeCam*)fIntensCam->GetCam() : fCam;
406
407 for (Int_t i=0; i<fBadPixels->GetSize(); i++)
408 {
409
410 MCalibrationRelTimePix &pix = (MCalibrationRelTimePix&)(*relcam)[i];
411
412 MBadPixelsPix &bad = (*fBadPixels)[i];
413
414 if (IsCheckDeviatingBehavior())
415 if (bad.IsUncalibrated(MBadPixelsPix::kDeviatingTimeResolution))
416 bad.SetUnsuitable(MBadPixelsPix::kUnreliableRun);
417
418 if (IsCheckFitResults())
419 if (bad.IsUncalibrated(MBadPixelsPix::kRelTimeNotFitted))
420 bad.SetUnsuitable(MBadPixelsPix::kUnreliableRun);
421
422 if (IsCheckOscillations())
423 if (bad.IsUncalibrated(MBadPixelsPix::kRelTimeOscillating))
424 bad.SetUnsuitable(MBadPixelsPix::kUnreliableRun);
425
426 if (bad.IsUnsuitable(MBadPixelsPix::kUnsuitableRun))
427 pix.SetExcluded();
428 }
429
430}
431
432
433// -----------------------------------------------------------------------------------------------
434//
435// - Print out statistics about BadPixels of type UnsuitableType_t
436// - store numbers of bad pixels of each type in fIntensCam or fCam
437//
438void MCalibrationRelTimeCalc::FinalizeUnsuitablePixels()
439{
440 *fLog << inf << endl;
441 *fLog << GetDescriptor() << ": Rel. Times Calibration status:" << endl;
442 *fLog << dec;
443
444 MCalibrationRelTimeCam *relcam = fIntensCam ? (MCalibrationRelTimeCam*)fIntensCam->GetCam() : fCam;
445
446 const Int_t nareas = fGeom->GetNumAreas();
447
448 TArrayI unsuit(nareas);
449 TArrayI unrel(nareas);
450
451 for (int aidx=0; aidx<nareas; aidx++)
452 {
453 unsuit[aidx] += fBadPixels->GetNumUnsuitable(MBadPixelsPix::kUnsuitableRun, fGeom, aidx);
454 unrel[aidx] += fBadPixels->GetNumUnsuitable(MBadPixelsPix::kUnreliableRun, fGeom, aidx);
455 relcam->SetNumUnsuitable(unsuit[aidx], aidx);
456 relcam->SetNumUnreliable(unrel[aidx], aidx);
457 }
458
459 if (fGeom->InheritsFrom("MGeomCamMagic"))
460 {
461 PrintUncalibrated("Uncalibrated Pixels:", unsuit[0], unsuit[1]);
462 PrintUncalibrated("Unreliable Pixels:", unrel[0], unrel[1]);
463 }
464}
465
466// -----------------------------------------------------------------------------------------------
467//
468// Print out statistics about BadPixels of type UncalibratedType_t
469//
470void MCalibrationRelTimeCalc::PrintUncalibrated(MBadPixelsPix::UncalibratedType_t typ, const char *text) const
471{
472 UInt_t countinner = 0;
473 UInt_t countouter = 0;
474 for (Int_t i=0; i<fBadPixels->GetSize(); i++)
475 {
476 if ((*fBadPixels)[i].IsUncalibrated(typ))
477 {
478 if (fGeom->GetPixRatio(i) == 1.)
479 countinner++;
480 else
481 countouter++;
482 }
483 }
484
485 PrintUncalibrated(text, countinner, countouter);
486}
487
488void MCalibrationRelTimeCalc::PrintUncalibrated(const char *text, Int_t in, Int_t out) const
489{
490 *fLog << " " << setfill(' ') << setw(56) << setiosflags(ios::left) << text;
491 *fLog << " Inner: " << Form("%3i", in);
492 *fLog << " Outer: " << Form("%3i", out);
493 *fLog << resetiosflags(ios::left) << endl;
494
495}
496
497// --------------------------------------------------------------------------
498//
499// MCalibrationRelTimeCam.CheckFitResults: Yes
500// MCalibrationRelTimeCam.CheckDeviatingBehavior: Yes
501// MCalibrationRelTimeCam.CheckHistOverflow: Yes
502// MCalibrationRelTimeCam.CheckOscillations: Yes
503//
504Int_t MCalibrationRelTimeCalc::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
505{
506 Bool_t rc = kFALSE;
507
508 if (IsEnvDefined(env, prefix, "CheckFitResults", print))
509 {
510 SetCheckFitResults(GetEnvValue(env, prefix, "CheckFitResults", IsCheckFitResults()));
511 rc = kTRUE;
512 }
513 if (IsEnvDefined(env, prefix, "CheckDeviatingBehavior", print))
514 {
515 SetCheckDeviatingBehavior(GetEnvValue(env, prefix, "CheckDeviatingBehavior", IsCheckDeviatingBehavior()));
516 rc = kTRUE;
517 }
518 if (IsEnvDefined(env, prefix, "CheckHistOverflow", print))
519 {
520 SetCheckHistOverflow(GetEnvValue(env, prefix, "CheckHistOverflow", IsCheckHistOverflow()));
521 rc = kTRUE;
522 }
523
524 if (IsEnvDefined(env, prefix, "CheckOscillations", print))
525 {
526 SetCheckOscillations(GetEnvValue(env, prefix, "CheckOscillations", IsCheckOscillations()));
527 rc = kTRUE;
528 }
529 if (IsEnvDefined(env, prefix, "RelTimeResolutionLimit", print))
530 {
531 SetRelTimeResolutionLimit(GetEnvValue(env, prefix, "RelTimeResolutionLimit", fRelTimeResolutionLimit));
532 rc = kTRUE;
533 }
534
535 return rc;
536}
Note: See TracBrowser for help on using the repository browser.