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

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