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