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

Last change on this file since 3928 was 3925, checked in by gaug, 21 years ago
*** empty log message ***
File size: 13.3 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// FindPeak
216// Finds highest sum of "window" consecutive FADC slices in a pixel, and store
217// in "startslice" the first slice of the group which yields the maximum sum.
218// In "max" the value of the maximum sum is stored, in "sat" the number of
219// saturated slices.
220//
221void MExtractFixedWindowPeakSearch::FindPeak(Byte_t *ptr, Byte_t window, Byte_t &startslice,
222 Int_t &max, Int_t &sat) const
223{
224
225 const Byte_t *end = ptr + fHiGainLast - fHiGainFirst + 1;
226
227 Int_t sum=0;
228
229 //
230 // Calculate the sum of the first "window" slices
231 //
232 sat = 0;
233 Byte_t *p = ptr;
234
235 while (p<ptr+window)
236 {
237 sum += *p;
238 if (*p++ >= fSaturationLimit)
239 sat++;
240 }
241
242 //
243 // Check for saturation in all other slices
244 //
245 while (p<end)
246 if (*p++ >= fSaturationLimit)
247 sat++;
248
249 //
250 // Calculate the i-th sum of n as
251 // sum_i+1 = sum_i + slice[i+window] - slice[i]
252 // This is fast and accurate (because we are using int's)
253 //
254 max=sum;
255 for (p=ptr; p+window<end; p++)
256 {
257 sum += *(p+window) - *p;
258 if (sum > max)
259 {
260 max = sum;
261 startslice = p-ptr;
262 }
263 }
264
265 return;
266}
267
268// --------------------------------------------------------------------------
269//
270// FindSignalHiGain:
271//
272// - Loop from ptr to (ptr+fWindowSizeHiGain)
273// - Sum up contents of *ptr
274// - If *ptr is greater than fSaturationLimit, raise sat by 1
275//
276void MExtractFixedWindowPeakSearch::FindSignalHiGain(Byte_t *ptr, Byte_t *logain, Int_t &sum, Byte_t &sat) const
277{
278
279 Byte_t *end = ptr + fWindowSizeHiGain-fHiLoLast;
280
281 //
282 // Calculate the sum of the "window" slices starting in ptr
283 //
284 while (ptr<end)
285 {
286 sum += *ptr;
287 if (*ptr++ >= fSaturationLimit)
288 sat++;
289 }
290
291 //
292 // If part of the "low-Gain" slices are used,
293 // repeat steps one and two for the logain part until fHiLoLast
294 //
295 Byte_t *p = logain;
296 end = logain + fHiLoLast;
297 while (p<end)
298 {
299
300 sum += *p;
301 if (*p++ >= fSaturationLimit)
302 sat++;
303 }
304
305 return;
306}
307
308// --------------------------------------------------------------------------
309//
310// FindSignalLoGain:
311//
312// - Loop from ptr to (ptr+fWindowSizeLoGain)
313// - Sum up contents of *ptr
314// - If *ptr is greater than fSaturationLimit, raise sat by 1
315//
316void MExtractFixedWindowPeakSearch::FindSignalLoGain(Byte_t *ptr, Int_t &sum, Byte_t &sat) const
317{
318 //
319 // Calculate the sum of the "window" slices starting in ptr
320 //
321 sum = 0;
322 sat = 0;
323 Byte_t *p = ptr;
324
325 while (p<ptr+fWindowSizeLoGain)
326 {
327 sum += *p;
328 if (*p++ >= fSaturationLimit)
329 sat++;
330 }
331
332 return;
333}
334
335// --------------------------------------------------------------------------
336//
337// Process
338// First find the pixel with highest sum of fPeakSearchWindowSize slices (default:4)
339// "startslice" will mark the slice at which the highest sum begins for that pixel.
340// Then define the beginning of the integration window for ALL pixels as the slice
341// before that: startslice-fOffsetFraomPeak, unless of course startslice-fOffsetFromPeak<=0,
342// in which case we start at 0. We will also check that the integration window does not
343// go beyond the FADC limits.
344//
345Int_t MExtractFixedWindowPeakSearch::Process()
346{
347
348 MRawEvtPixelIter pixel(fRawEvt);
349
350 Int_t sat;
351 Int_t maxsumhi = 0;
352 Byte_t startslice;
353 Byte_t hiGainFirst = 0;
354 Byte_t loGainFirst = 0;
355
356 while (pixel.Next())
357 {
358 Int_t sumhi;
359 sat = 0;
360
361 FindPeak(pixel.GetHiGainSamples()+fHiGainFirst, fPeakSearchWindowSize, startslice, sumhi, sat);
362
363 if (sumhi > maxsumhi && sat == 0 )
364 {
365 maxsumhi = sumhi;
366 if ((startslice-fOffsetFromPeak) > 0)
367 hiGainFirst = fHiGainFirst + startslice - fOffsetFromPeak;
368 else
369 hiGainFirst = fHiGainFirst;
370 }
371 }
372
373
374 loGainFirst = ( hiGainFirst > fLoGainFirst ) ? hiGainFirst : fLoGainFirst;
375
376 // Make sure we will not integrate beyond the hi gain limit:
377 if (hiGainFirst+fWindowSizeHiGain > pixel.GetNumHiGainSamples())
378 fHiLoLast = hiGainFirst+fWindowSizeHiGain - pixel.GetNumHiGainSamples();
379 // hiGainFirst = pixel.GetNumHiGainSamples()-fWindowSizeHiGain;
380
381 // Make sure we will not integrate beyond the lo gain limit:
382 if (loGainFirst+fWindowSizeLoGain > pixel.GetNumLoGainSamples())
383 loGainFirst = pixel.GetNumLoGainSamples()-fWindowSizeLoGain;
384
385 pixel.Reset();
386 fSignals->Clear();
387
388 sat = 0;
389 while (pixel.Next())
390 {
391 //
392 // Find signal in hi- and lo-gain
393 //
394 Int_t sumhi=0;
395 Byte_t sathi=0;
396
397 FindSignalHiGain(pixel.GetHiGainSamples()+hiGainFirst, pixel.GetLoGainSamples(), sumhi, sathi);
398
399 Int_t sumlo=0;
400 Byte_t satlo=0;
401 if (pixel.HasLoGain())
402 {
403 FindSignalLoGain(pixel.GetLoGainSamples()+loGainFirst, sumlo, satlo);
404 if (satlo)
405 sat++;
406 }
407
408 //
409 // Take corresponding pedestal
410 //
411 const Int_t pixid = pixel.GetPixelId();
412
413 const MPedestalPix &ped = (*fPedestals)[pixid];
414 MExtractedSignalPix &pix = (*fSignals)[pixid];
415
416 const Float_t pedes = ped.GetPedestal();
417 const Float_t pedrms = ped.GetPedestalRms();
418
419 //
420 // Set extracted signal with pedestal substracted
421 //
422 pix.SetExtractedSignal(sumhi - pedes*fNumHiGainSamples, pedrms*fSqrtHiGainSamples,
423 sumlo - pedes*fNumLoGainSamples, pedrms*fSqrtLoGainSamples);
424
425 pix.SetGainSaturation(sathi, sathi, satlo);
426 } /* while (pixel.Next()) */
427
428
429 fSignals->SetReadyToSave();
430
431 return kTRUE;
432}
Note: See TracBrowser for help on using the repository browser.