Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
rf802_mcstudy_addons.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook -js
4 /// Validation and MC studies:
5 /// RooMCStudy - using separate fit and generator models, using the chi^2 calculator model
6 /// Running a biased fit model against an optimal fit.
7 ///
8 /// \macro_image
9 /// \macro_output
10 /// \macro_code
11 /// \author 07/2008 - Wouter Verkerke
12 
13 #include "RooRealVar.h"
14 #include "RooDataSet.h"
15 #include "RooGaussian.h"
16 #include "RooConstVar.h"
17 #include "RooChebychev.h"
18 #include "RooAddPdf.h"
19 #include "RooMCStudy.h"
20 #include "RooChi2MCSModule.h"
21 #include "RooPlot.h"
22 #include "TCanvas.h"
23 #include "TAxis.h"
24 #include "TH1.h"
25 #include "TDirectory.h"
26 #include "TLegend.h"
27 
28 using namespace RooFit;
29 
30 void rf802_mcstudy_addons()
31 {
32 
33  // C r e a t e m o d e l
34  // -----------------------
35 
36  // Observables, parameters
37  RooRealVar x("x", "x", -10, 10);
38  x.setBins(10);
39  RooRealVar mean("mean", "mean of gaussian", 0, -2., 1.8);
40  RooRealVar sigma("sigma", "width of gaussian", 5, 1, 10);
41 
42  // Create Gaussian pdf
43  RooGaussian gauss("gauss", "gaussian PDF", x, mean, sigma);
44 
45  // C r e a t e m a n a g e r w i t h c h i ^ 2 a d d - o n m o d u l e
46  // ----------------------------------------------------------------------------
47 
48  // Create study manager for binned likelihood fits of a Gaussian pdf in 10 bins
49  RooMCStudy *mcs = new RooMCStudy(gauss, x, Silence(), Binned());
50 
51  // Add chi^2 calculator module to mcs
52  RooChi2MCSModule chi2mod;
53  mcs->addModule(chi2mod);
54 
55  // Generate 1000 samples of 1000 events
56  mcs->generateAndFit(2000, 1000);
57 
58  // Fill histograms with distributions chi2 and prob(chi2,ndf) that
59  // are calculated by RooChiMCSModule
60  TH1 *hist_chi2 = mcs->fitParDataSet().createHistogram("chi2");
61  hist_chi2->SetTitle("#chi^{2} values of all toy runs;#chi^{2}");
62  TH1 *hist_prob = mcs->fitParDataSet().createHistogram("prob");
63  hist_prob->SetTitle("Corresponding #chi^{2} probability;Prob(#chi^{2},ndof)");
64 
65 
66  // C r e a t e m a n a g e r w i t h s e p a r a t e f i t m o d e l
67  // ----------------------------------------------------------------------------
68 
69  // Create alternate pdf with shifted mean
70  RooRealVar mean2("mean2", "mean of gaussian 2", 2.);
71  RooGaussian gauss2("gauss2", "gaussian PDF2", x, mean2, sigma);
72 
73  // Create study manager with separate generation and fit model. This configuration
74  // is set up to generate biased fits as the fit and generator model have different means,
75  // and the mean parameter is limited to [-2., 1.8], so it just misses the optimal
76  // mean value of 2 in the data.
77  RooMCStudy *mcs2 = new RooMCStudy(gauss2, x, FitModel(gauss), Silence(), Binned());
78 
79  // Add chi^2 calculator module to mcs
80  RooChi2MCSModule chi2mod2;
81  mcs2->addModule(chi2mod2);
82 
83  // Generate 1000 samples of 1000 events
84  mcs2->generateAndFit(2000, 1000);
85 
86  // Request a the pull plot of mean. The pulls will be one-sided because
87  // `mean` is limited to 1.8.
88  // Note that RooFit will have trouble to compute the pulls because the parameters
89  // are called `mean` in the fit, but `mean2` in the generator model. It is not obvious
90  // that these are related. RooFit will nevertheless compute pulls, but complain that
91  // this is risky.
92  auto pullMeanFrame = mcs2->plotPull(mean);
93 
94  // Fill histograms with distributions chi2 and prob(chi2,ndf) that
95  // are calculated by RooChiMCSModule
96  TH1 *hist2_chi2 = mcs2->fitParDataSet().createHistogram("chi2");
97  TH1 *hist2_prob = mcs2->fitParDataSet().createHistogram("prob");
98  hist2_chi2->SetLineColor(kRed);
99  hist2_prob->SetLineColor(kRed);
100 
101  TLegend leg;
102  leg.AddEntry(hist_chi2, "Optimal fit", "L");
103  leg.AddEntry(hist2_chi2, "Biased fit", "L");
104  leg.SetBorderSize(0);
105  leg.SetFillStyle(0);
106 
107  TCanvas *c = new TCanvas("rf802_mcstudy_addons", "rf802_mcstudy_addons", 800, 400);
108  c->Divide(3);
109  c->cd(1);
110  gPad->SetLeftMargin(0.15);
111  hist_chi2->GetYaxis()->SetTitleOffset(1.4);
112  hist_chi2->Draw();
113  hist2_chi2->Draw("esame");
114  leg.DrawClone();
115  c->cd(2);
116  gPad->SetLeftMargin(0.15);
117  hist_prob->GetYaxis()->SetTitleOffset(1.4);
118  hist_prob->Draw();
119  hist2_prob->Draw("esame");
120  c->cd(3);
121  pullMeanFrame->Draw();
122 
123 
124  // Make RooMCStudy object available on command line after
125  // macro finishes
126  gDirectory->Add(mcs);
127 }