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

Last change on this file since 8405 was 8296, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 11.9 KB
Line 
1/* ======================================================================== *\
2! $Name: not supported by cvs2svn $:$Id: MPedCalcPedRun.cc,v 1.50 2007-02-03 20:03:35 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!
26! Copyright: MAGIC Software Development, 2000-2006
27!
28!
29\* ======================================================================== */
30/////////////////////////////////////////////////////////////////////////////
31//
32// MPedCalcPedRun
33//
34// This task takes a pedestal run file and fills MPedestalCam during
35// the Process() with the pedestal and rms computed in an event basis.
36// In the PostProcess() MPedestalCam is finally filled with the pedestal
37// mean and rms computed in a run basis.
38// More than one run (file) can be merged
39//
40// MPedCalcPedRun applies the following formula (1):
41//
42// Pedestal per slice = sum(x_i) / n / slices
43// PedRMS per slice = Sqrt( ( sum(x_i^2) - sum(x_i)^2/n ) / n-1 / slices )
44//
45// where x_i is the sum of "slices" FADC slices and sum means the sum over all
46// events. "n" is the number of events, "slices" is the number of summed FADC samples.
47//
48// Note that the slice-to-slice fluctuations are not Gaussian, but Poissonian, thus
49// asymmetric and they are correlated.
50//
51// It is important to know that the Pedestal per slice and PedRMS per slice depend
52// on the number of used FADC slices, as seen in the following plots:
53//
54//Begin_Html
55/*
56<img src="images/PedestalStudyInner.gif">
57*/
58//End_Html
59//
60//Begin_Html
61/*
62<img src="images/PedestalStudyOuter.gif">
63*/
64//
65// The plots show the inner and outer pixels, respectivly and have the following meaning:
66//
67// 1) The calculated mean pedestal per slice (from MPedCalcPedRun)
68// 2) The fitted mean pedestal per slice (from MHPedestalCam)
69// 3) The calculated pedestal RMS per slice (from MPedCalcPedRun)
70// 4) The fitted sigma of the pedestal distribution per slice
71// (from MHPedestalCam)
72// 5) The relative difference between calculation and histogram fit
73// for the mean
74// 6) The relative difference between calculation and histogram fit
75// for the sigma or RMS, respectively.
76//
77// The calculated means do not change significantly except for the case of 2 slices,
78// however the RMS changes from 5.7 per slice in the case of 2 extracted slices
79// to 8.3 per slice in the case of 26 extracted slices. This change is very significant.
80//
81// The plots have been produced on run 20123. You can reproduce them using
82// the macro pedestalstudies.C
83//
84// Usage of this class:
85// ====================
86//
87// Call: SetRange(higainfirst, higainlast, logainfirst, logainlast)
88// to modify the ranges in which the window is allowed to move.
89// Defaults are:
90//
91// fHiGainFirst = fgHiGainFirst = 0
92// fHiGainLast = fgHiGainLast = 29
93// fLoGainFirst = fgLoGainFirst = 0
94// fLoGainLast = fgLoGainLast = 14
95//
96// Call: SetWindowSize(windowhigain, windowlogain)
97// to modify the sliding window widths. Windows have to be an even number.
98// In case of odd numbers, the window will be modified.
99//
100// Defaults are:
101//
102// fHiGainWindowSize = fgHiGainWindowSize = 14
103// fLoGainWindowSize = fgLoGainWindowSize = 0
104//
105//
106// ToDo:
107// - Take a setup file (ReadEnv-implementation) as input
108//
109//
110// Input Containers:
111// MRawEvtData
112// MRawRunHeader
113// MRawEvtHeader
114// MGeomCam
115//
116// Output Containers:
117// MPedestalCam
118//
119// See also: MPedestalCam, MPedestalPix, MHPedestalCam, MExtractor
120//
121/////////////////////////////////////////////////////////////////////////////
122#include "MPedCalcPedRun.h"
123
124#include "MParList.h"
125
126#include "MLog.h"
127#include "MLogManip.h"
128
129#include "MRawRunHeader.h"
130#include "MRawEvtHeader.h"
131#include "MRawEvtPixelIter.h"
132#include "MRawEvtData.h"
133
134#include "MPedestalPix.h"
135#include "MPedestalCam.h"
136
137#include "MGeomPix.h"
138#include "MGeomCam.h"
139
140#include "MExtractPedestal.h"
141#include "MPedestalSubtractedEvt.h"
142
143#include "MTriggerPattern.h"
144
145ClassImp(MPedCalcPedRun);
146
147using namespace std;
148
149const UShort_t MPedCalcPedRun::fgExtractWinFirst = 0;
150const UShort_t MPedCalcPedRun::fgExtractWinSize = 6;
151
152const UInt_t MPedCalcPedRun::gkFirstRunWithFinalBits = 45605;
153
154// --------------------------------------------------------------------------
155//
156// Default constructor:
157//
158// Calls:
159// - SetExtractWindow(fgExtractWinFirst, fgExtractWinSize)
160//
161MPedCalcPedRun::MPedCalcPedRun(const char *name, const char *title)
162 : fIsFirstPedRun(kFALSE), fUsedEvents(0), fTrigPattern(NULL)
163{
164 fName = name ? name : "MPedCalcPedRun";
165 fTitle = title ? title : "Task to calculate pedestals from pedestal runs raw data";
166
167 SetExtractWindow(fgExtractWinFirst, fgExtractWinSize);
168}
169
170// --------------------------------------------------------------------------
171//
172// Call MExtractPedestal::reset and set fUsedEvents to 0.
173//
174void MPedCalcPedRun::Reset()
175{
176 MExtractPedestal::ResetArrays();
177 fUsedEvents = 0;
178}
179
180// --------------------------------------------------------------------------
181//
182// Set fIsFirstPedRun=kTRUE
183//
184Int_t MPedCalcPedRun::PreProcess(MParList *pList)
185{
186 fUsedEvents = 0;
187 fIsFirstPedRun = kTRUE;
188 fIsNotPedRun = kFALSE;
189
190 fTrigPattern = (MTriggerPattern*)pList->FindObject("MTriggerPattern");
191 if (!fTrigPattern)
192 *fLog << inf << "MTriggerPattern not found... Cannot use interlaced pedestal events." << endl;
193
194 return MExtractPedestal::PreProcess(pList);
195}
196
197// --------------------------------------------------------------------------
198//
199// The run type is checked for "kRTPedestal"
200// and the variable fIsNotPedRun is set in that case
201//
202Bool_t MPedCalcPedRun::ReInit(MParList *pList)
203{
204 if (!MExtractPedestal::ReInit(pList))
205 return kFALSE;
206
207 CheckExtractionWindow();
208
209 //
210 // If this is the first ped run, the next run (call to ReInit)
211 // is not the first anymore
212 //
213 switch (fRunHeader->GetRunType())
214 {
215 case MRawRunHeader::kRTPedestal:
216 case MRawRunHeader::kRTMonteCarlo:
217 fIsFirstPedRun = kFALSE;
218 fIsNotPedRun = kFALSE;
219 return kTRUE;
220
221 case MRawRunHeader::kRTCalibration:
222 {
223 TString proj(fRunHeader->GetProjectName());
224 proj.ToLower();
225
226 // test if a continuous light run has been declared as calibration...
227 if (proj.Contains("cl"))
228 {
229 fIsFirstPedRun = kFALSE;
230 fIsNotPedRun = kFALSE;
231 return kTRUE;
232 }
233 }
234 }
235
236 fIsNotPedRun = kTRUE;
237
238 //
239 // If this is the first call to ReInit (before reading the first file)
240 // nothing should be done. It occurs for the case that the first treated
241 // file is not of pedestal type.
242 //
243 if (fIsFirstPedRun)
244 return kTRUE;
245
246 //
247 // In the other case, produce the MPedestalCam to be use the subsequent
248 // (non-pedestal) events
249 //
250 *fLog << inf << "Finalizing pedestal calculations..." << flush;
251
252 if (!Finalize())
253 return kFALSE;
254
255 Reset();
256
257 return kTRUE;
258}
259
260// --------------------------------------------------------------------------
261//
262// Return kTRUE (without doing anything) in case that the run type is not
263// equal to 1 (pedestal run)
264//
265// Fill the MPedestalCam container with the signal mean and rms for the event.
266// Store the measured signal in arrays fSumx and fSumx2 so that we can
267// calculate the overall mean and rms in the PostProcess()
268//
269Int_t MPedCalcPedRun::Calc()
270{
271 if (fIsNotPedRun && !IsPedBitSet())
272 return kTRUE;
273
274 fUsedEvents++;
275
276 MRawEvtPixelIter pixel(fRawEvt);
277 while (pixel.Next())
278 {
279 const UInt_t idx = pixel.GetPixelId();
280
281 if (!CheckVariation(idx))
282 continue;
283
284 //extract pedestal
285 UInt_t ab[2];
286 const Float_t sum = fExtractor ?
287 CalcExtractor(pixel, 0) :
288 CalcSums(pixel, 0, ab[0], ab[1]);
289
290 if (fIntermediateStorage)
291 (*fPedestalsInter)[idx].Set(sum, 0, 0, fUsedEvents);
292
293 const Float_t sqrsum = sum*sum;
294
295 fSumx[idx] += sum;
296 fSumx2[idx] += sqrsum;
297
298 fSumAB0[idx] += ab[0];
299 fSumAB1[idx] += ab[1];
300
301 if (fUseSpecialPixels)
302 continue;
303
304 const UInt_t aidx = (*fGeom)[idx].GetAidx();
305 const UInt_t sector = (*fGeom)[idx].GetSector();
306
307 fAreaSumx[aidx] += sum;
308 fAreaSumx2[aidx] += sqrsum;
309 fSectorSumx[sector] += sum;
310 fSectorSumx2[sector] += sqrsum;
311
312 fAreaSumAB0[aidx] += ab[0];
313 fAreaSumAB1[aidx] += ab[1];
314 fSectorSumAB0[aidx] += ab[0];
315 fSectorSumAB1[aidx] += ab[1];
316 }
317
318 fPedestalsOut->SetReadyToSave();
319
320 return kTRUE;
321}
322
323// --------------------------------------------------------------------------
324//
325// Compute signal mean and rms in the whole run and store it in MPedestalCam
326//
327Int_t MPedCalcPedRun::Finalize()
328{
329 if (fUsedEvents == 0)
330 return kTRUE;
331
332 //
333 // Necessary check for extraction of special pixels
334 // together with data which does not yet have them
335 //
336 if (fSumx.GetSize()==0)
337 return kTRUE;
338
339 MRawEvtPixelIter pixel(fRawEvt);
340 while (pixel.Next())
341 CalcPixResults(fUsedEvents, pixel.GetPixelId());
342
343 if (!fUseSpecialPixels)
344 {
345
346 //
347 // Loop over the (two) area indices to get the averaged pedestal per aidx
348 //
349 for (UInt_t aidx=0; aidx<fAreaValid.GetSize(); aidx++)
350 if (fAreaValid[aidx]>0)
351 CalcAreaResults(fUsedEvents, fAreaValid[aidx], aidx);
352
353 //
354 // Loop over the (six) sector indices to get the averaged pedestal per sector
355 //
356 for (UInt_t sector=0; sector<fSectorValid.GetSize(); sector++)
357 if (fSectorValid[sector]>0)
358 CalcSectorResults(fUsedEvents, fSectorValid[sector], sector);
359 }
360
361 fPedestalsOut->SetTotalEntries(fUsedEvents*fExtractWinSize);
362 fPedestalsOut->SetReadyToSave();
363
364 return kTRUE;
365}
366
367//-----------------------------------------------------------------------
368//
369// PostProcess MExtractPedestal and call Finalize
370//
371Int_t MPedCalcPedRun::PostProcess()
372{
373 if (!fRawEvt)
374 return kTRUE;
375
376 if (!Finalize())
377 return kFALSE;
378
379 return MExtractPedestal::PostProcess();
380}
381
382//-----------------------------------------------------------------------
383//
384// Return if the pedestal bit was set from the calibration trigger box.
385// The last but one bit is used for the "pedestal-bit".
386//
387// This bit is set since run gkFirstRunWithFinalBits
388//
389Bool_t MPedCalcPedRun::IsPedBitSet()
390{
391 if (fRunHeader->GetRunNumber()<gkFirstRunWithFinalBits)
392 return kFALSE;
393
394 if (!fTrigPattern)
395 return kFALSE;
396
397 return (fTrigPattern->GetPrescaled() & MTriggerPattern::kPedestal) ? kTRUE : kFALSE;
398}
399
400//-----------------------------------------------------------------------
401//
402void MPedCalcPedRun::Print(Option_t *o) const
403{
404 MExtractPedestal::Print(o);
405
406 *fLog << "First pedrun out of sequence: " << (fIsFirstPedRun?"yes":"no") << endl;
407 *fLog << "Number of used events so far: " << fUsedEvents << endl;
408}
Note: See TracBrowser for help on using the repository browser.