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

Last change on this file since 8154 was 8151, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 13.1 KB
Line 
1/* ======================================================================== *\
2! $Name: not supported by cvs2svn $:$Id: MPedCalcFromLoGain.cc,v 1.34 2006-10-24 07:58:13 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 return kTRUE;
217}
218
219// --------------------------------------------------------------------------
220//
221// Fill the MPedestalCam container with the signal mean and rms for the event.
222// Store the measured signal in arrays fSumx and fSumx2 so that we can
223// calculate the overall mean and rms in the PostProcess()
224//
225Int_t MPedCalcFromLoGain::Calc()
226{
227 // Real Process
228 MRawEvtPixelIter pixel(fRawEvt);
229
230 while (pixel.Next())
231 {
232 const UInt_t idx = pixel.GetPixelId();
233
234 if (!CheckVariation(idx))
235 continue;
236
237 //extract pedestal
238 UInt_t ab[2];
239 const Float_t sum = fExtractor ?
240 CalcExtractor(pixel, pixel.GetNumHiGainSamples()) :
241 CalcSums(pixel, pixel.GetNumHiGainSamples(), ab[0], ab[1]);
242
243 const UInt_t aidx = (*fGeom)[idx].GetAidx();
244 const UInt_t sector = (*fGeom)[idx].GetSector();
245
246 const Float_t sqrsum = sum*sum;
247
248 fSumx[idx] += sum;
249 fSumx2[idx] += sqrsum;
250 fAreaSumx[aidx] += sum;
251 fAreaSumx2[aidx] += sqrsum;
252 fSectorSumx[sector] += sum;
253 fSectorSumx2[sector] += sqrsum;
254
255 if (fIntermediateStorage)
256 (*fPedestalsInter)[idx].Set(sum, 0, 0, fNumEventsUsed[idx]);
257
258 fNumEventsUsed[idx] ++;
259 fAreaFilled [aidx] ++;
260 fSectorFilled [sector]++;
261
262 if (!fExtractor && pixel.IsABFlagValid())
263 {
264 fSumAB0[idx] += ab[0];
265 fSumAB1[idx] += ab[1];
266 fAreaSumAB0[aidx] += ab[0];
267 fAreaSumAB1[aidx] += ab[1];
268 fSectorSumAB0[aidx] += ab[0];
269 fSectorSumAB1[aidx] += ab[1];
270 }
271
272 if (!fPedestalUpdate || (UInt_t)fNumEventsUsed[idx]<fNumEventsDump)
273 continue;
274
275 CalcPixResults(fNumEventsDump, idx);
276 fTotalCounter[idx]++;
277
278 fNumEventsUsed[idx]=0;
279 fSumx[idx]=0;
280 fSumx2[idx]=0;
281 fSumAB0[idx]=0;
282 fSumAB1[idx]=0;
283 }
284
285 if (!(GetNumExecutions() % fNumAreasDump))
286 CalcAreaResult();
287
288 if (!(GetNumExecutions() % fNumSectorsDump))
289 CalcSectorResult();
290
291 if (fPedestalUpdate)
292 fPedestalsOut->SetReadyToSave();
293
294 return kTRUE;
295}
296
297// --------------------------------------------------------------------------
298//
299// Loop over the sector indices to get the averaged pedestal per sector
300//
301void MPedCalcFromLoGain::CalcSectorResult()
302{
303 for (UInt_t sector=0; sector<fSectorFilled.GetSize(); sector++)
304 if (fSectorValid[sector]>0)
305 CalcSectorResults(fSectorFilled[sector], fSectorValid[sector], sector);
306}
307
308// --------------------------------------------------------------------------
309//
310// Loop over the (two) area indices to get the averaged pedestal per aidx
311//
312void MPedCalcFromLoGain::CalcAreaResult()
313{
314 for (UInt_t aidx=0; aidx<fAreaFilled.GetSize(); aidx++)
315 if (fAreaValid[aidx]>0)
316 CalcAreaResults(fAreaFilled[aidx], fAreaValid[aidx], aidx);
317
318}
319
320// --------------------------------------------------------------------------
321//
322// Compute signal mean and rms in the whole run and store it in MPedestalCam
323//
324Int_t MPedCalcFromLoGain::PostProcess()
325{
326 // Compute pedestals and rms from the whole run
327 if (fPedestalUpdate)
328 return kTRUE;
329
330 *fLog << flush << inf << "Calculating Pedestals..." << flush;
331
332 const Int_t npix = fGeom->GetNumPixels();
333 for (Int_t idx=0; idx<npix; idx++)
334 {
335 const ULong_t n = fNumEventsUsed[idx];
336 if (n>1)
337 {
338 CalcPixResults(n, idx);
339 fTotalCounter[idx]++;
340 }
341 }
342
343 CalcAreaResult();
344 CalcSectorResult();
345
346 fPedestalsOut->SetReadyToSave();
347
348 return MExtractPedestal::PostProcess();
349}
350
351// --------------------------------------------------------------------------
352//
353// The following resources are available:
354//
355Int_t MPedCalcFromLoGain::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
356{
357 Bool_t rc=kFALSE;
358
359 // find resource for pedestal update
360 if (IsEnvDefined(env, prefix, "PedestalUpdate", print))
361 {
362 SetPedestalUpdate(GetEnvValue(env, prefix, "PedestalUpdate", fPedestalUpdate));
363 rc = kTRUE;
364 }
365
366 // find resource for numdump
367 if (IsEnvDefined(env, prefix, "NumDump", print))
368 {
369 const Int_t num = GetEnvValue(env, prefix, "NumDump", -1);
370 if (num<=0)
371 {
372 *fLog << err << GetDescriptor() << ": ERROR - NumDump invalid!" << endl;
373 return kERROR;
374 }
375
376 SetNumDump(num);
377 rc = kTRUE;
378 }
379
380 // find resource for numeventsdump
381 if (IsEnvDefined(env, prefix, "NumEventsDump", print))
382 {
383 SetNumEventsDump(GetEnvValue(env, prefix, "NumEventsDump", (Int_t)fNumEventsDump));
384 rc = kTRUE;
385 }
386
387 // find resource for numeventsdump
388 if (IsEnvDefined(env, prefix, "NumAreasDump", print))
389 {
390 SetNumAreasDump(GetEnvValue(env, prefix, "NumAreasDump", (Int_t)fNumAreasDump));
391 rc = kTRUE;
392 }
393
394 // find resource for numeventsdump
395 if (IsEnvDefined(env, prefix, "NumSectorsDump", print))
396 {
397 SetNumSectorsDump(GetEnvValue(env, prefix, "NumSectorsDump", (Int_t)fNumSectorsDump));
398 rc = kTRUE;
399 }
400
401 return rc;
402}
403
404void MPedCalcFromLoGain::Print(Option_t *o) const
405{
406 MExtractPedestal::Print(o);
407
408 *fLog << "Pedestal Update is " << (fPedestalUpdate?"on":"off") << endl;
409 if (fPedestalUpdate)
410 {
411 *fLog << "Num evts for pedestal calc: " << fNumEventsDump << endl;
412 *fLog << "Num evts for avg.areas calc: " << fNumAreasDump << endl;
413 *fLog << "Num evts for avg.sector calc: " << fNumSectorsDump << endl;
414 }
415}
Note: See TracBrowser for help on using the repository browser.