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

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