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

Last change on this file since 8785 was 8744, checked in by tbretz, 17 years ago
*** empty log message ***
File size: 35.2 KB
Line 
1/* ======================================================================== *\
2! $Name: not supported by cvs2svn $:$Id: MExtractPedestal.cc,v 1.36 2007-09-07 12:17:16 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 // Check upper bound for check window
353 if (fCheckWinLast >= num)
354 {
355 *fLog << inf << "CheckWindow [" << fCheckWinFirst << "," << fCheckWinLast;
356 *fLog << "] out of range [0," << num-1 << "]... ";
357 *fLog << "reset upper edge to " << num-1 << "." << endl;
358
359 fCheckWinLast = num-1;
360 }
361
362 // Now check lower bound for check window
363 if (fCheckWinFirst>fCheckWinLast)
364 {
365 *fLog << err << "CheckWindow first slice " << fCheckWinFirst;
366 *fLog << " greater than last slice " << fCheckWinLast;
367 *fLog << "... reset to 0." << endl;
368
369 fCheckWinFirst = 0;
370 }
371
372 // check upper bound for extaction window
373 if (fExtractWinLast+offset >= num)
374 {
375 *fLog << inf << "ExtractWindow [" << fExtractWinFirst+offset << "," << fExtractWinLast+offset;
376 *fLog << "] out of range [0," << num-1 << "]... ";
377 *fLog << "reset upper edge to " << num-1 << "." << endl;
378
379 fExtractWinLast = num-offset-1;
380 }
381
382 // Now check lower bound for check window
383 if (fExtractWinFirst>fExtractWinLast)
384 {
385 *fLog << err << "ExtractWindow first slice " << fExtractWinFirst+offset;
386 *fLog << " greater than last slice " << fExtractWinLast+offset;
387 *fLog << "... reset to 0." << endl;
388
389 fExtractWinFirst = 0;
390 }
391
392 // Calculate window size for extraction window
393 fExtractWinSize = fExtractWinLast-fExtractWinFirst+1;
394
395 // Check if use tries to do a fundamental pedestal extraction
396 // with an odd number of slices
397 if (fExtractor || TMath::Even(fExtractWinSize))
398 return;
399
400 // Make sure the number of extracted slices is even
401 fExtractWinLast += offset+fExtractWinLast==num-1 ? -1 : +1;
402
403 *fLog << inf << "ExtractionWindow odd... set to [";
404 *fLog << fExtractWinFirst+offset << "," << fExtractWinLast+offset << "]" << endl;
405
406 fExtractWinSize = fExtractWinLast-fExtractWinFirst+1;
407}
408
409// --------------------------------------------------------------------------
410//
411// Look for the following input containers:
412//
413// - MRawEvtData
414// - MRawRunHeader
415// - MRawEvtHeader
416// - MGeomCam
417//
418// The following output containers are also searched and created if
419// they were not found:
420//
421// - MPedestalCam with the name fPedContainerName
422//
423Int_t MExtractPedestal::PreProcess(MParList *pList)
424{
425
426 Clear();
427
428 fRawEvt = (MRawEvtData*)pList->FindObject(AddSerialNumber(fNameRawEvtData));
429 if (!fRawEvt)
430 {
431 *fLog << err << AddSerialNumber(fNameRawEvtData) << " not found... aborting." << endl;
432 return kFALSE;
433 }
434
435 fRunHeader = (MRawRunHeader*)pList->FindObject(AddSerialNumber("MRawRunHeader"));
436 if (!fRunHeader)
437 {
438 *fLog << err << AddSerialNumber("MRawRunHeader") << " not found... aborting." << endl;
439 return kFALSE;
440 }
441
442 fGeom = (MGeomCam*)pList->FindObject(AddSerialNumber("MGeomCam"));
443 if (!fGeom)
444 {
445 *fLog << err << AddSerialNumber("MGeomCam") << " not found... aborting." << endl;
446 return kFALSE;
447 }
448
449 fSignal = (MPedestalSubtractedEvt*)pList->FindObject(AddSerialNumber("MPedestalSubtractedEvt"));
450 if (!fSignal)
451 {
452 *fLog << err << AddSerialNumber("MPedestalSubtractedEvt") << " not found... aborting." << endl;
453 return kFALSE;
454 }
455
456 if (!fPedestalsInter && fIntermediateStorage)
457 {
458 fPedestalsInter = (MPedestalCam*)pList->FindCreateObj("MPedestalCam", AddSerialNumber(fNamePedestalCamInter));
459 if (!fPedestalsInter)
460 return kFALSE;
461 }
462
463 if (!fPedestalsOut)
464 {
465 fPedestalsOut = (MPedestalCam*)pList->FindCreateObj("MPedestalCam", AddSerialNumber(fNamePedestalCamOut));
466 if (!fPedestalsOut)
467 return kFALSE;
468 }
469
470 return fExtractor ? fExtractor->CallPreProcess(pList) : kTRUE;
471}
472
473//-----------------------------------------------------------------------
474//
475// Call Calc(). If fExtractor!=NULL enclose the call in setting the
476// NoiseCalculation to fRandomCalculation
477//
478Int_t MExtractPedestal::Process()
479{
480 //
481 // Necessary check for extraction of special pixels
482 // together with data which does not yet have them
483 //
484 if (fSumx.GetSize()==0)
485 return kTRUE;
486
487 if (fExtractor)
488 fExtractor->SetNoiseCalculation(fRandomCalculation);
489
490 const Int_t rc = Calc();
491
492 if (fExtractor)
493 fExtractor->SetNoiseCalculation(kFALSE);
494
495 return rc;
496}
497
498// ---------------------------------------------------------------------------------
499//
500// Sets the size (from MPedestalCam::GetSize() ) and resets the following arrays:
501// - fSumx
502// - fSumx2
503// - fSumAB0
504// - fSumAB1
505// - fAreaSumx
506// - fAreaSumx2
507// - fAreaSumAB0
508// - fAreaSumAB1
509// - fAreaFilled
510// - fAreaValid
511// - fSectorSumx
512// - fSectorSumx2
513// - fSectorSumAB0
514// - fSectorSumAB1
515// - fSectorFilled
516// - fSectorValid
517//
518Bool_t MExtractPedestal::ReInit(MParList *pList)
519{
520 // Necessary check for special pixels which might not yet have existed
521 if (!fRawEvt)
522 {
523 if (fRunHeader->GetFormatVersion() > 3)
524 return kTRUE;
525
526 *fLog << err << "ERROR - " << fNameRawEvtData << " [MRawEvtData] has not ";
527 *fLog << "been found and format version > 3... abort." << endl;
528 return kFALSE;
529 }
530
531 // If the size is not yet set, set the size
532 if (fSumx.GetSize()==0)
533 {
534 // Initialize the normal pixels (size of MPedestalCam already set by MGeomApply)
535 const Int_t npixels = fPedestalsOut->GetSize();
536
537 fSumx. Set(npixels);
538 fSumx2. Set(npixels);
539 fSumAB0.Set(npixels);
540 fSumAB1.Set(npixels);
541
542 fNumEventsUsed.Set(npixels);
543
544 if (fUseSpecialPixels)
545 {
546 // Initialize size of MPedestalCam in case of special pixels (not done by MGeomApply)
547 const UShort_t nspecial = fRunHeader->GetNumSpecialPixels();
548 if (nspecial == 0)
549 {
550 *fLog << warn << "WARNING - Number of special pixels is 0." << endl;
551 return kTRUE;
552 }
553
554 fPedestalsOut->InitSize((UInt_t)nspecial);
555 }
556 else
557 {
558 // Initialize the averaged areas and sectors (do not exist for special pixels)
559 const Int_t areas = fPedestalsOut->GetNumAverageArea();
560 const Int_t sectors = fPedestalsOut->GetNumAverageSector();
561
562 fAreaSumx. Set(areas);
563 fAreaSumx2. Set(areas);
564 fAreaSumAB0.Set(areas);
565 fAreaSumAB1.Set(areas);
566 fAreaFilled.Set(areas);
567 fAreaValid .Set(areas);
568
569 fSectorSumx. Set(sectors);
570 fSectorSumx2. Set(sectors);
571 fSectorSumAB0.Set(sectors);
572 fSectorSumAB1.Set(sectors);
573 fSectorFilled.Set(sectors);
574 fSectorValid .Set(sectors);
575
576 for (Int_t i=0; i<npixels; i++)
577 {
578 const UInt_t aidx = (*fGeom)[i].GetAidx();
579 const UInt_t sector = (*fGeom)[i].GetSector();
580
581 fAreaValid [aidx] ++;
582 fSectorValid[sector]++;
583 }
584 }
585 }
586
587 if (fExtractor)
588 {
589 *fLog << all << fExtractor->ClassName() << "... " << flush;
590 if (!fExtractor->ReInit(pList))
591 return kFALSE;
592
593 SetRangeFromExtractor(*fExtractor);
594
595 // fSignal->GetNumSamples() not yet initialized!!!
596 const UInt_t num = fRunHeader->GetNumSamples();
597 if (fExtractWinLast >= num)
598 {
599 *fLog << err;
600 *fLog << "ERROR - Selected fExtractWinLast " << fExtractWinLast;
601 *fLog << " out of range (>=" << num<< ")." << endl;
602 return kFALSE;
603 }
604 }
605 else
606 if (fRunHeader->GetNumSamplesLoGain()==0 && (fCheckWinFirst!=0 || fCheckWinLast!=0))
607 {
608 *fLog << inf << "Data has no lo-gains... resetting check window to extraction window." << endl;
609 SetCheckRange(fExtractWinFirst, fExtractWinLast);
610 }
611
612 //CheckExtractionWindow();
613
614 return kTRUE;
615}
616
617// ---------------------------------------------------------------------------------
618//
619// PostProcess the extractor if available
620//
621Int_t MExtractPedestal::PostProcess()
622{
623 return fExtractor ? fExtractor->CallPostProcess() : kTRUE;
624}
625
626// ---------------------------------------------------------------------------------
627//
628// Check whether the signal variation between fCheckWinFirst and fCheckWinLast
629// exceeds fMaxSignalVar or the signal is greater than fMaxSignalAbs
630//
631Bool_t MExtractPedestal::CheckVariation(UInt_t idx) const
632{
633 // This is the fast workaround to put hi- and lo-gains together
634 USample_t *slices = fSignal->GetSamplesRaw(idx);
635
636 // Start 'real' work
637 USample_t max = 0;
638 USample_t min = (USample_t)-1;
639
640 // Find the maximum and minimum signal per slice in the high gain window
641 for (USample_t *slice=slices+fCheckWinFirst; slice<=slices+fCheckWinLast; slice++)
642 {
643 if (*slice > max)
644 max = *slice;
645 if (*slice < min)
646 min = *slice;
647 }
648
649 max /= fRunHeader->GetScale();
650 min /= fRunHeader->GetScale();
651
652 // If the maximum in the high gain window is smaller than
653 return max-min<fMaxSignalVar && max<fMaxSignalAbs;
654}
655
656// ---------------------------------------------------------------------------------
657//
658// Invoke the hi-gain extraction starting at fExtractWinFirst+offset
659// for fExtractWinLast-fExtractWinFirst+1 slices. If Noise calculation
660// is set it is up to the signal extractor to do the right thing.
661//
662// Returns the extracted signal.
663//
664Float_t MExtractPedestal::CalcExtractor(const MRawEvtPixelIter &pixel, Int_t offset) const
665{
666 // Use the same extraction window as for signal extraction
667 const Int_t first = fExtractWinFirst;
668 const Int_t last = fExtractWinLast;
669
670 const Int_t start = first+offset;
671
672 const Int_t range = last-first+1;
673
674 // This check is already done in CheckExtractionWindow
675 // if (range>pixel.GetNumSamples()-start)
676 // range = pixel.GetNumSamples()-start;
677
678 const Int_t idx = pixel.GetPixelId();
679
680 // Do some handling if maxpos is last slice?
681 const Int_t maxposhi = fRandomCalculation ? 0 : fSignal->GetMaxPos(idx, start, start+range-1);
682
683 const Float_t *sig = fSignal->GetSamples(idx);
684
685 // The pedestal is extracted with the hi-gain extractor (eg. digital
686 // filter weights) but from the lo-gains
687 Float_t dummy[3];
688 Float_t sum = 0;
689 fExtractor->FindTimeAndChargeHiGain2(sig+start, range, sum,
690 dummy[0], dummy[1], dummy[2],
691 0, maxposhi);
692
693 return sum;
694}
695
696// ---------------------------------------------------------------------------------
697//
698// Sum slices from fExtractWinFirst to fExtractWinLast. The total sum is
699// returned. ab0 and ab1 will contain the total sum splitted by the
700// AB-flag. If the AB-flag is invalid ab0=ab1=0 is returned.
701//
702UInt_t MExtractPedestal::CalcSums(const MRawEvtPixelIter &pixel, Int_t offset, UInt_t &ab0, UInt_t &ab1) const
703{
704 const Int_t first = fExtractWinFirst+offset;
705
706 USample_t *ptr = fSignal->GetSamplesRaw(pixel.GetPixelId())+first;
707 USample_t *end = ptr + fExtractWinSize;
708
709 Int_t abflag = pixel.HasABFlag() + first;
710
711 UInt_t ab[2] = { 0, 0 };
712 while (ptr<end)
713 ab[abflag++ & 0x1] += *ptr++;
714
715 // This check if for old data without AB-Flag in the data
716 const Bool_t valid = pixel.IsABFlagValid();
717
718 ab0 = valid ? ab[0] : 0;
719 ab1 = valid ? ab[1] : 0;
720
721 return ab[0]+ab[1];
722}
723
724// ---------------------------------------------------------------------------------
725//
726// Check for the variation of the pixel. Return kFALSE if this pixel
727// should not be used.
728// Calculate the pedestal either with the extractor or by summing slices.
729// And update all arrays.
730//
731Bool_t MExtractPedestal::CalcPixel(const MRawEvtPixelIter &pixel, Int_t offset, UInt_t usespecialpixels)
732{
733 const UInt_t idx = pixel.GetPixelId();
734 if (!CheckVariation(idx))
735 return kFALSE;
736
737 //extract pedestal
738 UInt_t ab[2];
739 const Float_t sum = fExtractor ?
740 CalcExtractor(pixel, offset) :
741 CalcSums(pixel, offset, ab[0], ab[1]);
742
743 if (fIntermediateStorage)
744 (*fPedestalsInter)[idx].Set(sum, 0, 0, fNumEventsUsed[idx]);
745
746 const Float_t sqrsum = sum*sum;
747
748 fSumx[idx] += sum;
749 fSumx2[idx] += sqrsum;
750
751 fNumEventsUsed[idx]++;
752
753 if (!fExtractor && pixel.IsABFlagValid())
754 {
755 fSumAB0[idx] += ab[0];
756 fSumAB1[idx] += ab[1];
757 }
758
759 if (usespecialpixels)
760 return kTRUE;
761
762 const UInt_t aidx = (*fGeom)[idx].GetAidx();
763 const UInt_t sector = (*fGeom)[idx].GetSector();
764
765 fAreaFilled[aidx]++;
766 fSectorFilled[sector]++;
767
768 fAreaSumx[aidx] += sum;
769 fAreaSumx2[aidx] += sqrsum;
770 fSectorSumx[sector] += sum;
771 fSectorSumx2[sector] += sqrsum;
772
773 if (!fExtractor && pixel.IsABFlagValid())
774 {
775 fAreaSumAB0[aidx] += ab[0];
776 fAreaSumAB1[aidx] += ab[1];
777 fSectorSumAB0[aidx] += ab[0];
778 fSectorSumAB1[aidx] += ab[1];
779 }
780
781 return kTRUE;
782}
783
784// ---------------------------------------------------------------------------------
785//
786// Calculates for pixel "idx":
787//
788// Ped per slice = sum / n / fExtractWinSize;
789// RMS per slice = sqrt { (sum2 - sum*sum/n) / (n-1) / fExtractWinSize }
790// ABOffset per slice = (fSumAB0[idx] - fSumAB1[idx]) / n / fExtractWinSize;
791//
792// Stores the results in MPedestalCam[pixid]
793//
794void MExtractPedestal::CalcPixResults(const UInt_t pixid)
795{
796 const UInt_t nevts = fNumEventsUsed[pixid];
797 if (nevts<2)
798 return;
799
800 const Double_t sum = fSumx[pixid];
801 const Double_t sum2 = fSumx2[pixid];
802
803 // 1. Calculate the mean of the sums:
804 Double_t ped = sum/nevts;
805
806 // 2. Calculate the Variance of the sums:
807 Double_t var = (sum2-sum*sum/nevts)/(nevts-1.);
808
809 // 3. Calculate the amplitude of the 150MHz "AB" noise
810 Double_t abOffs = (fSumAB0[pixid] - fSumAB1[pixid]) / nevts;
811
812 // 4. Scale the mean, variance and AB-noise to the number of slices:
813 ped /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
814 var /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
815 abOffs /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
816 // The pedestal extracted with the extractor is divided by
817 // the number of hi-gain samples because the calibration
818 // multiplies by this number
819
820 // scale to 256
821 const UInt_t scale = fExtractor ? 1 : fRunHeader->GetScale();
822
823 ped /= scale;
824 abOffs /= scale;
825
826 // 5. Calculate the RMS from the Variance:
827 const Double_t rms = var<0 ? 0 : TMath::Sqrt(var)/scale;
828
829 // abOffs contains only half of the signal as ped.
830 // Therefor abOffs is not the full, but the half amplitude
831 (*fPedestalsOut)[pixid].Set(ped, rms, abOffs, nevts);
832}
833
834// ---------------------------------------------------------------------------------
835//
836// Calculates for area idx "aidx" with "napix" valid pixels:
837//
838// Ped per slice = sum / nevts / fExtractWinSize / napix;
839// RMS per slice = sqrt { (sum2 - sum*sum/nevts) / (nevts-1) / fExtractWinSize / napix }
840// ABOffset per slice = (fSumAB0[idx] - fSumAB1[idx]) / nevts / fExtractWinSize / napix;
841//
842// Stores the results in MPedestalCam::GetAverageArea(aidx)
843//
844void MExtractPedestal::CalcAreaResults(const UInt_t aidx)
845{
846 const UInt_t nevts = fAreaFilled[aidx];
847 if (nevts<2)
848 return;
849
850 const UInt_t napix = fAreaValid[aidx];
851 if (napix<1)
852 return;
853
854 const Double_t sum = fAreaSumx[aidx];
855 const Double_t sum2 = fAreaSumx2[aidx];
856
857 // 1. Calculate the mean of the sums:
858 Double_t ped = sum/nevts;
859
860 // 2. Calculate the Variance of the sums:
861 Double_t var = (sum2/napix-sum*sum/nevts)/(nevts-1.);
862
863 // 3. Calculate the amplitude of the 150MHz "AB" noise
864 Double_t abOffs = (fAreaSumAB0[aidx] - fAreaSumAB1[aidx]) / nevts;
865
866 // 4. Scale the mean, variance and AB-noise to the number of slices:
867 ped /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
868 var /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
869 abOffs /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
870 // The pedestal extracted with the extractor is divided by
871 // the number of hi-gain samples because the calibration
872 // multiplies by this number
873
874 // scale to 256
875 const UInt_t scale = fExtractor ? 1 : fRunHeader->GetScale();
876
877 // 5. Scale the mean, variance and AB-noise to the number of pixels:
878 ped /= napix*scale;
879 abOffs /= napix*scale;
880
881 // 6. Calculate the RMS from the Variance:
882 const Double_t rms = var<0 ? 0 : TMath::Sqrt(var)/scale;
883
884 // abOffs contains only half of the signal as ped.
885 // Therefor abOffs is not the full, but the half amplitude
886 fPedestalsOut->GetAverageArea(aidx).Set(ped, rms, abOffs, nevts);
887}
888
889// ---------------------------------------------------------------------------------
890//
891// Calculates for sector idx "sector" with "nspix" valid pixels:
892//
893// Ped per slice = sum / nevts / fExtractWinSize / nspix;
894// RMS per slice = sqrt { (sum2 - sum*sum/nevts) / (nevts-1) / fExtractWinSize / nspix }
895// ABOffset per slice = (fSumAB0[idx] - fSumAB1[idx]) / nevts / fExtractWinSize / nspix;
896//
897// Stores the results in MPedestalCam::GetAverageSector(sector)
898//
899void MExtractPedestal::CalcSectorResults(const UInt_t sector)
900{
901 const UInt_t nevts = fSectorFilled[sector];
902 if (nevts<2)
903 return;
904
905 const UInt_t nspix = fSectorValid[sector];
906 if (nspix<1)
907 return;
908
909 const Double_t sum = fSectorSumx[sector];
910 const Double_t sum2 = fSectorSumx2[sector];
911
912 // 1. Calculate the mean of the sums:
913 Double_t ped = sum/nevts;
914
915 // 2. Calculate the Variance of the sums:
916 Double_t var = (sum2/nspix-sum*sum/nevts)/(nevts-1.);
917
918 // 3. Calculate the amplitude of the 150MHz "AB" noise
919 Double_t abOffs = (fSectorSumAB0[sector] - fSectorSumAB1[sector]) / nevts;
920
921 // 4. Scale the mean, variance and AB-noise to the number of slices:
922 ped /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
923 var /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
924 abOffs /= fExtractor ? fExtractor->GetNumHiGainSamples() : fExtractWinSize;
925 // The pedestal extracted with the extractor is divided by
926 // the number of hi-gain samples because the calibration
927 // multiplies by this number
928
929 // scale to 256
930 const UInt_t scale = fExtractor ? 1 : fRunHeader->GetScale();
931
932 // 5. Scale the mean, variance and AB-noise to the number of pixels:
933 ped /= nspix*scale;
934 abOffs /= nspix*scale;
935
936 // 6. Calculate the RMS from the Variance:
937 const Double_t rms = var<0 ? 0 : TMath::Sqrt(var)/scale;
938
939 // abOffs contains only half of the signal as ped.
940 // Therefor abOffs is not the full, but the half amplitude
941 fPedestalsOut->GetAverageSector(sector).Set(ped, rms, abOffs, nevts);
942}
943
944// --------------------------------------------------------------------------
945//
946// Loop over the pixels to get the averaged pedestal
947//
948void MExtractPedestal::CalcPixResult()
949{
950 for (UInt_t idx=0; idx<fNumEventsUsed.GetSize(); idx++)
951 CalcPixResults(idx);
952}
953
954// --------------------------------------------------------------------------
955//
956// Loop over the sector indices to get the averaged pedestal per sector
957//
958void MExtractPedestal::CalcSectorResult()
959{
960 for (UInt_t sector=0; sector<fSectorFilled.GetSize(); sector++)
961 CalcSectorResults(sector);
962}
963
964// --------------------------------------------------------------------------
965//
966// Loop over the (two) area indices to get the averaged pedestal per aidx
967//
968void MExtractPedestal::CalcAreaResult()
969{
970 for (UInt_t aidx=0; aidx<fAreaFilled.GetSize(); aidx++)
971 CalcAreaResults(aidx);
972}
973
974//-----------------------------------------------------------------------
975//
976void MExtractPedestal::Print(Option_t *o) const
977{
978 *fLog << GetDescriptor() << ":" << endl;
979 *fLog << "Name of interm. MPedestalCam: " << (fPedestalsInter?fPedestalsInter->GetName():fNamePedestalCamInter.Data()) << " (" << fPedestalsInter << ")" << endl;
980 *fLog << "Name of output MPedestalCam: " << (fPedestalsOut?fPedestalsOut->GetName():fNamePedestalCamOut.Data()) << " (" << fPedestalsOut << ")" << endl;
981 *fLog << "Intermediate Storage is " << (fIntermediateStorage?"on":"off") << endl;
982 *fLog << "Special pixel mode " << (fUseSpecialPixels?"on":"off") << endl;
983 if (fExtractor)
984 {
985 *fLog << "Extractor used: " << fExtractor->ClassName() << " (";
986 *fLog << (fRandomCalculation?"":"non-") << "random)" << endl;
987 }
988 *fLog << "ExtractWindow from slice " << fExtractWinFirst << " to " << fExtractWinLast << " incl." << endl;
989 *fLog << "CheckWindow from slice " << fCheckWinFirst << " to " << fCheckWinLast << " incl." << endl;
990 *fLog << "Max.allowed signal variation: " << fMaxSignalVar << endl;
991 *fLog << "Max.allowed signal absolute: " << fMaxSignalAbs << endl;
992}
993
994// --------------------------------------------------------------------------
995//
996// The following resources are available:
997// ExtractWindowFirst: 15
998// ExtractWindowSize: 6
999// PedestalUpdate: yes
1000// RandomCalculation: yes
1001//
1002Int_t MExtractPedestal::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
1003{
1004 Bool_t rc=kFALSE;
1005
1006 // find resource for fUseSpecialPixels
1007 if (IsEnvDefined(env, prefix, "UseSpecialPixels", print))
1008 {
1009 SetUseSpecialPixels(GetEnvValue(env, prefix, "UseSpecialPixels", fUseSpecialPixels));
1010 rc = kTRUE;
1011 }
1012
1013 if (IsEnvDefined(env, prefix, "IntermediateStorage", print))
1014 {
1015 SetIntermediateStorage(GetEnvValue(env, prefix, "IntermediateStorage", fIntermediateStorage));
1016 rc = kTRUE;
1017 }
1018
1019 // find resource for random calculation
1020 if (IsEnvDefined(env, prefix, "RandomCalculation", print))
1021 {
1022 SetRandomCalculation(GetEnvValue(env, prefix, "RandomCalculation", fRandomCalculation));
1023 rc = kTRUE;
1024 }
1025
1026 // Find resources for ExtractWindow
1027 Int_t ef = fExtractWinFirst;
1028 Int_t es = fExtractWinSize;
1029 if (IsEnvDefined(env, prefix, "ExtractWinFirst", print))
1030 {
1031 ef = GetEnvValue(env, prefix, "ExtractWinFirst", ef);
1032 rc = kTRUE;
1033 }
1034 if (IsEnvDefined(env, prefix, "ExtractWinSize", print))
1035 {
1036 es = GetEnvValue(env, prefix, "ExtractWinSize", es);
1037 rc = kTRUE;
1038 }
1039
1040 SetExtractWindow(ef,es);
1041
1042 // Find resources for CheckWindow
1043 Int_t cfs = fCheckWinFirst;
1044 Int_t cls = fCheckWinLast;
1045 if (IsEnvDefined(env, prefix, "CheckWinFirst", print))
1046 {
1047 cfs = GetEnvValue(env, prefix, "CheckWinFirst", cfs);
1048 rc = kTRUE;
1049 }
1050 if (IsEnvDefined(env, prefix, "CheckWinLast", print))
1051 {
1052 cls = GetEnvValue(env, prefix, "CheckWinLast", cls);
1053 rc = kTRUE;
1054 }
1055
1056 SetCheckRange(cfs,cls);
1057
1058 // find resource for maximum signal variation
1059 if (IsEnvDefined(env, prefix, "MaxSignalVar", print))
1060 {
1061 SetMaxSignalVar(GetEnvValue(env, prefix, "MaxSignalVar", fMaxSignalVar));
1062 rc = kTRUE;
1063 }
1064
1065 if (IsEnvDefined(env, prefix, "MaxSignalAbs", print))
1066 {
1067 SetMaxSignalAbs(GetEnvValue(env, prefix, "MaxSignalAbs", fMaxSignalAbs));
1068 rc = kTRUE;
1069 }
1070
1071 // find resource for MPedestalCam
1072 if (IsEnvDefined(env, prefix, "NamePedestalCamInter", print))
1073 {
1074 SetNamePedestalCamInter(GetEnvValue(env, prefix, "NamePedestalCamInter", fNamePedestalCamInter));
1075 rc = kTRUE;
1076 }
1077
1078 if (IsEnvDefined(env, prefix, "NamePedestalCamOut", print))
1079 {
1080 SetNamePedestalCamOut(GetEnvValue(env, prefix, "NamePedestalCamOut", fNamePedestalCamOut));
1081 rc = kTRUE;
1082 }
1083
1084 return rc;
1085}
Note: See TracBrowser for help on using the repository browser.