source: trunk/MagicSoft/Mars/manalysis/MPedCalcPedRun.cc@ 2997

Last change on this file since 2997 was 2997, checked in by gaug, 21 years ago
*** empty log message ***
File size: 8.8 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
22!
23! Copyright: MAGIC Software Development, 2000-2004
24!
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//
39// Actually, MPedCalcPedRun applies the following formula (1):
40//
41// PedRMS = Sqrt( (sum(x_i^2) - sum(x_i)/n) / n-1 / 14 )
42//
43// where x_i is the sum of 14 FADC slices and sum means the sum over all
44// events, n is the number of events.
45//
46// For a high number of events, this formula is equivalent to formula (2):
47//
48// PedRMS = Sqrt( (<x_i*x_i> - <x_i>*<x_i>*n) / 14 )
49//
50// where <> is the mean over all events and x_i again the sum over the 14
51// slices.
52//
53// If you assume statistical equivalence of all slices (say, all have equal
54// offset and are not correlated and fluctuate Gaussian), it should also be
55// equivalent to (old formula) (3):
56//
57// PedRMS = Sqrt( (<p_i*p_i> - <p_i>*<p_i>*m) / m ) * Sqrt(14)
58//
59// which is the RMS of a single slice (p_i) with m being the total number of
60// measurements, i.e. m = n*14, later re-scaled to the number of used slices
61// (the factor sqrt(14)).
62//
63// If we assume that at least our pairs fluctuate independently and Gaussian,
64// then we can use the actual formula (1) in order to get what you call
65// fluctuations of pairs by the transformation:
66//
67// PedRMS/pair = PedRMS (form. (3)) / Sqrt(7)
68//
69// (However, we know that our slice-to-slice fluctuations are not Gaussian
70// (and moreover asymmetric) and that they are also correlated.)
71//
72// We could still measure also the pair-to-pair fluctuations and add another
73// value to be investigated. What do you think?
74//
75//
76// Input Containers:
77// MRawEvtData
78//
79// Output Containers:
80// MPedestalCam
81//
82//
83/////////////////////////////////////////////////////////////////////////////
84#include "MPedCalcPedRun.h"
85
86#include "MParList.h"
87
88#include "MLog.h"
89#include "MLogManip.h"
90
91#include "MRawRunHeader.h"
92#include "MRawEvtPixelIter.h"
93#include "MRawEvtData.h"
94
95#include "MPedestalPix.h"
96#include "MPedestalCam.h"
97
98#include "MExtractedSignalPix.h"
99#include "MExtractedSignalCam.h"
100
101
102#include "MGeomCamMagic.h"
103
104ClassImp(MPedCalcPedRun);
105
106using namespace std;
107
108// --------------------------------------------------------------------------
109//
110// default constructor
111//
112MPedCalcPedRun::MPedCalcPedRun(const char *name, const char *title)
113 : fRawEvt(NULL), fPedestals(NULL), fSignals(NULL)
114{
115 fName = name ? name : "MPedCalcPedRun";
116 fTitle = title ? title : "Task to calculate pedestals from pedestal runs raw data";
117
118 AddToBranchList("fHiGainPixId");
119 AddToBranchList("fHiGainFadcSamples");
120
121 Clear();
122}
123
124void MPedCalcPedRun::Clear(const Option_t *o)
125{
126
127 fNumHiGainSamples = 0;
128 fNumPixels = 0;
129 fNumSamplesTot = 0;
130 fUseHists = kFALSE;
131
132 fRawEvt = NULL;
133 fPedestals = NULL;
134 fSignals = NULL;
135
136 return;
137
138}
139
140
141// --------------------------------------------------------------------------
142//
143// Look for the following input containers:
144//
145// - MRawEvtData
146//
147// The following output containers are also searched and created if
148// they were not found:
149//
150// - MPedestalCam
151//
152Int_t MPedCalcPedRun::PreProcess( MParList *pList )
153{
154 fRawEvt = (MRawEvtData*)pList->FindObject("MRawEvtData");
155 if (!fRawEvt)
156 {
157 *fLog << err << "MRawEvtData not found... aborting." << endl;
158 return kFALSE;
159 }
160
161 fPedestals = (MPedestalCam*)pList->FindCreateObj("MPedestalCam");
162 if (!fPedestals)
163 return kFALSE;
164
165 fSignals = (MExtractedSignalCam*)pList->FindObject("MExtractedSignalCam");
166
167 if (!fSignals && fUseHists)
168 {
169 *fLog << warn << "Cannot find MExtractedSignalCam... will not use histograms!" << endl;
170 fUseHists = kFALSE;
171 }
172
173 fNumSamplesTot=0;
174
175 return kTRUE;
176}
177
178// --------------------------------------------------------------------------
179//
180// The ReInit searches for the following input containers:
181// - MRawRunHeader
182//
183// It also initializes the data arrays fSumx and fSumx2
184// (only for the first read file)
185//
186Bool_t MPedCalcPedRun::ReInit(MParList *pList)
187{
188 const MRawRunHeader *runheader = (MRawRunHeader*)pList->FindObject("MRawRunHeader");
189 if (!runheader)
190 {
191 *fLog << warn << dbginf;
192 *fLog << "Warning - cannot check file type, MRawRunHeader not found." << endl;
193 }
194 else
195 if (runheader->GetRunType() == kRTMonteCarlo)
196 return kTRUE;
197
198 fNumPixels = fPedestals->GetSize();
199
200 if(fSumx.GetSize()==0)
201 {
202 fSumx.Set(fNumPixels);
203 fSumx2.Set(fNumPixels);
204
205 fSumx.Reset();
206 fSumx2.Reset();
207 }
208
209 // Calculate an even number for the hi gain samples to avoid
210 // biases due to the fluctuation in pedestal from one slice to
211 // the other one
212 fNumHiGainSamples = runheader->GetNumSamplesHiGain() & ~1;
213
214 if (fUseHists)
215 fPedestals->InitUseHists();
216
217 return kTRUE;
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 MPedCalcPedRun::Process()
226{
227 MRawEvtPixelIter pixel(fRawEvt);
228
229 while (pixel.Next())
230 {
231 Byte_t *ptr = pixel.GetHiGainSamples();
232 const Byte_t *end = ptr + fNumHiGainSamples;
233
234 UInt_t sum = 0;
235 UInt_t sqr = 0;
236
237 do
238 {
239 sum += *ptr;
240 sqr += *ptr * *ptr;
241 }
242 while (++ptr != end);
243
244 const Float_t msum = (Float_t)sum;
245
246 const UInt_t idx = pixel.GetPixelId();
247 //
248 // These three lines have been uncommented by Markus Gaug
249 // If anybody needs them, please contact me!!
250 //
251 // const Float_t higainped = msum/fNumHiGainSamples;
252 // const Float_t higainrms = TMath::Sqrt((msqr-msum*msum/fNumHiGainSamples)/(fNumHiGainSamples-1.));
253 // (*fPedestals)[idx].Set(higainped, higainrms);
254
255 fSumx[idx] += msum;
256 //
257 // The old version:
258 //
259 // const Float_t msqr = (Float_t)sqr;
260 // fSumx2[idx] += msqr;
261 //
262 // The new version:
263 //
264 fSumx2[idx] += msum*msum;
265
266 if (fUseHists)
267 {
268 MExtractedSignalPix &sig = (*fSignals)[idx];
269 const Float_t signal = sig.GetExtractedSignalHiGain();
270 (*fPedestals)[idx].FillHists(signal);
271 }
272 }
273
274 fPedestals->SetReadyToSave();
275 fNumSamplesTot += fNumHiGainSamples;
276
277
278 return kTRUE;
279}
280
281// --------------------------------------------------------------------------
282//
283// Compute signal mean and rms in the whole run and store it in MPedestalCam
284//
285Int_t MPedCalcPedRun::PostProcess()
286{
287
288 // Compute pedestals and rms from the whole run
289 const ULong_t n = fNumSamplesTot;
290 const ULong_t nevts = GetNumExecutions();
291
292 MRawEvtPixelIter pixel(fRawEvt);
293
294 while (pixel.Next())
295 {
296 const UInt_t pixid = pixel.GetPixelId();
297
298 const Float_t sum = fSumx.At(pixid);
299 const Float_t sum2 = fSumx2.At(pixid);
300
301 const Float_t higainped = sum/n;
302 //
303 // The old version:
304 //
305 // const Float_t higainrms = TMath::Sqrt((sum2-sum*sum/n)/(n-1.));
306 //
307 // The new version:
308 //
309 const Float_t higainrms = TMath::Sqrt((sum2-sum*sum/nevts)/(nevts-1.)/(Float_t)fNumHiGainSamples);
310
311 (*fPedestals)[pixid].Set(higainped, higainrms);
312
313 if (fUseHists)
314 (*fPedestals)[pixid].FitCharge();
315
316 }
317
318 fPedestals->SetNumTotSlices(fNumSamplesTot);
319
320 if (fUseHists)
321 fPedestals->SetNumExtractSlices(fSignals->GetNumUsedHiGainFADCSlices());
322
323 return kTRUE;
324}
325
326
Note: See TracBrowser for help on using the repository browser.