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

Last change on this file since 7394 was 7386, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 7.7 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// Return Li/Ma (5) for the error of the excess, under the assumption that
136// the existance of a signal is already known.
137//
138Double_t MMath::SignificanceLiMaExc(Double_t s, Double_t b, Double_t alpha)
139{
140 Double_t Ns = s - alpha*b;
141 Double_t sN = s + alpha*alpha*b;
142
143 return Ns<0 || sN<0 ? 0 : Ns/TMath::Sqrt(sN);
144}
145
146// --------------------------------------------------------------------------
147//
148// Returns: 2/(sigma*sqrt(2))*integral[0,x](exp(-(x-mu)^2/(2*sigma^2)))
149//
150Double_t MMath::GaussProb(Double_t x, Double_t sigma, Double_t mean)
151{
152 static const Double_t sqrt2 = TMath::Sqrt(2.);
153
154 const Double_t rc = TMath::Erf((x-mean)/(sigma*sqrt2));
155
156 if (rc<0)
157 return 0;
158 if (rc>1)
159 return 1;
160
161 return rc;
162}
163
164// --------------------------------------------------------------------------
165//
166// This function reduces the precision to roughly 0.5% of a Float_t by
167// changing its bit-pattern (Be carefull, in rare cases this function must
168// be adapted to different machines!). This is usefull to enforce better
169// compression by eg. gzip.
170//
171void MMath::ReducePrecision(Float_t &val)
172{
173 UInt_t &f = (UInt_t&)val;
174
175 f += 0x00004000;
176 f &= 0xffff8000;
177}
178
179// -------------------------------------------------------------------------
180//
181// Quadratic interpolation
182//
183// calculate the parameters of a parabula such that
184// y(i) = a + b*x(i) + c*x(i)^2
185//
186// If the determinant==0 an empty TVector3 is returned.
187//
188TVector3 MMath::GetParab(const TVector3 &x, const TVector3 &y)
189{
190 Double_t x1 = x(0);
191 Double_t x2 = x(1);
192 Double_t x3 = x(2);
193
194 Double_t y1 = y(0);
195 Double_t y2 = y(1);
196 Double_t y3 = y(2);
197
198 const double det =
199 + x2*x3*x3 + x1*x2*x2 + x3*x1*x1
200 - x2*x1*x1 - x3*x2*x2 - x1*x3*x3;
201
202
203 if (det==0)
204 return TVector3();
205
206 const double det1 = 1.0/det;
207
208 const double ai11 = x2*x3*x3 - x3*x2*x2;
209 const double ai12 = x3*x1*x1 - x1*x3*x3;
210 const double ai13 = x1*x2*x2 - x2*x1*x1;
211
212 const double ai21 = x2*x2 - x3*x3;
213 const double ai22 = x3*x3 - x1*x1;
214 const double ai23 = x1*x1 - x2*x2;
215
216 const double ai31 = x3 - x2;
217 const double ai32 = x1 - x3;
218 const double ai33 = x2 - x1;
219
220 return TVector3((ai11*y1 + ai12*y2 + ai13*y3) * det1,
221 (ai21*y1 + ai22*y2 + ai23*y3) * det1,
222 (ai31*y1 + ai32*y2 + ai33*y3) * det1);
223}
224
225Double_t MMath::InterpolParabLin(const TVector3 &vx, const TVector3 &vy, Double_t x)
226{
227 const TVector3 c = GetParab(vx, vy);
228 return c(0) + c(1)*x + c(2)*x*x;
229}
230
231Double_t MMath::InterpolParabLog(const TVector3 &vx, const TVector3 &vy, Double_t x)
232{
233 const Double_t l0 = TMath::Log10(vx(0));
234 const Double_t l1 = TMath::Log10(vx(1));
235 const Double_t l2 = TMath::Log10(vx(2));
236
237 const TVector3 vx0(l0, l1, l2);
238 return InterpolParabLin(vx0, vy, TMath::Log10(x));
239}
240
241Double_t MMath::InterpolParabCos(const TVector3 &vx, const TVector3 &vy, Double_t x)
242{
243 const Double_t l0 = TMath::Cos(vx(0));
244 const Double_t l1 = TMath::Cos(vx(1));
245 const Double_t l2 = TMath::Cos(vx(2));
246
247 const TVector3 vx0(l0, l1, l2);
248 return InterpolParabLin(vx0, vy, TMath::Cos(x));
249}
Note: See TracBrowser for help on using the repository browser.