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

Last change on this file since 8049 was 7999, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 11.9 KB
Line 
1#ifndef MARS_MExtralgoSpline
2#define MARS_MExtralgoSpline
3
4#ifndef ROOT_TROOT
5#include <TROOT.h>
6#endif
7
8#include <iostream>
9class TComplex;
10
11class MExtralgoSpline
12{
13public:
14 enum ExtractionType_t { kAmplitude, kIntegral }; //! Possible time and charge extraction types
15
16private:
17 ExtractionType_t fExtractionType;
18
19private:
20 //Bool_t fIsOwner; // Owner of derivatives....
21
22 // Input
23 Float_t *fVal;
24 Int_t fNum;
25
26 Float_t *fDer1;
27 Float_t *fDer2;
28
29 Float_t fRiseTime;
30 Float_t fFallTime;
31
32 Float_t fResolution;
33
34 // Result
35 Float_t fTime;
36 Float_t fTimeDev;
37 Float_t fSignal;
38 Float_t fSignalDev;
39
40 Double_t ReMul(const TComplex &c1, const TComplex &th) const;
41
42 inline Float_t Eval(Float_t val, Float_t a, Float_t deriv) const
43 {
44 return a*val + (a*a*a-a)*deriv;
45 }
46
47 // Evaluate value of spline in the interval i with x=[0;1[
48 inline Float_t Eval(const Int_t i, const Float_t x) const
49 {
50 // Eval(i,x) = (fDer2[i+1]-fDer2[i])*x*x*x + 3*fDer2[i]*x*x +
51 // (fVal[i+1]-fVal[i] -2*fDer2[i]-fDer2[i+1])*x + fVal[i];
52
53 // x := [0; 1[
54 return Eval(fVal[i], 1-x, fDer2[i]) + Eval(fVal[i+1], x, fDer2[i+1]);
55 }
56
57 /*
58 inline Float_t EvalAt(const Float_t x) const
59 {
60 Int_t i = TMath::FloorNint(x);
61
62 // handle under- and overflow of the array-range by extrapolation
63 if (i<0)
64 i=0;
65 if (i>fNum-2)
66 i = fNum-2;
67
68 return Eval(i, x-i);
69 }
70 */
71
72 // Evaluate first derivative of spline in the interval i with x=[0;1[
73 inline Double_t EvalDeriv1(const Float_t x, const Int_t i) const
74 {
75 // x := [0; 1[
76 const Double_t difval = fVal[i+1]-fVal[i];
77 const Double_t difder = fDer2[i+1]-fDer2[i];
78
79 return 3*difder*x*x + 6*fDer2[i]*x - 2*fDer2[i] - fDer2[i+1] + difval;
80 }
81
82 // Evaluate second derivative of spline in the interval i with x=[0;1[
83 inline Double_t EvalDeriv2(const Float_t x, const Int_t i) const
84 {
85 // x := [0; 1[
86 return 6*(fDer2[i+1]*x + fDer2[i]*(1-x));
87 }
88
89 Double_t FindY(Int_t i, Double_t y=0, Double_t min=0, Double_t max=1) const;
90 Double_t SearchY(Float_t maxpos, Float_t y) const;
91/*
92 // Evaluate first solution for a possible maximum (x|first deriv==0)
93 inline Double_t EvalDerivEq0S1(const Int_t i) const
94 {
95 // return the x value [0;1[ at which the derivative is zero (solution1)
96
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
103 Double_t x = 3*fDer2[i] - sqrt(3*sqt1 + 3*sqt2);
104
105 Double_t denom = 3*(fDer2[i+1]-fDer2[i]);
106
107 return -x/denom;
108 }
109
110 // Evaluate second solution for a possible maximum (x|first deriv==0)
111 inline Double_t EvalDerivEq0S2(const Int_t i) const
112 {
113 // return the x value [0;1[ at which the derivative is zero (solution2)
114
115 Double_t sumder = fDer2[i]+fDer2[i+1];
116 Double_t difder = fDer2[i]-fDer2[i+1];
117
118 Double_t sqt1 = sumder*sumder - fDer2[i]*fDer2[i+1];
119 Double_t sqt2 = difder*(fVal[i+1]-fVal[i]);
120
121 Double_t x = 3*fDer2[i] + sqrt(3*sqt1 + 3*sqt2);
122
123 Double_t denom = 3*(fDer2[i+1]-fDer2[i]);
124
125 return -x/denom;
126 }
127 */
128
129 inline void EvalDerivEq0(const Int_t i, Float_t &rc1, Float_t &rc2) const
130 {
131 Double_t sumder = fDer2[i]+fDer2[i+1];
132 Double_t difder = fDer2[i]-fDer2[i+1];
133
134 Double_t sqt1 = sumder*sumder - fDer2[i]*fDer2[i+1];
135 Double_t sqt2 = difder*(fVal[i+1]-fVal[i]);
136 Double_t sqt3 = sqrt(3*sqt1 + 3*sqt2);
137 Double_t denom = 3*(fDer2[i+1]-fDer2[i]);
138
139 rc1 = -(3*fDer2[i] + sqt3)/denom;
140 rc2 = -(3*fDer2[i] - sqt3)/denom;
141 }
142
143 // Calculate the "Stammfunktion" of the Eval-function
144 inline Double_t EvalPrimitive(Int_t i, Float_t x) const
145 {
146 /* TO BE CHECKED!
147 if (x==0)
148 return 0;
149
150 if (x==1)
151 return (fVal[i+1]+fVal[i])/2 - fDer2[i+1]/4;
152 */
153 Align(i, x);
154
155 const Double_t x2 = x*x;
156 const Double_t x4 = x2*x2;
157 const Double_t x1 = 1-x;
158 const Double_t x14 = x1*x1*x1*x1;
159
160 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];
161 }
162
163 inline void Align(Int_t &i, Float_t &x) const
164 {
165 if (i<0)
166 {
167 x += i;
168 i=0;
169 }
170 if (i>=fNum-1)
171 {
172 x += i-(fNum-2);
173 i=fNum-2;
174 }
175 }
176
177 // Calculate the intgeral of the Eval-function in
178 // bin i from a=[0;1[ to b=[0;1[
179 inline Double_t EvalInteg(Int_t i, Float_t a=0, Float_t b=1) const
180 {
181 // This is to make sure that we never access invalid
182 // memory, even if this should never happen.
183 // If it happens anyhow we extraolate the spline
184 Align(i, a);
185 Align(i, b);
186
187 return EvalPrimitive(i, b)-EvalPrimitive(i, a);
188 }
189
190 // Calculate the intgeral of the Eval-function betwen x0 and x1
191 inline Double_t EvalInteg(Float_t x0, Float_t x1) const
192 {
193 const Int_t min = TMath::CeilNint(x0);
194 const Int_t max = TMath::FloorNint(x1);
195
196 // This happens if x0 and x1 are in the same interval
197 if (min>max)
198 return EvalInteg(max, x0-max, x1-max);
199
200 // Sum complete intervals
201 Double_t sum = 0;
202 for (int i=min; i<max; i++)
203 sum += EvalInteg(i);
204
205 // Sum the incomplete intervals at the beginning and end
206 sum += EvalInteg(min-1, 1-(min-x0), 1);
207 sum += EvalInteg(max, 0, x1-max);
208
209 // return result
210 return sum;
211 }
212
213 // We search for the maximum from x=i-1 to x=i+1
214 // (Remeber: i corresponds to the value in bin i, i+1 to the
215 // next bin and i-1 to the last bin)
216 inline void GetMaxAroundI(Int_t i, Float_t &xmax, Float_t &ymax) const
217 {
218 Float_t xmax1, xmax2;
219 Float_t ymax1, ymax2;
220
221 Bool_t rc1 = i>0 && GetMax(i-1, xmax1, ymax1);
222 Bool_t rc2 = i<fNum-1 && GetMax(i, xmax2, ymax2);
223
224 // In case the medium bin is the first or last bin
225 // take the lower or upper edge of the region into account.
226 if (i==0)
227 {
228 xmax1 = 0;
229 ymax1 = fVal[0];
230 rc1 = kTRUE;
231 }
232 if (i>fNum-2)
233 {
234 xmax2 = fNum-1;
235 ymax2 = fVal[fNum-1];
236 rc2 = kTRUE;
237 }
238
239 // Take a default in case no maximum is found
240 xmax=i;
241 ymax=fVal[i];
242
243 if (rc1)
244 {
245 ymax = ymax1;
246 xmax = xmax1;
247 }
248 else
249 if (rc2)
250 {
251 ymax = ymax2;
252 xmax = xmax2;
253 }
254
255 if (rc2 && ymax2>ymax)
256 {
257 ymax = ymax2;
258 xmax = xmax2;
259 }
260 /*
261 // Search real maximum in [i-0.5, i+1.5]
262 Float_t xmax1, xmax2, xmax3;
263 Float_t ymax1, ymax2, ymax3;
264
265 Bool_t rc1 = i>0 && GetMax(i-1, xmax1, ymax1, 0.5, 1.0);
266 Bool_t rc2 = GetMax(i, xmax2, ymax2, 0.0, 1.0);
267 Bool_t rc3 = i<fNum-1 && GetMax(i+1, xmax3, ymax3, 0.0, 0.5);
268
269 // In case the medium bin is the first or last bin
270 // take the lower or upper edge of the region into account.
271 if (i==0)
272 {
273 xmax1 = 0;
274 ymax1 = Eval(0, 0);
275 rc1 = kTRUE;
276 }
277 if (i==fNum-1)
278 {
279 xmax3 = fNum-1e-5;
280 ymax3 = Eval(fNum-1, 1);
281 rc3 = kTRUE;
282 }
283
284 // Take a real default in case no maximum is found
285 xmax=i+0.5;
286 ymax=Eval(i, 0.5);
287
288 //if (!rc1 && !rc2 && !rc3)
289 // cout << "!!!!!!!!!!!!!!!" << endl;
290
291 if (rc1)
292 {
293 ymax = ymax1;
294 xmax = xmax1;
295 }
296 else
297 if (rc2)
298 {
299 ymax = ymax2;
300 xmax = xmax2;
301 }
302 else
303 if (rc3)
304 {
305 ymax = ymax3;
306 xmax = xmax3;
307 }
308
309 if (rc2 && ymax2>ymax)
310 {
311 ymax = ymax2;
312 xmax = xmax2;
313 }
314 if (rc3 && ymax3>ymax)
315 {
316 ymax = ymax3;
317 xmax = xmax3;
318 }
319*/ }
320
321 inline Bool_t GetMax(Int_t i, Float_t &xmax, Float_t &ymax, Float_t min=0, Float_t max=1) const
322 {
323 // Find analytical maximum in the bin i in the interval [min,max[
324
325 Float_t x1, x2;
326 EvalDerivEq0(i, x1, x2);
327 // const Float_t x1 = EvalDerivEq0S1(i);
328 // const Float_t x2 = EvalDerivEq0S2(i);
329
330 const Bool_t ismax1 = x1>=min && x1<max && EvalDeriv2(x1, i)<0;
331 const Bool_t ismax2 = x2>=min && x2<max && EvalDeriv2(x2, i)<0;
332
333 if (!ismax1 && !ismax2)
334 return kFALSE;
335
336 if (ismax1 && !ismax2)
337 {
338 xmax = i+x1;
339 ymax = Eval(i, x1);
340 return kTRUE;
341 }
342
343 if (!ismax1 && ismax2)
344 {
345 xmax = i+x2;
346 ymax = Eval(i, x2);
347 return kTRUE;
348 }
349
350 // Somehting must be wrong...
351 return kFALSE;
352 /*
353 std::cout << "?????????????" << std::endl;
354
355 const Double_t y1 = Eval(i, x1);
356 const Double_t y2 = Eval(i, x2);
357
358 if (y1>y2)
359 {
360 xmax = i+x1;
361 ymax = Eval(i, x1);
362 return kTRUE;
363 }
364 else
365 {
366 xmax = i+x2;
367 ymax = Eval(i, x2);
368 return kTRUE;
369 }
370
371 return kFALSE;*/
372 }
373/*
374 inline Int_t GetMaxPos(Int_t i, Float_t &xmax, Float_t &ymax) const
375 {
376 Double_t x[3];
377
378 x[0] = 0;
379 // x[1] = 1; // This means we miss a possible maximum at the
380 // upper edge of the last interval...
381
382 x[1] = EvalDerivEq0S1(i);
383 x[2] = EvalDerivEq0S2(i);
384
385 //y[0] = Eval(i, x[0]);
386 //y[1] = Eval(i, x[1]);
387 //y[1] = Eval(i, x[1]);
388 //y[2] = Eval(i, x[2]);
389
390 Int_t rc = 0;
391 Double_t max = Eval(i, x[0]);
392
393 for (Int_t j=1; j<3; j++)
394 {
395 if (x[j]<=0 || x[j]>=1)
396 continue;
397
398 const Float_t y = Eval(i, x[j]);
399 if (y>max)
400 {
401 max = y;
402 rc = j;
403 }
404 }
405
406 if (max>ymax)
407 {
408 xmax = x[rc]+i;
409 ymax = max;
410 }
411
412 return rc;
413 }
414
415 inline void GetMaxPos(Int_t min, Int_t max, Float_t &xmax, Float_t &ymax) const
416 {
417 Float_t xmax=-1;
418 Float_t ymax=-FLT_MAX;
419
420 for (int i=min; i<max; i++)
421 GetMaxPos(i, xmax, ymax);
422
423 for (int i=min+1; i<max; i++)
424 {
425 Float_t y = Eval(i, 0);
426 if (y>ymax)
427 {
428 ymax = y;
429 xmax = i;
430 }
431 }
432
433 }*/
434
435
436 void InitDerivatives() const;
437 Float_t CalcIntegral(Float_t start) const;
438
439public:
440 MExtralgoSpline(Float_t *val, Int_t n, Float_t *der1, Float_t *der2)
441 : fExtractionType(kIntegral), fVal(val), fNum(n), fDer1(der1), fDer2(der2), fTime(0), fTimeDev(-1), fSignal(0), fSignalDev(-1)
442 {
443 InitDerivatives();
444 }
445
446 void SetRiseFallTime(Float_t rise, Float_t fall) { fRiseTime=rise; fFallTime=fall; }
447 void SetExtrationType(ExtractionType_t typ) { fExtractionType = typ; }
448 void SetResolution(Float_t res) { fResolution=res; }
449
450 Float_t GetTime() const { return fTime; }
451 Float_t GetSignal() const { return fSignal; }
452
453 Float_t GetTimeDev() const { return fTimeDev; }
454 Float_t GetSignalDev() const { return fSignalDev; }
455
456 void GetSignal(Float_t &sig, Float_t &dsig) const { sig=fSignal; dsig=fSignalDev; }
457 void GetTime(Float_t &sig, Float_t &dsig) const { sig=fTime; dsig=fTimeDev; }
458
459 Float_t ExtractNoise(Int_t iter);
460 void Extract(Byte_t sat, Int_t maxpos);
461};
462
463#endif
Note: See TracBrowser for help on using the repository browser.