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