Logo ROOT  
Reference Guide
rf509_wsinteractive.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -js
4///
5/// Organization and simultaneous fits: easy interactive access to workspace contents - CINT
6/// to CLING code migration
7///
8/// \macro_image
9/// \macro_output
10/// \macro_code
11///
12/// \date 04/2009
13/// \author Wouter Verkerke
14
15using namespace RooFit;
16
17void fillWorkspace(RooWorkspace &w);
18
20{
21 // C r e a t e a n d f i l l w o r k s p a c e
22 // ------------------------------------------------
23
24 // Create a workspace named 'w'
25 // With CINT w could exports its contents to
26 // a same-name C++ namespace in CINT 'namespace w'.
27 // but this does not work anymore in CLING.
28 // so this tutorial is an example on how to
29 // change the code
30 RooWorkspace *w1 = new RooWorkspace("w", kTRUE);
31
32 // Fill workspace with p.d.f. and data in a separate function
33 fillWorkspace(*w1);
34
35 // Print workspace contents
36 w1->Print();
37
38 // this does not work anymore with CLING
39 // use normal workspace functionality
40
41 // U s e w o r k s p a c e c o n t e n t s
42 // ----------------------------------------------
43
44 // Old syntax to use the name space prefix operator to access the workspace contents
45 //
46 // RooDataSet* d = w::model.generate(w::x,1000) ;
47 // RooFitResult* r = w::model.fitTo(*d) ;
48
49 // use normal workspace methods
50 RooAbsPdf *model = w1->pdf("model");
51 RooRealVar *x = w1->var("x");
52
53 RooDataSet *d = model->generate(*x, 1000);
54 RooFitResult *r = model->fitTo(*d);
55
56 // old syntax to access the variable x
57 // RooPlot* frame = w::x.frame() ;
58
59 RooPlot *frame = x->frame();
60 d->plotOn(frame);
61
62 // OLD syntax to omit x::
63 // NB: The 'w::' prefix can be omitted if namespace w is imported in local namespace
64 // in the usual C++ way
65 //
66 // using namespace w;
67 // model.plotOn(frame) ;
68 // model.plotOn(frame,Components(bkg),LineStyle(kDashed)) ;
69
70 // new correct syntax
71 RooAbsPdf *bkg = w1->pdf("bkg");
72 model->plotOn(frame);
73 model->plotOn(frame, Components(*bkg), LineStyle(kDashed));
74
75 // Draw the frame on the canvas
76 new TCanvas("rf509_wsinteractive", "rf509_wsinteractive", 600, 600);
77 gPad->SetLeftMargin(0.15);
78 frame->GetYaxis()->SetTitleOffset(1.4);
79 frame->Draw();
80}
81
82void fillWorkspace(RooWorkspace &w)
83{
84 // 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
85 // --------------------------------------------------------
86
87 // Declare observable x
88 RooRealVar x("x", "x", 0, 10);
89
90 // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters
91 RooRealVar mean("mean", "mean of gaussians", 5, 0, 10);
92 RooRealVar sigma1("sigma1", "width of gaussians", 0.5);
93 RooRealVar sigma2("sigma2", "width of gaussians", 1);
94
95 RooGaussian sig1("sig1", "Signal component 1", x, mean, sigma1);
96 RooGaussian sig2("sig2", "Signal component 2", x, mean, sigma2);
97
98 // Build Chebychev polynomial p.d.f.
99 RooRealVar a0("a0", "a0", 0.5, 0., 1.);
100 RooRealVar a1("a1", "a1", 0.2, 0., 1.);
101 RooChebychev bkg("bkg", "Background", x, RooArgSet(a0, a1));
102
103 // Sum the signal components into a composite signal p.d.f.
104 RooRealVar sig1frac("sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.);
105 RooAddPdf sig("sig", "Signal", RooArgList(sig1, sig2), sig1frac);
106
107 // Sum the composite signal and background
108 RooRealVar bkgfrac("bkgfrac", "fraction of background", 0.5, 0., 1.);
109 RooAddPdf model("model", "g1+g2+a", RooArgList(bkg, sig), bkgfrac);
110
111 w.import(model);
112}
ROOT::R::TRInterface & r
Definition: Object.C:4
#define d(i)
Definition: RSha256.hxx:102
const Bool_t kTRUE
Definition: RtypesCore.h:89
@ kDashed
Definition: TAttLine.h:48
#define gPad
Definition: TVirtualPad.h:287
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())
See RooAbsPdf::generate(const RooArgSet&,const RooCmdArg&,const RooCmdArg&,const RooCmdArg&,...
Definition: RooAbsPdf.h:55
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:1254
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
Helper calling plotOn(RooPlot*, RooLinkedList&) const.
Definition: RooAbsPdf.h:118
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition: RooAddPdf.h:29
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgList.h:21
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
Chebychev polynomial p.d.f.
Definition: RooChebychev.h:25
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:33
RooFitResult is a container class to hold the input and output of a PDF fit to a dataset.
Definition: RooFitResult.h:40
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
A RooPlot is a plot frame and a container for graphics objects within that frame.
Definition: RooPlot.h:44
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1277
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:712
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:35
The RooWorkspace is a persistable container for RooFit projects.
Definition: RooWorkspace.h:43
void Print(Option_t *opts=0) const
Print contents of the workspace.
RooRealVar * var(const char *name) const
Retrieve real-valued variable (RooRealVar) with given name. A null pointer is returned if not found.
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.
RooAbsPdf * pdf(const char *name) const
Retrieve p.d.f (RooAbsPdf) with given name. A null pointer is returned if not found.
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:294
The Canvas class.
Definition: TCanvas.h:27
RooCmdArg Components(const RooArgSet &compSet)
RooCmdArg LineStyle(Style_t style)
Double_t x[n]
Definition: legend1.C:17
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...