source: trunk/MagicSoft/Mars/mcalib/MCalibrationPix.cc@ 2925

Last change on this file since 2925 was 2922, checked in by gaug, 22 years ago
*** empty log message ***
File size: 20.7 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 11/2003 <mailto:markus@ifae.es>
19!
20! Copyright: MAGIC Software Development, 2000-2001
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26// //
27// MCalibrationPix //
28// //
29// This is the storage container to hold informations about the pedestal //
30// (offset) value of one Pixel (PMT). //
31// //
32/////////////////////////////////////////////////////////////////////////////
33#include "MCalibrationPix.h"
34#include "MCalibrationConfig.h"
35
36#include "MLog.h"
37#include "MLogManip.h"
38
39ClassImp(MCalibrationPix);
40
41using namespace std;
42
43// --------------------------------------------------------------------------
44//
45// Default Constructor:
46//
47// The following values are initialized to meaningful values:
48//
49// - The Electronic Rms to 1.5 per FADC slice
50// - The uncertainty about the Electronic RMS to 0.3 per slice
51// - The F-Factor is assumed to have been measured in Munich to 1.13 - 1.17.
52// We use here the Square of the Munich definition, thus:
53// Mean F-Factor = 1.15*1.15 = 1.32
54// Error F-Factor = 2.*0.02 = 0.04
55//
56MCalibrationPix::MCalibrationPix(const char *name, const char *title)
57 : fPixId(-1),
58 fCharge(-1.),
59 fErrCharge(-1.),
60 fSigmaCharge(-1.),
61 fErrSigmaCharge(-1.),
62 fRSigmaSquare(-1.),
63 fChargeProb(-1.),
64 fPed(-1.),
65 fPedRms(-1.),
66 fErrPedRms(0.),
67 fElectronicPedRms(1.5),
68 fErrElectronicPedRms(0.3),
69 fTime(-1.),
70 fSigmaTime(-1.),
71 fTimeChiSquare(-1.),
72 fFactor(1.32),
73 fFactorError(0.04),
74 fPheFFactorMethod(-1.),
75 fPheFFactorMethodError(-1.),
76 fConversionFFactorMethod(-1.),
77 fConversionBlindPixelMethod(-1.),
78 fConversionPINDiodeMethod(-1.),
79 fConversionErrorFFactorMethod(-1.),
80 fConversionErrorBlindPixelMethod(-1.),
81 fConversionErrorPINDiodeMethod(-1.),
82 fConversionSigmaFFactorMethod(-1.),
83 fConversionSigmaBlindPixelMethod(-1.),
84 fConversionSigmaPINDiodeMethod(-1.),
85 fFlags(0),
86 fChargeLimit(3.),
87 fChargeErrLimit(0.),
88 fChargeRelErrLimit(1.)
89{
90
91 fName = name ? name : "MCalibrationPixel";
92 fTitle = title ? title : "Container of the MHCalibrationPixels and the fit results";
93
94 //
95 // At the moment, we don't have a database, yet,
96 // so we get it from the configuration file
97 //
98 fConversionHiLo = gkConversionHiLo;
99 fConversionHiLoError = gkConversionHiLoError;
100
101 fHist = new MHCalibrationPixel("MHCalibrationPixel","Calibration Histograms Pixel ");
102
103 if (!fHist)
104 *fLog << warn << dbginf << " Could not create MHCalibrationPixel " << endl;
105
106 CLRBIT(fFlags, kHiGainSaturation);
107 CLRBIT(fFlags, kExcluded);
108 CLRBIT(fFlags, kFitValid);
109 CLRBIT(fFlags, kFitted);
110 CLRBIT(fFlags, kBlindPixelMethodValid);
111 CLRBIT(fFlags, kFFactorMethodValid);
112 CLRBIT(fFlags, kPINDiodeMethodValid);
113
114}
115
116MCalibrationPix::~MCalibrationPix()
117{
118 delete fHist;
119}
120
121
122
123
124void MCalibrationPix::DefinePixId(Int_t i)
125{
126
127 fPixId = i;
128 fHist->ChangeHistId(i);
129
130}
131
132
133// ------------------------------------------------------------------------
134//
135// Invalidate values
136//
137void MCalibrationPix::Clear(Option_t *o)
138{
139
140 fHist->Reset();
141
142 CLRBIT(fFlags, kHiGainSaturation);
143 CLRBIT(fFlags, kExcluded);
144 CLRBIT(fFlags, kFitValid);
145 CLRBIT(fFlags, kFitted);
146 CLRBIT(fFlags, kBlindPixelMethodValid);
147 CLRBIT(fFlags, kFFactorMethodValid);
148 CLRBIT(fFlags, kPINDiodeMethodValid);
149
150}
151
152// --------------------------------------------------------------------------
153//
154// Set the pedestals from outside
155//
156void MCalibrationPix::SetPedestal(Float_t ped, Float_t pedrms)
157{
158
159 fPed = ped;
160 fPedRms = pedrms;
161
162}
163
164// --------------------------------------------------------------------------
165//
166// Set the conversion factors from outside (only for MC)
167//
168void MCalibrationPix::SetConversionFFactorMethod(Float_t c, Float_t err, Float_t sig)
169{
170 fConversionFFactorMethod = c;
171 fConversionErrorFFactorMethod = err;
172 fConversionSigmaFFactorMethod = sig;
173}
174
175
176// --------------------------------------------------------------------------
177//
178// Set the conversion factors from outside (only for MC)
179//
180void MCalibrationPix::SetConversionBlindPixelMethod(Float_t c, Float_t err, Float_t sig)
181{
182 fConversionBlindPixelMethod = c;
183 fConversionErrorBlindPixelMethod = err;
184 fConversionSigmaBlindPixelMethod = sig;
185}
186
187// --------------------------------------------------------------------------
188//
189// Set the conversion factors from outside (only for MC)
190//
191void MCalibrationPix::SetConversionPINDiodeMethod(Float_t c, Float_t err, Float_t sig)
192{
193 fConversionPINDiodeMethod = c ;
194 fConversionErrorPINDiodeMethod = err;
195 fConversionSigmaPINDiodeMethod = sig;
196}
197
198// --------------------------------------------------------------------------
199//
200// Set the Hi Gain Saturation Bit from outside (only for MC)
201//
202void MCalibrationPix::SetHiGainSaturation(Bool_t b)
203{
204
205 if (b)
206 {
207 SETBIT(fFlags, kHiGainSaturation);
208 fHist->SetUseLoGain(1);
209 }
210 else
211 {
212 CLRBIT(fFlags, kHiGainSaturation);
213 fHist->SetUseLoGain(0);
214 }
215}
216
217// --------------------------------------------------------------------------
218//
219// Set the Excluded Bit from outside
220//
221void MCalibrationPix::SetExcluded(Bool_t b )
222{
223 b ? SETBIT(fFlags, kExcluded) : CLRBIT(fFlags, kExcluded);
224}
225
226
227// --------------------------------------------------------------------------
228//
229// Set the Excluded Bit from outside
230//
231void MCalibrationPix::SetExcludeQualityCheck(Bool_t b )
232{
233 b ? SETBIT(fFlags, kExcludeQualityCheck) : CLRBIT(fFlags, kExcludeQualityCheck);
234}
235
236// --------------------------------------------------------------------------
237//
238// Set the Excluded Bit from outside
239//
240void MCalibrationPix::SetFitValid(Bool_t b )
241{
242 b ? SETBIT(fFlags, kFitValid) : CLRBIT(fFlags, kFitValid);
243}
244
245// --------------------------------------------------------------------------
246//
247// Set the Excluded Bit from outside
248//
249void MCalibrationPix::SetFitted(Bool_t b )
250{
251 b ? SETBIT(fFlags, kFitted) : CLRBIT(fFlags, kFitted);
252}
253
254// --------------------------------------------------------------------------
255//
256// Set the Excluded Bit from outside
257//
258void MCalibrationPix::SetBlindPixelMethodValid(Bool_t b )
259{
260 b ? SETBIT(fFlags, kBlindPixelMethodValid) : CLRBIT(fFlags, kBlindPixelMethodValid);
261}
262
263// --------------------------------------------------------------------------
264//
265// Set the Excluded Bit from outside
266//
267void MCalibrationPix::SetFFactorMethodValid(Bool_t b )
268{
269 b ? SETBIT(fFlags, kFFactorMethodValid) : CLRBIT(fFlags, kFFactorMethodValid);
270}
271
272// --------------------------------------------------------------------------
273//
274// Set the Excluded Bit from outside
275//
276void MCalibrationPix::SetPINDiodeMethodValid(Bool_t b )
277{
278 b ? SETBIT(fFlags, kPINDiodeMethodValid) : CLRBIT(fFlags, kPINDiodeMethodValid);
279}
280
281
282Bool_t MCalibrationPix::IsExcluded() const
283 {
284 return TESTBIT(fFlags,kExcluded);
285 }
286
287Bool_t MCalibrationPix::IsFitValid() const
288{
289 return TESTBIT(fFlags, kFitValid);
290}
291
292Bool_t MCalibrationPix::IsFitted() const
293{
294 return TESTBIT(fFlags, kFitted);
295}
296
297Bool_t MCalibrationPix::IsBlindPixelMethodValid() const
298{
299 return TESTBIT(fFlags, kBlindPixelMethodValid);
300}
301
302Bool_t MCalibrationPix::IsFFactorMethodValid() const
303{
304 return TESTBIT(fFlags, kFFactorMethodValid);
305}
306
307Bool_t MCalibrationPix::IsPINDiodeMethodValid() const
308{
309 return TESTBIT(fFlags, kPINDiodeMethodValid);
310}
311
312
313// --------------------------------------------------------------------------
314//
315// 1) Return if the charge distribution is already succesfully fitted
316// or if the histogram is empty
317// 2) Set a lower Fit range according to 1.5 Pedestal RMS in order to avoid
318// possible remaining cosmics to spoil the fit.
319// 3) Decide if the LoGain Histogram is fitted or the HiGain Histogram
320// 4) Fit the histograms with a Gaussian
321// 5) In case of failure print out the fit results
322// 6) Retrieve the results and store them in this class
323// 7) Calculate the number of photo-electrons after the F-Factor method
324// 8) Calculate the errors of the F-Factor method
325//
326// The fits are declared valid (fFitValid = kTRUE), if:
327//
328// 1) Pixel has a fitted charge greater than 5*PedRMS
329// 2) Pixel has a fit error greater than 0.
330// 3) Pixel has a fit Probability greater than 0.0001
331// 4) Pixel has a charge sigma bigger than its Pedestal RMS
332// 5) If FitTimes is used,
333// the mean arrival time is at least 1.0 slices from the used edge slices
334// (this stage is only performed in the times fit)
335//
336// If the histogram is empty, all values are set to -1.
337//
338// The conversion factor after the F-Factor method is declared valid, if:
339//
340// 1) fFitValid is kTRUE
341// 2) Conversion Factor is bigger than 0.
342// 3) The error of the conversion factor is smaller than 10%
343//
344Bool_t MCalibrationPix::FitCharge()
345{
346
347 //
348 // 1) Return if the charge distribution is already succesfully fitted
349 // or if the histogram is empty
350 //
351 if (fHist->IsFitOK() || fHist->IsEmpty())
352 return kTRUE;
353
354 //
355 // 2) Set a lower Fit range according to 1.5 Pedestal RMS in order to avoid
356 // possible remaining cosmics to spoil the fit.
357 //
358 // if (fPed && fPedRms)
359 // fHist->SetLowerFitRange(1.5*fPedRms);
360 // else
361 // *fLog << warn << "WARNING: Cannot set lower fit range: Pedestals not available" << endl;
362
363 //
364 // 3) Decide if the LoGain Histogram is fitted or the HiGain Histogram
365 //
366 if (fHist->UseLoGain())
367 SetHiGainSaturation();
368
369 //
370 // 4) Fit the Lo Gain histograms with a Gaussian
371 //
372 if(fHist->FitCharge())
373 {
374 SETBIT(fFlags,kFitted);
375 }
376 else
377 {
378 *fLog << warn << "WARNING: Could not fit charges of pixel " << fPixId << endl;
379 //
380 // 5) In case of failure print out the fit results
381 //
382 // fHist->PrintChargeFitResult();
383 CLRBIT(fFlags,kFitted);
384 }
385
386 //
387 // 6) Retrieve the results and store them in this class
388 //
389 fCharge = fHist->GetChargeMean();
390 fErrCharge = fHist->GetChargeMeanErr();
391 fSigmaCharge = fHist->GetChargeSigma();
392 fErrSigmaCharge = fHist->GetChargeSigmaErr();
393 fChargeProb = fHist->GetChargeProb();
394
395 if (CheckChargeFitValidity())
396 SETBIT(fFlags,kFitValid);
397 else
398 {
399 CLRBIT(fFlags,kFitValid);
400 return kFALSE;
401 }
402
403 //
404 // 7) Calculate the number of photo-electrons after the F-Factor method
405 // 8) Calculate the errors of the F-Factor method
406 //
407 if ((fPed > 0.) && (fPedRms > 0.))
408 {
409
410 //
411 // Square all variables in order to avoid applications of square root
412 //
413 // First the relative error squares
414 //
415 const Float_t chargeSquare = fCharge* fCharge;
416 const Float_t chargeSquareRelErrSquare = 4.*fErrCharge*fErrCharge / chargeSquare;
417
418 const Float_t fFactorRelErrSquare = fFactorError * fFactorError / (fFactor * fFactor);
419 //
420 // Now the absolute error squares
421 //
422 const Float_t sigmaSquare = fSigmaCharge* fSigmaCharge;
423 const Float_t sigmaSquareErrSquare = 4.*fErrSigmaCharge*fErrSigmaCharge * sigmaSquare;
424
425 const Float_t elecRmsSquare = fElectronicPedRms* fElectronicPedRms;
426 const Float_t elecRmsSquareErrSquare = 4.*fErrElectronicPedRms*fErrElectronicPedRms * elecRmsSquare;
427
428 Float_t pedRmsSquare = fPedRms* fPedRms;
429 Float_t pedRmsSquareErrSquare = 4.*fErrPedRms*fErrPedRms * pedRmsSquare;
430
431 if (TESTBIT(fFlags,kHiGainSaturation))
432 {
433
434 //
435 // We do not know the Lo Gain Pedestal RMS, so we have to retrieve it
436 // from the Hi Gain:
437 //
438 // We extract the pure NSB contribution:
439 //
440 Float_t nsbSquare = pedRmsSquare - elecRmsSquare;
441 Float_t nsbSquareRelErrSquare = (pedRmsSquareErrSquare + elecRmsSquareErrSquare)
442 / (nsbSquare * nsbSquare) ;
443
444 if (nsbSquare < 0.)
445 nsbSquare = 0.;
446
447 //
448 // Now, we divide the NSB by the conversion factor and
449 // add it quadratically to the electronic noise
450 //
451 const Float_t conversionSquare = fConversionHiLo *fConversionHiLo;
452 const Float_t conversionSquareRelErrSquare = 4.*fConversionHiLoError*fConversionHiLoError/conversionSquare;
453
454 //
455 // Calculate the new "Pedestal RMS"
456 //
457 const Float_t convertedNsbSquare = nsbSquare / conversionSquare;
458 const Float_t convertedNsbSquareErrSquare = (nsbSquareRelErrSquare + conversionSquareRelErrSquare)
459 * convertedNsbSquare * convertedNsbSquare;
460
461 pedRmsSquare = convertedNsbSquare + elecRmsSquare;
462 pedRmsSquareErrSquare = convertedNsbSquareErrSquare + elecRmsSquareErrSquare;
463
464 } /* if (kHiGainSaturation) */
465
466 //
467 // Calculate the reduced sigmas
468 //
469 fRSigmaSquare = sigmaSquare - pedRmsSquare;
470 if (fRSigmaSquare <= 0.)
471 {
472 *fLog << warn
473 << "WARNING: Cannot apply F-Factor calibration: Reduced Sigma smaller than 0 in pixel "
474 << fPixId << endl;
475 if (TESTBIT(fFlags,kHiGainSaturation))
476 ApplyLoGainConversion();
477 return kFALSE;
478 }
479
480 const Float_t rSigmaSquareRelErrSquare = (sigmaSquareErrSquare + pedRmsSquareErrSquare)
481 / (fRSigmaSquare * fRSigmaSquare) ;
482
483 //
484 // Calculate the number of phe's from the F-Factor method
485 // (independent on Hi Gain or Lo Gain)
486 //
487 fPheFFactorMethod = fFactor * chargeSquare / fRSigmaSquare;
488
489 const Float_t pheFFactorRelErrSquare = fFactorRelErrSquare
490 + chargeSquareRelErrSquare
491 + rSigmaSquareRelErrSquare ;
492
493 fPheFFactorMethodError = TMath::Sqrt(pheFFactorRelErrSquare) * fPheFFactorMethod;
494
495 //
496 // Calculate the conversion factors
497 //
498 if (TESTBIT(fFlags,kHiGainSaturation))
499 ApplyLoGainConversion();
500
501 const Float_t chargeRelErrSquare = fErrCharge*fErrCharge / (fCharge * fCharge);
502
503 fConversionFFactorMethod = fPheFFactorMethod / fCharge ;
504 fConversionErrorFFactorMethod = ( pheFFactorRelErrSquare + chargeRelErrSquare )
505 * fConversionFFactorMethod * fConversionFFactorMethod;
506
507 if ( IsFitValid() &&
508 (fConversionFFactorMethod > 0.) &&
509 (fConversionErrorFFactorMethod/fConversionFFactorMethod < 0.1) )
510 SETBIT(fFlags,kFFactorMethodValid);
511 else
512 CLRBIT(fFlags,kFFactorMethodValid);
513
514 } /* if ((fPed > 0.) && (fPedRms > 0.)) */
515
516 return kTRUE;
517
518}
519
520//
521// The check return kTRUE if:
522//
523// 1) Pixel has a fitted charge greater than 5*PedRMS
524// 2) Pixel has a fit error greater than 0.
525// 3) Pixel has a fitted charge greater its charge error
526// 4) Pixel has a fit Probability greater than 0.0001
527// 5) Pixel has a charge sigma bigger than its Pedestal RMS
528//
529Bool_t MCalibrationPix::CheckChargeFitValidity()
530{
531
532 if (TESTBIT(fFlags,kExcludeQualityCheck))
533 return kTRUE;
534
535 Float_t equivpedestal = GetPedRms();
536
537 if (TESTBIT(fFlags,kHiGainSaturation))
538 equivpedestal /= fConversionHiLo;
539
540 if (fCharge < fChargeLimit*equivpedestal)
541 {
542 *fLog << warn << "WARNING: Fitted Charge is smaller than "
543 << fChargeLimit << " Pedestal RMS in Pixel " << fPixId << endl;
544 return kFALSE;
545 }
546
547 if (fErrCharge < fChargeErrLimit)
548 {
549 *fLog << warn << "WARNING: Error of Fitted Charge is smaller than "
550 << fChargeErrLimit << " in Pixel " << fPixId << endl;
551 return kFALSE;
552 }
553
554 if (fCharge < fChargeRelErrLimit*fErrCharge)
555 {
556 *fLog << warn << "WARNING: Error of Fitted Charge is greater than "
557 << fChargeRelErrLimit << "* Fitted Charges itself in Pixel " << fPixId << endl;
558 return kFALSE;
559 }
560
561 if (!fHist->IsFitOK())
562 {
563 *fLog << warn << "WARNING: Probability of Fitted Charge too low in Pixel " << fPixId << endl;
564 return kFALSE;
565 }
566
567 if (fSigmaCharge < equivpedestal)
568 {
569 *fLog << warn << "WARNING: Sigma of Fitted Charge smaller than Pedestal RMS in Pixel " << fPixId << endl;
570 return kFALSE;
571 }
572 return kTRUE;
573}
574
575//
576// The check returns kTRUE if:
577//
578// The mean arrival time is at least 1.0 slices from the used edge slices
579//
580Bool_t MCalibrationPix::CheckTimeFitValidity()
581{
582
583 if (TESTBIT(fFlags,kExcludeQualityCheck))
584 return kTRUE;
585
586 Float_t lowerrange;
587 Float_t upperrange;
588
589 if (TESTBIT(fFlags,kHiGainSaturation))
590 {
591 lowerrange = (Float_t)fHist->GetTimeLowerFitRangeLoGain()+1.;
592 upperrange = (Float_t)fHist->GetTimeUpperFitRangeLoGain()+1.;
593 }
594 else
595 {
596 lowerrange = (Float_t)fHist->GetTimeLowerFitRangeHiGain()+1.;
597 upperrange = (Float_t)fHist->GetTimeUpperFitRangeHiGain()+1.;
598 }
599
600
601 if (fTime < lowerrange)
602 {
603 *fLog << warn
604 << "WARNING: Mean Fitted Time inside or smaller than first used FADC slice in Pixel "
605 << fPixId << " time: " << fTime << " Range: " << lowerrange << endl;
606 return kFALSE;
607 }
608
609 if (fTime > upperrange)
610 {
611 *fLog << warn
612 << "WARNING: Mean Fitted Time inside or greater than last used FADC slice in Pixel "
613 << fPixId << " time: " << fTime << " Range: " << upperrange << endl;
614 return kFALSE;
615 }
616
617 return kTRUE;
618}
619
620
621//
622// The check returns kTRUE if:
623//
624//
625//
626Bool_t MCalibrationPix::CheckOscillations()
627{
628
629
630 return kTRUE;
631}
632
633
634
635void MCalibrationPix::ApplyLoGainConversion()
636{
637
638 const Float_t chargeRelErrSquare = fErrCharge*fErrCharge
639 /( fCharge * fCharge);
640 const Float_t sigmaRelErrSquare = fErrSigmaCharge*fErrSigmaCharge
641 /( fSigmaCharge * fSigmaCharge);
642 const Float_t conversionRelErrSquare = fConversionHiLoError*fConversionHiLoError
643 /(fConversionHiLo * fConversionHiLo);
644
645 fCharge *= fConversionHiLo;
646 fErrCharge = TMath::Sqrt(chargeRelErrSquare + conversionRelErrSquare) * fCharge;
647
648 fSigmaCharge *= fConversionHiLo;
649 fErrSigmaCharge = TMath::Sqrt(sigmaRelErrSquare + conversionRelErrSquare) * fSigmaCharge;
650
651}
652
653
654
655// --------------------------------------------------------------------------
656//
657// 1) Fit the arrival times
658// 2) Retrieve the results
659// 3) Note that because of the low number of bins, the NDf is sometimes 0, so
660// Root does not give a reasonable Probability, the Chisquare is more significant
661//
662// This fit has to be done AFTER the Charges fit,
663// otherwise only the Hi Gain will be fitted, even if there are no entries
664//
665//
666Bool_t MCalibrationPix::FitTime()
667{
668
669 //
670 // Fit the Low Gain
671 //
672 if (TESTBIT(fFlags,kHiGainSaturation))
673 {
674 if(!fHist->FitTimeLoGain())
675 {
676 *fLog << warn << "WARNING: Could not fit Lo Gain times of pixel " << fPixId << endl;
677 // fHist->PrintTimeFitResult();
678 return kFALSE;
679 }
680 }
681
682 //
683 // Fit the High Gain
684 //
685 else
686 {
687 if(!fHist->FitTimeHiGain())
688 {
689 *fLog << warn << "WARNING: Could not fit Hi Gain times of pixel " << fPixId << endl;
690 // fHist->PrintTimeFitResult();
691 return kFALSE;
692 }
693 }
694
695 fTime = fHist->GetTimeMean();
696 fSigmaTime = fHist->GetTimeSigma();
697 fTimeChiSquare = fHist->GetTimeChiSquare();
698 fTimeProb = fHist->GetTimeProb();
699
700 if (CheckTimeFitValidity())
701 SETBIT(fFlags,kFitValid);
702 else
703 CLRBIT(fFlags,kFitValid);
704
705 return kTRUE;
706}
707
Note: See TracBrowser for help on using the repository browser.