Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rs101_limitexample.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roostats
3/// \notebook
4/// Limits: number counting experiment with uncertainty on both the background rate and signal efficiency.
5///
6/// The usage of a Confidence Interval Calculator to set a limit on the signal is illustrated
7///
8/// \macro_image
9/// \macro_output
10/// \macro_code
11///
12/// \author Kyle Cranmer
13
14#include "RooProfileLL.h"
15#include "RooAbsPdf.h"
17#include "RooRealVar.h"
18#include "RooPlot.h"
19#include "RooDataSet.h"
20#include "RooTreeDataStore.h"
21#include "TTree.h"
22#include "TCanvas.h"
23#include "TLine.h"
24#include "TStopwatch.h"
25
41#include "RooFitResult.h"
42#include "TGraph2D.h"
43
44#include <cassert>
45
46// use this order for safety on library loading
47using namespace RooFit;
48using namespace RooStats;
49
51{
52 // --------------------------------------
53 // An example of setting a limit in a number counting experiment with uncertainty on background and signal
54
55 // to time the macro
56 TStopwatch t;
57 t.Start();
58
59 // --------------------------------------
60 // The Model building stage
61 // --------------------------------------
62 RooWorkspace wspace{};
63 wspace.factory("Poisson::countingModel(obs[150,0,300], "
64 "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 =
76 wspace.var("b"); // get the background and set it to a constant. Uncertainty included in ratioBkgEff
77 b->setConstant();
78
79 RooRealVar *ratioSigEff = wspace.var("ratioSigEff"); // get uncertain parameter to constrain
80 RooRealVar *ratioBkgEff = wspace.var("ratioBkgEff"); // get uncertain parameter to constrain
81 RooArgSet constrainedParams(*ratioSigEff,
82 *ratioBkgEff); // need to constrain these in the fit (should change default behavior)
83
84 RooRealVar *gSigEff = wspace.var("gSigEff"); // global observables for signal efficiency
85 RooRealVar *gSigBkg = wspace.var("gSigBkg"); // global obs for background efficiency
86 gSigEff->setConstant();
87 gSigBkg->setConstant();
88
89 // Create an example dataset with 160 observed events
90 obs->setVal(160.);
91 RooDataSet dataOrig{"exampleData", "exampleData", *obs};
92 dataOrig.add(*obs);
93
94 RooArgSet all(*s, *ratioBkgEff, *ratioSigEff);
95
96 // not necessary
97 modelWithConstraints->fitTo(dataOrig, Constrain({*ratioSigEff, *ratioBkgEff}), PrintLevel(-1));
98
99 // Now let's make some confidence intervals for s, our parameter of interest
100 RooArgSet paramOfInterest(*s);
101
102 ModelConfig modelConfig(&wspace);
103 modelConfig.SetPdf(*modelWithConstraints);
104 modelConfig.SetParametersOfInterest(paramOfInterest);
105 modelConfig.SetNuisanceParameters(constrainedParams);
106 modelConfig.SetObservables(*obs);
107 modelConfig.SetGlobalObservables(RooArgSet(*gSigEff, *gSigBkg));
108 modelConfig.SetName("ModelConfig");
109 wspace.import(modelConfig);
110 wspace.import(dataOrig);
111 wspace.SetName("w");
112 wspace.writeToFile("rs101_ws.root");
113
114 // Make sure we reference the data in the workspace from now on
115 RooDataSet &data = static_cast<RooDataSet &>(*wspace.data(dataOrig.GetName()));
116
117 // First, let's use a Calculator based on the Profile Likelihood Ratio
118 // ProfileLikelihoodCalculator plc(data, *modelWithConstraints, paramOfInterest);
119 ProfileLikelihoodCalculator plc(data, modelConfig);
120 plc.SetTestSize(.05);
121 std::unique_ptr<LikelihoodInterval> lrinterval{static_cast<LikelihoodInterval*>(plc.GetInterval())};
122
123 // Let's make a plot
124 auto dataCanvas = new TCanvas("dataCanvas");
125 dataCanvas->Divide(2, 1);
126
127 dataCanvas->cd(1);
128 LikelihoodIntervalPlot plotInt(lrinterval.get());
129 plotInt.SetTitle("Profile Likelihood Ratio and Posterior for S");
130 plotInt.Draw();
131
132 // Second, use a Calculator based on the Feldman Cousins technique
133 FeldmanCousins fc(data, modelConfig);
134 fc.UseAdaptiveSampling(true);
135 fc.FluctuateNumDataEntries(false); // number counting analysis: dataset always has 1 entry with N events observed
136 fc.SetNBins(100); // number of points to test per parameter
137 fc.SetTestSize(.05);
138 // fc.SaveBeltToFile(true); // optional
139 std::unique_ptr<PointSetInterval> fcint{static_cast<PointSetInterval*>(fc.GetInterval())};
140
141 std::unique_ptr<RooFitResult> fit{modelWithConstraints->fitTo(data, Save(true), PrintLevel(-1))};
142
143 // Third, use a Calculator based on Markov Chain monte carlo
144 // Before configuring the calculator, let's make a ProposalFunction
145 // that will achieve a high acceptance rate
147 ph.SetVariables((RooArgSet &)fit->floatParsFinal());
148 ph.SetCovMatrix(fit->covarianceMatrix());
150 ph.SetCacheSize(100);
152
153 MCMCCalculator mc(data, modelConfig);
154 mc.SetNumIters(20000); // steps to propose in the chain
155 mc.SetTestSize(.05); // 95% CL
156 mc.SetNumBurnInSteps(40); // ignore first N steps in chain as "burn in"
157 mc.SetProposalFunction(*pdfProp);
158 mc.SetLeftSideTailFraction(0.5); // find a "central" interval
159 std::unique_ptr<MCMCInterval> mcInt{static_cast<MCMCInterval *>(mc.GetInterval())};
160
161 // Get Lower and Upper limits from Profile Calculator
162 std::cout << "Profile lower limit on s = " << lrinterval->LowerLimit(*s) << std::endl;
163 std::cout << "Profile upper limit on s = " << lrinterval->UpperLimit(*s) << std::endl;
164
165 // Get Lower and Upper limits from FeldmanCousins with profile construction
166 if (fcint) {
167 double fcul = fcint->UpperLimit(*s);
168 double fcll = fcint->LowerLimit(*s);
169 std::cout << "FC lower limit on s = " << fcll << std::endl;
170 std::cout << "FC upper limit on s = " << fcul << std::endl;
171 auto fcllLine = new TLine(fcll, 0, fcll, 1);
172 auto fculLine = new TLine(fcul, 0, fcul, 1);
173 fcllLine->SetLineColor(kRed);
174 fculLine->SetLineColor(kRed);
175 fcllLine->Draw("same");
176 fculLine->Draw("same");
177 dataCanvas->Update();
178 }
179
180 // Plot MCMC interval and print some statistics
181 MCMCIntervalPlot mcPlot(*mcInt);
182 mcPlot.SetLineColor(kMagenta);
183 mcPlot.SetLineWidth(2);
184 mcPlot.Draw("same");
185
186 double mcul = mcInt->UpperLimit(*s);
187 double mcll = mcInt->LowerLimit(*s);
188 std::cout << "MCMC lower limit on s = " << mcll << std::endl;
189 std::cout << "MCMC upper limit on s = " << mcul << std::endl;
190 std::cout << "MCMC Actual confidence level: " << mcInt->GetActualConfidenceLevel() << std::endl;
191
192 // 3-d plot of the parameter points
193 dataCanvas->cd(2);
194 // also plot the points in the markov chain
195 std::unique_ptr<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 = "
210 << parScanData->numEntries() << std::endl;
211 // getting the tree and drawing it -crashes (very strange....);
212 // TTree* parameterScan = RooStats::GetAsTTree("parScanTreeData","parScanTreeData",*parScanData);
213 // assert(parameterScan);
214 // parameterScan->Draw("s:ratioSigEff:ratioBkgEff","","goff");
215 auto gr = new TGraph2D(parScanData->numEntries());
216 for (int ievt = 0; ievt < parScanData->numEntries(); ++ievt) {
217 const RooArgSet *evt = parScanData->get(ievt);
218 double x = evt->getRealValue("ratioBkgEff");
219 double y = evt->getRealValue("ratioSigEff");
220 double z = evt->getRealValue("s");
221 gr->SetPoint(ievt, x, y, z);
222 // std::cout << ievt << " " << x << " " << y << " " << z << std::endl;
223 }
224 gr->SetMarkerStyle(24);
225 gr->Draw("P SAME");
226
227 // print timing info
228 t.Stop();
229 t.Print();
230}
#define b(i)
Definition RSha256.hxx:100
@ kRed
Definition Rtypes.h:66
@ kMagenta
Definition Rtypes.h:66
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
double getRealValue(const char *name, double defVal=0.0, bool verbose=false) const
Get value of a RooAbsReal stored in set with given name.
virtual Int_t numEntries() const
Return number of entries in dataset, i.e., count unweighted entries.
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:40
RooFit::OwningPtr< RooFitResult > fitTo(RooAbsData &data, CmdArgs_t const &... cmdArgs)
Fit PDF to given dataset.
Definition RooAbsPdf.h:157
void setConstant(bool value=true)
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
Container class to hold unbinned data.
Definition RooDataSet.h:57
const RooArgSet * get(Int_t index) const override
Return RooArgSet with coordinates of event 'index'.
Variable that can be changed from the outside.
Definition RooRealVar.h:37
void setVal(double value) override
Set value of variable to 'value'.
The FeldmanCousins class (like the Feldman-Cousins technique) is essentially a specific configuration...
This class provides simple and straightforward utilities to plot a LikelihoodInterval object.
LikelihoodInterval is a concrete implementation of the RooStats::ConfInterval interface.
Bayesian Calculator estimating an interval or a credible region using the Markov-Chain Monte Carlo me...
This class provides simple and straightforward utilities to plot a MCMCInterval object.
MCMCInterval is a concrete implementation of the RooStats::ConfInterval interface.
virtual double LowerLimit(RooRealVar &param)
get the lowest value of param that is within the confidence interval
ModelConfig is a simple class that holds configuration information specifying how a model should be u...
Definition ModelConfig.h:35
PointSetInterval is a concrete implementation of the ConfInterval interface.
The ProfileLikelihoodCalculator is a concrete implementation of CombinedCalculator (the interface cla...
ProposalFunction is an interface for all proposal functions that would be used with a Markov Chain Mo...
virtual void SetCovMatrix(const TMatrixDSym &covMatrix)
set the covariance matrix to use for a multi-variate Gaussian proposal
virtual ProposalFunction * GetProposalFunction()
Get the ProposalFunction that we've been designing.
virtual void SetVariables(RooArgList &vars)
virtual void SetCacheSize(Int_t size)
virtual void SetUpdateProposalParameters(bool updateParams)
Persistable container for RooFit projects.
RooFactoryWSTool & factory()
Return instance to factory tool.
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition TAttMarker.h:38
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition TAttMarker.h:40
The Canvas class.
Definition TCanvas.h:23
Graphics object made of three arrays X, Y and Z with the same number of points each.
Definition TGraph2D.h:41
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
Definition TGraph.cxx:2319
void Draw(Option_t *chopt="") override
Draw this graph with its current attributes.
Definition TGraph.cxx:809
Use the TLine constructor to create a simple line.
Definition TLine.h:22
void Print(Option_t *option="") const override
Print TNamed name and title.
Definition TNamed.cxx:128
Stopwatch class.
Definition TStopwatch.h:28
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
void Stop()
Stop the stopwatch.
void Print(Option_t *option="") const override
Print the real and cpu time passed between the start and stop events.
A TTree represents a columnar dataset.
Definition TTree.h:79
void Draw(Option_t *opt) override
Default Draw method for all objects.
Definition TTree.h:431
RooCmdArg Constrain(const RooArgSet &params)
RooCmdArg Save(bool flag=true)
RooCmdArg PrintLevel(Int_t code)
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
TGraphErrors * gr
Definition legend1.C:25
fit(model, train_loader, val_loader, num_epochs, batch_size, optimizer, criterion, save_best, scheduler)
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition JSONIO.h:26
Namespace for the RooStats classes.
Definition Asimov.h:19
TTree * GetAsTTree(TString name, TString desc, const RooDataSet &data)
Create a TTree with the given name and description. All RooRealVars in the RooDataSet are represented...