source: trunk/MagicSoft/Mars/msignal/MExtractTimeAndChargeSpline.cc@ 8294

Last change on this file since 8294 was 8166, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 15.2 KB
Line 
1/* ======================================================================== *\
2! $Name: not supported by cvs2svn $:$Id: MExtractTimeAndChargeSpline.cc,v 1.64 2006-10-25 18:41:47 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 Byte_t MExtractTimeAndChargeSpline::fgLoGainFirst = 1;
161const Byte_t MExtractTimeAndChargeSpline::fgLoGainLast = 14;
162const Float_t MExtractTimeAndChargeSpline::fgResolution = 0.05;
163const Float_t MExtractTimeAndChargeSpline::fgRiseTimeHiGain = 0.7;
164const Float_t MExtractTimeAndChargeSpline::fgFallTimeHiGain = 1.0;
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 : /*fRandomIter(0),*/ 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, Byte_t lofirst, Byte_t lolast)
208{
209
210 MExtractor::SetRange(hifirst, hilast, lofirst, lolast);
211
212 SetChargeType(fExtractionType);
213}
214
215//-------------------------------------------------------------------
216//
217// Set the Charge Extraction type. Possible are:
218// - kAmplitude: Search the value of the spline at the maximum
219// - kIntegral: Integral the spline from fHiGainFirst to fHiGainLast,
220// by counting the edge bins only half and setting the
221// second derivative to zero, there.
222//
223void MExtractTimeAndChargeSpline::SetChargeType( ExtractionType_t typ )
224{
225 fExtractionType = typ;
226
227 InitArrays(fHiGainFirstDeriv.GetSize());
228
229 switch (fExtractionType)
230 {
231 case kAmplitude:
232 SetResolutionPerPheHiGain(0.053);
233 SetResolutionPerPheLoGain(0.016);
234 return;
235
236 case kIntegral:
237 switch (fWindowSizeHiGain)
238 {
239 case 1:
240 SetResolutionPerPheHiGain(0.041);
241 break;
242 case 2:
243 SetResolutionPerPheHiGain(0.064);
244 break;
245 case 3:
246 case 4:
247 SetResolutionPerPheHiGain(0.050);
248 break;
249 case 5:
250 case 6:
251 SetResolutionPerPheHiGain(0.030);
252 break;
253 default:
254 *fLog << warn << GetDescriptor() << ": Could not set the high-gain extractor resolution per phe for window size "
255 << fWindowSizeHiGain << endl;
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 << endl;
281 break;
282 }
283 }
284}
285
286// --------------------------------------------------------------------------
287//
288// InitArrays
289//
290// Gets called in the ReInit() and initialized the arrays
291//
292Bool_t MExtractTimeAndChargeSpline::InitArrays(Int_t n)
293{
294 // Initialize arrays to the maximum number of entries necessary
295 fHiGainFirstDeriv .Set(n);
296 fHiGainSecondDeriv.Set(n);
297
298 fLoGainFirstDeriv .Set(n);
299 fLoGainSecondDeriv.Set(n);
300
301 fRiseTimeLoGain = fRiseTimeHiGain * fLoGainStretch;
302 fFallTimeLoGain = fFallTimeHiGain * fLoGainStretch;
303
304 switch (fExtractionType)
305 {
306 case kAmplitude:
307 fNumHiGainSamples = 1.;
308 fNumLoGainSamples = fLoGainLast ? 1. : 0.;
309 fSqrtHiGainSamples = 1.;
310 fSqrtLoGainSamples = 1.;
311 fWindowSizeHiGain = 1;
312 fWindowSizeLoGain = 1;
313 fRiseTimeHiGain = 0.5;
314 break;
315
316 case kIntegral:
317 fNumHiGainSamples = fRiseTimeHiGain + fFallTimeHiGain;
318 fNumLoGainSamples = fLoGainLast ? fRiseTimeLoGain + fFallTimeLoGain : 0.;
319 fSqrtHiGainSamples = TMath::Sqrt(fNumHiGainSamples);
320 fSqrtLoGainSamples = TMath::Sqrt(fNumLoGainSamples);
321 fWindowSizeHiGain = TMath::CeilNint(fRiseTimeHiGain + fFallTimeHiGain);
322 fWindowSizeLoGain = TMath::CeilNint(fRiseTimeLoGain + fFallTimeLoGain);
323 break;
324 }
325
326 return kTRUE;
327}
328
329void MExtractTimeAndChargeSpline::FindTimeAndChargeHiGain2(const Float_t *ptr, Int_t num,
330 Float_t &sum, Float_t &dsum,
331 Float_t &time, Float_t &dtime,
332 Byte_t sat, Int_t maxpos) const
333{
334 // Do some handling if maxpos is last slice!
335 MExtralgoSpline s(ptr, num, fHiGainFirstDeriv.GetArray(), fHiGainSecondDeriv.GetArray());
336
337 s.SetRiseFallTime(fRiseTimeHiGain, fFallTimeHiGain);
338
339 if (IsNoiseCalculation())
340 {
341 sum = s.ExtractNoise();
342 return;
343 }
344
345 s.Extract(sat, maxpos);
346 s.GetTime(time, dtime);
347 s.GetSignal(sum, dsum);
348}
349
350void MExtractTimeAndChargeSpline::FindTimeAndChargeLoGain2(const Float_t *ptr, Int_t num,
351 Float_t &sum, Float_t &dsum,
352 Float_t &time, Float_t &dtime,
353 Byte_t sat, Int_t maxpos) const
354{
355 MExtralgoSpline s(ptr, num, fLoGainFirstDeriv.GetArray(), fLoGainSecondDeriv.GetArray());
356
357 s.SetRiseFallTime(fRiseTimeLoGain, fFallTimeLoGain);
358
359 if (IsNoiseCalculation())
360 {
361 sum = s.ExtractNoise();
362 return;
363 }
364
365 s.Extract(sat, maxpos);
366 s.GetTime(time, dtime);
367 s.GetSignal(sum, dsum);
368}
369
370// --------------------------------------------------------------------------
371//
372// In addition to the resources of the base-class MExtractor:
373// Resolution
374// RiseTimeHiGain
375// FallTimeHiGain
376// LoGainStretch
377// ExtractionType: amplitude, integral
378//
379Int_t MExtractTimeAndChargeSpline::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
380{
381
382 Bool_t rc = kFALSE;
383
384 if (IsEnvDefined(env, prefix, "Resolution", print))
385 {
386 SetResolution(GetEnvValue(env, prefix, "Resolution",fResolution));
387 rc = kTRUE;
388 }
389 if (IsEnvDefined(env, prefix, "RiseTimeHiGain", print))
390 {
391 SetRiseTimeHiGain(GetEnvValue(env, prefix, "RiseTimeHiGain", fRiseTimeHiGain));
392 rc = kTRUE;
393 }
394 if (IsEnvDefined(env, prefix, "FallTimeHiGain", print))
395 {
396 SetFallTimeHiGain(GetEnvValue(env, prefix, "FallTimeHiGain", fFallTimeHiGain));
397 rc = kTRUE;
398 }
399 if (IsEnvDefined(env, prefix, "LoGainStretch", print))
400 {
401 SetLoGainStretch(GetEnvValue(env, prefix, "LoGainStretch", fLoGainStretch));
402 rc = kTRUE;
403 }
404
405 if (IsEnvDefined(env, prefix, "ExtractionType", print))
406 {
407 TString type = GetEnvValue(env, prefix, "ExtractionType", "");
408 type.ToLower();
409 type = type.Strip(TString::kBoth);
410 if (type==(TString)"amplitude")
411 SetChargeType(kAmplitude);
412 if (type==(TString)"integral")
413 SetChargeType(kIntegral);
414 rc=kTRUE;
415 }
416
417 return MExtractTimeAndCharge::ReadEnv(env, prefix, print) ? kTRUE : rc;
418}
Note: See TracBrowser for help on using the repository browser.