Logo ROOT   6.10/09
Reference Guide
rf802_mcstudy_addons.cxx
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////
2 //
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 // 07/2008 - Wouter Verkerke
9 //
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 "RooChebychev.h"
19 #include "RooAddPdf.h"
20 #include "RooMCStudy.h"
21 #include "RooChi2MCSModule.h"
22 #include "RooPlot.h"
23 #include "TCanvas.h"
24 #include "TH1.h"
25 #include "TDirectory.h"
26 
27 using namespace RooFit ;
28 
29 
30 // Elementary operations on a gaussian PDF
31 class TestBasic802 : public RooFitTestUnit
32 {
33 public:
34  TestBasic802(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("MC Study with chi^2 calculator",refFile,writeRef,verbose) {} ;
35  Bool_t testCode() {
36 
37  // C r e a t e m o d e l
38  // -----------------------
39 
40  // Observables, parameters
41  RooRealVar x("x","x",-10,10) ;
42  x.setBins(10) ;
43  RooRealVar mean("mean","mean of gaussian",0) ;
44  RooRealVar sigma("sigma","width of gaussian",5,1,10) ;
45 
46  // Create Gaussian pdf
47  RooGaussian gauss("gauss","gaussian PDF",x,mean,sigma) ;
48 
49 
50 
51  // 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
52  // ----------------------------------------------------------------------------
53 
54  // Create study manager for binned likelihood fits of a Gaussian pdf in 10 bins
55  RooMCStudy* mcs = new RooMCStudy(gauss,x,Silence(),Binned()) ;
56 
57  // Add chi^2 calculator module to mcs
58  RooChi2MCSModule chi2mod ;
59  mcs->addModule(chi2mod) ;
60 
61  // Generate 200 samples of 1000 events
62  mcs->generateAndFit(200,1000) ;
63 
64  // Fill histograms with distributions chi2 and prob(chi2,ndf) that
65  // are calculated by RooChiMCSModule
66 
67  RooRealVar* chi2 = (RooRealVar*) mcs->fitParDataSet().get()->find("chi2") ;
68  RooRealVar* prob = (RooRealVar*) mcs->fitParDataSet().get()->find("prob") ;
69 
70  TH1* h_chi2 = new TH1F("h_chi2","",40,0,20) ;
71  TH1* h_prob = new TH1F("h_prob","",40,0,1) ;
72 
73  mcs->fitParDataSet().fillHistogram(h_chi2,*chi2) ;
74  mcs->fitParDataSet().fillHistogram(h_prob,*prob) ;
75 
76 
77 
78  // 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
79  // ----------------------------------------------------------------------------
80 
81  // Create alternate pdf with shifted mean
82  RooRealVar mean2("mean2","mean of gaussian 2",0.5) ;
83  RooGaussian gauss2("gauss2","gaussian PDF2",x,mean2,sigma) ;
84 
85  // Create study manager with separate generation and fit model. This configuration
86  // is set up to generate bad fits as the fit and generator model have different means
87  // and the mean parameter is not floating in the fit
88  RooMCStudy* mcs2 = new RooMCStudy(gauss2,x,FitModel(gauss),Silence(),Binned()) ;
89 
90  // Add chi^2 calculator module to mcs
91  RooChi2MCSModule chi2mod2 ;
92  mcs2->addModule(chi2mod2) ;
93 
94  // Generate 200 samples of 1000 events
95  mcs2->generateAndFit(200,1000) ;
96 
97  // Fill histograms with distributions chi2 and prob(chi2,ndf) that
98  // are calculated by RooChiMCSModule
99 
100  TH1* h2_chi2 = new TH1F("h2_chi2","",40,0,20) ;
101  TH1* h2_prob = new TH1F("h2_prob","",40,0,1) ;
102 
103  mcs2->fitParDataSet().fillHistogram(h2_chi2,*chi2) ;
104  mcs2->fitParDataSet().fillHistogram(h2_prob,*prob) ;
105 
106  h_chi2->SetLineColor(kRed) ;
107  h_prob->SetLineColor(kRed) ;
108 
109  regTH(h_chi2,"rf802_hist_chi2") ;
110  regTH(h2_chi2,"rf802_hist2_chi2") ;
111  regTH(h_prob,"rf802_hist_prob") ;
112  regTH(h2_prob,"rf802_hist2_prob") ;
113 
114  delete mcs ;
115  delete mcs2 ;
116 
117  return kTRUE ;
118  }
119 } ;
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:56
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:311
const RooDataSet & fitParDataSet()
Return a RooDataSet the resulting fit parameters of each toy cycle.
Definition: RooMCStudy.cxx:956
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
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 &#39;nSamples&#39; samples of &#39;nEvtPerSample&#39; 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
bool verbose
virtual const RooArgSet * get(Int_t index) const
Return RooArgSet with coordinates of event &#39;index&#39;.
virtual TH1 * fillHistogram(TH1 *hist, const RooArgList &plotVars, const char *cuts="", const char *cutRange=0) const
Loop over columns of our tree data and fill the input histogram.
RooAbsArg * find(const char *name) const
Find object with given name in list.
RooCmdArg FitModel(RooAbsPdf &pdf)
The TH1 histogram class.
Definition: TH1.h:56
const Bool_t kTRUE
Definition: RtypesCore.h:91