Logo ROOT  
Reference Guide
rf804_mcstudy_constr.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook -js
4 /// Validation and MC studies: using RooMCStudy on models with constrains
5 ///
6 /// \macro_image
7 /// \macro_output
8 /// \macro_code
9 ///
10 /// \date July 2008
11 /// \author Wouter Verkerke
12 
13 #include "RooRealVar.h"
14 #include "RooDataSet.h"
15 #include "RooGaussian.h"
16 #include "RooConstVar.h"
17 #include "RooPolynomial.h"
18 #include "RooAddPdf.h"
19 #include "RooProdPdf.h"
20 #include "RooMCStudy.h"
21 #include "RooPlot.h"
22 #include "TCanvas.h"
23 #include "TAxis.h"
24 #include "TH1.h"
25 using namespace RooFit;
26 
27 void rf804_mcstudy_constr()
28 {
29  // C r e a t e m o d e l w i t h p a r a m e t e r c o n s t r a i n t
30  // ---------------------------------------------------------------------------
31 
32  // Observable
33  RooRealVar x("x", "x", -10, 10);
34 
35  // Signal component
36  RooRealVar m("m", "m", 0, -10, 10);
37  RooRealVar s("s", "s", 2, 0.1, 10);
38  RooGaussian g("g", "g", x, m, s);
39 
40  // Background component
41  RooPolynomial p("p", "p", x);
42 
43  // Composite model
44  RooRealVar f("f", "f", 0.4, 0., 1.);
45  RooAddPdf sum("sum", "sum", RooArgSet(g, p), f);
46 
47  // Construct constraint on parameter f
48  RooGaussian fconstraint("fconstraint", "fconstraint", f, RooConst(0.7), RooConst(0.1));
49 
50  // Multiply constraint with pdf
51  RooProdPdf sumc("sumc", "sum with constraint", RooArgSet(sum, fconstraint));
52 
53  // S e t u p t o y s t u d y w i t h m o d e l
54  // ---------------------------------------------------
55 
56  // Perform toy study with internal constraint on f
57  RooMCStudy mcs(sumc, x, Constrain(f), Silence(), Binned(), FitOptions(PrintLevel(-1)));
58 
59  // Run 500 toys of 2000 events.
60  // Before each toy is generated, a value for the f is sampled from the constraint pdf and
61  // that value is used for the generation of that toy.
62  mcs.generateAndFit(500, 2000);
63 
64  // Make plot of distribution of generated value of f parameter
65  TH1 *h_f_gen = mcs.fitParDataSet().createHistogram("f_gen", -40);
66 
67  // Make plot of distribution of fitted value of f parameter
68  RooPlot *frame1 = mcs.plotParam(f, Bins(40));
69  frame1->SetTitle("Distribution of fitted f values");
70 
71  // Make plot of pull distribution on f
72  RooPlot *frame2 = mcs.plotPull(f, Bins(40), FitGauss());
73  frame1->SetTitle("Distribution of f pull values");
74 
75  TCanvas *c = new TCanvas("rf804_mcstudy_constr", "rf804_mcstudy_constr", 1200, 400);
76  c->Divide(3);
77  c->cd(1);
78  gPad->SetLeftMargin(0.15);
79  h_f_gen->GetYaxis()->SetTitleOffset(1.4);
80  h_f_gen->Draw();
81  c->cd(2);
82  gPad->SetLeftMargin(0.15);
83  frame1->GetYaxis()->SetTitleOffset(1.4);
84  frame1->Draw();
85  c->cd(3);
86  gPad->SetLeftMargin(0.15);
87  frame2->GetYaxis()->SetTitleOffset(1.4);
88  frame2->Draw();
89 }
c
#define c(i)
Definition: RSha256.hxx:101
m
auto * m
Definition: textangle.C:8
RooPlot::Draw
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:691
RooAddPdf
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition: RooAddPdf.h:32
f
#define f(i)
Definition: RSha256.hxx:104
RooFit::Bins
RooCmdArg Bins(Int_t nbin)
Definition: RooGlobalFunc.cxx:177
RooMCStudy.h
RooFit::PrintLevel
RooCmdArg PrintLevel(Int_t code)
Definition: RooGlobalFunc.cxx:192
RooMCStudy
RooMCStudy is a helper class to facilitate Monte Carlo studies such as 'goodness-of-fit' studies,...
Definition: RooMCStudy.h:32
sum
static uint64_t sum(uint64_t i)
Definition: Factory.cxx:2345
RooGaussian.h
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:162
x
Double_t x[n]
Definition: legend1.C:17
RooGaussian
Plain Gaussian p.d.f.
Definition: RooGaussian.h:24
RooAddPdf.h
TCanvas.h
RooDataSet.h
RooFit::FitOptions
RooCmdArg FitOptions(const char *opts)
Definition: RooGlobalFunc.cxx:187
RooFit::FitGauss
RooCmdArg FitGauss(Bool_t flag=kTRUE)
Definition: RooGlobalFunc.cxx:288
RooPolynomial.h
RooProdPdf.h
TH1::GetYaxis
TAxis * GetYaxis()
Definition: TH1.h:321
RooFit
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition: RooCFunction1Binding.h:29
RooPolynomial
RooPolynomial implements a polynomial p.d.f of the form.
Definition: RooPolynomial.h:28
RooFit::Constrain
RooCmdArg Constrain(const RooArgSet &params)
Definition: RooGlobalFunc.cxx:203
RooPlot.h
RooPlot::GetYaxis
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1261
RooPlot
A RooPlot is a plot frame and a container for graphics objects within that frame.
Definition: RooPlot.h:44
RooRealVar.h
RooFit::Binned
RooCmdArg Binned(Bool_t flag=kTRUE)
Definition: RooGlobalFunc.cxx:274
RooConstVar.h
RooPlot::SetTitle
void SetTitle(const char *name)
Set the title of the RooPlot to 'title'.
Definition: RooPlot.cxx:1240
TCanvas
The Canvas class.
Definition: TCanvas.h:23
TAxis.h
TH1
TH1 is the base class of all histogramm classes in ROOT.
Definition: TH1.h:58
gPad
#define gPad
Definition: TVirtualPad.h:287
RooFit::Silence
RooCmdArg Silence(Bool_t flag=kTRUE)
Definition: RooGlobalFunc.cxx:266
TAttAxis::SetTitleOffset
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:293
RooRealVar
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:39
RooProdPdf
RooProdPdf is an efficient implementation of a product of PDFs of the form.
Definition: RooProdPdf.h:37
TH1.h
RooArgSet
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:29
TH1::Draw
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:3069
RooFit::RooConst
RooConstVar & RooConst(Double_t val)
Definition: RooGlobalFunc.cxx:347
g
#define g(i)
Definition: RSha256.hxx:105