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