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

Last change on this file since 4119 was 4119, checked in by moralejo, 20 years ago
*** empty log message ***
File size: 14.0 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::fgOffsetFromWindow = 1;
85// --------------------------------------------------------------------------
86//
87// Default constructor.
88//
89// Sets:
90// - fWindowSizeHiGain to fgWindowSizeHiGain
91// - fWindowSizeLoGain to fgWindowSizeLoGain
92// - fPeakSearchWindowSize to fgPeakSearchWindowSize
93//
94// Calls:
95// - SetOffsetFromWindow()
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 SetOffsetFromWindow();
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, Int_t &max, Int_t &sat) const
242{
243
244 const Byte_t *end = ptr + fHiGainLast - fHiGainFirst + 1;
245
246 sat = 0;
247 startslice = 0;
248 Int_t sum=0;
249
250 //
251 // Calculate the sum of the first "window" slices
252 //
253 sat = 0;
254 Byte_t *p = ptr;
255
256 while (p<ptr+window)
257 {
258 sum += *p;
259 if (*p++ >= fSaturationLimit)
260 sat++;
261 }
262
263 //
264 // Check for saturation in all other slices
265 //
266 while (p<end)
267 if (*p++ >= fSaturationLimit)
268 sat++;
269
270 //
271 // Calculate the i-th sum of n as
272 // sum_i+1 = sum_i + slice[i+window] - slice[i]
273 // This is fast and accurate (because we are using int's)
274 //
275 max=sum;
276 for (p=ptr; p+window<end; p++)
277 {
278 sum += *(p+window) - *p;
279 if (sum > max)
280 {
281 max = sum;
282 startslice = p-ptr;
283 }
284 }
285
286 return;
287}
288
289// --------------------------------------------------------------------------
290//
291// FindSignalHiGain:
292//
293// - Loop from ptr to (ptr+fWindowSizeHiGain)
294// - Sum up contents of *ptr
295// - If *ptr is greater than fSaturationLimit, raise sat by 1
296//
297void MExtractFixedWindowPeakSearch::FindSignalHiGain(Byte_t *ptr, Byte_t *logain, Int_t &sum, Byte_t &sat) const
298{
299
300 Byte_t *end = ptr + fWindowSizeHiGain-fHiLoLast;
301
302 //
303 // Calculate the sum of the "window" slices starting in ptr
304 //
305 while (ptr<end)
306 {
307 sum += *ptr;
308 if (*ptr++ >= fSaturationLimit)
309 sat++;
310 }
311
312 //
313 // If part of the "low-Gain" slices are used,
314 // repeat steps one and two for the logain part until fHiLoLast
315 //
316 Byte_t *p = logain;
317 end = logain + fHiLoLast;
318 while (p<end)
319 {
320
321 sum += *p;
322 if (*p++ >= fSaturationLimit)
323 sat++;
324 }
325
326 return;
327}
328
329// --------------------------------------------------------------------------
330//
331// FindSignalLoGain:
332//
333// - Loop from ptr to (ptr+fWindowSizeLoGain)
334// - Sum up contents of *ptr
335// - If *ptr is greater than fSaturationLimit, raise sat by 1
336//
337void MExtractFixedWindowPeakSearch::FindSignalLoGain(Byte_t *ptr, Int_t &sum, Byte_t &sat) const
338{
339 //
340 // Calculate the sum of the "window" slices starting in ptr
341 //
342 Byte_t *p = ptr;
343
344 while (p<ptr+fWindowSizeLoGain)
345 {
346 sum += *p;
347 if (*p++ >= fSaturationLimit)
348 sat++;
349 }
350
351 return;
352}
353
354// --------------------------------------------------------------------------
355//
356// Process
357// First find the pixel with highest sum of fPeakSearchWindowSize slices (default:4)
358// "startslice" will mark the slice at which the highest sum begins for that pixel.
359// Then define the beginning of the integration window for ALL pixels as the slice
360// before that: startslice-fOffsetFromWindow, unless of course startslice-fOffsetFromWindow<=0,
361// in which case we start at 0. We will also check that the integration window does not
362// go beyond the FADC limits.
363//
364Int_t MExtractFixedWindowPeakSearch::Process()
365{
366
367 MRawEvtPixelIter pixel(fRawEvt);
368
369 Int_t sat;
370 Int_t maxsumhi = -1000000;
371 Byte_t startslice;
372 Byte_t hiGainFirst = 0;
373 Byte_t loGainFirst = 0;
374 Byte_t hilolastsave = fHiLoLast;
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-fOffsetFromWindow) > 0)
387 hiGainFirst = fHiGainFirst + startslice - fOffsetFromWindow;
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 fHiLoLast = hilolastsave;
450 fSignals->SetReadyToSave();
451
452 return kTRUE;
453}
Note: See TracBrowser for help on using the repository browser.