Logo ROOT  
Reference Guide
rf510_wsnamedsets.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -js
4/// Organization and simultaneous fits: working with named parameter sets and parameter
5/// snapshots in workspaces
6///
7/// \macro_image
8/// \macro_output
9/// \macro_code
10///
11/// \date April 2009
12/// \author Wouter Verkerke
13
14#include "RooRealVar.h"
15#include "RooDataSet.h"
16#include "RooGaussian.h"
17#include "RooConstVar.h"
18#include "RooChebychev.h"
19#include "RooAddPdf.h"
20#include "RooWorkspace.h"
21#include "RooPlot.h"
22#include "TCanvas.h"
23#include "TAxis.h"
24#include "TFile.h"
25#include "TH1.h"
26
27using namespace RooFit;
28
29void fillWorkspace(RooWorkspace &w);
30
31void rf510_wsnamedsets()
32{
33 // C r e a t e m o d e l a n d d a t a s e t
34 // -----------------------------------------------
35
36 RooWorkspace *w = new RooWorkspace("w");
37 fillWorkspace(*w);
38
39 // Exploit convention encoded in named set "parameters" and "observables"
40 // to use workspace contents w/o need for introspected
41 RooAbsPdf *model = w->pdf("model");
42
43 // Generate data from pdf in given observables
44 RooDataSet *data = model->generate(*w->set("observables"), 1000);
45
46 // Fit model to data
47 model->fitTo(*data);
48
49 // Plot fitted model and data on frame of first (only) observable
50 RooPlot *frame = ((RooRealVar *)w->set("observables")->first())->frame();
51 data->plotOn(frame);
52 model->plotOn(frame);
53
54 // Overlay plot with model with reference parameters as stored in snapshots
55 w->loadSnapshot("reference_fit");
56 model->plotOn(frame, LineColor(kRed));
57 w->loadSnapshot("reference_fit_bkgonly");
58 model->plotOn(frame, LineColor(kRed), LineStyle(kDashed));
59
60 // Draw the frame on the canvas
61 new TCanvas("rf510_wsnamedsets", "rf503_wsnamedsets", 600, 600);
62 gPad->SetLeftMargin(0.15);
63 frame->GetYaxis()->SetTitleOffset(1.4);
64 frame->Draw();
65
66 // Print workspace contents
67 w->Print();
68
69 // Workspace will remain in memory after macro finishes
70 gDirectory->Add(w);
71}
72
73void fillWorkspace(RooWorkspace &w)
74{
75 // C r e a t e m o d e l
76 // -----------------------
77
78 // Declare observable x
79 RooRealVar x("x", "x", 0, 10);
80
81 // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters
82 RooRealVar mean("mean", "mean of gaussians", 5, 0, 10);
83 RooRealVar sigma1("sigma1", "width of gaussians", 0.5);
84 RooRealVar sigma2("sigma2", "width of gaussians", 1);
85
86 RooGaussian sig1("sig1", "Signal component 1", x, mean, sigma1);
87 RooGaussian sig2("sig2", "Signal component 2", x, mean, sigma2);
88
89 // Build Chebychev polynomial pdf
90 RooRealVar a0("a0", "a0", 0.5, 0., 1.);
91 RooRealVar a1("a1", "a1", 0.2, 0., 1.);
92 RooChebychev bkg("bkg", "Background", x, RooArgSet(a0, a1));
93
94 // Sum the signal components into a composite signal pdf
95 RooRealVar sig1frac("sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.);
96 RooAddPdf sig("sig", "Signal", RooArgList(sig1, sig2), sig1frac);
97
98 // Sum the composite signal and background
99 RooRealVar bkgfrac("bkgfrac", "fraction of background", 0.5, 0., 1.);
100 RooAddPdf model("model", "g1+g2+a", RooArgList(bkg, sig), bkgfrac);
101
102 // Import model into pdf
103 w.import(model);
104
105 // E n c o d e d e f i n i t i o n o f p a r a m e t e r s i n w o r k s p a c e
106 // ---------------------------------------------------------------------------------------
107
108 // Define named sets "parameters" and "observables", which list which variables should be considered
109 // parameters and observables by the users convention
110 //
111 // Variables appearing in sets _must_ live in the workspace already, or the autoImport flag
112 // of defineSet must be set to import them on the fly. Named sets contain only references
113 // to the original variables, therefore the value of observables in named sets already
114 // reflect their 'current' value
115 RooArgSet *params = (RooArgSet *)model.getParameters(x);
116 w.defineSet("parameters", *params);
117 w.defineSet("observables", x);
118
119 // E n c o d e r e f e r e n c e v a l u e f o r p a r a m e t e r s i n w o r k s p a c e
120 // ---------------------------------------------------------------------------------------------------
121
122 // Define a parameter 'snapshot' in the pdf
123 // Unlike a named set, a parameter snapshot stores an independent set of values for
124 // a given set of variables in the workspace. The values can be stored and reloaded
125 // into the workspace variable objects using the loadSnapshot() and saveSnapshot()
126 // methods. A snapshot saves the value of each variable, any errors that are stored
127 // with it as well as the 'Constant' flag that is used in fits to determine if a
128 // parameter is kept fixed or not.
129
130 // Do a dummy fit to a (supposedly) reference dataset here and store the results
131 // of that fit into a snapshot
132 RooDataSet *refData = model.generate(x, 10000);
133 model.fitTo(*refData, PrintLevel(-1));
134
135 // The kTRUE flag imports the values of the objects in (*params) into the workspace
136 // If not set, the present values of the workspace parameters objects are stored
137 w.saveSnapshot("reference_fit", *params, kTRUE);
138
139 // Make another fit with the signal component forced to zero
140 // and save those parameters too
141
142 bkgfrac.setVal(1);
143 bkgfrac.setConstant(kTRUE);
144 bkgfrac.removeError();
145 model.fitTo(*refData, PrintLevel(-1));
146
147 w.saveSnapshot("reference_fit_bkgonly", *params, kTRUE);
148}
const Bool_t kTRUE
Definition: RtypesCore.h:91
@ kRed
Definition: Rtypes.h:66
@ kDashed
Definition: TAttLine.h:48
#define gDirectory
Definition: TDirectory.h:290
#define gPad
Definition: TVirtualPad.h:287
RooAbsArg * first() const
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
Definition: RooAbsData.cxx:547
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition: RooAddPdf.h:32
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:29
Chebychev polynomial p.d.f.
Definition: RooChebychev.h:25
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:33
Plain Gaussian p.d.f.
Definition: RooGaussian.h:24
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:1263
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:691
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:39
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.
Bool_t defineSet(const char *name, const RooArgSet &aset, Bool_t importMissing=kFALSE)
Define a named RooArgSet with given constituents.
Bool_t saveSnapshot(const char *name, const char *paramNames)
Save snapshot of values and attributes (including "Constant") of given parameters.
Bool_t loadSnapshot(const char *name)
Load the values and attributes of the parameters in the snapshot saved with the given name.
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.
const RooArgSet * set(const char *name)
Return pointer to previously defined named set with given nmame If no such set is found a null pointe...
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:293
The Canvas class.
Definition: TCanvas.h:23
RooCmdArg PrintLevel(Int_t code)
RooCmdArg LineColor(Color_t color)
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...