Logo ROOT   6.08/07
Reference Guide
rs101_limitexample.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roostats
3 /// \notebook
4 /// 'Limit Example' RooStats tutorial macro #101
5 /// This tutorial shows an example of creating a simple
6 /// model for a number counting experiment with uncertainty
7 /// on both the background rate and signal efficiency. We then
8 /// use a Confidence Interval Calculator to set a limit on the signal.
9 ///
10 /// \macro_image
11 /// \macro_output
12 /// \macro_code
13 ///
14 /// \author Kyle Cranmer
15 
16 #include "RooProfileLL.h"
17 #include "RooAbsPdf.h"
19 #include "RooRealVar.h"
20 #include "RooPlot.h"
21 #include "RooDataSet.h"
22 #include "RooTreeDataStore.h"
23 #include "TTree.h"
24 #include "TCanvas.h"
25 #include "TLine.h"
26 #include "TStopwatch.h"
27 
33 #include "RooStats/ConfInterval.h"
37 #include "RooStats/RooStatsUtils.h"
38 #include "RooStats/ModelConfig.h"
39 #include "RooStats/MCMCInterval.h"
43 #include "RooFitResult.h"
44 #include "TGraph2D.h"
45 
46 // use this order for safety on library loading
47 using namespace RooFit;
48 using namespace RooStats;
49 
50 
51 void rs101_limitexample()
52 {
53  // --------------------------------------
54  // An example of setting a limit in a number counting experiment with uncertainty on background and signal
55 
56  // to time the macro
57  TStopwatch t;
58  t.Start();
59 
60  // --------------------------------------
61  // The Model building stage
62  // --------------------------------------
63  RooWorkspace* wspace = new RooWorkspace();
64  wspace->factory("Poisson::countingModel(obs[150,0,300], sum(s[50,0,120]*ratioSigEff[1.,0,3.],b[100]*ratioBkgEff[1.,0.,3.]))"); // counting model
65  // wspace->factory("Gaussian::sigConstraint(ratioSigEff,1,0.05)"); // 5% signal efficiency uncertainty
66  // wspace->factory("Gaussian::bkgConstraint(ratioBkgEff,1,0.1)"); // 10% background efficiency uncertainty
67  wspace->factory("Gaussian::sigConstraint(gSigEff[1,0,3],ratioSigEff,0.05)"); // 5% signal efficiency uncertainty
68  wspace->factory("Gaussian::bkgConstraint(gSigBkg[1,0,3],ratioBkgEff,0.2)"); // 10% background efficiency uncertainty
69  wspace->factory("PROD::modelWithConstraints(countingModel,sigConstraint,bkgConstraint)"); // product of terms
70  wspace->Print();
71 
72  RooAbsPdf* modelWithConstraints = wspace->pdf("modelWithConstraints"); // get the model
73  RooRealVar* obs = wspace->var("obs"); // get the observable
74  RooRealVar* s = wspace->var("s"); // get the signal we care about
75  RooRealVar* b = wspace->var("b"); // get the background and set it to a constant. Uncertainty included in ratioBkgEff
76  b->setConstant();
77 
78  RooRealVar* ratioSigEff = wspace->var("ratioSigEff"); // get uncertain parameter to constrain
79  RooRealVar* ratioBkgEff = wspace->var("ratioBkgEff"); // get uncertain parameter to constrain
80  RooArgSet constrainedParams(*ratioSigEff, *ratioBkgEff); // need to constrain these in the fit (should change default behavior)
81 
82  RooRealVar * gSigEff = wspace->var("gSigEff"); // global observables for signal efficiency
83  RooRealVar * gSigBkg = wspace->var("gSigBkg"); // global obs for background efficiency
84  gSigEff->setConstant();
85  gSigBkg->setConstant();
86 
87  // Create an example dataset with 160 observed events
88  obs->setVal(160.);
89  RooDataSet* data = new RooDataSet("exampleData", "exampleData", RooArgSet(*obs));
90  data->add(*obs);
91 
92  RooArgSet all(*s, *ratioBkgEff, *ratioSigEff);
93 
94  // not necessary
95  modelWithConstraints->fitTo(*data, RooFit::Constrain(RooArgSet(*ratioSigEff, *ratioBkgEff)));
96 
97  // Now let's make some confidence intervals for s, our parameter of interest
98  RooArgSet paramOfInterest(*s);
99 
100  ModelConfig modelConfig(wspace);
101  modelConfig.SetPdf(*modelWithConstraints);
102  modelConfig.SetParametersOfInterest(paramOfInterest);
103  modelConfig.SetNuisanceParameters(constrainedParams);
104  modelConfig.SetObservables(*obs);
105  modelConfig.SetGlobalObservables( RooArgSet(*gSigEff,*gSigBkg));
106  modelConfig.SetName("ModelConfig");
107  wspace->import(modelConfig);
108  wspace->import(*data);
109  wspace->SetName("w");
110  wspace->writeToFile("rs101_ws.root");
111 
112 
113 
114  // First, let's use a Calculator based on the Profile Likelihood Ratio
115  //ProfileLikelihoodCalculator plc(*data, *modelWithConstraints, paramOfInterest);
116  ProfileLikelihoodCalculator plc(*data, modelConfig);
117  plc.SetTestSize(.05);
118  ConfInterval* lrinterval = plc.GetInterval(); // that was easy.
119 
120  // Let's make a plot
121  TCanvas* dataCanvas = new TCanvas("dataCanvas");
122  dataCanvas->Divide(2,1);
123 
124  dataCanvas->cd(1);
125  LikelihoodIntervalPlot plotInt((LikelihoodInterval*)lrinterval);
126  plotInt.SetTitle("Profile Likelihood Ratio and Posterior for S");
127  plotInt.Draw();
128 
129  // Second, use a Calculator based on the Feldman Cousins technique
130  FeldmanCousins fc(*data, modelConfig);
131  fc.UseAdaptiveSampling(true);
132  fc.FluctuateNumDataEntries(false); // number counting analysis: dataset always has 1 entry with N events observed
133  fc.SetNBins(100); // number of points to test per parameter
134  fc.SetTestSize(.05);
135  // fc.SaveBeltToFile(true); // optional
136  ConfInterval* fcint = NULL;
137  fcint = fc.GetInterval(); // that was easy.
138 
139  RooFitResult* fit = modelWithConstraints->fitTo(*data, Save(true));
140 
141  // Third, use a Calculator based on Markov Chain monte carlo
142  // Before configuring the calculator, let's make a ProposalFunction
143  // that will achieve a high acceptance rate
144  ProposalHelper ph;
146  ph.SetCovMatrix(fit->covarianceMatrix());
148  ph.SetCacheSize(100);
149  ProposalFunction* pdfProp = ph.GetProposalFunction(); // that was easy
150 
151  MCMCCalculator mc(*data, modelConfig);
152  mc.SetNumIters(20000); // steps to propose in the chain
153  mc.SetTestSize(.05); // 95% CL
154  mc.SetNumBurnInSteps(40); // ignore first N steps in chain as "burn in"
155  mc.SetProposalFunction(*pdfProp);
156  mc.SetLeftSideTailFraction(0.5); // find a "central" interval
157  MCMCInterval* mcInt = (MCMCInterval*)mc.GetInterval(); // that was easy
158 
159 
160  // Get Lower and Upper limits from Profile Calculator
161  cout << "Profile lower limit on s = " << ((LikelihoodInterval*) lrinterval)->LowerLimit(*s) << endl;
162  cout << "Profile upper limit on s = " << ((LikelihoodInterval*) lrinterval)->UpperLimit(*s) << endl;
163 
164  // Get Lower and Upper limits from FeldmanCousins with profile construction
165  if (fcint != NULL) {
166  double fcul = ((PointSetInterval*) fcint)->UpperLimit(*s);
167  double fcll = ((PointSetInterval*) fcint)->LowerLimit(*s);
168  cout << "FC lower limit on s = " << fcll << endl;
169  cout << "FC upper limit on s = " << fcul << endl;
170  TLine* fcllLine = new TLine(fcll, 0, fcll, 1);
171  TLine* fculLine = new TLine(fcul, 0, fcul, 1);
172  fcllLine->SetLineColor(kRed);
173  fculLine->SetLineColor(kRed);
174  fcllLine->Draw("same");
175  fculLine->Draw("same");
176  dataCanvas->Update();
177  }
178 
179  // Plot MCMC interval and print some statistics
180  MCMCIntervalPlot mcPlot(*mcInt);
181  mcPlot.SetLineColor(kMagenta);
182  mcPlot.SetLineWidth(2);
183  mcPlot.Draw("same");
184 
185  double mcul = mcInt->UpperLimit(*s);
186  double mcll = mcInt->LowerLimit(*s);
187  cout << "MCMC lower limit on s = " << mcll << endl;
188  cout << "MCMC upper limit on s = " << mcul << endl;
189  cout << "MCMC Actual confidence level: "
190  << mcInt->GetActualConfidenceLevel() << endl;
191 
192  // 3-d plot of the parameter points
193  dataCanvas->cd(2);
194  // also plot the points in the markov chain
195  RooDataSet * chainData = mcInt->GetChainAsDataSet();
196 
197  assert(chainData);
198  std::cout << "plotting the chain data - nentries = " << chainData->numEntries() << std::endl;
199  TTree* chain = RooStats::GetAsTTree("chainTreeData","chainTreeData",*chainData);
200  assert(chain);
201  chain->SetMarkerStyle(6);
202  chain->SetMarkerColor(kRed);
203 
204  chain->Draw("s:ratioSigEff:ratioBkgEff","nll_MarkovChain_local_","box"); // 3-d box proportional to posterior
205 
206  // the points used in the profile construction
207  RooDataSet * parScanData = (RooDataSet*) fc.GetPointsToScan();
208  assert(parScanData);
209  std::cout << "plotting the scanned points used in the frequentist construction - npoints = " << parScanData->numEntries() << std::endl;
210  // getting the tree and drawing it -crashes (very strange....);
211  // TTree* parameterScan = RooStats::GetAsTTree("parScanTreeData","parScanTreeData",*parScanData);
212  // assert(parameterScan);
213  // parameterScan->Draw("s:ratioSigEff:ratioBkgEff","","goff");
214  TGraph2D *gr = new TGraph2D(parScanData->numEntries());
215  for (int ievt = 0; ievt < parScanData->numEntries(); ++ievt) {
216  const RooArgSet * evt = parScanData->get(ievt);
217  double x = evt->getRealValue("ratioBkgEff");
218  double y = evt->getRealValue("ratioSigEff");
219  double z = evt->getRealValue("s");
220  gr->SetPoint(ievt, x,y,z);
221  // std::cout << ievt << " " << x << " " << y << " " << z << std::endl;
222  }
223  gr->SetMarkerStyle(24);
224  gr->Draw("P SAME");
225 
226 
227  delete wspace;
228  delete lrinterval;
229  delete mcInt;
230  delete fcint;
231  delete data;
232 
233  // print timing info
234  t.Stop();
235  t.Print();
236 }
ProposalFunction is an interface for all proposal functions that would be used with a Markov Chain Mo...
ModelConfig is a simple class that holds configuration information specifying how a model should be u...
Definition: ModelConfig.h:52
const RooArgList & floatParsFinal() const
Definition: RooFitResult.h:109
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:58
virtual void Draw(Option_t *option="")
Specific drawing options can be used to paint a TGraph2D:
Definition: TGraph2D.cxx:704
This class provides simple and straightforward utilities to plot a MCMCInterval object.
void Print(Option_t *option="") const
Print the real and cpu time passed between the start and stop events.
Definition: TStopwatch.cxx:219
LikelihoodInterval is a concrete implementation of the RooStats::ConfInterval interface.
Definition: Rtypes.h:61
const TMatrixDSym & covarianceMatrix() const
Return covariance matrix.
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:131
virtual ProposalFunction * GetProposalFunction()
virtual void SetUpdateProposalParameters(Bool_t updateParams)
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
This class provides simple and straightforward utilities to plot a LikelihoodInterval object...
ProfileLikelihoodCalculator is a concrete implementation of CombinedCalculator (the interface class f...
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:255
TTree * GetAsTTree(TString name, TString desc, const RooDataSet &data)
static struct mg_connection * fc(struct mg_context *ctx)
Definition: civetweb.c:1956
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:77
Double_t x[n]
Definition: legend1.C:17
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:37
virtual void setVal(Double_t value)
Set value of variable to &#39;value&#39;.
Definition: RooRealVar.cxx:205
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:46
void setConstant(Bool_t value=kTRUE)
virtual void SetCacheSize(Int_t size)
A simple line.
Definition: TLine.h:33
Bool_t writeToFile(const char *fileName, Bool_t recreate=kTRUE)
Save this current workspace into given file.
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition: TAttMarker.h:45
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
TGraphErrors * gr
Definition: legend1.C:25
virtual const RooArgSet * get(Int_t index) const
Return RooArgSet with coordinates of event &#39;index&#39;.
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 &#39;data&#39; argset, to the data set...
The FeldmanCousins class (like the Feldman-Cousins technique) is essentially a specific configuration...
The Canvas class.
Definition: TCanvas.h:41
ConfInterval is an interface class for a generic interval in the RooStats framework.
Definition: ConfInterval.h:44
Namespace for the RooStats classes.
Definition: Asimov.h:20
PointSetInterval is a concrete implementation of the ConfInterval interface.
virtual void SetPoint(Int_t point, Double_t x, Double_t y, Double_t z)
Sets point number n.
Definition: TGraph2D.cxx:1692
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(RooRealVar &param)
get the lowest value of param that is within the confidence interval
Double_t y[n]
Definition: legend1.C:17
virtual void SetCovMatrix(const TMatrixDSym &covMatrix)
RooRealVar * var(const char *name) const
Retrieve real-valued variable (RooRealVar) with given name. A null pointer is returned if not found...
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
RooFactoryWSTool & factory()
Return instance to factory tool.
RooCmdArg Save(Bool_t flag=kTRUE)
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1089
Bool_t import(const RooAbsArg &arg, const RooCmdArg &arg1=RooCmdArg(), const RooCmdArg &arg2=RooCmdArg(), const RooCmdArg &arg3=RooCmdArg(), const RooCmdArg &arg4=RooCmdArg(), const RooCmdArg &arg5=RooCmdArg(), const RooCmdArg &arg6=RooCmdArg(), const RooCmdArg &arg7=RooCmdArg(), const RooCmdArg &arg8=RooCmdArg(), const RooCmdArg &arg9=RooCmdArg())
Import a RooAbsArg object, e.g.
Double_t getRealValue(const char *name, Double_t defVal=0, Bool_t verbose=kFALSE) const
Get value of a RooAbsReal stored in set with given name.
Definition: RooArgSet.cxx:527
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
#define NULL
Definition: Rtypes.h:82
virtual RooFitResult * fitTo(RooAbsData &data, const RooCmdArg &arg1=RooCmdArg::none(), const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none())
Fit PDF to given dataset.
Definition: RooAbsPdf.cxx:1056
MCMCInterval is a concrete implementation of the RooStats::ConfInterval interface.
Definition: MCMCInterval.h:88
virtual void SetVariables(RooArgList &vars)
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2183
Graphics object made of three arrays X, Y and Z with the same number of points each.
Definition: TGraph2D.h:50
Bayesian Calculator estimating an interval or a credible region using the Markov-Chain Monte Carlo me...
void Print(Option_t *opts=0) const
Print contents of the workspace.
RooCmdArg Constrain(const RooArgSet &params)
The RooWorkspace is a persistable container for RooFit projects.
Definition: RooWorkspace.h:42
virtual Int_t numEntries() const
Definition: RooAbsData.cxx:269
Stopwatch class.
Definition: TStopwatch.h:30