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

Last change on this file since 8490 was 8490, checked in by tbretz, 17 years ago
*** empty log message ***
File size: 34.1 KB
Line 
1/* ======================================================================== *\
2! $Name: not supported by cvs2svn $:$Id: MExtractPedestal.cc,v 1.29 2007-05-11 10:25:45 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;
184
185// --------------------------------------------------------------------------
186//
187// Default constructor:
188//
189// Sets:
190// - all pointers to NULL
191//
192// Calls:
193// - Clear()
194//
195MExtractPedestal::MExtractPedestal(const char *name, const char *title)
196 : fGeom(NULL), fPedestalsIn(NULL), fPedestalsInter(NULL),
197 fPedestalsOut(NULL), fExtractor(NULL), fSignal(0),
198 fExtractWinFirst(0), fExtractWinSize(0), fUseSpecialPixels(kFALSE)
199{
200 fName = name ? name : "MExtractPedestal";
201 fTitle = title ? title : "Base class to calculate pedestals";
202
203 SetIntermediateStorage( kFALSE );
204 SetRandomCalculation ( kTRUE );
205
206 SetNamePedestalCamIn();
207 SetNamePedestalCamOut();
208 SetNamePedestalCamInter();
209 SetNameRawEvtData();
210
211 SetCheckRange(fgCheckWinFirst, fgCheckWinLast);
212 SetMaxSignalVar(fgMaxSignalVar);
213
214 Clear();
215}
216
217// --------------------------------------------------------------------------
218//
219// Call reset() of all Arays
220//
221void MExtractPedestal::ResetArrays()
222{
223 // Reset contents of arrays.
224 fSumx.Reset();
225 fSumx2.Reset();
226 fSumAB0.Reset();
227 fSumAB1.Reset();
228 fAreaSumx.Reset();
229 fAreaSumx2.Reset();
230 fAreaSumAB0.Reset();
231 fAreaSumAB1.Reset();
232 fAreaFilled.Reset();
233 fAreaValid.Reset();
234 fSectorSumx.Reset();
235 fSectorSumx2.Reset();
236 fSectorSumAB0.Reset();
237 fSectorSumAB1.Reset();
238 fSectorFilled.Reset();
239 fSectorValid.Reset();
240 fNumEventsUsed.Reset();
241}
242
243// --------------------------------------------------------------------------
244//
245// Resets Arrays:
246//
247// Sets:
248// - fRawEvt to NULL
249// - fRunHeader to NULL
250//
251void MExtractPedestal::Clear(const Option_t *o)
252{
253
254 fRawEvt = NULL;
255 fRunHeader = NULL;
256
257 // If the size is yet set, set the size
258 if (fSumx.GetSize()>0)
259 ResetArrays();
260
261}
262
263// --------------------------------------------------------------------------
264//
265// Checks:
266// - if a window is odd
267//
268Bool_t MExtractPedestal::SetExtractWindow(UShort_t windowf, UShort_t windows)
269{
270 Bool_t rc = kTRUE;
271
272 if (windows==0)
273 {
274 *fLog << warn << GetDescriptor();
275 *fLog << " - WARNING: Window size in SetExtractWindow has to be > 0... adjusting to 2!" << endl;
276 windows = 2;
277 rc = kFALSE;
278 }
279
280 fExtractWinSize = windows;
281 fExtractWinFirst = windowf;
282 fExtractWinLast = fExtractWinFirst+fExtractWinSize-1;
283
284 return rc;
285}
286
287// --------------------------------------------------------------------------
288//
289// SetCheckRange:
290//
291// Exits, if the first argument is smaller than 0
292// Exits, if the the last argument is smaller than the first
293//
294Bool_t MExtractPedestal::SetCheckRange(UShort_t chfirst, UShort_t chlast)
295{
296
297 Bool_t rc = kTRUE;
298
299 if (chlast<=chfirst)
300 {
301 *fLog << warn << GetDescriptor();
302 *fLog << " - WARNING: Last slice in SetCheckRange smaller than first slice... set to first+2" << endl;
303 chlast = chfirst+1;
304 rc = kFALSE;
305 }
306
307 fCheckWinFirst = chfirst;
308 fCheckWinLast = chlast;
309
310 return rc;
311}
312
313Bool_t MExtractPedestal::SetRangeFromExtractor(const MExtractor &ext, Bool_t logain)
314{
315 const Bool_t haslogains = ext.GetLoGainFirst()!=0 && ext.GetLoGainLast()!=0;
316
317 Bool_t rc1 = kTRUE;
318 if (!haslogains)
319 {
320 // We assume that in case without lo-gains we
321 // deal with pedestal events only
322 rc1 = SetCheckRange(ext.GetHiGainFirst(), ext.GetHiGainLast());
323 }
324
325 const Int_t f = logain && haslogains ? ext.GetLoGainFirst() : ext.GetHiGainFirst();
326 const Int_t l = logain && haslogains ? ext.GetLoGainLast() : ext.GetHiGainLast();
327
328 const Int_t w = (l-f+1);
329
330 // Setup to use the hi-gain extraction window in the lo-gain
331 // range (the start of the lo-gain range is added automatically
332 // by MPedCalcFromLoGain)
333 const Bool_t rc2 = SetExtractWindow(f, w);
334
335 return rc1 && rc2;
336}
337
338// --------------------------------------------------------------------------
339//
340// Check (and if neccesary: correct) the extraction and check ranges.
341//
342void MExtractPedestal::CheckExtractionWindow(UInt_t offset)
343{
344 *fLog << inf;
345 *fLog << "Requested CheckWindow is [" << fCheckWinFirst << "," << fCheckWinLast << "]." <<endl;
346 *fLog << "Requested ExtractWindow is [" << fExtractWinFirst+offset << "," << fExtractWinLast+offset << "]." <<endl;
347
348 // fSignal->GetNumSamples() not yet initialized!!!
349 const UInt_t num = fRunHeader->GetNumSamplesHiGain()+fRunHeader->GetNumSamplesLoGain();
350
351 if (fCheckWinLast >= num)
352 {
353 *fLog << "CheckWindow [" << fCheckWinFirst << "," << fCheckWinLast;
354 *fLog << "] out of range [0," << num-1 << "]... ";
355 *fLog << "reset upper edge." << endl;
356
357 fCheckWinLast = num-1;
358 }
359
360 if (offset+fExtractWinLast >= num)
361 {
362 *fLog << "ExtractWindow [" << fExtractWinFirst+offset << "," << fExtractWinLast+offset;
363 *fLog << "] out of range [0," << num-1 << "]... ";
364 *fLog << "reset upper edge." << endl;
365
366 fExtractWinLast = num-offset-1;
367 }
368
369 fExtractWinSize = fExtractWinLast-fExtractWinFirst+1;
370
371 if (fExtractor || TMath::Even(fExtractWinSize))
372 return;
373
374 fExtractWinLast += offset+fExtractWinLast==num-1 ? -1 : +1;
375
376 *fLog << "ExtractionWindow odd... set to [";
377 *fLog << fExtractWinFirst+offset << "," << fExtractWinLast+offset << "]" << endl;
378
379 fExtractWinSize = fExtractWinLast-fExtractWinFirst+1;
380}
381
382// --------------------------------------------------------------------------
383//
384// Look for the following input containers:
385//
386// - MRawEvtData
387// - MRawRunHeader
388// - MRawEvtHeader
389// - MGeomCam
390//
391// The following output containers are also searched and created if
392// they were not found:
393//
394// - MPedestalCam with the name fPedContainerName
395//
396Int_t MExtractPedestal::PreProcess(MParList *pList)
397{
398
399 Clear();
400
401 fRawEvt = (MRawEvtData*)pList->FindObject(AddSerialNumber(fNameRawEvtData));
402 if (!fRawEvt)
403 {
404 *fLog << err << AddSerialNumber(fNameRawEvtData) << " not found... aborting." << endl;
405 return kFALSE;
406 }
407
408 fRunHeader = (MRawRunHeader*)pList->FindObject(AddSerialNumber("MRawRunHeader"));
409 if (!fRunHeader)
410 {
411 *fLog << err << AddSerialNumber("MRawRunHeader") << " not found... aborting." << endl;
412 return kFALSE;
413 }
414
415 fGeom = (MGeomCam*)pList->FindObject(AddSerialNumber("MGeomCam"));
416 if (!fGeom)
417 {
418 *fLog << err << AddSerialNumber("MGeomCam") << " not found... aborting." << endl;
419 return kFALSE;
420 }
421
422 fSignal = (MPedestalSubtractedEvt*)pList->FindObject(AddSerialNumber("MPedestalSubtractedEvt"));
423 if (!fSignal)
424 {
425 *fLog << err << AddSerialNumber("MPedestalSubtractedEvt") << " not found... aborting." << endl;
426 return kFALSE;
427 }
428
429 if (fExtractor && !fPedestalsIn)
430 {
431 fPedestalsIn = (MPedestalCam*)pList->FindObject(AddSerialNumber(fNamePedestalCamIn), "MPedestalCam");
432 if (!fPedestalsIn)
433 {
434 *fLog << err << AddSerialNumber(fNamePedestalCamIn) << " not found... aborting." << endl;
435 return kFALSE;
436 }
437 }
438
439 if (!fPedestalsInter && fIntermediateStorage)
440 {
441 fPedestalsInter = (MPedestalCam*)pList->FindCreateObj("MPedestalCam", AddSerialNumber(fNamePedestalCamInter));
442 if (!fPedestalsInter)
443 return kFALSE;
444 }
445
446 if (!fPedestalsOut)
447 {
448 fPedestalsOut = (MPedestalCam*)pList->FindCreateObj("MPedestalCam", AddSerialNumber(fNamePedestalCamOut));
449 if (!fPedestalsOut)
450 return kFALSE;
451 }
452
453 return fExtractor ? fExtractor->CallPreProcess(pList) : kTRUE;
454}
455
456//-----------------------------------------------------------------------
457//
458// Call Calc(). If fExtractor!=NULL enclose the call in setting the
459// NoiseCalculation to fRandomCalculation
460//
461Int_t MExtractPedestal::Process()
462{
463 //
464 // Necessary check for extraction of special pixels
465 // together with data which does not yet have them
466 //
467 if (fSumx.GetSize()==0)
468 return kTRUE;
469
470 if (fExtractor)
471 fExtractor->SetNoiseCalculation(fRandomCalculation);
472
473 const Int_t rc = Calc();
474
475 if (fExtractor)
476 fExtractor->SetNoiseCalculation(kFALSE);
477
478 return rc;
479}
480
481// ---------------------------------------------------------------------------------
482//
483// Sets the size (from MPedestalCam::GetSize() ) and resets the following arrays:
484// - fSumx
485// - fSumx2
486// - fSumAB0
487// - fSumAB1
488// - fAreaSumx
489// - fAreaSumx2
490// - fAreaSumAB0
491// - fAreaSumAB1
492// - fAreaFilled
493// - fAreaValid
494// - fSectorSumx
495// - fSectorSumx2
496// - fSectorSumAB0
497// - fSectorSumAB1
498// - fSectorFilled
499// - fSectorValid
500//
501Bool_t MExtractPedestal::ReInit(MParList *pList)
502{
503 // Necessary check for special pixels which might not yet have existed
504 if (!fRawEvt)
505 {
506 if (fRunHeader->GetFormatVersion() > 3)
507 return kTRUE;
508
509 *fLog << err << "ERROR - " << fNameRawEvtData << " [MRawEvtData] has not ";
510 *fLog << "been found and format version > 3... abort." << endl;
511 return kFALSE;
512 }
513
514 // If the size is not yet set, set the size
515 if (fSumx.GetSize()==0)
516 {
517 // Initialize the normal pixels (size of MPedestalCam already set by MGeomApply)
518 const Int_t npixels = fPedestalsOut->GetSize();
519
520 fSumx. Set(npixels);
521 fSumx2. Set(npixels);
522 fSumAB0.Set(npixels);
523 fSumAB1.Set(npixels);
524
525 fNumEventsUsed.Set(npixels);
526
527 if (fUseSpecialPixels)
528 {
529 // Initialize size of MPedestalCam in case of special pixels (not done by MGeomApply)
530 const UShort_t nspecial = fRunHeader->GetNumSpecialPixels();
531 if (nspecial == 0)
532 {
533 *fLog << warn << "WARNING - Number of special pixels is 0." << endl;
534 return kTRUE;
535 }
536
537 fPedestalsOut->InitSize((UInt_t)nspecial);
538 }
539 else
540 {
541 // Initialize the averaged areas and sectors (do not exist for special pixels)
542 const Int_t areas = fPedestalsOut->GetNumAverageArea();
543 const Int_t sectors = fPedestalsOut->GetNumAverageSector();
544
545 fAreaSumx. Set(areas);
546 fAreaSumx2. Set(areas);
547 fAreaSumAB0.Set(areas);
548 fAreaSumAB1.Set(areas);
549 fAreaFilled.Set(areas);
550 fAreaValid .Set(areas);
551
552 fSectorSumx. Set(sectors);
553 fSectorSumx2. Set(sectors);
554 fSectorSumAB0.Set(sectors);
555 fSectorSumAB1.Set(sectors);
556 fSectorFilled.Set(sectors);
557 fSectorValid .Set(sectors);
558
559 for (Int_t i=0; i<npixels; i++)
560 {
561 const UInt_t aidx = (*fGeom)[i].GetAidx();
562 const UInt_t sector = (*fGeom)[i].GetSector();
563
564 fAreaValid [aidx] ++;
565 fSectorValid[sector]++;
566 }
567 }
568 }
569
570 if (fExtractor)
571 {
572 if (!fExtractor->ReInit(pList))
573 return kFALSE;
574
575 SetRangeFromExtractor(*fExtractor);
576
577 // fSignal->GetNumSamples() not yet initialized!!!
578 const UInt_t num = fRunHeader->GetNumSamples();
579 if (fExtractWinLast >= num)
580 {
581 *fLog << err;
582 *fLog << "ERROR - Selected fExtractWinLast " << fExtractWinLast;
583 *fLog << " out of range (>=" << num<< ")." << endl;
584 return kFALSE;
585 }
586 }
587 else
588 if (fRunHeader->GetNumSamplesLoGain()==0 && (fCheckWinFirst!=0 || fCheckWinLast!=0))
589 {
590 *fLog << inf << "Data has no lo-gains... resetting check window to extraction window." << endl;
591 SetCheckRange(fExtractWinFirst, fExtractWinLast);
592 }
593
594 //CheckExtractionWindow();
595
596 return kTRUE;
597}
598
599// ---------------------------------------------------------------------------------
600//
601// PostProcess the extractor if available
602//
603Int_t MExtractPedestal::PostProcess()
604{
605 fPedestalsIn = NULL;
606 return fExtractor ? fExtractor->CallPostProcess() : kTRUE;
607}
608
609// ---------------------------------------------------------------------------------
610//
611// Check whether the signal variation between fCheckWinFirst and fCheckWinLast
612// exceeds fMaxSignalVar or the signal is greater than 250
613//
614Bool_t MExtractPedestal::CheckVariation(UInt_t idx) const
615{
616 // This is the fast workaround to put hi- and lo-gains together
617 Byte_t *slices = fSignal->GetSamplesRaw(idx);
618
619 // Start 'real' work
620 UInt_t max = 0;
621 UInt_t min = (UInt_t)-1;
622
623 // Find the maximum and minimum signal per slice in the high gain window
624 for (Byte_t *slice=slices+fCheckWinFirst; slice<=slices+fCheckWinLast; slice++)
625 {
626 if (*slice > max)
627 max = *slice;
628 if (*slice < min)
629 min = *slice;
630 }
631
632 // If the maximum in the high gain window is smaller than
633 // FIXME: Precompiled value!
634 return max-min<fMaxSignalVar && max<250;
635}
636
637// ---------------------------------------------------------------------------------
638//
639// Invoke the hi-gain extraction starting at fExtractWinFirst+offset
640// for fExtractWinLast-fExtractWinFirst+1 slices. If Noise calculation
641// is set it is up to the signal extractor to do the right thing.
642//
643// Returns the extracted signal.
644//
645Float_t MExtractPedestal::CalcExtractor(const MRawEvtPixelIter &pixel, Int_t offset) const
646{
647 // Use the same extraction window as for signal extraction
648 const Int_t first = fExtractWinFirst;
649 const Int_t last = fExtractWinLast;
650
651 const Int_t start = first+offset;
652
653 const Int_t range = last-first+1;
654
655 // This check is already done in CheckExtractionWindow
656 // if (range>pixel.GetNumSamples()-start)
657 // range = pixel.GetNumSamples()-start;
658
659 const Int_t idx = pixel.GetPixelId();
660
661 // Do some handling if maxpos is last slice?
662 const Int_t maxposhi = fSignal->GetMax(idx, start, start+range-1);
663
664 const Float_t *sig = fSignal->GetSamples(idx);
665
666 // The pedestal is extracted with the hi-gain extractor (eg. digital
667 // filter weights) but from the lo-gains
668 Float_t dummy[3];
669 Float_t sum = 0;
670 fExtractor->FindTimeAndChargeHiGain2(sig+start, range, sum,
671 dummy[0], dummy[1], dummy[2],
672 0, maxposhi);
673 return sum;
674}
675
676// ---------------------------------------------------------------------------------
677//
678// Sum slices from fExtractWinFirst to fExtractWinLast. The total sum is
679// returned. ab0 and ab1 will contain the total sum splitted by the
680// AB-flag. If the AB-flag is invalid ab0=ab1=0 is returned.
681//
682UInt_t MExtractPedestal::CalcSums(const MRawEvtPixelIter &pixel, Int_t offset, UInt_t &ab0, UInt_t &ab1) const
683{
684 const Int_t first = fExtractWinFirst+offset;
685
686 Byte_t *ptr = fSignal->GetSamplesRaw(pixel.GetPixelId())+first;
687 Byte_t *end = ptr + fExtractWinSize;
688
689 Int_t abflag = pixel.HasABFlag() + first;
690
691 UInt_t ab[2] = { 0, 0 };
692 while (ptr<end)
693 ab[abflag++ & 0x1] += *ptr++;
694
695 // This check if for old data without AB-Flag in the data
696 const Bool_t valid = pixel.IsABFlagValid();
697
698 ab0 = valid ? ab[0] : 0;
699 ab1 = valid ? ab[1] : 0;
700
701 return ab[0]+ab[1];
702}
703
704// ---------------------------------------------------------------------------------
705//
706// Check for the variation of the pixel. Return kFALSE if this pixel
707// should not be used.
708// Calculate the pedestal either with the extractor or by summing slices.
709// And update all arrays.
710//
711Bool_t MExtractPedestal::CalcPixel(const MRawEvtPixelIter &pixel, Int_t offset, UInt_t usespecialpixels)
712{
713 const UInt_t idx = pixel.GetPixelId();
714 if (!CheckVariation(idx))
715 return kFALSE;
716
717 //extract pedestal
718 UInt_t ab[2];
719 const Float_t sum = fExtractor ?
720 CalcExtractor(pixel, offset) :
721 CalcSums(pixel, offset, ab[0], ab[1]);
722
723 fNumEventsUsed[idx]++;
724
725 if (fIntermediateStorage)
726 (*fPedestalsInter)[idx].Set(sum, 0, 0, fNumEventsUsed[idx]);
727
728 const Float_t sqrsum = sum*sum;
729
730 fSumx[idx] += sum;
731 fSumx2[idx] += sqrsum;
732
733 if (!fExtractor && pixel.IsABFlagValid())
734 {
735 fSumAB0[idx] += ab[0];
736 fSumAB1[idx] += ab[1];
737 }
738
739 if (usespecialpixels)
740 return kTRUE;
741
742 const UInt_t aidx = (*fGeom)[idx].GetAidx();
743 const UInt_t sector = (*fGeom)[idx].GetSector();
744
745 fAreaFilled[aidx]++;
746 fSectorFilled[sector]++;
747
748 fAreaSumx[aidx] += sum;
749 fAreaSumx2[aidx] += sqrsum;
750 fSectorSumx[sector] += sum;
751 fSectorSumx2[sector] += sqrsum;
752
753 if (!fExtractor && pixel.IsABFlagValid())
754 {
755 fAreaSumAB0[aidx] += ab[0];
756 fAreaSumAB1[aidx] += ab[1];
757 fSectorSumAB0[aidx] += ab[0];
758 fSectorSumAB1[aidx] += ab[1];
759 }
760
761 return kTRUE;
762}
763
764// ---------------------------------------------------------------------------------
765//
766// Calculates for pixel "idx":
767//
768// Ped per slice = sum / n / fExtractWinSize;
769// RMS per slice = sqrt { (sum2 - sum*sum/n) / (n-1) / fExtractWinSize }
770// ABOffset per slice = (fSumAB0[idx] - fSumAB1[idx]) / n / fExtractWinSize;
771//
772// Stores the results in MPedestalCam[pixid]
773//
774void MExtractPedestal::CalcPixResults(const UInt_t pixid)
775{
776 const UInt_t nevts = fNumEventsUsed[pixid];
777 if (nevts<2)
778 return;
779
780 const Double_t sum = fSumx[pixid];
781 const Double_t sum2 = fSumx2[pixid];
782
783 // 1. Calculate the mean of the sums:
784 Double_t ped = sum/nevts;
785
786 // 2. Calculate the Variance of the sums:
787 Double_t var = (sum2-sum*sum/nevts)/(nevts-1.);
788
789 // 3. Calculate the amplitude of the 150MHz "AB" noise
790 Double_t abOffs = (fSumAB0[pixid] - fSumAB1[pixid]) / nevts;
791
792 // 4. Scale the mean, variance and AB-noise to the number of slices:
793 ped /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
794 var /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
795 abOffs /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
796 // The pedestal extracted with the extractor is divided by
797 // the number of hi-gain samples because the calibration
798 // multiplies by this number
799
800 // 5. Calculate the RMS from the Variance:
801 const Double_t rms = var<0 ? 0 : TMath::Sqrt(var);
802
803 // abOffs contains only half of the signal as ped.
804 // Therefor abOffs is not the full, but the half amplitude
805 (*fPedestalsOut)[pixid].Set(ped, rms, abOffs, nevts);
806}
807
808// ---------------------------------------------------------------------------------
809//
810// Calculates for area idx "aidx" with "napix" valid pixels:
811//
812// Ped per slice = sum / nevts / fExtractWinSize / napix;
813// RMS per slice = sqrt { (sum2 - sum*sum/nevts) / (nevts-1) / fExtractWinSize / napix }
814// ABOffset per slice = (fSumAB0[idx] - fSumAB1[idx]) / nevts / fExtractWinSize / napix;
815//
816// Stores the results in MPedestalCam::GetAverageArea(aidx)
817//
818void MExtractPedestal::CalcAreaResults(const UInt_t aidx)
819{
820 const UInt_t nevts = fAreaFilled[aidx];
821 if (nevts<2)
822 return;
823
824 const UInt_t napix = fAreaValid[aidx];
825 if (napix<1)
826 return;
827
828 const Double_t sum = fAreaSumx[aidx];
829 const Double_t sum2 = fAreaSumx2[aidx];
830
831 // 1. Calculate the mean of the sums:
832 Double_t ped = sum/nevts;
833
834 // 2. Calculate the Variance of the sums:
835 Double_t var = (sum2/napix-sum*sum/nevts)/(nevts-1.);
836
837 // 3. Calculate the amplitude of the 150MHz "AB" noise
838 Double_t abOffs = (fAreaSumAB0[aidx] - fAreaSumAB1[aidx]) / nevts;
839
840 // 4. Scale the mean, variance and AB-noise to the number of slices:
841 ped /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
842 var /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
843 abOffs /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
844 // The pedestal extracted with the extractor is divided by
845 // the number of hi-gain samples because the calibration
846 // multiplies by this number
847
848 // 5. Scale the mean, variance and AB-noise to the number of pixels:
849 ped /= napix;
850 var /= napix;
851 abOffs /= napix;
852
853 // 6. Calculate the RMS from the Variance:
854 const Double_t rms = var<0 ? 0 : TMath::Sqrt(var);
855
856 // abOffs contains only half of the signal as ped.
857 // Therefor abOffs is not the full, but the half amplitude
858 fPedestalsOut->GetAverageArea(aidx).Set(ped, rms, abOffs, nevts);
859}
860
861// ---------------------------------------------------------------------------------
862//
863// Calculates for sector idx "sector" with "nspix" valid pixels:
864//
865// Ped per slice = sum / nevts / fExtractWinSize / nspix;
866// RMS per slice = sqrt { (sum2 - sum*sum/nevts) / (nevts-1) / fExtractWinSize / nspix }
867// ABOffset per slice = (fSumAB0[idx] - fSumAB1[idx]) / nevts / fExtractWinSize / nspix;
868//
869// Stores the results in MPedestalCam::GetAverageSector(sector)
870//
871void MExtractPedestal::CalcSectorResults(const UInt_t sector)
872{
873 const UInt_t nevts = fSectorFilled[sector];
874 if (nevts<2)
875 return;
876
877 const UInt_t nspix = fSectorValid[sector];
878 if (nspix<1)
879 return;
880
881 const Double_t sum = fSectorSumx[sector];
882 const Double_t sum2 = fSectorSumx2[sector];
883
884 // 1. Calculate the mean of the sums:
885 Double_t ped = sum/nevts;
886
887 // 2. Calculate the Variance of the sums:
888 Double_t var = (sum2/nspix-sum*sum/nevts)/(nevts-1.);
889
890 // 3. Calculate the amplitude of the 150MHz "AB" noise
891 Double_t abOffs = (fSectorSumAB0[sector] - fSectorSumAB1[sector]) / nevts;
892
893 // 4. Scale the mean, variance and AB-noise to the number of slices:
894 ped /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
895 var /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
896 abOffs /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
897 // The pedestal extracted with the extractor is divided by
898 // the number of hi-gain samples because the calibration
899 // multiplies by this number
900
901 // 5. Scale the mean, variance and AB-noise to the number of pixels:
902 ped /= nspix;
903 var /= nspix;
904 abOffs /= nspix;
905
906 // 6. Calculate the RMS from the Variance:
907 const Double_t rms = var<0 ? 0 : TMath::Sqrt(var);
908
909 // abOffs contains only half of the signal as ped.
910 // Therefor abOffs is not the full, but the half amplitude
911 fPedestalsOut->GetAverageSector(sector).Set(ped, rms, abOffs, nevts);
912}
913
914// --------------------------------------------------------------------------
915//
916// Loop over the pixels to get the averaged pedestal
917//
918void MExtractPedestal::CalcPixResult()
919{
920 for (UInt_t idx=0; idx<fNumEventsUsed.GetSize(); idx++)
921 CalcPixResults(idx);
922}
923
924// --------------------------------------------------------------------------
925//
926// Loop over the sector indices to get the averaged pedestal per sector
927//
928void MExtractPedestal::CalcSectorResult()
929{
930 for (UInt_t sector=0; sector<fSectorFilled.GetSize(); sector++)
931 CalcSectorResults(sector);
932}
933
934// --------------------------------------------------------------------------
935//
936// Loop over the (two) area indices to get the averaged pedestal per aidx
937//
938void MExtractPedestal::CalcAreaResult()
939{
940 for (UInt_t aidx=0; aidx<fAreaFilled.GetSize(); aidx++)
941 CalcAreaResults(aidx);
942}
943
944//-----------------------------------------------------------------------
945//
946void MExtractPedestal::Print(Option_t *o) const
947{
948 *fLog << GetDescriptor() << ":" << endl;
949 *fLog << "Name of input MPedestalCam: " << (fPedestalsIn?fPedestalsIn->GetName():fNamePedestalCamIn.Data()) << " (" << fPedestalsIn << ")" << 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}
963
964// --------------------------------------------------------------------------
965//
966// The following resources are available:
967// ExtractWindowFirst: 15
968// ExtractWindowSize: 6
969// PedestalUpdate: yes
970// RandomCalculation: yes
971//
972Int_t MExtractPedestal::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
973{
974 Bool_t rc=kFALSE;
975
976 // find resource for fUseSpecialPixels
977 if (IsEnvDefined(env, prefix, "UseSpecialPixels", print))
978 {
979 SetUseSpecialPixels(GetEnvValue(env, prefix, "UseSpecialPixels", fUseSpecialPixels));
980 rc = kTRUE;
981 }
982
983 if (IsEnvDefined(env, prefix, "IntermediateStorage", print))
984 {
985 SetIntermediateStorage(GetEnvValue(env, prefix, "IntermediateStorage", fIntermediateStorage));
986 rc = kTRUE;
987 }
988
989 // find resource for random calculation
990 if (IsEnvDefined(env, prefix, "RandomCalculation", print))
991 {
992 SetRandomCalculation(GetEnvValue(env, prefix, "RandomCalculation", fRandomCalculation));
993 rc = kTRUE;
994 }
995
996 // Find resources for ExtractWindow
997 Int_t ef = fExtractWinFirst;
998 Int_t es = fExtractWinSize;
999 if (IsEnvDefined(env, prefix, "ExtractWinFirst", print))
1000 {
1001 ef = GetEnvValue(env, prefix, "ExtractWinFirst", ef);
1002 rc = kTRUE;
1003 }
1004 if (IsEnvDefined(env, prefix, "ExtractWinSize", print))
1005 {
1006 es = GetEnvValue(env, prefix, "ExtractWinSize", es);
1007 rc = kTRUE;
1008 }
1009
1010 SetExtractWindow(ef,es);
1011
1012 // Find resources for CheckWindow
1013 Int_t cfs = fCheckWinFirst;
1014 Int_t cls = fCheckWinLast;
1015 if (IsEnvDefined(env, prefix, "CheckWinFirst", print))
1016 {
1017 cfs = GetEnvValue(env, prefix, "CheckWinFirst", cfs);
1018 rc = kTRUE;
1019 }
1020 if (IsEnvDefined(env, prefix, "CheckWinLast", print))
1021 {
1022 cls = GetEnvValue(env, prefix, "CheckWinLast", cls);
1023 rc = kTRUE;
1024 }
1025
1026 SetCheckRange(cfs,cls);
1027
1028 // find resource for maximum signal variation
1029 if (IsEnvDefined(env, prefix, "MaxSignalVar", print))
1030 {
1031 SetMaxSignalVar(GetEnvValue(env, prefix, "MaxSignalVar", fMaxSignalVar));
1032 rc = kTRUE;
1033 }
1034
1035 // find resource for MPedestalCam
1036 if (IsEnvDefined(env, prefix, "NamePedestalCamIn", print))
1037 {
1038 SetNamePedestalCamIn(GetEnvValue(env, prefix, "NamePedestalCamIn", fNamePedestalCamIn));
1039 rc = kTRUE;
1040 }
1041
1042 if (IsEnvDefined(env, prefix, "NamePedestalCamInter", print))
1043 {
1044 SetNamePedestalCamInter(GetEnvValue(env, prefix, "NamePedestalCamInter", fNamePedestalCamInter));
1045 rc = kTRUE;
1046 }
1047
1048 if (IsEnvDefined(env, prefix, "NamePedestalCamOut", print))
1049 {
1050 SetNamePedestalCamOut(GetEnvValue(env, prefix, "NamePedestalCamOut", fNamePedestalCamOut));
1051 rc = kTRUE;
1052 }
1053
1054 return rc;
1055}
1056
Note: See TracBrowser for help on using the repository browser.