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

Last change on this file since 5817 was 5715, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 12.7 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::Calc()
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->FindTimeAndChargeHiGain(first,logain,sum,dummy,dummy,dummy,sat,ped,abflag);
319}
320
321void MPedCalcPedRun::CalcSums(const MRawEvtPixelIter &pixel, Float_t &sum, UInt_t &ab0, UInt_t &ab1)
322{
323
324 Byte_t *higain = pixel.GetHiGainSamples() + fExtractWinFirst;
325 Byte_t *ptr = higain;
326 Byte_t *end = ptr + fExtractWinSize;
327
328 const Bool_t abflag = pixel.HasABFlag();
329
330 Int_t sumi = 0;
331
332 Int_t cnt = 0;
333 do
334 {
335 sumi += *ptr;
336 if (!pixel.IsABFlagValid())
337 continue;
338
339 const Int_t abFlag = (fExtractWinFirst + abflag + cnt) & 0x1;
340 if (abFlag)
341 ab1 += *ptr;
342 else
343 ab0 += *ptr;
344
345 cnt++;
346 } while (++ptr != end);
347
348 if (fOverlap != 0)
349 {
350 ptr = pixel.GetLoGainSamples();
351 end = ptr + fOverlap;
352
353 do
354 {
355 sumi += *ptr;
356 if (!pixel.IsABFlagValid())
357 continue;
358
359 const Int_t abFlag = (fExtractWinFirst + abflag + cnt) & 0x1;
360 if (abFlag)
361 ab1 += *ptr;
362 else
363 ab0 += *ptr;
364
365 cnt++;
366 } while (++ptr != end);
367 }
368
369 sum = (Float_t)sumi;
370}
371
372// --------------------------------------------------------------------------
373//
374// Compute signal mean and rms in the whole run and store it in MPedestalCam
375//
376Int_t MPedCalcPedRun::Finalize()
377{
378 if (fUsedEvents == 0)
379 return kTRUE;
380
381 MRawEvtPixelIter pixel(fRawEvt);
382 while (pixel.Next())
383 CalcPixResults(fUsedEvents, pixel.GetPixelId());
384
385 //
386 // Loop over the (two) area indices to get the averaged pedestal per aidx
387 //
388 for (UInt_t aidx=0; aidx<fAreaValid.GetSize(); aidx++)
389 if (fAreaValid[aidx]>0)
390 CalcAreaResults(fUsedEvents, fAreaValid[aidx], aidx);
391
392 //
393 // Loop over the (six) sector indices to get the averaged pedestal per sector
394 //
395 for (UInt_t sector=0; sector<fSectorValid.GetSize(); sector++)
396 if (fSectorValid[sector]>0)
397 CalcSectorResults(fUsedEvents, fSectorValid[sector], sector);
398
399 fPedestalsOut->SetTotalEntries(fUsedEvents*fExtractWinSize);
400 fPedestalsOut->SetReadyToSave();
401
402 return kTRUE;
403}
404
405Int_t MPedCalcPedRun::PostProcess()
406{
407 if (!Finalize())
408 return kFALSE;
409
410 return MExtractPedestal::PostProcess();
411}
412
413//-------------------------------------------------------------
414//
415// Return if the pedestal bit was set from the calibration trigger box.
416// The last but one bit is used for the "pedestal-bit".
417//
418// This bit is set since run gkFinalizationTriggerBit
419//
420Bool_t MPedCalcPedRun::IsPedBitSet()
421{
422 if (fRunHeader->GetRunNumber() == 38996)
423 return kTRUE;
424
425 if (fRunHeader->GetRunNumber() < gkFirstRunWithFinalBits)
426 return kTRUE;
427
428 return (fEvtHeader->GetTriggerID() & BIT(3)) ? kTRUE : kFALSE;
429}
430
431void MPedCalcPedRun::Print(Option_t *o) const
432{
433 MExtractPedestal::Print(o);
434
435 *fLog << "ExtractWindow from slice " << fExtractWinFirst << " to " << fExtractWinLast << " incl." << endl;
436 *fLog << "Num overlap lo-gain slices: " << fOverlap << endl;
437 *fLog << "First pedrun out of sequence: " << (fIsFirstPedRun?"yes":"no") << endl;
438 *fLog << "Number of used events so far: " << fUsedEvents << endl;
439}
Note: See TracBrowser for help on using the repository browser.