source: trunk/MagicSoft/Mars/mbase/MMath.cc@ 7825

Last change on this file since 7825 was 7672, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 11.3 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz 3/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2005
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MMath
28//
29// Mars - Math package (eg Significances, etc)
30//
31/////////////////////////////////////////////////////////////////////////////
32#include "MMath.h"
33
34#ifndef ROOT_TVector3
35#include <TVector3.h>
36#endif
37
38#ifndef ROOT_TArrayD
39#include <TArrayD.h>
40#endif
41// --------------------------------------------------------------------------
42//
43// Calculate Significance as
44// significance = (s-b)/sqrt(s+k*k*b) mit k=s/b
45//
46// s: total number of events in signal region
47// b: number of background events in signal region
48//
49Double_t MMath::Significance(Double_t s, Double_t b)
50{
51 const Double_t k = b==0 ? 0 : s/b;
52 const Double_t f = s+k*k*b;
53
54 return f==0 ? 0 : (s-b)/TMath::Sqrt(f);
55}
56
57// --------------------------------------------------------------------------
58//
59// Symmetrized significance - this is somehow analog to
60// SignificanceLiMaSigned
61//
62// Returns Significance(s,b) if s>b otherwise -Significance(b, s);
63//
64Double_t MMath::SignificanceSym(Double_t s, Double_t b)
65{
66 return s>b ? Significance(s, b) : -Significance(b, s);
67}
68
69// --------------------------------------------------------------------------
70//
71// calculates the significance according to Li & Ma
72// ApJ 272 (1983) 317, Formula 17
73//
74// s // s: number of on events
75// b // b: number of off events
76// alpha = t_on/t_off; // t: observation time
77//
78// The significance has the same (positive!) value for s>b and b>s.
79//
80// Returns -1 if s<0 or b<0 or alpha<0 or the argument of sqrt<0
81//
82// Here is some eMail written by Daniel Mazin about the meaning of the arguments:
83//
84// > Ok. Here is my understanding:
85// > According to Li&Ma paper (correctly cited in MMath.cc) alpha is the
86// > scaling factor. The mathematics behind the formula 17 (and/or 9) implies
87// > exactly this. If you scale OFF to ON first (using time or using any other
88// > method), then you cannot use formula 17 (9) anymore. You can just try
89// > the formula before scaling (alpha!=1) and after scaling (alpha=1), you
90// > will see the result will be different.
91//
92// > Here are less mathematical arguments:
93//
94// > 1) the better background determination you have (smaller alpha) the more
95// > significant is your excess, thus your analysis is more sensitive. If you
96// > normalize OFF to ON first, you loose this sensitivity.
97//
98// > 2) the normalization OFF to ON has an error, which naturally depends on
99// > the OFF and ON. This error is propagating to the significance of your
100// > excess if you use the Li&Ma formula 17 correctly. But if you normalize
101// > first and use then alpha=1, the error gets lost completely, you loose
102// > somehow the criteria of goodness of the normalization.
103//
104Double_t MMath::SignificanceLiMa(Double_t s, Double_t b, Double_t alpha)
105{
106 const Double_t sum = s+b;
107
108 if (s<0 || b<0 || alpha<=0)
109 return -1;
110
111 const Double_t l = s==0 ? 0 : s*TMath::Log(s/sum*(alpha+1)/alpha);
112 const Double_t m = b==0 ? 0 : b*TMath::Log(b/sum*(alpha+1) );
113
114 return l+m<0 ? -1 : TMath::Sqrt((l+m)*2);
115}
116
117// --------------------------------------------------------------------------
118//
119// Calculates MMath::SignificanceLiMa(s, b, alpha). Returns 0 if the
120// calculation has failed. Otherwise the Li/Ma significance which was
121// calculated. If s<b a negative value is returned.
122//
123Double_t MMath::SignificanceLiMaSigned(Double_t s, Double_t b, Double_t alpha)
124{
125 const Double_t sig = SignificanceLiMa(s, b, alpha);
126 if (sig<=0)
127 return 0;
128
129 return TMath::Sign(sig, s-alpha*b);
130}
131
132// --------------------------------------------------------------------------
133//
134// Return Li/Ma (5) for the error of the excess, under the assumption that
135// the existance of a signal is already known.
136//
137Double_t MMath::SignificanceLiMaExc(Double_t s, Double_t b, Double_t alpha)
138{
139 Double_t Ns = s - alpha*b;
140 Double_t sN = s + alpha*alpha*b;
141
142 return Ns<0 || sN<0 ? 0 : Ns/TMath::Sqrt(sN);
143}
144
145// --------------------------------------------------------------------------
146//
147// Returns: 2/(sigma*sqrt(2))*integral[0,x](exp(-(x-mu)^2/(2*sigma^2)))
148//
149Double_t MMath::GaussProb(Double_t x, Double_t sigma, Double_t mean)
150{
151 static const Double_t sqrt2 = TMath::Sqrt(2.);
152
153 const Double_t rc = TMath::Erf((x-mean)/(sigma*sqrt2));
154
155 if (rc<0)
156 return 0;
157 if (rc>1)
158 return 1;
159
160 return rc;
161}
162
163// --------------------------------------------------------------------------
164//
165// This function reduces the precision to roughly 0.5% of a Float_t by
166// changing its bit-pattern (Be carefull, in rare cases this function must
167// be adapted to different machines!). This is usefull to enforce better
168// compression by eg. gzip.
169//
170void MMath::ReducePrecision(Float_t &val)
171{
172 UInt_t &f = (UInt_t&)val;
173
174 f += 0x00004000;
175 f &= 0xffff8000;
176}
177
178// -------------------------------------------------------------------------
179//
180// Quadratic interpolation
181//
182// calculate the parameters of a parabula such that
183// y(i) = a + b*x(i) + c*x(i)^2
184//
185// If the determinant==0 an empty TVector3 is returned.
186//
187TVector3 MMath::GetParab(const TVector3 &x, const TVector3 &y)
188{
189 Double_t x1 = x(0);
190 Double_t x2 = x(1);
191 Double_t x3 = x(2);
192
193 Double_t y1 = y(0);
194 Double_t y2 = y(1);
195 Double_t y3 = y(2);
196
197 const double det =
198 + x2*x3*x3 + x1*x2*x2 + x3*x1*x1
199 - x2*x1*x1 - x3*x2*x2 - x1*x3*x3;
200
201
202 if (det==0)
203 return TVector3();
204
205 const double det1 = 1.0/det;
206
207 const double ai11 = x2*x3*x3 - x3*x2*x2;
208 const double ai12 = x3*x1*x1 - x1*x3*x3;
209 const double ai13 = x1*x2*x2 - x2*x1*x1;
210
211 const double ai21 = x2*x2 - x3*x3;
212 const double ai22 = x3*x3 - x1*x1;
213 const double ai23 = x1*x1 - x2*x2;
214
215 const double ai31 = x3 - x2;
216 const double ai32 = x1 - x3;
217 const double ai33 = x2 - x1;
218
219 return TVector3((ai11*y1 + ai12*y2 + ai13*y3) * det1,
220 (ai21*y1 + ai22*y2 + ai23*y3) * det1,
221 (ai31*y1 + ai32*y2 + ai33*y3) * det1);
222}
223
224Double_t MMath::InterpolParabLin(const TVector3 &vx, const TVector3 &vy, Double_t x)
225{
226 const TVector3 c = GetParab(vx, vy);
227 return c(0) + c(1)*x + c(2)*x*x;
228}
229
230Double_t MMath::InterpolParabLog(const TVector3 &vx, const TVector3 &vy, Double_t x)
231{
232 const Double_t l0 = TMath::Log10(vx(0));
233 const Double_t l1 = TMath::Log10(vx(1));
234 const Double_t l2 = TMath::Log10(vx(2));
235
236 const TVector3 vx0(l0, l1, l2);
237 return InterpolParabLin(vx0, vy, TMath::Log10(x));
238}
239
240Double_t MMath::InterpolParabCos(const TVector3 &vx, const TVector3 &vy, Double_t x)
241{
242 const Double_t l0 = TMath::Cos(vx(0));
243 const Double_t l1 = TMath::Cos(vx(1));
244 const Double_t l2 = TMath::Cos(vx(2));
245
246 const TVector3 vx0(l0, l1, l2);
247 return InterpolParabLin(vx0, vy, TMath::Cos(x));
248}
249
250// --------------------------------------------------------------------------
251//
252// Analytically calculated result of a least square fit of:
253// y = A*e^(B*x)
254// Equal weights
255//
256// It returns TArrayD(2) = { A, B };
257//
258// see: http://mathworld.wolfram.com/LeastSquaresFittingExponential.html
259//
260TArrayD MMath::LeastSqFitExpW1(Int_t n, Double_t *x, Double_t *y)
261{
262 Double_t sumxsqy = 0;
263 Double_t sumylny = 0;
264 Double_t sumxy = 0;
265 Double_t sumy = 0;
266 Double_t sumxylny = 0;
267 for (int i=0; i<n; i++)
268 {
269 sumylny += y[i]*TMath::Log(y[i]);
270 sumxy += x[i]*y[i];
271 sumxsqy += x[i]*x[i]*y[i];
272 sumxylny += x[i]*y[i]*TMath::Log(y[i]);
273 sumy += y[i];
274 }
275
276 const Double_t dev = sumy*sumxsqy - sumxy*sumxy;
277
278 const Double_t a = (sumxsqy*sumylny - sumxy*sumxylny)/dev;
279 const Double_t b = (sumy*sumxylny - sumxy*sumylny)/dev;
280
281 TArrayD rc(2);
282 rc[0] = TMath::Exp(a);
283 rc[1] = b;
284 return rc;
285}
286
287// --------------------------------------------------------------------------
288//
289// Analytically calculated result of a least square fit of:
290// y = A*e^(B*x)
291// Greater weights to smaller values
292//
293// It returns TArrayD(2) = { A, B };
294//
295// see: http://mathworld.wolfram.com/LeastSquaresFittingExponential.html
296//
297TArrayD MMath::LeastSqFitExp(Int_t n, Double_t *x, Double_t *y)
298{
299 // -------- Greater weights to smaller values ---------
300 Double_t sumlny = 0;
301 Double_t sumxlny = 0;
302 Double_t sumxsq = 0;
303 Double_t sumx = 0;
304 for (int i=0; i<n; i++)
305 {
306 sumlny += TMath::Log(y[i]);
307 sumxlny += x[i]*TMath::Log(y[i]);
308
309 sumxsq += x[i]*x[i];
310 sumx += x[i];
311 }
312
313 const Double_t dev = n*sumxsq-sumx*sumx;
314
315 const Double_t a = (sumlny*sumxsq - sumx*sumxlny)/dev;
316 const Double_t b = (n*sumxlny - sumx*sumlny)/dev;
317
318 TArrayD rc(2);
319 rc[0] = TMath::Exp(a);
320 rc[1] = b;
321 return rc;
322}
323
324// --------------------------------------------------------------------------
325//
326// Analytically calculated result of a least square fit of:
327// y = A+B*ln(x)
328//
329// It returns TArrayD(2) = { A, B };
330//
331// see: http://mathworld.wolfram.com/LeastSquaresFittingLogarithmic.html
332//
333TArrayD MMath::LeastSqFitLog(Int_t n, Double_t *x, Double_t *y)
334{
335 Double_t sumylnx = 0;
336 Double_t sumy = 0;
337 Double_t sumlnx = 0;
338 Double_t sumlnxsq = 0;
339 for (int i=0; i<n; i++)
340 {
341 sumylnx += y[i]*TMath::Log(x[i]);
342 sumy += y[i];
343 sumlnx += TMath::Log(x[i]);
344 sumlnxsq += TMath::Log(x[i])*TMath::Log(x[i]);
345 }
346
347 const Double_t b = (n*sumylnx-sumy*sumlnx)/(n*sumlnxsq-sumlnx*sumlnx);
348 const Double_t a = (sumy-b*sumlnx)/n;
349
350 TArrayD rc(2);
351 rc[0] = a;
352 rc[1] = b;
353 return rc;
354}
355
356// --------------------------------------------------------------------------
357//
358// Analytically calculated result of a least square fit of:
359// y = A*x^B
360//
361// It returns TArrayD(2) = { A, B };
362//
363// see: http://mathworld.wolfram.com/LeastSquaresFittingPowerLaw.html
364//
365TArrayD MMath::LeastSqFitPowerLaw(Int_t n, Double_t *x, Double_t *y)
366{
367 Double_t sumlnxlny = 0;
368 Double_t sumlnx = 0;
369 Double_t sumlny = 0;
370 Double_t sumlnxsq = 0;
371 for (int i=0; i<n; i++)
372 {
373 sumlnxlny += TMath::Log(x[i])*TMath::Log(y[i]);
374 sumlnx += TMath::Log(x[i]);
375 sumlny += TMath::Log(y[i]);
376 sumlnxsq += TMath::Log(x[i])*TMath::Log(x[i]);
377 }
378
379 const Double_t b = (n*sumlnxlny-sumlnx*sumlny)/(n*sumlnxsq-sumlnx*sumlnx);
380 const Double_t a = (sumlny-b*sumlnx)/n;
381
382 TArrayD rc(2);
383 rc[0] = TMath::Exp(a);
384 rc[1] = b;
385 return rc;
386}
Note: See TracBrowser for help on using the repository browser.