source: trunk/MagicSoft/Mars/mextralgo/MExtralgoSpline.h@ 9127

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