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

Last change on this file since 5883 was 5883, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 5.5 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 or s==0
73//
74Double_t MMath::SignificanceLiMa(Double_t s, Double_t b, Double_t alpha)
75{
76 const Double_t sum = s+b;
77
78 if (s==0 || sum==0)
79 return 0;
80
81 if (sum<0 || alpha<=0)
82 return -1;
83
84 const Double_t l = s*TMath::Log(s/sum*(alpha+1)/alpha);
85 const Double_t m = b*TMath::Log(b/sum*(alpha+1) );
86
87 return l+m<0 ? -1 : TMath::Sqrt((l+m)*2);
88}
89
90// --------------------------------------------------------------------------
91//
92// Calculates MMath::SignificanceLiMa(s, b, alpha). Returns 0 if the
93// calculation has failed. Otherwise the Li/Ma significance which was
94// calculated. If s<b a negative value is returned.
95//
96Double_t MMath::SignificanceLiMaSigned(Double_t s, Double_t b, Double_t alpha)
97{
98 const Double_t sig = SignificanceLiMa(s, b, alpha);
99 if (sig<=0)
100 return 0;
101
102 return TMath::Sign(sig, s-alpha*b);
103}
104
105// --------------------------------------------------------------------------
106//
107// Returns: 2/(sigma*sqrt(2))*integral[0,x](exp(-(x-mu)^2/(2*sigma^2)))
108//
109Double_t MMath::GaussProb(Double_t x, Double_t sigma, Double_t mean)
110{
111 static const Double_t sqrt2 = TMath::Sqrt(2.);
112 return TMath::Erf((x-mean)/(sigma*sqrt2));
113}
114
115// -------------------------------------------------------------------------
116//
117// Quadratic interpolation
118//
119// calculate the parameters of a parabula such that
120// y(i) = a + b*x(i) + c*x(i)^2
121//
122// If the determinant==0 an empty TVector3 is returned.
123//
124TVector3 MMath::GetParab(const TVector3 &x, const TVector3 &y)
125{
126 Double_t x1 = x(0);
127 Double_t x2 = x(1);
128 Double_t x3 = x(2);
129
130 Double_t y1 = y(0);
131 Double_t y2 = y(1);
132 Double_t y3 = y(2);
133
134 const double det =
135 + x2*x3*x3 + x1*x2*x2 + x3*x1*x1
136 - x2*x1*x1 - x3*x2*x2 - x1*x3*x3;
137
138
139 if (det==0)
140 return TVector3();
141
142 const double det1 = 1.0/det;
143
144 const double ai11 = x2*x3*x3 - x3*x2*x2;
145 const double ai12 = x3*x1*x1 - x1*x3*x3;
146 const double ai13 = x1*x2*x2 - x2*x1*x1;
147
148 const double ai21 = x2*x2 - x3*x3;
149 const double ai22 = x3*x3 - x1*x1;
150 const double ai23 = x1*x1 - x2*x2;
151
152 const double ai31 = x3 - x2;
153 const double ai32 = x1 - x3;
154 const double ai33 = x2 - x1;
155
156 return TVector3((ai11*y1 + ai12*y2 + ai13*y3) * det1,
157 (ai21*y1 + ai22*y2 + ai23*y3) * det1,
158 (ai31*y1 + ai32*y2 + ai33*y3) * det1);
159}
160
161Double_t MMath::InterpolParabLin(const TVector3 &vx, const TVector3 &vy, Double_t x)
162{
163 const TVector3 c = GetParab(vx, vy);
164 return c(0) + c(1)*x + c(2)*x*x;
165}
166
167Double_t MMath::InterpolParabLog(const TVector3 &vx, const TVector3 &vy, Double_t x)
168{
169 const Double_t l0 = TMath::Log10(vx(0));
170 const Double_t l1 = TMath::Log10(vx(1));
171 const Double_t l2 = TMath::Log10(vx(2));
172
173 const TVector3 vx0(l0, l1, l2);
174 return InterpolParabLin(vx0, vy, TMath::Log10(x));
175}
176
177Double_t MMath::InterpolParabCos(const TVector3 &vx, const TVector3 &vy, Double_t x)
178{
179 const Double_t l0 = TMath::Cos(vx(0));
180 const Double_t l1 = TMath::Cos(vx(1));
181 const Double_t l2 = TMath::Cos(vx(2));
182
183 const TVector3 vx0(l0, l1, l2);
184 return InterpolParabLin(vx0, vy, TMath::Cos(x));
185}
Note: See TracBrowser for help on using the repository browser.