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

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