| 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 | ClassImp(MMath);
|
|---|
| 33 |
|
|---|
| 34 | using 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 | //
|
|---|
| 44 | Double_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 | //
|
|---|
| 59 | Double_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 | //
|
|---|
| 78 | Double_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 | //
|
|---|
| 100 | Double_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 | //
|
|---|
| 113 | Double_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 | //
|
|---|
| 128 | #include <iostream>
|
|---|
| 129 | TVector3 MMath::GetParab(const TVector3 &x, const TVector3 &y)
|
|---|
| 130 | {
|
|---|
| 131 | Double_t x1 = x(0);
|
|---|
| 132 | Double_t x2 = x(1);
|
|---|
| 133 | Double_t x3 = x(2);
|
|---|
| 134 |
|
|---|
| 135 | Double_t y1 = y(0);
|
|---|
| 136 | Double_t y2 = y(1);
|
|---|
| 137 | Double_t y3 = y(2);
|
|---|
| 138 |
|
|---|
| 139 | const double det =
|
|---|
| 140 | + x2*x3*x3 + x1*x2*x2 + x3*x1*x1
|
|---|
| 141 | - x2*x1*x1 - x3*x2*x2 - x1*x3*x3;
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 | if (det==0)
|
|---|
| 145 | return TVector3();
|
|---|
| 146 |
|
|---|
| 147 | const double det1 = 1.0/det;
|
|---|
| 148 |
|
|---|
| 149 | const double ai11 = x2*x3*x3 - x3*x2*x2;
|
|---|
| 150 | const double ai12 = x3*x1*x1 - x1*x3*x3;
|
|---|
| 151 | const double ai13 = x1*x2*x2 - x2*x1*x1;
|
|---|
| 152 |
|
|---|
| 153 | const double ai21 = x2*x2 - x3*x3;
|
|---|
| 154 | const double ai22 = x3*x3 - x1*x1;
|
|---|
| 155 | const double ai23 = x1*x1 - x2*x2;
|
|---|
| 156 |
|
|---|
| 157 | const double ai31 = x3 - x2;
|
|---|
| 158 | const double ai32 = x1 - x3;
|
|---|
| 159 | const double ai33 = x2 - x1;
|
|---|
| 160 |
|
|---|
| 161 | return TVector3((ai11*y1 + ai12*y2 + ai13*y3) * det1,
|
|---|
| 162 | (ai21*y1 + ai22*y2 + ai23*y3) * det1,
|
|---|
| 163 | (ai31*y1 + ai32*y2 + ai33*y3) * det1);
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | Double_t MMath::InterpolParabLin(const TVector3 &vx, const TVector3 &vy, Double_t x)
|
|---|
| 167 | {
|
|---|
| 168 | const TVector3 c = GetParab(vx, vy);
|
|---|
| 169 | return c(0) + c(1)*x + c(2)*x*x;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | Double_t MMath::InterpolParabLog(const TVector3 &vx, const TVector3 &vy, Double_t x)
|
|---|
| 173 | {
|
|---|
| 174 | const Double_t l0 = TMath::Log10(vx(0));
|
|---|
| 175 | const Double_t l1 = TMath::Log10(vx(1));
|
|---|
| 176 | const Double_t l2 = TMath::Log10(vx(2));
|
|---|
| 177 |
|
|---|
| 178 | const TVector3 vx0(l0, l1, l2);
|
|---|
| 179 | return InterpolParabLin(vx0, vy, TMath::Log10(x));
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | Double_t MMath::InterpolParabCos(const TVector3 &vx, const TVector3 &vy, Double_t x)
|
|---|
| 183 | {
|
|---|
| 184 | const Double_t l0 = TMath::Cos(vx(0));
|
|---|
| 185 | const Double_t l1 = TMath::Cos(vx(1));
|
|---|
| 186 | const Double_t l2 = TMath::Cos(vx(2));
|
|---|
| 187 |
|
|---|
| 188 | const TVector3 vx0(l0, l1, l2);
|
|---|
| 189 | return InterpolParabLin(vx0, vy, TMath::Cos(x));
|
|---|
| 190 | }
|
|---|