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"
25using namespace RooFit;
26
27void 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}
#define f(i)
Definition: RSha256.hxx:104
#define c(i)
Definition: RSha256.hxx:101
#define g(i)
Definition: RSha256.hxx:105
#define gPad
Definition: TVirtualPad.h:287
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition: RooAddPdf.h:32
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:29
Plain Gaussian p.d.f.
Definition: RooGaussian.h:24
RooMCStudy is a helper class to facilitate Monte Carlo studies such as 'goodness-of-fit' studies,...
Definition: RooMCStudy.h:32
A RooPlot is a plot frame and a container for graphics objects within that frame.
Definition: RooPlot.h:44
void SetTitle(const char *name)
Set the title of the RooPlot to 'title'.
Definition: RooPlot.cxx:1242
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1263
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:691
RooPolynomial implements a polynomial p.d.f of the form.
Definition: RooPolynomial.h:28
RooProdPdf is an efficient implementation of a product of PDFs of the form.
Definition: RooProdPdf.h:37
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:39
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:293
The Canvas class.
Definition: TCanvas.h:23
TH1 is the base class of all histogram classes in ROOT.
Definition: TH1.h:58
TAxis * GetYaxis()
Definition: TH1.h:321
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:3073
RooCmdArg Binned(Bool_t flag=kTRUE)
RooCmdArg FitGauss(Bool_t flag=kTRUE)
RooCmdArg Silence(Bool_t flag=kTRUE)
RooConstVar & RooConst(Double_t val)
RooCmdArg Bins(Int_t nbin)
RooCmdArg Constrain(const RooArgSet &params)
RooCmdArg PrintLevel(Int_t code)
RooCmdArg FitOptions(const char *opts)
Double_t x[n]
Definition: legend1.C:17
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
static constexpr double s
auto * m
Definition: textangle.C:8
static uint64_t sum(uint64_t i)
Definition: Factory.cxx:2345