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

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