source: trunk/MagicSoft/Mars/msignal/MExtractTimeAndChargeSlidingWindow.cc@ 5832

Last change on this file since 5832 was 5747, checked in by gaug, 20 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 analyzing 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! Author(s): Thomas Bretz, 02/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
18! Author(s): Hendrik Bartko, 01/2004 <mailto:hbartko@mppmu.mpg.de>
19! Author(s): Markus Gaug, 09/2004 <mailto:markus@ifae.es>
20!
21! Copyright: MAGIC Software Development, 2002-2004
22!
23!
24\* ======================================================================== */
25//////////////////////////////////////////////////////////////////////////////
26//
27// MExtractSlidingWindow
28//
29// Extracts the signal from a sliding window of size fHiGainWindowSize and
30// fLoGainWindowSize, respectively. The signal is the one which maximizes
31// the clock-noise and pedestal-corrected integral contents.
32//
33// The amplitude-weighted arrival time is calculated from the window with
34// the highest integral using the following formula:
35//
36// t = sum(s(i) * i) / sum(i)
37//
38// where i denotes the FADC slice index and s(i) the clock-noise and
39/// pedestal-corrected FADC value at slice index i. The sum runs over the
40// extraction window size.
41//
42// Call: SetRange(higainfirst, higainlast, logainfirst, logainlast)
43// to modify the ranges in which the window is allowed to move.
44//
45// Defaults are:
46//
47// fHiGainFirst = fgHiGainFirst = 0
48// fHiGainLast = fgHiGainLast = 14
49// fLoGainFirst = fgLoGainFirst = 2
50// fLoGainLast = fgLoGainLast = 14
51//
52// Call: SetWindowSize(windowhigain, windowlogain)
53// to modify the sliding window widths. Windows have to be an even number.
54// Odd numbers are possible since the clock-noise is corrected for.
55//
56// Defaults are:
57//
58// fHiGainWindowSize = 6
59// fLoGainWindowSize = 6
60//
61//////////////////////////////////////////////////////////////////////////////
62#include "MExtractTimeAndChargeSlidingWindow.h"
63
64#include "MPedestalPix.h"
65
66#include "MLog.h"
67#include "MLogManip.h"
68
69ClassImp(MExtractTimeAndChargeSlidingWindow);
70
71using namespace std;
72
73const Byte_t MExtractTimeAndChargeSlidingWindow::fgHiGainFirst = 2;
74const Byte_t MExtractTimeAndChargeSlidingWindow::fgHiGainLast = 16;
75const Byte_t MExtractTimeAndChargeSlidingWindow::fgLoGainFirst = 2;
76const Byte_t MExtractTimeAndChargeSlidingWindow::fgLoGainLast = 14;
77const Byte_t MExtractTimeAndChargeSlidingWindow::fgHiGainWindowSize = 6;
78const Byte_t MExtractTimeAndChargeSlidingWindow::fgLoGainWindowSize = 6;
79// --------------------------------------------------------------------------
80//
81// Default constructor.
82//
83// Calls:
84// - SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast)
85//
86// Initializes:
87// - fWindowSizeHiGain to fgHiGainWindowSize
88// - fWindowSizeLoGain to fgLoGainWindowSize
89//
90MExtractTimeAndChargeSlidingWindow::MExtractTimeAndChargeSlidingWindow(const char *name, const char *title)
91{
92
93 fName = name ? name : "MExtractTimeAndChargeSlidingWindow";
94 fTitle = title ? title : "Calculate arrival times and charges using a sliding window";
95
96 fWindowSizeHiGain = fgHiGainWindowSize;
97 fWindowSizeLoGain = fgLoGainWindowSize;
98
99 SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast);
100}
101
102//-------------------------------------------------------------------
103//
104// Set the ranges
105//
106// Calls:
107// - MExtractor::SetRange(hifirst,hilast,lofirst,lolast);
108// - SetWindowSize(fWindowSizeHiGain,fWindowSizeLoGain);
109//
110void MExtractTimeAndChargeSlidingWindow::SetRange(Byte_t hifirst, Byte_t hilast, Byte_t lofirst, Byte_t lolast)
111{
112
113 MExtractor::SetRange(hifirst, hilast, lofirst, lolast);
114
115 //
116 // Redo the checks if the window is still inside the ranges
117 //
118 SetWindowSize(fWindowSizeHiGain,fWindowSizeLoGain);
119
120}
121
122// -----------------------------------------------------------------------------------------
123//
124// Checks:
125// - if a window is bigger than the one defined by the ranges, set it to the available range
126// - if a window is smaller than 2, set it to 2
127//
128// Sets:
129// - fNumHiGainSamples to: (Float_t)fWindowSizeHiGain
130// - fNumLoGainSamples to: (Float_t)fWindowSizeLoGain
131// - fSqrtHiGainSamples to: TMath::Sqrt(fNumHiGainSamples)
132// - fSqrtLoGainSamples to: TMath::Sqrt(fNumLoGainSamples)
133//
134void MExtractTimeAndChargeSlidingWindow::SetWindowSize(Int_t windowh, Int_t windowl)
135{
136
137 fWindowSizeHiGain = windowh;
138 fWindowSizeLoGain = windowl;
139
140 const Int_t availhirange = (Int_t)(fHiGainLast-fHiGainFirst+1);
141
142 if (fWindowSizeHiGain > availhirange)
143 {
144 *fLog << warn << GetDescriptor()
145 << Form("%s%2i%s%2i%s%2i%s",": Hi Gain window size: ",(int)fWindowSizeHiGain,
146 " is bigger than available range: [",(int)fHiGainFirst,",",(int)fHiGainLast,"]") << endl;
147 *fLog << warn << GetDescriptor()
148 << ": Will set window size to: " << (int)availhirange << endl;
149 fWindowSizeHiGain = availhirange;
150 }
151
152 if (fWindowSizeHiGain<1)
153 {
154 fWindowSizeHiGain = 1;
155 *fLog << warn << GetDescriptor() << ": High Gain window size too small, set to one sample" << endl;
156 }
157
158 if (fLoGainLast != 0 && fWindowSizeLoGain != 0)
159 {
160 const Int_t availlorange = (Int_t)(fLoGainLast-fLoGainFirst+1);
161
162 if (fWindowSizeLoGain > availlorange)
163 {
164 *fLog << warn << GetDescriptor()
165 << Form("%s%2i%s%2i%s%2i%s",": Lo Gain window size: ",(int)fWindowSizeLoGain,
166 " is bigger than available range: [",(int)fLoGainFirst,",",(int)fLoGainLast,"]") << endl;
167 *fLog << warn << GetDescriptor()
168 << ": Will set window size to: " << (int)availlorange << endl;
169 fWindowSizeLoGain = availlorange;
170 }
171 }
172
173 fNumHiGainSamples = (Float_t)fWindowSizeHiGain;
174 fNumLoGainSamples = fLoGainLast ? (Float_t)fWindowSizeLoGain : 0.;
175
176 fSqrtHiGainSamples = TMath::Sqrt(fNumHiGainSamples);
177 fSqrtLoGainSamples = TMath::Sqrt(fNumLoGainSamples);
178
179}
180
181// --------------------------------------------------------------------------
182//
183// InitArrays
184//
185// Gets called in the ReInit() and initialized the arrays
186//
187Bool_t MExtractTimeAndChargeSlidingWindow::InitArrays()
188{
189 Int_t range = (Int_t)(fHiGainLast - fHiGainFirst + 1 + fHiLoLast);
190 fHiGainSignal.Set(range);
191 range = (Int_t)(fLoGainLast - fLoGainFirst + 1);
192 fLoGainSignal.Set(range);
193
194 return kTRUE;
195
196}
197
198// --------------------------------------------------------------------------
199//
200// Calculates the arrival time for each pixel
201//
202void MExtractTimeAndChargeSlidingWindow::FindTimeAndChargeHiGain(Byte_t *first, Byte_t *logain, Float_t &sum, Float_t &dsum,
203 Float_t &time, Float_t &dtime,
204 Byte_t &sat, const MPedestalPix &ped, const Bool_t abflag)
205{
206
207 Int_t range = fHiGainLast - fHiGainFirst + 1;
208 const Byte_t *end = first + range;
209 Byte_t *p = first;
210
211 Float_t max = 0; // highest integral content of all windows
212 Int_t count = 0;
213 sat = 0;
214
215 const Float_t pedes = ped.GetPedestal();
216 const Float_t ABoffs = ped.GetPedestalABoffset();
217
218 Float_t PedMean[2] = { pedes + ABoffs, pedes - ABoffs };
219 //
220 // Check for saturation in all other slices
221 //
222 Int_t ids = fHiGainFirst;
223
224 while (p<first+fWindowSizeHiGain)
225 {
226
227 const Float_t signal = (Float_t)*p - PedMean[(ids++ + abflag) & 0x1];
228 sum += signal;
229 fHiGainSignal[count] = signal;
230
231 if (*p++ >= fSaturationLimit)
232 if (!sat)
233 sat = ids-2;
234
235 count++;
236 }
237
238 //
239 // Check for saturation in all other slices
240 //
241 while (p<end)
242 if (*p++ >= fSaturationLimit)
243 if (!sat)
244 sat = ids-2;
245
246 if (IsNoiseCalculation())
247 return;
248
249 //
250 // Calculate the i-th sum as
251 // sum_i+1 = sum_i + slice[i+8] - slice[i]
252 // This is fast and accurate (because we are using int's)
253 //
254 count = 0;
255 max = sum;
256 Int_t idx = 0; // idx of the first slice of the maximum window
257
258 for (p=first; p+fWindowSizeHiGain<end; p++)
259 {
260
261 const Float_t signal = (Float_t)*(p+fWindowSizeHiGain) - PedMean[(ids++ + abflag) & 0x1];
262 sum += signal - fHiGainSignal[count];
263 fHiGainSignal[count + fWindowSizeHiGain] = signal;
264
265 if (sum>max)
266 {
267 max = sum;
268 idx = count+1;
269 }
270 count++;
271 }
272
273 if (fHiLoLast != 0)
274 {
275
276 //
277 // overlap bins
278 //
279 Byte_t *l = logain;
280
281 while (p < end && l < logain+fHiLoLast)
282 {
283
284 const Float_t signal = (Float_t)*l - PedMean[(ids++ + abflag) & 0x1];
285 sum += signal - fHiGainSignal[count];
286 fHiGainSignal[count + fWindowSizeHiGain] = signal;
287 if (*l++ >= fSaturationLimit)
288 if (!sat)
289 sat = ids-2;
290
291 if (sum>max)
292 {
293 max = sum;
294 idx = count+1;
295 }
296 count++;
297 p++;
298 }
299
300 if (fHiLoLast > (Byte_t)fWindowSizeHiGain)
301 {
302 while (l < logain + fHiLoLast)
303 {
304 const Float_t signal = (Float_t)*l - PedMean[(ids++ + abflag) & 0x1];
305 sum += signal - fHiGainSignal[count];
306 fHiGainSignal[count+fWindowSizeHiGain] = signal;
307
308 if (*l++ >= fSaturationLimit)
309 if (!sat)
310 sat = ids-2;
311
312 if (sum>max)
313 {
314 max = sum;
315 idx = count+1;
316 }
317 count++;
318 } /* while (l < logain + fHiLoLast) */
319 } /* if (fHiLoLast > fWindowSizeHiGain) */
320 } /* if (fHiLoLast != 0) */
321
322 //
323 // now calculate the time for the maximum window
324 //
325 Float_t timesignalsum = 0.;
326 Int_t timesquaredsum = 0;
327
328 for (Int_t i=idx; i<idx+fWindowSizeHiGain; i++)
329 {
330 timesignalsum += fHiGainSignal[i]*i;
331 timesquaredsum += i*i;
332 }
333
334 sum = max;
335
336 time = max > 0.1 ? timesignalsum / max + Float_t(fHiGainFirst) : 1.;
337 dtime = max > 0.1 ? ped.GetPedestalRms() / max * sqrt(timesquaredsum - fWindowSizeHiGain*time) : 1.;
338
339}
340
341
342// --------------------------------------------------------------------------
343//
344// Calculates the arrival time for each pixel
345//
346void MExtractTimeAndChargeSlidingWindow::FindTimeAndChargeLoGain(Byte_t *first, Float_t &sum, Float_t &dsum,
347 Float_t &time, Float_t &dtime,
348 Byte_t &sat, const MPedestalPix &ped, const Bool_t abflag)
349{
350
351 Int_t range = fLoGainLast - fLoGainFirst + 1;
352 const Byte_t *end = first + range;
353 Byte_t *p = first;
354
355 Float_t max = 0; // highest integral content of all windows
356 Int_t count = 0; // counter to recognize the AB-flag
357
358 Float_t pedes = ped.GetPedestal();
359 const Float_t ABoffs = ped.GetPedestalABoffset();
360
361 Float_t PedMean[2] = { pedes + ABoffs, pedes - ABoffs };
362 //
363 // Check for saturation in all other slices
364 //
365 Int_t ids = fLoGainFirst;
366
367 while (p<first+fWindowSizeLoGain)
368 {
369 const Float_t signal = (Float_t)*p - PedMean[(ids++ + abflag) & 0x1];
370 sum += signal;
371 fLoGainSignal[count] = signal;
372
373 if (*p++ >= fSaturationLimit)
374 sat++;
375
376 count++;
377 }
378
379 //
380 // Check for saturation in all other slices
381 //
382 while (p<end)
383 if (*p++ >= fSaturationLimit)
384 sat++;
385
386 if (IsNoiseCalculation())
387 return;
388
389 //
390 // Calculate the i-th sum as
391 // sum_i+1 = sum_i + slice[i+8] - slice[i]
392 // This is fast and accurate (because we are using int's)
393 //
394 count = 0;
395 max = sum;
396 Int_t idx = 0; // idx of the first slice of the maximum window
397
398 for (p=first; p+fWindowSizeLoGain<end; p++)
399 {
400
401 const Float_t signal = (Float_t)*(p+fWindowSizeLoGain) - PedMean[(ids++ + abflag) & 0x1];
402 sum += signal - fLoGainSignal[count];
403 fLoGainSignal[count + fWindowSizeLoGain] = signal;
404
405 if (sum>max)
406 {
407 max = sum;
408 idx = count+1;
409 }
410 count++;
411 }
412
413 //
414 // now calculate the time for the maximum window
415 //
416 Float_t timesignalsum = 0;
417 Int_t timesquaredsum = 0;
418
419 for (Int_t i=idx; i<idx+fWindowSizeLoGain; i++)
420 {
421 timesignalsum += fLoGainSignal[i]*i;
422 timesquaredsum += i*i;
423 }
424
425 sum = max;
426
427 time = max > 0.1 ? timesignalsum / max + Float_t(fLoGainFirst) : 1.;
428 dtime = max > 0.1 ? ped.GetPedestalRms() / max * sqrt(timesquaredsum - fWindowSizeLoGain*time) : 1.;
429}
430
431// --------------------------------------------------------------------------
432//
433// In addition to the resources of the base-class MExtractor:
434// MJPedestal.MExtractor.WindowSizeHiGain: 6
435// MJPedestal.MExtractor.WindowSizeLoGain: 6
436//
437Int_t MExtractTimeAndChargeSlidingWindow::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
438{
439
440 Byte_t hw = fWindowSizeHiGain;
441 Byte_t lw = fWindowSizeLoGain;
442
443 Bool_t rc = kFALSE;
444
445 if (IsEnvDefined(env, prefix, "HiGainWindowSize", print))
446 {
447 hw = GetEnvValue(env, prefix, "HiGainWindowSize", hw);
448 rc = kTRUE;
449 }
450 if (IsEnvDefined(env, prefix, "LoGainWindowSize", print))
451 {
452 lw = GetEnvValue(env, prefix, "LoGainWindowSize", lw);
453 rc = kTRUE;
454 }
455
456 if (rc)
457 SetWindowSize(hw, lw);
458
459 return MExtractTime::ReadEnv(env, prefix, print) ? kTRUE : rc;
460
461}
462
Note: See TracBrowser for help on using the repository browser.