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

Last change on this file since 5381 was 5381, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 19.5 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Josep Flix 04/2001 <mailto:jflix@ifae.es>
19! Author(s): Thomas Bretz 05/2001 <mailto:tbretz@astro.uni-wuerzburg.de>
20! Author(s): Sebastian Commichau 12/2003
21! Author(s): Javier Rico 01/2004 <mailto:jrico@ifae.es>
22! Author(s): Markus Gaug 01/2004 <mailto:markus@ifae.es>
23! Author(s): Florian Goebel 06/2004 <mailto:fgoebel@mppmu.mpg.de>
24! Author(s): Nepomuk Otte 10/2004 <mailto:otte@mppmu.mpg.de>
25!
26! Copyright: MAGIC Software Development, 2000-2004
27!
28!
29\* ======================================================================== */
30
31/////////////////////////////////////////////////////////////////////////////
32//
33// MPedCalcLoGain
34//
35// This task is devide form MPedCalcPedRun, described below. However, It
36// calculates the pedstals using the low gain slices, whenever the difference
37// between the highest and the lowest slice in the high gain
38// slices is below a given threshold (SetMaxHiGainVar). In this case the receiver
39// boards do not switch to lo gain and the so called lo gain slices are actually
40// high gain slices.
41//
42// MPedCalcLoGain also fills the ABoffset in MPedestalPix which allows to correct
43// the 150 MHz clock noise.
44//
45// This task takes a pedestal run file and fills MPedestalCam during
46// the Process() with the pedestal and rms computed in an event basis.
47// In the PostProcess() MPedestalCam is finally filled with the pedestal
48// mean and rms computed in a run basis.
49// More than one run (file) can be merged
50//
51// MPedCalcPedRun applies the following formula (1):
52//
53// Pedestal per slice = sum(x_i) / n / slices
54// PedRMS per slice = Sqrt( ( sum(x_i^2) - sum(x_i)^2/n ) / n-1 / slices )
55//
56// where x_i is the sum of "slices" FADC slices and sum means the sum over all
57// events. "n" is the number of events, "slices" is the number of summed FADC samples.
58//
59// Note that the slice-to-slice fluctuations are not Gaussian, but Poissonian, thus
60// asymmetric and they are correlated.
61//
62// It is important to know that the Pedestal per slice and PedRMS per slice depend
63// on the number of used FADC slices, as seen in the following plots:
64//
65//Begin_Html
66/*
67<img src="images/PedestalStudyInner.gif">
68*/
69//End_Html
70//
71//Begin_Html
72/*
73<img src="images/PedestalStudyOuter.gif">
74*/
75//
76// The plots show the inner and outer pixels, respectivly and have the following meaning:
77//
78// 1) The calculated mean pedestal per slice (from MPedCalcFromLoGain)
79// 2) The fitted mean pedestal per slice (from MHPedestalCam)
80// 3) The calculated pedestal RMS per slice (from MPedCalcFromLoGain)
81// 4) The fitted sigma of the pedestal distribution per slice
82// (from MHPedestalCam)
83// 5) The relative difference between calculation and histogram fit
84// for the mean
85// 6) The relative difference between calculation and histogram fit
86// for the sigma or RMS, respectively.
87//
88// The calculated means do not change significantly except for the case of 2 slices,
89// however the RMS changes from 5.7 per slice in the case of 2 extracted slices
90// to 8.3 per slice in the case of 26 extracted slices. This change is very significant.
91//
92// The plots have been produced on run 20123. You can reproduce them using
93// the macro pedestalstudies.C
94//
95// Usage of this class:
96// ====================
97//
98//
99// fCheckWinFirst = fgCheckWinFirst = 0
100// fHiGainLast = fgCheckWinLast = 29
101// fExtractWinFirst = fgExtractWinFirst = 0
102// fExtractWinSize = fgExtractWinSize = 6
103// fMaxSignalVar = fgMaxSignalVar = 40;
104//
105// Call:
106// SetCheckRange(fCheckWinFirst,fCheckWinLast);
107// to set the Window in which a signal is searched
108//
109// SetExtractWindow(fExtractWindFirst,fExtractWinSize);
110// to set the Window from which a signal is extracted
111//
112// SetMaxSignalVar(fMaxSignalVar);
113// set the maximum allowed difference between maximum and minimal signal in CheckWindow
114//
115// Variables:
116// fgCheckWinFirst; First FADC slice to check for signal (currently set to: 0)
117// fgCheckWinLast: Last FADC slice to check for signal (currently set to: 29)
118// fgExtractWinFirst: First FADC slice to be used for pedestal extraction (currently set to: 15)
119// fgExtractWinSize: Window size in slices used for pedestal extraction (currently set to: 6)
120// fgMaxSignalVar: The maximum difference between the highest and lowest slice
121// in the check window allowed in order to use event
122//
123// Input Containers:
124// MRawEvtData
125// MRawRunHeader
126// MGeomCam
127//
128// Output Containers:
129// MPedestalCam
130//
131// See also: MPedestalCam, MPedestalPix, MHPedestalCam, MExtractor
132//
133/////////////////////////////////////////////////////////////////////////////
134#include "MPedCalcFromLoGain.h"
135#include "MExtractor.h"
136
137#include "MParList.h"
138
139#include "MLog.h"
140#include "MLogManip.h"
141
142#include "MRawRunHeader.h"
143#include "MRawEvtPixelIter.h"
144#include "MRawEvtData.h"
145
146#include "MPedestalPix.h"
147#include "MPedestalCam.h"
148
149#include "MGeomPix.h"
150#include "MGeomCam.h"
151
152ClassImp(MPedCalcFromLoGain);
153
154using namespace std;
155
156const UShort_t MPedCalcFromLoGain::fgCheckWinFirst = 0;
157const UShort_t MPedCalcFromLoGain::fgCheckWinLast = 29;
158const UShort_t MPedCalcFromLoGain::fgExtractWinFirst = 15;
159const UShort_t MPedCalcFromLoGain::fgExtractWinSize = 6;
160const UShort_t MPedCalcFromLoGain::fgMaxSignalVar = 40;
161
162// --------------------------------------------------------------------------
163//
164// Default constructor:
165//
166// Sets:
167// - all pointers to NULL
168//
169// Calls:
170// - AddToBranchList("fHiGainPixId");
171// - AddToBranchList("fHiGainFadcSamples");
172// - SetCheckRange(fgCheckWinFirst, fgCheckWinLast, fgExtractWinFirst, fgExtractWinSize)
173// - Clear()
174//
175MPedCalcFromLoGain::MPedCalcFromLoGain(const char *name, const char *title)
176 : fGeom(NULL), fNamePedestalCam("MPedestalCam")
177{
178 fName = name ? name : "MPedCalcFromLoGain";
179 fTitle = title ? title : "Task to calculate pedestals from lo-gains";
180
181 AddToBranchList("fHiGainPixId");
182 AddToBranchList("fLoGainPixId");
183 AddToBranchList("fHiGainFadcSamples");
184 AddToBranchList("fLoGainFadcSamples");
185
186 SetCheckRange(fgCheckWinFirst, fgCheckWinLast);
187 SetExtractWindow(fgExtractWinFirst, fgExtractWinSize);
188
189 SetMaxSignalVar(fgMaxSignalVar);
190 SetPedestalUpdate(kTRUE);
191
192 Clear();
193}
194
195void MPedCalcFromLoGain::ResetArrays()
196{
197 // Reset contents of arrays.
198 fSumx.Reset();
199 fSumx2.Reset();
200 fSumAB0.Reset();
201 fSumAB1.Reset();
202 fNumEventsUsed.Reset();
203 fTotalCounter.Reset();
204}
205
206// --------------------------------------------------------------------------
207//
208// Sets:
209// - fRawEvt to NULL
210// - fRunHeader to NULL
211// - fPedestals to NULL
212//
213// Resets:
214// - fSumx
215// - fSumx2
216// - fSumAB0
217// - fSumAB1
218// - fNumEventsUsed
219// - fTotalCounter
220//
221void MPedCalcFromLoGain::Clear(const Option_t *o)
222{
223
224 fRawEvt = NULL;
225 fRunHeader = NULL;
226 fPedestals = NULL;
227
228 // If the size is yet set, set the size
229 if (fSumx.GetSize()>0)
230 ResetArrays();
231}
232
233// --------------------------------------------------------------------------
234//
235// SetCheckRange:
236//
237// Exits, if the first argument is smaller than 0
238// Exits, if the the last argument is smaller than the first
239//
240Bool_t MPedCalcFromLoGain::SetCheckRange(UShort_t chfirst, UShort_t chlast)
241{
242 Bool_t rc = kTRUE;
243
244 if (chlast<=chfirst)
245 {
246 *fLog << warn << GetDescriptor();
247 *fLog << " - WARNING: Last slice in SetCheckRange smaller than first slice... set to first+2" << endl;
248 chlast = chfirst+1;
249 rc = kFALSE;
250 }
251
252 fCheckWinFirst = chfirst;
253 fCheckWinLast = chlast;
254
255 return rc;
256}
257
258// --------------------------------------------------------------------------
259//
260// Checks:
261// - if a window is odd
262//
263Bool_t MPedCalcFromLoGain::SetExtractWindow(UShort_t windowf, UShort_t windows)
264{
265 Bool_t rc = kTRUE;
266
267 const Int_t odd = windows & 0x1;
268
269 if (odd || windows==0)
270 {
271 *fLog << warn << GetDescriptor();
272 *fLog << " - WARNING: Window size in SetExtraxtWindow has to be even and > 0... adjusting!" << endl;
273 windows += 1;
274 rc = kFALSE;
275 }
276
277 fExtractWinSize = windows;
278 fExtractWinFirst = windowf;
279 fExtractWinLast = fExtractWinFirst+fExtractWinSize-1;
280
281//
282// NO RANGE CHECK IMPLEMENTED, YET
283//
284/*
285 const Byte_t availhirange = (fHiGainLast-fHiGainFirst+1) & ~1;
286 const Byte_t availlorange = (fLoGainLast-fLoGainFirst+1) & ~1;
287
288 if (fWindowSizeHiGain > availhirange)
289 {
290 *fLog << warn;
291 *fLog << GetDescriptor() << ": HiGain window " << (int)fWindowSizeHiGain;
292 *fLog << " out of range [" << (int)fHiGainFirst;
293 *fLog << "," << (int)fHiGainLast << "]" << endl;
294 *fLog << "Will set window size to " << (int)availhirange << endl;
295 fWindowSizeHiGain = availhirange;
296 }
297
298 if (fWindowSizeLoGain > availlorange)
299 {
300 *fLog << warn;
301 *fLog << GetDescriptor() << ": LoGain window " << (int)fWindowSizeLoGain;
302 *fLog << " out of range [" << (int)fLoGainFirst;
303 *fLog << "," << (int)fLoGainLast << "]" << endl;
304 *fLog << "Will set window size to " << (int)availlorange << endl;
305 fWindowSizeLoGain = availlorange;
306 }
307 */
308
309 return rc;
310}
311
312// --------------------------------------------------------------------------
313//
314// Look for the following input containers:
315//
316// - MRawEvtData
317// - MRawRunHeader
318// - MGeomCam
319//
320// The following output containers are also searched and created if
321// they were not found:
322//
323// - MPedestalCam with the name fPedContainerName
324//
325Int_t MPedCalcFromLoGain::PreProcess(MParList *pList)
326{
327
328 Clear();
329
330 fRawEvt = (MRawEvtData*)pList->FindObject(AddSerialNumber("MRawEvtData"));
331 if (!fRawEvt)
332 {
333 *fLog << err << AddSerialNumber("MRawEvtData") << " not found... aborting." << endl;
334 return kFALSE;
335 }
336
337 fRunHeader = (MRawRunHeader*)pList->FindObject(AddSerialNumber("MRawRunHeader"));
338 if (!fRunHeader)
339 {
340 *fLog << err << AddSerialNumber("MRawRunHeader") << " not found... aborting." << endl;
341 return kFALSE;
342 }
343
344 fGeom = (MGeomCam*)pList->FindObject(AddSerialNumber("MGeomCam"));
345 if (!fGeom)
346 {
347 *fLog << err << AddSerialNumber("MGeomCam") << " not found... aborting." << endl;
348 return kFALSE;
349 }
350
351 fPedestals = (MPedestalCam*)pList->FindCreateObj("MPedestalCam", AddSerialNumber(fNamePedestalCam));
352 if (!fPedestals)
353 return kFALSE;
354
355 *fLog << inf;
356 Print();
357
358 return kTRUE;
359}
360
361// ---------------------------------------------------------------------------------
362//
363// Checks:
364// - if the number of available slices
365// (fRunHeader->GetNumSamplesHiGain()+(Int_t)fRunHeader->GetNumSamplesLoGain()-1)
366// is smaller than the number of used slices
367// (fExtractWinSize+ fExtractWinFirst-1) or
368// fCheckWinLast
369//
370// Sets the size (from MPedestalCam::GetSize() ) and resets the following arrays:
371// - fSumx
372// - fSumx2
373// - fSumAB0
374// - fSumAB1
375// - fNumEventsUsed
376// - fTotalCounter
377//
378Bool_t MPedCalcFromLoGain::ReInit(MParList *pList)
379{
380 Int_t lastavailableslice = fRunHeader->GetNumSamplesHiGain()+fRunHeader->GetNumSamplesLoGain()-1;
381 Int_t lastextractslice = fExtractWinSize + fExtractWinFirst - 1;
382
383 if (lastextractslice>lastavailableslice)//changed to override check
384 {
385 *fLog << warn << GetDescriptor();
386 *fLog << " - WARNING: Selected Extract Window ranges out of range...adjusting to last available slice ";
387 *fLog << lastavailableslice << endl;
388
389 lastextractslice=lastavailableslice;
390 }
391
392 if (fCheckWinLast>lastavailableslice)//changed to override check
393 {
394 *fLog << warn << GetDescriptor();
395 *fLog << " - WARNING: Last Check Window slice out of range...adjusting to last available slice ";
396 *fLog << lastavailableslice << endl;
397
398 fCheckWinLast=lastavailableslice;
399 }
400
401
402 // If the size is not yet set, set the size
403 if (fSumx.GetSize()==0)
404 {
405 const Int_t npixels = fPedestals->GetSize();
406
407 fSumx. Set(npixels);
408 fSumx2.Set(npixels);
409 fSumAB0.Set(npixels);
410 fSumAB1.Set(npixels);
411 fNumEventsUsed.Set(npixels);
412 fTotalCounter.Set(npixels);
413
414 ResetArrays();
415 }
416
417 return kTRUE;
418}
419
420// ---------------------------------------------------------------------------------
421//
422// Calculates for pixel "idx":
423//
424// Ped per slice = sum / n / fExtractWinSize;
425// RMS per slice = sqrt { (sum2 - sum*sum/n) / (n-1) / fExtractWinSize }
426// ABOffset per slice = (fSumAB0[idx] - fSumAB1[idx]) / n / fExtractWinSize;
427//
428// Sets fTotalCounter up by 1.
429//
430void MPedCalcFromLoGain::Calc(ULong_t n, UInt_t idx)
431{
432
433 const ULong_t nsamplestot = n*fExtractWinSize;
434
435 const Float_t sum = fSumx.At(idx);
436 const Float_t sum2 = fSumx2.At(idx);
437 const Float_t ped = sum/nsamplestot;
438
439 // 1. Calculate the Variance of the sums:
440 Float_t var = (sum2-sum*sum/n)/(n-1.);
441
442 // 2. Scale the variance to the number of slices:
443 var /= (Float_t)(fExtractWinSize);
444
445 // 3. Calculate the RMS from the Variance:
446 const Float_t rms = var<0 ? 0 : TMath::Sqrt(var);
447
448 // 4. Calculate the amplitude of the 150MHz "AB" noise
449 const Float_t abOffs = (fSumAB0[idx] - fSumAB1[idx]) / nsamplestot;
450
451 (*fPedestals)[idx].Set(ped, rms, abOffs, n);
452
453 fTotalCounter[idx]++;
454}
455
456// ---------------------------------------------------------------------------------
457//
458// Returns the pointer to slice "slice".
459//
460UShort_t MPedCalcFromLoGain::GetSlice(MRawEvtPixelIter *pixel, UInt_t slice)
461{
462 const UShort_t nh = (Int_t)fRunHeader->GetNumSamplesHiGain();
463
464 Byte_t *ptr;
465
466 if(slice<nh)
467 ptr = pixel->GetHiGainSamples() + slice;
468 else
469 ptr = pixel->GetLoGainSamples() + slice - nh;
470
471 return *ptr;
472}
473
474// --------------------------------------------------------------------------
475//
476// Fill the MPedestalCam container with the signal mean and rms for the event.
477// Store the measured signal in arrays fSumx and fSumx2 so that we can
478// calculate the overall mean and rms in the PostProcess()
479//
480Int_t MPedCalcFromLoGain::Process()
481{
482 MRawEvtPixelIter pixel(fRawEvt);
483
484 while (pixel.Next())
485 {
486 const UInt_t idx = pixel.GetPixelId();
487
488 UShort_t max = 0;
489 UShort_t min = (UShort_t)-1;
490
491 // Find the maximum and minimum signal per slice in the high gain window
492 for (Int_t slice=fCheckWinFirst; slice<=fCheckWinLast; slice++)
493 {
494 const UShort_t svalue = GetSlice(&pixel,slice);
495 if (svalue > max)
496 max = svalue;
497 if (svalue < min)
498 min = svalue;
499 }
500
501 // If the maximum in the high gain window is smaller than
502 if (max-min>=fMaxSignalVar || max>=250)
503 continue;
504
505 UInt_t sum = 0;
506 UInt_t sqr = 0;
507
508 //extract pedestal
509 for(Int_t slice=fExtractWinFirst; slice<=fExtractWinLast; slice++)
510 {
511 const UInt_t svalue = GetSlice(&pixel,slice);
512 sum += svalue;
513 sqr += svalue*svalue;
514 }
515
516 fSumx[idx] += sum;
517 fSumx2[idx] += sum*sum;
518
519 fNumEventsUsed[idx]++;
520
521 // Calculate the amplitude of the 150MHz "AB" noise
522 UShort_t abFlag = (fExtractWinFirst + pixel.HasABFlag()) & 0x1;
523
524 for (UShort_t islice=fExtractWinFirst; islice<=fExtractWinLast; islice+=2)
525 {
526 const UShort_t sliceAB0 = islice + abFlag;
527 const UShort_t sliceAB1 = islice - abFlag + 1;
528 fSumAB0[idx] += GetSlice(&pixel, sliceAB0);
529 fSumAB1[idx] += GetSlice(&pixel, sliceAB1);
530 }
531
532 if (!fPedestalUpdate || (UInt_t)fNumEventsUsed[idx]<fNumEventsDump)
533 continue;
534
535 Calc(fNumEventsDump, idx);
536
537 fNumEventsUsed[idx]=0;
538 fSumx[idx]=0;
539 fSumx2[idx]=0;
540 fSumAB0[idx]=0;
541 fSumAB1[idx]=0;
542 }
543
544 if (fPedestalUpdate)
545 fPedestals->SetReadyToSave();
546
547 return kTRUE;
548}
549
550// --------------------------------------------------------------------------
551//
552// Compute signal mean and rms in the whole run and store it in MPedestalCam
553//
554Int_t MPedCalcFromLoGain::PostProcess()
555{
556 // Compute pedestals and rms from the whole run
557 if (fPedestalUpdate)
558 return kTRUE;
559
560 *fLog << flush << inf << "Calculating Pedestals..." << flush;
561
562 const Int_t npix = fGeom->GetNumPixels();
563 for (Int_t idx=0; idx<npix; idx++)
564 {
565 const ULong_t n = fNumEventsUsed[idx];
566 if (n>1)
567 Calc(n, idx);
568 }
569
570 fPedestals->SetReadyToSave();
571 return kTRUE;
572}
573
574
575// --------------------------------------------------------------------------
576//
577// The following resources are available:
578// FirstCheckWindowSlice: 0
579// LastCheckWindowSlice: 29
580// ExtractWindowFirst: 15
581// ExtractWindowSize: 6
582// NumEventsDump: 500
583// MaxSignalVar: 40
584// PedestalUpdate: yes
585//
586Int_t MPedCalcFromLoGain::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
587{
588 Bool_t rc=kFALSE;
589
590 // Find resources for CheckWindow
591 Int_t fs = fCheckWinFirst;
592 Int_t ls = fCheckWinLast;
593 if (IsEnvDefined(env, prefix, "CheckWinFirst", print))
594 {
595 fs = GetEnvValue(env, prefix, "CheckWinFirst", fs);
596 rc = kTRUE;
597 }
598 if (IsEnvDefined(env, prefix, "CheckWinLast", print))
599 {
600 ls = GetEnvValue(env, prefix, "CheckWinLast", ls);
601 rc = kTRUE;
602 }
603
604 SetCheckRange(fs,ls);
605
606 // Find resources for ExtractWindow
607 Int_t lw = fExtractWinSize;
608 Int_t wf = fExtractWinFirst;
609 if (IsEnvDefined(env, prefix, "ExtractWinSize", print))
610 {
611 lw = GetEnvValue(env, prefix, "ExtractWinSize", lw);
612 rc = kTRUE;
613 }
614
615 if (IsEnvDefined(env, prefix, "ExtractWinFirst", print))
616 {
617 wf = GetEnvValue(env, prefix, "ExtractWinFirst", wf);
618 rc = kTRUE;
619 }
620 SetExtractWindow(wf,lw);
621
622 // find resource for numeventsdump
623 if (IsEnvDefined(env, prefix, "NumEventsDump", print))
624 {
625 SetNumEventsDump(GetEnvValue(env, prefix, "NumEventsDump", (Int_t)fNumEventsDump));
626 rc = kTRUE;
627 }
628
629 // find resource for maximum signal variation
630 if (IsEnvDefined(env, prefix, "MaxSignalVar", print))
631 {
632 SetMaxSignalVar(GetEnvValue(env, prefix, "MaxSignalVar", fMaxSignalVar));
633 rc = kTRUE;
634 }
635
636 // find resource for pedestal update
637 if (IsEnvDefined(env, prefix, "PedestalUpdate", print))
638 {
639 SetPedestalUpdate(GetEnvValue(env, prefix, "PedestalUpdate", fPedestalUpdate));
640 rc = kTRUE;
641 }
642
643 return rc;
644}
645
646void MPedCalcFromLoGain::Print(Option_t *o) const
647{
648 *fLog << GetDescriptor() << ":" << endl;
649 *fLog << "Parameters used for pedestal calculation from " << fNamePedestalCam << ":"<<endl;
650 *fLog << "CheckWindow from slice " << fCheckWinFirst << " to " << fCheckWinLast << endl;
651 *fLog << "ExtractWindow from slice " << fExtractWinFirst << " to " << fExtractWinLast << endl;
652 *fLog << "Max allowed signal variation: " << fMaxSignalVar << endl;
653 *fLog << "Pedestal Update is " << (fPedestalUpdate?"on":"off") << endl;
654}
Note: See TracBrowser for help on using the repository browser.