source: trunk/MagicSoft/Mars/mcalib/MCalibrateData.cc@ 7829

Last change on this file since 7829 was 7829, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 32.2 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
745 if (data)
746 {
747 const MExtractedSignalPix &sig = (*fSignals)[pixidx];
748
749 Float_t signal = 0.;
750 Float_t signalErr = 0.;
751
752 if (sig.IsLoGainUsed())
753 {
754 if (fHiLoConv[pixidx] < 0.5)
755 {
756 signal = sig.GetExtractedSignalHiGain()*1.5;
757 signalErr = sig.GetExtractedSignalHiGain()*0.5;
758 }
759 else
760 {
761 const Float_t siglo = sig.GetExtractedSignalLoGain();
762
763 if (siglo > 0.1) // low-gain signal has been extracted successfully
764 {
765 signal = siglo*fHiLoConv [pixidx];
766 signalErr = siglo*fHiLoConvErr[pixidx];
767 }
768 else // low-gain signal has not been extracted successfully, get a rough estimate from the high-gain
769 {
770 signal = sig.GetExtractedSignalHiGain()*1.5;
771 signalErr = sig.GetExtractedSignalHiGain()*0.5;
772 }
773 }
774 }
775 else
776 {
777 if (sig.GetExtractedSignalHiGain() <= 9999.)
778 signal = sig.GetExtractedSignalHiGain();
779 }
780
781 const Float_t nphot = signal * fCalibConsts [pixidx];
782 const Float_t nphotErr = TMath::Sqrt(TMath::Abs(nphot)) * fCalibFFactors[pixidx];
783
784 fCerPhotEvt->AddPixel(pixidx, nphot, nphotErr);
785
786 if (!(*fBadPixels)[pixidx].IsUnsuitable())
787 {
788 if (sig.GetNumHiGainSaturated() > 0)
789 numsathi++;
790
791 if (sig.GetNumLoGainSaturated() > 0)
792 numsatlo++;
793 }
794 } /* if (data) */
795
796
797 if (pedestal)
798 {
799 TIter NextPed(&fPedestalCams);
800 TIter NextPhot(&fPedPhotCams);
801
802 MPedestalCam *pedestal = 0;
803 MPedPhotCam *pedphot = 0;
804
805 const Float_t pedmeancalib = slices *fCalibConsts[pixidx];
806 const Float_t pedrmscalib = sqrtslices*fCalibConsts[pixidx];
807
808 while ((pedestal=(MPedestalCam*)NextPed()) &&
809 (pedphot =(MPedPhotCam*)NextPhot()))
810 {
811 // pedestals/(used FADC slices) in [number of photons]
812 const Float_t mean = (*pedestal)[pixidx].GetPedestal() *pedmeancalib;
813 const Float_t rms = (*pedestal)[pixidx].GetPedestalRms()*pedrmscalib;
814
815 (*pedphot)[pixidx].Set(mean, rms);
816 pedphot->SetReadyToSave();
817 }
818 } /* if (pedestal) */
819 }
820
821 if (data)
822 {
823 fCerPhotEvt->SetNumPixelsSaturated(numsathi, numsatlo);
824 fCerPhotEvt->SetReadyToSave();
825 }
826 return kTRUE;
827}
828
829// --------------------------------------------------------------------------
830//
831// Apply the calibration factors to the extracted signal according to the
832// selected calibration method
833//
834Int_t MCalibrateData::Process()
835{
836 return Calibrate(fCalibrationMode!=kSkip, TestPedestalFlag(kEvent));
837}
838
839// --------------------------------------------------------------------------
840//
841// Implementation of SavePrimitive. Used to write the call to a constructor
842// to a macro. In the original root implementation it is used to write
843// gui elements to a macro-file.
844//
845void MCalibrateData::StreamPrimitive(ostream &out) const
846{
847 out << " " << ClassName() << " " << GetUniqueName() << "(\"";
848 out << "\"" << fName << "\", \"" << fTitle << "\");" << endl;
849
850 if (TestPedestalFlag(kEvent))
851 out << " " << GetUniqueName() << ".EnablePedestalType(MCalibrateData::kEvent)" << endl;
852 if (TestPedestalFlag(kRun))
853 out << " " << GetUniqueName() << ".EnablePedestalType(MCalibrateData::kRun)" << endl;
854
855 if (fCalibrationMode != gkDefault)
856 {
857 out << " " << GetUniqueName() << ".SetCalibrationMode(MCalibrateData::";
858 switch (fCalibrationMode)
859 {
860 case kSkip: out << "kSkip"; break;
861 case kNone: out << "kNone"; break;
862 case kFlatCharge: out << "kFlatCharge"; break;
863 case kBlindPixel: out << "kBlindPixel"; break;
864 case kFfactor: out << "kFfactor"; break;
865 case kPinDiode: out << "kPinDiode"; break;
866 case kCombined: out << "kCombined"; break;
867 case kDummy: out << "kDummy"; break;
868 default: out << (int)fCalibrationMode; break;
869 }
870 out << ")" << endl;
871 }
872
873 TIter Next(&fNamesPedestal);
874 TObject *o=0;
875 while ((o=Next()))
876 {
877 out << " " << GetUniqueName() << ".AddPedestal(\"";
878 out << o->GetName() << "\", \"" << o->GetTitle() << "\");" << endl;
879 }
880}
881
882// --------------------------------------------------------------------------
883//
884// Read the setup from a TEnv, eg:
885// MJPedestal.MCalibrateDate.PedestalFlag: no,run,event
886// MJPedestal.MCalibrateDate.CalibrationMode: skip,none,flatcharge,blindpixel,ffactor,pindiode,combined,dummy,default
887//
888Int_t MCalibrateData::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
889{
890 Bool_t rc = kFALSE;
891 if (IsEnvDefined(env, prefix, "PedestalFlag", print))
892 {
893 rc = kTRUE;
894 TString s = GetEnvValue(env, prefix, "PedestalFlag", "");
895 s.ToLower();
896 if (s.BeginsWith("no"))
897 SetPedestalFlag(kNo);
898 if (s.BeginsWith("run"))
899 SetPedestalFlag(kRun);
900 if (s.BeginsWith("event"))
901 SetPedestalFlag(kEvent);
902 }
903
904 if (IsEnvDefined(env, prefix, "CalibrationMode", print))
905 {
906 rc = kTRUE;
907 TString s = GetEnvValue(env, prefix, "CalibrationMode", "");
908 s.ToLower();
909 if (s.BeginsWith("skip"))
910 SetCalibrationMode(kSkip);
911 if (s.BeginsWith("none"))
912 SetCalibrationMode(kNone);
913 if (s.BeginsWith("flatcharge"))
914 SetCalibrationMode(kFlatCharge);
915 if (s.BeginsWith("blindpixel"))
916 SetCalibrationMode(kBlindPixel);
917 if (s.BeginsWith("ffactor"))
918 SetCalibrationMode(kFfactor);
919 if (s.BeginsWith("pindiode"))
920 SetCalibrationMode(kPinDiode);
921 if (s.BeginsWith("combined"))
922 SetCalibrationMode(kCombined);
923 if (s.BeginsWith("dummy"))
924 SetCalibrationMode(kDummy);
925 if (s.BeginsWith("default"))
926 SetCalibrationMode();
927 }
928
929 if (IsEnvDefined(env, prefix, "SignalType", print))
930 {
931 rc = kTRUE;
932 TString s = GetEnvValue(env, prefix, "SignalType", "");
933 s.ToLower();
934 if (s.BeginsWith("phot"))
935 SetSignalType(kPhot);
936 if (s.BeginsWith("phe"))
937 SetSignalType(kPhe);
938 if (s.BeginsWith("default"))
939 SetSignalType();
940 }
941
942 if (IsEnvDefined(env, prefix, "CalibConvMinLimit", print))
943 {
944 fCalibConvMinLimit = GetEnvValue(env, prefix, "CalibConvMinLimit", fCalibConvMinLimit);
945 rc = kTRUE;
946 }
947
948 if (IsEnvDefined(env, prefix, "CalibConvMaxLimit", print))
949 {
950 fCalibConvMaxLimit = GetEnvValue(env, prefix, "CalibConvMaxLimit", fCalibConvMaxLimit);
951 rc = kTRUE;
952 }
953
954 if (IsEnvDefined(env, prefix, "ScaleFactor", print))
955 {
956 fScaleFactor = GetEnvValue(env, prefix, "ScaleFactor", fScaleFactor);
957 rc = kTRUE;
958 }
959
960 if (IsEnvDefined(env, prefix, "FileNameScale", print))
961 {
962 fFileNameScale = GetEnvValue(env, prefix, "FileNameScale", fFileNameScale);
963 rc = kTRUE;
964 }
965
966 return rc;
967}
968
969void MCalibrateData::Print(Option_t *o) const
970{
971
972 *fLog << all << GetDescriptor() << ":" << endl;
973
974 for (UInt_t pixidx=0; pixidx<fGeomCam->GetNumPixels(); pixidx++)
975 {
976 *fLog << all
977 << "Pixel: " << Form("%3i",pixidx)
978 << " CalibConst: " << Form("%4.2f",fCalibConsts[pixidx])
979 << " F-Factor: " << Form("%4.2f",fCalibFFactors[pixidx])
980 << " HiLoConv: " << Form("%4.2f",fHiLoConv[pixidx])
981 << endl;
982 }
983}
984
Note: See TracBrowser for help on using the repository browser.