source: trunk/MagicSoft/Mars/manalysis/MCalibrationCalc.cc@ 2624

Last change on this file since 2624 was 2603, checked in by gaug, 22 years ago
*** empty log message ***
File size: 15.5 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Markus Gaug 09/2003 <mailto:markus@ifae.es>
19!
20! Copyright: MAGIC Software Development, 2000-2001
21!
22!
23\* ======================================================================== */
24
25//////////////////////////////////////////////////////////////////////////////
26// //
27// MCalibrationCalc //
28// //
29// This is a task which calculates the number of photons from the FADC //
30// time slices. At the moment it integrates simply the FADC values. //
31// //
32// Input Containers: //
33// MRawEvtData //
34// //
35// Output Containers: //
36// MCalibrationCam //
37// //
38// //
39// The class MCalibrationCam hold one entry of type MCalibrationPix for //
40// every pixel. It is filled in the following way: //
41// PreParocess: MalibrationCam::InitSize(577) is called which allocates //
42// memory in an TClonesArray of type MCalibrationPix and //
43// all pointers to NULL. //
44// //
45// Process: The NULL pointer is tested on every pixel via //
46// MalibrationCam::IsPixelUsed(npix). //
47// //
48// In case, IsPixelUsed returns NULL, //
49// MalibrationCam::AddPixel(npix) is invoked which creates a //
50// new MCalibrationPix(npix) in the npix's entry //
51// of the TClonesArray. //
52// //
53// Every MCalibrationPix holds a histogram class, //
54// MHCalibrationPixel which itself hold histograms of type: //
55// HCharge(npix) (distribution of summed FADC time slice entries) //
56// HTime(npix) (distribution of position of maximum) //
57// HChargevsN(npix) (distribution of charges vs. event number. //
58// //
59// PostProcess: All histograms HCharge(npix) are fitted to a Gaussian //
60// All histograms HTime(npix) are fitted to a Gaussian //
61// The histogram HBlindPixelCharge (blind pixel) is fitted to a single //
62// PhE fit //
63// The histogram HBlindPixelTime (blind pixel) is fitted to a Gaussian //
64// The histograms of the PIN Diode are fitted to Gaussians //
65// //
66// Fits can be excluded via the commands: //
67// MalibrationCam::SetSkipTimeFits() (skip all time fits) //
68// MalibrationCam::SetSkipBlindPixelFits() (skip all blind pixel fits) //
69// MalibrationCam::SetSkipPinDiodeFits() (skip all PIN Diode fits) //
70// //
71//////////////////////////////////////////////////////////////////////////////
72
73#include "MCalibrationCalc.h"
74#include "MCalibrationConfig.h"
75#include "MCalibrationFits.h"
76
77#include "MCalibrationCam.h"
78#include "MCalibrationPix.h"
79#include "MCalibrationBlindPix.h"
80#include "MCalibrationPINDiode.h"
81
82#include "MPedestalCam.h"
83#include "MPedestalPix.h"
84
85#include "MLog.h"
86#include "MLogManip.h"
87
88#include "MParList.h"
89#include "MH.h"
90
91#include "MRawRunHeader.h"
92#include "MRawEvtData.h" // MRawEvtData::GetNumPixels
93#include "MRawEvtPixelIter.h"
94
95#include "MTime.h"
96#include "TMath.h"
97
98ClassImp(MCalibrationCalc);
99
100using namespace std;
101// --------------------------------------------------------------------------
102//
103// Default constructor. b is the number of slices before the maximum slice,
104// a the number of slices behind the maximum slice which is taken as signal.
105//
106MCalibrationCalc::MCalibrationCalc(const char *name, const char *title)
107 : fColor(kEBlue)
108{
109
110 fName = name ? name : "MCalibrationCalc";
111 fTitle = title ? title : "Task to calculate the calibration constants and MCalibrationCam ";
112
113 AddToBranchList("MRawEvtData.fHiGainPixId");
114 AddToBranchList("MRawEvtData.fLoGainPixId");
115 AddToBranchList("MRawEvtData.fHiGainFadcSamples");
116 AddToBranchList("MRawEvtData.fLoGainFadcSamples");
117
118 SETBIT(fFlags, kUseTimeFits);
119 SETBIT(fFlags, kUseBlindPixelFit);
120 SETBIT(fFlags, kUsePinDiodeFit);
121}
122
123// --------------------------------------------------------------------------
124//
125// The PreProcess searches for the following input containers:
126// - MRawEvtData
127// - MPedestalCam
128//
129// The following output containers are also searched and created if
130// they were not found:
131//
132// - MHCalibrationBlindPixel
133// - MCalibrationCam
134// - MTime
135//
136Int_t MCalibrationCalc::PreProcess(MParList *pList)
137{
138
139 fHistOverFlow = 0;
140 fEvents = 0;
141 fCosmics = 0;
142
143 fRawEvt = (MRawEvtData*)pList->FindObject("MRawEvtData");
144 if (!fRawEvt)
145 {
146 *fLog << dbginf << "MRawEvtData not found... aborting." << endl;
147 return kFALSE;
148 }
149
150 const MRawRunHeader *runheader = (MRawRunHeader*)pList->FindObject("MRawRunHeader");
151 if (!runheader)
152 *fLog << warn << dbginf << "Warning - cannot check file type, MRawRunHeader not found." << endl;
153 else
154 if (runheader->GetRunType() == kRTMonteCarlo)
155 {
156 return kTRUE;
157 }
158
159 fCalibrations = (MCalibrationCam*)pList->FindCreateObj("MCalibrationCam");
160 if (!fCalibrations)
161 {
162 *fLog << err << dbginf << "MCalibrationCam could not be created ... aborting." << endl;
163 return kFALSE;
164 }
165
166
167 switch (fColor)
168 {
169 case kEBlue:
170 fCalibrations->SetColor(MCalibrationCam::kECBlue);
171 break;
172 case kEGreen:
173 fCalibrations->SetColor(MCalibrationCam::kECGreen);
174 break;
175 case kEUV:
176 fCalibrations->SetColor(MCalibrationCam::kECUV);
177 }
178
179
180
181 fPedestals = (MPedestalCam*)pList->FindObject("MPedestalCam");
182 if (!fPedestals)
183 {
184 *fLog << err << dbginf << "Cannot find MPedestalCam ... aborting" << endl;
185 return kFALSE;
186 }
187
188
189 return kTRUE;
190}
191
192
193// --------------------------------------------------------------------------
194//
195// The ReInit searches for the following input containers:
196// - MRawRunHeader
197//
198Bool_t MCalibrationCalc::ReInit(MParList *pList )
199{
200
201 fRunHeader = (MRawRunHeader*)pList->FindObject("MRawRunHeader");
202 if (!fRunHeader)
203 {
204 *fLog << dbginf << "MRawRunHeader not found... aborting." << endl;
205 return kFALSE;
206 }
207
208 fNumHiGainSamples = fRunHeader->GetNumSamplesHiGain();
209 fNumLoGainSamples = fRunHeader->GetNumSamplesLoGain();
210
211 //
212 // FIXME: The next statement simply does not work:
213 // fRawEvt->GetNumPixels() returns always 0
214 // fCalibrations->InitSize(fRawEvt->GetNumPixels());
215 //
216
217 fCalibrations->InitSize(577);
218
219 for (Int_t i=0;i<577;i++)
220 {
221 MCalibrationPix &pix = (*fCalibrations)[i];
222 pix.DefinePixId(i);
223 }
224
225 return kTRUE;
226
227}
228
229
230// --------------------------------------------------------------------------
231//
232// Calculate the integral of the FADC time slices and store them as a new
233// pixel in the MCerPhotEvt container.
234//
235Int_t MCalibrationCalc::Process()
236{
237
238 fEvents++;
239
240 Int_t cosmicpix = 0;
241
242 MCalibrationBlindPix &blindpixel = *(fCalibrations->GetBlindPixel());
243 MCalibrationPINDiode &pindiode = *(fCalibrations->GetPINDiode());
244
245 MRawEvtPixelIter pixel(fRawEvt);
246
247 // Create a first loop to sort out the cosmics ...
248 //
249 // This is a very primitive check for the number of cosmicpixs
250 // The cut will be applied in the fit, but for the blind pixel,
251 // we need to remove this event
252 //
253 // FIXME: In the future need a much more sophisticated one!!!
254 //
255
256 while (pixel.Next())
257 {
258
259 const Int_t pixid = pixel.GetPixelId();
260
261 Int_t sum = pixel.GetSumHiGainSamples();
262
263 MPedestalPix &ped = (*fPedestals)[pixid];
264
265 Float_t pedes = ped.GetPedestal();
266 Float_t pedrms = ped.GetPedestalRms();
267
268 if ((float)sum < (pedes*fNumHiGainSamples)+(1.5*pedrms) )
269 cosmicpix++;
270 }
271
272
273 if (cosmicpix > 50.)
274 {
275 fCosmics++;
276 return kTRUE;
277 }
278
279 pixel.Reset();
280
281 while (pixel.Next())
282 {
283
284 UShort_t sat = 0;
285 UShort_t lowgainoverflow = 0;
286
287 const Int_t pixid = pixel.GetPixelId();
288
289 Byte_t *ptr = pixel.GetHiGainSamples();
290 Byte_t mid = pixel.GetIdxMaxHiGainSample();
291 UInt_t max = pixel.GetMaxHiGainSample();
292
293 Int_t sum = (max > gkSaturationLimit // overflow ?
294 ? sat++, pixel.GetSumLoGainSamples() // take Low Gain
295 : pixel.GetSumHiGainSamples()); // no overflow
296
297 if (sat)
298 {
299
300 ptr = pixel.GetLoGainSamples();
301 max = pixel.GetMaxLoGainSample();
302 mid = pixel.GetIdxMaxLoGainSample();
303
304 //
305 // FIXME: It seems the conversion HiGain LoGain is already
306 // performed in the data?!?
307 //
308 sum = (max > gkSaturationLimit // overflow of LoGain ??? -> GimmeABreak!!!
309 ? lowgainoverflow++, gkLoGainOverFlow // OUCH (Florian was maybe right)
310 : sum*gkConversionHiLo ); // OUFF (Florian was wrong) !!
311 // : sum );
312
313 if (lowgainoverflow)
314 {
315 *fLog << err << dbginf << "Warning: Saturation of LoGain reached in pixel: " << pixid << " "
316 << " sum = " << sum << endl;
317 fHistOverFlow++;
318 }
319 }
320
321 MPedestalPix &ped = (*fPedestals)[pixid];
322 MCalibrationPix &pix = (*fCalibrations)[pixid];
323
324 Float_t pedes = ped.GetPedestal();
325 Float_t pedrms = ped.GetPedestalRms();
326
327 //
328 // FIXME: This is preliminary, we will change to pedestals per slice!!!
329 // Assume pedestals per time slice ==> multiply with number of slices
330 //
331 pedes *= (sat ? fNumLoGainSamples : fNumHiGainSamples );
332 pedrms *= (sat ? fNumLoGainSamples : fNumHiGainSamples );
333
334 Float_t rsum = (float)sum - pedes;
335
336 switch(pixid)
337 {
338
339 case gkCalibrationBlindPixelId:
340
341 //
342 // FIXME: This works only when the blind pixel ID is much larger than
343 // the rest of the pixels (which is the case right now)
344 //
345 if (!blindpixel.FillCharge(sum))
346 *fLog << warn <<
347 "Overflow or Underflow occurred filling Blind Pixel sum = " << sum << endl;
348
349 if (!blindpixel.FillTime((int)mid))
350 *fLog << warn <<
351 "Overflow or Underflow occurred filling Blind Pixel time = " << (int)mid << endl;
352
353 if (!blindpixel.FillRChargevsTime(rsum,fEvents))
354 *fLog << warn <<
355 "Overflow or Underflow occurred filling Blind Pixel eventnr = " << fEvents << endl;
356
357 case gkCalibrationPINDiodeId:
358 if (!pindiode.FillCharge(sum))
359 *fLog << warn <<
360 "Overflow or Underflow occurred filling HCharge: means = " << sum << endl;
361 if (!pindiode.FillTime((int)mid))
362 *fLog << warn <<
363 "Overflow or Underflow occurred filling HTime: time = " << (int)mid << endl;
364 if (!pindiode.FillRChargevsTime(rsum,fEvents))
365 *fLog << warn <<
366 "Overflow or Underflow occurred filling HChargevsN: eventnr = " << fEvents << endl;
367
368 default:
369
370 if (!pix.FillCharge(sum))
371 *fLog << warn << "Could not fill Charge of pixel: " << pixid
372 << " signal = " << sum << endl;
373
374 //
375 // Fill the reduced charge into the control histo for better visibility
376 //
377 if (!pix.FillRChargevsTime(rsum,fEvents))
378 *fLog << warn << "Could not fill red. Charge vs. EvtNr of pixel: " << pixid
379 << " signal = " << rsum << " event Nr: " << fEvents << endl;
380
381
382 if (!pix.FillTime((int)mid))
383 *fLog << warn << "Could not fill Time of pixel: " << pixid << " time = " << (int)mid << endl;
384
385
386 } /* switch(pixid) */
387
388 } /* while (pixel.Next()) */
389
390 return kTRUE;
391}
392
393Int_t MCalibrationCalc::PostProcess()
394{
395
396 *fLog << inf << endl;
397 *fLog << GetDescriptor() << " Cut Histogram Edges" << endl;
398 //
399 // Cut edges to make fits and viewing of the hists easier
400 //
401 fCalibrations->CutEdges();
402
403 //
404 // Get pointer to blind pixel
405 //
406 MCalibrationBlindPix &blindpixel = *(fCalibrations->GetBlindPixel());
407
408 *fLog << GetDescriptor() << " Fitting the Blind Pixel" << endl;
409
410 //
411 // Fit the blind pixel
412 //
413 if (TESTBIT(fFlags,kUseBlindPixelFit))
414 {
415 if (blindpixel.FitCharge())
416 if (!fCalibrations->CalcNrPhotInnerPixel())
417 *fLog << err << dbginf << "Could not calculate Number of photons from the blind pixel " << endl;
418 else
419 *fLog << err << dbginf << "Could not fit the blind pixel " << endl;
420
421 if (!blindpixel.FitTime())
422 *fLog << warn << "Could not the Times of the blind pixel " << endl;
423
424 blindpixel.Draw();
425
426 }
427
428 *fLog << GetDescriptor() << " Fitting the Normal Pixels" << endl;
429
430 //
431 // loop over the pedestal events and check if we have calibration
432 //
433 for (Int_t pixid=0; pixid<fPedestals->GetSize(); pixid++)
434 {
435
436 MCalibrationPix &pix = (*fCalibrations)[pixid];
437
438 const Float_t ped = (*fPedestals)[pixid].GetPedestal() * fNumHiGainSamples;
439 const Float_t prms = (*fPedestals)[pixid].GetPedestalRms() * TMath::Sqrt((float)fNumHiGainSamples);
440
441 pix.SetPedestal(ped,prms);
442
443 if (TESTBIT(fFlags,kUseTimeFits))
444 pix.FitTime();
445
446 pix.FitCharge();
447 }
448
449 fCalibrations->SetReadyToSave();
450
451 if (GetNumExecutions()==0)
452 return kTRUE;
453
454 *fLog << endl;
455 *fLog << dec << setfill(' ') << fCosmics << " Events presumably cosmics" << endl;
456
457 return kTRUE;
458}
Note: See TracBrowser for help on using the repository browser.