ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
rf802_mcstudy_addons.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// 'VALIDATION AND MC STUDIES' RooFit tutorial macro #802
4 ///
5 /// RooMCStudy: using separate fit and generator models, using the chi^2 calculator model
6 ///
7 ///
8 /// \macro_code
9 /// \author 07/2008 - Wouter Verkerke
10 
11 
12 #ifndef __CINT__
13 #include "RooGlobalFunc.h"
14 #endif
15 #include "RooRealVar.h"
16 #include "RooDataSet.h"
17 #include "RooGaussian.h"
18 #include "RooConstVar.h"
19 #include "RooChebychev.h"
20 #include "RooAddPdf.h"
21 #include "RooMCStudy.h"
22 #include "RooChi2MCSModule.h"
23 #include "RooPlot.h"
24 #include "TCanvas.h"
25 #include "TAxis.h"
26 #include "TH1.h"
27 #include "TDirectory.h"
28 
29 using namespace RooFit ;
30 
31 
32 void rf802_mcstudy_addons()
33 {
34 
35  // C r e a t e m o d e l
36  // -----------------------
37 
38  // Observables, parameters
39  RooRealVar x("x","x",-10,10) ;
40  x.setBins(10) ;
41  RooRealVar mean("mean","mean of gaussian",0) ;
42  RooRealVar sigma("sigma","width of gaussian",5,1,10) ;
43 
44  // Create Gaussian pdf
45  RooGaussian gauss("gauss","gaussian PDF",x,mean,sigma) ;
46 
47 
48 
49  // 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
50  // ----------------------------------------------------------------------------
51 
52  // Create study manager for binned likelihood fits of a Gaussian pdf in 10 bins
53  RooMCStudy* mcs = new RooMCStudy(gauss,x,Silence(),Binned()) ;
54 
55  // Add chi^2 calculator module to mcs
56  RooChi2MCSModule chi2mod ;
57  mcs->addModule(chi2mod) ;
58 
59  // Generate 1000 samples of 1000 events
60  mcs->generateAndFit(2000,1000) ;
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") ;
65  TH1* hist_prob = mcs->fitParDataSet().createHistogram("prob") ;
66 
67 
68 
69  // 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
70  // ----------------------------------------------------------------------------
71 
72  // Create alternate pdf with shifted mean
73  RooRealVar mean2("mean2","mean of gaussian 2",0.5) ;
74  RooGaussian gauss2("gauss2","gaussian PDF2",x,mean2,sigma) ;
75 
76  // Create study manager with separate generation and fit model. This configuration
77  // is set up to generate bad fits as the fit and generator model have different means
78  // and the mean parameter is not floating in the fit
79  RooMCStudy* mcs2 = new RooMCStudy(gauss2,x,FitModel(gauss),Silence(),Binned()) ;
80 
81  // Add chi^2 calculator module to mcs
82  RooChi2MCSModule chi2mod2 ;
83  mcs2->addModule(chi2mod2) ;
84 
85  // Generate 1000 samples of 1000 events
86  mcs2->generateAndFit(2000,1000) ;
87 
88  // Fill histograms with distributions chi2 and prob(chi2,ndf) that
89  // are calculated by RooChiMCSModule
90  TH1* hist2_chi2 = mcs2->fitParDataSet().createHistogram("chi2") ;
91  TH1* hist2_prob = mcs2->fitParDataSet().createHistogram("prob") ;
92  hist2_chi2->SetLineColor(kRed) ;
93  hist2_prob->SetLineColor(kRed) ;
94 
95 
96 
97  TCanvas* c = new TCanvas("rf802_mcstudy_addons","rf802_mcstudy_addons",800,400) ;
98  c->Divide(2) ;
99  c->cd(1) ; gPad->SetLeftMargin(0.15) ; hist_chi2->GetYaxis()->SetTitleOffset(1.4) ; hist_chi2->Draw() ; hist2_chi2->Draw("esame") ;
100  c->cd(2) ; gPad->SetLeftMargin(0.15) ; hist_prob->GetYaxis()->SetTitleOffset(1.4) ; hist_prob->Draw() ; hist2_prob->Draw("esame") ;
101 
102 
103 
104  // Make RooMCStudy object available on command line after
105  // macro finishes
106  gDirectory->Add(mcs) ;
107 }
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:245
friend class RooMCStudy
Definition: RooAbsPdf.h:286
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:61
const RooDataSet & fitParDataSet()
Return a RooDataSet the resulting fit parameters of each toy cycle.
Definition: RooMCStudy.cxx:956
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:37
RooChi2MCSModule is an add-on modules to RooMCStudy that calculates the chi-squared of fitted p...
virtual void SetLineColor(Color_t lcolor)
Definition: TAttLine.h:54
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
TAxis * GetYaxis()
Definition: TH1.h:320
RooCmdArg FitModel(RooAbsPdf &pdf)
The TH1 histogram class.
Definition: TH1.h:80
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.
#define gPad
Definition: TVirtualPad.h:288
#define gDirectory
Definition: TDirectory.h:221
RooAbsMoment * mean(RooRealVar &obs)
Definition: RooAbsReal.h:297