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

Last change on this file since 3720 was 3701, checked in by gaug, 21 years ago
*** empty log message ***
File size: 11.0 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#include "MGeomPix.h"
99#include "MGeomCam.h"
100
101#include "MGeomCamMagic.h"
102
103ClassImp(MPedCalcPedRun);
104
105using namespace std;
106
107// --------------------------------------------------------------------------
108//
109// default constructor
110//
111MPedCalcPedRun::MPedCalcPedRun(const char *name, const char *title)
112 : fRawEvt(NULL), fPedestals(NULL)
113{
114 fName = name ? name : "MPedCalcPedRun";
115 fTitle = title ? title : "Task to calculate pedestals from pedestal runs raw data";
116
117 AddToBranchList("fHiGainPixId");
118 AddToBranchList("fHiGainFadcSamples");
119
120 Clear();
121}
122
123void MPedCalcPedRun::Clear(const Option_t *o)
124{
125 fNumHiGainSamples = 0;
126 fNumSamplesTot = 0;
127
128 fRawEvt = NULL;
129 fPedestals = NULL;
130}
131
132
133// --------------------------------------------------------------------------
134//
135// Look for the following input containers:
136//
137// - MRawEvtData
138//
139// The following output containers are also searched and created if
140// they were not found:
141//
142// - MPedestalCam
143//
144Int_t MPedCalcPedRun::PreProcess( MParList *pList )
145{
146
147 Clear();
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 fGeom = (MGeomCam*)pList->FindObject("MGeomCam");
157 if (!fGeom)
158 {
159 *fLog << err << "MGeomCam not found... aborting." << endl;
160 return kFALSE;
161 }
162
163 fPedestals = (MPedestalCam*)pList->FindCreateObj("MPedestalCam");
164 if (!fPedestals)
165 return kFALSE;
166
167 return kTRUE;
168}
169
170// --------------------------------------------------------------------------
171//
172// The ReInit searches for the following input containers:
173// - MRawRunHeader
174//
175// It also initializes the data arrays fSumx and fSumx2
176// (only for the first read file)
177//
178Bool_t MPedCalcPedRun::ReInit(MParList *pList)
179{
180 const MRawRunHeader *runheader = (MRawRunHeader*)pList->FindObject("MRawRunHeader");
181 if (!runheader)
182 {
183 *fLog << warn << dbginf;
184 *fLog << "Warning - cannot check file type, MRawRunHeader not found." << endl;
185 }
186 else
187 if (runheader->IsMonteCarloRun())
188 return kTRUE;
189
190 Int_t npixels = fPedestals->GetSize();
191 Int_t areas = fPedestals->GetAverageAreas();
192 Int_t sectors = fPedestals->GetAverageSectors();
193
194 if (fSumx.GetSize()==0)
195 {
196 fSumx. Set(npixels);
197 fSumx2.Set(npixels);
198
199 fAreaSumx. Set(areas);
200 fAreaSumx2.Set(areas);
201 fAreaValid.Set(areas);
202
203 fSectorSumx. Set(sectors);
204 fSectorSumx2.Set(sectors);
205 fSectorValid.Set(sectors);
206
207 fSumx.Reset();
208 fSumx2.Reset();
209 }
210
211 // Calculate an even number for the hi gain samples to avoid
212 // biases due to the fluctuation in pedestal from one slice to
213 // the other one
214 fNumHiGainSamples = runheader->GetNumSamplesHiGain() & ~1;
215
216 return kTRUE;
217}
218// --------------------------------------------------------------------------
219//
220// Fill the MPedestalCam container with the signal mean and rms for the event.
221// Store the measured signal in arrays fSumx and fSumx2 so that we can
222// calculate the overall mean and rms in the PostProcess()
223//
224Int_t MPedCalcPedRun::Process()
225{
226
227 MRawEvtPixelIter pixel(fRawEvt);
228
229 while (pixel.Next())
230 {
231
232 const UInt_t idx = pixel.GetPixelId();
233 const UInt_t aidx = (*fGeom)[idx].GetAidx();
234 const UInt_t sector = (*fGeom)[idx].GetSector();
235
236 Byte_t *ptr = pixel.GetHiGainSamples();
237 const Byte_t *end = ptr + fNumHiGainSamples;
238
239 UInt_t sum = 0;
240 UInt_t sqr = 0;
241
242 do
243 {
244 sum += *ptr;
245 sqr += *ptr * *ptr;
246 }
247 while (++ptr != end);
248
249 const Float_t msum = (Float_t)sum;
250
251 //
252 // These three lines have been uncommented by Markus Gaug
253 // If anybody needs them, please contact me!!
254 //
255 // const Float_t higainped = msum/fNumHiGainSamples;
256 // const Float_t higainrms = TMath::Sqrt((msqr-msum*msum/fNumHiGainSamples)/(fNumHiGainSamples-1.));
257 // (*fPedestals)[idx].Set(higainped, higainrms);
258
259 fSumx[idx] += msum;
260 fAreaSumx[aidx] += msum;
261 fSectorSumx[sector] += msum;
262 //
263 // The old version:
264 //
265 // const Float_t msqr = (Float_t)sqr;
266 // fSumx2[idx] += msqr;
267 //
268 // The new version:
269 //
270 const Float_t sqrsum = msum*msum;
271 fSumx2[idx] += sqrsum;
272 fAreaSumx2[aidx] += sqrsum;
273 fSectorSumx2[sector] += sqrsum;
274 }
275
276 fPedestals->SetReadyToSave();
277 fNumSamplesTot += fNumHiGainSamples;
278
279
280 return kTRUE;
281}
282
283// --------------------------------------------------------------------------
284//
285// Compute signal mean and rms in the whole run and store it in MPedestalCam
286//
287Int_t MPedCalcPedRun::PostProcess()
288{
289
290 // Compute pedestals and rms from the whole run
291 const ULong_t n = fNumSamplesTot;
292 const ULong_t nevts = GetNumExecutions();
293
294 MRawEvtPixelIter pixel(fRawEvt);
295
296 while (pixel.Next())
297 {
298
299 const Int_t pixid = pixel.GetPixelId();
300 const UInt_t aidx = (*fGeom)[pixid].GetAidx();
301 const UInt_t sector = (*fGeom)[pixid].GetSector();
302
303 fAreaValid [aidx]++;
304 fSectorValid[sector]++;
305
306 const Float_t sum = fSumx.At(pixid);
307 const Float_t sum2 = fSumx2.At(pixid);
308
309 const Float_t higainped = sum/n;
310 //
311 // The old version:
312 //
313 // const Float_t higainrms = TMath::Sqrt((sum2-sum*sum/n)/(n-1.));
314 //
315 // The new version:
316 //
317 // 1. Calculate the Variance of the sums:
318 Float_t higainVar = (sum2-sum*sum/nevts)/(nevts-1.);
319 // 2. Scale the variance to the number of slices:
320 higainVar /= (Float_t)fNumHiGainSamples;
321 // 3. Calculate the RMS from the Variance:
322 const Float_t higainrms = TMath::Sqrt(higainVar);
323
324 (*fPedestals)[pixid].Set(higainped, higainrms);
325
326 }
327
328 //
329 // Loop over the (two) area indices to get the averaged pedestal per aidx
330 //
331 for (Int_t aidx=0; aidx<fAreaValid.GetSize(); aidx++)
332 {
333
334 const Int_t napix = fAreaValid.At(aidx);
335 const Float_t sum = fAreaSumx.At(aidx);
336 const Float_t sum2 = fAreaSumx2.At(aidx);
337 const ULong_t an = napix * n;
338 const ULong_t aevts = napix * nevts;
339
340 const Float_t higainped = sum/an;
341
342 // 1. Calculate the Variance of the sums:
343 Float_t higainVar = (sum2-sum*sum/aevts)/(aevts-1.);
344 // 2. Scale the variance to the number of slices:
345 higainVar /= (Float_t)fNumHiGainSamples;
346 // 3. Calculate the RMS from the Variance:
347 Float_t higainrms = TMath::Sqrt(higainVar);
348 // 4. Re-scale it with the square root of the number of involved pixels
349 // in order to be comparable to the mean of pedRMS of that area
350 higainrms *= TMath::Sqrt((Float_t)napix);
351
352 fPedestals->GetAverageArea(aidx).Set(higainped, higainrms);
353 }
354
355 //
356 // Loop over the (six) sector indices to get the averaged pedestal per sector
357 //
358 for (Int_t sector=0; sector<fSectorValid.GetSize(); sector++)
359 {
360
361 const Int_t nspix = fSectorValid.At(sector);
362 const Float_t sum = fSectorSumx.At(sector);
363 const Float_t sum2 = fSectorSumx2.At(sector);
364 const ULong_t sn = nspix * n;
365 const ULong_t sevts = nspix * nevts;
366
367 const Float_t higainped = sum/sn;
368
369 // 1. Calculate the Variance of the sums:
370 Float_t higainVar = (sum2-sum*sum/sevts)/(sevts-1.);
371 // 2. Scale the variance to the number of slices:
372 higainVar /= (Float_t)fNumHiGainSamples;
373 // 3. Calculate the RMS from the Variance:
374 Float_t higainrms = TMath::Sqrt(higainVar);
375 // 4. Re-scale it with the square root of the number of involved pixels
376 // in order to be comparable to the mean of pedRMS of that sector
377 higainrms *= TMath::Sqrt((Float_t)nspix);
378
379 fPedestals->GetAverageSector(sector).Set(higainped, higainrms);
380 }
381
382 fPedestals->SetTotalEntries(fNumSamplesTot);
383
384 return kTRUE;
385}
386
387
Note: See TracBrowser for help on using the repository browser.