Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rs_bernsteinCorrection.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roostats
3/// \notebook -js
4/// Example of the BernsteinCorrection utility in RooStats.
5///
6/// The idea is that one has a distribution coming either from data or Monte Carlo
7/// (called "reality" in the macro) and a nominal model that is not sufficiently
8/// flexible to take into account the real distribution. One wants to take into
9/// account the systematic associated with this imperfect modeling by augmenting
10/// the nominal model with some correction term (in this case a polynomial).
11/// The BernsteinCorrection utility will import into your workspace a corrected model
12/// given by nominal(x) * poly_N(x), where poly_N is an n-th order polynomial in
13/// the Bernstein basis. The degree N of the polynomial is chosen by specifying the tolerance
14/// one has in adding an extra term to the polynomial.
15/// The Bernstein basis is nice because it only has positive-definite terms
16/// and works well with PDFs.
17/// Finally, the macro makes a plot of:
18/// - the data (drawn from 'reality'),
19/// - the best fit of the nominal model (blue)
20/// - and the best fit corrected model.
21///
22/// \macro_image
23/// \macro_output
24/// \macro_code
25///
26/// \author Kyle Cranmer
27
28#include "RooDataSet.h"
29#include "RooRealVar.h"
30#include "RooConstVar.h"
31#include "RooBernstein.h"
32#include "TCanvas.h"
33#include "RooAbsPdf.h"
34#include "RooFit.h"
35#include "RooFitResult.h"
36#include "RooPlot.h"
37#include <string>
38#include <vector>
39#include <stdio.h>
40#include <sstream>
41#include <iostream>
42
43#include "RooProdPdf.h"
44#include "RooAddPdf.h"
45#include "RooGaussian.h"
46#include "RooNLLVar.h"
47#include "RooProfileLL.h"
48#include "RooWorkspace.h"
49
51
52// use this order for safety on library loading
53using namespace RooFit;
54using namespace RooStats;
55
56//____________________________________
57void rs_bernsteinCorrection()
58{
59
60 // set range of observable
61 Double_t lowRange = -1, highRange = 5;
62
63 // make a RooRealVar for the observable
64 RooRealVar x("x", "x", lowRange, highRange);
65
66 // true model
67 RooGaussian narrow("narrow", "", x, RooConst(0.), RooConst(.8));
68 RooGaussian wide("wide", "", x, RooConst(0.), RooConst(2.));
69 RooAddPdf reality("reality", "", RooArgList(narrow, wide), RooConst(0.8));
70
71 RooDataSet *data = reality.generate(x, 1000);
72
73 // nominal model
74 RooRealVar sigma("sigma", "", 1., 0, 10);
75 RooGaussian nominal("nominal", "", x, RooConst(0.), sigma);
76
77 RooWorkspace *wks = new RooWorkspace("myWorksspace");
78
79 wks->import(*data, Rename("data"));
80 wks->import(nominal);
81
82 if (TClass::GetClass("ROOT::Minuit2::Minuit2Minimizer")) {
83 // use Minuit2 if ROOT was built with support for it:
85 }
86
87 // The tolerance sets the probability to add an unnecessary term.
88 // lower tolerance will add fewer terms, while higher tolerance
89 // will add more terms and provide a more flexible function.
90 Double_t tolerance = 0.05;
91 BernsteinCorrection bernsteinCorrection(tolerance);
92 Int_t degree = bernsteinCorrection.ImportCorrectedPdf(wks, "nominal", "x", "data");
93
94 if (degree < 0) {
95 Error("rs_bernsteinCorrection", "Bernstein correction failed ! ");
96 return;
97 }
98
99 cout << " Correction based on Bernstein Poly of degree " << degree << endl;
100
101 RooPlot *frame = x.frame();
102 data->plotOn(frame);
103 // plot the best fit nominal model in blue
105 nominal.fitTo(*data, PrintLevel(0), Minimizer(minimType));
106 nominal.plotOn(frame);
107
108 // plot the best fit corrected model in red
109 RooAbsPdf *corrected = wks->pdf("corrected");
110 if (!corrected)
111 return;
112
113 // fit corrected model
114 corrected->fitTo(*data, PrintLevel(0), Minimizer(minimType));
115 corrected->plotOn(frame, LineColor(kRed));
116
117 // plot the correction term (* norm constant) in dashed green
118 // should make norm constant just be 1, not depend on binning of data
119 RooAbsPdf *poly = wks->pdf("poly");
120 if (poly)
121 poly->plotOn(frame, LineColor(kGreen), LineStyle(kDashed));
122
123 // this is a switch to check the sampling distribution
124 // of -2 log LR for two comparisons:
125 // the first is for n-1 vs. n degree polynomial corrections
126 // the second is for n vs. n+1 degree polynomial corrections
127 // Here we choose n to be the one chosen by the tolerance
128 // criterion above, eg. n = "degree" in the code.
129 // Setting this to true is takes about 10 min.
130 bool checkSamplingDist = true;
131 int numToyMC = 20; // increase this value for sensible results
132
133 TCanvas *c1 = new TCanvas();
134 if (checkSamplingDist) {
135 c1->Divide(1, 2);
136 c1->cd(1);
137 }
138 frame->Draw();
139 gPad->Update();
140
141 if (checkSamplingDist) {
142 // check sampling dist
144 TH1F *samplingDist = new TH1F("samplingDist", "", 20, 0, 10);
145 TH1F *samplingDistExtra = new TH1F("samplingDistExtra", "", 20, 0, 10);
146 bernsteinCorrection.CreateQSamplingDist(wks, "nominal", "x", "data", samplingDist, samplingDistExtra, degree,
147 numToyMC);
148
149 c1->cd(2);
150 samplingDistExtra->SetLineColor(kRed);
151 samplingDistExtra->Draw();
152 samplingDist->Draw("same");
153 }
154}
int Int_t
Definition RtypesCore.h:45
double Double_t
Definition RtypesCore.h:59
@ kRed
Definition Rtypes.h:66
@ kGreen
Definition Rtypes.h:66
@ kDashed
Definition TAttLine.h:48
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:187
#define gPad
static void SetDefaultMinimizer(const char *type, const char *algo=0)
static const std::string & DefaultMinimizerType()
static void SetDefaultPrintLevel(int level)
virtual RooPlot * plotOn(RooPlot *frame, 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()) const
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.
virtual RooPlot * plotOn(RooPlot *frame, 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(), const RooCmdArg &arg9=RooCmdArg::none(), const RooCmdArg &arg10=RooCmdArg::none()) const
Helper calling plotOn(RooPlot*, RooLinkedList&) const.
Definition RooAbsPdf.h:121
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition RooAddPdf.h:32
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooDataSet is a container class to hold unbinned data.
Definition RooDataSet.h:36
Plain Gaussian p.d.f.
Definition RooGaussian.h:24
A RooPlot is a plot frame and a container for graphics objects within that frame.
Definition RooPlot.h:44
static RooPlot * frame(const RooAbsRealLValue &var, Double_t xmin, Double_t xmax, Int_t nBins)
Create a new frame for a given variable in x.
Definition RooPlot.cxx:249
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition RooPlot.cxx:661
RooRealVar represents a variable that can be changed from the outside.
Definition RooRealVar.h:39
BernsteinCorrection is a utility in RooStats to augment a nominal PDF with a polynomial correction te...
The RooWorkspace is a persistable container for RooFit projects.
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.
RooAbsPdf * pdf(const char *name) const
Retrieve p.d.f (RooAbsPdf) with given name. A null pointer is returned if not found.
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
The Canvas class.
Definition TCanvas.h:23
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition TClass.cxx:2966
1-D histogram with a float per channel (see TH1 documentation)}
Definition TH1.h:575
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition TH1.cxx:3074
Basic string class.
Definition TString.h:136
RooCmdArg Rename(const char *suffix)
RooConstVar & RooConst(Double_t val)
RooCmdArg PrintLevel(Int_t code)
RooCmdArg Minimizer(const char *type, const char *alg=0)
RooCmdArg LineColor(Color_t color)
RooCmdArg LineStyle(Style_t style)
const Double_t sigma
return c1
Definition legend1.C:41
Double_t x[n]
Definition legend1.C:17
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition Common.h:18
Namespace for the RooStats classes.
Definition Asimov.h:19