source: trunk/MagicSoft/Mars/msignal/MExtractFixedWindowPeakSearch.cc@ 5400

Last change on this file since 5400 was 5250, checked in by gaug, 20 years ago
*** empty log message ***
File size: 16.4 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): Abelardo Moralejo, 04/2004 <mailto:moralejo@pd.infn.it>
19! Author(s): Markus Gaug, 04/2004 <mailto:markus@ifae.es>
20!
21! Copyright: MAGIC Software Development, 2000-2004
22!
23!
24\* ======================================================================== */
25
26//////////////////////////////////////////////////////////////////////////////
27//
28// MExtractFixedWindowPeakSearch
29//
30// Calculate the signal integrating fWindowSize time slices, the same for
31// all pixels. The integrated slices change from event to event, since the
32// pulse positions in the FADC jump between events, but apparently in a
33// "coherent" fashion. We first loop over all pixels and find the one
34// which has the highest sum of fPeakSearchWindowSize (default: 4) consecutive
35// non-saturated high gain slices. The position of the peak in this pixel
36// determines the integration window position for all pixels of this event.
37// For the moment we neglect time delays between pixels (which are in most
38// cases small). The class is based on MExtractSlidingWindow.
39//
40// Call: SetRange(higainfirst, higainlast, logainfirst, logainlast)
41// to modify the ranges in which the window is allowed to move.
42// Defaults are:
43//
44// fHiGainFirst = fgHiGainFirst = 0
45// fHiGainLast = fgHiGainLast = 14
46// fLoGainFirst = fgLoGainFirst = 3
47// fLoGainLast = fgLoGainLast = 14
48//
49// Call: SetWindows(windowhigain, windowlogain,peaksearchwindow)
50// to modify the sliding window widths. Windows have to be an even number.
51// In case of odd numbers, the window will be modified.
52//
53// Defaults are:
54//
55// fHiGainWindowSize = fgHiGainWindowSize = 6
56// fLoGainWindowSize = fgLoGainWindowSize = 6
57// fPeakSearchWindowSize = fgPeakSearchWindowSize = 4
58// fLoGainPeakShift = fgLoGainPeakShift = 0
59//
60//////////////////////////////////////////////////////////////////////////////
61#include "MExtractFixedWindowPeakSearch.h"
62#include "MExtractor.h"
63
64#include "MLog.h"
65#include "MLogManip.h"
66
67#include "MRawEvtPixelIter.h"
68
69#include "MExtractedSignalCam.h"
70#include "MExtractedSignalPix.h"
71
72#include "MPedestalCam.h"
73#include "MPedestalPix.h"
74
75ClassImp(MExtractFixedWindowPeakSearch);
76
77using namespace std;
78
79const Byte_t MExtractFixedWindowPeakSearch::fgHiGainFirst = 0;
80const Byte_t MExtractFixedWindowPeakSearch::fgHiGainLast = 14;
81const Byte_t MExtractFixedWindowPeakSearch::fgLoGainFirst = 3;
82const Byte_t MExtractFixedWindowPeakSearch::fgLoGainLast = 14;
83const Byte_t MExtractFixedWindowPeakSearch::fgHiGainWindowSize = 6;
84const Byte_t MExtractFixedWindowPeakSearch::fgLoGainWindowSize = 6;
85const Byte_t MExtractFixedWindowPeakSearch::fgPeakSearchWindowSize = 4;
86const Byte_t MExtractFixedWindowPeakSearch::fgOffsetFromWindow = 1;
87const Byte_t MExtractFixedWindowPeakSearch::fgLoGainPeakShift = 1;
88// --------------------------------------------------------------------------
89//
90// Default constructor.
91//
92// Sets:
93// - fHiGainWindowSize to fgHiGainWindowSize
94// - fLoGainWindowSize to fgLoGainWindowSize
95// - fPeakSearchWindowSize to fgPeakSearchWindowSize
96// - fLoGainPeakShift to fgLoGainPeakShift
97//
98// Calls:
99// - SetOffsetFromWindow()
100// - SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast)
101//
102MExtractFixedWindowPeakSearch::MExtractFixedWindowPeakSearch(const char *name, const char *title)
103 : fHiGainWindowSize(fgHiGainWindowSize),
104 fLoGainWindowSize(fgLoGainWindowSize),
105 fPeakSearchWindowSize(fgPeakSearchWindowSize),
106 fLoGainPeakShift(fgLoGainPeakShift)
107{
108
109 fName = name ? name : "MExtractFixedWindowPeakSearch";
110 fTitle = title ? title : "Task to extract the signal from the FADC slices";
111
112 SetOffsetFromWindow();
113 SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast);
114}
115
116// --------------------------------------------------------------------------
117//
118// SetRange:
119//
120// Calls:
121// - MExtractor::SetRange(hifirst,hilast,lofirst,lolast);
122// - SetWindows(fHiGainWindowSize,fLoGainWindowSize,fPeakSearchWindowSize);
123//
124void MExtractFixedWindowPeakSearch::SetRange(Byte_t hifirst, Byte_t hilast, Byte_t lofirst, Byte_t lolast)
125{
126
127 MExtractor::SetRange(hifirst,hilast,lofirst,lolast);
128
129 //
130 // Redo the checks if the window is still inside the ranges
131 //
132 SetWindows(fHiGainWindowSize,fLoGainWindowSize,fPeakSearchWindowSize);
133
134}
135
136// --------------------------------------------------------------------------
137//
138// Checks:
139// - if a window is odd, subtract one
140// - if a window is bigger than the one defined by the ranges, set it to the available range
141// - if a window is smaller than 2, set it to 2
142//
143// Sets:
144// - fNumHiGainSamples to: (Float_t)fHiGainWindowSize
145// - fNumLoGainSamples to: (Float_t)fLoGainWindowSize
146// - fSqrtHiGainSamples to: TMath::Sqrt(fNumHiGainSamples)
147// - fSqrtLoGainSamples to: TMath::Sqrt(fNumLoGainSamples)
148//
149void MExtractFixedWindowPeakSearch::SetWindows(Byte_t windowh, Byte_t windowl, Byte_t peaksearchwindow)
150{
151
152 fHiGainWindowSize = windowh & ~1;
153 fLoGainWindowSize = windowl & ~1;
154 fPeakSearchWindowSize = peaksearchwindow & ~1;
155
156 if (fHiGainWindowSize != windowh)
157 *fLog << warn << GetDescriptor() << ": Hi Gain window size has to be even, set to: "
158 << int(fHiGainWindowSize) << " samples " << endl;
159
160 if (fLoGainWindowSize != windowl)
161 *fLog << warn << GetDescriptor() << ": Lo Gain window size has to be even, set to: "
162 << int(fLoGainWindowSize) << " samples " << endl;
163
164 if (fPeakSearchWindowSize != peaksearchwindow)
165 *fLog << warn << GetDescriptor() << ": Peak Search window size has to be even, set to: "
166 << int(fPeakSearchWindowSize) << " samples " << endl;
167
168 const Byte_t availhirange = (fHiGainLast-fHiGainFirst+1) & ~1;
169 const Byte_t availlorange = (fLoGainLast-fLoGainFirst+1) & ~1;
170
171 if (fHiGainWindowSize > availhirange)
172 {
173 *fLog << warn << GetDescriptor()
174 << Form("%s%2i%s%2i%s%2i%s",": Hi Gain window size: ",(int)fHiGainWindowSize,
175 " is bigger than available range: [",(int)fHiGainFirst,",",(int)fHiGainLast,"]") << endl;
176 *fLog << warn << GetDescriptor()
177 << ": Will set window size to: " << (int)availhirange << endl;
178 fHiGainWindowSize = availhirange;
179 }
180
181
182 if (fLoGainWindowSize > availlorange)
183 {
184 *fLog << warn << GetDescriptor()
185 << Form("%s%2i%s%2i%s%2i%s",": Lo Gain window size: ",(int)fLoGainWindowSize,
186 " is bigger than available range: [",(int)fLoGainFirst,",",(int)fLoGainLast,"]") << endl;
187 *fLog << warn << GetDescriptor()
188 << ": Will set window size to: " << (int)availlorange << endl;
189 fLoGainWindowSize = availlorange;
190 }
191
192 if (fHiGainWindowSize<2)
193 {
194 fHiGainWindowSize = 2;
195 *fLog << warn << GetDescriptor() << ": Hi Gain window size set to two samples" << endl;
196 }
197
198 if (fLoGainWindowSize<2)
199 {
200 fLoGainWindowSize = 2;
201 *fLog << warn << GetDescriptor() << ": Lo Gain window size set to two samples" << endl;
202 }
203
204 if (fPeakSearchWindowSize<2)
205 {
206 fPeakSearchWindowSize = 2;
207 *fLog << warn << GetDescriptor()
208 << ": Peak Search window size set to two samples" << endl;
209 }
210
211 fNumHiGainSamples = (Float_t)fHiGainWindowSize;
212 fNumLoGainSamples = (Float_t)fLoGainWindowSize;
213
214 fSqrtHiGainSamples = TMath::Sqrt(fNumHiGainSamples);
215 fSqrtLoGainSamples = TMath::Sqrt(fNumLoGainSamples);
216}
217
218
219// --------------------------------------------------------------------------
220//
221// The ReInit calls:
222// - MExtractor::ReInit()
223// - fSignals->SetUsedFADCSlices(fHiGainFirst, fHiGainLast+fHiLoLast, fNumHiGainSamples,
224// fLoGainFirst, fLoGainLast, fNumLoGainSamples);
225//
226Bool_t MExtractFixedWindowPeakSearch::ReInit(MParList *pList)
227{
228
229 MExtractor::ReInit(pList);
230
231 fSignals->SetUsedFADCSlices(fHiGainFirst, fHiGainLast+fHiLoLast, fNumHiGainSamples,
232 fLoGainFirst, fLoGainLast, fNumLoGainSamples);
233
234 return kTRUE;
235
236}
237
238// --------------------------------------------------------------------------
239//
240// FindPeak
241// Finds highest sum of "window" consecutive FADC slices in a pixel, and store
242// in "startslice" the first slice of the group which yields the maximum sum.
243// In "max" the value of the maximum sum is stored, in "sat" the number of
244// saturated slices.
245//
246void MExtractFixedWindowPeakSearch::FindPeak(Byte_t *ptr, Byte_t window, Byte_t &startslice, Int_t &max, Int_t &sat) const
247{
248
249 const Byte_t *end = ptr + fHiGainLast - fHiGainFirst + 1;
250
251 sat = 0;
252 startslice = 0;
253 Int_t sum=0;
254
255 //
256 // Calculate the sum of the first "window" slices
257 //
258 sat = 0;
259 Byte_t *p = ptr;
260
261 while (p<ptr+window)
262 {
263 sum += *p;
264 if (*p++ >= fSaturationLimit)
265 sat++;
266 }
267
268 //
269 // Check for saturation in all other slices
270 //
271 while (p<end)
272 if (*p++ >= fSaturationLimit)
273 sat++;
274
275 //
276 // Calculate the i-th sum of n as
277 // sum_i+1 = sum_i + slice[i+window] - slice[i]
278 // This is fast and accurate (because we are using int's)
279 //
280 max=sum;
281 for (p=ptr; p+window<end; p++)
282 {
283 sum += *(p+window) - *p;
284 if (sum > max)
285 {
286 max = sum;
287 startslice = p-ptr;
288 }
289 }
290
291 return;
292}
293
294// --------------------------------------------------------------------------
295//
296// FindSignalHiGain:
297//
298// - Loop from ptr to (ptr+fHiGainWindowSize)
299// - Sum up contents of *ptr
300// - If *ptr is greater than fSaturationLimit, raise sat by 1
301//
302void MExtractFixedWindowPeakSearch::FindSignalHiGain(Byte_t *ptr, Byte_t *logain, Float_t &sum, Byte_t &sat) const
303{
304
305 Byte_t *end = ptr + fHiGainWindowSize-fHiLoLast;
306
307 Int_t summ = 0;
308 //
309 // Calculate the sum of the "window" slices starting in ptr
310 //
311 while (ptr<end)
312 {
313 summ += *ptr;
314 if (*ptr++ >= fSaturationLimit)
315 sat++;
316 }
317
318 //
319 // If part of the "lo-Gain" slices are used,
320 // repeat steps one and two for the logain part until fHiLoLast
321 //
322 Byte_t *p = logain;
323 end = logain + fHiLoLast;
324 while (p<end)
325 {
326
327 summ += *p;
328 if (*p++ >= fSaturationLimit)
329 sat++;
330 }
331
332 sum = (Float_t)summ;
333 return;
334}
335
336// --------------------------------------------------------------------------
337//
338// FindSignalLoGain:
339//
340// - Loop from ptr to (ptr+fLoGainWindowSize)
341// - Sum up contents of *ptr
342// - If *ptr is greater than fSaturationLimit, raise sat by 1
343//
344void MExtractFixedWindowPeakSearch::FindSignalLoGain(Byte_t *ptr, Float_t &sum, Byte_t &sat) const
345{
346 //
347 // Calculate the sum of the "window" slices starting in ptr
348 //
349 Byte_t *p = ptr;
350 Int_t summ = 0;
351
352 while (p<ptr+fLoGainWindowSize)
353 {
354 summ += *p;
355 if (*p++ >= fSaturationLimit)
356 sat++;
357 }
358
359 sum = (Float_t)summ;
360 return;
361}
362
363// --------------------------------------------------------------------------
364//
365// Process
366// First find the pixel with highest sum of fPeakSearchWindowSize slices (default:4)
367// "startslice" will mark the slice at which the highest sum begins for that pixel.
368// Then define the beginning of the integration window for ALL pixels as the slice
369// before that: startslice-fOffsetFromWindow, unless of course startslice-fOffsetFromWindow<=0,
370// in which case we start at 0. We will also check that the integration window does not
371// go beyond the FADC limits.
372//
373Int_t MExtractFixedWindowPeakSearch::Process()
374{
375
376 MRawEvtPixelIter pixel(fRawEvt);
377
378 Int_t sat;
379 Int_t maxsumhi = -1000000;
380 Byte_t startslice;
381 Byte_t hiGainFirst = 0;
382 Byte_t loGainFirst = 0;
383 Byte_t hilolastsave = fHiLoLast;
384
385 while (pixel.Next())
386 {
387 Int_t sumhi;
388 sat = 0;
389
390 FindPeak(pixel.GetHiGainSamples()+fHiGainFirst, fPeakSearchWindowSize, startslice, sumhi, sat);
391
392 if (sumhi > maxsumhi && sat == 0 )
393 {
394 maxsumhi = sumhi;
395 if ((startslice-fOffsetFromWindow) > 0)
396 hiGainFirst = fHiGainFirst + startslice - fOffsetFromWindow;
397 else
398 hiGainFirst = fHiGainFirst;
399 }
400 }
401
402
403 loGainFirst = ( hiGainFirst+fLoGainPeakShift > fLoGainFirst ) ?
404 hiGainFirst+fLoGainPeakShift : fLoGainFirst;
405
406 // Make sure we will not integrate beyond the hi gain limit:
407 if (hiGainFirst+fHiGainWindowSize > pixel.GetNumHiGainSamples())
408 fHiLoLast = hiGainFirst+fHiGainWindowSize - pixel.GetNumHiGainSamples();
409 // hiGainFirst = pixel.GetNumHiGainSamples()-fHiGainWindowSize;
410
411 // Make sure we will not integrate beyond the lo gain limit:
412 if (loGainFirst+fLoGainWindowSize > pixel.GetNumLoGainSamples())
413 loGainFirst = pixel.GetNumLoGainSamples()-fLoGainWindowSize;
414
415 pixel.Reset();
416 fSignals->Clear();
417
418 sat = 0;
419 while (pixel.Next())
420 {
421 //
422 // Find signal in hi- and lo-gain
423 //
424 Float_t sumhi=0.;
425 Byte_t sathi=0;
426
427 FindSignalHiGain(pixel.GetHiGainSamples()+hiGainFirst, pixel.GetLoGainSamples(), sumhi, sathi);
428
429 Float_t sumlo=0.;
430 Byte_t satlo=0;
431 if (pixel.HasLoGain())
432 {
433 FindSignalLoGain(pixel.GetLoGainSamples()+loGainFirst, sumlo, satlo);
434 if (satlo)
435 sat++;
436 }
437
438 //
439 // Take corresponding pedestal
440 //
441 const Int_t pixid = pixel.GetPixelId();
442
443 const MPedestalPix &ped = (*fPedestals)[pixid];
444 MExtractedSignalPix &pix = (*fSignals)[pixid];
445
446 const Float_t pedes = ped.GetPedestal();
447 const Float_t pedrms = ped.GetPedestalRms();
448
449 //
450 // Set extracted signal with pedestal substracted
451 //
452 pix.SetExtractedSignal(sumhi - pedes*fNumHiGainSamples, pedrms*fSqrtHiGainSamples,
453 sumlo - pedes*fNumLoGainSamples, pedrms*fSqrtLoGainSamples);
454
455 pix.SetGainSaturation(sathi, sathi, satlo);
456
457 // pix.SetNumHiGainSlices(fNumHiGainSamples);
458 // pix.SetNumLoGainSlices(fNumLoGainSamples);
459
460 } /* while (pixel.Next()) */
461
462
463 fHiLoLast = hilolastsave;
464 fSignals->SetReadyToSave();
465
466 return kTRUE;
467}
468
469// --------------------------------------------------------------------------
470//
471// In addition to the resources of the base-class MExtractor:
472// MJPedestal.MExtractor.WindowSizeHiGain: 6
473// MJPedestal.MExtractor.WindowSizeLoGain: 6
474// MJPedestal.MExtractor.PeakSearchWindow: 4
475// MJPedestal.MExtractor.OffsetFromWindow: 1
476// MJPedestal.MExtractor.LoGainPeakShift: 1
477//
478Int_t MExtractFixedWindowPeakSearch::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
479{
480 Byte_t hw = fHiGainWindowSize;
481 Byte_t lw = fLoGainWindowSize;
482 Byte_t pw = fPeakSearchWindowSize;
483
484 Bool_t rc = kFALSE;
485
486 if (IsEnvDefined(env, prefix, "PeakSearchWindow", print))
487 {
488 pw = GetEnvValue(env, prefix, "PeakSearchWindow", pw);
489 rc = kTRUE;
490 }
491 if (IsEnvDefined(env, prefix, "HiGainWindowSize", print))
492 {
493 hw = GetEnvValue(env, prefix, "HiGainWindowSize", hw);
494 rc = kTRUE;
495 }
496 if (IsEnvDefined(env, prefix, "LoGainWindowSize", print))
497 {
498 lw = GetEnvValue(env, prefix, "LoGainWindowSize", lw);
499 rc = kTRUE;
500 }
501
502 if (rc)
503 SetWindows(hw, lw, pw);
504
505
506 if (IsEnvDefined(env, prefix, "OffsetFromWindow", print))
507 {
508 SetOffsetFromWindow(GetEnvValue(env, prefix, "OffsetFromWindow", fOffsetFromWindow));
509 rc = kTRUE;
510 }
511
512 if (IsEnvDefined(env, prefix, "LoGainPeakShift", print))
513 {
514 SetLoGainPeakShift(GetEnvValue(env, prefix, "LoGainPeakShift", fLoGainPeakShift));
515 rc = kTRUE;
516 }
517
518 rc = MExtractor::ReadEnv(env, prefix, print) ? kTRUE : rc;
519
520 return rc;
521}
522
523void MExtractFixedWindowPeakSearch::Print(Option_t *o) const
524{
525 *fLog << all;
526 *fLog << GetDescriptor() << ":" << endl;
527 *fLog << " Windows: Hi-Gain=" << (int)fHiGainWindowSize << " Lo-Gain=" << (int)fLoGainWindowSize;
528 *fLog << " Peak-Search=" << (int)fPeakSearchWindowSize << endl;
529 *fLog << " Offset From Window: " << (int)fOffsetFromWindow << endl;
530 *fLog << " Lo-Gain peak Shift: " << (int)fLoGainPeakShift << endl;
531 MExtractor::Print(o);
532}
Note: See TracBrowser for help on using the repository browser.