Logo ROOT   6.12/06
Reference Guide
rs701_BayesianCalculator.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roostats
3 /// \notebook
4 /// 'Bayesian Calculator' RooStats tutorial macro #701
5 ///
6 /// This tutorial shows an example of using the BayesianCalculator class
7 ///
8 /// \macro_code
9 ///
10 /// \author Gregory Schott
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 messages 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 }
return c1
Definition: legend1.C:41
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
static RooMsgService & instance()
Return reference to singleton instance.
virtual Double_t LowerLimit()
virtual Double_t ConfidenceLevel() const
return confidence level
void setGlobalKillBelow(RooFit::MsgLevel level)
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
A RooPlot is a plot frame and a container for graphics objects within that frame. ...
Definition: RooPlot.h:41
The Canvas class.
Definition: TCanvas.h:31
Namespace for the RooStats classes.
Definition: Asimov.h:20
RooAbsPdf * pdf(const char *name) const
Retrieve p.d.f (RooAbsPdf) with given name. A null pointer is returned if not found.
RooRealVar * var(const char *name) const
Retrieve real-valued variable (RooRealVar) with given name. A null pointer is returned if not found...
RooFactoryWSTool & factory()
Return instance to factory tool.
virtual Double_t UpperLimit()
SimpleInterval is a concrete implementation of the ConfInterval interface.
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:285
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