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

Last change on this file since 4305 was 4252, checked in by moralejo, 21 years ago
*** empty log message ***
File size: 14.2 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, Int_t &sum, Byte_t &sat) const
302{
303
304 Byte_t *end = ptr + fWindowSizeHiGain-fHiLoLast;
305
306 //
307 // Calculate the sum of the "window" slices starting in ptr
308 //
309 while (ptr<end)
310 {
311 sum += *ptr;
312 if (*ptr++ >= fSaturationLimit)
313 sat++;
314 }
315
316 //
317 // If part of the "low-Gain" slices are used,
318 // repeat steps one and two for the logain part until fHiLoLast
319 //
320 Byte_t *p = logain;
321 end = logain + fHiLoLast;
322 while (p<end)
323 {
324
325 sum += *p;
326 if (*p++ >= fSaturationLimit)
327 sat++;
328 }
329
330 return;
331}
332
333// --------------------------------------------------------------------------
334//
335// FindSignalLoGain:
336//
337// - Loop from ptr to (ptr+fWindowSizeLoGain)
338// - Sum up contents of *ptr
339// - If *ptr is greater than fSaturationLimit, raise sat by 1
340//
341void MExtractFixedWindowPeakSearch::FindSignalLoGain(Byte_t *ptr, Int_t &sum, Byte_t &sat) const
342{
343 //
344 // Calculate the sum of the "window" slices starting in ptr
345 //
346 Byte_t *p = ptr;
347
348 while (p<ptr+fWindowSizeLoGain)
349 {
350 sum += *p;
351 if (*p++ >= fSaturationLimit)
352 sat++;
353 }
354
355 return;
356}
357
358// --------------------------------------------------------------------------
359//
360// Process
361// First find the pixel with highest sum of fPeakSearchWindowSize slices (default:4)
362// "startslice" will mark the slice at which the highest sum begins for that pixel.
363// Then define the beginning of the integration window for ALL pixels as the slice
364// before that: startslice-fOffsetFromWindow, unless of course startslice-fOffsetFromWindow<=0,
365// in which case we start at 0. We will also check that the integration window does not
366// go beyond the FADC limits.
367//
368Int_t MExtractFixedWindowPeakSearch::Process()
369{
370
371 MRawEvtPixelIter pixel(fRawEvt);
372
373 Int_t sat;
374 Int_t maxsumhi = -1000000;
375 Byte_t startslice;
376 Byte_t hiGainFirst = 0;
377 Byte_t loGainFirst = 0;
378 Byte_t hilolastsave = fHiLoLast;
379
380 while (pixel.Next())
381 {
382 Int_t sumhi;
383 sat = 0;
384
385 FindPeak(pixel.GetHiGainSamples()+fHiGainFirst, fPeakSearchWindowSize, startslice, sumhi, sat);
386
387 if (sumhi > maxsumhi && sat == 0 )
388 {
389 maxsumhi = sumhi;
390 if ((startslice-fOffsetFromWindow) > 0)
391 hiGainFirst = fHiGainFirst + startslice - fOffsetFromWindow;
392 else
393 hiGainFirst = fHiGainFirst;
394 }
395 }
396
397
398 loGainFirst = ( hiGainFirst+fLowGainPeakShift > fLoGainFirst ) ?
399 hiGainFirst+fLowGainPeakShift : fLoGainFirst;
400
401 // Make sure we will not integrate beyond the hi gain limit:
402 if (hiGainFirst+fWindowSizeHiGain > pixel.GetNumHiGainSamples())
403 fHiLoLast = hiGainFirst+fWindowSizeHiGain - pixel.GetNumHiGainSamples();
404 // hiGainFirst = pixel.GetNumHiGainSamples()-fWindowSizeHiGain;
405
406 // Make sure we will not integrate beyond the lo gain limit:
407 if (loGainFirst+fWindowSizeLoGain > pixel.GetNumLoGainSamples())
408 loGainFirst = pixel.GetNumLoGainSamples()-fWindowSizeLoGain;
409
410 pixel.Reset();
411 fSignals->Clear();
412
413 sat = 0;
414 while (pixel.Next())
415 {
416 //
417 // Find signal in hi- and lo-gain
418 //
419 Int_t sumhi=0;
420 Byte_t sathi=0;
421
422 FindSignalHiGain(pixel.GetHiGainSamples()+hiGainFirst, pixel.GetLoGainSamples(), sumhi, sathi);
423
424 Int_t sumlo=0;
425 Byte_t satlo=0;
426 if (pixel.HasLoGain())
427 {
428 FindSignalLoGain(pixel.GetLoGainSamples()+loGainFirst, sumlo, satlo);
429 if (satlo)
430 sat++;
431 }
432
433 //
434 // Take corresponding pedestal
435 //
436 const Int_t pixid = pixel.GetPixelId();
437
438 const MPedestalPix &ped = (*fPedestals)[pixid];
439 MExtractedSignalPix &pix = (*fSignals)[pixid];
440
441 const Float_t pedes = ped.GetPedestal();
442 const Float_t pedrms = ped.GetPedestalRms();
443
444 //
445 // Set extracted signal with pedestal substracted
446 //
447 pix.SetExtractedSignal(sumhi - pedes*fNumHiGainSamples, pedrms*fSqrtHiGainSamples,
448 sumlo - pedes*fNumLoGainSamples, pedrms*fSqrtLoGainSamples);
449
450 pix.SetGainSaturation(sathi, sathi, satlo);
451 } /* while (pixel.Next()) */
452
453
454 fHiLoLast = hilolastsave;
455 fSignals->SetReadyToSave();
456
457 return kTRUE;
458}
Note: See TracBrowser for help on using the repository browser.