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

Last change on this file since 5474 was 5416, checked in by gaug, 20 years ago
*** empty log message ***
File size: 14.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 : fWindowSizeHiGain(fgHiGainWindowSize),
92 fWindowSizeLoGain(fgLoGainWindowSize),
93 fHiGainSignal(NULL), fLoGainSignal(NULL)
94{
95
96 fName = name ? name : "MExtractTimeAndChargeSlidingWindow";
97 fTitle = title ? title : "Calculate arrival times and charges using a sliding window";
98
99 SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast);
100}
101
102
103// --------------------------------------------------------------------------
104//
105// Destructor: Deletes the arrays
106//
107MExtractTimeAndChargeSlidingWindow::~MExtractTimeAndChargeSlidingWindow()
108{
109
110 if (fHiGainSignal)
111 delete [] fHiGainSignal;
112 if (fLoGainSignal)
113 delete [] fLoGainSignal;
114
115}
116
117//-------------------------------------------------------------------
118//
119// Set the ranges
120//
121// Calls:
122// - MExtractor::SetRange(hifirst,hilast,lofirst,lolast);
123// - SetWindowSize(fWindowSizeHiGain,fWindowSizeLoGain);
124//
125void MExtractTimeAndChargeSlidingWindow::SetRange(Byte_t hifirst, Byte_t hilast, Byte_t lofirst, Byte_t lolast)
126{
127
128 MExtractor::SetRange(hifirst, hilast, lofirst, lolast);
129
130 //
131 // Redo the checks if the window is still inside the ranges
132 //
133 SetWindowSize(fWindowSizeHiGain,fWindowSizeLoGain);
134
135}
136
137// -----------------------------------------------------------------------------------------
138//
139// Checks:
140// - if a window is bigger than the one defined by the ranges, set it to the available range
141// - if a window is smaller than 2, set it to 2
142//
143// Sets:
144// - fNumHiGainSamples to: (Float_t)fWindowSizeHiGain
145// - fNumLoGainSamples to: (Float_t)fWindowSizeLoGain
146// - fSqrtHiGainSamples to: TMath::Sqrt(fNumHiGainSamples)
147// - fSqrtLoGainSamples to: TMath::Sqrt(fNumLoGainSamples)
148//
149void MExtractTimeAndChargeSlidingWindow::SetWindowSize(Byte_t windowh, Byte_t windowl)
150{
151
152 fWindowSizeHiGain = windowh;
153 fWindowSizeLoGain = windowl;
154
155 const Byte_t availhirange = fHiGainLast-fHiGainFirst+1;
156
157 if (fWindowSizeHiGain > availhirange)
158 {
159 *fLog << warn << GetDescriptor()
160 << Form("%s%2i%s%2i%s%2i%s",": Hi Gain window size: ",(int)fWindowSizeHiGain,
161 " is bigger than available range: [",(int)fHiGainFirst,",",(int)fHiGainLast,"]") << endl;
162 *fLog << warn << GetDescriptor()
163 << ": Will set window size to: " << (int)availhirange << endl;
164 fWindowSizeHiGain = availhirange;
165 }
166
167 if (fWindowSizeHiGain<1)
168 {
169 fWindowSizeHiGain = 1;
170 *fLog << warn << GetDescriptor() << ": High Gain window size too small, set to one sample" << endl;
171 }
172
173 if (fLoGainLast != 0 && fWindowSizeLoGain != 0)
174 {
175 const Byte_t availlorange = fLoGainLast-fLoGainFirst+1;
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
188 fNumHiGainSamples = (Float_t)fWindowSizeHiGain;
189 fNumLoGainSamples = fLoGainLast ? (Float_t)fWindowSizeLoGain : 0.;
190
191 fSqrtHiGainSamples = TMath::Sqrt(fNumHiGainSamples);
192 fSqrtLoGainSamples = TMath::Sqrt(fNumLoGainSamples);
193
194}
195
196// --------------------------------------------------------------------------
197//
198// ReInit
199//
200// Calls:
201// - MExtractTimeAndCharge::ReInit(pList);
202// - Deletes all arrays, if not NULL
203// - Creates new arrays according to the extraction range
204//
205Bool_t MExtractTimeAndChargeSlidingWindow::ReInit(MParList *pList)
206{
207
208 if (!MExtractTimeAndCharge::ReInit(pList))
209 return kFALSE;
210
211 if (fHiGainSignal)
212 delete [] fHiGainSignal;
213 if (fLoGainSignal)
214 delete [] fLoGainSignal;
215
216 Int_t range = fHiGainLast - fHiGainFirst + 1 + fHiLoLast;
217
218 fHiGainSignal = new Float_t[range];
219 memset(fHiGainSignal,0,range*sizeof(Float_t));
220
221 range = fLoGainLast - fLoGainFirst + 1;
222
223 fLoGainSignal = new Float_t[range];
224 memset(fLoGainSignal,0,range*sizeof(Float_t));
225
226 return kTRUE;
227}
228
229// --------------------------------------------------------------------------
230//
231// Calculates the arrival time for each pixel
232//
233void MExtractTimeAndChargeSlidingWindow::FindTimeAndChargeHiGain(Byte_t *first, Byte_t *logain, Float_t &sum, Float_t &dsum,
234 Float_t &time, Float_t &dtime,
235 Byte_t &sat, const MPedestalPix &ped, const Bool_t abflag)
236{
237
238 Int_t range = fHiGainLast - fHiGainFirst + 1;
239 const Byte_t *end = first + range;
240 Byte_t *p = first;
241
242 Float_t max = 0; // highest integral content of all windows
243 Int_t count = 0; // counter to recognize the AB-flag
244
245 const Float_t pedes = ped.GetPedestal();
246 const Float_t ABoffs = ped.GetPedestalABoffset();
247
248 Float_t PedMean[2];
249 PedMean[0] = pedes + ABoffs;
250 PedMean[1] = pedes - ABoffs;
251
252 //
253 // Check for saturation in all other slices
254 //
255 while (p<first+fWindowSizeHiGain)
256 {
257 const Int_t ids = fHiGainFirst + count;
258 const Float_t signal = (Float_t)*p - PedMean[(ids+abflag) & 0x1];
259 sum += signal;
260 fHiGainSignal[count] = signal;
261
262 if (*p++ >= fSaturationLimit)
263 sat++;
264
265 count++;
266 }
267
268 //
269 // Check for saturation in all other slices
270 //
271 while (p<end)
272 if (*p++ >= fSaturationLimit)
273 sat++;
274
275 if (IsNoiseCalculation())
276 return;
277
278 //
279 // Calculate the i-th sum as
280 // sum_i+1 = sum_i + slice[i+8] - slice[i]
281 // This is fast and accurate (because we are using int's)
282 //
283 count = 0;
284 max = sum;
285 Int_t idx = 0; // idx of the first slice of the maximum window
286
287 for (p=first; p+fWindowSizeHiGain<end; p++)
288 {
289
290 const Int_t ids = fHiGainFirst + count + fWindowSizeHiGain;
291 const Float_t signal = (Float_t)*(p+fWindowSizeHiGain) - PedMean[(ids+abflag) & 0x1];
292 sum += signal - fHiGainSignal[count];
293 fHiGainSignal[count + fWindowSizeHiGain] = signal;
294
295 if (sum>max)
296 {
297 max = sum;
298 idx = count+1;
299 }
300 count++;
301 }
302
303 if (fHiLoLast != 0)
304 {
305
306 //
307 // overlap bins
308 //
309 Byte_t *l = logain;
310
311 while (p < end && l < logain+fHiLoLast)
312 {
313
314 const Int_t ids = fHiGainFirst + count + fWindowSizeHiGain;
315 const Float_t signal = (Float_t)*l - PedMean[(ids+abflag) & 0x1];
316 sum += signal - fHiGainSignal[count];
317 fHiGainSignal[count + fWindowSizeHiGain] = signal;
318 count++;
319 if (*l++ >= fSaturationLimit)
320 sat++;
321
322 if (sum>max)
323 {
324 max = sum;
325 idx = count+1;
326 }
327 p++;
328 }
329
330 if (fHiLoLast > fWindowSizeHiGain)
331 {
332 while (l < logain + fHiLoLast)
333 {
334 const Int_t ids = fHiGainFirst + count + fWindowSizeHiGain;
335 const Float_t signal = (Float_t)*l - PedMean[(ids+abflag) & 0x1];
336 sum += signal - fHiGainSignal[count];
337 fHiGainSignal[count+fWindowSizeHiGain] = signal;
338 count++;
339 if (*(l+fWindowSizeHiGain) >= fSaturationLimit)
340 sat++;
341
342 if (sum>max)
343 {
344 max = sum;
345 idx = count+1;
346 }
347 l++;
348 } /* while (l < logain + fHiLoLast) */
349 } /* if (fHiLoLast > fWindowSizeHiGain) */
350 } /* if (fHiLoLast != 0) */
351
352 //
353 // now calculate the time for the maximum window
354 //
355 Float_t timesignalsum = 0.;
356 Int_t timesquaredsum = 0;
357
358 for (Int_t i=idx; i<idx+fWindowSizeHiGain; i++)
359 {
360 timesignalsum += fHiGainSignal[i]*i;
361 timesquaredsum += i*i;
362 }
363
364 sum = max;
365
366 time = sum != 0 ? timesignalsum / max + Float_t(fHiGainFirst) : 1.;
367 dtime = sum != 0 ? ped.GetPedestalRms() / max * sqrt(timesquaredsum - fWindowSizeHiGain*time) : 1.;
368
369}
370
371
372// --------------------------------------------------------------------------
373//
374// Calculates the arrival time for each pixel
375//
376void MExtractTimeAndChargeSlidingWindow::FindTimeAndChargeLoGain(Byte_t *first, Float_t &sum, Float_t &dsum,
377 Float_t &time, Float_t &dtime,
378 Byte_t &sat, const MPedestalPix &ped, const Bool_t abflag)
379{
380
381 Int_t range = fLoGainLast - fLoGainFirst + 1;
382 const Byte_t *end = first + range;
383 Byte_t *p = first;
384
385 Float_t max = 0; // highest integral content of all windows
386 Int_t count = 0; // counter to recognize the AB-flag
387
388 Float_t pedes = ped.GetPedestal();
389 const Float_t ABoffs = ped.GetPedestalABoffset();
390
391 Float_t PedMean[2];
392 PedMean[0] = pedes + ABoffs;
393 PedMean[1] = pedes - ABoffs;
394
395 //
396 // Check for saturation in all other slices
397 //
398 while (p<first+fWindowSizeLoGain)
399 {
400 const Int_t ids = fLoGainFirst + count;
401 const Float_t signal = (Float_t)*p - PedMean[(ids+abflag) & 0x1];
402 sum += signal;
403 fLoGainSignal[count] = signal;
404
405 if (*p++ >= fSaturationLimit)
406 sat++;
407
408 count++;
409 }
410
411 //
412 // Check for saturation in all other slices
413 //
414 while (p<end)
415 if (*p++ >= fSaturationLimit)
416 sat++;
417
418 if (IsNoiseCalculation())
419 return;
420
421 //
422 // Calculate the i-th sum as
423 // sum_i+1 = sum_i + slice[i+8] - slice[i]
424 // This is fast and accurate (because we are using int's)
425 //
426 count = 0;
427 max = sum;
428 Int_t idx = 0; // idx of the first slice of the maximum window
429
430 for (p=first; p+fWindowSizeLoGain<end; p++)
431 {
432
433 const Int_t ids = fLoGainFirst + count + fWindowSizeLoGain;
434 const Float_t signal = (Float_t)*(p+fWindowSizeLoGain) - PedMean[(ids+abflag) & 0x1];
435 sum += signal - fLoGainSignal[count];
436 fLoGainSignal[count + fWindowSizeLoGain] = signal;
437
438 if (sum>max)
439 {
440 max = sum;
441 idx = count+1;
442 }
443 count++;
444 }
445
446 //
447 // now calculate the time for the maximum window
448 //
449 Float_t timesignalsum = 0;
450 Int_t timesquaredsum = 0;
451
452 for (Int_t i=idx; i<idx+fWindowSizeLoGain; i++)
453 {
454 timesignalsum += fLoGainSignal[i]*i;
455 timesquaredsum += i*i;
456 }
457
458 sum = max;
459
460 time = sum != 0 ? timesignalsum / max + Float_t(fLoGainFirst) : 1.;
461 dtime = sum != 0 ? ped.GetPedestalRms() / max * sqrt(timesquaredsum - fWindowSizeLoGain*time) : 1.;
462}
463
464// --------------------------------------------------------------------------
465//
466// In addition to the resources of the base-class MExtractor:
467// MJPedestal.MExtractor.WindowSizeHiGain: 6
468// MJPedestal.MExtractor.WindowSizeLoGain: 6
469//
470Int_t MExtractTimeAndChargeSlidingWindow::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
471{
472
473 Byte_t hw = fWindowSizeHiGain;
474 Byte_t lw = fWindowSizeLoGain;
475
476 Bool_t rc = kFALSE;
477
478 if (IsEnvDefined(env, prefix, "HiGainWindowSize", print))
479 {
480 hw = GetEnvValue(env, prefix, "HiGainWindowSize", hw);
481 rc = kTRUE;
482 }
483 if (IsEnvDefined(env, prefix, "LoGainWindowSize", print))
484 {
485 lw = GetEnvValue(env, prefix, "LoGainWindowSize", lw);
486 rc = kTRUE;
487 }
488
489 if (rc)
490 SetWindowSize(hw, lw);
491
492 return MExtractTime::ReadEnv(env, prefix, print) ? kTRUE : rc;
493
494}
495
Note: See TracBrowser for help on using the repository browser.