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

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