ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitCommon/MathTools/src/MathUtils.cc
Revision: 1.10
Committed: Tue Nov 3 10:28:08 2009 UTC (15 years, 6 months ago) by sixie
Content type: text/plain
Branch: MAIN
Changes since 1.9: +47 -13 lines
Log Message:
Add Clopper Pearson interval, and Feldman Cousins interval calculations to the CalcRatio function

File Contents

# User Rev Content
1 sixie 1.10 // $Id: MathUtils.cc,v 1.9 2009/07/20 04:55:44 loizides 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     // Calculate ratio and lower/upper errors from given values using Bayes.
27    
28     if (n1>n2) {
29     Error("CalcRatio", "First value should be smaller than second: %f > %f", n1, n2);
30     r = n1/n2;
31     rlow = 0;
32     rup = 0;
33     return;
34     }
35    
36 sixie 1.10
37     if (n2 >= 1) {
38     if (type == 1) {
39     r = double(n1) / double(n2);
40    
41     //compute errors using Feldman-Cousins
42     FeldmanCousinsBinomialInterval fc;
43     const double alpha = (1-0.682);
44     fc.init(alpha);
45     fc.calculate(n1, n2);
46     rlow = r - fc.lower();
47     rup = fc.upper() - r;
48    
49     } else if (type == 2) {
50     r = double(n1) / double(n2);
51    
52     ClopperPearsonBinomialInterval cp;
53     const double alpha = (1-0.682);
54     cp.init(alpha);
55     cp.calculate(n1, n2);
56     rlow = r - cp.lower();
57     rup = cp.upper() - r;
58    
59     } else {
60     TH1D h1("dummy1","",1,1,2);
61     h1.SetBinContent(1,n1);
62    
63     TH1D h2("dummy2","",1,1,2);
64     h2.SetBinContent(1,n2);
65    
66     TGraphAsymmErrors g;
67     g.BayesDivide(&h1,&h2);
68     r = g.GetY()[0];
69     rup = g.GetErrorYhigh(0);
70     rlow = g.GetErrorYlow(0);
71     }
72     } else {
73     r = 0;
74     rlow = 0;
75     rup = 0;
76     }
77     }
78 loizides 1.8
79    
80     //--------------------------------------------------------------------------------------------------
81 loizides 1.4 Double_t MathUtils::DeltaPhi(Double_t phi1, Double_t phi2)
82 sixie 1.1 {
83 loizides 1.5 // Compute DeltaPhi between two given angles. Results is in [-pi/2,pi/2].
84    
85 loizides 1.4 Double_t dphi = TMath::Abs(phi1-phi2);
86     while (dphi>TMath::Pi())
87     dphi = TMath::Abs(dphi - TMath::TwoPi());
88     return(dphi);
89 sixie 1.1 }
90    
91 loizides 1.2 //--------------------------------------------------------------------------------------------------
92 loizides 1.4 Double_t MathUtils::DeltaR(Double_t phi1, Double_t eta1, Double_t phi2, Double_t eta2)
93     {
94 loizides 1.5 // Compute DeltaR between two given points in the eta/phi plane.
95    
96 loizides 1.7 Double_t dR = TMath::Sqrt(DeltaR2(phi1,eta1,phi2,eta2));
97     return(dR);
98     }
99    
100     //--------------------------------------------------------------------------------------------------
101     Double_t MathUtils::DeltaR2(Double_t phi1, Double_t eta1, Double_t phi2, Double_t eta2)
102     {
103     // Compute DeltaR between two given points in the eta/phi plane.
104    
105 loizides 1.4 Double_t dphi = DeltaPhi(phi1, phi2);
106     Double_t deta = eta1-eta2;
107 loizides 1.7 Double_t dR = dphi*dphi + deta*deta;
108 sixie 1.1 return(dR);
109     }
110    
111 loizides 1.2 //--------------------------------------------------------------------------------------------------
112 loizides 1.4 Double_t MathUtils::Eta2Theta(Double_t eta)
113 loizides 1.2 {
114 loizides 1.5 // Compute theta from given eta value.
115    
116 loizides 1.2 return 2.*TMath::ATan(exp(-eta));
117 sixie 1.1 }
118    
119 loizides 1.2 //--------------------------------------------------------------------------------------------------
120 loizides 1.4 Double_t MathUtils::Theta2Eta(Double_t theta)
121 loizides 1.2 {
122 loizides 1.5 // Compute eta from given theta value.
123    
124 loizides 1.2 return -TMath::Log(TMath::Tan(theta/2.));
125 sixie 1.1 }