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

Last change on this file since 7353 was 7181, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 7.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// --------------------------------------------------------------------------
39//
40// Calculate Significance as
41// significance = (s-b)/sqrt(s+k*k*b) mit k=s/b
42//
43// s: total number of events in signal region
44// b: number of background events in signal region
45//
46Double_t MMath::Significance(Double_t s, Double_t b)
47{
48 const Double_t k = b==0 ? 0 : s/b;
49 const Double_t f = s+k*k*b;
50
51 return f==0 ? 0 : (s-b)/TMath::Sqrt(f);
52}
53
54// --------------------------------------------------------------------------
55//
56// Symmetrized significance - this is somehow analog to
57// SignificanceLiMaSigned
58//
59// Returns Significance(s,b) if s>b otherwise -Significance(b, s);
60//
61Double_t MMath::SignificanceSym(Double_t s, Double_t b)
62{
63 return s>b ? Significance(s, b) : -Significance(b, s);
64}
65
66// --------------------------------------------------------------------------
67//
68// calculates the significance according to Li & Ma
69// ApJ 272 (1983) 317, Formula 17
70//
71// s // s: number of on events
72// b // b: number of off events
73// alpha = t_on/t_off; // t: observation time
74//
75// The significance has the same (positive!) value for s>b and b>s.
76//
77// Returns -1 if sum<0 or alpha<0 or the argument of sqrt<0
78// Returns 0 if s+b==0, s==0 or b==0
79//
80// Here is some eMail written by Daniel Mazin about the meaning of the arguments:
81//
82// > Ok. Here is my understanding:
83// > According to Li&Ma paper (correctly cited in MMath.cc) alpha is the
84// > scaling factor. The mathematics behind the formula 17 (and/or 9) implies
85// > exactly this. If you scale OFF to ON first (using time or using any other
86// > method), then you cannot use formula 17 (9) anymore. You can just try
87// > the formula before scaling (alpha!=1) and after scaling (alpha=1), you
88// > will see the result will be different.
89//
90// > Here are less mathematical arguments:
91//
92// > 1) the better background determination you have (smaller alpha) the more
93// > significant is your excess, thus your analysis is more sensitive. If you
94// > normalize OFF to ON first, you loose this sensitivity.
95//
96// > 2) the normalization OFF to ON has an error, which naturally depends on
97// > the OFF and ON. This error is propagating to the significance of your
98// > excess if you use the Li&Ma formula 17 correctly. But if you normalize
99// > first and use then alpha=1, the error gets lost completely, you loose
100// > somehow the criteria of goodness of the normalization.
101//
102Double_t MMath::SignificanceLiMa(Double_t s, Double_t b, Double_t alpha)
103{
104 const Double_t sum = s+b;
105
106 if (s==0 || b==0 || sum==0)
107 return 0;
108
109 if (sum<0 || alpha<=0)
110 return -1;
111
112 const Double_t l = s*TMath::Log(s/sum*(alpha+1)/alpha);
113 const Double_t m = b*TMath::Log(b/sum*(alpha+1) );
114
115 return l+m<0 ? -1 : TMath::Sqrt((l+m)*2);
116}
117
118// --------------------------------------------------------------------------
119//
120// Calculates MMath::SignificanceLiMa(s, b, alpha). Returns 0 if the
121// calculation has failed. Otherwise the Li/Ma significance which was
122// calculated. If s<b a negative value is returned.
123//
124Double_t MMath::SignificanceLiMaSigned(Double_t s, Double_t b, Double_t alpha)
125{
126 const Double_t sig = SignificanceLiMa(s, b, alpha);
127 if (sig<=0)
128 return 0;
129
130 return TMath::Sign(sig, s-alpha*b);
131}
132
133// --------------------------------------------------------------------------
134//
135// Returns: 2/(sigma*sqrt(2))*integral[0,x](exp(-(x-mu)^2/(2*sigma^2)))
136//
137Double_t MMath::GaussProb(Double_t x, Double_t sigma, Double_t mean)
138{
139 static const Double_t sqrt2 = TMath::Sqrt(2.);
140
141 const Double_t rc = TMath::Erf((x-mean)/(sigma*sqrt2));
142
143 if (rc<0)
144 return 0;
145 if (rc>1)
146 return 1;
147
148 return rc;
149}
150
151// --------------------------------------------------------------------------
152//
153// This function reduces the precision to roughly 0.5% of a Float_t by
154// changing its bit-pattern (Be carefull, in rare cases this function must
155// be adapted to different machines!). This is usefull to enforce better
156// compression by eg. gzip.
157//
158void MMath::ReducePrecision(Float_t &val)
159{
160 UInt_t &f = (UInt_t&)val;
161
162 f += 0x00004000;
163 f &= 0xffff8000;
164}
165
166// -------------------------------------------------------------------------
167//
168// Quadratic interpolation
169//
170// calculate the parameters of a parabula such that
171// y(i) = a + b*x(i) + c*x(i)^2
172//
173// If the determinant==0 an empty TVector3 is returned.
174//
175TVector3 MMath::GetParab(const TVector3 &x, const TVector3 &y)
176{
177 Double_t x1 = x(0);
178 Double_t x2 = x(1);
179 Double_t x3 = x(2);
180
181 Double_t y1 = y(0);
182 Double_t y2 = y(1);
183 Double_t y3 = y(2);
184
185 const double det =
186 + x2*x3*x3 + x1*x2*x2 + x3*x1*x1
187 - x2*x1*x1 - x3*x2*x2 - x1*x3*x3;
188
189
190 if (det==0)
191 return TVector3();
192
193 const double det1 = 1.0/det;
194
195 const double ai11 = x2*x3*x3 - x3*x2*x2;
196 const double ai12 = x3*x1*x1 - x1*x3*x3;
197 const double ai13 = x1*x2*x2 - x2*x1*x1;
198
199 const double ai21 = x2*x2 - x3*x3;
200 const double ai22 = x3*x3 - x1*x1;
201 const double ai23 = x1*x1 - x2*x2;
202
203 const double ai31 = x3 - x2;
204 const double ai32 = x1 - x3;
205 const double ai33 = x2 - x1;
206
207 return TVector3((ai11*y1 + ai12*y2 + ai13*y3) * det1,
208 (ai21*y1 + ai22*y2 + ai23*y3) * det1,
209 (ai31*y1 + ai32*y2 + ai33*y3) * det1);
210}
211
212Double_t MMath::InterpolParabLin(const TVector3 &vx, const TVector3 &vy, Double_t x)
213{
214 const TVector3 c = GetParab(vx, vy);
215 return c(0) + c(1)*x + c(2)*x*x;
216}
217
218Double_t MMath::InterpolParabLog(const TVector3 &vx, const TVector3 &vy, Double_t x)
219{
220 const Double_t l0 = TMath::Log10(vx(0));
221 const Double_t l1 = TMath::Log10(vx(1));
222 const Double_t l2 = TMath::Log10(vx(2));
223
224 const TVector3 vx0(l0, l1, l2);
225 return InterpolParabLin(vx0, vy, TMath::Log10(x));
226}
227
228Double_t MMath::InterpolParabCos(const TVector3 &vx, const TVector3 &vy, Double_t x)
229{
230 const Double_t l0 = TMath::Cos(vx(0));
231 const Double_t l1 = TMath::Cos(vx(1));
232 const Double_t l2 = TMath::Cos(vx(2));
233
234 const TVector3 vx0(l0, l1, l2);
235 return InterpolParabLin(vx0, vy, TMath::Cos(x));
236}
Note: See TracBrowser for help on using the repository browser.