source: tags/Mars-V1.1/msignal/MExtractTimeAndChargeSpline.cc

Last change on this file was 8368, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 15.3 KB
Line 
1/* ======================================================================== *\
2! $Name: not supported by cvs2svn $:$Id: MExtractTimeAndChargeSpline.cc,v 1.67 2007-03-05 08:55:09 tbretz Exp $
3! --------------------------------------------------------------------------
4!
5! *
6! * This file is part of MARS, the MAGIC Analysis and Reconstruction
7! * Software. It is distributed to you in the hope that it can be a useful
8! * and timesaving tool in analyzing Data of imaging Cerenkov telescopes.
9! * It is distributed WITHOUT ANY WARRANTY.
10! *
11! * Permission to use, copy, modify and distribute this software and its
12! * documentation for any purpose is hereby granted without fee,
13! * provided that the above copyright notice appear in all copies and
14! * that both that copyright notice and this permission notice appear
15! * in supporting documentation. It is provided "as is" without express
16! * or implied warranty.
17! *
18!
19! Author(s): Thomas Bretz <mailto:tbretz@astro.uni-wuerzbrug.de>
20! Author(s): Markus Gaug 09/2004 <mailto:markus@ifae.es>
21!
22! Copyright: MAGIC Software Development, 2002-2006
23!
24!
25\* ======================================================================== */
26
27//////////////////////////////////////////////////////////////////////////////
28//
29// MExtractTimeAndChargeSpline
30//
31// Fast Spline extractor using a cubic spline algorithm, adapted from
32// Numerical Recipes in C++, 2nd edition, pp. 116-119.
33//
34// The coefficients "ya" are here denoted as "fHiGainSignal" and "fLoGainSignal"
35// which means the FADC value subtracted by the clock-noise corrected pedestal.
36//
37// The coefficients "y2a" get immediately divided 6. and are called here
38// "fHiGainSecondDeriv" and "fLoGainSecondDeriv" although they are now not exactly
39// the second derivative coefficients any more.
40//
41// The calculation of the cubic-spline interpolated value "y" on a point
42// "x" along the FADC-slices axis becomes:
43//
44// y = a*fHiGainSignal[klo] + b*fHiGainSignal[khi]
45// + (a*a*a-a)*fHiGainSecondDeriv[klo] + (b*b*b-b)*fHiGainSecondDeriv[khi]
46//
47// with:
48// a = (khi - x)
49// b = (x - klo)
50//
51// and "klo" being the lower bin edge FADC index and "khi" the upper bin edge FADC index.
52// fHiGainSignal[klo] and fHiGainSignal[khi] are the FADC values at "klo" and "khi".
53//
54// An analogues formula is used for the low-gain values.
55//
56// The coefficients fHiGainSecondDeriv and fLoGainSecondDeriv are calculated with the
57// following simplified algorithm:
58//
59// for (Int_t i=1;i<range-1;i++) {
60// pp = fHiGainSecondDeriv[i-1] + 4.;
61// fHiGainFirstDeriv[i] = fHiGainSignal[i+1] - 2.*fHiGainSignal[i] + fHiGainSignal[i-1]
62// fHiGainFirstDeriv[i] = (6.0*fHiGainFirstDeriv[i]-fHiGainFirstDeriv[i-1])/pp;
63// }
64//
65// for (Int_t k=range-2;k>=0;k--)
66// fHiGainSecondDeriv[k] = (fHiGainSecondDeriv[k]*fHiGainSecondDeriv[k+1] + fHiGainFirstDeriv[k])/6.;
67//
68//
69// This algorithm takes advantage of the fact that the x-values are all separated by exactly 1
70// which simplifies the Numerical Recipes algorithm.
71// (Note that the variables "fHiGainFirstDeriv" are not real first derivative coefficients.)
72//
73// The algorithm to search the time proceeds as follows:
74//
75// 1) Calculate all fHiGainSignal from fHiGainFirst to fHiGainLast
76// (note that an "overlap" to the low-gain arrays is possible: i.e. fHiGainLast>14 in the case of
77// the MAGIC FADCs).
78// 2) Remember the position of the slice with the highest content "fAbMax" at "fAbMaxPos".
79// 3) If one or more slices are saturated or fAbMaxPos is less than 2 slices from fHiGainFirst,
80// return fAbMaxPos as time and fAbMax as charge (note that the pedestal is subtracted here).
81// 4) Calculate all fHiGainSecondDeriv from the fHiGainSignal array
82// 5) Search for the maximum, starting in interval fAbMaxPos-1 in steps of 0.2 till fAbMaxPos-0.2.
83// If no maximum is found, go to interval fAbMaxPos+1.
84// --> 4 function evaluations
85// 6) Search for the absolute maximum from fAbMaxPos to fAbMaxPos+1 in steps of 0.2
86// --> 4 function evaluations
87// 7) Try a better precision searching from new max. position fAbMaxPos-0.2 to fAbMaxPos+0.2
88// in steps of 0.025 (83 psec. in the case of the MAGIC FADCs).
89// --> 14 function evaluations
90// 8) If Time Extraction Type kMaximum has been chosen, the position of the found maximum is
91// returned, else:
92// 9) The Half Maximum is calculated.
93// 10) fHiGainSignal is called beginning from fAbMaxPos-1 backwards until a value smaller than fHalfMax
94// is found at "klo".
95// 11) Then, the spline value between "klo" and "klo"+1 is halfed by means of bisection as long as
96// the difference between fHalfMax and spline evaluation is less than fResolution (default: 0.01).
97// --> maximum 12 interations.
98//
99// The algorithm to search the charge proceeds as follows:
100//
101// 1) If Charge Type: kAmplitude was chosen, return the Maximum of the spline, found during the
102// time search.
103// 2) If Charge Type: kIntegral was chosen, sum the fHiGainSignal between:
104// (Int_t)(fAbMaxPos - fRiseTimeHiGain) and
105// (Int_t)(fAbMaxPos + fFallTimeHiGain)
106// (default: fRiseTime: 1.5, fFallTime: 4.5)
107// sum the fLoGainSignal between:
108// (Int_t)(fAbMaxPos - fRiseTimeHiGain*fLoGainStretch) and
109// (Int_t)(fAbMaxPos + fFallTimeHiGain*fLoGainStretch)
110// (default: fLoGainStretch: 1.5)
111//
112// The values: fNumHiGainSamples and fNumLoGainSamples are set to:
113// 1) If Charge Type: kAmplitude was chosen: 1.
114// 2) If Charge Type: kIntegral was chosen: fRiseTimeHiGain + fFallTimeHiGain
115// or: fNumHiGainSamples*fLoGainStretch in the case of the low-gain
116//
117// Call: SetRange(fHiGainFirst, fHiGainLast, fLoGainFirst, fLoGainLast)
118// to modify the ranges.
119//
120// Defaults:
121// fHiGainFirst = 2
122// fHiGainLast = 14
123// fLoGainFirst = 2
124// fLoGainLast = 14
125//
126// Call: SetResolution() to define the resolution of the half-maximum search.
127// Default: 0.01
128//
129// Call: SetRiseTime() and SetFallTime() to define the integration ranges
130// for the case, the extraction type kIntegral has been chosen.
131//
132// Call: - SetChargeType(MExtractTimeAndChargeSpline::kAmplitude) for the
133// computation of the amplitude at the maximum (default) and extraction
134// the position of the maximum (default)
135// --> no further function evaluation needed
136// - SetChargeType(MExtractTimeAndChargeSpline::kIntegral) for the
137// computation of the integral beneith the spline between fRiseTimeHiGain
138// from the position of the maximum to fFallTimeHiGain after the position of
139// the maximum. The Low Gain is computed with half a slice more at the rising
140// edge and half a slice more at the falling edge.
141// The time of the half maximum is returned.
142// --> needs one function evaluations but is more precise
143//
144//////////////////////////////////////////////////////////////////////////////
145#include "MExtractTimeAndChargeSpline.h"
146
147#include "MExtralgoSpline.h"
148
149#include "MPedestalPix.h"
150
151#include "MLog.h"
152#include "MLogManip.h"
153
154ClassImp(MExtractTimeAndChargeSpline);
155
156using namespace std;
157
158const Byte_t MExtractTimeAndChargeSpline::fgHiGainFirst = 0;
159const Byte_t MExtractTimeAndChargeSpline::fgHiGainLast = 14;
160const Int_t MExtractTimeAndChargeSpline::fgLoGainFirst = 1;
161const Byte_t MExtractTimeAndChargeSpline::fgLoGainLast = 14;
162const Float_t MExtractTimeAndChargeSpline::fgResolution = 0.05;
163const Float_t MExtractTimeAndChargeSpline::fgRiseTimeHiGain = 0.64;
164const Float_t MExtractTimeAndChargeSpline::fgFallTimeHiGain = 0.76;
165const Float_t MExtractTimeAndChargeSpline::fgLoGainStretch = 1.5;
166const Float_t MExtractTimeAndChargeSpline::fgOffsetLoGain = 1.3;
167
168// --------------------------------------------------------------------------
169//
170// Default constructor.
171//
172// Calls:
173// - SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast)
174//
175// Initializes:
176// - fResolution to fgResolution
177// - fRiseTimeHiGain to fgRiseTimeHiGain
178// - fFallTimeHiGain to fgFallTimeHiGain
179// - Charge Extraction Type to kAmplitude
180// - fLoGainStretch to fgLoGainStretch
181//
182MExtractTimeAndChargeSpline::MExtractTimeAndChargeSpline(const char *name, const char *title)
183 : fExtractionType(kIntegral)
184{
185
186 fName = name ? name : "MExtractTimeAndChargeSpline";
187 fTitle = title ? title : "Calculate photons arrival time using a fast spline";
188
189 SetResolution();
190 SetLoGainStretch();
191 SetOffsetLoGain(fgOffsetLoGain);
192
193 SetRiseTimeHiGain();
194 SetFallTimeHiGain();
195
196 SetRange(fgHiGainFirst, fgHiGainLast, fgLoGainFirst, fgLoGainLast);
197}
198
199
200//-------------------------------------------------------------------
201//
202// Set the ranges
203// In order to set the fNum...Samples variables correctly for the case,
204// the integral is computed, have to overwrite this function and make an
205// explicit call to SetChargeType().
206//
207void MExtractTimeAndChargeSpline::SetRange(Byte_t hifirst, Byte_t hilast, Int_t lofirst, Byte_t lolast)
208{
209 MExtractor::SetRange(hifirst, hilast, lofirst, lolast);
210
211 SetChargeType(fExtractionType);
212}
213
214//-------------------------------------------------------------------
215//
216// Set the Charge Extraction type. Possible are:
217// - kAmplitude: Search the value of the spline at the maximum
218// - kIntegral: Integral the spline from fHiGainFirst to fHiGainLast,
219// by counting the edge bins only half and setting the
220// second derivative to zero, there.
221//
222void MExtractTimeAndChargeSpline::SetChargeType( ExtractionType_t typ )
223{
224 fExtractionType = typ;
225
226 InitArrays(fHiGainFirstDeriv.GetSize());
227
228 switch (fExtractionType)
229 {
230 case kAmplitude:
231 SetResolutionPerPheHiGain(0.053);
232 SetResolutionPerPheLoGain(0.016);
233 return;
234
235 case kIntegral:
236 switch (fWindowSizeHiGain)
237 {
238 case 1:
239 SetResolutionPerPheHiGain(0.041);
240 break;
241 case 2:
242 SetResolutionPerPheHiGain(0.064);
243 break;
244 case 3:
245 case 4:
246 SetResolutionPerPheHiGain(0.050);
247 break;
248 case 5:
249 case 6:
250 SetResolutionPerPheHiGain(0.030);
251 break;
252 default:
253 *fLog << warn << GetDescriptor() << ": Could not set the high-gain extractor resolution per phe for window size "
254 << fWindowSizeHiGain << "... using default!" << endl;
255 SetResolutionPerPheHiGain(0.050);
256 break;
257 }
258
259 switch (fWindowSizeLoGain)
260 {
261 case 1:
262 case 2:
263 SetResolutionPerPheLoGain(0.005);
264 break;
265 case 3:
266 case 4:
267 SetResolutionPerPheLoGain(0.017);
268 break;
269 case 5:
270 case 6:
271 case 7:
272 SetResolutionPerPheLoGain(0.005);
273 break;
274 case 8:
275 case 9:
276 SetResolutionPerPheLoGain(0.005);
277 break;
278 default:
279 *fLog << warn << "Could not set the low-gain extractor resolution per phe for window size "
280 << fWindowSizeLoGain << "... using default!" << endl;
281 SetResolutionPerPheLoGain(0.005);
282 break;
283 }
284 }
285}
286
287// --------------------------------------------------------------------------
288//
289// InitArrays
290//
291// Gets called in the ReInit() and initialized the arrays
292//
293Bool_t MExtractTimeAndChargeSpline::InitArrays(Int_t n)
294{
295 // Initialize arrays to the maximum number of entries necessary
296 fHiGainFirstDeriv .Set(n);
297 fHiGainSecondDeriv.Set(n);
298
299 fLoGainFirstDeriv .Set(n);
300 fLoGainSecondDeriv.Set(n);
301
302 fRiseTimeLoGain = fRiseTimeHiGain * fLoGainStretch;
303 fFallTimeLoGain = fFallTimeHiGain * fLoGainStretch;
304
305 switch (fExtractionType)
306 {
307 case kAmplitude:
308 fNumHiGainSamples = 1.;
309 fNumLoGainSamples = fLoGainLast ? 1. : 0.;
310 fSqrtHiGainSamples = 1.;
311 fSqrtLoGainSamples = 1.;
312 fWindowSizeHiGain = 1;
313 fWindowSizeLoGain = 1;
314 fRiseTimeHiGain = 0.5;
315 break;
316
317 case kIntegral:
318 fNumHiGainSamples = fRiseTimeHiGain + fFallTimeHiGain;
319 fNumLoGainSamples = fLoGainLast ? fRiseTimeLoGain + fFallTimeLoGain : 0.;
320 fSqrtHiGainSamples = TMath::Sqrt(fNumHiGainSamples);
321 fSqrtLoGainSamples = TMath::Sqrt(fNumLoGainSamples);
322 fWindowSizeHiGain = TMath::CeilNint(fRiseTimeHiGain + fFallTimeHiGain);
323 fWindowSizeLoGain = TMath::CeilNint(fRiseTimeLoGain + fFallTimeLoGain);
324 break;
325 }
326
327 return kTRUE;
328}
329
330void MExtractTimeAndChargeSpline::FindTimeAndChargeHiGain2(const Float_t *ptr, Int_t num,
331 Float_t &sum, Float_t &dsum,
332 Float_t &time, Float_t &dtime,
333 Byte_t sat, Int_t maxpos) const
334{
335 // Do some handling if maxpos is last slice!
336 MExtralgoSpline s(ptr, num, fHiGainFirstDeriv.GetArray(), fHiGainSecondDeriv.GetArray());
337
338 s.SetRiseFallTime(fRiseTimeHiGain, fFallTimeHiGain);
339
340 if (IsNoiseCalculation())
341 {
342 sum = s.ExtractNoise();
343 return;
344 }
345
346 s.Extract(sat, maxpos);
347 s.GetTime(time, dtime);
348 s.GetSignal(sum, dsum);
349
350}
351
352void MExtractTimeAndChargeSpline::FindTimeAndChargeLoGain2(const Float_t *ptr, Int_t num,
353 Float_t &sum, Float_t &dsum,
354 Float_t &time, Float_t &dtime,
355 Byte_t sat, Int_t maxpos) const
356{
357 MExtralgoSpline s(ptr, num, fLoGainFirstDeriv.GetArray(), fLoGainSecondDeriv.GetArray());
358
359 s.SetRiseFallTime(fRiseTimeLoGain, fFallTimeLoGain);
360
361 if (IsNoiseCalculation())
362 {
363 sum = s.ExtractNoise();
364 return;
365 }
366
367 s.Extract(sat, maxpos);
368 s.GetTime(time, dtime);
369 s.GetSignal(sum, dsum);
370}
371
372// --------------------------------------------------------------------------
373//
374// In addition to the resources of the base-class MExtractor:
375// Resolution
376// RiseTimeHiGain
377// FallTimeHiGain
378// LoGainStretch
379// ExtractionType: amplitude, integral
380//
381Int_t MExtractTimeAndChargeSpline::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
382{
383
384 Bool_t rc = kFALSE;
385
386 if (IsEnvDefined(env, prefix, "Resolution", print))
387 {
388 SetResolution(GetEnvValue(env, prefix, "Resolution",fResolution));
389 rc = kTRUE;
390 }
391 if (IsEnvDefined(env, prefix, "RiseTimeHiGain", print))
392 {
393 SetRiseTimeHiGain(GetEnvValue(env, prefix, "RiseTimeHiGain", fRiseTimeHiGain));
394 rc = kTRUE;
395 }
396 if (IsEnvDefined(env, prefix, "FallTimeHiGain", print))
397 {
398 SetFallTimeHiGain(GetEnvValue(env, prefix, "FallTimeHiGain", fFallTimeHiGain));
399 rc = kTRUE;
400 }
401 if (IsEnvDefined(env, prefix, "LoGainStretch", print))
402 {
403 SetLoGainStretch(GetEnvValue(env, prefix, "LoGainStretch", fLoGainStretch));
404 rc = kTRUE;
405 }
406
407 if (IsEnvDefined(env, prefix, "ExtractionType", print))
408 {
409 TString type = GetEnvValue(env, prefix, "ExtractionType", "");
410 type.ToLower();
411 type = type.Strip(TString::kBoth);
412 if (type==(TString)"amplitude")
413 SetChargeType(kAmplitude);
414 if (type==(TString)"integral")
415 SetChargeType(kIntegral);
416 rc=kTRUE;
417 }
418
419 return MExtractTimeAndCharge::ReadEnv(env, prefix, print) ? kTRUE : rc;
420}
Note: See TracBrowser for help on using the repository browser.