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 ///
5 ///
6 /// \brief Validation and MC studies: using RooMCStudy on models with constrains
7 ///
8 /// \macro_image
9 /// \macro_output
10 /// \macro_code
11 ///
12 /// \date 07/2008
13 /// \author Wouter Verkerke
14 
15 #include "RooRealVar.h"
16 #include "RooDataSet.h"
17 #include "RooGaussian.h"
18 #include "RooConstVar.h"
19 #include "RooPolynomial.h"
20 #include "RooAddPdf.h"
21 #include "RooProdPdf.h"
22 #include "RooMCStudy.h"
23 #include "RooPlot.h"
24 #include "TCanvas.h"
25 #include "TAxis.h"
26 #include "TH1.h"
27 using namespace RooFit;
28 
29 void rf804_mcstudy_constr()
30 {
31  // 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
32  // ---------------------------------------------------------------------------
33 
34  // Observable
35  RooRealVar x("x", "x", -10, 10);
36 
37  // Signal component
38  RooRealVar m("m", "m", 0, -10, 10);
39  RooRealVar s("s", "s", 2, 0.1, 10);
40  RooGaussian g("g", "g", x, m, s);
41 
42  // Background component
43  RooPolynomial p("p", "p", x);
44 
45  // Composite model
46  RooRealVar f("f", "f", 0.4, 0., 1.);
47  RooAddPdf sum("sum", "sum", RooArgSet(g, p), f);
48 
49  // Construct constraint on parameter f
50  RooGaussian fconstraint("fconstraint", "fconstraint", f, RooConst(0.7), RooConst(0.1));
51 
52  // Multiply constraint with p.d.f
53  RooProdPdf sumc("sumc", "sum with constraint", RooArgSet(sum, fconstraint));
54 
55  // S e t u p t o y s t u d y w i t h m o d e l
56  // ---------------------------------------------------
57 
58  // Perform toy study with internal constraint on f
59  RooMCStudy mcs(sumc, x, Constrain(f), Silence(), Binned(), FitOptions(PrintLevel(-1)));
60 
61  // Run 500 toys of 2000 events.
62  // Before each toy is generated, a value for the f is sampled from the constraint pdf and
63  // that value is used for the generation of that toy.
64  mcs.generateAndFit(500, 2000);
65 
66  // Make plot of distribution of generated value of f parameter
67  TH1 *h_f_gen = mcs.fitParDataSet().createHistogram("f_gen", -40);
68 
69  // Make plot of distribution of fitted value of f parameter
70  RooPlot *frame1 = mcs.plotParam(f, Bins(40));
71  frame1->SetTitle("Distribution of fitted f values");
72 
73  // Make plot of pull distribution on f
74  RooPlot *frame2 = mcs.plotPull(f, Bins(40), FitGauss());
75  frame1->SetTitle("Distribution of f pull values");
76 
77  TCanvas *c = new TCanvas("rf804_mcstudy_constr", "rf804_mcstudy_constr", 1200, 400);
78  c->Divide(3);
79  c->cd(1);
80  gPad->SetLeftMargin(0.15);
81  h_f_gen->GetYaxis()->SetTitleOffset(1.4);
82  h_f_gen->Draw();
83  c->cd(2);
84  gPad->SetLeftMargin(0.15);
85  frame1->GetYaxis()->SetTitleOffset(1.4);
86  frame1->Draw();
87  c->cd(3);
88  gPad->SetLeftMargin(0.15);
89  frame2->GetYaxis()->SetTitleOffset(1.4);
90  frame2->Draw();
91 }
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:174
RooMCStudy.h
RooFit::PrintLevel
RooCmdArg PrintLevel(Int_t code)
Definition: RooGlobalFunc.cxx:189
RooMCStudy
RooMCStudy is a helper class to facilitate Monte Carlo studies such as 'goodness-of-fit' studies,...
Definition: RooMCStudy.h:32
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:25
RooAddPdf.h
TCanvas.h
RooDataSet.h
RooFit::FitOptions
RooCmdArg FitOptions(const char *opts)
Definition: RooGlobalFunc.cxx:184
RooFit::FitGauss
RooCmdArg FitGauss(Bool_t flag=kTRUE)
Definition: RooGlobalFunc.cxx:282
RooPolynomial.h
RooProdPdf.h
TH1::GetYaxis
TAxis * GetYaxis()
Definition: TH1.h:319
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:200
RooPlot.h
RooPlot::GetYaxis
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1258
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:268
RooConstVar.h
sum
static long int sum(long int i)
Definition: Factory.cxx:2272
RooPlot::SetTitle
void SetTitle(const char *name)
Set the title of the RooPlot to 'title'.
Definition: RooPlot.cxx:1237
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:260
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:36
RooProdPdf
RooProdPdf is an efficient implementation of a product of PDFs of the form.
Definition: RooProdPdf.h:33
TH1.h
RooArgSet
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
TH1::Draw
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:3050
RooFit::RooConst
RooConstVar & RooConst(Double_t val)
Definition: RooGlobalFunc.cxx:341
g
#define g(i)
Definition: RSha256.hxx:105