source: trunk/MagicSoft/Mars/mpedestal/MExtractPedestal.cc@ 8573

Last change on this file since 8573 was 8564, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 34.1 KB
Line 
1/* ======================================================================== *\
2! $Name: not supported by cvs2svn $:$Id: MExtractPedestal.cc,v 1.32 2007-06-16 21:59:19 tbretz Exp $
3! --------------------------------------------------------------------------
4!
5! *
6! * This file is part of MARS, the MAGIC Analysis and Reconstruction
7! * Software. It is distributed to you in the hope that it can be a useful
8! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
9! * It is distributed WITHOUT ANY WARRANTY.
10! *
11! * Permission to use, copy, modify and distribute this software and its
12! * documentation for any purpose is hereby granted without fee,
13! * provided that the above copyright notice appear in all copies and
14! * that both that copyright notice and this permission notice appear
15! * in supporting documentation. It is provided "as is" without express
16! * or implied warranty.
17! *
18!
19!
20! Author(s): Markus Gaug 01/2004 <mailto:markus@ifae.es>
21! Author(s): Thomas Bretz 01/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
22!
23! Copyright: MAGIC Software Development, 2000-2007
24!
25!
26\* ======================================================================== */
27
28/////////////////////////////////////////////////////////////////////////////
29//
30// MExtractPedestal
31//
32// Pedestal Extractor base class
33//
34// Input Containers:
35// MRawEvtData
36// MRawRunHeader
37// MRawEvtHeader
38// MGeomCam
39// MPedestalCam
40//
41// Output Containers:
42// MPedestalCam
43//
44// This class should be used for pedestal extractors with the following facilities:
45// a) Standardized calculation of AB-noise, mean pedestals and RMS
46// b) Standardized treatment of area- and sector averaged pedestals values
47// c) Possibility to use a signal extractor to be applied on the pedestals
48// d) Possibility to handle two MPedestalCams: one for the signal extractor and
49// a second to be filled during the pedestal calculating process.
50//
51// ad a): Every calculated value is referred to one FADC slice (e.g. Mean pedestal per slice),
52// RMS per slice.
53// MExtractPedestal applies the following formula (1):
54//
55// Pedestal per slice = sum(x_i) / n / slices
56// PedRMS per slice = Sqrt( ( sum(x_i^2) - sum(x_i)^2/n ) / n-1 / slices )
57// AB-Offset per slice = (sumAB0 - sumAB1) / n / slices
58//
59// where x_i is the sum of "slices" FADC slices and sum means the sum over all
60// events. "n" is the number of events, "slices" is the number of summed FADC samples.
61//
62// Note that the slice-to-slice fluctuations are not Gaussian, but Poissonian, thus
63// asymmetric and they are correlated.
64//
65// It is important to know that the Pedestal per slice and PedRMS per slice depend
66// on the number of used FADC slices, as seen in the following plots:
67//
68//Begin_Html
69/*
70<img src="images/PedestalStudyInner.gif">
71*/
72//End_Html
73//
74//Begin_Html
75/*
76<img src="images/PedestalStudyOuter.gif">
77*/
78//End_Html
79//
80// The plots show the inner and outer pixels, respectivly and have the following meaning:
81//
82// 1) The calculated mean pedestal per slice (from MPedCalcPedRun)
83// 2) The fitted mean pedestal per slice (from MHPedestalCam)
84// 3) The calculated pedestal RMS per slice (from MPedCalcPedRun)
85// 4) The fitted sigma of the pedestal distribution per slice
86// (from MHPedestalCam)
87// 5) The relative difference between calculation and histogram fit
88// for the mean
89// 6) The relative difference between calculation and histogram fit
90// for the sigma or RMS, respectively.
91//
92// The calculated means do not change significantly except for the case of 2 slices,
93// however the RMS changes from 5.7 per slice in the case of 2 extracted slices
94// to 8.3 per slice in the case of 26 extracted slices. This change is very significant.
95//
96// ad b) Every calculated value is referred to one FADC slice and one (averaged) pixel,
97// (e.g. Mean Pedestal per area index per slice per pixel, etc. )
98//
99// MExtractPedestal applies the following formula (2):
100//
101// Averaged Pedestal per slice = sum(x_i) / n / slices / n_pix
102// PedRMS per slice = Sqrt( ( sum(x_i^2) - sum(x_i)^2/n ) / n-1 / slices / n_pix )
103// AB-Offset per slice = (sumAB0 - sumAB1) / n / slices / n_pix
104//
105// where x_i is the sum of "slices" FADC slices and sum means the sum over all
106// events and all concerned pixels.
107// "n" is the number of events, "slices" is the number of summed FADC samples and
108// "n_pix" is the number of pixels belonging to the specific area index or camera sector.
109//
110// Calculating these averaged event-by-event values is very important to trace coherent
111// fluctuations. An example is given in the following plots:
112//
113//Begin_Html
114/*
115<img src="images/PedestalOscillations.gif">
116*/
117//End_Html
118//
119// The plots show the extracted pedestals of the inner pixels (obtained
120// with MHPedestalCam), averaged on an event-by-event basis from
121// run 13428 with switched off camera LV.
122// The meaning of the four plots is:
123//
124// 1) The distribution of the averaged pedestals
125// 2) The averaged pedestals vs. time.
126// One can see clearly the oscillation pattern
127// 3) The fourier transform of the averaged pedestals vs. time.
128// One can see clearly a peak at a certain frequency
129// 4) The projection of the fourier components with the non-exponential
130// (and therefore significant) outlier.
131//
132// ad c) Many signal extractors, especially those using a sliding window
133// have biases and their resolutions for zero-signals do not agree
134// with the pedestal RMS. For the F-Factor method in the calibration
135// and the image cleaning, however, both have to be known and measured.
136//
137// For this reason, a signal extractor can be handed over to the
138// pedestal extractor and applied on the pedestal events with the
139// function SetExtractor().
140// The results will get stored in an MPedestalCam.
141//
142// Note that only extractors deriving from MExtractTimeAndCharge
143// can be used.
144//
145// ad d) The signal extractors themselves need a pedestal to be subtracted
146// from the FADC slices.
147// If the user wishes that the pededestals do not get overwritten by
148// the results from the signal extractor, a different named MPedestalCam
149// can be created with the function: SetNamePedestalOut().
150//
151// See also: MPedestalCam, MPedestalPix, MPedCalcPedRun, MPedCalcFromLoGain
152//
153/////////////////////////////////////////////////////////////////////////////
154#include "MExtractPedestal.h"
155
156#include "MParList.h"
157
158#include "MLog.h"
159#include "MLogManip.h"
160
161#include "MRawRunHeader.h"
162#include "MRawEvtHeader.h"
163#include "MRawEvtPixelIter.h"
164
165#include "MPedestalPix.h"
166#include "MPedestalCam.h"
167
168#include "MGeomPix.h"
169#include "MGeomCam.h"
170
171#include "MExtractTimeAndCharge.h"
172#include "MPedestalSubtractedEvt.h"
173
174ClassImp(MExtractPedestal);
175
176using namespace std;
177
178const TString MExtractPedestal::fgNamePedestalCam = "MPedestalCam";
179const TString MExtractPedestal::fgNameRawEvtData = "MRawEvtData";
180
181const UShort_t MExtractPedestal::fgCheckWinFirst = 0;
182const UShort_t MExtractPedestal::fgCheckWinLast = 29;
183const UShort_t MExtractPedestal::fgMaxSignalVar = 40;
184const UShort_t MExtractPedestal::fgMaxSignalAbs = 250;
185
186// --------------------------------------------------------------------------
187//
188// Default constructor:
189//
190// Sets:
191// - all pointers to NULL
192//
193// Calls:
194// - Clear()
195//
196MExtractPedestal::MExtractPedestal(const char *name, const char *title)
197 : fGeom(NULL), fPedestalsInter(NULL),
198 fPedestalsOut(NULL), fExtractor(NULL), fSignal(0),
199 fExtractWinFirst(0), fExtractWinSize(0), fUseSpecialPixels(kFALSE)
200{
201 fName = name ? name : "MExtractPedestal";
202 fTitle = title ? title : "Base class to calculate pedestals";
203
204 SetIntermediateStorage( kFALSE );
205 SetRandomCalculation ( kTRUE );
206
207 SetNamePedestalCamOut();
208 SetNamePedestalCamInter();
209 SetNameRawEvtData();
210
211 SetCheckRange(fgCheckWinFirst, fgCheckWinLast);
212 SetMaxSignalVar(fgMaxSignalVar);
213 SetMaxSignalAbs(fgMaxSignalAbs);
214
215 Clear();
216}
217
218// --------------------------------------------------------------------------
219//
220// Call reset() of all Arays
221//
222void MExtractPedestal::ResetArrays()
223{
224 // Reset contents of arrays.
225 fSumx.Reset();
226 fSumx2.Reset();
227 fSumAB0.Reset();
228 fSumAB1.Reset();
229 fAreaSumx.Reset();
230 fAreaSumx2.Reset();
231 fAreaSumAB0.Reset();
232 fAreaSumAB1.Reset();
233 fAreaFilled.Reset();
234 fAreaValid.Reset();
235 fSectorSumx.Reset();
236 fSectorSumx2.Reset();
237 fSectorSumAB0.Reset();
238 fSectorSumAB1.Reset();
239 fSectorFilled.Reset();
240 fSectorValid.Reset();
241 fNumEventsUsed.Reset();
242}
243
244// --------------------------------------------------------------------------
245//
246// Resets Arrays:
247//
248// Sets:
249// - fRawEvt to NULL
250// - fRunHeader to NULL
251//
252void MExtractPedestal::Clear(const Option_t *o)
253{
254
255 fRawEvt = NULL;
256 fRunHeader = NULL;
257
258 // If the size is yet set, set the size
259 if (fSumx.GetSize()>0)
260 ResetArrays();
261
262}
263
264// --------------------------------------------------------------------------
265//
266// Checks:
267// - if a window is odd
268//
269Bool_t MExtractPedestal::SetExtractWindow(UShort_t windowf, UShort_t windows)
270{
271 Bool_t rc = kTRUE;
272
273 if (windows==0)
274 {
275 *fLog << warn << GetDescriptor();
276 *fLog << " - WARNING: Window size in SetExtractWindow has to be > 0... adjusting to 2!" << endl;
277 windows = 2;
278 rc = kFALSE;
279 }
280
281 fExtractWinSize = windows;
282 fExtractWinFirst = windowf;
283 fExtractWinLast = fExtractWinFirst+fExtractWinSize-1;
284
285 return rc;
286}
287
288// --------------------------------------------------------------------------
289//
290// SetCheckRange:
291//
292// Exits, if the first argument is smaller than 0
293// Exits, if the the last argument is smaller than the first
294//
295Bool_t MExtractPedestal::SetCheckRange(UShort_t chfirst, UShort_t chlast)
296{
297
298 Bool_t rc = kTRUE;
299
300 if (chlast<=chfirst)
301 {
302 *fLog << warn << GetDescriptor();
303 *fLog << " - WARNING: Last slice in SetCheckRange smaller than first slice... set to first+2" << endl;
304 chlast = chfirst+1;
305 rc = kFALSE;
306 }
307
308 fCheckWinFirst = chfirst;
309 fCheckWinLast = chlast;
310
311 return rc;
312}
313
314Bool_t MExtractPedestal::SetRangeFromExtractor(const MExtractor &ext, Bool_t logain)
315{
316 const Bool_t haslogains = ext.GetLoGainFirst()!=0 && ext.GetLoGainLast()!=0;
317
318 Bool_t rc1 = kTRUE;
319 if (!haslogains)
320 {
321 // We assume that in case without lo-gains we
322 // deal with pedestal events only
323 rc1 = SetCheckRange(ext.GetHiGainFirst(), ext.GetHiGainLast());
324 }
325
326 const Int_t f = logain && haslogains ? ext.GetLoGainFirst() : ext.GetHiGainFirst();
327 const Int_t l = logain && haslogains ? ext.GetLoGainLast() : ext.GetHiGainLast();
328
329 const Int_t w = (l-f+1);
330
331 // Setup to use the hi-gain extraction window in the lo-gain
332 // range (the start of the lo-gain range is added automatically
333 // by MPedCalcFromLoGain)
334 const Bool_t rc2 = SetExtractWindow(f, w);
335
336 return rc1 && rc2;
337}
338
339// --------------------------------------------------------------------------
340//
341// Check (and if neccesary: correct) the extraction and check ranges.
342//
343void MExtractPedestal::CheckExtractionWindow(UInt_t offset)
344{
345 *fLog << inf;
346 *fLog << "Requested CheckWindow is [" << fCheckWinFirst << "," << fCheckWinLast << "]." <<endl;
347 *fLog << "Requested ExtractWindow is [" << fExtractWinFirst+offset << "," << fExtractWinLast+offset << "]." <<endl;
348
349 // fSignal->GetNumSamples() not yet initialized!!!
350 const UInt_t num = fRunHeader->GetNumSamplesHiGain()+fRunHeader->GetNumSamplesLoGain();
351
352 if (fCheckWinLast >= num)
353 {
354 *fLog << "CheckWindow [" << fCheckWinFirst << "," << fCheckWinLast;
355 *fLog << "] out of range [0," << num-1 << "]... ";
356 *fLog << "reset upper edge." << endl;
357
358 fCheckWinLast = num-1;
359 }
360
361 if (offset+fExtractWinLast >= num)
362 {
363 *fLog << "ExtractWindow [" << fExtractWinFirst+offset << "," << fExtractWinLast+offset;
364 *fLog << "] out of range [0," << num-1 << "]... ";
365 *fLog << "reset upper edge." << endl;
366
367 fExtractWinLast = num-offset-1;
368 }
369
370 fExtractWinSize = fExtractWinLast-fExtractWinFirst+1;
371
372 if (fExtractor || TMath::Even(fExtractWinSize))
373 return;
374
375 fExtractWinLast += offset+fExtractWinLast==num-1 ? -1 : +1;
376
377 *fLog << "ExtractionWindow odd... set to [";
378 *fLog << fExtractWinFirst+offset << "," << fExtractWinLast+offset << "]" << endl;
379
380 fExtractWinSize = fExtractWinLast-fExtractWinFirst+1;
381}
382
383// --------------------------------------------------------------------------
384//
385// Look for the following input containers:
386//
387// - MRawEvtData
388// - MRawRunHeader
389// - MRawEvtHeader
390// - MGeomCam
391//
392// The following output containers are also searched and created if
393// they were not found:
394//
395// - MPedestalCam with the name fPedContainerName
396//
397Int_t MExtractPedestal::PreProcess(MParList *pList)
398{
399
400 Clear();
401
402 fRawEvt = (MRawEvtData*)pList->FindObject(AddSerialNumber(fNameRawEvtData));
403 if (!fRawEvt)
404 {
405 *fLog << err << AddSerialNumber(fNameRawEvtData) << " not found... aborting." << endl;
406 return kFALSE;
407 }
408
409 fRunHeader = (MRawRunHeader*)pList->FindObject(AddSerialNumber("MRawRunHeader"));
410 if (!fRunHeader)
411 {
412 *fLog << err << AddSerialNumber("MRawRunHeader") << " not found... aborting." << endl;
413 return kFALSE;
414 }
415
416 fGeom = (MGeomCam*)pList->FindObject(AddSerialNumber("MGeomCam"));
417 if (!fGeom)
418 {
419 *fLog << err << AddSerialNumber("MGeomCam") << " not found... aborting." << endl;
420 return kFALSE;
421 }
422
423 fSignal = (MPedestalSubtractedEvt*)pList->FindObject(AddSerialNumber("MPedestalSubtractedEvt"));
424 if (!fSignal)
425 {
426 *fLog << err << AddSerialNumber("MPedestalSubtractedEvt") << " not found... aborting." << endl;
427 return kFALSE;
428 }
429
430 if (!fPedestalsInter && fIntermediateStorage)
431 {
432 fPedestalsInter = (MPedestalCam*)pList->FindCreateObj("MPedestalCam", AddSerialNumber(fNamePedestalCamInter));
433 if (!fPedestalsInter)
434 return kFALSE;
435 }
436
437 if (!fPedestalsOut)
438 {
439 fPedestalsOut = (MPedestalCam*)pList->FindCreateObj("MPedestalCam", AddSerialNumber(fNamePedestalCamOut));
440 if (!fPedestalsOut)
441 return kFALSE;
442 }
443
444 return fExtractor ? fExtractor->CallPreProcess(pList) : kTRUE;
445}
446
447//-----------------------------------------------------------------------
448//
449// Call Calc(). If fExtractor!=NULL enclose the call in setting the
450// NoiseCalculation to fRandomCalculation
451//
452Int_t MExtractPedestal::Process()
453{
454 //
455 // Necessary check for extraction of special pixels
456 // together with data which does not yet have them
457 //
458 if (fSumx.GetSize()==0)
459 return kTRUE;
460
461 if (fExtractor)
462 fExtractor->SetNoiseCalculation(fRandomCalculation);
463
464 const Int_t rc = Calc();
465
466 if (fExtractor)
467 fExtractor->SetNoiseCalculation(kFALSE);
468
469 return rc;
470}
471
472// ---------------------------------------------------------------------------------
473//
474// Sets the size (from MPedestalCam::GetSize() ) and resets the following arrays:
475// - fSumx
476// - fSumx2
477// - fSumAB0
478// - fSumAB1
479// - fAreaSumx
480// - fAreaSumx2
481// - fAreaSumAB0
482// - fAreaSumAB1
483// - fAreaFilled
484// - fAreaValid
485// - fSectorSumx
486// - fSectorSumx2
487// - fSectorSumAB0
488// - fSectorSumAB1
489// - fSectorFilled
490// - fSectorValid
491//
492Bool_t MExtractPedestal::ReInit(MParList *pList)
493{
494 // Necessary check for special pixels which might not yet have existed
495 if (!fRawEvt)
496 {
497 if (fRunHeader->GetFormatVersion() > 3)
498 return kTRUE;
499
500 *fLog << err << "ERROR - " << fNameRawEvtData << " [MRawEvtData] has not ";
501 *fLog << "been found and format version > 3... abort." << endl;
502 return kFALSE;
503 }
504
505 // If the size is not yet set, set the size
506 if (fSumx.GetSize()==0)
507 {
508 // Initialize the normal pixels (size of MPedestalCam already set by MGeomApply)
509 const Int_t npixels = fPedestalsOut->GetSize();
510
511 fSumx. Set(npixels);
512 fSumx2. Set(npixels);
513 fSumAB0.Set(npixels);
514 fSumAB1.Set(npixels);
515
516 fNumEventsUsed.Set(npixels);
517
518 if (fUseSpecialPixels)
519 {
520 // Initialize size of MPedestalCam in case of special pixels (not done by MGeomApply)
521 const UShort_t nspecial = fRunHeader->GetNumSpecialPixels();
522 if (nspecial == 0)
523 {
524 *fLog << warn << "WARNING - Number of special pixels is 0." << endl;
525 return kTRUE;
526 }
527
528 fPedestalsOut->InitSize((UInt_t)nspecial);
529 }
530 else
531 {
532 // Initialize the averaged areas and sectors (do not exist for special pixels)
533 const Int_t areas = fPedestalsOut->GetNumAverageArea();
534 const Int_t sectors = fPedestalsOut->GetNumAverageSector();
535
536 fAreaSumx. Set(areas);
537 fAreaSumx2. Set(areas);
538 fAreaSumAB0.Set(areas);
539 fAreaSumAB1.Set(areas);
540 fAreaFilled.Set(areas);
541 fAreaValid .Set(areas);
542
543 fSectorSumx. Set(sectors);
544 fSectorSumx2. Set(sectors);
545 fSectorSumAB0.Set(sectors);
546 fSectorSumAB1.Set(sectors);
547 fSectorFilled.Set(sectors);
548 fSectorValid .Set(sectors);
549
550 for (Int_t i=0; i<npixels; i++)
551 {
552 const UInt_t aidx = (*fGeom)[i].GetAidx();
553 const UInt_t sector = (*fGeom)[i].GetSector();
554
555 fAreaValid [aidx] ++;
556 fSectorValid[sector]++;
557 }
558 }
559 }
560
561 if (fExtractor)
562 {
563 if (!fExtractor->ReInit(pList))
564 return kFALSE;
565
566 SetRangeFromExtractor(*fExtractor);
567
568 // fSignal->GetNumSamples() not yet initialized!!!
569 const UInt_t num = fRunHeader->GetNumSamples();
570 if (fExtractWinLast >= num)
571 {
572 *fLog << err;
573 *fLog << "ERROR - Selected fExtractWinLast " << fExtractWinLast;
574 *fLog << " out of range (>=" << num<< ")." << endl;
575 return kFALSE;
576 }
577 }
578 else
579 if (fRunHeader->GetNumSamplesLoGain()==0 && (fCheckWinFirst!=0 || fCheckWinLast!=0))
580 {
581 *fLog << inf << "Data has no lo-gains... resetting check window to extraction window." << endl;
582 SetCheckRange(fExtractWinFirst, fExtractWinLast);
583 }
584
585 //CheckExtractionWindow();
586
587 return kTRUE;
588}
589
590// ---------------------------------------------------------------------------------
591//
592// PostProcess the extractor if available
593//
594Int_t MExtractPedestal::PostProcess()
595{
596 return fExtractor ? fExtractor->CallPostProcess() : kTRUE;
597}
598
599// ---------------------------------------------------------------------------------
600//
601// Check whether the signal variation between fCheckWinFirst and fCheckWinLast
602// exceeds fMaxSignalVar or the signal is greater than fMaxSignalAbs
603//
604Bool_t MExtractPedestal::CheckVariation(UInt_t idx) const
605{
606 // This is the fast workaround to put hi- and lo-gains together
607 USample_t *slices = fSignal->GetSamplesRaw(idx);
608
609 // Start 'real' work
610 USample_t max = 0;
611 USample_t min = (USample_t)-1;
612
613 // Find the maximum and minimum signal per slice in the high gain window
614 for (USample_t *slice=slices+fCheckWinFirst; slice<=slices+fCheckWinLast; slice++)
615 {
616 if (*slice > max)
617 max = *slice;
618 if (*slice < min)
619 min = *slice;
620 }
621
622 // If the maximum in the high gain window is smaller than
623 // FIXME: Precompiled value!
624 return max-min<fMaxSignalVar && max<fMaxSignalAbs;
625}
626
627// ---------------------------------------------------------------------------------
628//
629// Invoke the hi-gain extraction starting at fExtractWinFirst+offset
630// for fExtractWinLast-fExtractWinFirst+1 slices. If Noise calculation
631// is set it is up to the signal extractor to do the right thing.
632//
633// Returns the extracted signal.
634//
635Float_t MExtractPedestal::CalcExtractor(const MRawEvtPixelIter &pixel, Int_t offset) const
636{
637 // Use the same extraction window as for signal extraction
638 const Int_t first = fExtractWinFirst;
639 const Int_t last = fExtractWinLast;
640
641 const Int_t start = first+offset;
642
643 const Int_t range = last-first+1;
644
645 // This check is already done in CheckExtractionWindow
646 // if (range>pixel.GetNumSamples()-start)
647 // range = pixel.GetNumSamples()-start;
648
649 const Int_t idx = pixel.GetPixelId();
650
651 // Do some handling if maxpos is last slice?
652 const Int_t maxposhi = fRandomCalculation ? 0 : fSignal->GetMaxPos(idx, start, start+range-1);
653
654 const Float_t *sig = fSignal->GetSamples(idx);
655
656 // The pedestal is extracted with the hi-gain extractor (eg. digital
657 // filter weights) but from the lo-gains
658 Float_t dummy[3];
659 Float_t sum = 0;
660 fExtractor->FindTimeAndChargeHiGain2(sig+start, range, sum,
661 dummy[0], dummy[1], dummy[2],
662 0, maxposhi);
663
664 return sum;
665}
666
667// ---------------------------------------------------------------------------------
668//
669// Sum slices from fExtractWinFirst to fExtractWinLast. The total sum is
670// returned. ab0 and ab1 will contain the total sum splitted by the
671// AB-flag. If the AB-flag is invalid ab0=ab1=0 is returned.
672//
673UInt_t MExtractPedestal::CalcSums(const MRawEvtPixelIter &pixel, Int_t offset, UInt_t &ab0, UInt_t &ab1) const
674{
675 const Int_t first = fExtractWinFirst+offset;
676
677 USample_t *ptr = fSignal->GetSamplesRaw(pixel.GetPixelId())+first;
678 USample_t *end = ptr + fExtractWinSize;
679
680 Int_t abflag = pixel.HasABFlag() + first;
681
682 UInt_t ab[2] = { 0, 0 };
683 while (ptr<end)
684 ab[abflag++ & 0x1] += *ptr++;
685
686 // This check if for old data without AB-Flag in the data
687 const Bool_t valid = pixel.IsABFlagValid();
688
689 ab0 = valid ? ab[0] : 0;
690 ab1 = valid ? ab[1] : 0;
691
692 return ab[0]+ab[1];
693}
694
695// ---------------------------------------------------------------------------------
696//
697// Check for the variation of the pixel. Return kFALSE if this pixel
698// should not be used.
699// Calculate the pedestal either with the extractor or by summing slices.
700// And update all arrays.
701//
702Bool_t MExtractPedestal::CalcPixel(const MRawEvtPixelIter &pixel, Int_t offset, UInt_t usespecialpixels)
703{
704 const UInt_t idx = pixel.GetPixelId();
705 if (!CheckVariation(idx))
706 return kFALSE;
707
708 //extract pedestal
709 UInt_t ab[2];
710 const Float_t sum = fExtractor ?
711 CalcExtractor(pixel, offset) :
712 CalcSums(pixel, offset, ab[0], ab[1]);
713
714 fNumEventsUsed[idx]++;
715
716 if (fIntermediateStorage)
717 (*fPedestalsInter)[idx].Set(sum, 0, 0, fNumEventsUsed[idx]);
718
719 const Float_t sqrsum = sum*sum;
720
721 fSumx[idx] += sum;
722 fSumx2[idx] += sqrsum;
723
724 if (!fExtractor && pixel.IsABFlagValid())
725 {
726 fSumAB0[idx] += ab[0];
727 fSumAB1[idx] += ab[1];
728 }
729
730 if (usespecialpixels)
731 return kTRUE;
732
733 const UInt_t aidx = (*fGeom)[idx].GetAidx();
734 const UInt_t sector = (*fGeom)[idx].GetSector();
735
736 fAreaFilled[aidx]++;
737 fSectorFilled[sector]++;
738
739 fAreaSumx[aidx] += sum;
740 fAreaSumx2[aidx] += sqrsum;
741 fSectorSumx[sector] += sum;
742 fSectorSumx2[sector] += sqrsum;
743
744 if (!fExtractor && pixel.IsABFlagValid())
745 {
746 fAreaSumAB0[aidx] += ab[0];
747 fAreaSumAB1[aidx] += ab[1];
748 fSectorSumAB0[aidx] += ab[0];
749 fSectorSumAB1[aidx] += ab[1];
750 }
751
752 return kTRUE;
753}
754
755// ---------------------------------------------------------------------------------
756//
757// Calculates for pixel "idx":
758//
759// Ped per slice = sum / n / fExtractWinSize;
760// RMS per slice = sqrt { (sum2 - sum*sum/n) / (n-1) / fExtractWinSize }
761// ABOffset per slice = (fSumAB0[idx] - fSumAB1[idx]) / n / fExtractWinSize;
762//
763// Stores the results in MPedestalCam[pixid]
764//
765void MExtractPedestal::CalcPixResults(const UInt_t pixid)
766{
767 const UInt_t nevts = fNumEventsUsed[pixid];
768 if (nevts<2)
769 return;
770
771 const Double_t sum = fSumx[pixid];
772 const Double_t sum2 = fSumx2[pixid];
773
774 // 1. Calculate the mean of the sums:
775 Double_t ped = sum/nevts;
776
777 // 2. Calculate the Variance of the sums:
778 Double_t var = (sum2-sum*sum/nevts)/(nevts-1.);
779
780 // 3. Calculate the amplitude of the 150MHz "AB" noise
781 Double_t abOffs = (fSumAB0[pixid] - fSumAB1[pixid]) / nevts;
782
783 // 4. Scale the mean, variance and AB-noise to the number of slices:
784 ped /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
785 var /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
786 abOffs /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
787 // The pedestal extracted with the extractor is divided by
788 // the number of hi-gain samples because the calibration
789 // multiplies by this number
790
791 // scale to 256
792 const UInt_t scale = fExtractor ? 1 : fRunHeader->GetScale();
793
794 ped /= scale;
795 abOffs /= scale;
796
797 // 5. Calculate the RMS from the Variance:
798 const Double_t rms = var<0 ? 0 : TMath::Sqrt(var)/scale;
799
800 // abOffs contains only half of the signal as ped.
801 // Therefor abOffs is not the full, but the half amplitude
802 (*fPedestalsOut)[pixid].Set(ped, rms, abOffs, nevts);
803}
804
805// ---------------------------------------------------------------------------------
806//
807// Calculates for area idx "aidx" with "napix" valid pixels:
808//
809// Ped per slice = sum / nevts / fExtractWinSize / napix;
810// RMS per slice = sqrt { (sum2 - sum*sum/nevts) / (nevts-1) / fExtractWinSize / napix }
811// ABOffset per slice = (fSumAB0[idx] - fSumAB1[idx]) / nevts / fExtractWinSize / napix;
812//
813// Stores the results in MPedestalCam::GetAverageArea(aidx)
814//
815void MExtractPedestal::CalcAreaResults(const UInt_t aidx)
816{
817 const UInt_t nevts = fAreaFilled[aidx];
818 if (nevts<2)
819 return;
820
821 const UInt_t napix = fAreaValid[aidx];
822 if (napix<1)
823 return;
824
825 const Double_t sum = fAreaSumx[aidx];
826 const Double_t sum2 = fAreaSumx2[aidx];
827
828 // 1. Calculate the mean of the sums:
829 Double_t ped = sum/nevts;
830
831 // 2. Calculate the Variance of the sums:
832 Double_t var = (sum2/napix-sum*sum/nevts)/(nevts-1.);
833
834 // 3. Calculate the amplitude of the 150MHz "AB" noise
835 Double_t abOffs = (fAreaSumAB0[aidx] - fAreaSumAB1[aidx]) / nevts;
836
837 // 4. Scale the mean, variance and AB-noise to the number of slices:
838 ped /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
839 var /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
840 abOffs /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
841 // The pedestal extracted with the extractor is divided by
842 // the number of hi-gain samples because the calibration
843 // multiplies by this number
844
845 // scale to 256
846 const UInt_t scale = fExtractor ? 1 : fRunHeader->GetScale();
847
848 // 5. Scale the mean, variance and AB-noise to the number of pixels:
849 ped /= napix*scale;
850 abOffs /= napix*scale;
851
852 // 6. Calculate the RMS from the Variance:
853 const Double_t rms = var<0 ? 0 : TMath::Sqrt(var)/scale;
854
855 // abOffs contains only half of the signal as ped.
856 // Therefor abOffs is not the full, but the half amplitude
857 fPedestalsOut->GetAverageArea(aidx).Set(ped, rms, abOffs, nevts);
858}
859
860// ---------------------------------------------------------------------------------
861//
862// Calculates for sector idx "sector" with "nspix" valid pixels:
863//
864// Ped per slice = sum / nevts / fExtractWinSize / nspix;
865// RMS per slice = sqrt { (sum2 - sum*sum/nevts) / (nevts-1) / fExtractWinSize / nspix }
866// ABOffset per slice = (fSumAB0[idx] - fSumAB1[idx]) / nevts / fExtractWinSize / nspix;
867//
868// Stores the results in MPedestalCam::GetAverageSector(sector)
869//
870void MExtractPedestal::CalcSectorResults(const UInt_t sector)
871{
872 const UInt_t nevts = fSectorFilled[sector];
873 if (nevts<2)
874 return;
875
876 const UInt_t nspix = fSectorValid[sector];
877 if (nspix<1)
878 return;
879
880 const Double_t sum = fSectorSumx[sector];
881 const Double_t sum2 = fSectorSumx2[sector];
882
883 // 1. Calculate the mean of the sums:
884 Double_t ped = sum/nevts;
885
886 // 2. Calculate the Variance of the sums:
887 Double_t var = (sum2/nspix-sum*sum/nevts)/(nevts-1.);
888
889 // 3. Calculate the amplitude of the 150MHz "AB" noise
890 Double_t abOffs = (fSectorSumAB0[sector] - fSectorSumAB1[sector]) / nevts;
891
892 // 4. Scale the mean, variance and AB-noise to the number of slices:
893 ped /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
894 var /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
895 abOffs /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
896 // The pedestal extracted with the extractor is divided by
897 // the number of hi-gain samples because the calibration
898 // multiplies by this number
899
900 // scale to 256
901 const UInt_t scale = fExtractor ? 1 : fRunHeader->GetScale();
902
903 // 5. Scale the mean, variance and AB-noise to the number of pixels:
904 ped /= nspix*scale;
905 abOffs /= nspix*scale;
906
907 // 6. Calculate the RMS from the Variance:
908 const Double_t rms = var<0 ? 0 : TMath::Sqrt(var)/scale;
909
910 // abOffs contains only half of the signal as ped.
911 // Therefor abOffs is not the full, but the half amplitude
912 fPedestalsOut->GetAverageSector(sector).Set(ped, rms, abOffs, nevts);
913}
914
915// --------------------------------------------------------------------------
916//
917// Loop over the pixels to get the averaged pedestal
918//
919void MExtractPedestal::CalcPixResult()
920{
921 for (UInt_t idx=0; idx<fNumEventsUsed.GetSize(); idx++)
922 CalcPixResults(idx);
923}
924
925// --------------------------------------------------------------------------
926//
927// Loop over the sector indices to get the averaged pedestal per sector
928//
929void MExtractPedestal::CalcSectorResult()
930{
931 for (UInt_t sector=0; sector<fSectorFilled.GetSize(); sector++)
932 CalcSectorResults(sector);
933}
934
935// --------------------------------------------------------------------------
936//
937// Loop over the (two) area indices to get the averaged pedestal per aidx
938//
939void MExtractPedestal::CalcAreaResult()
940{
941 for (UInt_t aidx=0; aidx<fAreaFilled.GetSize(); aidx++)
942 CalcAreaResults(aidx);
943}
944
945//-----------------------------------------------------------------------
946//
947void MExtractPedestal::Print(Option_t *o) const
948{
949 *fLog << GetDescriptor() << ":" << endl;
950 *fLog << "Name of interm. MPedestalCam: " << (fPedestalsInter?fPedestalsInter->GetName():fNamePedestalCamInter.Data()) << " (" << fPedestalsInter << ")" << endl;
951 *fLog << "Name of output MPedestalCam: " << (fPedestalsOut?fPedestalsOut->GetName():fNamePedestalCamOut.Data()) << " (" << fPedestalsOut << ")" << endl;
952 *fLog << "Intermediate Storage is " << (fIntermediateStorage?"on":"off") << endl;
953 *fLog << "Special pixel mode " << (fUseSpecialPixels?"on":"off") << endl;
954 if (fExtractor)
955 {
956 *fLog << "Extractor used: " << fExtractor->ClassName() << " (";
957 *fLog << (fRandomCalculation?"":"non-") << "random)" << endl;
958 }
959 *fLog << "ExtractWindow from slice " << fExtractWinFirst << " to " << fExtractWinLast << " incl." << endl;
960 *fLog << "CheckWindow from slice " << fCheckWinFirst << " to " << fCheckWinLast << " incl." << endl;
961 *fLog << "Max.allowed signal variation: " << fMaxSignalVar << endl;
962 *fLog << "Max.allowed signal absolute: " << fMaxSignalAbs << endl;
963}
964
965// --------------------------------------------------------------------------
966//
967// The following resources are available:
968// ExtractWindowFirst: 15
969// ExtractWindowSize: 6
970// PedestalUpdate: yes
971// RandomCalculation: yes
972//
973Int_t MExtractPedestal::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
974{
975 Bool_t rc=kFALSE;
976
977 // find resource for fUseSpecialPixels
978 if (IsEnvDefined(env, prefix, "UseSpecialPixels", print))
979 {
980 SetUseSpecialPixels(GetEnvValue(env, prefix, "UseSpecialPixels", fUseSpecialPixels));
981 rc = kTRUE;
982 }
983
984 if (IsEnvDefined(env, prefix, "IntermediateStorage", print))
985 {
986 SetIntermediateStorage(GetEnvValue(env, prefix, "IntermediateStorage", fIntermediateStorage));
987 rc = kTRUE;
988 }
989
990 // find resource for random calculation
991 if (IsEnvDefined(env, prefix, "RandomCalculation", print))
992 {
993 SetRandomCalculation(GetEnvValue(env, prefix, "RandomCalculation", fRandomCalculation));
994 rc = kTRUE;
995 }
996
997 // Find resources for ExtractWindow
998 Int_t ef = fExtractWinFirst;
999 Int_t es = fExtractWinSize;
1000 if (IsEnvDefined(env, prefix, "ExtractWinFirst", print))
1001 {
1002 ef = GetEnvValue(env, prefix, "ExtractWinFirst", ef);
1003 rc = kTRUE;
1004 }
1005 if (IsEnvDefined(env, prefix, "ExtractWinSize", print))
1006 {
1007 es = GetEnvValue(env, prefix, "ExtractWinSize", es);
1008 rc = kTRUE;
1009 }
1010
1011 SetExtractWindow(ef,es);
1012
1013 // Find resources for CheckWindow
1014 Int_t cfs = fCheckWinFirst;
1015 Int_t cls = fCheckWinLast;
1016 if (IsEnvDefined(env, prefix, "CheckWinFirst", print))
1017 {
1018 cfs = GetEnvValue(env, prefix, "CheckWinFirst", cfs);
1019 rc = kTRUE;
1020 }
1021 if (IsEnvDefined(env, prefix, "CheckWinLast", print))
1022 {
1023 cls = GetEnvValue(env, prefix, "CheckWinLast", cls);
1024 rc = kTRUE;
1025 }
1026
1027 SetCheckRange(cfs,cls);
1028
1029 // find resource for maximum signal variation
1030 if (IsEnvDefined(env, prefix, "MaxSignalVar", print))
1031 {
1032 SetMaxSignalVar(GetEnvValue(env, prefix, "MaxSignalVar", fMaxSignalVar));
1033 rc = kTRUE;
1034 }
1035
1036 if (IsEnvDefined(env, prefix, "MaxSignalAbs", print))
1037 {
1038 SetMaxSignalAbs(GetEnvValue(env, prefix, "MaxSignalAbs", fMaxSignalAbs));
1039 rc = kTRUE;
1040 }
1041
1042 // find resource for MPedestalCam
1043 if (IsEnvDefined(env, prefix, "NamePedestalCamInter", print))
1044 {
1045 SetNamePedestalCamInter(GetEnvValue(env, prefix, "NamePedestalCamInter", fNamePedestalCamInter));
1046 rc = kTRUE;
1047 }
1048
1049 if (IsEnvDefined(env, prefix, "NamePedestalCamOut", print))
1050 {
1051 SetNamePedestalCamOut(GetEnvValue(env, prefix, "NamePedestalCamOut", fNamePedestalCamOut));
1052 rc = kTRUE;
1053 }
1054
1055 return rc;
1056}
Note: See TracBrowser for help on using the repository browser.