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

Last change on this file since 5550 was 5550, checked in by gaug, 20 years ago
*** empty log message ***
File size: 12.4 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#include "MExtractPedestal.h"
122
123#include "MExtractTimeAndCharge.h"
124
125#include "MParList.h"
126
127#include "MLog.h"
128#include "MLogManip.h"
129
130#include "MRawRunHeader.h"
131#include "MRawEvtHeader.h"
132#include "MRawEvtPixelIter.h"
133#include "MRawEvtData.h"
134
135#include "MPedestalPix.h"
136#include "MPedestalCam.h"
137
138#include "MGeomPix.h"
139#include "MGeomCam.h"
140
141#include "MStatusDisplay.h"
142
143ClassImp(MPedCalcPedRun);
144
145using namespace std;
146
147const UShort_t MPedCalcPedRun::fgExtractWinFirst = 0;
148const UShort_t MPedCalcPedRun::fgExtractWinSize = 6;
149const UInt_t MPedCalcPedRun::gkFirstRunWithFinalBits = 45589;
150// --------------------------------------------------------------------------
151//
152// Default constructor:
153//
154// Sets:
155// - all pointers to NULL
156// - fWindowSizeHiGain to fgHiGainWindowSize
157// - fWindowSizeLoGain to fgLoGainWindowSize
158//
159// Calls:
160// - AddToBranchList("fHiGainPixId");
161// - AddToBranchList("fHiGainFadcSamples");
162// - SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast)
163// - Clear()
164//
165MPedCalcPedRun::MPedCalcPedRun(const char *name, const char *title)
166{
167 fName = name ? name : "MPedCalcPedRun";
168 fTitle = title ? title : "Task to calculate pedestals from pedestal runs raw data";
169
170 AddToBranchList("fHiGainPixId");
171 AddToBranchList("fHiGainFadcSamples");
172
173 SetExtractWindow(fgExtractWinFirst, fgExtractWinSize);
174
175 Clear();
176
177 fFirstRun = kTRUE;
178 fSkip = kFALSE;
179 fOverlap = 0;
180 fUsedEvents = 0;
181}
182
183
184
185void MPedCalcPedRun::ResetArrays()
186{
187
188 MExtractPedestal::ResetArrays();
189 fUsedEvents = 0;
190}
191
192// --------------------------------------------------------------------------
193//
194// In case that the variables fExtractLast is greater than the number of
195// High-Gain FADC samples obtained from the run header, the flag
196// fOverlap is set to the difference and fExtractLast is set back by the
197// same number of slices.
198//
199// The run type is checked for "Pedestal" (run type: 1)
200// and the variable fSkip is set in that case
201//
202Bool_t MPedCalcPedRun::ReInit(MParList *pList)
203{
204
205 MExtractPedestal::ReInit(pList);
206
207 const UShort_t lastavailable = fRunHeader->GetNumSamplesHiGain()-1;
208
209 if (fExtractWinLast > lastavailable)
210 {
211 const UShort_t diff = fExtractWinLast - lastavailable;
212 *fLog << endl;
213 *fLog << warn << GetDescriptor()
214 << Form("%s%2i%s%2i%s%2i%s",": Selected ExtractWindow [",
215 fExtractWinFirst,",",fExtractWinLast,
216 "] ranges out of available limits: [0,",lastavailable,"].") << endl;
217 *fLog << warn << GetDescriptor()
218 << Form("%s%2i%s",": Will use ",diff," samples from the 'Low-Gain' slices for the pedestal extraction")
219 << endl;
220 fExtractWinLast -= diff;
221 fOverlap = diff;
222 }
223
224 const UShort_t runtype = fRunHeader->GetRunType();
225
226 switch (runtype)
227 {
228 case 1:
229 fFirstRun = kFALSE;
230 fSkip = kFALSE;
231 return kTRUE;
232 break;
233 default:
234 fSkip = kTRUE;
235 if (!fFirstRun)
236 {
237 *fLog << endl;
238 *fLog << inf << GetDescriptor() << " : Finalize pedestal calculations..." << flush;
239 Finalize();
240 Reset();
241 }
242 return kTRUE;
243 break;
244 }
245
246 Print();
247
248 return kTRUE;
249}
250
251// --------------------------------------------------------------------------
252//
253// Return kTRUE (without doing anything) in case that the run type is not
254// equal to 1 (pedestal run)
255//
256// Fill the MPedestalCam container with the signal mean and rms for the event.
257// Store the measured signal in arrays fSumx and fSumx2 so that we can
258// calculate the overall mean and rms in the PostProcess()
259//
260Int_t MPedCalcPedRun::Process()
261{
262
263 fUsedEvents++;
264
265 MRawEvtPixelIter pixel(fRawEvt);
266
267 while (pixel.Next())
268 {
269 const UInt_t idx = pixel.GetPixelId();
270 const UInt_t aidx = (*fGeom)[idx].GetAidx();
271 const UInt_t sector = (*fGeom)[idx].GetSector();
272
273 Float_t sum = 0.;
274 UInt_t ab0 = 0;
275 UInt_t ab1 = 0;
276
277 if (fExtractor)
278 CalcExtractor( &pixel, sum, (*fPedestalsIn)[idx]);
279 else
280 CalcSums( &pixel, sum, ab0, ab1);
281
282 fSumx[idx] += sum;
283 fAreaSumx[aidx] += sum;
284 fSectorSumx[sector] += sum;
285
286 const Float_t sqrsum = sum*sum;
287 fSumx2[idx] += sqrsum;
288 fAreaSumx2[aidx] += sqrsum;
289 fSectorSumx2[sector] += sqrsum;
290
291 fSumAB0[idx] += ab0;
292 fSumAB1[idx] += ab1;
293
294 fAreaSumAB0[aidx] += ab0;
295 fAreaSumAB1[aidx] += ab1;
296
297 fSectorSumAB0[aidx] += ab0;
298 fSectorSumAB1[aidx] += ab1;
299 }
300
301 fPedestalsOut->SetReadyToSave();
302
303 return kTRUE;
304}
305
306
307void MPedCalcPedRun::CalcExtractor( MRawEvtPixelIter *pixel, Float_t &sum, MPedestalPix &ped)
308{
309
310 Byte_t sat = 0;
311 Byte_t *first = pixel->GetHiGainSamples() + fExtractWinFirst;
312 Byte_t *logain = pixel->GetLoGainSamples();
313 const Bool_t abflag = pixel->HasABFlag();
314 Float_t dummy;
315 fExtractor->FindTimeAndChargeHiGain(first,logain,sum,dummy,dummy,dummy,sat,ped,abflag);
316
317}
318
319
320void MPedCalcPedRun::CalcSums( MRawEvtPixelIter *pixel, Float_t &sum, UInt_t &ab0, UInt_t &ab1)
321{
322
323 Byte_t *higain = pixel->GetHiGainSamples() + fExtractWinFirst;
324 Byte_t *ptr = higain;
325 Byte_t *end = ptr + fExtractWinSize;
326
327 const Bool_t abflag = pixel->HasABFlag();
328
329 Int_t sumi = 0;
330
331 Int_t cnt = 0;
332 do
333 {
334 sumi += *ptr;
335 if (pixel->IsABFlagValid())
336 {
337 const Int_t abFlag = (fExtractWinFirst + abflag + cnt) & 0x1;
338 if (abFlag)
339 ab1 += *ptr;
340 else
341 ab0 += *ptr;
342
343 cnt++;
344 }
345 }
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 {
358 const Int_t abFlag = (fExtractWinFirst + abflag + cnt) & 0x1;
359 if (abFlag)
360 ab1 += *ptr;
361 else
362 ab0 += *ptr;
363
364 cnt++;
365 }
366 }
367 while (++ptr != end);
368 }
369
370 sum = (Float_t)sumi;
371
372}
373
374Int_t MPedCalcPedRun::PostProcess()
375{
376 Finalize();
377 return kTRUE;
378}
379
380// --------------------------------------------------------------------------
381//
382// Compute signal mean and rms in the whole run and store it in MPedestalCam
383//
384void MPedCalcPedRun::Finalize()
385{
386
387 if (fUsedEvents == 0)
388 return;
389
390 MRawEvtPixelIter pixel(fRawEvt);
391
392 while (pixel.Next())
393 {
394 const Int_t pixid = pixel.GetPixelId();
395 CalcPixResults(fUsedEvents,pixid);
396 }
397
398 //
399 // Loop over the (two) area indices to get the averaged pedestal per aidx
400 //
401 for (UInt_t aidx=0; aidx<fAreaValid.GetSize(); aidx++)
402 {
403 const Int_t napix = fAreaValid.At(aidx);
404 if (napix == 0)
405 continue;
406
407 CalcAreaResults(fUsedEvents,napix,aidx);
408 }
409
410 //
411 // Loop over the (six) sector indices to get the averaged pedestal per sector
412 //
413 for (UInt_t sector=0; sector<fSectorValid.GetSize(); sector++)
414 {
415 const Int_t nspix = fSectorValid.At(sector);
416 if (nspix == 0)
417 continue;
418
419 CalcSectorResults(fUsedEvents,nspix,sector);
420 }
421
422 fPedestalsOut->SetTotalEntries(fUsedEvents*fExtractWinSize);
423 fPedestalsOut->SetReadyToSave();
424
425 return;
426}
427
428//-------------------------------------------------------------
429//
430// Return if the pedestal bit was set from the calibration trigger box.
431// The last but one bit is used for the "pedestal-bit".
432//
433// This bit is set since run gkFinalizationTriggerBit
434//
435Bool_t MPedCalcPedRun::IsPedBitSet()
436{
437
438 if (fRunHeader->GetRunNumber() < gkFirstRunWithFinalBits)
439 return kFALSE;
440
441 return (fEvtHeader->GetTriggerID() >> 3 & 1);
442}
443
444void MPedCalcPedRun::Print(Option_t *o) const
445{
446
447 MExtractPedestal::Print(o);
448
449 *fLog << "Number overlap low-gain slices: " << fOverlap << endl;
450 *fLog << "First run out of sequence: " << (fFirstRun?"yes":"no") << endl;
451 *fLog << "Skip this run: " << (fSkip?"yes":"no") << endl;
452 *fLog << "Number of used events so far: " << fUsedEvents << endl;
453 *fLog << endl;
454}
Note: See TracBrowser for help on using the repository browser.