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