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

Last change on this file since 4909 was 4766, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 21.3 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 // Now doing:
433 // calibConvVar = calibConvVar/(calibConv*calibConv) + calibQEVar/(calibQE*calibQE);
434 // calibConvVar *= (calibConv*calibConv);
435 calibConvVar += calibQEVar*(calibConv*calibConv)/(calibQE*calibQE);
436 }
437
438 return kTRUE;
439}
440
441void MCalibrateData::Calibrate(Bool_t data, Bool_t pedestal) const
442{
443 if (!data && !pedestal)
444 return;
445
446 const UInt_t npix = fSignals->GetSize();
447 const Float_t slices = fSignals->GetNumUsedHiGainFADCSlices();
448 const Float_t sqrtslices = TMath::Sqrt(slices);
449
450 Float_t hiloconv;
451 Float_t hiloconverr;
452 Float_t calibConv;
453 Float_t calibConvErr;
454 Float_t calibFFactor;
455
456 for (UInt_t pixidx=0; pixidx<npix; pixidx++)
457 {
458 if (!GetConversionFactor(pixidx, hiloconv, hiloconverr,
459 calibConv, calibConvErr, calibFFactor))
460 continue;
461
462 if (data)
463 {
464 const MExtractedSignalPix &sig = (*fSignals)[pixidx];
465
466 Float_t signal = 0;
467 Float_t signalErr = 0.;
468
469 if (sig.IsLoGainUsed())
470 {
471 signal = sig.GetExtractedSignalLoGain()*hiloconv;
472 signalErr = signal*hiloconverr;
473 }
474 else
475 {
476 if (sig.GetExtractedSignalHiGain() <= 9999.)
477 signal = sig.GetExtractedSignalHiGain();
478 }
479
480 const Float_t nphot = signal*calibConv;
481 const Float_t nphotErr = calibFFactor*TMath::Sqrt(TMath::Abs(nphot));
482
483 //
484 // The following part is the commented first version of the error calculation
485 // Contact Markus Gaug for questions (or wait for the next documentation update...)
486 //
487 /*
488 nphotErr = signal > 0 ? signalErr*signalErr / (signal * signal) : 0.
489 + calibConv > 0 ? calibConvVar / (calibConv * calibConv ) : 0.;
490 nphotErr = TMath::Sqrt(nphotErr) * nphot;
491 */
492
493 MCerPhotPix *cpix = fCerPhotEvt->AddPixel(pixidx, nphot, nphotErr);
494
495 if (sig.GetNumHiGainSaturated() > 0)
496 cpix->SetPixelHGSaturated();
497
498 if (sig.GetNumLoGainSaturated() > 0)
499 cpix->SetPixelSaturated();
500 }
501 if (pedestal)
502 {
503 const MPedestalPix &ped = (*fPedestal)[pixidx];
504
505 // pedestals/(used FADC slices) in [ADC] counts
506 const Float_t pedes = ped.GetPedestal() * slices;
507 const Float_t pedrms = ped.GetPedestalRms() * sqrtslices;
508
509 //
510 // pedestals/(used FADC slices) in [number of photons]
511 //
512 const Float_t pedphot = pedes * calibConv;
513 const Float_t pedphotrms = pedrms * calibConv;
514
515 (*fPedPhot)[pixidx].Set(pedphot, pedphotrms);
516 }
517 }
518
519 if (pedestal)
520 fPedPhot->SetReadyToSave();
521
522 if (data)
523 {
524 fCerPhotEvt->FixSize();
525 fCerPhotEvt->SetReadyToSave();
526 }
527}
528
529// --------------------------------------------------------------------------
530//
531// Apply the calibration factors to the extracted signal according to the
532// selected calibration method
533//
534Int_t MCalibrateData::Process()
535{
536 /*
537 if (fCalibrations->GetNumPixels() != (UInt_t)fSignals->GetSize())
538 {
539 // FIXME: MExtractedSignal must be of variable size -
540 // like MCerPhotEvt - because we must be able
541 // to reduce size by zero supression
542 // For the moment this check could be done in ReInit...
543 *fLog << err << "MExtractedSignal and MCalibrationCam have different sizes... abort." << endl;
544 return kFALSE;
545 }
546 */
547
548 Calibrate(fCalibrationMode!=kSkip, TestPedestalFlag(kEvent));
549 return kTRUE;
550}
551
552// --------------------------------------------------------------------------
553//
554// Implementation of SavePrimitive. Used to write the call to a constructor
555// to a macro. In the original root implementation it is used to write
556// gui elements to a macro-file.
557//
558void MCalibrateData::StreamPrimitive(ofstream &out) const
559{
560 out << " " << ClassName() << " " << GetUniqueName() << "(\"";
561 out << "\"" << fName << "\", \"" << fTitle << "\");" << endl;
562
563 if (TestPedestalFlag(kEvent))
564 out << " " << GetUniqueName() << ".EnablePedestalType(MCalibrateData::kEvent)" << endl;
565 if (TestPedestalFlag(kRun))
566 out << " " << GetUniqueName() << ".EnablePedestalType(MCalibrateData::kRun)" << endl;
567
568 if (fCalibrationMode != kDefault)
569 {
570 out << " " << GetUniqueName() << ".SetCalibrationMode(MCalibrateData::";
571 switch (fCalibrationMode)
572 {
573 case kSkip: out << "kSkip"; break;
574 case kNone: out << "kNone"; break;
575 case kFlatCharge: out << "kFlatCharge"; break;
576 case kBlindPixel: out << "kBlindPixel"; break;
577 case kFfactor: out << "kFfactor"; break;
578 case kPinDiode: out << "kPinDiode"; break;
579 case kCombined: out << "kCombined"; break;
580 case kDummy: out << "kDummy"; break;
581 default: out << (int)fCalibrationMode; break;
582 }
583 out << ")" << endl;
584 }
585
586 if (fNamePedADCContainer != fgNamePedADCContainer)
587 {
588 out << " " << GetUniqueName() << ".SetNamePedADCContainer(";
589 out << fNamePedADCContainer.Data() << ");" << endl;
590 }
591
592 if (fNamePedPhotContainer != fgNamePedPhotContainer)
593 {
594 out << " " << GetUniqueName() << ".SetNamePedPhotContainer(";
595 out << fNamePedPhotContainer.Data() << ");" << endl;
596 }
597}
598
599// --------------------------------------------------------------------------
600//
601// Read the setup from a TEnv, eg:
602// MJPedestal.MCalibrateDate.PedestalFlag: no,run,event
603// MJPedestal.MCalibrateDate.CalibrationMode: skip,none,flatcharge,blindpixel,ffactor,pindiode,combined,dummy,default
604//
605Int_t MCalibrateData::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
606{
607 Bool_t rc = kFALSE;
608 if (IsEnvDefined(env, prefix, "PedestalFlag", print))
609 {
610 rc = kTRUE;
611 TString s = GetEnvValue(env, prefix, "PedestalFlag", "");
612 s.ToLower();
613 if (s.BeginsWith("no"))
614 SetPedestalFlag(kNo);
615 if (s.BeginsWith("run"))
616 SetPedestalFlag(kRun);
617 if (s.BeginsWith("event"))
618 SetPedestalFlag(kEvent);
619 }
620
621 if (IsEnvDefined(env, prefix, "CalibrationMode", print))
622 {
623 rc = kTRUE;
624 TString s = GetEnvValue(env, prefix, "CalibrationMode", "");
625 s.ToLower();
626 if (s.BeginsWith("skip"))
627 SetCalibrationMode(kSkip);
628 if (s.BeginsWith("none"))
629 SetCalibrationMode(kNone);
630 if (s.BeginsWith("flatcharge"))
631 SetCalibrationMode(kFlatCharge);
632 if (s.BeginsWith("blindpixel"))
633 SetCalibrationMode(kBlindPixel);
634 if (s.BeginsWith("ffactor"))
635 SetCalibrationMode(kFfactor);
636 if (s.BeginsWith("pindiode"))
637 SetCalibrationMode(kPinDiode);
638 if (s.BeginsWith("combined"))
639 SetCalibrationMode(kCombined);
640 if (s.BeginsWith("dummy"))
641 SetCalibrationMode(kDummy);
642 if (s.BeginsWith("default"))
643 SetCalibrationMode();
644 }
645
646 return rc;
647}
Note: See TracBrowser for help on using the repository browser.