Logo ROOT   6.08/07
Reference Guide
rf501_simultaneouspdf.cxx
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////
2 //
3 // 'ORGANIZATION AND SIMULTANEOUS FITS' RooFit tutorial macro #501
4 //
5 // Using simultaneous p.d.f.s to describe simultaneous fits to multiple
6 // datasets
7 //
8 //
9 //
10 // 07/2008 - Wouter Verkerke
11 //
12 /////////////////////////////////////////////////////////////////////////
13 
14 #ifndef __CINT__
15 #include "RooGlobalFunc.h"
16 #endif
17 #include "RooRealVar.h"
18 #include "RooDataSet.h"
19 #include "RooGaussian.h"
20 #include "RooChebychev.h"
21 #include "RooAddPdf.h"
22 #include "RooSimultaneous.h"
23 #include "RooCategory.h"
24 #include "TCanvas.h"
25 #include "RooPlot.h"
26 using namespace RooFit ;
27 
28 
29 class TestBasic501 : public RooFitTestUnit
30 {
31 public:
32  TestBasic501(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Simultaneous p.d.f. operator",refFile,writeRef,verbose) {} ;
33  Bool_t testCode() {
34 
35  // C r e a t e m o d e l f o r p h y s i c s s a m p l e
36  // -------------------------------------------------------------
37 
38  // Create observables
39  RooRealVar x("x","x",-8,8) ;
40 
41  // Construct signal pdf
42  RooRealVar mean("mean","mean",0,-8,8) ;
43  RooRealVar sigma("sigma","sigma",0.3,0.1,10) ;
44  RooGaussian gx("gx","gx",x,mean,sigma) ;
45 
46  // Construct background pdf
47  RooRealVar a0("a0","a0",-0.1,-1,1) ;
48  RooRealVar a1("a1","a1",0.004,-1,1) ;
49  RooChebychev px("px","px",x,RooArgSet(a0,a1)) ;
50 
51  // Construct composite pdf
52  RooRealVar f("f","f",0.2,0.,1.) ;
53  RooAddPdf model("model","model",RooArgList(gx,px),f) ;
54 
55 
56 
57  // C r e a t e m o d e l f o r c o n t r o l s a m p l e
58  // --------------------------------------------------------------
59 
60  // Construct signal pdf.
61  // NOTE that sigma is shared with the signal sample model
62  RooRealVar mean_ctl("mean_ctl","mean_ctl",-3,-8,8) ;
63  RooGaussian gx_ctl("gx_ctl","gx_ctl",x,mean_ctl,sigma) ;
64 
65  // Construct the background pdf
66  RooRealVar a0_ctl("a0_ctl","a0_ctl",-0.1,-1,1) ;
67  RooRealVar a1_ctl("a1_ctl","a1_ctl",0.5,-0.1,1) ;
68  RooChebychev px_ctl("px_ctl","px_ctl",x,RooArgSet(a0_ctl,a1_ctl)) ;
69 
70  // Construct the composite model
71  RooRealVar f_ctl("f_ctl","f_ctl",0.5,0.,1.) ;
72  RooAddPdf model_ctl("model_ctl","model_ctl",RooArgList(gx_ctl,px_ctl),f_ctl) ;
73 
74 
75 
76  // G e n e r a t e e v e n t s f o r b o t h s a m p l e s
77  // ---------------------------------------------------------------
78 
79  // Generate 1000 events in x and y from model
80  RooDataSet *data = model.generate(RooArgSet(x),100) ;
81  RooDataSet *data_ctl = model_ctl.generate(RooArgSet(x),2000) ;
82 
83 
84 
85  // C r e a t e i n d e x c a t e g o r y a n d j o i n s a m p l e s
86  // ---------------------------------------------------------------------------
87 
88  // Define category to distinguish physics and control samples events
89  RooCategory sample("sample","sample") ;
90  sample.defineType("physics") ;
91  sample.defineType("control") ;
92 
93  // Construct combined dataset in (x,sample)
94  RooDataSet combData("combData","combined data",x,Index(sample),Import("physics",*data),Import("control",*data_ctl)) ;
95 
96 
97 
98  // C o n s t r u c t a s i m u l t a n e o u s p d f i n ( x , s a m p l e )
99  // -----------------------------------------------------------------------------------
100 
101  // Construct a simultaneous pdf using category sample as index
102  RooSimultaneous simPdf("simPdf","simultaneous pdf",sample) ;
103 
104  // Associate model with the physics state and model_ctl with the control state
105  simPdf.addPdf(model,"physics") ;
106  simPdf.addPdf(model_ctl,"control") ;
107 
108 
109 
110  // P e r f o r m a s i m u l t a n e o u s f i t
111  // ---------------------------------------------------
112 
113  // Perform simultaneous fit of model to data and model_ctl to data_ctl
114  simPdf.fitTo(combData) ;
115 
116 
117 
118  // P l o t m o d e l s l i c e s o n d a t a s l i c e s
119  // ----------------------------------------------------------------
120 
121  // Make a frame for the physics sample
122  RooPlot* frame1 = x.frame(Bins(30),Title("Physics sample")) ;
123 
124  // Plot all data tagged as physics sample
125  combData.plotOn(frame1,Cut("sample==sample::physics")) ;
126 
127  // Plot "physics" slice of simultaneous pdf.
128  // NBL You _must_ project the sample index category with data using ProjWData
129  // as a RooSimultaneous makes no prediction on the shape in the index category
130  // and can thus not be integrated
131  simPdf.plotOn(frame1,Slice(sample,"physics"),ProjWData(sample,combData)) ;
132  simPdf.plotOn(frame1,Slice(sample,"physics"),Components("px"),ProjWData(sample,combData),LineStyle(kDashed)) ;
133 
134  // The same plot for the control sample slice
135  RooPlot* frame2 = x.frame(Bins(30),Title("Control sample")) ;
136  combData.plotOn(frame2,Cut("sample==sample::control")) ;
137  simPdf.plotOn(frame2,Slice(sample,"control"),ProjWData(sample,combData)) ;
138  simPdf.plotOn(frame2,Slice(sample,"control"),Components("px_ctl"),ProjWData(sample,combData),LineStyle(kDashed)) ;
139 
140 
141  regPlot(frame1,"rf501_plot1") ;
142  regPlot(frame2,"rf501_plot2") ;
143 
144  delete data ;
145  delete data_ctl ;
146 
147  return kTRUE ;
148 
149  }
150 
151 } ;
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition: RooAddPdf.h:29
RooCmdArg Cut(const char *cutSpec)
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
RooCmdArg ProjWData(const RooAbsData &projData, Bool_t binData=kFALSE)
RooCmdArg Title(const char *name)
Double_t x[n]
Definition: legend1.C:17
RooCmdArg LineStyle(Style_t style)
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
const Double_t sigma
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:37
bool verbose
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
RooCategory represents a fundamental (non-derived) discrete value object.
Definition: RooCategory.h:25
A RooPlot is a plot frame and a container for graphics objects within that frame. ...
Definition: RooPlot.h:41
RooCmdArg Import(const char *state, TH1 &histo)
RooCmdArg Index(RooCategory &icat)
double f(double x)
RooCmdArg Components(const RooArgSet &compSet)
RooCmdArg Bins(Int_t nbin)
Chebychev polynomial p.d.f.
Definition: RooChebychev.h:25
RooCmdArg Slice(const RooArgSet &sliceSet)
const Bool_t kTRUE
Definition: Rtypes.h:91
RooSimultaneous facilitates simultaneous fitting of multiple PDFs to subsets of a given dataset...