source: tags/Mars-V0.10.2/mcalib/MCalibrateData.cc

Last change on this file was 7886, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 31.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): Javier Lopez 12/2003 <mailto:jlopez@ifae.es>
19! Author(s): Javier Rico 01/2004 <mailto:jrico@ifae.es>
20! Author(s): Wolfgang Wittek 02/2004 <mailto:wittek@mppmu.mpg.de>
21! Author(s): Markus Gaug 04/2004 <mailto:markus@ifae.es>
22! Author(s): Hendrik Bartko 08/2004 <mailto:hbartko@mppmu.mpg.de>
23! Author(s): Thomas Bretz 08/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
24!
25! Copyright: MAGIC Software Development, 2000-2004
26!
27!
28\* ======================================================================== */
29
30///////////////////////////////////////////////////////////////////////////////////
31//
32// MCalibrateData
33//
34// This task takes the integrated charge from MExtractedSignal and applies
35// the calibration constants from MCalibrationCam to convert the summed FADC
36// slices into photons. The number of photons obtained is stored in MSignalCam.
37// Optionally, the calibration of pedestals from an MPedestalCam container into
38// an MPedPhotCam container can be chosen with the member functions
39// SetPedestalType(). Default is 'kRun', i.e. calibration of pedestals from a
40// dedicated pedestal run.
41// In case, the chosen pedestal type is kRun or kEvent, in ReInit() the MPedPhotCam
42// container is filled using the information from MPedestalCam, MExtractedSignalCam,
43// MCalibrationChargeCam and MCalibrationQECam
44//
45// Selection of different calibration methods is allowed through the
46// SetCalibrationMode() member function (default: kFfactor)
47//
48// The calibration modes which exclude non-valid pixels are the following:
49//
50// kFfactor: calibrates using the F-Factor method
51// kBlindpixel: calibrates using the BlindPixel method
52// kBlindpixel: calibrates using the BlindPixel method
53// kFlatCharge: perform a charge flat-flatfielding. Outer pixels are area-corrected.
54// kDummy: calibrates with fixed conversion factors of 1 and errors of 0.
55//
56// The calibration modes which include all pixels regardless of their validity is:
57//
58// kNone: calibrates with fixed conversion factors of 1 and errors of 0.
59//
60// Use the kDummy and kNone methods ONLY FOR DEBUGGING!
61//
62//
63// This class can calibrate data and/or pedestals. To switch off calibration of data
64// set the Calibration Mode to kSkip. To switch on pedestal calibration call either
65// SetPedestalFlag(MCalibrateData::kRun) (calibration is done once in ReInit)
66// SetPedestalFlag(MCalibrateData::kEvent) (calibration is done for each event)
67//
68// By calling AddPedestal() you can control the name of the
69// MPedestalCam and/or MPedPhotCam container which is used.
70//
71// Assume you want to calibrate "MPedestalCam" once and "MPedestalFromLoGain" [MPedestalCam]
72// event-by-event, so:
73// MCalibrateData cal1;
74// cal1.SetCalibrationMode(MCalibrateData::kSkip);
75// cal1.SetPedestalFlag(MCalibrateData::kRun);
76// MCalibrateData cal2;
77// cal2.SetCalibrationMode(MCalibrateData::kSkip);
78// cal2.AddPedestal("FromLoGain");
79// cal2.SetPedestalFlag(MCalibrateData::kEvent);
80//
81//
82// Input Containers:
83// [MPedestalCam]
84// [MExtractedSignalCam]
85// [MCalibrationChargeCam]
86// [MCalibrationQECam]
87// MBadPixelsCam
88//
89// Output Containers:
90// [MPedPhotCam]
91// [MSignalCam]
92//
93// See also: MJCalibration, MJPedestal, MJExtractSignal, MJExtractCalibTest
94//
95//////////////////////////////////////////////////////////////////////////////
96#include "MCalibrateData.h"
97
98#include <fstream>
99
100#include <TEnv.h>
101
102#include "MLog.h"
103#include "MLogManip.h"
104
105#include "MParList.h"
106#include "MH.h"
107
108#include "MGeomCam.h"
109#include "MRawRunHeader.h"
110
111#include "MPedestalCam.h"
112#include "MPedestalPix.h"
113
114#include "MCalibrationIntensityChargeCam.h"
115#include "MCalibrationChargeCam.h"
116#include "MCalibrationChargePix.h"
117
118#include "MCalibrationIntensityQECam.h"
119#include "MCalibrationQECam.h"
120#include "MCalibrationQEPix.h"
121
122#include "MCalibrationIntensityConstCam.h"
123#include "MCalibConstCam.h"
124#include "MCalibConstPix.h"
125
126#include "MExtractedSignalCam.h"
127#include "MExtractedSignalPix.h"
128
129#include "MPedPhotCam.h"
130#include "MPedPhotPix.h"
131
132#include "MBadPixelsCam.h"
133#include "MBadPixelsPix.h"
134
135#include "MSignalCam.h"
136
137ClassImp(MCalibrateData);
138
139using namespace std;
140
141const Float_t MCalibrateData::gkCalibConvMinLimit = 0.01;
142const Float_t MCalibrateData::gkCalibConvMaxLimit = 5.;
143
144const MCalibrateData::CalibrationMode_t MCalibrateData::gkDefault = kFfactor;
145
146// --------------------------------------------------------------------------
147//
148// Default constructor.
149//
150// Sets all pointers to NULL
151//
152// Initializes:
153// - fCalibrationMode to kDefault
154// - fPedestalFlag to kNo
155//
156MCalibrateData::MCalibrateData(CalibrationMode_t calmode,const char *name, const char *title)
157 : fGeomCam(NULL), fBadPixels(NULL), fCalibrations(NULL), fIntensCalib(NULL),
158 fQEs(NULL), fIntensQE(NULL), fSignals(NULL), fCerPhotEvt(NULL), fCalibConstCam(NULL),
159 fIntensConst(NULL), fPedestalFlag(kNo), fSignalType(kPhot), fRenormFactor(1.),
160 fScaleFactor(1.)
161{
162
163 fName = name ? name : "MCalibrateData";
164 fTitle = title ? title : "Task to calculate the number of photons in one event";
165
166 SetCalibrationMode(calmode);
167
168 SetCalibConvMinLimit();
169 SetCalibConvMaxLimit();
170
171 fNamesPedestal.SetOwner();
172}
173
174void MCalibrateData::AddPedestal(const char *name)
175{
176 TString ped(name);
177 TString pho(name);
178 ped.Prepend("MPedestal");
179 pho.Prepend("MPedPhot");
180
181 fNamesPedestal.Add(new TNamed(ped, pho));
182}
183
184void MCalibrateData::AddPedestal(const char *pedestal, const char *pedphot)
185{
186 fNamesPedestal.Add(new TNamed(pedestal, pedphot));
187}
188
189// --------------------------------------------------------------------------
190//
191// The PreProcess searches for the following input containers:
192//
193// - MGeomCam
194// - MPedestalCam
195// - MCalibrationChargeCam
196// - MCalibrationQECam
197// - MExtractedSignalCam
198// - MBadPixelsCam
199//
200// The following output containers are also searched and created if
201// they were not found:
202//
203// - MPedPhotCam
204// - MSignalCam
205//
206Int_t MCalibrateData::PreProcess(MParList *pList)
207{
208 // input containers
209
210 fBadPixels = (MBadPixelsCam*)pList->FindObject(AddSerialNumber("MBadPixelsCam"));
211 if (!fBadPixels)
212 {
213 *fLog << err << AddSerialNumber("MBadPixelsCam") << " not found ... aborting" << endl;
214 return kFALSE;
215 }
216
217 fSignals = 0;
218 fCerPhotEvt = 0;
219 if (fCalibrationMode>kSkip)
220 {
221 fSignals = (MExtractedSignalCam*)pList->FindObject(AddSerialNumber("MExtractedSignalCam"));
222 if (!fSignals)
223 {
224 *fLog << err << AddSerialNumber("MExtractedSignalCam") << " not found ... aborting" << endl;
225 return kFALSE;
226 }
227
228 fCerPhotEvt = (MSignalCam*)pList->FindCreateObj(AddSerialNumber("MSignalCam"));
229 if (!fCerPhotEvt)
230 return kFALSE;
231
232 fIntensConst = (MCalibrationIntensityConstCam*)pList->FindObject(AddSerialNumber("MCalibrationIntensityConstCam"));
233 if (fIntensConst)
234 *fLog << inf << "Found MCalibrationIntensityConstCam ... " << endl;
235 else
236 {
237 fCalibConstCam = (MCalibConstCam*)pList->FindCreateObj(AddSerialNumber("MCalibConstCam"));
238 if (!fCalibConstCam)
239 return kFALSE;
240 }
241 }
242
243 fCalibrations = 0;
244 fQEs = 0;
245 if (fCalibrationMode>kNone)
246 {
247 fIntensCalib = (MCalibrationIntensityChargeCam*)pList->FindObject(AddSerialNumber("MCalibrationIntensityChargeCam"));
248 if (fIntensCalib)
249 *fLog << inf << "Found MCalibrationIntensityChargeCam ... " << endl;
250
251 fCalibrations = (MCalibrationChargeCam*)pList->FindObject(AddSerialNumber("MCalibrationChargeCam"));
252 if (!fCalibrations)
253 {
254 *fLog << err << AddSerialNumber("MCalibrationChargeCam") << " not found ... aborting." << endl;
255 return kFALSE;
256 }
257
258 fIntensQE = (MCalibrationIntensityQECam*)pList->FindObject(AddSerialNumber("MCalibrationIntensityQECam"));
259 if (fIntensQE)
260 *fLog << inf << "Found MCalibrationIntensityQECam ... " << endl;
261
262 fQEs = (MCalibrationQECam*)pList->FindObject(AddSerialNumber("MCalibrationQECam"));
263 if (!fQEs)
264 {
265 *fLog << err << AddSerialNumber("MCalibrationQECam") << " not found ... aborting." << endl;
266 return kFALSE;
267 }
268
269 }
270
271 if (fNamesPedestal.GetSize()>0 && fPedestalFlag==kNo)
272 {
273 *fLog << warn << "Pedestal list contains entries, but mode is set to kNo... setting to kEvent." << endl;
274 fPedestalFlag = kEvent;
275 }
276
277 if (fPedestalFlag!=kNo)
278 {
279 if (fNamesPedestal.GetSize()==0)
280 {
281 *fLog << inf << "No container names specified... using default: MPedestalCam and MPedPhotCam." << endl;
282 AddPedestal();
283 }
284
285 fPedestalCams.Clear();
286 fPedPhotCams.Clear();
287
288 TIter Next(&fNamesPedestal);
289 TObject *o=0;
290 while ((o=Next()))
291 {
292 TObject *pedcam = pList->FindObject(AddSerialNumber(o->GetName()), "MPedestalCam");
293 if (!pedcam)
294 {
295 *fLog << err << AddSerialNumber(o->GetName()) << " [MPedestalCam] not found ... aborting" << endl;
296 return kFALSE;
297 }
298 TObject *pedphot = pList->FindCreateObj("MPedPhotCam", AddSerialNumber(o->GetTitle()));
299 if (!pedphot)
300 return kFALSE;
301
302 fPedestalCams.Add(pedcam);
303 fPedPhotCams.Add(pedphot);
304 }
305 }
306
307 switch (fSignalType)
308 {
309 case kPhe:
310 //
311 // Average QE for C-photons, for pixels in the inner pixel region ("area 0"),
312 // used later to convert from C-photons into "equivalent phes":
313 //
314
315 // Average areas not yet initialized? use default value
316 fRenormFactor = MCalibrationQEPix::gkDefaultAverageQE;
317 if (fQEs->GetAverageAreas() > 0)
318 {
319 MCalibrationQEPix& avqepix = (MCalibrationQEPix&)(fQEs->GetAverageArea(0));
320 fRenormFactor = avqepix.GetAverageQE();
321 }
322 break;
323
324 case kPhot:
325 fRenormFactor = 1.;
326 break;
327 }
328
329 fCalibConsts.Reset();
330 fCalibFFactors.Reset();
331 fHiLoConv.Reset();
332 fHiLoConvErr.Reset();
333
334 return kTRUE;
335}
336
337// --------------------------------------------------------------------------
338//
339// The ReInit searches for the following input containers:
340//
341// - MGeomCam
342//
343// Check for validity of the selected calibration method, switch to a
344// different one in case of need
345//
346// Fill the MPedPhotCam container using the information from MPedestalCam,
347// MExtractedSignalCam and MCalibrationCam
348//
349Bool_t MCalibrateData::ReInit(MParList *pList)
350{
351 MRawRunHeader *header = (MRawRunHeader*)pList->FindObject("MRawRunHeader");
352 if (!header)
353 {
354 *fLog << err << "MRawRunHeader not found... abort." << endl;
355 return kFALSE;
356 }
357
358 fGeomCam = (MGeomCam*)pList->FindObject(AddSerialNumber("MGeomCam"));
359 if (!fGeomCam)
360 {
361 *fLog << err << "No MGeomCam found... aborting." << endl;
362 return kFALSE;
363 }
364
365 // Sizes might have changed
366 if (fPedestalFlag!=kNo)
367 {
368 TIter Next(&fPedestalCams);
369 MPedestalCam *cam=0;
370 while ((cam=(MPedestalCam*)Next()))
371 if ((Int_t)cam->GetSize() != fSignals->GetSize())
372 {
373 *fLog << err << "Size mismatch of " << cam->GetDescriptor() << " and MCalibrationCam... abort." << endl;
374 return kFALSE;
375 }
376 }
377
378 const MCalibrationQECam *qecam = fIntensQE ? (MCalibrationQECam*)fIntensQE->GetCam() : fQEs;
379 if(fCalibrationMode == kBlindPixel && !qecam->IsBlindPixelMethodValid())
380 {
381 *fLog << warn << "Blind pixel calibration method not valid, switching to F-factor method" << endl;
382 fCalibrationMode = kFfactor;
383 }
384
385 if(fCalibrationMode == kPinDiode && !qecam->IsPINDiodeMethodValid())
386 {
387 *fLog << warn << "PIN diode calibration method not valid, switching to F-factor method" << endl;
388 fCalibrationMode = kFfactor;
389 }
390
391 if(fCalibrationMode == kCombined && !qecam->IsCombinedMethodValid())
392 {
393 *fLog << warn << "Combined calibration method not valid, switching to F-factor method" << endl;
394 fCalibrationMode = kFfactor;
395 }
396
397 //
398 // output information or warnings:
399 //
400 switch(fCalibrationMode)
401 {
402 case kBlindPixel:
403 break;
404 case kFfactor:
405 break;
406 case kPinDiode:
407 *fLog << err << "PIN Diode Calibration mode not yet available!" << endl;
408 return kFALSE;
409 break;
410 case kCombined:
411 *fLog << err << "Combined Calibration mode not yet available!" << endl;
412 return kFALSE;
413 break;
414 case kFlatCharge:
415 *fLog << warn << "WARNING - Flat-fielding charges - only for muon calibration!" << endl;
416 break;
417 case kDummy:
418 *fLog << warn << "WARNING - Dummy calibration, no calibration applied!" << endl;
419 break;
420 case kNone:
421 *fLog << warn << "WARNING - No calibration applied!" << endl;
422 break;
423 default:
424 *fLog << warn << "WARNING - Calibration mode value (" << fCalibrationMode << ") not known" << endl;
425 return kFALSE;
426 }
427
428 //
429 // output information or warnings:
430 //
431 switch (fSignalType)
432 {
433 case kPhe:
434 *fLog << inf << "Calibrating in units of equivalent (outer/inner=4) photo-electrons." << endl;
435 break;
436 case kPhot:
437 *fLog << inf << "Calibrating in units of photons." << endl;
438 break;
439 }
440
441 if (header->IsMonteCarloRun())
442 {
443 *fLog << inf << "Additional scale factor forced to: 1 (MonteCarloRun)" << endl;
444 fScaleFactor = 1;
445 }
446 else
447 {
448 if (!fFileNameScale.IsNull())
449 {
450 if (gSystem->AccessPathName(fFileNameScale, kFileExists))
451 {
452 *fLog << err << "ERROR - Configuration file '" << fFileNameScale << "' doesn't exist... abort." << endl;
453 return kFALSE;
454 }
455
456 const Int_t p = header->GetRunStart().GetMagicPeriod();
457 const TString per = Form("%2d", p);
458
459 TEnv rc(fFileNameScale);
460 const Double_t scale = rc.GetValue(per, -1.);
461
462 if (scale<=0)
463 {
464 *fLog << err << "ERROR - No valid entry for scale factor found for period " << p << " in " << fFileNameScale << "... abort." << endl;
465 return kFALSE;
466 }
467
468 *fLog << inf << "New additional scale factor for period " << p << ": " << scale << endl;
469 fScaleFactor = scale;
470 }
471 else
472 *fLog << inf << "Additional scale factor set to: " << fScaleFactor << endl;
473 }
474
475 const Int_t npixels = fGeomCam->GetNumPixels();
476
477 if (fCalibrationMode > kNone)
478 {
479
480 const MCalibrationCam *chargecam = fIntensCalib ? fIntensCalib->GetCam() : fCalibrations;
481 if (chargecam->GetSize() != npixels)
482 {
483 *fLog << "Size mismatch between MGeomCam and MCalibrationChargeCam... abort!" << endl;
484 return kFALSE;
485 }
486
487 if (fBadPixels->GetSize() != npixels)
488 {
489 *fLog << "Size mismatch between MGeomCam and MBadPixelsCam... abort!" << endl;
490 return kFALSE;
491 }
492 }
493
494 fCalibConsts .Set(npixels);
495 fCalibFFactors.Set(npixels);
496 fHiLoConv .Set(npixels);
497 fHiLoConvErr .Set(npixels);
498
499 if (!UpdateConversionFactors())
500 return kFALSE;
501
502 if (TestPedestalFlag(kRun))
503 Calibrate(kFALSE, kTRUE);
504
505 return kTRUE;
506}
507
508// --------------------------------------------------------------------------
509//
510// Update the conversion factors and F-Factors from MCalibrationCams into
511// the arrays. Later, the here pre-calcualted conversion factors get simply
512// copied from memory.
513//
514// This function can be called from outside in case that the MCalibrationCams
515// have been updated...
516//
517Bool_t MCalibrateData::UpdateConversionFactors( const MCalibrationChargeCam *updatecam)
518{
519
520 *fLog << inf << GetDescriptor() << ": Updating Conversion Factors... " << endl;
521
522 fCalibConsts.Reset();
523 fCalibFFactors.Reset();
524 fHiLoConv.Reset();
525 fHiLoConvErr.Reset();
526
527 MCalibConstCam *constcam = fIntensConst ? fIntensConst->GetCam() : fCalibConstCam;
528
529 MCalibrationChargeCam *chargecam = NULL;
530 MCalibrationQECam *qecam = NULL;
531 if (updatecam)
532 {
533 chargecam = fCalibrations;
534 qecam = fQEs;
535 }
536 else
537 {
538 chargecam = fIntensCalib ? (MCalibrationChargeCam*)fIntensCalib->GetCam() : fCalibrations;
539 qecam = fIntensQE ? (MCalibrationQECam*) fIntensQE->GetCam() : fQEs;
540 }
541
542 //
543 // For the moment, we use only a dummy zenith for the calibration:
544 //
545 UInt_t skip = 0;
546
547 for (UInt_t pixidx=0; pixidx<fGeomCam->GetNumPixels(); pixidx++)
548 {
549
550 Float_t hiloconv = 1.;
551 Float_t hiloconverr = 0.;
552 Float_t calibConv = 1.;
553 Float_t calibConvVar = 0.;
554 Float_t calibFFactor = 0.;
555
556 Float_t calibQE = 1.;
557 Float_t calibQEVar = 0.;
558
559 Float_t calibUpdate = 1.;
560
561 MCalibConstPix &cpix = (*constcam)[pixidx];
562
563 if(fCalibrationMode!=kNone)
564 {
565 if ((*fBadPixels)[pixidx].IsUnsuitable(MBadPixelsPix::kUnsuitableRun))
566 {
567 skip++;
568 continue; // calibConv will remain 0
569 }
570
571 const MCalibrationChargePix &pix = (MCalibrationChargePix&)(*chargecam)[pixidx];
572 const MCalibrationChargePix &avpix = (MCalibrationChargePix&)chargecam->GetAverageArea(0);
573
574 hiloconv = pix.GetConversionHiLo ();
575 hiloconverr= pix.GetConversionHiLoSigma();
576
577 calibConv = pix.GetMeanConvFADC2Phe();
578 calibConvVar = pix.GetMeanConvFADC2PheVar();
579 calibFFactor = pix.GetMeanFFactorFADC2Phot();
580
581 const MCalibrationQEPix &qe = (MCalibrationQEPix&)(*qecam)[pixidx];
582
583 if (updatecam)
584 {
585 const MCalibrationChargePix &upix = (MCalibrationChargePix&)(*updatecam)[pixidx];
586
587 //
588 // Correct for the possible change in amplification of the individual pixels chain
589 //
590 const Float_t pixmean = upix.GetConvertedMean();
591 calibUpdate = pixmean==0 ? 1 : pix.GetConvertedMean()/pixmean;
592
593 //
594 // Correct for global shifts in light emission
595 //
596 const MCalibrationChargePix &ugpix = (MCalibrationChargePix&)updatecam->GetAverageArea(0);
597
598 const Float_t globmean = avpix.GetConvertedMean();
599 calibUpdate = globmean==0 ? 1 : ugpix.GetConvertedMean()/globmean;
600
601 MBadPixelsPix &ubad = (MBadPixelsPix&)updatecam->GetAverageBadArea(0);
602 if (ubad.IsUncalibrated(MBadPixelsPix::kChargeIsPedestal))
603 {
604 *fLog << warn << GetDescriptor() << ": Mean charge in inner pixels is smaller than 3 ped. RMS." << endl;
605 *fLog << "Maybe calibration pulses have been lost!" << endl;
606 calibUpdate = 1.;
607
608 }
609 }
610
611 switch(fCalibrationMode)
612 {
613 case kFlatCharge:
614 {
615 calibConv = avpix.GetConvertedMean()
616 / (pix.GetConvertedMean() * fGeomCam->GetPixRatio(pixidx));
617 calibConvVar = (avpix.GetMeanRelVar() + pix.GetMeanRelVar()) * calibConv * calibConv;
618 if (pix.IsFFactorMethodValid())
619 {
620 const Float_t convmin1 = qe.GetQECascadesFFactor()/pix.GetMeanConvFADC2Phe();
621 if (convmin1 > 0)
622 calibFFactor *= TMath::Sqrt(convmin1);
623 else
624 calibFFactor = -1.;
625 }
626 break;
627 }
628 case kBlindPixel:
629 if (!qe.IsBlindPixelMethodValid())
630 {
631 skip++;
632 continue;
633 }
634 calibQE = qe.GetQECascadesBlindPixel();
635 calibQEVar = qe.GetQECascadesBlindPixelVar();
636 break;
637
638 case kPinDiode:
639 if (!qe.IsPINDiodeMethodValid())
640 {
641 skip++;
642 continue;
643 }
644 calibQE = qe.GetQECascadesPINDiode();
645 calibQEVar = qe.GetQECascadesPINDiodeVar();
646 break;
647
648 case kFfactor:
649 if (!pix.IsFFactorMethodValid())
650 {
651 skip++;
652 continue;
653 }
654 calibQE = qe.GetQECascadesFFactor();
655 calibQEVar = qe.GetQECascadesFFactorVar();
656 break;
657
658 case kCombined:
659 if (!qe.IsCombinedMethodValid())
660 {
661 skip++;
662 continue;
663 }
664 calibQE = qe.GetQECascadesCombined();
665 calibQEVar = qe.GetQECascadesCombinedVar();
666 break;
667
668 case kDummy:
669 hiloconv = 1.;
670 hiloconverr = 0.;
671 calibUpdate = 1.;
672 break;
673
674 default:
675 break;
676 } /* switch calibration mode */
677 } /* if(fCalibrationMode!=kNone) */
678 else
679 {
680 calibConv = 1./fGeomCam->GetPixRatio(pixidx);
681 }
682
683 calibConv /= calibQE;
684
685 if (calibConv > 0.00001 && calibQE > 0.00001)
686 {
687 calibConvVar = calibConvVar/(calibConv*calibConv) + calibQEVar/(calibQE*calibQE);
688 calibConvVar *= (calibConv*calibConv);
689 }
690
691 calibConv *= fRenormFactor*fScaleFactor * calibUpdate;
692 calibFFactor *= TMath::Sqrt(fRenormFactor*fScaleFactor);
693
694 fHiLoConv [pixidx] = hiloconv;
695 fHiLoConvErr [pixidx] = hiloconverr;
696 fCalibConsts [pixidx] = calibConv;
697 fCalibFFactors[pixidx] = calibFFactor;
698
699 if (calibConv < fCalibConvMinLimit || calibConv > fCalibConvMaxLimit)
700 {
701 (*fBadPixels)[pixidx].SetUnsuitable(MBadPixelsPix::kUnsuitableRun);
702 calibConv = -1.;
703 calibFFactor = -1.;
704 *fLog << warn << GetDescriptor()
705 << ": WARNING - Conversion factor of Pixel " << pixidx << " out of range... set to 0. " << endl;
706 }
707 cpix.SetCalibConst(calibConv);
708 cpix.SetCalibFFactor(calibFFactor);
709
710 } /* for (Int_t pixidx=0; pixidx<fGeomCam->GetNumPixels(); pixidx++) */
711
712 if (skip>fGeomCam->GetNumPixels()*0.9)
713 {
714 *fLog << warn << GetDescriptor()
715 << ": WARNING - GetConversionFactor has skipped more than 90% of the pixels... abort." << endl;
716 return kFALSE;
717 }
718
719 return kTRUE;
720}
721
722
723// --------------------------------------------------------------------------
724//
725// Apply the conversion factors and F-Factors from the arrays to the data.
726//
727// The flags 'data' and 'pedestal' decide whether the signal and/or the pedetals
728// shall be calibrated, respectively.
729//
730Int_t MCalibrateData::Calibrate(Bool_t data, Bool_t pedestal) const
731{
732 if (!data && !pedestal)
733 return kTRUE;
734
735 const UInt_t npix = fSignals->GetSize();
736 const Float_t slices = fSignals->GetNumUsedHiGainFADCSlices();
737 const Float_t sqrtslices = TMath::Sqrt(slices);
738
739 Int_t numsatlo=0;
740 Int_t numsathi=0;
741
742 for (UInt_t pixidx=0; pixidx<npix; pixidx++)
743 {
744 MBadPixelsPix &bpix = (*fBadPixels)[pixidx];
745
746 if (data)
747 {
748 const MExtractedSignalPix &sig = (*fSignals)[pixidx];
749
750 Float_t signal = 0.;
751 Float_t signalErr = 0.;
752 Bool_t ok = kFALSE;
753
754 // If hi-gain is not saturated and has successfully been
755 // extracted use the hi-gain arrival time
756 if (!sig.IsHiGainSaturated() && sig.IsHiGainValid())
757 {
758 signal = sig.GetExtractedSignalHiGain();
759 ok = kTRUE;
760 }
761 else
762 {
763 // Use lo-gain if it can be used
764 if (sig.IsLoGainValid() && fHiLoConv[pixidx]>0.5)
765 {
766 signal = sig.GetExtractedSignalLoGain()*fHiLoConv[pixidx];
767 signalErr = sig.GetExtractedSignalLoGain()*fHiLoConvErr[pixidx];
768 ok = kTRUE;
769 }
770 }
771
772 // In this cases the signal extraction has failed.
773 if (!ok)
774 bpix.SetUnsuitable(MBadPixelsPix::kUnsuitableEvt);
775
776 const Float_t nphot = signal * fCalibConsts [pixidx];
777 const Float_t nphotErr = TMath::Sqrt(TMath::Abs(nphot)) * fCalibFFactors[pixidx];
778
779 fCerPhotEvt->AddPixel(pixidx, nphot, nphotErr);
780
781 if (!bpix.IsUnsuitable())
782 {
783 if (sig.IsHiGainSaturated())
784 numsathi++;
785
786 if (sig.IsLoGainSaturated())
787 numsatlo++;
788 }
789 } /* if (data) */
790
791
792 if (pedestal)
793 {
794 TIter NextPed(&fPedestalCams);
795 TIter NextPhot(&fPedPhotCams);
796
797 MPedestalCam *pedestal = 0;
798 MPedPhotCam *pedphot = 0;
799
800 const Float_t pedmeancalib = slices *fCalibConsts[pixidx];
801 const Float_t pedrmscalib = sqrtslices*fCalibConsts[pixidx];
802
803 while ((pedestal=(MPedestalCam*)NextPed()) &&
804 (pedphot =(MPedPhotCam*)NextPhot()))
805 {
806 // pedestals/(used FADC slices) in [number of photons]
807 const Float_t mean = (*pedestal)[pixidx].GetPedestal() *pedmeancalib;
808 const Float_t rms = (*pedestal)[pixidx].GetPedestalRms()*pedrmscalib;
809
810 (*pedphot)[pixidx].Set(mean, rms);
811 pedphot->SetReadyToSave();
812 }
813 } /* if (pedestal) */
814 }
815
816 if (data)
817 {
818 fCerPhotEvt->SetNumPixelsSaturated(numsathi, numsatlo);
819 fCerPhotEvt->SetReadyToSave();
820 }
821 return kTRUE;
822}
823
824// --------------------------------------------------------------------------
825//
826// Apply the calibration factors to the extracted signal according to the
827// selected calibration method
828//
829Int_t MCalibrateData::Process()
830{
831 return Calibrate(fCalibrationMode!=kSkip, TestPedestalFlag(kEvent));
832}
833
834// --------------------------------------------------------------------------
835//
836// Implementation of SavePrimitive. Used to write the call to a constructor
837// to a macro. In the original root implementation it is used to write
838// gui elements to a macro-file.
839//
840void MCalibrateData::StreamPrimitive(ostream &out) const
841{
842 out << " " << ClassName() << " " << GetUniqueName() << "(\"";
843 out << "\"" << fName << "\", \"" << fTitle << "\");" << endl;
844
845 if (TestPedestalFlag(kEvent))
846 out << " " << GetUniqueName() << ".EnablePedestalType(MCalibrateData::kEvent)" << endl;
847 if (TestPedestalFlag(kRun))
848 out << " " << GetUniqueName() << ".EnablePedestalType(MCalibrateData::kRun)" << endl;
849
850 if (fCalibrationMode != gkDefault)
851 {
852 out << " " << GetUniqueName() << ".SetCalibrationMode(MCalibrateData::";
853 switch (fCalibrationMode)
854 {
855 case kSkip: out << "kSkip"; break;
856 case kNone: out << "kNone"; break;
857 case kFlatCharge: out << "kFlatCharge"; break;
858 case kBlindPixel: out << "kBlindPixel"; break;
859 case kFfactor: out << "kFfactor"; break;
860 case kPinDiode: out << "kPinDiode"; break;
861 case kCombined: out << "kCombined"; break;
862 case kDummy: out << "kDummy"; break;
863 default: out << (int)fCalibrationMode; break;
864 }
865 out << ")" << endl;
866 }
867
868 TIter Next(&fNamesPedestal);
869 TObject *o=0;
870 while ((o=Next()))
871 {
872 out << " " << GetUniqueName() << ".AddPedestal(\"";
873 out << o->GetName() << "\", \"" << o->GetTitle() << "\");" << endl;
874 }
875}
876
877// --------------------------------------------------------------------------
878//
879// Read the setup from a TEnv, eg:
880// MJPedestal.MCalibrateDate.PedestalFlag: no,run,event
881// MJPedestal.MCalibrateDate.CalibrationMode: skip,none,flatcharge,blindpixel,ffactor,pindiode,combined,dummy,default
882//
883Int_t MCalibrateData::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
884{
885 Bool_t rc = kFALSE;
886 if (IsEnvDefined(env, prefix, "PedestalFlag", print))
887 {
888 rc = kTRUE;
889 TString s = GetEnvValue(env, prefix, "PedestalFlag", "");
890 s.ToLower();
891 if (s.BeginsWith("no"))
892 SetPedestalFlag(kNo);
893 if (s.BeginsWith("run"))
894 SetPedestalFlag(kRun);
895 if (s.BeginsWith("event"))
896 SetPedestalFlag(kEvent);
897 }
898
899 if (IsEnvDefined(env, prefix, "CalibrationMode", print))
900 {
901 rc = kTRUE;
902 TString s = GetEnvValue(env, prefix, "CalibrationMode", "");
903 s.ToLower();
904 if (s.BeginsWith("skip"))
905 SetCalibrationMode(kSkip);
906 if (s.BeginsWith("none"))
907 SetCalibrationMode(kNone);
908 if (s.BeginsWith("flatcharge"))
909 SetCalibrationMode(kFlatCharge);
910 if (s.BeginsWith("blindpixel"))
911 SetCalibrationMode(kBlindPixel);
912 if (s.BeginsWith("ffactor"))
913 SetCalibrationMode(kFfactor);
914 if (s.BeginsWith("pindiode"))
915 SetCalibrationMode(kPinDiode);
916 if (s.BeginsWith("combined"))
917 SetCalibrationMode(kCombined);
918 if (s.BeginsWith("dummy"))
919 SetCalibrationMode(kDummy);
920 if (s.BeginsWith("default"))
921 SetCalibrationMode();
922 }
923
924 if (IsEnvDefined(env, prefix, "SignalType", print))
925 {
926 rc = kTRUE;
927 TString s = GetEnvValue(env, prefix, "SignalType", "");
928 s.ToLower();
929 if (s.BeginsWith("phot"))
930 SetSignalType(kPhot);
931 if (s.BeginsWith("phe"))
932 SetSignalType(kPhe);
933 if (s.BeginsWith("default"))
934 SetSignalType();
935 }
936
937 if (IsEnvDefined(env, prefix, "CalibConvMinLimit", print))
938 {
939 fCalibConvMinLimit = GetEnvValue(env, prefix, "CalibConvMinLimit", fCalibConvMinLimit);
940 rc = kTRUE;
941 }
942
943 if (IsEnvDefined(env, prefix, "CalibConvMaxLimit", print))
944 {
945 fCalibConvMaxLimit = GetEnvValue(env, prefix, "CalibConvMaxLimit", fCalibConvMaxLimit);
946 rc = kTRUE;
947 }
948
949 if (IsEnvDefined(env, prefix, "ScaleFactor", print))
950 {
951 fScaleFactor = GetEnvValue(env, prefix, "ScaleFactor", fScaleFactor);
952 rc = kTRUE;
953 }
954
955 if (IsEnvDefined(env, prefix, "FileNameScale", print))
956 {
957 fFileNameScale = GetEnvValue(env, prefix, "FileNameScale", fFileNameScale);
958 rc = kTRUE;
959 }
960
961 return rc;
962}
963
964void MCalibrateData::Print(Option_t *o) const
965{
966
967 *fLog << all << GetDescriptor() << ":" << endl;
968
969 for (UInt_t pixidx=0; pixidx<fGeomCam->GetNumPixels(); pixidx++)
970 {
971 *fLog << all
972 << "Pixel: " << Form("%3i",pixidx)
973 << " CalibConst: " << Form("%4.2f",fCalibConsts[pixidx])
974 << " F-Factor: " << Form("%4.2f",fCalibFFactors[pixidx])
975 << " HiLoConv: " << Form("%4.2f",fHiLoConv[pixidx])
976 << endl;
977 }
978}
979
Note: See TracBrowser for help on using the repository browser.