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

Last change on this file since 8165 was 8165, 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.63 2006-10-25 18:36:26 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 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 << endl;
255 break;
256 }
257
258 switch (fWindowSizeLoGain)
259 {
260 case 1:
261 case 2:
262 SetResolutionPerPheLoGain(0.005);
263 break;
264 case 3:
265 case 4:
266 SetResolutionPerPheLoGain(0.017);
267 break;
268 case 5:
269 case 6:
270 case 7:
271 SetResolutionPerPheLoGain(0.005);
272 break;
273 case 8:
274 case 9:
275 SetResolutionPerPheLoGain(0.005);
276 break;
277 default:
278 *fLog << warn << "Could not set the low-gain extractor resolution per phe for window size "
279 << fWindowSizeLoGain << endl;
280 break;
281 }
282 }
283}
284
285// --------------------------------------------------------------------------
286//
287// InitArrays
288//
289// Gets called in the ReInit() and initialized the arrays
290//
291Bool_t MExtractTimeAndChargeSpline::InitArrays(Int_t n)
292{
293 // Initialize arrays to the maximum number of entries necessary
294 fHiGainFirstDeriv .Set(n);
295 fHiGainSecondDeriv.Set(n);
296
297 fLoGainFirstDeriv .Set(n);
298 fLoGainSecondDeriv.Set(n);
299
300 fRiseTimeLoGain = fRiseTimeHiGain * fLoGainStretch;
301 fFallTimeLoGain = fFallTimeHiGain * fLoGainStretch;
302
303 switch (fExtractionType)
304 {
305 case kAmplitude:
306 fNumHiGainSamples = 1.;
307 fNumLoGainSamples = fLoGainLast ? 1. : 0.;
308 fSqrtHiGainSamples = 1.;
309 fSqrtLoGainSamples = 1.;
310 fWindowSizeHiGain = 1;
311 fWindowSizeLoGain = 1;
312 fRiseTimeHiGain = 0.5;
313 break;
314
315 case kIntegral:
316 fNumHiGainSamples = fRiseTimeHiGain + fFallTimeHiGain;
317 fNumLoGainSamples = fLoGainLast ? fRiseTimeLoGain + fFallTimeLoGain : 0.;
318 fSqrtHiGainSamples = TMath::Sqrt(fNumHiGainSamples);
319 fSqrtLoGainSamples = TMath::Sqrt(fNumLoGainSamples);
320 fWindowSizeHiGain = TMath::CeilNint(fRiseTimeHiGain + fFallTimeHiGain);
321 fWindowSizeLoGain = TMath::CeilNint(fRiseTimeLoGain + fFallTimeLoGain);
322 break;
323 }
324
325 return kTRUE;
326}
327
328void MExtractTimeAndChargeSpline::FindTimeAndChargeHiGain2(const Float_t *ptr, Int_t num,
329 Float_t &sum, Float_t &dsum,
330 Float_t &time, Float_t &dtime,
331 Byte_t sat, Int_t maxpos) const
332{
333 // Do some handling if maxpos is last slice!
334 MExtralgoSpline s(ptr, num, fHiGainFirstDeriv.GetArray(), fHiGainSecondDeriv.GetArray());
335
336 s.SetRiseFallTime(fRiseTimeHiGain, fFallTimeHiGain);
337
338 if (IsNoiseCalculation())
339 {
340 sum = s.ExtractNoise();
341 return;
342 }
343
344 s.Extract(sat, maxpos);
345 s.GetTime(time, dtime);
346 s.GetSignal(sum, dsum);
347}
348
349void MExtractTimeAndChargeSpline::FindTimeAndChargeLoGain2(const Float_t *ptr, Int_t num,
350 Float_t &sum, Float_t &dsum,
351 Float_t &time, Float_t &dtime,
352 Byte_t sat, Int_t maxpos) const
353{
354 MExtralgoSpline s(ptr, num, fLoGainFirstDeriv.GetArray(), fLoGainSecondDeriv.GetArray());
355
356 s.SetRiseFallTime(fRiseTimeLoGain, fFallTimeLoGain);
357
358 if (IsNoiseCalculation())
359 {
360 sum = s.ExtractNoise();
361 return;
362 }
363
364 s.Extract(sat, maxpos);
365 s.GetTime(time, dtime);
366 s.GetSignal(sum, dsum);
367}
368
369// --------------------------------------------------------------------------
370//
371// In addition to the resources of the base-class MExtractor:
372// Resolution
373// RiseTimeHiGain
374// FallTimeHiGain
375// LoGainStretch
376// ExtractionType: amplitude, integral
377//
378Int_t MExtractTimeAndChargeSpline::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
379{
380
381 Bool_t rc = kFALSE;
382
383 if (IsEnvDefined(env, prefix, "Resolution", print))
384 {
385 SetResolution(GetEnvValue(env, prefix, "Resolution",fResolution));
386 rc = kTRUE;
387 }
388 if (IsEnvDefined(env, prefix, "RiseTimeHiGain", print))
389 {
390 SetRiseTimeHiGain(GetEnvValue(env, prefix, "RiseTimeHiGain", fRiseTimeHiGain));
391 rc = kTRUE;
392 }
393 if (IsEnvDefined(env, prefix, "FallTimeHiGain", print))
394 {
395 SetFallTimeHiGain(GetEnvValue(env, prefix, "FallTimeHiGain", fFallTimeHiGain));
396 rc = kTRUE;
397 }
398 if (IsEnvDefined(env, prefix, "LoGainStretch", print))
399 {
400 SetLoGainStretch(GetEnvValue(env, prefix, "LoGainStretch", fLoGainStretch));
401 rc = kTRUE;
402 }
403
404 if (IsEnvDefined(env, prefix, "ExtractionType", print))
405 {
406 TString type = GetEnvValue(env, prefix, "ExtractionType", "");
407 type.ToLower();
408 type = type.Strip(TString::kBoth);
409 if (type==(TString)"amplitude")
410 SetChargeType(kAmplitude);
411 if (type==(TString)"integral")
412 SetChargeType(kIntegral);
413 rc=kTRUE;
414 }
415
416 return MExtractTimeAndCharge::ReadEnv(env, prefix, print) ? kTRUE : rc;
417}
Note: See TracBrowser for help on using the repository browser.