Logo ROOT   6.14/05
Reference Guide
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' RooFit tutorial macro #802
5 ///
6 /// RooMCStudy: using separate fit and generator models, using the chi^2 calculator model
7 ///
8 /// \macro_image
9 /// \macro_output
10 /// \macro_code
11 /// \author 07/2008 - Wouter Verkerke
12 
13 
14 #include "RooRealVar.h"
15 #include "RooDataSet.h"
16 #include "RooGaussian.h"
17 #include "RooConstVar.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 
28 using namespace RooFit ;
29 
30 
31 void rf802_mcstudy_addons()
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) ;
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 
47 
48  // 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
49  // ----------------------------------------------------------------------------
50 
51  // Create study manager for binned likelihood fits of a Gaussian pdf in 10 bins
52  RooMCStudy* mcs = new RooMCStudy(gauss,x,Silence(),Binned()) ;
53 
54  // Add chi^2 calculator module to mcs
55  RooChi2MCSModule chi2mod ;
56  mcs->addModule(chi2mod) ;
57 
58  // Generate 1000 samples of 1000 events
59  mcs->generateAndFit(2000,1000) ;
60 
61  // Fill histograms with distributions chi2 and prob(chi2,ndf) that
62  // are calculated by RooChiMCSModule
63  TH1* hist_chi2 = mcs->fitParDataSet().createHistogram("chi2") ;
64  TH1* hist_prob = mcs->fitParDataSet().createHistogram("prob") ;
65 
66 
67 
68  // 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
69  // ----------------------------------------------------------------------------
70 
71  // Create alternate pdf with shifted mean
72  RooRealVar mean2("mean2","mean of gaussian 2",0.5) ;
73  RooGaussian gauss2("gauss2","gaussian PDF2",x,mean2,sigma) ;
74 
75  // Create study manager with separate generation and fit model. This configuration
76  // is set up to generate bad fits as the fit and generator model have different means
77  // and the mean parameter is not floating in the fit
78  RooMCStudy* mcs2 = new RooMCStudy(gauss2,x,FitModel(gauss),Silence(),Binned()) ;
79 
80  // Add chi^2 calculator module to mcs
81  RooChi2MCSModule chi2mod2 ;
82  mcs2->addModule(chi2mod2) ;
83 
84  // Generate 1000 samples of 1000 events
85  mcs2->generateAndFit(2000,1000) ;
86 
87  // Fill histograms with distributions chi2 and prob(chi2,ndf) that
88  // are calculated by RooChiMCSModule
89  TH1* hist2_chi2 = mcs2->fitParDataSet().createHistogram("chi2") ;
90  TH1* hist2_prob = mcs2->fitParDataSet().createHistogram("prob") ;
91  hist2_chi2->SetLineColor(kRed) ;
92  hist2_prob->SetLineColor(kRed) ;
93 
94 
95 
96  TCanvas* c = new TCanvas("rf802_mcstudy_addons","rf802_mcstudy_addons",800,400) ;
97  c->Divide(2) ;
98  c->cd(1) ; gPad->SetLeftMargin(0.15) ; hist_chi2->GetYaxis()->SetTitleOffset(1.4) ; hist_chi2->Draw() ; hist2_chi2->Draw("esame") ;
99  c->cd(2) ; gPad->SetLeftMargin(0.15) ; hist_prob->GetYaxis()->SetTitleOffset(1.4) ; hist_prob->Draw() ; hist2_prob->Draw("esame") ;
100 
101 
102 
103  // Make RooMCStudy object available on command line after
104  // macro finishes
105  gDirectory->Add(mcs) ;
106 }
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title Offset is a correction factor with respect to the "s...
Definition: TAttAxis.cxx:294
RooMCStudy is a help class to facilitate Monte Carlo studies such as 'goodness-of-fit' studies...
Definition: RooMCStudy.h:32
RooCmdArg Binned(Bool_t flag=kTRUE)
Definition: Rtypes.h:59
const RooDataSet & fitParDataSet()
Return a RooDataSet the resulting fit parameters of each toy cycle.
Definition: RooMCStudy.cxx:956
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:688
Double_t x[n]
Definition: legend1.C:17
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
void addModule(RooAbsMCStudyModule &module)
Insert given RooMCStudy add-on module to the processing chain of this MCStudy object.
Definition: RooMCStudy.cxx:438
Bool_t generateAndFit(Int_t nSamples, Int_t nEvtPerSample=0, Bool_t keepGenData=kFALSE, const char *asciiFilePat=0)
Generate and fit 'nSamples' samples of 'nEvtPerSample' events.
Definition: RooMCStudy.cxx:653
RooCmdArg Silence(Bool_t flag=kTRUE)
const Double_t sigma
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
RooChi2MCSModule is an add-on modules to RooMCStudy that calculates the chi-squared of fitted p...
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2974
TH2F * createHistogram(const RooAbsRealLValue &var1, const RooAbsRealLValue &var2, const char *cuts="", const char *name="hist") const
Create a TH2F histogram of the distribution of the specified variable using this dataset.
TAxis * GetYaxis()
Definition: TH1.h:316
The Canvas class.
Definition: TCanvas.h:31
static constexpr double gauss
RooCmdArg FitModel(RooAbsPdf &pdf)
The TH1 histogram class.
Definition: TH1.h:56
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1162
#define gPad
Definition: TVirtualPad.h:285
#define c(i)
Definition: RSha256.hxx:101
#define gDirectory
Definition: TDirectory.h:213