ROOT  6.06/08
Reference Guide
HybridResult.cxx
Go to the documentation of this file.
1 // @(#)root/roostats:$Id$
2 
3 /*************************************************************************
4  * Project: RooStats *
5  * Package: RooFit/RooStats *
6  * Authors: *
7  * Kyle Cranmer, Lorenzo Moneta, Gregory Schott, Wouter Verkerke *
8  *************************************************************************
9  * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers. *
10  * All rights reserved. *
11  * *
12  * For the licensing terms see $ROOTSYS/LICENSE. *
13  * For the list of contributors see $ROOTSYS/README/CREDITS. *
14  *************************************************************************/
15 
16 ////////////////////////////////////////////////////////////////////////////////
17 
18 
19 
20 #include "RooDataHist.h"
21 #include "RooDataSet.h"
22 #include "RooGlobalFunc.h" // for RooFit::Extended()
23 #include "RooNLLVar.h"
24 #include "RooRealVar.h"
25 #include "RooAbsData.h"
26 
27 #include "RooStats/HybridResult.h"
28 #include "RooStats/HybridPlot.h"
29 
30 
31 /// ClassImp for building the THtml documentation of the class
32 using namespace std;
33 
35 
36 using namespace RooStats;
37 
38 ///////////////////////////////////////////////////////////////////////////
39 
40 HybridResult::HybridResult( const char *name) :
42  fTestStat_data(-999.),
43  fComputationsNulDoneFlag(false),
44  fComputationsAltDoneFlag(false),
45  fSumLargerValues(false)
46 {
47  // HybridResult default constructor (with name )
48 }
49 
50 ///////////////////////////////////////////////////////////////////////////
51 
52 HybridResult::HybridResult( const char *name,
53  const std::vector<double>& testStat_sb_vals,
54  const std::vector<double>& testStat_b_vals,
55  bool sumLargerValues ) :
56  HypoTestResult(name,0,0),
57  fTestStat_data(-999.),
58  fComputationsNulDoneFlag(false),
59  fComputationsAltDoneFlag(false),
60  fSumLargerValues(sumLargerValues)
61 {
62  // HybridResult constructor (with name, title and vectors of S+B and B values)
63 
64  int vector_size_sb = testStat_sb_vals.size();
65  assert(vector_size_sb>0);
66 
67  int vector_size_b = testStat_b_vals.size();
68  assert(vector_size_b>0);
69 
70  fTestStat_sb.reserve(vector_size_sb);
71  for (int i=0;i<vector_size_sb;++i)
72  fTestStat_sb.push_back(testStat_sb_vals[i]);
73 
74  fTestStat_b.reserve(vector_size_b);
75  for (int i=0;i<vector_size_b;++i)
76  fTestStat_b.push_back(testStat_b_vals[i]);
77 }
78 
79 
80 ///////////////////////////////////////////////////////////////////////////
81 
83 {
84  // HybridResult destructor
85 
86  fTestStat_sb.clear();
87  fTestStat_b.clear();
88 }
89 
90 ///////////////////////////////////////////////////////////////////////////
91 
92 void HybridResult::SetDataTestStatistics(double testStat_data_val)
93 {
94  // set the value of the test statistics on data
95 
98  fTestStat_data = testStat_data_val;
99  return;
100 }
101 
102 ///////////////////////////////////////////////////////////////////////////
103 /// Returns \f$1 - CL_{b}\f$ : the B p-value
105 {
106  if (fComputationsNulDoneFlag==false) {
107  int nToys = fTestStat_b.size();
108  if (nToys==0) {
109  std::cout << "Error: no toy data present. Returning -1.\n";
110  return -1;
111  }
112 
113  double larger_than_measured=0;
114  if (fSumLargerValues) {
115  for (int iToy=0;iToy<nToys;++iToy)
116  if ( fTestStat_b[iToy] >= fTestStat_data ) ++larger_than_measured;
117  } else {
118  for (int iToy=0;iToy<nToys;++iToy)
119  if ( fTestStat_b[iToy] <= fTestStat_data ) ++larger_than_measured;
120  }
121 
122  if (larger_than_measured==0) std::cout << "Warning: CLb = 0 ... maybe more toys are needed!\n";
123 
125  fNullPValue = 1-larger_than_measured/nToys;
126  }
127 
128  return fNullPValue;
129 }
130 
131 ///////////////////////////////////////////////////////////////////////////
132 /// Returns \f$CL_{s+b}\f$ : the S+B p-value
134 {
135  if (fComputationsAltDoneFlag==false) {
136  int nToys = fTestStat_b.size();
137  if (nToys==0) {
138  std::cout << "Error: no toy data present. Returning -1.\n";
139  return -1;
140  }
141 
142  double larger_than_measured=0;
143  if (fSumLargerValues) {
144  for (int iToy=0;iToy<nToys;++iToy)
145  if ( fTestStat_sb[iToy] >= fTestStat_data ) ++larger_than_measured;
146  } else {
147  for (int iToy=0;iToy<nToys;++iToy)
148  if ( fTestStat_sb[iToy] <= fTestStat_data ) ++larger_than_measured;
149  }
150 
151  if (larger_than_measured==0) std::cout << "Warning: CLsb = 0 ... maybe more toys are needed!\n";
152 
154  fAlternatePValue = larger_than_measured/nToys;
155  }
156 
157  return fAlternatePValue;
158 }
159 
160 ///////////////////////////////////////////////////////////////////////////
161 /// Returns an estimate of the error on \f$CL_{b}\f$ assuming a binomial
162 /// error on \f$CL_{b}\f$:
163 /// \f[
164 /// \sigma_{CL_{b}} = \sqrt{CL_{b} \left( 1 - CL_{b} \right) / n_{toys}}
165 /// \f]
167 {
168  unsigned const int n = fTestStat_b.size();
169  return TMath::Sqrt(CLb() * (1. - CLb()) / n);
170 }
171 
172 ///////////////////////////////////////////////////////////////////////////
173 /// Returns an estimate of the error on \f$CL_{s+b}\f$ assuming a binomial
174 /// error on \f$CL_{s+b}\f$:
175 /// \f[
176 /// \sigma_{CL_{s+b}} = \sqrt{CL_{s+b} \left( 1 - CL_{s+b} \right) / n_{toys}}
177 /// \f]
179 {
180  unsigned const int n = fTestStat_sb.size();
181  return TMath::Sqrt(CLsplusb() * (1. - CLsplusb()) / n);
182 }
183 
184 ///////////////////////////////////////////////////////////////////////////
185 /// Returns an estimate of the error on \f$CL_{s}\f$ through combination
186 /// of the errors on \f$CL_{b}\f$ and \f$CL_{s+b}\f$:
187 /// \f[
188 /// \sigma_{CL_s} = CL_s \sqrt{\left( \frac{\sigma_{CL_{s+b}}}{CL_{s+b}} \right)^2 + \left( \frac{\sigma_{CL_{b}}}{CL_{b}} \right)^2}
189 /// \f]
191 {
192  unsigned const int n_b = fTestStat_b.size();
193  unsigned const int n_sb = fTestStat_sb.size();
194 
195  if (CLb() == 0 || CLsplusb() == 0)
196  return 0;
197 
198  double cl_b_err = (1. - CLb()) / (n_b * CLb());
199  double cl_sb_err = (1. - CLsplusb()) / (n_sb * CLsplusb());
200 
201  return CLs() * TMath::Sqrt(cl_b_err + cl_sb_err);
202 }
203 
204 ///////////////////////////////////////////////////////////////////////////
205 
207 {
208  // add additional toy-MC experiments to the current results
209  // use the data test statistics of the added object if none is already present (otherwise, ignore the new one)
210 
211  int other_size_sb = other->GetTestStat_sb().size();
212  for (int i=0;i<other_size_sb;++i)
213  fTestStat_sb.push_back(other->GetTestStat_sb()[i]);
214 
215  int other_size_b = other->GetTestStat_b().size();
216  for (int i=0;i<other_size_b;++i)
217  fTestStat_b.push_back(other->GetTestStat_b()[i]);
218 
219  // if no data is present use the other's HybridResult's data
220  if (fTestStat_data==-999.)
221  fTestStat_data = other->GetTestStat_data();
222 
223  fComputationsAltDoneFlag = false;
224  fComputationsNulDoneFlag = false;
225 
226  return;
227 }
228 
229 ///////////////////////////////////////////////////////////////////////////
230 
231 HybridPlot* HybridResult::GetPlot(const char* name,const char* title, int n_bins)
232 {
233  // prepare a plot showing a result and return a pointer to a HybridPlot object
234  // the needed arguments are: an object name, a title and the number of bins in the plot
235 
236  // default plot name
237  TString plot_name;
238  if ( TString(name)=="" ) {
239  plot_name += GetName();
240  plot_name += "_plot";
241  } else plot_name = name;
242 
243  // default plot title
244  TString plot_title;
245  if ( TString(title)=="" ) {
246  plot_title += GetTitle();
247  plot_title += "_plot (";
248  plot_title += fTestStat_b.size();
249  plot_title += " toys)";
250  } else plot_title = title;
251 
252  HybridPlot* plot = new HybridPlot( plot_name.Data(),
253  plot_title.Data(),
254  fTestStat_sb,
255  fTestStat_b,
257  n_bins,
258  true );
259  return plot;
260 }
261 
262 ///////////////////////////////////////////////////////////////////////////
263 
264 void HybridResult::PrintMore(const char* /*options */)
265 {
266  // Print out some information about the results
267 
268  std::cout << "\nResults " << GetName() << ":\n"
269  << " - Number of S+B toys: " << fTestStat_b.size() << std::endl
270  << " - Number of B toys: " << fTestStat_sb.size() << std::endl
271  << " - test statistics evaluated on data: " << fTestStat_data << std::endl
272  << " - CL_b " << CLb() << std::endl
273  << " - CL_s+b " << CLsplusb() << std::endl
274  << " - CL_s " << CLs() << std::endl;
275 
276  return;
277 }
278 
279 
280 
281 
282 
283 
284 
285 
286 
287 
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
HybridPlot * GetPlot(const char *name, const char *title, int n_bins)
virtual Double_t CLb() const
Convert NullPValue into a "confidence level".
Double_t CLbError() const
The error on the "confidence level" of the null hypothesis.
void Add(HybridResult *other)
#define assert(cond)
Definition: unittest.h:542
HypoTestResult is a base class for results from hypothesis tests.
Basic string class.
Definition: TString.h:137
Double_t CLsError() const
The error on the ratio .
STL namespace.
virtual ~HybridResult()
Destructor of HybridResult.
void SetDataTestStatistics(double testStat_data_val)
This class provides the plots for the result of a study performed with the HybridCalculatorOriginal c...
Definition: HybridPlot.h:53
Double_t NullPValue() const
Returns : the B p-value.
Double_t CLsplusbError() const
The error on the "confidence level" of the alternative hypothesis.
virtual Double_t CLs() const
is simply (not a method, but a quantity)
virtual Double_t CLsplusb() const
Convert AlternatePValue into a "confidence level".
Namespace for the RooStats classes.
Definition: Asimov.h:20
#define ClassImp(name)
Definition: Rtypes.h:279
double Double_t
Definition: RtypesCore.h:55
double GetTestStat_data()
Get test statistics value for data.
Definition: HybridResult.h:74
std::vector< double > fTestStat_b
Definition: HybridResult.h:93
std::vector< double > GetTestStat_sb()
Get test statistics values for the sb model.
Definition: HybridResult.h:68
void PrintMore(const char *options)
#define name(a, b)
Definition: linkTestLib0.cpp:5
std::vector< double > GetTestStat_b()
Get test statistics values for the b model.
Definition: HybridResult.h:71
Double_t Sqrt(Double_t x)
Definition: TMath.h:464
const Int_t n
Definition: legend1.C:16
Double_t AlternatePValue() const
Returns : the S+B p-value.
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52
std::vector< double > fTestStat_sb
Definition: HybridResult.h:94
const char * Data() const
Definition: TString.h:349