Logo ROOT   6.10/09
Reference Guide
rf509_wsinteractive.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook -js
4 /// 'ORGANIZATION AND SIMULTANEOUS FITS' RooFit tutorial macro #509
5 ///
6 /// Easy CINT interactive access to workspace contents through a
7 /// 'C++' namespace in CINT that maps the workspace contents in a typesafe way
8 ///
9 /// *********************************************************************************
10 /// *** NB: This macro exploits a feature native to CINT and _cannot_ be compiled ***
11 /// *********************************************************************************
12 ///
13 /// \macro_image
14 /// \macro_output
15 /// \macro_code
16 /// \author 04/2009 - Wouter Verkerke
17 
18 
19 #include "RooRealVar.h"
20 #include "RooDataSet.h"
21 #include "RooGaussian.h"
22 #include "RooConstVar.h"
23 #include "RooChebychev.h"
24 #include "RooAddPdf.h"
25 #include "RooWorkspace.h"
26 #include "RooPlot.h"
27 #include "TCanvas.h"
28 #include "TAxis.h"
29 #include "TFile.h"
30 #include "TH1.h"
31 using namespace RooFit ;
32 
33 
34 void fillWorkspace(RooWorkspace& w) ;
35 
36 void rf509_wsinteractive()
37 {
38  // C r e a t e a n d f i l l w o r k s p a c e
39  // ------------------------------------------------
40 
41  // Create a workspace named 'w'
42  // With CINT w could exports its contents to
43  // a same-name C++ namespace in CINT 'namespace w'.
44  // but this does not work anymore in CLING.
45  // so this tutorial is an example on how to
46  // change the code
47  RooWorkspace* w1 = new RooWorkspace("w",kTRUE) ;
48 
49  // Fill workspace with p.d.f. and data in a separate function
50  fillWorkspace(*w1) ;
51 
52  // Print workspace contents
53  w1->Print() ;
54 
55  // this does not work anymore with CLING
56  // use normal workspace functionality
57 
58 
59  // U s e w o r k s p a c e c o n t e n t s
60  // ----------------------------------------------
61 
62 
63  // Old syntax to use the name space prefix operator to access the workspace contents
64  //
65  //RooDataSet* d = w::model.generate(w::x,1000) ;
66  //RooFitResult* r = w::model.fitTo(*d) ;
67 
68  // use normal workspace methods
69  RooAbsPdf * model = w1->pdf("model");
70  RooRealVar * x = w1->var("x");
71 
72  RooDataSet* d = model->generate(*x,1000) ;
73  RooFitResult* r = model->fitTo(*d) ;
74 
75  // old syntax to access the variable x
76  // RooPlot* frame = w::x.frame() ;
77 
78  RooPlot* frame = x->frame() ;
79  d->plotOn(frame) ;
80 
81  // OLD syntax to omit x::
82  // NB: The 'w::' prefix can be omitted if namespace w is imported in local namespace
83  // in the usual C++ way
84  //
85  // using namespace w;
86  // model.plotOn(frame) ;
87  // model.plotOn(frame,Components(bkg),LineStyle(kDashed)) ;
88 
89  // new correct syntax
90  RooAbsPdf *bkg = w1->pdf("bkg");
91  model->plotOn(frame);
92  model->plotOn(frame,Components(*bkg),LineStyle(kDashed)) ;
93 
94  // Draw the frame on the canvas
95  new TCanvas("rf509_wsinteractive","rf509_wsinteractive",600,600) ;
96  gPad->SetLeftMargin(0.15) ; frame->GetYaxis()->SetTitleOffset(1.4) ; frame->Draw() ;
97 
98 
99 }
100 
101 
102 
103 
104 void fillWorkspace(RooWorkspace& w)
105 {
106  // C r e a t e p d f a n d f i l l w o r k s p a c e
107  // --------------------------------------------------------
108 
109  // Declare observable x
110  RooRealVar x("x","x",0,10) ;
111 
112  // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters
113  RooRealVar mean("mean","mean of gaussians",5,0,10) ;
114  RooRealVar sigma1("sigma1","width of gaussians",0.5) ;
115  RooRealVar sigma2("sigma2","width of gaussians",1) ;
116 
117  RooGaussian sig1("sig1","Signal component 1",x,mean,sigma1) ;
118  RooGaussian sig2("sig2","Signal component 2",x,mean,sigma2) ;
119 
120  // Build Chebychev polynomial p.d.f.
121  RooRealVar a0("a0","a0",0.5,0.,1.) ;
122  RooRealVar a1("a1","a1",0.2,0.,1.) ;
123  RooChebychev bkg("bkg","Background",x,RooArgSet(a0,a1)) ;
124 
125  // Sum the signal components into a composite signal p.d.f.
126  RooRealVar sig1frac("sig1frac","fraction of component 1 in signal",0.8,0.,1.) ;
127  RooAddPdf sig("sig","Signal",RooArgList(sig1,sig2),sig1frac) ;
128 
129  // Sum the composite signal and background
130  RooRealVar bkgfrac("bkgfrac","fraction of background",0.5,0.,1.) ;
131  RooAddPdf model("model","g1+g2+a",RooArgList(bkg,sig),bkgfrac) ;
132 
133  w.import(model) ;
134 
135 }
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:262
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition: RooAddPdf.h:29
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1118
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
Plot dataset on specified frame.
Definition: RooAbsData.cxx:552
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
Double_t x[n]
Definition: legend1.C:17
RooCmdArg LineStyle(Style_t style)
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
TRandom2 r(17)
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
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
The Canvas class.
Definition: TCanvas.h:31
RooAbsPdf * pdf(const char *name) const
Retrieve p.d.f (RooAbsPdf) with given name. A null pointer is returned if not found.
RooCmdArg Components(const RooArgSet &compSet)
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.
RooDataSet * generate(const RooArgSet &whatVars, Int_t nEvents, const RooCmdArg &arg1, const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none())
Generate a new dataset containing the specified variables with events sampled from our distribution...
Definition: RooAbsPdf.cxx:1702
#define gPad
Definition: TVirtualPad.h:284
virtual RooFitResult * fitTo(RooAbsData &data, 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())
Fit PDF to given dataset.
Definition: RooAbsPdf.cxx:1056
Chebychev polynomial p.d.f.
Definition: RooChebychev.h:25
void Print(Option_t *opts=0) const
Print contents of the workspace.
const Bool_t kTRUE
Definition: RtypesCore.h:91
The RooWorkspace is a persistable container for RooFit projects.
Definition: RooWorkspace.h:42
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:559