1 | /* ======================================================================== *\
|
---|
2 | ! $Name: not supported by cvs2svn $:$Id: MMath.cc,v 1.37 2007-08-17 10:53:48 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 analysing 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 | !
|
---|
20 | ! Author(s): Thomas Bretz 3/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
|
---|
21 | !
|
---|
22 | ! Copyright: MAGIC Software Development, 2000-2005
|
---|
23 | !
|
---|
24 | !
|
---|
25 | \* ======================================================================== */
|
---|
26 |
|
---|
27 | /////////////////////////////////////////////////////////////////////////////
|
---|
28 | //
|
---|
29 | // MMath
|
---|
30 | //
|
---|
31 | // Mars - Math package (eg Significances, etc)
|
---|
32 | //
|
---|
33 | /////////////////////////////////////////////////////////////////////////////
|
---|
34 | #include "MMath.h"
|
---|
35 |
|
---|
36 | #ifndef ROOT_TVector2
|
---|
37 | #include <TVector2.h>
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | #ifndef ROOT_TVector3
|
---|
41 | #include <TVector3.h>
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | #ifndef ROOT_TArrayD
|
---|
45 | #include <TArrayD.h>
|
---|
46 | #endif
|
---|
47 |
|
---|
48 | #ifndef ROOT_TComplex
|
---|
49 | #include <TComplex.h>
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | //NamespaceImp(MMath);
|
---|
53 |
|
---|
54 | // --------------------------------------------------------------------------
|
---|
55 | //
|
---|
56 | // Calculate Significance as
|
---|
57 | // significance = (s-b)/sqrt(s+k*k*b) mit k=s/b
|
---|
58 | //
|
---|
59 | // s: total number of events in signal region
|
---|
60 | // b: number of background events in signal region
|
---|
61 | //
|
---|
62 | Double_t MMath::Significance(Double_t s, Double_t b)
|
---|
63 | {
|
---|
64 | const Double_t k = b==0 ? 0 : s/b;
|
---|
65 | const Double_t f = s+k*k*b;
|
---|
66 |
|
---|
67 | return f==0 ? 0 : (s-b)/TMath::Sqrt(f);
|
---|
68 | }
|
---|
69 |
|
---|
70 | // --------------------------------------------------------------------------
|
---|
71 | //
|
---|
72 | // Symmetrized significance - this is somehow analog to
|
---|
73 | // SignificanceLiMaSigned
|
---|
74 | //
|
---|
75 | // Returns Significance(s,b) if s>b otherwise -Significance(b, s);
|
---|
76 | //
|
---|
77 | Double_t MMath::SignificanceSym(Double_t s, Double_t b)
|
---|
78 | {
|
---|
79 | return s>b ? Significance(s, b) : -Significance(b, s);
|
---|
80 | }
|
---|
81 |
|
---|
82 | // --------------------------------------------------------------------------
|
---|
83 | //
|
---|
84 | // calculates the significance according to Li & Ma
|
---|
85 | // ApJ 272 (1983) 317, Formula 17
|
---|
86 | //
|
---|
87 | // s // s: number of on events
|
---|
88 | // b // b: number of off events
|
---|
89 | // alpha = t_on/t_off; // t: observation time
|
---|
90 | //
|
---|
91 | // The significance has the same (positive!) value for s>b and b>s.
|
---|
92 | //
|
---|
93 | // Returns -1 if s<0 or b<0 or alpha<0 or the argument of sqrt<0
|
---|
94 | //
|
---|
95 | // Here is some eMail written by Daniel Mazin about the meaning of the arguments:
|
---|
96 | //
|
---|
97 | // > Ok. Here is my understanding:
|
---|
98 | // > According to Li&Ma paper (correctly cited in MMath.cc) alpha is the
|
---|
99 | // > scaling factor. The mathematics behind the formula 17 (and/or 9) implies
|
---|
100 | // > exactly this. If you scale OFF to ON first (using time or using any other
|
---|
101 | // > method), then you cannot use formula 17 (9) anymore. You can just try
|
---|
102 | // > the formula before scaling (alpha!=1) and after scaling (alpha=1), you
|
---|
103 | // > will see the result will be different.
|
---|
104 | //
|
---|
105 | // > Here are less mathematical arguments:
|
---|
106 | //
|
---|
107 | // > 1) the better background determination you have (smaller alpha) the more
|
---|
108 | // > significant is your excess, thus your analysis is more sensitive. If you
|
---|
109 | // > normalize OFF to ON first, you loose this sensitivity.
|
---|
110 | //
|
---|
111 | // > 2) the normalization OFF to ON has an error, which naturally depends on
|
---|
112 | // > the OFF and ON. This error is propagating to the significance of your
|
---|
113 | // > excess if you use the Li&Ma formula 17 correctly. But if you normalize
|
---|
114 | // > first and use then alpha=1, the error gets lost completely, you loose
|
---|
115 | // > somehow the criteria of goodness of the normalization.
|
---|
116 | //
|
---|
117 | Double_t MMath::SignificanceLiMa(Double_t s, Double_t b, Double_t alpha)
|
---|
118 | {
|
---|
119 | const Double_t sum = s+b;
|
---|
120 |
|
---|
121 | if (s<0 || b<0 || alpha<=0)
|
---|
122 | return -1;
|
---|
123 |
|
---|
124 | const Double_t l = s==0 ? 0 : s*TMath::Log(s/sum*(alpha+1)/alpha);
|
---|
125 | const Double_t m = b==0 ? 0 : b*TMath::Log(b/sum*(alpha+1) );
|
---|
126 |
|
---|
127 | return l+m<0 ? -1 : TMath::Sqrt((l+m)*2);
|
---|
128 | }
|
---|
129 |
|
---|
130 | // --------------------------------------------------------------------------
|
---|
131 | //
|
---|
132 | // Calculates MMath::SignificanceLiMa(s, b, alpha). Returns 0 if the
|
---|
133 | // calculation has failed. Otherwise the Li/Ma significance which was
|
---|
134 | // calculated. If s<b a negative value is returned.
|
---|
135 | //
|
---|
136 | Double_t MMath::SignificanceLiMaSigned(Double_t s, Double_t b, Double_t alpha)
|
---|
137 | {
|
---|
138 | const Double_t sig = SignificanceLiMa(s, b, alpha);
|
---|
139 | if (sig<=0)
|
---|
140 | return 0;
|
---|
141 |
|
---|
142 | return TMath::Sign(sig, s-alpha*b);
|
---|
143 | }
|
---|
144 |
|
---|
145 | // --------------------------------------------------------------------------
|
---|
146 | //
|
---|
147 | // Return Li/Ma (5) for the error of the excess, under the assumption that
|
---|
148 | // the existance of a signal is already known.
|
---|
149 | //
|
---|
150 | Double_t MMath::SignificanceLiMaExc(Double_t s, Double_t b, Double_t alpha)
|
---|
151 | {
|
---|
152 | Double_t Ns = s - alpha*b;
|
---|
153 | Double_t sN = s + alpha*alpha*b;
|
---|
154 |
|
---|
155 | if (Ns<0 || sN<0)
|
---|
156 | return 0;
|
---|
157 |
|
---|
158 | if (Ns==0 && sN==0)
|
---|
159 | return 0;
|
---|
160 |
|
---|
161 | return Ns/TMath::Sqrt(sN);
|
---|
162 | }
|
---|
163 |
|
---|
164 | // --------------------------------------------------------------------------
|
---|
165 | //
|
---|
166 | // Returns: 2/(sigma*sqrt(2))*integral[0,x](exp(-(x-mu)^2/(2*sigma^2)))
|
---|
167 | //
|
---|
168 | Double_t MMath::GaussProb(Double_t x, Double_t sigma, Double_t mean)
|
---|
169 | {
|
---|
170 | if (x<mean)
|
---|
171 | return 0;
|
---|
172 |
|
---|
173 | static const Double_t sqrt2 = TMath::Sqrt(2.);
|
---|
174 |
|
---|
175 | const Double_t rc = TMath::Erf((x-mean)/(sigma*sqrt2));
|
---|
176 |
|
---|
177 | if (rc<0)
|
---|
178 | return 0;
|
---|
179 | if (rc>1)
|
---|
180 | return 1;
|
---|
181 |
|
---|
182 | return rc;
|
---|
183 | }
|
---|
184 |
|
---|
185 | // ------------------------------------------------------------------------
|
---|
186 | //
|
---|
187 | // Return the "median" (at 68.3%) value of the distribution of
|
---|
188 | // abs(a[i]-Median)
|
---|
189 | //
|
---|
190 | template <class Size, class Element>
|
---|
191 | Double_t MMath::MedianDevImp(Size n, const Element *a, Double_t &med)
|
---|
192 | {
|
---|
193 | static const Double_t prob = 0.682689477208650697; //MMath::GaussProb(1.0);
|
---|
194 |
|
---|
195 | // Sanity check
|
---|
196 | if (n <= 0 || !a)
|
---|
197 | return 0;
|
---|
198 |
|
---|
199 | // Get median of distribution
|
---|
200 | med = TMath::Median(n, a);
|
---|
201 |
|
---|
202 | // Create the abs(a[i]-med) distribution
|
---|
203 | Double_t arr[n];
|
---|
204 | for (int i=0; i<n; i++)
|
---|
205 | arr[i] = TMath::Abs(a[i]-med);
|
---|
206 |
|
---|
207 | //return TMath::Median(n, arr)/0.67449896936; //MMath::GaussProb(x)=0.5
|
---|
208 |
|
---|
209 | // Define where to divide (floor because the highest possible is n-1)
|
---|
210 | const Int_t div = TMath::FloorNint(n*prob);
|
---|
211 |
|
---|
212 | // Calculate result
|
---|
213 | Double_t dev = TMath::KOrdStat(n, arr, div);
|
---|
214 | if (n%2 == 0)
|
---|
215 | {
|
---|
216 | dev += TMath::KOrdStat(n, arr, div-1);
|
---|
217 | dev /= 2;
|
---|
218 | }
|
---|
219 |
|
---|
220 | return dev;
|
---|
221 | }
|
---|
222 |
|
---|
223 | // ------------------------------------------------------------------------
|
---|
224 | //
|
---|
225 | // Return the "median" (at 68.3%) value of the distribution of
|
---|
226 | // abs(a[i]-Median)
|
---|
227 | //
|
---|
228 | Double_t MMath::MedianDev(Long64_t n, const Short_t *a, Double_t &med)
|
---|
229 | {
|
---|
230 | return MedianDevImp(n, a, med);
|
---|
231 | }
|
---|
232 |
|
---|
233 | // ------------------------------------------------------------------------
|
---|
234 | //
|
---|
235 | // Return the "median" (at 68.3%) value of the distribution of
|
---|
236 | // abs(a[i]-Median)
|
---|
237 | //
|
---|
238 | Double_t MMath::MedianDev(Long64_t n, const Int_t *a, Double_t &med)
|
---|
239 | {
|
---|
240 | return MedianDevImp(n, a, med);
|
---|
241 | }
|
---|
242 |
|
---|
243 | // ------------------------------------------------------------------------
|
---|
244 | //
|
---|
245 | // Return the "median" (at 68.3%) value of the distribution of
|
---|
246 | // abs(a[i]-Median)
|
---|
247 | //
|
---|
248 | Double_t MMath::MedianDev(Long64_t n, const Float_t *a, Double_t &med)
|
---|
249 | {
|
---|
250 | return MedianDevImp(n, a, med);
|
---|
251 | }
|
---|
252 |
|
---|
253 | // ------------------------------------------------------------------------
|
---|
254 | //
|
---|
255 | // Return the "median" (at 68.3%) value of the distribution of
|
---|
256 | // abs(a[i]-Median)
|
---|
257 | //
|
---|
258 | Double_t MMath::MedianDev(Long64_t n, const Double_t *a, Double_t &med)
|
---|
259 | {
|
---|
260 | return MedianDevImp(n, a, med);
|
---|
261 | }
|
---|
262 |
|
---|
263 | // ------------------------------------------------------------------------
|
---|
264 | //
|
---|
265 | // Return the "median" (at 68.3%) value of the distribution of
|
---|
266 | // abs(a[i]-Median)
|
---|
267 | //
|
---|
268 | Double_t MMath::MedianDev(Long64_t n, const Long_t *a, Double_t &med)
|
---|
269 | {
|
---|
270 | return MedianDevImp(n, a, med);
|
---|
271 | }
|
---|
272 |
|
---|
273 | // ------------------------------------------------------------------------
|
---|
274 | //
|
---|
275 | // Return the "median" (at 68.3%) value of the distribution of
|
---|
276 | // abs(a[i]-Median)
|
---|
277 | //
|
---|
278 | Double_t MMath::MedianDev(Long64_t n, const Long64_t *a, Double_t &med)
|
---|
279 | {
|
---|
280 | return MedianDevImp(n, a, med);
|
---|
281 | }
|
---|
282 |
|
---|
283 | Double_t MMath::MedianDev(Long64_t n, const Short_t *a) { Double_t med; return MedianDevImp(n, a, med); }
|
---|
284 | Double_t MMath::MedianDev(Long64_t n, const Int_t *a) { Double_t med; return MedianDevImp(n, a, med); }
|
---|
285 | Double_t MMath::MedianDev(Long64_t n, const Float_t *a) { Double_t med; return MedianDevImp(n, a, med); }
|
---|
286 | Double_t MMath::MedianDev(Long64_t n, const Double_t *a) { Double_t med; return MedianDevImp(n, a, med); }
|
---|
287 | Double_t MMath::MedianDev(Long64_t n, const Long_t *a) { Double_t med; return MedianDevImp(n, a, med); }
|
---|
288 | Double_t MMath::MedianDev(Long64_t n, const Long64_t *a) { Double_t med; return MedianDevImp(n, a, med); }
|
---|
289 |
|
---|
290 | // --------------------------------------------------------------------------
|
---|
291 | //
|
---|
292 | // This function reduces the precision to roughly 0.5% of a Float_t by
|
---|
293 | // changing its bit-pattern (Be carefull, in rare cases this function must
|
---|
294 | // be adapted to different machines!). This is usefull to enforce better
|
---|
295 | // compression by eg. gzip.
|
---|
296 | //
|
---|
297 | void MMath::ReducePrecision(Float_t &val)
|
---|
298 | {
|
---|
299 | UInt_t &f = (UInt_t&)val;
|
---|
300 |
|
---|
301 | f += 0x00004000;
|
---|
302 | f &= 0xffff8000;
|
---|
303 | }
|
---|
304 |
|
---|
305 | // -------------------------------------------------------------------------
|
---|
306 | //
|
---|
307 | // Quadratic interpolation
|
---|
308 | //
|
---|
309 | // calculate the parameters of a parabula such that
|
---|
310 | // y(i) = a + b*x(i) + c*x(i)^2
|
---|
311 | //
|
---|
312 | // If the determinant==0 an empty TVector3 is returned.
|
---|
313 | //
|
---|
314 | TVector3 MMath::GetParab(const TVector3 &x, const TVector3 &y)
|
---|
315 | {
|
---|
316 | const Double_t x1 = x(0);
|
---|
317 | const Double_t x2 = x(1);
|
---|
318 | const Double_t x3 = x(2);
|
---|
319 |
|
---|
320 | const Double_t y1 = y(0);
|
---|
321 | const Double_t y2 = y(1);
|
---|
322 | const Double_t y3 = y(2);
|
---|
323 |
|
---|
324 | const double det =
|
---|
325 | + x2*x3*x3 + x1*x2*x2 + x3*x1*x1
|
---|
326 | - x2*x1*x1 - x3*x2*x2 - x1*x3*x3;
|
---|
327 |
|
---|
328 |
|
---|
329 | if (det==0)
|
---|
330 | return TVector3();
|
---|
331 |
|
---|
332 | const double det1 = 1.0/det;
|
---|
333 |
|
---|
334 | const double ai11 = x2*x3*x3 - x3*x2*x2;
|
---|
335 | const double ai12 = x3*x1*x1 - x1*x3*x3;
|
---|
336 | const double ai13 = x1*x2*x2 - x2*x1*x1;
|
---|
337 |
|
---|
338 | const double ai21 = x2*x2 - x3*x3;
|
---|
339 | const double ai22 = x3*x3 - x1*x1;
|
---|
340 | const double ai23 = x1*x1 - x2*x2;
|
---|
341 |
|
---|
342 | const double ai31 = x3 - x2;
|
---|
343 | const double ai32 = x1 - x3;
|
---|
344 | const double ai33 = x2 - x1;
|
---|
345 |
|
---|
346 | return TVector3((ai11*y1 + ai12*y2 + ai13*y3) * det1,
|
---|
347 | (ai21*y1 + ai22*y2 + ai23*y3) * det1,
|
---|
348 | (ai31*y1 + ai32*y2 + ai33*y3) * det1);
|
---|
349 | }
|
---|
350 |
|
---|
351 | // --------------------------------------------------------------------------
|
---|
352 | //
|
---|
353 | // Interpolate the points with x-coordinates vx and y-coordinates vy
|
---|
354 | // by a parabola (second order polynomial) and return the value at x.
|
---|
355 | //
|
---|
356 | Double_t MMath::InterpolParabLin(const TVector3 &vx, const TVector3 &vy, Double_t x)
|
---|
357 | {
|
---|
358 | const TVector3 c = GetParab(vx, vy);
|
---|
359 | return c(0) + c(1)*x + c(2)*x*x;
|
---|
360 | }
|
---|
361 |
|
---|
362 | // --------------------------------------------------------------------------
|
---|
363 | //
|
---|
364 | // Interpolate the points with x-coordinates vx=(-1,0,1) and
|
---|
365 | // y-coordinates vy by a parabola (second order polynomial) and return
|
---|
366 | // the value at x.
|
---|
367 | //
|
---|
368 | Double_t MMath::InterpolParabLin(const TVector3 &vy, Double_t x)
|
---|
369 | {
|
---|
370 | const TVector3 c(vy(1), (vy(2)-vy(0))/2, vy(0)/2 - vy(1) + vy(2)/2);
|
---|
371 | return c(0) + c(1)*x + c(2)*x*x;
|
---|
372 | }
|
---|
373 |
|
---|
374 | Double_t MMath::InterpolParabLog(const TVector3 &vx, const TVector3 &vy, Double_t x)
|
---|
375 | {
|
---|
376 | const Double_t l0 = TMath::Log10(vx(0));
|
---|
377 | const Double_t l1 = TMath::Log10(vx(1));
|
---|
378 | const Double_t l2 = TMath::Log10(vx(2));
|
---|
379 |
|
---|
380 | const TVector3 vx0(l0, l1, l2);
|
---|
381 | return InterpolParabLin(vx0, vy, TMath::Log10(x));
|
---|
382 | }
|
---|
383 |
|
---|
384 | Double_t MMath::InterpolParabCos(const TVector3 &vx, const TVector3 &vy, Double_t x)
|
---|
385 | {
|
---|
386 | const Double_t l0 = TMath::Cos(vx(0));
|
---|
387 | const Double_t l1 = TMath::Cos(vx(1));
|
---|
388 | const Double_t l2 = TMath::Cos(vx(2));
|
---|
389 |
|
---|
390 | const TVector3 vx0(l0, l1, l2);
|
---|
391 | return InterpolParabLin(vx0, vy, TMath::Cos(x));
|
---|
392 | }
|
---|
393 |
|
---|
394 | // --------------------------------------------------------------------------
|
---|
395 | //
|
---|
396 | // Analytically calculated result of a least square fit of:
|
---|
397 | // y = A*e^(B*x)
|
---|
398 | // Equal weights
|
---|
399 | //
|
---|
400 | // It returns TArrayD(2) = { A, B };
|
---|
401 | //
|
---|
402 | // see: http://mathworld.wolfram.com/LeastSquaresFittingExponential.html
|
---|
403 | //
|
---|
404 | TArrayD MMath::LeastSqFitExpW1(Int_t n, Double_t *x, Double_t *y)
|
---|
405 | {
|
---|
406 | Double_t sumxsqy = 0;
|
---|
407 | Double_t sumylny = 0;
|
---|
408 | Double_t sumxy = 0;
|
---|
409 | Double_t sumy = 0;
|
---|
410 | Double_t sumxylny = 0;
|
---|
411 | for (int i=0; i<n; i++)
|
---|
412 | {
|
---|
413 | sumylny += y[i]*TMath::Log(y[i]);
|
---|
414 | sumxy += x[i]*y[i];
|
---|
415 | sumxsqy += x[i]*x[i]*y[i];
|
---|
416 | sumxylny += x[i]*y[i]*TMath::Log(y[i]);
|
---|
417 | sumy += y[i];
|
---|
418 | }
|
---|
419 |
|
---|
420 | const Double_t dev = sumy*sumxsqy - sumxy*sumxy;
|
---|
421 |
|
---|
422 | const Double_t a = (sumxsqy*sumylny - sumxy*sumxylny)/dev;
|
---|
423 | const Double_t b = (sumy*sumxylny - sumxy*sumylny)/dev;
|
---|
424 |
|
---|
425 | TArrayD rc(2);
|
---|
426 | rc[0] = TMath::Exp(a);
|
---|
427 | rc[1] = b;
|
---|
428 | return rc;
|
---|
429 | }
|
---|
430 |
|
---|
431 | // --------------------------------------------------------------------------
|
---|
432 | //
|
---|
433 | // Analytically calculated result of a least square fit of:
|
---|
434 | // y = A*e^(B*x)
|
---|
435 | // Greater weights to smaller values
|
---|
436 | //
|
---|
437 | // It returns TArrayD(2) = { A, B };
|
---|
438 | //
|
---|
439 | // see: http://mathworld.wolfram.com/LeastSquaresFittingExponential.html
|
---|
440 | //
|
---|
441 | TArrayD MMath::LeastSqFitExp(Int_t n, Double_t *x, Double_t *y)
|
---|
442 | {
|
---|
443 | // -------- Greater weights to smaller values ---------
|
---|
444 | Double_t sumlny = 0;
|
---|
445 | Double_t sumxlny = 0;
|
---|
446 | Double_t sumxsq = 0;
|
---|
447 | Double_t sumx = 0;
|
---|
448 | for (int i=0; i<n; i++)
|
---|
449 | {
|
---|
450 | sumlny += TMath::Log(y[i]);
|
---|
451 | sumxlny += x[i]*TMath::Log(y[i]);
|
---|
452 |
|
---|
453 | sumxsq += x[i]*x[i];
|
---|
454 | sumx += x[i];
|
---|
455 | }
|
---|
456 |
|
---|
457 | const Double_t dev = n*sumxsq-sumx*sumx;
|
---|
458 |
|
---|
459 | const Double_t a = (sumlny*sumxsq - sumx*sumxlny)/dev;
|
---|
460 | const Double_t b = (n*sumxlny - sumx*sumlny)/dev;
|
---|
461 |
|
---|
462 | TArrayD rc(2);
|
---|
463 | rc[0] = TMath::Exp(a);
|
---|
464 | rc[1] = b;
|
---|
465 | return rc;
|
---|
466 | }
|
---|
467 |
|
---|
468 | // --------------------------------------------------------------------------
|
---|
469 | //
|
---|
470 | // Analytically calculated result of a least square fit of:
|
---|
471 | // y = A+B*ln(x)
|
---|
472 | //
|
---|
473 | // It returns TArrayD(2) = { A, B };
|
---|
474 | //
|
---|
475 | // see: http://mathworld.wolfram.com/LeastSquaresFittingLogarithmic.html
|
---|
476 | //
|
---|
477 | TArrayD MMath::LeastSqFitLog(Int_t n, Double_t *x, Double_t *y)
|
---|
478 | {
|
---|
479 | Double_t sumylnx = 0;
|
---|
480 | Double_t sumy = 0;
|
---|
481 | Double_t sumlnx = 0;
|
---|
482 | Double_t sumlnxsq = 0;
|
---|
483 | for (int i=0; i<n; i++)
|
---|
484 | {
|
---|
485 | sumylnx += y[i]*TMath::Log(x[i]);
|
---|
486 | sumy += y[i];
|
---|
487 | sumlnx += TMath::Log(x[i]);
|
---|
488 | sumlnxsq += TMath::Log(x[i])*TMath::Log(x[i]);
|
---|
489 | }
|
---|
490 |
|
---|
491 | const Double_t b = (n*sumylnx-sumy*sumlnx)/(n*sumlnxsq-sumlnx*sumlnx);
|
---|
492 | const Double_t a = (sumy-b*sumlnx)/n;
|
---|
493 |
|
---|
494 | TArrayD rc(2);
|
---|
495 | rc[0] = a;
|
---|
496 | rc[1] = b;
|
---|
497 | return rc;
|
---|
498 | }
|
---|
499 |
|
---|
500 | // --------------------------------------------------------------------------
|
---|
501 | //
|
---|
502 | // Analytically calculated result of a least square fit of:
|
---|
503 | // y = A*x^B
|
---|
504 | //
|
---|
505 | // It returns TArrayD(2) = { A, B };
|
---|
506 | //
|
---|
507 | // see: http://mathworld.wolfram.com/LeastSquaresFittingPowerLaw.html
|
---|
508 | //
|
---|
509 | TArrayD MMath::LeastSqFitPowerLaw(Int_t n, Double_t *x, Double_t *y)
|
---|
510 | {
|
---|
511 | Double_t sumlnxlny = 0;
|
---|
512 | Double_t sumlnx = 0;
|
---|
513 | Double_t sumlny = 0;
|
---|
514 | Double_t sumlnxsq = 0;
|
---|
515 | for (int i=0; i<n; i++)
|
---|
516 | {
|
---|
517 | sumlnxlny += TMath::Log(x[i])*TMath::Log(y[i]);
|
---|
518 | sumlnx += TMath::Log(x[i]);
|
---|
519 | sumlny += TMath::Log(y[i]);
|
---|
520 | sumlnxsq += TMath::Log(x[i])*TMath::Log(x[i]);
|
---|
521 | }
|
---|
522 |
|
---|
523 | const Double_t b = (n*sumlnxlny-sumlnx*sumlny)/(n*sumlnxsq-sumlnx*sumlnx);
|
---|
524 | const Double_t a = (sumlny-b*sumlnx)/n;
|
---|
525 |
|
---|
526 | TArrayD rc(2);
|
---|
527 | rc[0] = TMath::Exp(a);
|
---|
528 | rc[1] = b;
|
---|
529 | return rc;
|
---|
530 | }
|
---|
531 |
|
---|
532 | // --------------------------------------------------------------------------
|
---|
533 | //
|
---|
534 | // Calculate the intersection of two lines defined by (x1;y1) and (x2;x2)
|
---|
535 | // Returns the intersection point.
|
---|
536 | //
|
---|
537 | // It is assumed that the lines intersect. If there is no intersection
|
---|
538 | // TVector2() is returned (which is not destinguishable from
|
---|
539 | // TVector2(0,0) if the intersection is at the coordinate source)
|
---|
540 | //
|
---|
541 | // Formula from: http://mathworld.wolfram.com/Line-LineIntersection.html
|
---|
542 | //
|
---|
543 | TVector2 MMath::GetIntersectionPoint(const TVector2 &x1, const TVector2 &y1, const TVector2 &x2, const TVector2 &y2)
|
---|
544 | {
|
---|
545 | TMatrix d(2,2);
|
---|
546 | d[0][0] = x1.X()-y1.X();
|
---|
547 | d[0][1] = x2.X()-y2.X();
|
---|
548 | d[1][0] = x1.Y()-y1.Y();
|
---|
549 | d[1][1] = x2.Y()-y2.Y();
|
---|
550 |
|
---|
551 | const Double_t denom = d.Determinant();
|
---|
552 | if (denom==0)
|
---|
553 | return TVector2();
|
---|
554 |
|
---|
555 | TMatrix l1(2,2);
|
---|
556 | TMatrix l2(2,2);
|
---|
557 |
|
---|
558 | l1[0][0] = x1.X();
|
---|
559 | l1[0][1] = y1.X();
|
---|
560 | l2[0][0] = x2.X();
|
---|
561 | l2[0][1] = y2.X();
|
---|
562 |
|
---|
563 | l1[1][0] = x1.Y();
|
---|
564 | l1[1][1] = y1.Y();
|
---|
565 | l2[1][0] = x2.Y();
|
---|
566 | l2[1][1] = y2.Y();
|
---|
567 |
|
---|
568 | TMatrix a(2,2);
|
---|
569 | a[0][0] = l1.Determinant();
|
---|
570 | a[0][1] = l2.Determinant();
|
---|
571 | a[1][0] = x1.X()-y1.X();
|
---|
572 | a[1][1] = x2.X()-y2.X();
|
---|
573 |
|
---|
574 | const Double_t X = a.Determinant()/denom;
|
---|
575 |
|
---|
576 | a[1][0] = x1.Y()-y1.Y();
|
---|
577 | a[1][1] = x2.Y()-y2.Y();
|
---|
578 |
|
---|
579 | const Double_t Y = a.Determinant()/denom;
|
---|
580 |
|
---|
581 | return TVector2(X, Y);
|
---|
582 | }
|
---|
583 |
|
---|
584 | // --------------------------------------------------------------------------
|
---|
585 | //
|
---|
586 | // Solves: x^2 + ax + b = 0;
|
---|
587 | // Return number of solutions returned as x1, x2
|
---|
588 | //
|
---|
589 | Int_t MMath::SolvePol2(Double_t a, Double_t b, Double_t &x1, Double_t &x2)
|
---|
590 | {
|
---|
591 | const Double_t r = a*a - 4*b;
|
---|
592 | if (r<0)
|
---|
593 | return 0;
|
---|
594 |
|
---|
595 | if (r==0)
|
---|
596 | {
|
---|
597 | x1 = x2 = -a/2;
|
---|
598 | return 1;
|
---|
599 | }
|
---|
600 |
|
---|
601 | const Double_t s = TMath::Sqrt(r);
|
---|
602 |
|
---|
603 | x1 = (-a+s)/2;
|
---|
604 | x2 = (-a-s)/2;
|
---|
605 |
|
---|
606 | return 2;
|
---|
607 | }
|
---|
608 |
|
---|
609 | // --------------------------------------------------------------------------
|
---|
610 | //
|
---|
611 | // This is a helper function making the execution of SolverPol3 a bit faster
|
---|
612 | //
|
---|
613 | static inline Double_t ReMul(const TComplex &c1, const TComplex &th)
|
---|
614 | {
|
---|
615 | const TComplex c2 = TComplex::Cos(th/3.);
|
---|
616 | return c1.Re() * c2.Re() - c1.Im() * c2.Im();
|
---|
617 | }
|
---|
618 |
|
---|
619 | // --------------------------------------------------------------------------
|
---|
620 | //
|
---|
621 | // Solves: x^3 + ax^2 + bx + c = 0;
|
---|
622 | // Return number of the real solutions, returned as z1, z2, z3
|
---|
623 | //
|
---|
624 | // Algorithm adapted from http://home.att.net/~srschmitt/cubizen.heml
|
---|
625 | // Which is based on the solution given in
|
---|
626 | // http://mathworld.wolfram.com/CubicEquation.html
|
---|
627 | //
|
---|
628 | // -------------------------------------------------------------------------
|
---|
629 | //
|
---|
630 | // Exact solutions of cubic polynomial equations
|
---|
631 | // by Stephen R. Schmitt Algorithm
|
---|
632 | //
|
---|
633 | // An exact solution of the cubic polynomial equation:
|
---|
634 | //
|
---|
635 | // x^3 + a*x^2 + b*x + c = 0
|
---|
636 | //
|
---|
637 | // was first published by Gerolamo Cardano (1501-1576) in his treatise,
|
---|
638 | // Ars Magna. He did not discoverer of the solution; a professor of
|
---|
639 | // mathematics at the University of Bologna named Scipione del Ferro (ca.
|
---|
640 | // 1465-1526) is credited as the first to find an exact solution. In the
|
---|
641 | // years since, several improvements to the original solution have been
|
---|
642 | // discovered. Zeno source code
|
---|
643 | //
|
---|
644 | // http://home.att.net/~srschmitt/cubizen.html
|
---|
645 | //
|
---|
646 | // % compute real or complex roots of cubic polynomial
|
---|
647 | // function cubic( var z1, z2, z3 : real, a, b, c : real ) : real
|
---|
648 | //
|
---|
649 | // var Q, R, D, S, T : real
|
---|
650 | // var im, th : real
|
---|
651 | //
|
---|
652 | // Q := (3*b - a^2)/9
|
---|
653 | // R := (9*b*a - 27*c - 2*a^3)/54
|
---|
654 | // D := Q^3 + R^2 % polynomial discriminant
|
---|
655 | //
|
---|
656 | // if (D >= 0) then % complex or duplicate roots
|
---|
657 | //
|
---|
658 | // S := sgn(R + sqrt(D))*abs(R + sqrt(D))^(1/3)
|
---|
659 | // T := sgn(R - sqrt(D))*abs(R - sqrt(D))^(1/3)
|
---|
660 | //
|
---|
661 | // z1 := -a/3 + (S + T) % real root
|
---|
662 | // z2 := -a/3 - (S + T)/2 % real part of complex root
|
---|
663 | // z3 := -a/3 - (S + T)/2 % real part of complex root
|
---|
664 | // im := abs(sqrt(3)*(S - T)/2) % complex part of root pair
|
---|
665 | //
|
---|
666 | // else % distinct real roots
|
---|
667 | //
|
---|
668 | // th := arccos(R/sqrt( -Q^3))
|
---|
669 | //
|
---|
670 | // z1 := 2*sqrt(-Q)*cos(th/3) - a/3
|
---|
671 | // z2 := 2*sqrt(-Q)*cos((th + 2*pi)/3) - a/3
|
---|
672 | // z3 := 2*sqrt(-Q)*cos((th + 4*pi)/3) - a/3
|
---|
673 | // im := 0
|
---|
674 | //
|
---|
675 | // end if
|
---|
676 | //
|
---|
677 | // return im % imaginary part
|
---|
678 | //
|
---|
679 | // end function
|
---|
680 | //
|
---|
681 | // see also http://en.wikipedia.org/wiki/Cubic_equation
|
---|
682 | //
|
---|
683 | Int_t MMath::SolvePol3(Double_t a, Double_t b, Double_t c,
|
---|
684 | Double_t &x1, Double_t &x2, Double_t &x3)
|
---|
685 | {
|
---|
686 | // Double_t coeff[4] = { 1, a, b, c };
|
---|
687 | // return TMath::RootsCubic(coeff, x1, x2, x3) ? 1 : 3;
|
---|
688 |
|
---|
689 | const Double_t Q = (a*a - 3*b)/9;
|
---|
690 | const Double_t R = (9*b*a - 27*c - 2*a*a*a)/54;
|
---|
691 | const Double_t D = R*R - Q*Q*Q; // polynomial discriminant
|
---|
692 |
|
---|
693 | // ----- The single-real / duplicate-roots solution -----
|
---|
694 |
|
---|
695 | // D<0: three real roots
|
---|
696 | // D>0: one real root
|
---|
697 | // D==0: maximum two real roots (two identical roots)
|
---|
698 |
|
---|
699 | // R==0: only one unique root
|
---|
700 | // R!=0: two roots
|
---|
701 |
|
---|
702 | if (D==0)
|
---|
703 | {
|
---|
704 | const Double_t r = MMath::Sqrt3(R);
|
---|
705 |
|
---|
706 | x1 = r - a/3.; // real root
|
---|
707 | if (R==0)
|
---|
708 | return 1;
|
---|
709 |
|
---|
710 | x2 = 2*r - a/3.; // real root
|
---|
711 | return 2;
|
---|
712 | }
|
---|
713 |
|
---|
714 | if (D>0) // complex or duplicate roots
|
---|
715 | {
|
---|
716 | const Double_t sqrtd = TMath::Sqrt(D);
|
---|
717 |
|
---|
718 | const Double_t A = TMath::Sign(1., R)*MMath::Sqrt3(TMath::Abs(R)+sqrtd);
|
---|
719 |
|
---|
720 | // The case A==0 cannot happen. This would imply D==0
|
---|
721 | // if (A==0)
|
---|
722 | // {
|
---|
723 | // x1 = -a/3;
|
---|
724 | // return 1;
|
---|
725 | // }
|
---|
726 |
|
---|
727 | x1 = (A+Q/A)-a/3;
|
---|
728 |
|
---|
729 | //const Double_t S = MMath::Sqrt3(R + sqrtd);
|
---|
730 | //const Double_t T = MMath::Sqrt3(R - sqrtd);
|
---|
731 | //x1 = (S+T) - a/3.; // real root
|
---|
732 |
|
---|
733 | return 1;
|
---|
734 |
|
---|
735 | //z2 = (S + T)/2 - a/3.; // real part of complex root
|
---|
736 | //z3 = (S + T)/2 - a/3.; // real part of complex root
|
---|
737 | //im = fabs(sqrt(3)*(S - T)/2) // complex part of root pair
|
---|
738 | }
|
---|
739 |
|
---|
740 | // ----- The general solution with three roots ---
|
---|
741 |
|
---|
742 | if (Q==0)
|
---|
743 | return 0;
|
---|
744 |
|
---|
745 | if (Q>0) // This is here for speed reasons
|
---|
746 | {
|
---|
747 | const Double_t sqrtq = TMath::Sqrt(Q);
|
---|
748 | const Double_t rq = R/TMath::Abs(Q);
|
---|
749 |
|
---|
750 | const Double_t t = TMath::ACos(rq/sqrtq)/3;
|
---|
751 |
|
---|
752 | static const Double_t sqrt3 = TMath::Sqrt(3.);
|
---|
753 |
|
---|
754 | const Double_t s = TMath::Sin(t)*sqrt3;
|
---|
755 | const Double_t c = TMath::Cos(t);
|
---|
756 |
|
---|
757 | x1 = 2*sqrtq * c - a/3;
|
---|
758 | x2 = -sqrtq * (s + c) - a/3;
|
---|
759 | x3 = sqrtq * (s - c) - a/3;
|
---|
760 |
|
---|
761 | /* --- Easier to understand but slower ---
|
---|
762 | const Double_t th1 = TMath::ACos(rq/sqrtq);
|
---|
763 | const Double_t th2 = th1 + TMath::TwoPi();
|
---|
764 | const Double_t th3 = th2 + TMath::TwoPi();
|
---|
765 |
|
---|
766 | x1 = 2.*sqrtq * TMath::Cos(th1/3.) - a/3.;
|
---|
767 | x2 = 2.*sqrtq * TMath::Cos(th2/3.) - a/3.;
|
---|
768 | x3 = 2.*sqrtq * TMath::Cos(th3/3.) - a/3.;
|
---|
769 | */
|
---|
770 | return 3;
|
---|
771 | }
|
---|
772 |
|
---|
773 | const TComplex sqrtq = TComplex::Sqrt(Q);
|
---|
774 | const Double_t rq = R/TMath::Abs(Q);
|
---|
775 |
|
---|
776 | const TComplex th1 = TComplex::ACos(rq/sqrtq);
|
---|
777 | const TComplex th2 = th1 + TMath::TwoPi();
|
---|
778 | const TComplex th3 = th2 + TMath::TwoPi();
|
---|
779 |
|
---|
780 | // For ReMul, see bove
|
---|
781 | x1 = ReMul(2.*sqrtq, th1) - a/3.;
|
---|
782 | x2 = ReMul(2.*sqrtq, th2) - a/3.;
|
---|
783 | x3 = ReMul(2.*sqrtq, th3) - a/3.;
|
---|
784 |
|
---|
785 | return 3;
|
---|
786 | }
|
---|