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 "RooChebychev.h"
18#include "RooAddPdf.h"
19#include "RooWorkspace.h"
20#include "RooPlot.h"
21#include "TCanvas.h"
22#include "TAxis.h"
23#include "TFile.h"
24#include "TH1.h"
25
26using namespace RooFit;
27
28void fillWorkspace(RooWorkspace &w);
29
31{
32 // C r e a t e m o d e l a n d d a t a s e t
33 // -----------------------------------------------
34
35 RooWorkspace *w = new RooWorkspace("w");
36 fillWorkspace(*w);
37
38 // Exploit convention encoded in named set "parameters" and "observables"
39 // to use workspace contents w/o need for introspected
40 RooAbsPdf *model = w->pdf("model");
41
42 // Generate data from pdf in given observables
43 RooDataSet *data = model->generate(*w->set("observables"), 1000);
44
45 // Fit model to data
46 model->fitTo(*data);
47
48 // Plot fitted model and data on frame of first (only) observable
49 RooPlot *frame = ((RooRealVar *)w->set("observables")->first())->frame();
50 data->plotOn(frame);
51 model->plotOn(frame);
52
53 // Overlay plot with model with reference parameters as stored in snapshots
54 w->loadSnapshot("reference_fit");
55 model->plotOn(frame, LineColor(kRed));
56 w->loadSnapshot("reference_fit_bkgonly");
57 model->plotOn(frame, LineColor(kRed), LineStyle(kDashed));
58
59 // Draw the frame on the canvas
60 new TCanvas("rf510_wsnamedsets", "rf503_wsnamedsets", 600, 600);
61 gPad->SetLeftMargin(0.15);
62 frame->GetYaxis()->SetTitleOffset(1.4);
63 frame->Draw();
64
65 // Print workspace contents
66 w->Print();
67
68 // Workspace will remain in memory after macro finishes
69 gDirectory->Add(w);
70}
71
72void fillWorkspace(RooWorkspace &w)
73{
74 // C r e a t e m o d e l
75 // -----------------------
76
77 // Declare observable x
78 RooRealVar x("x", "x", 0, 10);
79
80 // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters
81 RooRealVar mean("mean", "mean of gaussians", 5, 0, 10);
82 RooRealVar sigma1("sigma1", "width of gaussians", 0.5);
83 RooRealVar sigma2("sigma2", "width of gaussians", 1);
84
85 RooGaussian sig1("sig1", "Signal component 1", x, mean, sigma1);
86 RooGaussian sig2("sig2", "Signal component 2", x, mean, sigma2);
87
88 // Build Chebychev polynomial pdf
89 RooRealVar a0("a0", "a0", 0.5, 0., 1.);
90 RooRealVar a1("a1", "a1", 0.2, 0., 1.);
91 RooChebychev bkg("bkg", "Background", x, RooArgSet(a0, a1));
92
93 // Sum the signal components into a composite signal pdf
94 RooRealVar sig1frac("sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.);
95 RooAddPdf sig("sig", "Signal", RooArgList(sig1, sig2), sig1frac);
96
97 // Sum the composite signal and background
98 RooRealVar bkgfrac("bkgfrac", "fraction of background", 0.5, 0., 1.);
99 RooAddPdf model("model", "g1+g2+a", RooArgList(bkg, sig), bkgfrac);
100
101 // Import model into pdf
102 w.import(model);
103
104 // 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
105 // ---------------------------------------------------------------------------------------
106
107 // Define named sets "parameters" and "observables", which list which variables should be considered
108 // parameters and observables by the users convention
109 //
110 // Variables appearing in sets _must_ live in the workspace already, or the autoImport flag
111 // of defineSet must be set to import them on the fly. Named sets contain only references
112 // to the original variables, therefore the value of observables in named sets already
113 // reflect their 'current' value
114 RooArgSet *params = (RooArgSet *)model.getParameters(x);
115 w.defineSet("parameters", *params);
116 w.defineSet("observables", x);
117
118 // 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
119 // ---------------------------------------------------------------------------------------------------
120
121 // Define a parameter 'snapshot' in the pdf
122 // Unlike a named set, a parameter snapshot stores an independent set of values for
123 // a given set of variables in the workspace. The values can be stored and reloaded
124 // into the workspace variable objects using the loadSnapshot() and saveSnapshot()
125 // methods. A snapshot saves the value of each variable, any errors that are stored
126 // with it as well as the 'Constant' flag that is used in fits to determine if a
127 // parameter is kept fixed or not.
128
129 // Do a dummy fit to a (supposedly) reference dataset here and store the results
130 // of that fit into a snapshot
131 RooDataSet *refData = model.generate(x, 10000);
132 model.fitTo(*refData, PrintLevel(-1));
133
134 // The true flag imports the values of the objects in (*params) into the workspace
135 // If not set, the present values of the workspace parameters objects are stored
136 w.saveSnapshot("reference_fit", *params, true);
137
138 // Make another fit with the signal component forced to zero
139 // and save those parameters too
140
141 bkgfrac.setVal(1);
142 bkgfrac.setConstant(true);
143 bkgfrac.removeError();
144 model.fitTo(*refData, PrintLevel(-1));
145
146 w.saveSnapshot("reference_fit_bkgonly", *params, true);
147}
@ kRed
Definition: Rtypes.h:66
@ kDashed
Definition: TAttLine.h:48
#define gDirectory
Definition: TDirectory.h:348
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
#define gPad
Definition: TVirtualPad.h:288
RooArgSet * getParameters(const RooAbsData *data, bool stripDisconnected=true) const
Create a list of leaf nodes in the arg tree starting with ourself as top node that don't match any of...
Definition: RooAbsArg.cxx:541
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:1501
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:61
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 override
Helper calling plotOn(RooPlot*, RooLinkedList&) const.
Definition: RooAbsPdf.h:127
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition: RooAddPdf.h:34
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgList.h:22
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:56
Chebychev polynomial p.d.f.
Definition: RooChebychev.h:25
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:55
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:43
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1276
void Draw(Option_t *options=nullptr) override
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:649
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:40
The RooWorkspace is a persistable container for RooFit projects.
Definition: RooWorkspace.h:43
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:301
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...
Definition: Common.h:18