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

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