Logo ROOT   6.10/09
Reference Guide
rf599_wspacepersist.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 "RooWorkspace.h"
18 #include "RooProdPdf.h"
19 #include "RooRealVar.h"
20 #include "RooDataSet.h"
21 #include "RooGaussian.h"
22 #include "RooGaussModel.h"
23 #include "RooAddModel.h"
24 #include "RooDecay.h"
25 #include "RooChebychev.h"
26 #include "RooAddPdf.h"
27 #include "RooSimultaneous.h"
28 #include "RooCategory.h"
29 #include "TCanvas.h"
30 #include "RooPlot.h"
31 using namespace RooFit ;
32 
33 
34 class TestBasic599 : public RooFitTestUnit
35 {
36 public:
37  TestBasic599(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Workspace and p.d.f. persistence",refFile,writeRef,verbose) {} ;
38  Bool_t testCode() {
39 
40  if (_write) {
41 
42  RooWorkspace *w = new RooWorkspace("TestBasic11_ws") ;
43 
44  regWS(w,"Basic11_ws") ;
45 
46  // Build Gaussian PDF in X
47  RooRealVar x("x","x",-10,10) ;
48  RooRealVar meanx("meanx","mean of gaussian",-1) ;
49  RooRealVar sigmax("sigmax","width of gaussian",3) ;
50  RooGaussian gaussx("gaussx","gaussian PDF",x,meanx,sigmax) ;
51 
52  // Build Gaussian PDF in Y
53  RooRealVar y("y","y",-10,10) ;
54  RooRealVar meany("meany","mean of gaussian",-1) ;
55  RooRealVar sigmay("sigmay","width of gaussian",3) ;
56  RooGaussian gaussy("gaussy","gaussian PDF",y,meany,sigmay) ;
57 
58  // Make product of X and Y
59  RooProdPdf gaussxy("gaussxy","gaussx*gaussy",RooArgSet(gaussx,gaussy)) ;
60 
61  // Make flat bkg in X and Y
62  RooPolynomial flatx("flatx","flatx",x) ;
63  RooPolynomial flaty("flaty","flaty",x) ;
64  RooProdPdf flatxy("flatxy","flatx*flaty",RooArgSet(flatx,flaty)) ;
65 
66  // Make sum of gaussxy and flatxy
67  RooRealVar frac("frac","frac",0.5,0.,1.) ;
68  RooAddPdf sumxy("sumxy","sumxy",RooArgList(gaussxy,flatxy),frac) ;
69 
70  // Store p.d.f in workspace
71  w->import(gaussx) ;
72  w->import(gaussxy,RenameConflictNodes("set2")) ;
73  w->import(sumxy,RenameConflictNodes("set3")) ;
74 
75  // Make reference plot of GaussX
76  RooPlot* frame1 = x.frame() ;
77  gaussx.plotOn(frame1) ;
78  regPlot(frame1,"Basic11_gaussx_framex") ;
79 
80  // Make reference plots for GaussXY
81  RooPlot* frame2 = x.frame() ;
82  gaussxy.plotOn(frame2) ;
83  regPlot(frame2,"Basic11_gaussxy_framex") ;
84 
85  RooPlot* frame3 = y.frame() ;
86  gaussxy.plotOn(frame3) ;
87  regPlot(frame3,"Basic11_gaussxy_framey") ;
88 
89  // Make reference plots for SumXY
90  RooPlot* frame4 = x.frame() ;
91  sumxy.plotOn(frame4) ;
92  regPlot(frame4,"Basic11_sumxy_framex") ;
93 
94  RooPlot* frame5 = y.frame() ;
95  sumxy.plotOn(frame5) ;
96  regPlot(frame5,"Basic11_sumxy_framey") ;
97 
98  // Analytically convolved p.d.f.s
99 
100  // Build a simple decay PDF
101  RooRealVar dt("dt","dt",-20,20) ;
102  RooRealVar tau("tau","tau",1.548) ;
103 
104  // Build a gaussian resolution model
105  RooRealVar bias1("bias1","bias1",0) ;
106  RooRealVar sigma1("sigma1","sigma1",1) ;
107  RooGaussModel gm1("gm1","gauss model 1",dt,bias1,sigma1) ;
108 
109  // Construct a decay PDF, smeared with single gaussian resolution model
110  RooDecay decay_gm1("decay_gm1","decay",dt,tau,gm1,RooDecay::DoubleSided) ;
111 
112  // Build another gaussian resolution model
113  RooRealVar bias2("bias2","bias2",0) ;
114  RooRealVar sigma2("sigma2","sigma2",5) ;
115  RooGaussModel gm2("gm2","gauss model 2",dt,bias2,sigma2) ;
116 
117  // Build a composite resolution model
118  RooRealVar gm1frac("gm1frac","fraction of gm1",0.5) ;
119  RooAddModel gmsum("gmsum","sum of gm1 and gm2",RooArgList(gm1,gm2),gm1frac) ;
120 
121  // Construct a decay PDF, smeared with double gaussian resolution model
122  RooDecay decay_gmsum("decay_gmsum","decay",dt,tau,gmsum,RooDecay::DoubleSided) ;
123 
124  w->import(decay_gm1) ;
125  w->import(decay_gmsum,RenameConflictNodes("set3")) ;
126 
127  RooPlot* frame6 = dt.frame() ;
128  decay_gm1.plotOn(frame6) ;
129  regPlot(frame6,"Basic11_decay_gm1_framedt") ;
130 
131  RooPlot* frame7 = dt.frame() ;
132  decay_gmsum.plotOn(frame7) ;
133  regPlot(frame7,"Basic11_decay_gmsum_framedt") ;
134 
135  // Construct simultaneous p.d.f
136  RooCategory cat("cat","cat") ;
137  cat.defineType("A") ;
138  cat.defineType("B") ;
139  RooSimultaneous sim("sim","sim",cat) ;
140  sim.addPdf(gaussxy,"A") ;
141  sim.addPdf(flatxy,"B") ;
142 
143  w->import(sim,RenameConflictNodes("set4")) ;
144 
145  // Make plot with dummy dataset for index projection
146  RooDataHist dh("dh","dh",cat) ;
147  cat.setLabel("A") ;
148  dh.add(cat) ;
149  cat.setLabel("B") ;
150  dh.add(cat) ;
151 
152  RooPlot* frame8 = x.frame() ;
153  sim.plotOn(frame8,ProjWData(cat,dh),Project(cat)) ;
154 
155  regPlot(frame8,"Basic11_sim_framex") ;
156 
157 
158  } else {
159 
160  RooWorkspace* w = getWS("Basic11_ws") ;
161  if (!w) return kFALSE ;
162 
163  // Retrieve p.d.f from workspace
164  RooAbsPdf* gaussx = w->pdf("gaussx") ;
165 
166  // Make test plot and offer for comparison against ref plot
167  RooPlot* frame1 = w->var("x")->frame() ;
168  gaussx->plotOn(frame1) ;
169  regPlot(frame1,"Basic11_gaussx_framex") ;
170 
171  // Retrieve p.d.f from workspace
172  RooAbsPdf* gaussxy = w->pdf("gaussxy") ;
173 
174  // Make test plot and offer for comparison against ref plot
175  RooPlot* frame2 = w->var("x")->frame() ;
176  gaussxy->plotOn(frame2) ;
177  regPlot(frame2,"Basic11_gaussxy_framex") ;
178 
179  // Make test plot and offer for comparison against ref plot
180  RooPlot* frame3 = w->var("y")->frame() ;
181  gaussxy->plotOn(frame3) ;
182  regPlot(frame3,"Basic11_gaussxy_framey") ;
183 
184  // Retrieve p.d.f from workspace
185  RooAbsPdf* sumxy = w->pdf("sumxy") ;
186 
187  // Make test plot and offer for comparison against ref plot
188  RooPlot* frame4 = w->var("x")->frame() ;
189  sumxy->plotOn(frame4) ;
190  regPlot(frame4,"Basic11_sumxy_framex") ;
191 
192  // Make test plot and offer for comparison against ref plot
193  RooPlot* frame5 = w->var("y")->frame() ;
194  sumxy->plotOn(frame5) ;
195  regPlot(frame5,"Basic11_sumxy_framey") ;
196 
197  // Retrieve p.d.f from workspace
198  RooAbsPdf* decay_gm1 = w->pdf("decay_gm1") ;
199 
200  // Make test plot and offer for comparison against ref plot
201  RooPlot* frame6 = w->var("dt")->frame() ;
202  decay_gm1->plotOn(frame6) ;
203  regPlot(frame6,"Basic11_decay_gm1_framedt") ;
204 
205  // Retrieve p.d.f from workspace
206  RooAbsPdf* decay_gmsum = w->pdf("decay_gmsum") ;
207 
208  // Make test plot and offer for comparison against ref plot
209  RooPlot* frame7 = w->var("dt")->frame() ;
210  decay_gmsum->plotOn(frame7) ;
211  regPlot(frame7,"Basic11_decay_gmsum_framedt") ;
212 
213  // Retrieve p.d.f. from workspace
214  RooAbsPdf* sim = w->pdf("sim") ;
215  RooCategory* cat = w->cat("cat") ;
216 
217  // Make plot with dummy dataset for index projection
218  RooPlot* frame8 = w->var("x")->frame() ;
219 
220  RooDataHist dh("dh","dh",*cat) ;
221  cat->setLabel("A") ;
222  dh.add(*cat) ;
223  cat->setLabel("B") ;
224  dh.add(*cat) ;
225 
226  sim->plotOn(frame8,ProjWData(*cat,dh),Project(*cat)) ;
227 
228  regPlot(frame8,"Basic11_sim_framex") ;
229 
230  }
231 
232  // "Workspace persistence"
233  return kTRUE ;
234  }
235 } ;
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition: RooAddPdf.h:29
RooProdPdf is an efficient implementation of a product of PDFs of the form.
Definition: RooProdPdf.h:31
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
RooCmdArg Project(const RooArgSet &projSet)
RooCmdArg ProjWData(const RooAbsData &projData, Bool_t binData=kFALSE)
virtual RooPlot * plotOn(RooPlot *frame, 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(), const RooCmdArg &arg9=RooCmdArg::none(), const RooCmdArg &arg10=RooCmdArg::none()) const
Plot (project) PDF on specified frame.
Definition: RooAbsPdf.h:105
virtual Bool_t setLabel(const char *label, Bool_t printError=kTRUE)
Set value by specifying the name of the desired state If printError is set, a message will be printed...
RooDataSet is a container class to hold N-dimensional binned data.
Definition: RooDataHist.h:40
Double_t x[n]
Definition: legend1.C:17
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t tau
Definition: TRolke.cxx:630
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
RooCategory * cat(const char *name) const
Retrieve discrete variable (RooCategory) with given name. A null pointer is returned if not found...
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
bool verbose
RooCategory represents a fundamental (non-derived) discrete value object.
Definition: RooCategory.h:24
RooPlot * frame(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()) const
A RooPlot is a plot frame and a container for graphics objects within that frame. ...
Definition: RooPlot.h:41
const Bool_t kFALSE
Definition: RtypesCore.h:92
RooAbsPdf * pdf(const char *name) const
Retrieve p.d.f (RooAbsPdf) with given name. A null pointer is returned if not found.
Double_t y[n]
Definition: legend1.C:17
RooRealVar * var(const char *name) const
Retrieve real-valued variable (RooRealVar) with given name. A null pointer is returned if not found...
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
Bool_t import(const RooAbsArg &arg, const RooCmdArg &arg1=RooCmdArg(), const RooCmdArg &arg2=RooCmdArg(), const RooCmdArg &arg3=RooCmdArg(), const RooCmdArg &arg4=RooCmdArg(), const RooCmdArg &arg5=RooCmdArg(), const RooCmdArg &arg6=RooCmdArg(), const RooCmdArg &arg7=RooCmdArg(), const RooCmdArg &arg8=RooCmdArg(), const RooCmdArg &arg9=RooCmdArg())
Import a RooAbsArg object, e.g.
RooPolynomial implements a polynomial p.d.f of the form By default coefficient a_0 is chosen to be 1...
Definition: RooPolynomial.h:28
RooCmdArg RenameConflictNodes(const char *suffix, Bool_t renameOrigNodes=kFALSE)
RooSimultaneous facilitates simultaneous fitting of multiple PDFs to subsets of a given dataset...
const Bool_t kTRUE
Definition: RtypesCore.h:91
The RooWorkspace is a persistable container for RooFit projects.
Definition: RooWorkspace.h:42
Single or double sided decay function that can be analytically convolved with any RooResolutionModel ...
Definition: RooDecay.h:22
Class RooGaussModel implements a RooResolutionModel that models a Gaussian distribution.
Definition: RooGaussModel.h:26