source: trunk/MagicSoft/Mars/mpedestal/MPedCalcPedRun.cc@ 5660

Last change on this file since 5660 was 5570, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 13.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): Josep Flix 04/2001 <mailto:jflix@ifae.es>
19! Author(s): Thomas Bretz 05/2001 <mailto:tbretz@astro.uni-wuerzburg.de>
20! Author(s): Sebastian Commichau 12/2003
21! Author(s): Javier Rico 01/2004 <mailto:jrico@ifae.es>
22! Author(s): Markus Gaug 01/2004 <mailto:markus@ifae.es>
23!
24! Copyright: MAGIC Software Development, 2000-2004
25!
26!
27\* ======================================================================== */
28/////////////////////////////////////////////////////////////////////////////
29//
30// MPedCalcPedRun
31//
32// This task takes a pedestal run file and fills MPedestalCam during
33// the Process() with the pedestal and rms computed in an event basis.
34// In the PostProcess() MPedestalCam is finally filled with the pedestal
35// mean and rms computed in a run basis.
36// More than one run (file) can be merged
37//
38// MPedCalcPedRun applies the following formula (1):
39//
40// Pedestal per slice = sum(x_i) / n / slices
41// PedRMS per slice = Sqrt( ( sum(x_i^2) - sum(x_i)^2/n ) / n-1 / slices )
42//
43// where x_i is the sum of "slices" FADC slices and sum means the sum over all
44// events. "n" is the number of events, "slices" is the number of summed FADC samples.
45//
46// Note that the slice-to-slice fluctuations are not Gaussian, but Poissonian, thus
47// asymmetric and they are correlated.
48//
49// It is important to know that the Pedestal per slice and PedRMS per slice depend
50// on the number of used FADC slices, as seen in the following plots:
51//
52//Begin_Html
53/*
54<img src="images/PedestalStudyInner.gif">
55*/
56//End_Html
57//
58//Begin_Html
59/*
60<img src="images/PedestalStudyOuter.gif">
61*/
62//
63// The plots show the inner and outer pixels, respectivly and have the following meaning:
64//
65// 1) The calculated mean pedestal per slice (from MPedCalcPedRun)
66// 2) The fitted mean pedestal per slice (from MHPedestalCam)
67// 3) The calculated pedestal RMS per slice (from MPedCalcPedRun)
68// 4) The fitted sigma of the pedestal distribution per slice
69// (from MHPedestalCam)
70// 5) The relative difference between calculation and histogram fit
71// for the mean
72// 6) The relative difference between calculation and histogram fit
73// for the sigma or RMS, respectively.
74//
75// The calculated means do not change significantly except for the case of 2 slices,
76// however the RMS changes from 5.7 per slice in the case of 2 extracted slices
77// to 8.3 per slice in the case of 26 extracted slices. This change is very significant.
78//
79// The plots have been produced on run 20123. You can reproduce them using
80// the macro pedestalstudies.C
81//
82// Usage of this class:
83// ====================
84//
85// Call: SetRange(higainfirst, higainlast, logainfirst, logainlast)
86// to modify the ranges in which the window is allowed to move.
87// Defaults are:
88//
89// fHiGainFirst = fgHiGainFirst = 0
90// fHiGainLast = fgHiGainLast = 29
91// fLoGainFirst = fgLoGainFirst = 0
92// fLoGainLast = fgLoGainLast = 14
93//
94// Call: SetWindowSize(windowhigain, windowlogain)
95// to modify the sliding window widths. Windows have to be an even number.
96// In case of odd numbers, the window will be modified.
97//
98// Defaults are:
99//
100// fHiGainWindowSize = fgHiGainWindowSize = 14
101// fLoGainWindowSize = fgLoGainWindowSize = 0
102//
103//
104// ToDo:
105// - Take a setup file (ReadEnv-implementation) as input
106//
107//
108// Input Containers:
109// MRawEvtData
110// MRawRunHeader
111// MRawEvtHeader
112// MGeomCam
113//
114// Output Containers:
115// MPedestalCam
116//
117// See also: MPedestalCam, MPedestalPix, MHPedestalCam, MExtractor
118//
119/////////////////////////////////////////////////////////////////////////////
120#include "MPedCalcPedRun.h"
121
122#include "MParList.h"
123
124#include "MLog.h"
125#include "MLogManip.h"
126
127#include "MRawRunHeader.h"
128#include "MRawEvtHeader.h"
129#include "MRawEvtPixelIter.h"
130#include "MRawEvtData.h"
131
132#include "MPedestalPix.h"
133#include "MPedestalCam.h"
134
135#include "MGeomPix.h"
136#include "MGeomCam.h"
137
138#include "MExtractPedestal.h"
139#include "MExtractTimeAndCharge.h"
140
141ClassImp(MPedCalcPedRun);
142
143using namespace std;
144
145const UShort_t MPedCalcPedRun::fgExtractWinFirst = 0;
146const UShort_t MPedCalcPedRun::fgExtractWinSize = 6;
147const UInt_t MPedCalcPedRun::gkFirstRunWithFinalBits = 45589;
148
149// --------------------------------------------------------------------------
150//
151// Default constructor:
152//
153// Sets:
154// - all pointers to NULL
155// - fWindowSizeHiGain to fgHiGainWindowSize
156// - fWindowSizeLoGain to fgLoGainWindowSize
157//
158// Calls:
159// - AddToBranchList("fHiGainPixId");
160// - AddToBranchList("fHiGainFadcSamples");
161// - SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast)
162//
163MPedCalcPedRun::MPedCalcPedRun(const char *name, const char *title)
164 : fOverlap(0)
165{
166 fName = name ? name : "MPedCalcPedRun";
167 fTitle = title ? title : "Task to calculate pedestals from pedestal runs raw data";
168
169 SetExtractWindow(fgExtractWinFirst, fgExtractWinSize);
170}
171
172// --------------------------------------------------------------------------
173//
174// In case that the variables fExtractLast is greater than the number of
175// High-Gain FADC samples obtained from the run header, the flag
176// fOverlap is set to the difference and fExtractLast is set back by the
177// same number of slices.
178//
179void MPedCalcPedRun::CheckExtractionWindow()
180{
181 const UShort_t lastavailable = fRunHeader->GetNumSamplesHiGain()-1;
182
183 if (fExtractWinLast <= lastavailable)
184 return;
185
186 const UShort_t diff = fExtractWinLast - lastavailable;
187
188 *fLog << warn << endl;
189 *fLog << "Selected ExtractWindow [" << fExtractWinFirst << "," << fExtractWinLast;
190 *fLog << "] ranges out of range [0," << lastavailable << "]." << endl;
191 *fLog << "Using " << diff << " samples from the 'Lo-Gain' slices for the pedestal extraction" << endl;
192
193 fExtractWinLast -= diff;
194 fOverlap = diff;
195}
196
197// --------------------------------------------------------------------------
198//
199// Set fIsFirstPedRun=kTRUE
200//
201Int_t MPedCalcPedRun::PreProcess(MParList *pList)
202{
203 fUsedEvents = 0;
204 fIsFirstPedRun = kTRUE;
205 return MExtractPedestal::PreProcess(pList);
206}
207
208// --------------------------------------------------------------------------
209//
210// The run type is checked for "kRTPedestal"
211// and the variable fSkip is set in that case
212//
213Bool_t MPedCalcPedRun::ReInit(MParList *pList)
214{
215 if (!MExtractPedestal::ReInit(pList))
216 return kFALSE;
217
218 CheckExtractionWindow();
219
220 // If this is the first ped run, the next run (call to ReInit)
221 // is not the first anymore
222 if (fRunHeader->GetRunType()==MRawRunHeader::kRTPedestal)
223 {
224 fIsFirstPedRun = kFALSE;
225 return kTRUE;
226 }
227
228 // If this is the first call to ReInit (before reading the first file)
229 // nothing should be done
230 if (fIsFirstPedRun)
231 return kTRUE;
232
233 // In any other case some kind of finaliazation must be done
234 *fLog << inf << "Finalizing pedestal calculations..." << flush;
235
236 if (!Finalize())
237 return kFALSE;
238
239 Reset();
240
241 return kTRUE;
242}
243
244// --------------------------------------------------------------------------
245//
246// Return kTRUE (without doing anything) in case that the run type is not
247// equal to 1 (pedestal run)
248//
249// Fill the MPedestalCam container with the signal mean and rms for the event.
250// Store the measured signal in arrays fSumx and fSumx2 so that we can
251// calculate the overall mean and rms in the PostProcess()
252//
253Int_t MPedCalcPedRun::Process()
254{
255 if (!IsPedBitSet())
256 {
257 *fLog << err << GetDescriptor() << " - ERROR: IsPedBitSet() returned kFALSE... abort." << endl;
258 return kERROR;
259 }
260
261 if (fRunHeader->GetRunType() != MRawRunHeader::kRTPedestal)
262 return kTRUE;
263
264 fUsedEvents++;
265
266 MRawEvtPixelIter pixel(fRawEvt);
267
268 while (pixel.Next())
269 {
270 const UInt_t idx = pixel.GetPixelId();
271 const UInt_t aidx = (*fGeom)[idx].GetAidx();
272 const UInt_t sector = (*fGeom)[idx].GetSector();
273
274 Float_t sum = 0.;
275 UInt_t ab0 = 0;
276 UInt_t ab1 = 0;
277
278 if (fExtractor)
279 CalcExtractor(pixel, sum, (*fPedestalsIn)[idx]);
280 else
281 CalcSums(pixel, sum, ab0, ab1);
282
283 fSumx[idx] += sum;
284 fAreaSumx[aidx] += sum;
285 fSectorSumx[sector] += sum;
286
287 const Float_t sqrsum = sum*sum;
288 fSumx2[idx] += sqrsum;
289 fAreaSumx2[aidx] += sqrsum;
290 fSectorSumx2[sector] += sqrsum;
291
292 fSumAB0[idx] += ab0;
293 fSumAB1[idx] += ab1;
294
295 fAreaSumAB0[aidx] += ab0;
296 fAreaSumAB1[aidx] += ab1;
297
298 fSectorSumAB0[aidx] += ab0;
299 fSectorSumAB1[aidx] += ab1;
300 }
301
302 fPedestalsOut->SetReadyToSave();
303
304 return kTRUE;
305}
306
307
308void MPedCalcPedRun::CalcExtractor(const MRawEvtPixelIter &pixel, Float_t &sum, MPedestalPix &ped)
309{
310 const Bool_t abflag = pixel.HasABFlag();
311
312 Byte_t *first = pixel.GetHiGainSamples() + fExtractWinFirst;
313 Byte_t *logain = pixel.GetLoGainSamples();
314
315 Byte_t sat = 0;
316
317 Float_t dummy;
318 fExtractor->SetNoiseCalculation(kTRUE);
319 fExtractor->FindTimeAndChargeHiGain(first,logain,sum,dummy,dummy,dummy,sat,ped,abflag);
320 fExtractor->SetNoiseCalculation(kFALSE);
321}
322
323void MPedCalcPedRun::CalcSums(const MRawEvtPixelIter &pixel, Float_t &sum, UInt_t &ab0, UInt_t &ab1)
324{
325
326 Byte_t *higain = pixel.GetHiGainSamples() + fExtractWinFirst;
327 Byte_t *ptr = higain;
328 Byte_t *end = ptr + fExtractWinSize;
329
330 const Bool_t abflag = pixel.HasABFlag();
331
332 Int_t sumi = 0;
333
334 Int_t cnt = 0;
335 do
336 {
337 sumi += *ptr;
338 if (!pixel.IsABFlagValid())
339 continue;
340
341 const Int_t abFlag = (fExtractWinFirst + abflag + cnt) & 0x1;
342 if (abFlag)
343 ab1 += *ptr;
344 else
345 ab0 += *ptr;
346
347 cnt++;
348 } while (++ptr != end);
349
350 if (fOverlap != 0)
351 {
352 ptr = pixel.GetLoGainSamples();
353 end = ptr + fOverlap;
354
355 do
356 {
357 sumi += *ptr;
358 if (!pixel.IsABFlagValid())
359 continue;
360
361 const Int_t abFlag = (fExtractWinFirst + abflag + cnt) & 0x1;
362 if (abFlag)
363 ab1 += *ptr;
364 else
365 ab0 += *ptr;
366
367 cnt++;
368 } while (++ptr != end);
369 }
370
371 sum = (Float_t)sumi;
372}
373
374// --------------------------------------------------------------------------
375//
376// Compute signal mean and rms in the whole run and store it in MPedestalCam
377//
378Int_t MPedCalcPedRun::Finalize()
379{
380 if (fUsedEvents == 0)
381 return kTRUE;
382
383 MRawEvtPixelIter pixel(fRawEvt);
384 while (pixel.Next())
385 CalcPixResults(fUsedEvents, pixel.GetPixelId());
386
387 //
388 // Loop over the (two) area indices to get the averaged pedestal per aidx
389 //
390 for (UInt_t aidx=0; aidx<fAreaValid.GetSize(); aidx++)
391 if (fAreaValid[aidx]>0)
392 CalcAreaResults(fUsedEvents, fAreaValid[aidx], aidx);
393
394 //
395 // Loop over the (six) sector indices to get the averaged pedestal per sector
396 //
397 for (UInt_t sector=0; sector<fSectorValid.GetSize(); sector++)
398 if (fSectorValid[sector]>0)
399 CalcSectorResults(fUsedEvents, fSectorValid[sector], sector);
400
401 fPedestalsOut->SetTotalEntries(fUsedEvents*fExtractWinSize);
402 fPedestalsOut->SetReadyToSave();
403
404 return kTRUE;
405}
406
407Int_t MPedCalcPedRun::PostProcess()
408{
409 if (!Finalize())
410 return kFALSE;
411
412 return MExtractPedestal::PostProcess();
413}
414
415//-------------------------------------------------------------
416//
417// Return if the pedestal bit was set from the calibration trigger box.
418// The last but one bit is used for the "pedestal-bit".
419//
420// This bit is set since run gkFinalizationTriggerBit
421//
422Bool_t MPedCalcPedRun::IsPedBitSet()
423{
424 if (fRunHeader->GetRunNumber() == 38996)
425 return kTRUE;
426
427 if (fRunHeader->GetRunNumber() < gkFirstRunWithFinalBits)
428 return kTRUE;
429
430 return (fEvtHeader->GetTriggerID() & BIT(3)) ? kTRUE : kFALSE;
431}
432
433void MPedCalcPedRun::Print(Option_t *o) const
434{
435 *fLog << GetDescriptor() << ":" << endl;
436 *fLog << "Name of input MPedestalCam: " << (fPedestalsIn?fPedestalsIn->GetName():fNamePedestalCamIn.Data()) << " (" << fPedestalsIn << ")" << endl;
437 *fLog << "Name of output MPedestalCam: " << (fPedestalsOut?fPedestalsOut->GetName():fNamePedestalCamOut.Data()) << " (" << fPedestalsOut << ")" << endl;
438 *fLog << "ExtractWindow from slice " << fExtractWinFirst << " to " << fExtractWinLast << " incl." << endl;
439 *fLog << "Number overlap lo-gain slices: " << fOverlap << endl;
440 *fLog << "First ped run out of sequence: " << (fIsFirstPedRun?"yes":"no") << endl;
441 *fLog << "Number of used events so far: " << fUsedEvents << endl;
442
443 if (fExtractor)
444 *fLog << "Extractor used: " << fExtractor->ClassName() << endl;
445
446 *fLog << endl;
447}
Note: See TracBrowser for help on using the repository browser.