source: tags/Mars-V0.9/mcalib/MCalibrationRelTimeCalc.cc

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