source: trunk/Mars/mextralgo/MExtralgoSpline.h@ 16894

Last change on this file since 16894 was 14854, checked in by tbretz, 12 years ago
Added a few more options of what to extract, including width of pulse.
File size: 10.8 KB
Line 
1#ifndef MARS_MExtralgoSpline
2#define MARS_MExtralgoSpline
3
4#ifndef ROOT_TMath
5#include <TMath.h>
6#endif
7
8class MArrayF;
9class TComplex;
10
11class MExtralgoSpline
12{
13public:
14 enum ExtractionType_t
15 {
16 kIntegral = BIT(0),
17 kTimeRel = BIT(1),
18 kMaximum = BIT(2),
19 kDynWidth = BIT(3),
20 kFixedWidth = BIT(4),
21
22 // For backward compatibility
23 kAmplitudeAbs = 0, // Height of maximum, absolute height leading edge
24 kAmplitudeRel = kTimeRel, // Height of maximum, relative height leading edge
25 kAmplitude = kMaximum, // Position and height of maximum
26 kIntegralAbs = kIntegral, // Integral, absolute height leading edge
27 kIntegralRel = kIntegral|kTimeRel, // Integral, relative height leading edge
28 kIntegralDyn = kTimeRel|kDynWidth, // Integrate between leading edge and falling edge
29 kIntegralFixed = kTimeRel|kFixedWidth, // Integrate between leading edge minus fRiseTime and plus fFallTime
30 };
31
32private:
33 ExtractionType_t fExtractionType;
34
35private:
36 //Bool_t fIsOwner; // Owner of derivatives....
37
38 // Input
39 Float_t const *fVal;
40 const Int_t fNum;
41
42 Float_t *fDer1;
43 Float_t *fDer2;
44
45 Float_t fRiseTime;
46 Float_t fFallTime;
47
48 Float_t fHeightTm;
49
50 // Result
51 Float_t fTime;
52 Float_t fTimeDev;
53 Float_t fWidth;
54 Float_t fWidthDev;
55 Float_t fSignal;
56 Float_t fSignalDev;
57 Float_t fHeight;
58
59 Double_t ReMul(const TComplex &c1, const TComplex &th) const;
60
61 inline Float_t Eval(Float_t val, Float_t a, Float_t deriv) const
62 {
63 return a*val + (a*a*a-a)*deriv;
64 }
65
66 // Evaluate value of spline in the interval i with x=[0;1[
67 inline Float_t Eval(const Int_t i, const Float_t x) const
68 {
69 // Eval(i,x) = (fDer2[i+1]-fDer2[i])*x*x*x + 3*fDer2[i]*x*x +
70 // (fVal[i+1]-fVal[i] -2*fDer2[i]-fDer2[i+1])*x + fVal[i];
71
72 // x := [0; 1[
73 return Eval(fVal[i], 1-x, fDer2[i]) + Eval(fVal[i+1], x, fDer2[i+1]);
74 }
75
76 // Evaluate first derivative of spline in the interval i with x=[0;1[
77 inline Double_t EvalDeriv1(const Int_t i, const Float_t x) const
78 {
79 // x := [0; 1[
80 const Double_t difval = fVal[i+1]-fVal[i];
81 const Double_t difder = fDer2[i+1]-fDer2[i];
82
83 //return 3*difder*x*x + 6*fDer2[i]*x - 2*fDer2[i] - fDer2[i+1] + difval;
84 return 3*difder*x*x + (6*x - 2)*fDer2[i] - fDer2[i+1] + difval;
85 }
86
87 // Evaluate second derivative of spline in the interval i with x=[0;1[
88 inline Double_t EvalDeriv2(const Int_t i, const Float_t x) const
89 {
90 // x := [0; 1[
91 return 6*(fDer2[i+1]*x + fDer2[i]*(1-x));
92 }
93
94 Int_t SolvePol3(Int_t i, Double_t y, Double_t &x1, Double_t &x2, Double_t &x3) const;
95 Double_t FindYdn(Int_t i, Double_t y=0, Double_t min=0, Double_t max=1) const;
96 Double_t FindYup(Int_t i, Double_t y=0, Double_t min=0, Double_t max=1) const;
97 //Double_t FindY(Int_t i, Bool_t downwards, Double_t y=0, Double_t min=0, Double_t max=1) const;
98
99 Int_t EvalDerivEq0(const Int_t i, Double_t &x1, Double_t &x2) const;
100/*
101 inline void EvalDerivEq0(const Int_t i, Float_t &rc1, Float_t &rc2) const
102 {
103 // --- ORIGINAL CODE ---
104 Double_t sumder = fDer2[i]+fDer2[i+1];
105 Double_t difder = fDer2[i]-fDer2[i+1];
106
107 Double_t sqt1 = sumder*sumder - fDer2[i]*fDer2[i+1];
108 Double_t sqt2 = difder*(fVal[i+1]-fVal[i]);
109 Double_t sqt3 = sqrt(3*sqt1 + 3*sqt2);
110 Double_t denom = -3*(fDer2[i+1]-fDer2[i]);
111
112 rc1 = (3*fDer2[i] + sqt3)/denom;
113 rc2 = (3*fDer2[i] - sqt3)/denom;
114
115 // --- NEW CODE ---
116 Double_t sumder = fDer2[i]+fDer2[i+1];
117 Double_t difder = fDer2[i]-fDer2[i+1];
118
119 Double_t sqt1 = sumder*sumder - fDer2[i]*fDer2[i+1];
120 Double_t sqt2 = difder*(fVal[i+1]-fVal[i]);
121 Double_t sqt3 = sqt1+sqt2<0 ? 0 : sqrt((sqt1 + sqt2)/3);
122
123 rc1 = (fDer2[i] + sqt3)/difder;
124 rc2 = (fDer2[i] - sqt3)/difder;
125 }*/
126
127 // Calculate the "Stammfunktion" of the Eval-function
128 inline Double_t EvalPrimitive(Int_t i, Float_t x) const
129 {
130 Align(i, x);
131
132 if (x==0)
133 return -fDer2[i]/4;
134
135 if (x==1)
136 return (fVal[i+1] + fVal[i])/2 - fDer2[i+1]/4 - fDer2[i]/2;
137
138 const Double_t x2 = x*x;
139 const Double_t x4 = x2*x2;
140 const Double_t x1 = 1-x;
141 const Double_t x14 = x1*x1*x1*x1;
142
143 return x2*fVal[i+1]/2 + (x4/2-x2)*fDer2[i+1]/2 + (x-x2/2)*fVal[i] + (x2/2-x-x14/4)*fDer2[i];
144
145 }
146
147 inline void Align(Int_t &i, Float_t &x) const
148 {
149 if (i<0)
150 {
151 x += i;
152 i=0;
153 }
154 if (i>=fNum-1)
155 {
156 x += i-(fNum-2);
157 i=fNum-2;
158 }
159 }
160
161 // Calculate the intgeral of the Eval-function in
162 // bin i from 0 <= a < b < 1
163 inline Double_t EvalInteg(Int_t i, Float_t a, Float_t b) const
164 {
165 return EvalPrimitive(i, b)-EvalPrimitive(i, a);
166 }
167
168 // Identical to EvalInteg(i, 0, 1) but much faster
169 // Be carefull: NO RANGECHECK!
170 inline Double_t EvalInteg(Int_t i) const
171 {
172 return (fVal[i+1] + fVal[i])/2 - (fDer2[i+1] + fDer2[i])/4;
173 }
174
175 // Identical to sum of EvalInteg(i, 0, 1) for i=a to i=b-1,
176 // but much faster
177 // It is identical to EvalInteg(fVal[a], fVal[b])
178 // Be carefull: NO RANGECHECK!
179 inline Double_t EvalInteg(Int_t a, Int_t b) const
180 {
181 /*
182 Double_t sum = 0;
183 for (int i=a; i<b; i++)
184 sum += EvalInteg(i);
185
186 return sum;
187 */
188
189 if (a==b)
190 return 0;
191
192 Double_t sum=0;
193 for (const Float_t *ptr=fDer2+a+1; ptr<fDer2+b; ptr++)
194 sum -= *ptr;
195
196 sum -= (fDer2[a]+fDer2[b])/2;
197
198 sum /= 2;
199
200 for (const Float_t *ptr=fVal+a+1; ptr<fVal+b; ptr++)
201 sum += *ptr;
202
203 sum += (fVal[a]+fVal[b])/2;
204
205 return sum;
206 }
207
208 // Calculate the intgeral of the Eval-function betwen x0 and x1
209 inline Double_t EvalInteg(Float_t x0, Float_t x1) const
210 {
211 // RANGE CHECK MISSING!
212
213 const Int_t min = TMath::CeilNint(x0);
214 const Int_t max = TMath::FloorNint(x1);
215
216 // This happens if x0 and x1 are in the same interval
217 if (min>max)
218 return EvalInteg(max, x0-max, x1-max);
219
220 // Sum complete intervals
221 Double_t sum = EvalInteg(min, max);
222
223 // Sum the incomplete intervals at the beginning and end
224 sum += EvalInteg(min-1, 1-(min-x0), 1);
225 sum += EvalInteg(max, 0, x1-max);
226
227 // return result
228 return sum;
229 }
230
231 // We search for the maximum from x=i-1 to x=i+1
232 // (Remeber: i corresponds to the value in bin i, i+1 to the
233 // next bin and i-1 to the last bin)
234 inline void GetMaxAroundI(Int_t i, Float_t &xmax, Float_t &ymax) const
235 {
236 Float_t xmax1=0, xmax2=0;
237 Float_t ymax1=0, ymax2=0;
238
239 Bool_t rc1 = i>0 && GetMax(i-1, xmax1, ymax1);
240 Bool_t rc2 = i<fNum-1 && GetMax(i, xmax2, ymax2);
241
242 // In case the medium bin is the first or last bin
243 // take the lower or upper edge of the region into account.
244 if (i==0)
245 {
246 xmax1 = 0;
247 ymax1 = fVal[0];
248 rc1 = kTRUE;
249 }
250 if (i>=fNum-1)
251 {
252 xmax2 = fNum-1;
253 ymax2 = fVal[fNum-1];
254 rc2 = kTRUE;
255 }
256
257 // Take a default in case no maximum is found
258 // FIXME: Check THIS!!!
259 xmax=i;
260 ymax=fVal[i];
261
262 if (rc1)
263 {
264 ymax = ymax1;
265 xmax = xmax1;
266 }
267 else
268 if (rc2)
269 {
270 ymax = ymax2;
271 xmax = xmax2;
272 }
273
274 if (rc2 && ymax2>ymax)
275 {
276 ymax = ymax2;
277 xmax = xmax2;
278 }
279 }
280
281 inline Bool_t GetMax(Int_t i, Float_t &xmax, Float_t &ymax, Float_t min=0, Float_t max=1) const
282 {
283 // Find analytical maximum in the bin i in the interval [min,max[
284
285 Double_t x1=-1; // This initialisation should not really be
286 Double_t x2=-1; // necessary but makes valgriund happy.
287
288 if (!EvalDerivEq0(i, x1, x2))
289 return kFALSE;
290
291 const Bool_t ismax1 = x1>=min && x1<max && EvalDeriv2(i, x1)<0;
292 const Bool_t ismax2 = x2>=min && x2<max && EvalDeriv2(i, x2)<0;
293
294 if (!ismax1 && !ismax2)
295 return kFALSE;
296
297 if (ismax1 && !ismax2)
298 {
299 xmax = i+x1;
300 ymax = Eval(i, x1);
301 return kTRUE;
302 }
303
304 if (!ismax1 && ismax2)
305 {
306 xmax = i+x2;
307 ymax = Eval(i, x2);
308 return kTRUE;
309 }
310
311 // Somehting must be wrong...
312 return kFALSE;
313 }
314
315 void InitDerivatives() const;
316 Float_t CalcIntegral(Float_t start) const;
317 Float_t CalcIntegral(Float_t beg, Float_t width) const;
318
319public:
320 MExtralgoSpline(const Float_t *val, Int_t n, Float_t *der1, Float_t *der2)
321 : fExtractionType(kIntegralRel), fVal(val), fNum(n), fDer1(der1), fDer2(der2), fHeightTm(0.5), fTime(0), fTimeDev(-1), fSignal(0), fSignalDev(-1)
322 {
323 InitDerivatives();
324 }
325
326 void SetRiseFallTime(Float_t rise, Float_t fall) { fRiseTime=rise; fFallTime=fall; }
327 void SetExtractionType(ExtractionType_t typ) { fExtractionType = typ; }
328 void SetHeightTm(Float_t h) { fHeightTm = h; }
329
330 Float_t GetTime() const { return fTime; }
331 Float_t GetWidth() const { return fWidth; }
332 Float_t GetSignal() const { return fSignal; }
333 Float_t GetHeight() const { return fHeight; }
334
335 Float_t GetTimeDev() const { return fTimeDev; }
336 Float_t GetWidthDev() const { return fWidthDev; }
337 Float_t GetSignalDev() const { return fSignalDev; }
338
339 void GetSignal(Float_t &sig, Float_t &dsig) const { sig=fSignal; dsig=fSignalDev; }
340 void GetWidth(Float_t &sig, Float_t &dsig) const { sig=fWidth; dsig=fWidthDev; }
341 void GetTime(Float_t &sig, Float_t &dsig) const { sig=fTime; dsig=fTimeDev; }
342
343 Float_t ExtractNoise(/*Int_t iter*/);
344 void Extract(Int_t maxpos, Bool_t width=kFALSE);
345
346 Float_t EvalAt(const Float_t x) const;
347 Float_t Deriv1(const Float_t x) const;
348
349 Double_t SearchYdn(Float_t maxpos, Float_t y) const;
350 Double_t SearchYup(Float_t maxpos, Float_t y) const;
351
352 Double_t SearchYdn(Float_t y) const { return SearchYdn(fNum, y); }
353 Double_t SearchYup(Float_t y) const { return SearchYup(0, y); }
354
355 MArrayF GetIntegral(bool norm=false) const;
356};
357
358inline Float_t MExtralgoSpline::EvalAt(const Float_t x) const
359{
360 Int_t i = TMath::FloorNint(x);
361 Float_t f = x-i;
362
363 Align(i, f);
364
365 return Eval(i, f);
366}
367
368inline Float_t MExtralgoSpline::Deriv1(const Float_t x) const
369{
370 Int_t i = TMath::FloorNint(x);
371 Float_t f = x-i;
372
373 Align(i, f);
374
375 return EvalDeriv1(i, f);
376}
377
378#endif
Note: See TracBrowser for help on using the repository browser.