1 |
loizides |
1.11 |
// $Id: MathUtils.cc,v 1.10 2009/11/03 10:28:08 sixie Exp $
|
2 |
sixie |
1.1 |
|
3 |
|
|
#include "MitCommon/MathTools/interface/MathUtils.h"
|
4 |
loizides |
1.8 |
#include <TError.h>
|
5 |
|
|
#include <TH1D.h>
|
6 |
|
|
#include <TGraphAsymmErrors.h>
|
7 |
sixie |
1.10 |
#include "PhysicsTools/RooStatsCms/interface/ClopperPearsonBinomialInterval.h"
|
8 |
|
|
#include "PhysicsTools/RooStatsCms/interface/FeldmanCousinsBinomialInterval.h"
|
9 |
|
|
|
10 |
sixie |
1.1 |
|
11 |
loizides |
1.9 |
ClassImp(mithep::MathUtils)
|
12 |
|
|
|
13 |
loizides |
1.3 |
using namespace mithep;
|
14 |
sixie |
1.1 |
|
15 |
loizides |
1.2 |
//--------------------------------------------------------------------------------------------------
|
16 |
loizides |
1.4 |
Double_t MathUtils::AddInQuadrature(Double_t a, Double_t b)
|
17 |
loizides |
1.2 |
{
|
18 |
loizides |
1.5 |
// Add quantities in quadrature.
|
19 |
|
|
|
20 |
loizides |
1.2 |
return(TMath::Sqrt(a*a + b*b));
|
21 |
|
|
}
|
22 |
sixie |
1.1 |
|
23 |
loizides |
1.2 |
//--------------------------------------------------------------------------------------------------
|
24 |
sixie |
1.10 |
void MathUtils::CalcRatio(Double_t n1, Double_t n2, Double_t &r, Double_t &rlow, Double_t &rup, Int_t type = 0)
|
25 |
loizides |
1.8 |
{
|
26 |
loizides |
1.11 |
// Calculate ratio and lower/upper errors from given values using various methods, dependent on type:
|
27 |
|
|
// 0: Bayes
|
28 |
|
|
// 1: Feldman-Cousins
|
29 |
|
|
// 2: Clopper-Pearson
|
30 |
loizides |
1.8 |
|
31 |
|
|
if (n1>n2) {
|
32 |
|
|
Error("CalcRatio", "First value should be smaller than second: %f > %f", n1, n2);
|
33 |
|
|
r = n1/n2;
|
34 |
|
|
rlow = 0;
|
35 |
|
|
rup = 0;
|
36 |
|
|
return;
|
37 |
|
|
}
|
38 |
sixie |
1.10 |
|
39 |
|
|
if (n2 >= 1) {
|
40 |
|
|
if (type == 1) {
|
41 |
loizides |
1.11 |
//compute errors using Feldman-Cousins
|
42 |
sixie |
1.10 |
r = double(n1) / double(n2);
|
43 |
|
|
FeldmanCousinsBinomialInterval fc;
|
44 |
|
|
const double alpha = (1-0.682);
|
45 |
|
|
fc.init(alpha);
|
46 |
|
|
fc.calculate(n1, n2);
|
47 |
|
|
rlow = r - fc.lower();
|
48 |
|
|
rup = fc.upper() - r;
|
49 |
|
|
|
50 |
|
|
} else if (type == 2) {
|
51 |
loizides |
1.11 |
//compute errors using Clopper-Pearson
|
52 |
sixie |
1.10 |
r = double(n1) / double(n2);
|
53 |
|
|
ClopperPearsonBinomialInterval cp;
|
54 |
|
|
const double alpha = (1-0.682);
|
55 |
|
|
cp.init(alpha);
|
56 |
|
|
cp.calculate(n1, n2);
|
57 |
|
|
rlow = r - cp.lower();
|
58 |
|
|
rup = cp.upper() - r;
|
59 |
|
|
|
60 |
|
|
} else {
|
61 |
loizides |
1.11 |
//compute using Bayes
|
62 |
sixie |
1.10 |
TH1D h1("dummy1","",1,1,2);
|
63 |
|
|
h1.SetBinContent(1,n1);
|
64 |
|
|
|
65 |
|
|
TH1D h2("dummy2","",1,1,2);
|
66 |
|
|
h2.SetBinContent(1,n2);
|
67 |
|
|
|
68 |
|
|
TGraphAsymmErrors g;
|
69 |
|
|
g.BayesDivide(&h1,&h2);
|
70 |
|
|
r = g.GetY()[0];
|
71 |
|
|
rup = g.GetErrorYhigh(0);
|
72 |
|
|
rlow = g.GetErrorYlow(0);
|
73 |
|
|
}
|
74 |
|
|
} else {
|
75 |
|
|
r = 0;
|
76 |
|
|
rlow = 0;
|
77 |
|
|
rup = 0;
|
78 |
|
|
}
|
79 |
|
|
}
|
80 |
loizides |
1.8 |
|
81 |
|
|
|
82 |
|
|
//--------------------------------------------------------------------------------------------------
|
83 |
loizides |
1.4 |
Double_t MathUtils::DeltaPhi(Double_t phi1, Double_t phi2)
|
84 |
sixie |
1.1 |
{
|
85 |
loizides |
1.5 |
// Compute DeltaPhi between two given angles. Results is in [-pi/2,pi/2].
|
86 |
|
|
|
87 |
loizides |
1.4 |
Double_t dphi = TMath::Abs(phi1-phi2);
|
88 |
|
|
while (dphi>TMath::Pi())
|
89 |
|
|
dphi = TMath::Abs(dphi - TMath::TwoPi());
|
90 |
|
|
return(dphi);
|
91 |
sixie |
1.1 |
}
|
92 |
|
|
|
93 |
loizides |
1.2 |
//--------------------------------------------------------------------------------------------------
|
94 |
loizides |
1.4 |
Double_t MathUtils::DeltaR(Double_t phi1, Double_t eta1, Double_t phi2, Double_t eta2)
|
95 |
|
|
{
|
96 |
loizides |
1.5 |
// Compute DeltaR between two given points in the eta/phi plane.
|
97 |
|
|
|
98 |
loizides |
1.7 |
Double_t dR = TMath::Sqrt(DeltaR2(phi1,eta1,phi2,eta2));
|
99 |
|
|
return(dR);
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
//--------------------------------------------------------------------------------------------------
|
103 |
|
|
Double_t MathUtils::DeltaR2(Double_t phi1, Double_t eta1, Double_t phi2, Double_t eta2)
|
104 |
|
|
{
|
105 |
|
|
// Compute DeltaR between two given points in the eta/phi plane.
|
106 |
|
|
|
107 |
loizides |
1.4 |
Double_t dphi = DeltaPhi(phi1, phi2);
|
108 |
|
|
Double_t deta = eta1-eta2;
|
109 |
loizides |
1.7 |
Double_t dR = dphi*dphi + deta*deta;
|
110 |
sixie |
1.1 |
return(dR);
|
111 |
|
|
}
|
112 |
|
|
|
113 |
loizides |
1.2 |
//--------------------------------------------------------------------------------------------------
|
114 |
loizides |
1.4 |
Double_t MathUtils::Eta2Theta(Double_t eta)
|
115 |
loizides |
1.2 |
{
|
116 |
loizides |
1.5 |
// Compute theta from given eta value.
|
117 |
|
|
|
118 |
loizides |
1.2 |
return 2.*TMath::ATan(exp(-eta));
|
119 |
sixie |
1.1 |
}
|
120 |
|
|
|
121 |
loizides |
1.2 |
//--------------------------------------------------------------------------------------------------
|
122 |
loizides |
1.4 |
Double_t MathUtils::Theta2Eta(Double_t theta)
|
123 |
loizides |
1.2 |
{
|
124 |
loizides |
1.5 |
// Compute eta from given theta value.
|
125 |
|
|
|
126 |
loizides |
1.2 |
return -TMath::Log(TMath::Tan(theta/2.));
|
127 |
sixie |
1.1 |
}
|