Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 /// \author 07/2008 - Wouter Verkerke
10 
11 #include "RooRealVar.h"
12 #include "RooDataSet.h"
13 #include "RooGaussian.h"
14 #include "RooConstVar.h"
15 #include "RooPolynomial.h"
16 #include "RooAddPdf.h"
17 #include "RooProdPdf.h"
18 #include "RooMCStudy.h"
19 #include "RooPlot.h"
20 #include "TCanvas.h"
21 #include "TAxis.h"
22 #include "TH1.h"
23 using namespace RooFit;
24 
25 void rf804_mcstudy_constr()
26 {
27  // 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
28  // ---------------------------------------------------------------------------
29 
30  // Observable
31  RooRealVar x("x", "x", -10, 10);
32 
33  // Signal component
34  RooRealVar m("m", "m", 0, -10, 10);
35  RooRealVar s("s", "s", 2, 0.1, 10);
36  RooGaussian g("g", "g", x, m, s);
37 
38  // Background component
39  RooPolynomial p("p", "p", x);
40 
41  // Composite model
42  RooRealVar f("f", "f", 0.4, 0., 1.);
43  RooAddPdf sum("sum", "sum", RooArgSet(g, p), f);
44 
45  // Construct constraint on parameter f
46  RooGaussian fconstraint("fconstraint", "fconstraint", f, RooConst(0.7), RooConst(0.1));
47 
48  // Multiply constraint with p.d.f
49  RooProdPdf sumc("sumc", "sum with constraint", RooArgSet(sum, fconstraint));
50 
51  // S e t u p t o y s t u d y w i t h m o d e l
52  // ---------------------------------------------------
53 
54  // Perform toy study with internal constraint on f
55  RooMCStudy mcs(sumc, x, Constrain(f), Silence(), Binned(), FitOptions(PrintLevel(-1)));
56 
57  // Run 500 toys of 2000 events.
58  // Before each toy is generated, a value for the f is sampled from the constraint pdf and
59  // that value is used for the generation of that toy.
60  mcs.generateAndFit(500, 2000);
61 
62  // Make plot of distribution of generated value of f parameter
63  TH1 *h_f_gen = mcs.fitParDataSet().createHistogram("f_gen", -40);
64 
65  // Make plot of distribution of fitted value of f parameter
66  RooPlot *frame1 = mcs.plotParam(f, Bins(40));
67  frame1->SetTitle("Distribution of fitted f values");
68 
69  // Make plot of pull distribution on f
70  RooPlot *frame2 = mcs.plotPull(f, Bins(40), FitGauss());
71  frame1->SetTitle("Distribution of f pull values");
72 
73  TCanvas *c = new TCanvas("rf804_mcstudy_constr", "rf804_mcstudy_constr", 1200, 400);
74  c->Divide(3);
75  c->cd(1);
76  gPad->SetLeftMargin(0.15);
77  h_f_gen->GetYaxis()->SetTitleOffset(1.4);
78  h_f_gen->Draw();
79  c->cd(2);
80  gPad->SetLeftMargin(0.15);
81  frame1->GetYaxis()->SetTitleOffset(1.4);
82  frame1->Draw();
83  c->cd(3);
84  gPad->SetLeftMargin(0.15);
85  frame2->GetYaxis()->SetTitleOffset(1.4);
86  frame2->Draw();
87 }