source: trunk/MagicSoft/Mars/mpedestal/MPedCalcFromLoGain.cc@ 8422

Last change on this file since 8422 was 8361, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 13.4 KB
Line 
1/* ======================================================================== *\
2! $Name: not supported by cvs2svn $:$Id: MPedCalcFromLoGain.cc,v 1.37 2007-03-04 12:00:30 tbretz Exp $
3! --------------------------------------------------------------------------
4!
5! *
6! * This file is part of MARS, the MAGIC Analysis and Reconstruction
7! * Software. It is distributed to you in the hope that it can be a useful
8! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
9! * It is distributed WITHOUT ANY WARRANTY.
10! *
11! * Permission to use, copy, modify and distribute this software and its
12! * documentation for any purpose is hereby granted without fee,
13! * provided that the above copyright notice appear in all copies and
14! * that both that copyright notice and this permission notice appear
15! * in supporting documentation. It is provided "as is" without express
16! * or implied warranty.
17! *
18!
19!
20! Author(s): Josep Flix 04/2001 <mailto:jflix@ifae.es>
21! Author(s): Thomas Bretz 05/2001 <mailto:tbretz@astro.uni-wuerzburg.de>
22! Author(s): Sebastian Commichau 12/2003
23! Author(s): Javier Rico 01/2004 <mailto:jrico@ifae.es>
24! Author(s): Markus Gaug 01/2004 <mailto:markus@ifae.es>
25! Author(s): Florian Goebel 06/2004 <mailto:fgoebel@mppmu.mpg.de>
26! Author(s): Nepomuk Otte 10/2004 <mailto:otte@mppmu.mpg.de>
27!
28! Copyright: MAGIC Software Development, 2000-2006
29!
30!
31\* ======================================================================== */
32
33/////////////////////////////////////////////////////////////////////////////
34//
35// MPedCalcLoGain
36//
37// This task derives from MExtractPedestal.
38// It calculates the pedestals using the low gain slices, whenever the difference
39// between the highest and the lowest slice in the high gain
40// slices is below a given threshold (SetMaxHiGainVar). In this case the receiver
41// boards do not switch to lo gain and the so called lo gain slices are actually
42// high gain slices.
43//
44// MPedCalcLoGain also fills the ABoffset in MPedestalPix which allows to correct
45// the 150 MHz clock noise.
46//
47// This task takes a pedestal run file and fills MPedestalCam during
48// the Process() with the pedestal and rms computed in an event basis.
49// In the PostProcess() MPedestalCam is finally filled with the pedestal
50// mean and rms computed in a run basis.
51// More than one run (file) can be merged
52//
53// MPedCalcPedRun applies the following formula (1):
54//
55// Pedestal per slice = sum(x_i) / n / slices
56// PedRMS per slice = Sqrt( ( sum(x_i^2) - sum(x_i)^2/n ) / n-1 / slices )
57//
58// where x_i is the sum of "slices" FADC slices and sum means the sum over all
59// events. "n" is the number of events, "slices" is the number of summed FADC samples.
60//
61// Note that the slice-to-slice fluctuations are not Gaussian, but Poissonian, thus
62// asymmetric and they are correlated.
63//
64// It is important to know that the Pedestal per slice and PedRMS per slice depend
65// on the number of used FADC slices, as seen in the following plots:
66//
67//Begin_Html
68/*
69<img src="images/PedestalStudyInner.gif">
70*/
71//End_Html
72//
73//Begin_Html
74/*
75<img src="images/PedestalStudyOuter.gif">
76*/
77//End_Html
78//
79// The plots show the inner and outer pixels, respectivly and have the following meaning:
80//
81// 1) The calculated mean pedestal per slice (from MPedCalcFromLoGain)
82// 2) The fitted mean pedestal per slice (from MHPedestalCam)
83// 3) The calculated pedestal RMS per slice (from MPedCalcFromLoGain)
84// 4) The fitted sigma of the pedestal distribution per slice
85// (from MHPedestalCam)
86// 5) The relative difference between calculation and histogram fit
87// for the mean
88// 6) The relative difference between calculation and histogram fit
89// for the sigma or RMS, respectively.
90//
91// The calculated means do not change significantly except for the case of 2 slices,
92// however the RMS changes from 5.7 per slice in the case of 2 extracted slices
93// to 8.3 per slice in the case of 26 extracted slices. This change is very significant.
94//
95// The plots have been produced on run 20123. You can reproduce them using
96// the macro pedestalstudies.C
97//
98// Usage of this class:
99// ====================
100//
101//
102// fCheckWinFirst = fgCheckWinFirst = 0
103// fCheckWinLast = fgCheckWinLast = 29
104// fExtractWinFirst = fgExtractWinFirst = 17
105// fExtractWinSize = fgExtractWinSize = 6
106// fMaxSignalVar = fgMaxSignalVar = 40;
107//
108// Call:
109// SetCheckRange(fCheckWinFirst,fCheckWinLast);
110// to set the Window in which a signal is searched
111//
112// SetExtractWindow(fExtractWinFirst,fExtractWinSize);
113// to set the Window from which a signal is extracted
114//
115// SetMaxSignalVar(fMaxSignalVar);
116// set the maximum allowed difference between maximum and minimal signal in CheckWindow
117//
118// Variables:
119// fgCheckWinFirst; First FADC slice to check for signal (currently set to: 0)
120// fgCheckWinLast: Last FADC slice to check for signal (currently set to: 29)
121// fgExtractWinFirst: First FADC slice to be used for pedestal extraction (currently set to: 15)
122// fgExtractWinSize: Window size in slices used for pedestal extraction (currently set to: 6)
123// fgMaxSignalVar: The maximum difference between the highest and lowest slice
124// in the check window allowed in order to use event
125//
126// Input Containers:
127// MRawEvtData
128// MRawRunHeader
129// MGeomCam
130//
131// Output Containers:
132// MPedestalCam
133//
134// See also: MPedestalCam, MPedestalPix, MHPedestalCam, MExtractor
135//
136/////////////////////////////////////////////////////////////////////////////
137#include "MPedCalcFromLoGain.h"
138
139#include "MParList.h"
140
141#include "MLog.h"
142#include "MLogManip.h"
143
144#include "MRawRunHeader.h"
145#include "MRawEvtPixelIter.h"
146
147#include "MPedestalPix.h"
148#include "MPedestalCam.h"
149
150#include "MGeomPix.h"
151#include "MGeomCam.h"
152
153#include "MExtractPedestal.h"
154#include "MPedestalSubtractedEvt.h"
155
156ClassImp(MPedCalcFromLoGain);
157
158using namespace std;
159
160const UShort_t MPedCalcFromLoGain::fgExtractWinFirst = 17;
161const UShort_t MPedCalcFromLoGain::fgExtractWinSize = 6;
162const UInt_t MPedCalcFromLoGain::fgNumDump = 500;
163
164// --------------------------------------------------------------------------
165//
166// Default constructor:
167//
168// Calls:
169// - SetExtractWindow(fgExtractWinFirst, fgExtractWinSize)
170//
171MPedCalcFromLoGain::MPedCalcFromLoGain(const char *name, const char *title)
172{
173 fName = name ? name : "MPedCalcFromLoGain";
174 fTitle = title ? title : "Task to calculate pedestals from lo-gains";
175
176 SetExtractWindow(fgExtractWinFirst, fgExtractWinSize);
177 SetPedestalUpdate(kTRUE);
178 SetNumDump();
179}
180
181// --------------------------------------------------------------------------
182//
183// Call MExtractPedestl::ResetArrays aand reset fNumAventsUsed and
184// fTotalCounter
185//
186void MPedCalcFromLoGain::ResetArrays()
187{
188 MExtractPedestal::ResetArrays();
189
190 fNumEventsUsed.Reset();
191 fTotalCounter.Reset();
192}
193
194// ---------------------------------------------------------------------------------
195//
196// Checks:
197// - if the number of available slices
198// (fRunHeader->GetNumSamplesHiGain()+(Int_t)fRunHeader->GetNumSamplesLoGain()-1)
199// is smaller than the number of used slices
200// (fExtractWinSize+ fExtractWinFirst-1) or
201// fCheckWinLast
202//
203Bool_t MPedCalcFromLoGain::ReInit(MParList *pList)
204{
205 // If the size is not yet set, set the size
206 if (fSumx.GetSize()==0)
207 {
208 const Int_t npixels = fPedestalsOut->GetSize();
209 fNumEventsUsed.Set(npixels);
210 fTotalCounter.Set(npixels);
211 }
212
213 if (!MExtractPedestal::ReInit(pList))
214 return kFALSE;
215
216 const Int_t nhi = fRunHeader->GetNumSamplesHiGain();
217 const Int_t nlo = fRunHeader->GetNumSamplesLoGain();
218 CheckExtractionWindow(nlo>0?nhi:0);
219
220 return kTRUE;
221}
222
223// --------------------------------------------------------------------------
224//
225// Fill the MPedestalCam container with the signal mean and rms for the event.
226// Store the measured signal in arrays fSumx and fSumx2 so that we can
227// calculate the overall mean and rms in the PostProcess()
228//
229Int_t MPedCalcFromLoGain::Calc()
230{
231 const Int_t nhi = fRunHeader->GetNumSamplesHiGain();
232 const Int_t nlo = fRunHeader->GetNumSamplesLoGain();
233
234 // Real Process
235 MRawEvtPixelIter pixel(fRawEvt);
236 while (pixel.Next())
237 {
238 const UInt_t idx = pixel.GetPixelId();
239
240 if (!CheckVariation(idx))
241 continue;
242
243 //extract pedestal
244 UInt_t ab[2];
245 const Float_t sum = fExtractor ?
246 CalcExtractor(pixel, nlo>0?nhi:0) :
247 CalcSums(pixel, nlo>0?nhi:0, ab[0], ab[1]);
248
249 const UInt_t aidx = (*fGeom)[idx].GetAidx();
250 const UInt_t sector = (*fGeom)[idx].GetSector();
251
252 const Float_t sqrsum = sum*sum;
253
254 fSumx[idx] += sum;
255 fSumx2[idx] += sqrsum;
256 fAreaSumx[aidx] += sum;
257 fAreaSumx2[aidx] += sqrsum;
258 fSectorSumx[sector] += sum;
259 fSectorSumx2[sector] += sqrsum;
260
261 if (fIntermediateStorage)
262 (*fPedestalsInter)[idx].Set(sum, 0, 0, fNumEventsUsed[idx]);
263
264 fNumEventsUsed[idx] ++;
265 fAreaFilled [aidx] ++;
266 fSectorFilled [sector]++;
267
268 if (!fExtractor && pixel.IsABFlagValid())
269 {
270 fSumAB0[idx] += ab[0];
271 fSumAB1[idx] += ab[1];
272 fAreaSumAB0[aidx] += ab[0];
273 fAreaSumAB1[aidx] += ab[1];
274 fSectorSumAB0[aidx] += ab[0];
275 fSectorSumAB1[aidx] += ab[1];
276 }
277
278 if (!fPedestalUpdate || (UInt_t)fNumEventsUsed[idx]<fNumEventsDump)
279 continue;
280
281 CalcPixResults(fNumEventsDump, idx);
282 fTotalCounter[idx]++;
283
284 fNumEventsUsed[idx]=0;
285 fSumx[idx]=0;
286 fSumx2[idx]=0;
287 fSumAB0[idx]=0;
288 fSumAB1[idx]=0;
289 }
290
291 if (!(GetNumExecutions() % fNumAreasDump))
292 CalcAreaResult();
293
294 if (!(GetNumExecutions() % fNumSectorsDump))
295 CalcSectorResult();
296
297 if (fPedestalUpdate)
298 fPedestalsOut->SetReadyToSave();
299
300 return kTRUE;
301}
302
303// --------------------------------------------------------------------------
304//
305// Loop over the sector indices to get the averaged pedestal per sector
306//
307void MPedCalcFromLoGain::CalcSectorResult()
308{
309 for (UInt_t sector=0; sector<fSectorFilled.GetSize(); sector++)
310 if (fSectorValid[sector]>0)
311 CalcSectorResults(fSectorFilled[sector], fSectorValid[sector], sector);
312}
313
314// --------------------------------------------------------------------------
315//
316// Loop over the (two) area indices to get the averaged pedestal per aidx
317//
318void MPedCalcFromLoGain::CalcAreaResult()
319{
320 for (UInt_t aidx=0; aidx<fAreaFilled.GetSize(); aidx++)
321 if (fAreaValid[aidx]>0)
322 CalcAreaResults(fAreaFilled[aidx], fAreaValid[aidx], aidx);
323
324}
325
326// --------------------------------------------------------------------------
327//
328// Compute signal mean and rms in the whole run and store it in MPedestalCam
329//
330Int_t MPedCalcFromLoGain::PostProcess()
331{
332 // Compute pedestals and rms from the whole run
333 if (fPedestalUpdate)
334 return kTRUE;
335
336 *fLog << flush << inf << "Calculating Pedestals..." << flush;
337
338 const Int_t npix = fGeom->GetNumPixels();
339 for (Int_t idx=0; idx<npix; idx++)
340 {
341 const ULong_t n = fNumEventsUsed[idx];
342 if (n>1)
343 {
344 CalcPixResults(n, idx);
345 fTotalCounter[idx]++;
346 }
347 }
348
349 CalcAreaResult();
350 CalcSectorResult();
351
352 fPedestalsOut->SetReadyToSave();
353
354 return MExtractPedestal::PostProcess();
355}
356
357// --------------------------------------------------------------------------
358//
359// The following resources are available:
360//
361Int_t MPedCalcFromLoGain::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
362{
363 Int_t rc=MExtractPedestal::ReadEnv(env, prefix, print);
364 if (rc==kERROR)
365 return kERROR;
366
367 // find resource for pedestal update
368 if (IsEnvDefined(env, prefix, "PedestalUpdate", print))
369 {
370 SetPedestalUpdate(GetEnvValue(env, prefix, "PedestalUpdate", fPedestalUpdate));
371 rc = kTRUE;
372 }
373
374 // find resource for numdump
375 if (IsEnvDefined(env, prefix, "NumDump", print))
376 {
377 const Int_t num = GetEnvValue(env, prefix, "NumDump", -1);
378 if (num<=0)
379 {
380 *fLog << err << GetDescriptor() << ": ERROR - NumDump invalid!" << endl;
381 return kERROR;
382 }
383
384 SetNumDump(num);
385 rc = kTRUE;
386 }
387
388 // find resource for numeventsdump
389 if (IsEnvDefined(env, prefix, "NumEventsDump", print))
390 {
391 SetNumEventsDump(GetEnvValue(env, prefix, "NumEventsDump", (Int_t)fNumEventsDump));
392 rc = kTRUE;
393 }
394
395 // find resource for numeventsdump
396 if (IsEnvDefined(env, prefix, "NumAreasDump", print))
397 {
398 SetNumAreasDump(GetEnvValue(env, prefix, "NumAreasDump", (Int_t)fNumAreasDump));
399 rc = kTRUE;
400 }
401
402 // find resource for numeventsdump
403 if (IsEnvDefined(env, prefix, "NumSectorsDump", print))
404 {
405 SetNumSectorsDump(GetEnvValue(env, prefix, "NumSectorsDump", (Int_t)fNumSectorsDump));
406 rc = kTRUE;
407 }
408
409 return rc;
410}
411
412void MPedCalcFromLoGain::Print(Option_t *o) const
413{
414 MExtractPedestal::Print(o);
415
416 *fLog << "Pedestal Update is " << (fPedestalUpdate?"on":"off") << endl;
417 if (fPedestalUpdate)
418 {
419 *fLog << "Num evts for pedestal calc: " << fNumEventsDump << endl;
420 *fLog << "Num evts for avg.areas calc: " << fNumAreasDump << endl;
421 *fLog << "Num evts for avg.sector calc: " << fNumSectorsDump << endl;
422 }
423}
Note: See TracBrowser for help on using the repository browser.