Logo ROOT   6.14/05
Reference Guide
rf801_mcstudy.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 #801
5 ///
6 /// A Toy Monte Carlo study that perform cycles of
7 ///
8 /// \macro_image
9 /// \macro_output
10 /// \macro_code
11 /// \author
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 "RooPlot.h"
22 #include "TCanvas.h"
23 #include "TAxis.h"
24 #include "TH2.h"
25 #include "RooFitResult.h"
26 #include "TStyle.h"
27 #include "TDirectory.h"
28 
29 using namespace RooFit ;
30 
31 
32 void rf801_mcstudy()
33 {
34  // C r e a t e m o d e l
35  // -----------------------
36 
37  // Declare observable x
38  RooRealVar x("x","x",0,10) ;
39  x.setBins(40) ;
40 
41  // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters
42  RooRealVar mean("mean","mean of gaussians",5,0,10) ;
43  RooRealVar sigma1("sigma1","width of gaussians",0.5) ;
44  RooRealVar sigma2("sigma2","width of gaussians",1) ;
45 
46  RooGaussian sig1("sig1","Signal component 1",x,mean,sigma1) ;
47  RooGaussian sig2("sig2","Signal component 2",x,mean,sigma2) ;
48 
49  // Build Chebychev polynomial p.d.f.
50  RooRealVar a0("a0","a0",0.5,0.,1.) ;
51  RooRealVar a1("a1","a1",-0.2,-1,1.) ;
52  RooChebychev bkg("bkg","Background",x,RooArgSet(a0,a1)) ;
53 
54  // Sum the signal components into a composite signal p.d.f.
55  RooRealVar sig1frac("sig1frac","fraction of component 1 in signal",0.8,0.,1.) ;
56  RooAddPdf sig("sig","Signal",RooArgList(sig1,sig2),sig1frac) ;
57 
58  // Sum the composite signal and background
59  RooRealVar nbkg("nbkg","number of background events,",150,0,1000) ;
60  RooRealVar nsig("nsig","number of signal events",150,0,1000) ;
61  RooAddPdf model("model","g1+g2+a",RooArgList(bkg,sig),RooArgList(nbkg,nsig)) ;
62 
63 
64 
65  // C r e a t e m a n a g e r
66  // ---------------------------
67 
68  // Instantiate RooMCStudy manager on model with x as observable and given choice of fit options
69  //
70  // The Silence() option kills all messages below the PROGRESS level, leaving only a single message
71  // per sample executed, and any error message that occur during fitting
72  //
73  // The Extended() option has two effects:
74  // 1) The extended ML term is included in the likelihood and
75  // 2) A poisson fluctuation is introduced on the number of generated events
76  //
77  // The FitOptions() given here are passed to the fitting stage of each toy experiment.
78  // If Save() is specified, the fit result of each experiment is saved by the manager
79  //
80  // A Binned() option is added in this example to bin the data between generation and fitting
81  // to speed up the study at the expense of some precision
82 
85 
86 
87  // G e n e r a t e a n d f i t e v e n t s
88  // ---------------------------------------------
89 
90  // Generate and fit 1000 samples of Poisson(nExpected) events
91  mcstudy->generateAndFit(1000) ;
92 
93 
94 
95  // E x p l o r e r e s u l t s o f s t u d y
96  // ------------------------------------------------
97 
98  // Make plots of the distributions of mean, the error on mean and the pull of mean
99  RooPlot* frame1 = mcstudy->plotParam(mean,Bins(40)) ;
100  RooPlot* frame2 = mcstudy->plotError(mean,Bins(40)) ;
101  RooPlot* frame3 = mcstudy->plotPull(mean,Bins(40),FitGauss(kTRUE)) ;
102 
103  // Plot distribution of minimized likelihood
104  RooPlot* frame4 = mcstudy->plotNLL(Bins(40)) ;
105 
106  // Make some histograms from the parameter dataset
107  TH1* hh_cor_a0_s1f = mcstudy->fitParDataSet().createHistogram("hh",a1,YVar(sig1frac)) ;
108  TH1* hh_cor_a0_a1 = mcstudy->fitParDataSet().createHistogram("hh",a0,YVar(a1)) ;
109 
110  // Access some of the saved fit results from individual toys
111  TH2* corrHist000 = mcstudy->fitResult(0)->correlationHist("c000") ;
112  TH2* corrHist127 = mcstudy->fitResult(127)->correlationHist("c127") ;
113  TH2* corrHist953 = mcstudy->fitResult(953)->correlationHist("c953") ;
114 
115 
116 
117  // Draw all plots on a canvas
118  gStyle->SetOptStat(0) ;
119  TCanvas* c = new TCanvas("rf801_mcstudy","rf801_mcstudy",900,900) ;
120  c->Divide(3,3) ;
121  c->cd(1) ; gPad->SetLeftMargin(0.15) ; frame1->GetYaxis()->SetTitleOffset(1.4) ; frame1->Draw() ;
122  c->cd(2) ; gPad->SetLeftMargin(0.15) ; frame2->GetYaxis()->SetTitleOffset(1.4) ; frame2->Draw() ;
123  c->cd(3) ; gPad->SetLeftMargin(0.15) ; frame3->GetYaxis()->SetTitleOffset(1.4) ; frame3->Draw() ;
124  c->cd(4) ; gPad->SetLeftMargin(0.15) ; frame4->GetYaxis()->SetTitleOffset(1.4) ; frame4->Draw() ;
125  c->cd(5) ; gPad->SetLeftMargin(0.15) ; hh_cor_a0_s1f->GetYaxis()->SetTitleOffset(1.4) ; hh_cor_a0_s1f->Draw("box") ;
126  c->cd(6) ; gPad->SetLeftMargin(0.15) ; hh_cor_a0_a1->GetYaxis()->SetTitleOffset(1.4) ; hh_cor_a0_a1->Draw("box") ;
127  c->cd(7) ; gPad->SetLeftMargin(0.15) ; corrHist000->GetYaxis()->SetTitleOffset(1.4) ; corrHist000->Draw("colz") ;
128  c->cd(8) ; gPad->SetLeftMargin(0.15) ; corrHist127->GetYaxis()->SetTitleOffset(1.4) ; corrHist127->Draw("colz") ;
129  c->cd(9) ; gPad->SetLeftMargin(0.15) ; corrHist953->GetYaxis()->SetTitleOffset(1.4) ; corrHist953->Draw("colz") ;
130 
131  // Make RooMCStudy object available on command line after
132  // macro finishes
133  gDirectory->Add(mcstudy) ;
134 }
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
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition: RooAddPdf.h:29
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1117
RooCmdArg Binned(Bool_t flag=kTRUE)
R__EXTERN TStyle * gStyle
Definition: TStyle.h:406
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
RooCmdArg PrintEvalErrors(Int_t numErrors)
RooCmdArg Extended(Bool_t flag=kTRUE)
RooPlot * plotError(const RooRealVar &param, const RooCmdArg &arg1=RooCmdArg::none(), const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none())
Plot the distribution of the fit errors for the specified parameter on a newly created frame...
Double_t x[n]
Definition: legend1.C:17
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
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)
RooCmdArg FitOptions(const char *opts)
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
TH2 * correlationHist(const char *name="correlation_matrix") const
Return TH2D of correlation matrix.
const RooFitResult * fitResult(Int_t sampleNum) const
Return the RooFitResult object of the fit to given sample.
Definition: RooMCStudy.cxx:992
Service class for 2-Dim histogram classes.
Definition: TH2.h:30
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
A RooPlot is a plot frame and a container for graphics objects within that frame. ...
Definition: RooPlot.h:41
RooCmdArg FitGauss(Bool_t flag=kTRUE)
The Canvas class.
Definition: TCanvas.h:31
RooPlot * plotNLL(const RooCmdArg &arg1=RooCmdArg::none(), const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none())
Plot the distribution of the -log(L) values on a newly created frame.
RooCmdArg YVar(const RooAbsRealLValue &var, const RooCmdArg &arg=RooCmdArg::none())
RooPlot * plotPull(const RooRealVar &param, const RooCmdArg &arg1, const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none())
Plot the distribution of pull values for the specified parameter on a newly created frame...
RooPlot * plotParam(const RooRealVar &param, const RooCmdArg &arg1=RooCmdArg::none(), const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none())
Plot the distribution of the fitted value of the given parameter on a newly created frame...
The TH1 histogram class.
Definition: TH1.h:56
RooCmdArg Save(Bool_t flag=kTRUE)
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
RooCmdArg Bins(Int_t nbin)
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition: TStyle.cxx:1444
#define gPad
Definition: TVirtualPad.h:285
#define c(i)
Definition: RSha256.hxx:101
#define gDirectory
Definition: TDirectory.h:213
Chebychev polynomial p.d.f.
Definition: RooChebychev.h:25
const Bool_t kTRUE
Definition: RtypesCore.h:87
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:558