Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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_code
10/// \macro_output
11///
12/// \date July 2008
13/// \author Wouter Verkerke
14
15#include "RooRealVar.h"
16#include "RooDataSet.h"
17#include "RooGaussian.h"
18#include "RooChebychev.h"
19#include "RooAddPdf.h"
20#include "RooMCStudy.h"
21#include "RooChi2MCSModule.h"
22#include "RooPlot.h"
23#include "TCanvas.h"
24#include "TAxis.h"
25#include "TH1.h"
26#include "TDirectory.h"
27#include "TLegend.h"
28
29using namespace RooFit;
30
32{
33
34 // C r e a t e m o d e l
35 // -----------------------
36
37 // Observables, parameters
38 RooRealVar x("x", "x", -10, 10);
39 x.setBins(10);
40 RooRealVar mean("mean", "mean of gaussian", 0, -2., 1.8);
41 RooRealVar sigma("sigma", "width of gaussian", 5, 1, 10);
42
43 // Create Gaussian pdf
44 RooGaussian gauss("gauss", "gaussian PDF", x, mean, sigma);
45
46 // 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
47 // ----------------------------------------------------------------------------
48
49 // Create study manager for binned likelihood fits of a Gaussian pdf in 10 bins
50 RooMCStudy *mcs = new RooMCStudy(gauss, x, Silence(), Binned());
51
52 // Add chi^2 calculator module to mcs
53 RooChi2MCSModule chi2mod;
54 mcs->addModule(chi2mod);
55
56 // Generate 1000 samples of 1000 events
57 mcs->generateAndFit(2000, 1000);
58
59 // Number of bins for chi2 plots
60 int nBins = 100;
61
62 // Fill histograms with distributions chi2 and prob(chi2,ndf) that
63 // are calculated by RooChiMCSModule
64 TH1 *hist_chi2 = mcs->fitParDataSet().createHistogram("chi2", AutoBinning(nBins));
65 hist_chi2->SetTitle("#chi^{2} values of all toy runs;#chi^{2}");
66 TH1 *hist_prob = mcs->fitParDataSet().createHistogram("prob", AutoBinning(nBins));
67 hist_prob->SetTitle("Corresponding #chi^{2} probability;Prob(#chi^{2},ndof)");
68
69
70 // 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
71 // ----------------------------------------------------------------------------
72
73 // Create alternate pdf with shifted mean
74 RooRealVar mean2("mean2", "mean of gaussian 2", 2.);
75 RooGaussian gauss2("gauss2", "gaussian PDF2", x, mean2, sigma);
76
77 // Create study manager with separate generation and fit model. This configuration
78 // is set up to generate biased fits as the fit and generator model have different means,
79 // and the mean parameter is limited to [-2., 1.8], so it just misses the optimal
80 // mean value of 2 in the data.
81 RooMCStudy *mcs2 = new RooMCStudy(gauss2, x, FitModel(gauss), Silence(), Binned());
82
83 // Add chi^2 calculator module to mcs
84 RooChi2MCSModule chi2mod2;
85 mcs2->addModule(chi2mod2);
86
87 // Generate 1000 samples of 1000 events
88 mcs2->generateAndFit(2000, 1000);
89
90 // Request a the pull plot of mean. The pulls will be one-sided because
91 // `mean` is limited to 1.8.
92 // Note that RooFit will have trouble to compute the pulls because the parameters
93 // are called `mean` in the fit, but `mean2` in the generator model. It is not obvious
94 // that these are related. RooFit will nevertheless compute pulls, but complain that
95 // this is risky.
96 auto pullMeanFrame = mcs2->plotPull(mean);
97
98 // Fill histograms with distributions chi2 and prob(chi2,ndf) that
99 // are calculated by RooChiMCSModule
100 TH1 *hist2_chi2 = mcs2->fitParDataSet().createHistogram("chi2", AutoBinning(nBins));
101 TH1 *hist2_prob = mcs2->fitParDataSet().createHistogram("prob", AutoBinning(nBins));
102 hist2_chi2->SetLineColor(kRed);
103 hist2_prob->SetLineColor(kRed);
104
105 TLegend leg;
106 leg.AddEntry(hist_chi2, "Optimal fit", "L");
107 leg.AddEntry(hist2_chi2, "Biased fit", "L");
108 leg.SetBorderSize(0);
109 leg.SetFillStyle(0);
110
111 TCanvas *c = new TCanvas("rf802_mcstudy_addons", "rf802_mcstudy_addons", 800, 400);
112 c->Divide(3);
113 c->cd(1);
114 gPad->SetLeftMargin(0.15);
115 hist_chi2->GetYaxis()->SetTitleOffset(1.4);
116 hist_chi2->Draw();
117 hist2_chi2->Draw("esame");
118 leg.DrawClone();
119 c->cd(2);
120 gPad->SetLeftMargin(0.15);
121 hist_prob->GetYaxis()->SetTitleOffset(1.4);
122 hist_prob->Draw();
123 hist2_prob->Draw("esame");
124 c->cd(3);
125 pullMeanFrame->Draw();
126
127
128 // Make RooMCStudy object available on command line after
129 // macro finishes
130 gDirectory->Add(mcs);
131}
#define c(i)
Definition RSha256.hxx:101
@ kRed
Definition Rtypes.h:66
#define gDirectory
Definition TDirectory.h:384
#define gPad
TH1 * createHistogram(const char *name, const RooAbsRealLValue &xvar, const RooCmdArg &arg1={}, const RooCmdArg &arg2={}, const RooCmdArg &arg3={}, const RooCmdArg &arg4={}, const RooCmdArg &arg5={}, const RooCmdArg &arg6={}, const RooCmdArg &arg7={}, const RooCmdArg &arg8={}) const
Calls createHistogram(const char *name, const RooAbsRealLValue& xvar, const RooLinkedList& argList) c...
RooChi2MCSModule is an add-on module to RooMCStudy that calculates the chi-squared of fitted p....
Plain Gaussian p.d.f.
Definition RooGaussian.h:24
Helper class to facilitate Monte Carlo studies such as 'goodness-of-fit' studies, that involve fittin...
Definition RooMCStudy.h:32
RooPlot * plotPull(const RooRealVar &param, const RooCmdArg &arg1, const RooCmdArg &arg2={}, const RooCmdArg &arg3={}, const RooCmdArg &arg4={}, const RooCmdArg &arg5={}, const RooCmdArg &arg6={}, const RooCmdArg &arg7={}, const RooCmdArg &arg8={})
Plot the distribution of pull values for the specified parameter on a newly created frame.
const RooDataSet & fitParDataSet()
Return a RooDataSet containing the post-fit parameters of each toy cycle.
bool generateAndFit(Int_t nSamples, Int_t nEvtPerSample=0, bool keepGenData=false, const char *asciiFilePat=nullptr)
Generate and fit 'nSamples' samples of 'nEvtPerSample' events.
void addModule(RooAbsMCStudyModule &module)
Insert given RooMCStudy add-on module to the processing chain of this MCStudy object.
Variable that can be changed from the outside.
Definition RooRealVar.h:37
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition TAttAxis.cxx:298
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
Definition TAttFill.h:39
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
The Canvas class.
Definition TCanvas.h:23
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:59
void SetTitle(const char *title) override
Change/set the title.
Definition TH1.cxx:6709
TAxis * GetYaxis()
Definition TH1.h:325
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3066
This class displays a legend box (TPaveText) containing several legend entries.
Definition TLegend.h:23
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition TLegend.cxx:320
RooCmdArg AutoBinning(Int_t nbins=100, double marginFactor=0.1)
RooCmdArg Silence(bool flag=true)
RooCmdArg Binned(bool flag=true)
const Double_t sigma
Double_t x[n]
Definition legend1.C:17
leg
Definition legend1.C:34
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition JSONIO.h:26
void FitModel(RooWorkspace *, std::string data_name="obsData")