source: trunk/MagicSoft/Mars/mpedestal/MPedCalcFromLoGain.cc@ 4467

Last change on this file since 4467 was 4404, checked in by fgoebel, 20 years ago
*** empty log message ***
File size: 19.6 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! Author(s): Florian Goebel 06/2004 <mailto:fgoebel@mppmu.mpg.de>
24!
25! Copyright: MAGIC Software Development, 2000-2004
26!
27!
28\* ======================================================================== */
29/////////////////////////////////////////////////////////////////////////////
30//
31// MPedCalcLoGain
32//
33//
34// This task is devide form MPedCalcPedRun, described below. However, It
35// calculates the pedstals using the low gain slices, whenever the difference
36// between the highest and the lowest slice in the high gain
37// slices is below a given threshold (SetMaxHiGainVar). In this case the receiver
38// boards do not switch to lo gain and the so called lo gain slices are actually
39// high gain slices.
40//
41// MPedCalcLoGain also fills the ABoffset in MPedestalPix which allows to correct
42// the 150 MHz clock noise.
43//
44// This task takes a pedestal run file and fills MPedestalCam during
45// the Process() with the pedestal and rms computed in an event basis.
46// In the PostProcess() MPedestalCam is finally filled with the pedestal
47// mean and rms computed in a run basis.
48// More than one run (file) can be merged
49//
50// MPedCalcPedRun applies the following formula (1):
51//
52// Pedestal per slice = sum(x_i) / n / slices
53// PedRMS per slice = Sqrt( ( sum(x_i^2) - sum(x_i)^2/n ) / n-1 / slices )
54//
55// where x_i is the sum of "slices" FADC slices and sum means the sum over all
56// events. "n" is the number of events, "slices" is the number of summed FADC samples.
57//
58// Note that the slice-to-slice fluctuations are not Gaussian, but Poissonian, thus
59// asymmetric and they are correlated.
60//
61// It is important to know that the Pedestal per slice and PedRMS per slice depend
62// on the number of used FADC slices, as seen in the following plots:
63//
64//Begin_Html
65/*
66<img src="images/PedestalStudyInner.gif">
67*/
68//End_Html
69//
70//Begin_Html
71/*
72<img src="images/PedestalStudyOuter.gif">
73*/
74//
75// The plots show the inner and outer pixels, respectivly and have the following meaning:
76//
77// 1) The calculated mean pedestal per slice (from MPedCalcFromLoGain)
78// 2) The fitted mean pedestal per slice (from MHPedestalCam)
79// 3) The calculated pedestal RMS per slice (from MPedCalcFromLoGain)
80// 4) The fitted sigma of the pedestal distribution per slice
81// (from MHPedestalCam)
82// 5) The relative difference between calculation and histogram fit
83// for the mean
84// 6) The relative difference between calculation and histogram fit
85// for the sigma or RMS, respectively.
86//
87// The calculated means do not change significantly except for the case of 2 slices,
88// however the RMS changes from 5.7 per slice in the case of 2 extracted slices
89// to 8.3 per slice in the case of 26 extracted slices. This change is very significant.
90//
91// The plots have been produced on run 20123. You can reproduce them using
92// the macro pedestalstudies.C
93//
94// Usage of this class:
95// ====================
96//
97// Call: SetRange(higainfirst, higainlast, logainfirst, logainlast)
98// to modify the ranges in which the window is allowed to move.
99// Defaults are:
100//
101// fHiGainFirst = fgHiGainFirst = 0
102// fHiGainLast = fgHiGainLast = 29
103// fLoGainFirst = fgLoGainFirst = 0
104// fLoGainLast = fgLoGainLast = 14
105//
106// Call: SetWindowSize(windowhigain, windowlogain)
107// to modify the sliding window widths. Windows have to be an even number.
108// In case of odd numbers, the window will be modified.
109//
110// Defaults are:
111//
112// fHiGainWindowSize = fgHiGainWindowSize = 14
113// fLoGainWindowSize = fgLoGainWindowSize = 0
114//
115// Input Containers:
116// MRawEvtData
117// MRawRunHeader
118// MGeomCam
119//
120// Output Containers:
121// MPedestalCam
122//
123// See also: MPedestalCam, MPedestalPix, MHPedestalCam, MExtractor
124//
125/////////////////////////////////////////////////////////////////////////////
126#include "MPedCalcFromLoGain.h"
127#include "MExtractor.h"
128
129#include "MParList.h"
130
131#include "MLog.h"
132#include "MLogManip.h"
133
134#include "MRawRunHeader.h"
135#include "MRawEvtPixelIter.h"
136#include "MRawEvtData.h"
137
138#include "MPedestalPix.h"
139#include "MPedestalCam.h"
140
141#include "MGeomPix.h"
142#include "MGeomCam.h"
143
144#include "MBadPixelsPix.h"
145#include "MBadPixelsCam.h"
146
147#include "MGeomCamMagic.h"
148
149ClassImp(MPedCalcFromLoGain);
150
151using namespace std;
152
153const Byte_t MPedCalcFromLoGain::fgHiGainFirst = 0;
154const Byte_t MPedCalcFromLoGain::fgHiGainLast = 11;
155const Byte_t MPedCalcFromLoGain::fgLoGainFirst = 1;
156const Byte_t MPedCalcFromLoGain::fgLoGainLast = 14;
157const Byte_t MPedCalcFromLoGain::fgHiGainWindowSize = 12;
158const Byte_t MPedCalcFromLoGain::fgLoGainWindowSize = 14;
159const Byte_t MPedCalcFromLoGain::fgMaxHiGainVar = 20;
160
161// --------------------------------------------------------------------------
162//
163// Default constructor:
164//
165// Sets:
166// - all pointers to NULL
167// - fWindowSizeHiGain to fgHiGainWindowSize
168// - fWindowSizeLoGain to fgLoGainWindowSize
169//
170// Calls:
171// - AddToBranchList("fHiGainPixId");
172// - AddToBranchList("fHiGainFadcSamples");
173// - SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast)
174// - Clear()
175//
176MPedCalcFromLoGain::MPedCalcFromLoGain(const char *name, const char *title)
177 : fWindowSizeHiGain(fgHiGainWindowSize),
178 fWindowSizeLoGain(fgLoGainWindowSize),
179 fGeom(NULL),fBad(NULL)
180{
181 fName = name ? name : "MPedCalcFromLoGain";
182 fTitle = title ? title : "Task to calculate pedestals from pedestal runs raw data";
183
184 AddToBranchList("fHiGainPixId");
185 AddToBranchList("fHiGainFadcSamples");
186
187 SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast);
188
189 SetMaxHiGainVar(fgMaxHiGainVar);
190 SetPedestalUpdate(kTRUE);
191 Clear();
192}
193
194// --------------------------------------------------------------------------
195//
196// Sets:
197// - fNumSamplesTot to 0
198// - fRawEvt to NULL
199// - fRunHeader to NULL
200// - fPedestals to NULL
201//
202void MPedCalcFromLoGain::Clear(const Option_t *o)
203{
204
205 fRawEvt = NULL;
206 fRunHeader = NULL;
207 fPedestals = NULL;
208}
209
210// --------------------------------------------------------------------------
211//
212// SetRange:
213//
214// Calls:
215// - MExtractor::SetRange(hifirst,hilast,lofirst,lolast);
216// - SetWindowSize(fWindowSizeHiGain,fWindowSizeLoGain);
217//
218void MPedCalcFromLoGain::SetRange(Byte_t hifirst, Byte_t hilast, Byte_t lofirst, Byte_t lolast)
219{
220
221 MExtractor::SetRange(hifirst,hilast,lofirst,lolast);
222
223 //
224 // Redo the checks if the window is still inside the ranges
225 //
226 SetWindowSize(fWindowSizeHiGain,fWindowSizeLoGain);
227
228}
229
230
231// --------------------------------------------------------------------------
232//
233void MPedCalcFromLoGain::SetMaxHiGainVar(Byte_t maxvar) {
234 fMaxHiGainVar = maxvar;
235}
236
237
238// --------------------------------------------------------------------------
239//
240// Checks:
241// - if a window is odd, subtract one
242// - if a window is bigger than the one defined by the ranges, set it to the available range
243// - if a window is smaller than 2, set it to 2
244//
245// Sets:
246// - fNumHiGainSamples to: (Float_t)fWindowSizeHiGain
247// - fNumLoGainSamples to: (Float_t)fWindowSizeLoGain
248// - fSqrtHiGainSamples to: TMath::Sqrt(fNumHiGainSamples)
249// - fSqrtLoGainSamples to: TMath::Sqrt(fNumLoGainSamples)
250//
251void MPedCalcFromLoGain::SetWindowSize(Byte_t windowh, Byte_t windowl)
252{
253
254 fWindowSizeHiGain = windowh & ~1;
255 fWindowSizeLoGain = windowl & ~1;
256
257 if (fWindowSizeHiGain != windowh)
258 *fLog << warn << GetDescriptor() << ": Hi Gain window size has to be even, set to: "
259 << int(fWindowSizeHiGain) << " samples " << endl;
260
261 if (fWindowSizeLoGain != windowl)
262 *fLog << warn << GetDescriptor() << ": Lo Gain window size has to be even, set to: "
263 << int(fWindowSizeLoGain) << " samples " << endl;
264
265 const Byte_t availhirange = (fHiGainLast-fHiGainFirst+1) & ~1;
266 const Byte_t availlorange = (fLoGainLast-fLoGainFirst+1) & ~1;
267
268 if (fWindowSizeHiGain > availhirange)
269 {
270 *fLog << warn << GetDescriptor()
271 << Form("%s%2i%s%2i%s%2i%s",": Hi Gain window size: ",(int)fWindowSizeHiGain,
272 " is bigger than available range: [",(int)fHiGainFirst,",",(int)fHiGainLast,"]") << endl;
273 *fLog << warn << GetDescriptor()
274 << ": Will set window size to: " << (int)availhirange << endl;
275 fWindowSizeHiGain = availhirange;
276 }
277
278 if (fWindowSizeLoGain > availlorange)
279 {
280 *fLog << warn << GetDescriptor()
281 << Form("%s%2i%s%2i%s%2i%s",": Lo Gain window size: ",(int)fWindowSizeLoGain,
282 " is bigger than available range: [",(int)fLoGainFirst,",",(int)fLoGainLast,"]") << endl;
283 *fLog << warn << GetDescriptor()
284 << ": Will set window size to: " << (int)availlorange << endl;
285 fWindowSizeLoGain = availlorange;
286 }
287
288
289 fNumHiGainSamples = (Float_t)fWindowSizeHiGain;
290 fNumLoGainSamples = (Float_t)fWindowSizeLoGain;
291
292 fSqrtHiGainSamples = TMath::Sqrt(fNumHiGainSamples);
293 fSqrtLoGainSamples = TMath::Sqrt(fNumLoGainSamples);
294
295}
296
297
298
299// --------------------------------------------------------------------------
300//
301// Look for the following input containers:
302//
303// - MRawEvtData
304// - MRawRunHeader
305// - MGeomCam
306// - MBadPixelsCam
307//
308// The following output containers are also searched and created if
309// they were not found:
310//
311// - MPedestalCam
312//
313Int_t MPedCalcFromLoGain::PreProcess( MParList *pList )
314{
315
316 Clear();
317
318 fRawEvt = (MRawEvtData*)pList->FindObject("MRawEvtData");
319 if (!fRawEvt)
320 {
321 *fLog << err << "MRawEvtData not found... aborting." << endl;
322 return kFALSE;
323 }
324
325 fRunHeader = (MRawRunHeader*)pList->FindObject(AddSerialNumber("MRawRunHeader"));
326 if (!fRunHeader)
327 {
328 *fLog << err << AddSerialNumber("MRawRunHeader") << " not found... aborting." << endl;
329 return kFALSE;
330 }
331
332 fGeom = (MGeomCam*)pList->FindObject("MGeomCam");
333 if (!fGeom)
334 {
335 *fLog << err << "MGeomCam not found... aborting." << endl;
336 return kFALSE;
337 }
338
339 fPedestals = (MPedestalCam*)pList->FindCreateObj("MPedestalCam");
340 if (!fPedestals)
341 return kFALSE;
342
343 fBad = (MBadPixelsCam*)pList->FindObject("MBadPixelsCam");
344
345 return kTRUE;
346}
347
348// --------------------------------------------------------------------------
349//
350// The ReInit searches for:
351// - MRawRunHeader::GetNumSamplesHiGain()
352// - MRawRunHeader::GetNumSamplesLoGain()
353//
354// In case that the variables fHiGainLast and fLoGainLast are smaller than
355// the even part of the number of samples obtained from the run header, a
356// warning is given an the range is set back accordingly. A call to:
357// - SetRange(fHiGainFirst, fHiGainLast-diff, fLoGainFirst, fLoGainLast) or
358// - SetRange(fHiGainFirst, fHiGainLast, fLoGainFirst, fLoGainLast-diff)
359// is performed in that case. The variable diff means here the difference
360// between the requested range (fHiGainLast) and the available one. Note that
361// the functions SetRange() are mostly overloaded and perform more checks,
362// modifying the ranges again, if necessary.
363//
364// A loop over the MBadPixelsCam is performed and bad pixels are set
365// to MPedestalPix::SetValid(kFALSE);
366//
367Bool_t MPedCalcFromLoGain::ReInit(MParList *pList)
368{
369
370 Int_t lastdesired = (Int_t)fLoGainLast;
371 Int_t lastavailable = (Int_t)fRunHeader->GetNumSamplesLoGain()-1;
372
373 if (lastdesired > lastavailable)
374 {
375 const Int_t diff = lastdesired - lastavailable;
376 *fLog << endl;
377 *fLog << warn << GetDescriptor()
378 << Form("%s%2i%s%2i%s%2i%s",": Selected Lo Gain FADC Window [",
379 (int)fLoGainFirst,",",lastdesired,
380 "] ranges out of the available limits: [0,",lastavailable,"].") << endl;
381 *fLog << GetDescriptor() << ": Will reduce the upper edge to " << (int)(fLoGainLast - diff) << endl;
382 SetRange(fHiGainFirst, fHiGainLast, fLoGainFirst, fLoGainLast-diff);
383 }
384
385 lastdesired = (Int_t)fHiGainLast;
386 lastavailable = (Int_t)fRunHeader->GetNumSamplesHiGain()-1;
387
388 if (lastdesired > lastavailable)
389 {
390 const Int_t diff = lastdesired - lastavailable;
391 *fLog << endl;
392 *fLog << warn << GetDescriptor()
393 << Form("%s%2i%s%2i%s%2i%s",": Selected Hi Gain Range [",
394 (int)fHiGainFirst,",",lastdesired,
395 "] ranges out of the available limits: [0,",lastavailable,"].") << endl;
396 *fLog << warn << GetDescriptor()
397 << Form("%s%2i%s",": Will possibly use ",diff," samples from the Low-Gain for the High-Gain range")
398 << endl;
399 fHiGainLast -= diff;
400 fHiLoLast = diff;
401 }
402
403 lastdesired = (Int_t)fHiGainFirst+fWindowSizeHiGain-1;
404 lastavailable = (Int_t)fRunHeader->GetNumSamplesHiGain()-1;
405
406 if (lastdesired > lastavailable)
407 {
408 const Int_t diff = lastdesired - lastavailable;
409 *fLog << endl;
410 *fLog << warn << GetDescriptor()
411 << Form("%s%2i%s%2i%s",": Selected Hi Gain FADC Window size ",
412 (int)fWindowSizeHiGain,
413 " ranges out of the available limits: [0,",lastavailable,"].") << endl;
414 *fLog << warn << GetDescriptor()
415 << Form("%s%2i%s",": Will use ",diff," samples from the Low-Gain for the High-Gain extraction")
416 << endl;
417
418 if ((Int_t)fWindowSizeHiGain > diff)
419 {
420 fWindowSizeHiGain -= diff;
421 fWindowSizeLoGain += diff;
422 }
423 else
424 {
425 fWindowSizeLoGain += fWindowSizeHiGain;
426 fLoGainFirst = diff-fWindowSizeHiGain;
427 fWindowSizeHiGain = 0;
428 }
429 }
430
431
432 Int_t npixels = fPedestals->GetSize();
433
434 if (fSumx.GetSize()==0)
435 {
436 fSumx. Set(npixels);
437 fSumx2.Set(npixels);
438 fSumAB0.Set(npixels);
439 fSumAB1.Set(npixels);
440 fNumEventsUsed.Set(npixels);
441 fTotalCounter.Set(npixels);
442
443 fSumx.Reset();
444 fSumx2.Reset();
445 fSumAB0.Reset();
446 fSumAB1.Reset();
447 fNumEventsUsed.Reset();
448 fTotalCounter.Reset();
449 }
450
451 if (fWindowSizeHiGain == 0 && fWindowSizeLoGain == 0)
452 {
453 *fLog << err << GetDescriptor()
454 << ": Number of extracted Slices is 0, abort ... " << endl;
455 return kFALSE;
456 }
457
458
459 *fLog << endl;
460 *fLog << inf << GetDescriptor() << ": Taking " << (int)fWindowSizeHiGain
461 << " HiGain FADC samples starting with slice: " << (int)fHiGainFirst << endl;
462 *fLog << inf << GetDescriptor() << ": Taking " << (int)fWindowSizeLoGain
463 << " LoGain FADC samples starting with slice: " << (int)fLoGainFirst << endl;
464
465
466 if (fBad)
467 {
468 const Int_t nbads = fBad->GetSize();
469 for (Int_t i=0; i<(nbads>npixels?npixels:nbads);i++)
470 if ((*fBad)[i].IsBad())
471 (*fPedestals)[i].SetValid(kFALSE);
472 }
473
474 return kTRUE;
475
476}
477// --------------------------------------------------------------------------
478//
479// Fill the MPedestalCam container with the signal mean and rms for the event.
480// Store the measured signal in arrays fSumx and fSumx2 so that we can
481// calculate the overall mean and rms in the PostProcess()
482//
483Int_t MPedCalcFromLoGain::Process()
484{
485
486 MRawEvtPixelIter pixel(fRawEvt);
487
488 // cout << "fHiGainFirst: " << (Int_t)fHiGainFirst << " fWindowSizeHiGain: " << (Int_t)fWindowSizeHiGain << " fLoGainFirst: " << (Int_t)fLoGainFirst << " fWindowSizeLoGain: " << (Int_t)fWindowSizeLoGain << endl;
489
490
491 while (pixel.Next()) {
492
493 const UInt_t idx = pixel.GetPixelId();
494
495 Byte_t *ptr = pixel.GetHiGainSamples() + fHiGainFirst;
496 Byte_t *end = ptr + fWindowSizeHiGain;
497
498 UInt_t sum = 0;
499 UInt_t sqr = 0;
500
501 UInt_t max = 0;
502 UInt_t min = 255;
503
504 // Find the maximum and minimum signal per slice in the high gain window
505 do {
506 if (*ptr > max) {
507 max = *ptr;
508 }
509 if (*ptr < min) {
510 min = *ptr;
511 }
512 } while (++ptr != end);
513
514 // If the maximum in the high gain window is smaller than
515 if ((max-min < fMaxHiGainVar) && (max < 255)) {
516
517 Byte_t *firstSlice = pixel.GetLoGainSamples() + fLoGainFirst;
518 Byte_t *lastSlice = firstSlice + fWindowSizeLoGain;
519
520 Byte_t *slice = firstSlice;
521 do {
522 sum += *slice;
523 sqr += *slice * *slice;
524 } while (++slice != lastSlice);
525
526 const Float_t msum = (Float_t)sum;
527 const Float_t sqrsum = msum*msum;
528
529 fSumx[idx] += msum;
530 fSumx2[idx] += sqrsum;
531 fNumEventsUsed[idx]++;
532
533 // Calculate the amplitude of the 150MHz "AB" noise
534
535 Int_t abFlag = (fRunHeader->GetNumSamplesHiGain()
536 + fLoGainFirst
537 + pixel.HasABFlag()) & 0x1;
538 for (Int_t islice=0; islice<fWindowSizeLoGain; islice+=2) {
539 Int_t sliceAB0 = islice + abFlag;
540 Int_t sliceAB1 = islice + 1 - abFlag;
541 fSumAB0[idx] += firstSlice[sliceAB0];
542 fSumAB1[idx] += firstSlice[sliceAB1];
543 }
544
545 if (fPedestalUpdate && (fNumEventsUsed[idx] == fNumEventsDump)) {
546
547 const ULong_t n = fNumEventsDump;
548
549 const ULong_t nsamplestot = n*fWindowSizeLoGain;
550
551 const Float_t sum = fSumx.At(idx);
552 const Float_t sum2 = fSumx2.At(idx);
553 const Float_t ped = sum/(nsamplestot);
554
555 // 1. Calculate the Variance of the sums:
556 Float_t var = (sum2-sum*sum/n)/(n-1.);
557
558 // 2. Scale the variance to the number of slices:
559 var /= (Float_t)(fWindowSizeLoGain);
560
561 // 3. Calculate the RMS from the Variance:
562 Float_t rms = var < 0 ? 0. : TMath::Sqrt(var);
563
564 // 4. Calculate the amplitude of the 150MHz "AB" noise
565 Float_t abOffs = (fSumAB0[idx] - fSumAB1[idx]) / nsamplestot;
566
567 (*fPedestals)[idx].Set(ped, rms, abOffs, n);
568
569 fTotalCounter[idx]++;
570 fNumEventsUsed[idx]=0;
571 fSumx[idx]=0;
572 fSumx2[idx]=0;
573 fSumAB0[idx]=0;
574 fSumAB1[idx]=0;
575 }
576 }
577 }
578
579 fPedestals->SetReadyToSave();
580
581 return kTRUE;
582}
583
584// --------------------------------------------------------------------------
585//
586// Compute signal mean and rms in the whole run and store it in MPedestalCam
587//
588Int_t MPedCalcFromLoGain::PostProcess()
589{
590
591 // Compute pedestals and rms from the whole run
592
593 if (!fPedestalUpdate) {
594
595 MRawEvtPixelIter pixel(fRawEvt);
596
597 while (pixel.Next()) {
598
599 const Int_t idx = pixel.GetPixelId();
600
601 const ULong_t n = fNumEventsUsed[idx];
602
603 if (n < 2)
604 continue;
605
606 const ULong_t nsamplestot = n*fWindowSizeLoGain;
607
608 const Float_t sum = fSumx.At(idx);
609 const Float_t sum2 = fSumx2.At(idx);
610 const Float_t ped = sum/(nsamplestot);
611
612 // 1. Calculate the Variance of the sums:
613 Float_t var = (sum2-sum*sum/n)/(n-1.);
614
615 // 2. Scale the variance to the number of slices:
616 var /= (Float_t)(fWindowSizeLoGain);
617
618 // 3. Calculate the RMS from the Variance:
619 Float_t rms = var < 0 ? 0. : TMath::Sqrt(var);
620
621 // 4. Calculate the amplitude of the 150MHz "AB" noise
622 Float_t abOffs = (fSumAB0[idx] - fSumAB1[idx]) / nsamplestot;
623
624 (*fPedestals)[idx].Set(ped, rms, abOffs, n);
625
626 fTotalCounter[idx]++;
627 }
628
629 fPedestals->SetReadyToSave();
630 }
631
632 return kTRUE;
633}
Note: See TracBrowser for help on using the repository browser.