ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
rs701_BayesianCalculator.C
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////
2 //
3 // 'Bayesian Calculator' RooStats tutorial macro #701
4 // author: Gregory Schott
5 // date Sep 2009
6 //
7 // This tutorial shows an example of using the BayesianCalculator class
8 //
9 /////////////////////////////////////////////////////////////////////////
10 
11 
12 #include "RooRealVar.h"
13 #include "RooWorkspace.h"
14 #include "RooDataSet.h"
15 #include "RooPlot.h"
16 #include "RooMsgService.h"
17 
20 #include "TCanvas.h"
21 
22 using namespace RooFit;
23 using namespace RooStats;
24 
25 void rs701_BayesianCalculator(bool useBkg = true, double confLevel = 0.90)
26 {
27 
28 
29  RooWorkspace* w = new RooWorkspace("w",true);
30  w->factory("SUM::pdf(s[0.001,15]*Uniform(x[0,1]),b[1,0,2]*Uniform(x))");
31  w->factory("Gaussian::prior_b(b,1,1)");
32  w->factory("PROD::model(pdf,prior_b)");
33  RooAbsPdf* model = w->pdf("model"); // pdf*priorNuisance
34  RooArgSet nuisanceParameters(*(w->var("b")));
35 
36 
37 
38  RooAbsRealLValue* POI = w->var("s");
39  RooAbsPdf* priorPOI = (RooAbsPdf *) w->factory("Uniform::priorPOI(s)");
40  RooAbsPdf* priorPOI2 = (RooAbsPdf *) w->factory("GenericPdf::priorPOI2('1/sqrt(@0)',s)");
41 
42  w->factory("n[3]"); // observed number of events
43  // create a data set with n observed events
44  RooDataSet data("data","",RooArgSet(*(w->var("x")),*(w->var("n"))),"n");
45  data.add(RooArgSet(*(w->var("x"))),w->var("n")->getVal());
46 
47  // to suppress messgaes when pdf goes to zero
49 
50  RooArgSet * nuisPar = 0;
51  if (useBkg) nuisPar = &nuisanceParameters;
52  //if (!useBkg) ((RooRealVar *)w->var("b"))->setVal(0);
53 
54  double size = 1.-confLevel;
55  std::cout << "\nBayesian Result using a Flat prior " << std::endl;
56  BayesianCalculator bcalc(data,*model,RooArgSet(*POI),*priorPOI, nuisPar);
57  bcalc.SetTestSize(size);
58  SimpleInterval* interval = bcalc.GetInterval();
59  double cl = bcalc.ConfidenceLevel();
60  std::cout << cl <<"% CL central interval: [ " << interval->LowerLimit() << " - " << interval->UpperLimit()
61  << " ] or "
62  << cl+(1.-cl)/2 << "% CL limits\n";
63  RooPlot * plot = bcalc.GetPosteriorPlot();
64  TCanvas * c1 = new TCanvas("c1","Bayesian Calculator Result");
65  c1->Divide(1,2);
66  c1->cd(1);
67  plot->Draw();
68  c1->Update();
69 
70  std::cout << "\nBayesian Result using a 1/sqrt(s) prior " << std::endl;
71  BayesianCalculator bcalc2(data,*model,RooArgSet(*POI),*priorPOI2,nuisPar);
72  bcalc2.SetTestSize(size);
73  SimpleInterval* interval2 = bcalc2.GetInterval();
74  cl = bcalc2.ConfidenceLevel();
75  std::cout << cl <<"% CL central interval: [ " << interval2->LowerLimit() << " - " << interval2->UpperLimit()
76  << " ] or "
77  << cl+(1.-cl)/2 << "% CL limits\n";
78 
79  RooPlot * plot2 = bcalc2.GetPosteriorPlot();
80  c1->cd(2);
81  plot2->Draw();
82  gPad->SetLogy();
83  c1->Update();
84 
85  // observe one event while expecting one background event -> the 95% CL upper limit on s is 4.10
86  // observe one event while expecting zero background event -> the 95% CL upper limit on s is 4.74
87 }
RooPlot * GetPosteriorPlot(bool norm=false, double precision=0.01) const
get the plot with option to get it normalized
TCanvas * c1
Definition: legend1.C:2
static RooMsgService & instance()
Return reference to singleton instance.
virtual SimpleInterval * GetInterval() const
compute the interval.
void plot(TString fname="data.root", TString var0="var0", TString var1="var1")
Definition: createData.C:17
RooAbsPdf * pdf(const char *name) const
Retrieve p.d.f (RooAbsPdf) with given name. A null pointer is returned if not found.
virtual Double_t LowerLimit()
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
tuple w
Definition: qtexample.py:51
void setGlobalKillBelow(RooFit::MsgLevel level)
virtual Double_t ConfidenceLevel() const
Get the Confidence level for the test.
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
virtual void add(const RooArgSet &row, Double_t weight=1.0, Double_t weightError=0)
Add a data point, with its coordinates specified in the 'data' argset, to the data set...
RooRealVar * var(const char *name) const
Retrieve real-valued variable (RooRealVar) with given name. A null pointer is returned if not found...
A RooPlot is a plot frame and a container for graphics objects within that frame. ...
Definition: RooPlot.h:41
RooFactoryWSTool & factory()
Return instance to factory tool.
virtual void SetTestSize(Double_t size)
set the size of the test (rate of Type I error) ( Eg. 0.05 for a 95% Confidence Interval) ...
virtual Double_t UpperLimit()
RooAbsRealLValue is the common abstract base class for objects that represent a real value that may a...
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
#define gPad
Definition: TVirtualPad.h:288
BayesianCalculator is a concrete implementation of IntervalCalculator, providing the computation of a...
The RooWorkspace is a persistable container for RooFit projects.
Definition: RooWorkspace.h:42
void rs701_BayesianCalculator(bool useBkg=true, double confLevel=0.90)
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:559