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

Last change on this file since 4752 was 4752, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 21.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 MCerPhotEvt.
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 SetNamePedADCContainer() and/or SetNamePedPhotContainer() you
69// can control the name of the 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.SetNamePedADCContainer("MPedestalFromLoGain");
79// cal2.SetNamePedPhotContainer("MPedPhotFromLoGain")
80// cal2.SetPedestalFlag(MCalibrateData::kEvent);
81//
82//
83// Input Containers:
84// [MPedestalCam]
85// [MExtractedSignalCam]
86// [MCalibrationChargeCam]
87// [MCalibrationQECam]
88// MBadPixelsCam
89//
90// Output Containers:
91// [MPedPhotCam]
92// [MCerPhotEvt]
93//
94// See also: MJCalibration, MJPedestal, MJExtractSignal, MJExtractCalibTest
95//
96//////////////////////////////////////////////////////////////////////////////
97#include "MCalibrateData.h"
98
99#include <fstream>
100
101#include <TEnv.h>
102
103#include "MLog.h"
104#include "MLogManip.h"
105
106#include "MParList.h"
107#include "MH.h"
108
109#include "MGeomCam.h"
110
111#include "MPedestalCam.h"
112#include "MPedestalPix.h"
113
114#include "MCalibrationChargeCam.h"
115#include "MCalibrationChargePix.h"
116
117#include "MCalibrationQECam.h"
118#include "MCalibrationQEPix.h"
119
120#include "MExtractedSignalCam.h"
121#include "MExtractedSignalPix.h"
122
123#include "MPedPhotCam.h"
124#include "MPedPhotPix.h"
125
126#include "MBadPixelsCam.h"
127#include "MBadPixelsPix.h"
128
129#include "MCerPhotEvt.h"
130
131ClassImp(MCalibrateData);
132
133using namespace std;
134
135const TString MCalibrateData::fgNamePedADCContainer = "MPedestalCam";
136const TString MCalibrateData::fgNamePedPhotContainer = "MPedPhotCam";
137// --------------------------------------------------------------------------
138//
139// Default constructor.
140//
141// Sets all pointers to NULL
142//
143// Initializes:
144// - fCalibrationMode to kDefault
145// - fPedestalFlag to kRun
146// - fNamePedADCRunContainer to "MPedestalCam"
147// - fNamePedPhotRunContainer to "MPedPhotCam"
148//
149MCalibrateData::MCalibrateData(CalibrationMode_t calmode,const char *name, const char *title)
150 : fGeomCam(NULL), fPedestal(NULL),
151 fBadPixels(NULL), fCalibrations(NULL), fQEs(NULL), fSignals(NULL),
152 fPedPhot(NULL), fCerPhotEvt(NULL), fPedestalFlag(kNo)
153{
154
155 fName = name ? name : "MCalibrateData";
156 fTitle = title ? title : "Task to calculate the number of photons in one event";
157
158 SetCalibrationMode();
159
160 SetNamePedADCContainer();
161 SetNamePedPhotContainer();
162}
163
164// --------------------------------------------------------------------------
165//
166// The PreProcess searches for the following input containers:
167//
168// - MGeomCam
169// - MPedestalCam
170// - MCalibrationChargeCam
171// - MCalibrationQECam
172// - MExtractedSignalCam
173// - MBadPixelsCam
174//
175// The following output containers are also searched and created if
176// they were not found:
177//
178// - MPedPhotCam
179// - MCerPhotEvt
180//
181Int_t MCalibrateData::PreProcess(MParList *pList)
182{
183 // input containers
184
185 fBadPixels = (MBadPixelsCam*)pList->FindObject(AddSerialNumber("MBadPixelsCam"));
186 if (!fBadPixels)
187 {
188 *fLog << err << AddSerialNumber("MBadPixelsCam") << " not found ... aborting" << endl;
189 return kFALSE;
190 }
191
192 fSignals = 0;
193 fCerPhotEvt = 0;
194 if (fCalibrationMode>kSkip)
195 {
196 fSignals = (MExtractedSignalCam*)pList->FindObject(AddSerialNumber("MExtractedSignalCam"));
197 if (!fSignals)
198 {
199 *fLog << err << AddSerialNumber("MExtractedSignalCam") << " not found ... aborting" << endl;
200 return kFALSE;
201 }
202
203 fCerPhotEvt = (MCerPhotEvt*)pList->FindCreateObj(AddSerialNumber("MCerPhotEvt"));
204 if (!fCerPhotEvt)
205 return kFALSE;
206 }
207
208 fCalibrations = 0;
209 fQEs = 0;
210 if (fCalibrationMode>kNone)
211 {
212 fCalibrations = (MCalibrationChargeCam*)pList->FindObject(AddSerialNumber("MCalibrationChargeCam"));
213 if (!fCalibrations)
214 {
215 *fLog << err << AddSerialNumber("MCalibrationChargeCam") << " not found ... aborting." << endl;
216 return kFALSE;
217 }
218
219 fQEs = (MCalibrationQECam*)pList->FindObject(AddSerialNumber("MCalibrationQECam"));
220 if (!fQEs)
221 {
222 *fLog << err << AddSerialNumber("MCalibrationQECam") << " not found ... aborting." << endl;
223 return kFALSE;
224 }
225 }
226
227 fPedestal = 0;
228 fPedPhot = 0;
229 if (fPedestalFlag)
230 {
231 fPedestal = (MPedestalCam*)pList->FindObject(AddSerialNumber(fNamePedADCContainer), "MPedestalCam");
232 if (!fPedestal)
233 {
234 *fLog << err << AddSerialNumber(fNamePedADCContainer) << " [MPedestalCam] not found ... aborting" << endl;
235 return kFALSE;
236 }
237
238 fPedPhot = (MPedPhotCam*)pList->FindCreateObj("MPedPhotCam", AddSerialNumber(fNamePedPhotContainer));
239 if (!fPedPhot)
240 return kFALSE;
241 }
242
243 return kTRUE;
244}
245
246// --------------------------------------------------------------------------
247//
248// The ReInit searches for the following input containers:
249//
250// - MGeomCam
251//
252// Check for validity of the selected calibration method, switch to a
253// different one in case of need
254//
255// Fill the MPedPhotCam container using the information from MPedestalCam,
256// MExtractedSignalCam and MCalibrationCam
257//
258Bool_t MCalibrateData::ReInit(MParList *pList)
259{
260 fGeomCam = (MGeomCam*)pList->FindObject(AddSerialNumber("MGeomCam"));
261 if (!fGeomCam)
262 {
263 *fLog << err << "No MGeomCam found... aborting." << endl;
264 return kFALSE;
265 }
266
267 // Sizes might have changed
268 if (fPedestalFlag && (Int_t)fPedestal->GetSize() != fSignals->GetSize())
269 {
270 *fLog << err << "Size mismatch of MPedestalCam and MCalibrationCam... abort." << endl;
271 return kFALSE;
272 }
273
274 if(fCalibrationMode == kBlindPixel && !fQEs->IsBlindPixelMethodValid())
275 {
276 *fLog << warn << "Blind pixel calibration method not valid, switching to F-factor method" << endl;
277 fCalibrationMode = kFfactor;
278 }
279
280 if(fCalibrationMode == kPinDiode && !fQEs->IsPINDiodeMethodValid())
281 {
282 *fLog << warn << "PIN diode calibration method not valid, switching to F-factor method" << endl;
283 fCalibrationMode = kFfactor;
284 }
285
286 if(fCalibrationMode == kCombined && !fQEs->IsCombinedMethodValid())
287 {
288 *fLog << warn << "Combined calibration method not valid, switching to F-factor method" << endl;
289 fCalibrationMode = kFfactor;
290 }
291
292 //
293 // output information or warnings:
294 //
295 switch(fCalibrationMode)
296 {
297 case kBlindPixel:
298 break;
299 case kFfactor:
300 break;
301 case kPinDiode:
302 *fLog << err << "PIN Diode Calibration mode not yet available!" << endl;
303 return kFALSE;
304 break;
305 case kCombined:
306 *fLog << err << "Combined Calibration mode not yet available!" << endl;
307 return kFALSE;
308 break;
309 case kFlatCharge:
310 *fLog << warn << "WARNING - Flat-fielding charges - only for muon calibration!" << endl;
311 break;
312 case kDummy:
313 *fLog << warn << "WARNING - Dummy calibration, no calibration applied!" << endl;
314 break;
315 case kNone:
316 *fLog << warn << "WARNING - No calibration applied!" << endl;
317 break;
318 default:
319 *fLog << warn << "WARNING - Calibration mode value (" << fCalibrationMode << ") not known" << endl;
320 return kFALSE;
321 }
322
323 if (TestPedestalFlag(kRun))
324 Calibrate(kFALSE, kTRUE);
325
326 return kTRUE;
327}
328
329// --------------------------------------------------------------------------
330//
331// Get conversion factor and its error from MCalibrationCam
332//
333Bool_t MCalibrateData::GetConversionFactor(UInt_t pixidx, Float_t &hiloconv, Float_t &hiloconverr,
334 Float_t &calibConv, Float_t &calibConvVar,
335 Float_t &calibFFactor) const
336{
337 //
338 // For the moment, we use only a dummy zenith for the calibration:
339 //
340 const Float_t zenith = -1.;
341
342 hiloconv = 1.;
343 hiloconverr = 0.;
344 calibConv = 1.;
345 calibConvVar = 0.;
346 calibFFactor = 0.;
347
348 Float_t calibQE = 1.;
349 Float_t calibQEVar = 0.;
350
351 if(fCalibrationMode!=kNone)
352 {
353 MCalibrationChargePix &pix = (MCalibrationChargePix&)(*fCalibrations)[pixidx];
354
355 hiloconv = pix.GetConversionHiLo ();
356 hiloconverr= pix.GetConversionHiLoErr();
357
358 if (fBadPixels)
359 {
360 MBadPixelsPix &bad = (*fBadPixels)[pixidx];
361 if (bad.IsUnsuitable())
362 return kFALSE;
363 }
364
365 calibConv = pix.GetMeanConvFADC2Phe();
366 calibConvVar = pix.GetMeanConvFADC2PheVar();
367 calibFFactor = pix.GetMeanFFactorFADC2Phot();
368
369 MCalibrationQEPix &qe = (MCalibrationQEPix&) (*fQEs)[pixidx];
370
371 switch(fCalibrationMode)
372 {
373 case kFlatCharge:
374 {
375 MCalibrationChargePix &avpix = (MCalibrationChargePix&)fCalibrations->GetAverageArea(0);
376 calibConv = avpix.GetMean() / (pix.GetMean() * fGeomCam->GetPixRatio(pixidx));
377 calibConvVar = (avpix.GetMeanRelVar() + pix.GetMeanRelVar()) * calibConv * calibConv;
378 if (pix.IsFFactorMethodValid())
379 {
380 const Float_t convmin1 = qe.GetQECascadesFFactor(zenith)/pix.GetMeanConvFADC2Phe();
381 if (convmin1 > 0)
382 calibFFactor *= TMath::Sqrt(convmin1);
383 else
384 calibFFactor = -1.;
385 }
386 }
387 break;
388
389 case kBlindPixel:
390 if (!qe.IsBlindPixelMethodValid())
391 return kFALSE;
392 calibQE = qe.GetQECascadesBlindPixel ( zenith );
393 calibQEVar = qe.GetQECascadesBlindPixelVar( zenith );
394 break;
395
396 case kPinDiode:
397 if (!qe.IsPINDiodeMethodValid())
398 return kFALSE;
399 calibQE = qe.GetQECascadesPINDiode ( zenith );
400 calibQEVar = qe.GetQECascadesPINDiodeVar( zenith );
401 break;
402
403 case kFfactor:
404 if (!pix.IsFFactorMethodValid())
405 return kFALSE;
406 calibQE = qe.GetQECascadesFFactor ( zenith );
407 calibQEVar = qe.GetQECascadesFFactorVar( zenith );
408 break;
409
410 case kCombined:
411 if (!qe.IsCombinedMethodValid())
412 return kFALSE;
413 calibQE = qe.GetQECascadesCombined ( zenith );
414 calibQEVar = qe.GetQECascadesCombinedVar( zenith );
415 break;
416
417 case kDummy:
418 hiloconv = 1.;
419 hiloconverr = 0.;
420 break;
421 } /* switch calibration mode */
422 } /* if(fCalibrationMode!=kNone) */
423 else
424 {
425 calibConv = 1./fGeomCam->GetPixRatio(pixidx);
426 }
427
428 calibConv /= calibQE;
429
430 if (calibConv != 0. && calibQE != 0.)
431 {
432 const Float_t calibConv2 = calibConv*calibConv;
433 calibConvVar = calibConvVar/calibConv2 + calibQEVar/(calibQE*calibQE);
434 calibConvVar *= calibConv2;
435 }
436
437 return kTRUE;
438}
439
440void MCalibrateData::Calibrate(Bool_t data, Bool_t pedestal) const
441{
442 if (!data && !pedestal)
443 return;
444
445 const UInt_t npix = fSignals->GetSize();
446 const Float_t slices = fSignals->GetNumUsedHiGainFADCSlices();
447 const Float_t sqrtslices = TMath::Sqrt(slices);
448
449 Float_t hiloconv;
450 Float_t hiloconverr;
451 Float_t calibConv;
452 Float_t calibConvErr;
453 Float_t calibFFactor;
454
455 for (UInt_t pixidx=0; pixidx<npix; pixidx++)
456 {
457 if (!GetConversionFactor(pixidx, hiloconv, hiloconverr,
458 calibConv, calibConvErr, calibFFactor))
459 continue;
460
461 if (data)
462 {
463 const MExtractedSignalPix &sig = (*fSignals)[pixidx];
464
465 Float_t signal = 0;
466 Float_t signalErr = 0.;
467
468 if (sig.IsLoGainUsed())
469 {
470 signal = sig.GetExtractedSignalLoGain()*hiloconv;
471 signalErr = signal*hiloconverr;
472 }
473 else
474 {
475 if (sig.GetExtractedSignalHiGain() <= 9999.)
476 signal = sig.GetExtractedSignalHiGain();
477 }
478
479 const Float_t nphot = signal*calibConv;
480 const Float_t nphotErr = calibFFactor*TMath::Sqrt(TMath::Abs(nphot));
481
482 //
483 // The following part is the commented first version of the error calculation
484 // Contact Markus Gaug for questions (or wait for the next documentation update...)
485 //
486 /*
487 nphotErr = signal > 0 ? signalErr*signalErr / (signal * signal) : 0.
488 + calibConv > 0 ? calibConvVar / (calibConv * calibConv ) : 0.;
489 nphotErr = TMath::Sqrt(nphotErr) * nphot;
490 */
491
492 MCerPhotPix *cpix = fCerPhotEvt->AddPixel(pixidx, nphot, nphotErr);
493
494 if (sig.GetNumHiGainSaturated() > 0)
495 cpix->SetPixelHGSaturated();
496
497 if (sig.GetNumLoGainSaturated() > 0)
498 cpix->SetPixelSaturated();
499 }
500 if (pedestal)
501 {
502 const MPedestalPix &ped = (*fPedestal)[pixidx];
503
504 // pedestals/(used FADC slices) in [ADC] counts
505 const Float_t pedes = ped.GetPedestal() * slices;
506 const Float_t pedrms = ped.GetPedestalRms() * sqrtslices;
507
508 //
509 // pedestals/(used FADC slices) in [number of photons]
510 //
511 const Float_t pedphot = pedes * calibConv;
512 const Float_t pedphotrms = pedrms * calibConv;
513
514 (*fPedPhot)[pixidx].Set(pedphot, pedphotrms);
515 }
516 }
517
518 if (pedestal)
519 fPedPhot->SetReadyToSave();
520
521 if (data)
522 {
523 fCerPhotEvt->FixSize();
524 fCerPhotEvt->SetReadyToSave();
525 }
526}
527
528// --------------------------------------------------------------------------
529//
530// Apply the calibration factors to the extracted signal according to the
531// selected calibration method
532//
533Int_t MCalibrateData::Process()
534{
535 /*
536 if (fCalibrations->GetNumPixels() != (UInt_t)fSignals->GetSize())
537 {
538 // FIXME: MExtractedSignal must be of variable size -
539 // like MCerPhotEvt - because we must be able
540 // to reduce size by zero supression
541 // For the moment this check could be done in ReInit...
542 *fLog << err << "MExtractedSignal and MCalibrationCam have different sizes... abort." << endl;
543 return kFALSE;
544 }
545 */
546
547 Calibrate(fCalibrationMode!=kSkip, TestPedestalFlag(kEvent));
548 return kTRUE;
549}
550
551// --------------------------------------------------------------------------
552//
553// Implementation of SavePrimitive. Used to write the call to a constructor
554// to a macro. In the original root implementation it is used to write
555// gui elements to a macro-file.
556//
557void MCalibrateData::StreamPrimitive(ofstream &out) const
558{
559 out << " " << ClassName() << " " << GetUniqueName() << "(\"";
560 out << "\"" << fName << "\", \"" << fTitle << "\");" << endl;
561
562 if (TestPedestalFlag(kEvent))
563 out << " " << GetUniqueName() << ".EnablePedestalType(MCalibrateData::kEvent)" << endl;
564 if (TestPedestalFlag(kRun))
565 out << " " << GetUniqueName() << ".EnablePedestalType(MCalibrateData::kRun)" << endl;
566
567 if (fCalibrationMode != kDefault)
568 {
569 out << " " << GetUniqueName() << ".SetCalibrationMode(MCalibrateData::";
570 switch (fCalibrationMode)
571 {
572 case kSkip: out << "kSkip"; break;
573 case kNone: out << "kNone"; break;
574 case kFlatCharge: out << "kFlatCharge"; break;
575 case kBlindPixel: out << "kBlindPixel"; break;
576 case kFfactor: out << "kFfactor"; break;
577 case kPinDiode: out << "kPinDiode"; break;
578 case kCombined: out << "kCombined"; break;
579 case kDummy: out << "kDummy"; break;
580 default: out << (int)fCalibrationMode; break;
581 }
582 out << ")" << endl;
583 }
584
585 if (fNamePedADCContainer != fgNamePedADCContainer)
586 {
587 out << " " << GetUniqueName() << ".SetNamePedADCContainer(";
588 out << fNamePedADCContainer.Data() << ");" << endl;
589 }
590
591 if (fNamePedPhotContainer != fgNamePedPhotContainer)
592 {
593 out << " " << GetUniqueName() << ".SetNamePedPhotContainer(";
594 out << fNamePedPhotContainer.Data() << ");" << endl;
595 }
596}
597
598// --------------------------------------------------------------------------
599//
600// Read the setup from a TEnv, eg:
601// MJPedestal.MCalibrateDate.PedestalFlag: no,run,event
602// MJPedestal.MCalibrateDate.CalibrationMode: skip,none,flatcharge,blindpixel,ffactor,pindiode,combined,dummy,default
603//
604Int_t MCalibrateData::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
605{
606 Bool_t rc = kFALSE;
607 if (IsEnvDefined(env, prefix, "PedestalFlag", print))
608 {
609 rc = kTRUE;
610 TString s = GetEnvValue(env, prefix, "PedestalFlag", "");
611 s.ToLower();
612 if (s.BeginsWith("no"))
613 SetPedestalFlag(kNo);
614 if (s.BeginsWith("run"))
615 SetPedestalFlag(kRun);
616 if (s.BeginsWith("event"))
617 SetPedestalFlag(kEvent);
618 }
619
620 if (IsEnvDefined(env, prefix, "CalibrationMode", print))
621 {
622 rc = kTRUE;
623 TString s = GetEnvValue(env, prefix, "CalibrationMode", "");
624 s.ToLower();
625 if (s.BeginsWith("skip"))
626 SetCalibrationMode(kSkip);
627 if (s.BeginsWith("none"))
628 SetCalibrationMode(kNone);
629 if (s.BeginsWith("flatcharge"))
630 SetCalibrationMode(kFlatCharge);
631 if (s.BeginsWith("blindpixel"))
632 SetCalibrationMode(kBlindPixel);
633 if (s.BeginsWith("ffactor"))
634 SetCalibrationMode(kFfactor);
635 if (s.BeginsWith("pindiode"))
636 SetCalibrationMode(kPinDiode);
637 if (s.BeginsWith("combined"))
638 SetCalibrationMode(kCombined);
639 if (s.BeginsWith("dummy"))
640 SetCalibrationMode(kDummy);
641 if (s.BeginsWith("default"))
642 SetCalibrationMode();
643 }
644
645 return rc;
646}
Note: See TracBrowser for help on using the repository browser.