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

Last change on this file since 4966 was 4966, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 5.0 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
32ClassImp(MMath);
33
34using namespace std;
35
36// --------------------------------------------------------------------------
37//
38// Calculate Significance as
39// significance = (s-b)/sqrt(s+k*k*b) mit k=s/b
40//
41// s: total number of events in signal region
42// b: number of background events in signal region
43//
44Double_t MMath::Significance(Double_t s, Double_t b)
45{
46 const Double_t k = b==0 ? 0 : s/b;
47 const Double_t f = s+k*k*b;
48
49 return f==0 ? 0 : (s-b)/Sqrt(f);
50}
51
52// --------------------------------------------------------------------------
53//
54// Symmetrized significance - this is somehow analog to
55// SignificanceLiMaSigned
56//
57// Returns Significance(s,b) if s>b otherwise -Significance(b, s);
58//
59Double_t MMath::SignificanceSym(Double_t s, Double_t b)
60{
61 return s>b ? Significance(s, b) : -Significance(b, s);
62}
63
64// --------------------------------------------------------------------------
65//
66// calculates the significance according to Li & Ma
67// ApJ 272 (1983) 317, Formula 17
68//
69// s // s: number of on events
70// b // b: number of off events
71// alpha = t_on/t_off; // t: observation time
72//
73// The significance has the same (positive!) value for s>b and b>s.
74//
75// Returns -1 if sum<0 or alpha<0 or the argument of sqrt<0
76// Returns 0 if s+b==0
77//
78Double_t MMath::SignificanceLiMa(Double_t s, Double_t b, Double_t alpha)
79{
80 const Double_t sum = s+b;
81
82 if (sum==0)
83 return 0;
84
85 if (sum<0 || alpha<=0)
86 return -1;
87
88 const Double_t l = s*Log(s/sum*(alpha+1)/alpha);
89 const Double_t m = b*Log(b/sum*(alpha+1) );
90
91 return l+m<0 ? -1 : Sqrt((l+m)*2);
92}
93
94// --------------------------------------------------------------------------
95//
96// Calculates MMath::SignificanceLiMa(s, b, alpha). Returns 0 if the
97// calculation has failed. Otherwise the Li/Ma significance which was
98// calculated. If s<b a negative value is returned.
99//
100Double_t MMath::SignificanceLiMaSigned(Double_t s, Double_t b, Double_t alpha)
101{
102 const Double_t sig = SignificanceLiMa(s, b, alpha);
103 if (sig<=0)
104 return 0;
105
106 return Sign(sig, s-alpha*b);
107}
108
109// --------------------------------------------------------------------------
110//
111// Returns: 2/(sigma*sqrt(2))*integral[0,x](exp(-(x-mu)^2/(2*sigma^2)))
112//
113Double_t MMath::GaussProb(Double_t x, Double_t sigma, Double_t mean)
114{
115 static const Double_t sqrt2 = Sqrt(2.);
116 return Erf((x-mean)/(sigma*sqrt2));
117}
118
119// -------------------------------------------------------------------------
120//
121// Quadratic interpolation
122//
123// calculate the parameters of a parabula such that
124// y(i) = a + b*x(i) + c*x(i)^2
125//
126// If the determinant==0 an empty TVector3 is returned.
127//
128TVector3 MMath::GetParab(const TVector3 &x, const TVector3 &y)
129{
130 const TVector3 sq(x(0)*x(0), x(1)*x(1), x(2)*x(2));
131
132 const TVector3 ai2 = sq.Cross(sq);
133
134 const Double_t det = x.Dot(ai2);
135 if (det==0)
136 return TVector3();
137
138 const TVector3 ai1 = x.Cross(sq);
139 const TVector3 ai3 = x.Cross(x);
140
141 TVector3 res(y.Dot(ai1), y.Dot(ai2), y.Dot(ai3));
142 res *= 1./det;
143
144 return res;
145}
146
147Double_t MMath::InterpolParabLin(const TVector3 &vx, const TVector3 &vy, Double_t x)
148{
149 const TVector3 c = GetParab(vx, vy);
150 return c(0) + c(1)*x + c(2)*x*x;
151}
152
153Double_t MMath::InterpolParabLog(const TVector3 &vx, const TVector3 &vy, Double_t x)
154{
155 const Double_t l0 = TMath::Log10(vx(0));
156 const Double_t l1 = TMath::Log10(vx(1));
157 const Double_t l2 = TMath::Log10(vx(2));
158
159 const TVector3 vx0(l0, l1, l2);
160 return pow(10, InterpolParabLin(vx0, vy, TMath::Log10(x)));
161}
162
163Double_t MMath::InterpolParabCos(const TVector3 &vx, const TVector3 &vy, Double_t x)
164{
165 const Double_t l0 = TMath::Cos(vx(0));
166 const Double_t l1 = TMath::Cos(vx(1));
167 const Double_t l2 = TMath::Cos(vx(2));
168
169 const TVector3 vx0(l0, l1, l2);
170 return TMath::ACos(InterpolParabLin(vx0, vy, TMath::Cos(x)));
171}
Note: See TracBrowser for help on using the repository browser.