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/// Validation and MC studies: using RooMCStudy on models with constrains
6///
7/// \macro_image
8/// \macro_output
9/// \macro_code
10///
11/// \date 07/2008
12/// \author Wouter Verkerke
13
14#include "RooRealVar.h"
15#include "RooDataSet.h"
16#include "RooGaussian.h"
17#include "RooConstVar.h"
18#include "RooPolynomial.h"
20#include "RooProdPdf.h"
21#include "RooMCStudy.h"
22#include "RooPlot.h"
23#include "TCanvas.h"
24#include "TAxis.h"
25#include "TH1.h"
26using namespace RooFit;
27
28void rf804_mcstudy_constr()
29{
30 // 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
31 // ---------------------------------------------------------------------------
32
33 // Observable
34 RooRealVar x("x", "x", -10, 10);
35
36 // Signal component
37 RooRealVar m("m", "m", 0, -10, 10);
38 RooRealVar s("s", "s", 2, 0.1, 10);
39 RooGaussian g("g", "g", x, m, s);
40
41 // Background component
42 RooPolynomial p("p", "p", x);
43
44 // Composite model
45 RooRealVar f("f", "f", 0.4, 0., 1.);
46 RooAddPdf sum("sum", "sum", RooArgSet(g, p), f);
47
48 // Construct constraint on parameter f
49 RooGaussian fconstraint("fconstraint", "fconstraint", f, RooConst(0.7), RooConst(0.1));
50
51 // Multiply constraint with p.d.f
52 RooProdPdf sumc("sumc", "sum with constraint", RooArgSet(sum, fconstraint));
53
54 // S e t u p t o y s t u d y w i t h m o d e l
55 // ---------------------------------------------------
56
57 // Perform toy study with internal constraint on f
58 RooMCStudy mcs(sumc, x, Constrain(f), Silence(), Binned(), FitOptions(PrintLevel(-1)));
59
60 // Run 500 toys of 2000 events.
61 // Before each toy is generated, a value for the f is sampled from the constraint pdf and
62 // that value is used for the generation of that toy.
63 mcs.generateAndFit(500, 2000);
64
65 // Make plot of distribution of generated value of f parameter
66 TH1 *h_f_gen = mcs.fitParDataSet().createHistogram("f_gen", -40);
67
68 // Make plot of distribution of fitted value of f parameter
69 RooPlot *frame1 = mcs.plotParam(f, Bins(40));
70 frame1->SetTitle("Distribution of fitted f values");
71
72 // Make plot of pull distribution on f
73 RooPlot *frame2 = mcs.plotPull(f, Bins(40), FitGauss());
74 frame1->SetTitle("Distribution of f pull values");
75
76 TCanvas *c = new TCanvas("rf804_mcstudy_constr", "rf804_mcstudy_constr", 1200, 400);
77 c->Divide(3);
78 c->cd(1);
80 h_f_gen->GetYaxis()->SetTitleOffset(1.4);
81 h_f_gen->Draw();
82 c->cd(2);
84 frame1->GetYaxis()->SetTitleOffset(1.4);
85 frame1->Draw();
86 c->cd(3);
88 frame2->GetYaxis()->SetTitleOffset(1.4);
89 frame2->Draw();
90}
#define f(i)
Definition: RSha256.hxx:104
#define c(i)
Definition: RSha256.hxx:101
#define g(i)
Definition: RSha256.hxx:105
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
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:1258
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1277
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:712
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:31
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:35
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:294
The Canvas class.
Definition: TCanvas.h:27
The TH1 histogram class.
Definition: TH1.h:56
TAxis * GetYaxis()
Definition: TH1.h:317
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2998
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 long int sum(long int i)
Definition: Factory.cxx:2275