Logo ROOT   6.07/09
Reference Guide
rf801_mcstudy.cxx
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////
2 //
3 // 'VALIDATION AND MC STUDIES' RooFit tutorial macro #801
4 //
5 // A Toy Monte Carlo study that perform cycles of
6 // event generation and fittting
7 //
8 //
9 /////////////////////////////////////////////////////////////////////////
10 
11 #ifndef __CINT__
12 #include "RooGlobalFunc.h"
13 #endif
14 #include "RooRealVar.h"
15 #include "RooDataSet.h"
16 #include "RooGaussian.h"
17 #include "RooChebychev.h"
18 #include "RooAddPdf.h"
19 #include "RooMCStudy.h"
20 #include "RooPlot.h"
21 #include "TCanvas.h"
22 #include "TH2.h"
23 #include "RooFitResult.h"
24 #include "TStyle.h"
25 #include "TDirectory.h"
26 
27 using namespace RooFit ;
28 
29 
30 // Elementary operations on a gaussian PDF
31 class TestBasic801 : public RooFitTestUnit
32 {
33 public:
34  TestBasic801(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Automated MC studies",refFile,writeRef,verbose) {} ;
35  Bool_t testCode() {
36 
37  // C r e a t e m o d e l
38  // -----------------------
39 
40  // Declare observable x
41  RooRealVar x("x","x",0,10) ;
42  x.setBins(40) ;
43 
44  // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters
45  RooRealVar mean("mean","mean of gaussians",5,0,10) ;
46  RooRealVar sigma1("sigma1","width of gaussians",0.5) ;
47  RooRealVar sigma2("sigma2","width of gaussians",1) ;
48 
49  RooGaussian sig1("sig1","Signal component 1",x,mean,sigma1) ;
50  RooGaussian sig2("sig2","Signal component 2",x,mean,sigma2) ;
51 
52  // Build Chebychev polynomial p.d.f.
53  RooRealVar a0("a0","a0",0.5,0.,1.) ;
54  RooRealVar a1("a1","a1",-0.2,-1,1.) ;
55  RooChebychev bkg("bkg","Background",x,RooArgSet(a0,a1)) ;
56 
57  // Sum the signal components into a composite signal p.d.f.
58  RooRealVar sig1frac("sig1frac","fraction of component 1 in signal",0.8,0.,1.) ;
59  RooAddPdf sig("sig","Signal",RooArgList(sig1,sig2),sig1frac) ;
60 
61  // Sum the composite signal and background
62  RooRealVar nbkg("nbkg","number of background events,",150,0,1000) ;
63  RooRealVar nsig("nsig","number of signal events",150,0,1000) ;
64  RooAddPdf model("model","g1+g2+a",RooArgList(bkg,sig),RooArgList(nbkg,nsig)) ;
65 
66 
67 
68  // C r e a t e m a n a g e r
69  // ---------------------------
70 
71  // Instantiate RooMCStudy manager on model with x as observable and given choice of fit options
72  //
73  // The Silence() option kills all messages below the PROGRESS level, leaving only a single message
74  // per sample executed, and any error message that occur during fitting
75  //
76  // The Extended() option has two effects:
77  // 1) The extended ML term is included in the likelihood and
78  // 2) A poisson fluctuation is introduced on the number of generated events
79  //
80  // The FitOptions() given here are passed to the fitting stage of each toy experiment.
81  // If Save() is specified, the fit result of each experiment is saved by the manager
82  //
83  // A Binned() option is added in this example to bin the data between generation and fitting
84  // to speed up the study at the expemse of some precision
85 
86  RooMCStudy* mcstudy = new RooMCStudy(model,x,Binned(kTRUE),Silence(),Extended(),
88 
89 
90  // G e n e r a t e a n d f i t e v e n t s
91  // ---------------------------------------------
92 
93  // Generate and fit 100 samples of Poisson(nExpected) events
94  mcstudy->generateAndFit(100) ;
95 
96 
97 
98  // E x p l o r e r e s u l t s o f s t u d y
99  // ------------------------------------------------
100 
101  // Make plots of the distributions of mean, the error on mean and the pull of mean
102  RooPlot* frame1 = mcstudy->plotParam(mean,Bins(40)) ;
103  RooPlot* frame2 = mcstudy->plotError(mean,Bins(40)) ;
104  RooPlot* frame3 = mcstudy->plotPull(mean,Bins(40),FitGauss(kTRUE)) ;
105 
106  // Plot distribution of minimized likelihood
107  RooPlot* frame4 = mcstudy->plotNLL(Bins(40)) ;
108 
109  regPlot(frame1,"rf801_plot1") ;
110  regPlot(frame2,"rf801_plot2") ;
111  regPlot(frame3,"rf801_plot3") ;
112  regPlot(frame4,"rf801_plot4") ;
113 
114  delete mcstudy ;
115 
116  return kTRUE ;
117  }
118 } ;
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
RooCmdArg Binned(Bool_t flag=kTRUE)
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:50
RooCmdArg PrintEvalErrors(Int_t numErrors)
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
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:37
bool verbose
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)
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.
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...
RooCmdArg Save(Bool_t flag=kTRUE)
RooCmdArg Bins(Int_t nbin)
Chebychev polynomial p.d.f.
Definition: RooChebychev.h:25
const Bool_t kTRUE
Definition: Rtypes.h:91