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 "RooFitResult.h"
35#include "RooPlot.h"
36#include <string>
37#include <vector>
38#include <stdio.h>
39#include <sstream>
40#include <iostream>
41
42#include "RooProdPdf.h"
43#include "RooAddPdf.h"
44#include "RooGaussian.h"
45#include "RooProfileLL.h"
46#include "RooWorkspace.h"
47
49
50// use this order for safety on library loading
51using namespace RooFit;
52using namespace RooStats;
53
54//____________________________________
56{
57
58 // set range of observable
59 Double_t lowRange = -1, highRange = 5;
60
61 // make a RooRealVar for the observable
62 RooRealVar x("x", "x", lowRange, highRange);
63
64 // true model
65 RooGaussian narrow("narrow", "", x, RooConst(0.), RooConst(.8));
66 RooGaussian wide("wide", "", x, RooConst(0.), RooConst(2.));
67 RooAddPdf reality("reality", "", RooArgList(narrow, wide), RooConst(0.8));
68
69 std::unique_ptr<RooDataSet> data{reality.generate(x, 1000)};
70
71 // nominal model
72 RooRealVar sigma("sigma", "", 1., 0, 10);
73 RooGaussian nominal("nominal", "", x, RooConst(0.), sigma);
74
75 RooWorkspace *wks = new RooWorkspace("myWorksspace");
76
77 wks->import(*data, Rename("data"));
78 wks->import(nominal);
79
80 if (TClass::GetClass("ROOT::Minuit2::Minuit2Minimizer")) {
81 // use Minuit2 if ROOT was built with support for it:
83 }
84
85 // The tolerance sets the probability to add an unnecessary term.
86 // lower tolerance will add fewer terms, while higher tolerance
87 // will add more terms and provide a more flexible function.
88 Double_t tolerance = 0.05;
89 BernsteinCorrection bernsteinCorrection(tolerance);
90 Int_t degree = bernsteinCorrection.ImportCorrectedPdf(wks, "nominal", "x", "data");
91
92 if (degree < 0) {
93 Error("rs_bernsteinCorrection", "Bernstein correction failed ! ");
94 return;
95 }
96
97 cout << " Correction based on Bernstein Poly of degree " << degree << endl;
98
99 RooPlot *frame = x.frame();
100 data->plotOn(frame);
101 // plot the best fit nominal model in blue
102 nominal.fitTo(*data, PrintLevel(0));
103 nominal.plotOn(frame);
104
105 // plot the best fit corrected model in red
106 RooAbsPdf *corrected = wks->pdf("corrected");
107 if (!corrected)
108 return;
109
110 // fit corrected model
111 corrected->fitTo(*data, PrintLevel(0));
112 corrected->plotOn(frame, LineColor(kRed));
113
114 // plot the correction term (* norm constant) in dashed green
115 // should make norm constant just be 1, not depend on binning of data
116 RooAbsPdf *poly = wks->pdf("poly");
117 if (poly)
118 poly->plotOn(frame, LineColor(kGreen), LineStyle(kDashed));
119
120 // this is a switch to check the sampling distribution
121 // of -2 log LR for two comparisons:
122 // the first is for n-1 vs. n degree polynomial corrections
123 // the second is for n vs. n+1 degree polynomial corrections
124 // Here we choose n to be the one chosen by the tolerance
125 // criterion above, eg. n = "degree" in the code.
126 // Setting this to true is takes about 10 min.
127 bool checkSamplingDist = true;
128 int numToyMC = 20; // increase this value for sensible results
129
130 TCanvas *c1 = new TCanvas();
131 if (checkSamplingDist) {
132 c1->Divide(1, 2);
133 c1->cd(1);
134 }
135 frame->Draw();
136 gPad->Update();
137
138 if (checkSamplingDist) {
139 // check sampling dist
141 TH1F *samplingDist = new TH1F("samplingDist", "", 20, 0, 10);
142 TH1F *samplingDistExtra = new TH1F("samplingDistExtra", "", 20, 0, 10);
143 bernsteinCorrection.CreateQSamplingDist(wks, "nominal", "x", "data", samplingDist, samplingDistExtra, degree,
144 numToyMC);
145
146 c1->cd(2);
147 samplingDistExtra->SetLineColor(kRed);
148 samplingDistExtra->Draw();
149 samplingDist->Draw("same");
150 }
151}
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:185
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
#define gPad
static void SetDefaultMinimizer(const char *type, const char *algo=nullptr)
Set the default Minimizer type and corresponding algorithms.
static void SetDefaultPrintLevel(int level)
Set the default Print Level.
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:40
RooPlot * plotOn(RooPlot *frame, const RooCmdArg &arg1={}, const RooCmdArg &arg2={}, const RooCmdArg &arg3={}, const RooCmdArg &arg4={}, const RooCmdArg &arg5={}, const RooCmdArg &arg6={}, const RooCmdArg &arg7={}, const RooCmdArg &arg8={}, const RooCmdArg &arg9={}, const RooCmdArg &arg10={}) const override
Helper calling plotOn(RooPlot*, RooLinkedList&) const.
Definition RooAbsPdf.h:124
RooFit::OwningPtr< RooFitResult > fitTo(RooAbsData &data, CmdArgs_t const &... cmdArgs)
Fit PDF to given dataset.
Definition RooAbsPdf.h:157
Efficient implementation of a sum of PDFs of the form.
Definition RooAddPdf.h:33
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
Plain Gaussian p.d.f.
Definition RooGaussian.h:24
Plot frame and a container for graphics objects within that frame.
Definition RooPlot.h:45
static RooPlot * frame(const RooAbsRealLValue &var, double xmin, double xmax, Int_t nBins)
Create a new frame for a given variable in x.
Definition RooPlot.cxx:225
void Draw(Option_t *options=nullptr) override
Draw this plot and all of the elements it contains.
Definition RooPlot.cxx:637
Variable that can be changed from the outside.
Definition RooRealVar.h:37
BernsteinCorrection is a utility in RooStats to augment a nominal PDF with a polynomial correction te...
Persistable container for RooFit projects.
RooAbsPdf * pdf(RooStringView name) const
Retrieve p.d.f (RooAbsPdf) with given name. A null pointer is returned if not found.
bool import(const RooAbsArg &arg, const RooCmdArg &arg1={}, const RooCmdArg &arg2={}, const RooCmdArg &arg3={}, const RooCmdArg &arg4={}, const RooCmdArg &arg5={}, const RooCmdArg &arg6={}, const RooCmdArg &arg7={}, const RooCmdArg &arg8={}, const RooCmdArg &arg9={})
Import a RooAbsArg object, e.g.
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:2968
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:621
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3066
RooCmdArg Rename(const char *suffix)
RooConstVar & RooConst(double val)
RooCmdArg PrintLevel(Int_t code)
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 JSONIO.h:26
Namespace for the RooStats classes.
Definition Asimov.h:19