Organisation and simultaneous fits: reading and using a workspace
The input file for this macro is generated by rf502_wspacewrite.C
Author: Wouter Verkerke
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, March 19, 2024 at 07:16 PM.
%%cpp -d
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "RooChebychev.h"
#include "RooAddPdf.h"
#include "RooWorkspace.h"
#include "RooPlot.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "TFile.h"
#include "TH1.h"
using namespace RooFit;
Open input file with workspace (generated by rf503_wspacewrite)
TFile *f = new TFile("rf502_workspace.root");
Retrieve workspace from file
RooWorkspace *w = (RooWorkspace *)f->Get("w");
Retrieve x,model and data from workspace
RooRealVar *x = w->var("x");
RooAbsPdf *model = w->pdf("model");
RooAbsData *data = w->data("modelData");
input_line_60:4:1: warning: 'data' shadows a declaration with the same name in the 'std' namespace; use '::data' to reference this declaration RooAbsData *data = w->data("modelData"); ^
Print structure of composite pdf
model->Print("t");
0x7f7d6525d270 RooAddPdf::model = 0.9/1 [Auto,Clean] 0x7f7d654db440/V- RooChebychev::bkg = 0.8 [Auto,Dirty] 0x7f7d65151db0/V- RooRealVar::x = 5 0x7f7d655e23d0/V- RooRealVar::a0 = 0.5 0x7f7d655e2be0/V- RooRealVar::a1 = 0.2 0x7f7d655e3640/V- RooRealVar::bkgfrac = 0.5 0x7f7d655d78b0/V- RooAddPdf::sig = 1/1 [Auto,Clean] 0x7f7d6555b910/V- RooGaussian::sig1 = 1 [Auto,Dirty] 0x7f7d65151db0/V- RooRealVar::x = 5 0x7f7d64fbd060/V- RooRealVar::mean = 5 0x7f7d655e1680/V- RooRealVar::sigma1 = 0.5 0x7f7d655db4c0/V- RooRealVar::sig1frac = 0.8 0x7f7d65573090/V- RooGaussian::sig2 = 1 [Auto,Dirty] 0x7f7d65151db0/V- RooRealVar::x = 5 0x7f7d64fbd060/V- RooRealVar::mean = 5 0x7f7d65575120/V- RooRealVar::sigma2 = 1
Fit model to data
model->fitTo(*data, PrintLevel(-1));
input_line_62:2:16: error: reference to 'data' is ambiguous model->fitTo(*data, PrintLevel(-1)); ^ input_line_60:4:13: note: candidate found by name lookup is 'data' RooAbsData *data = w->data("modelData"); ^ /usr/include/c++/9/bits/range_access.h:318:5: note: candidate found by name lookup is 'std::data' data(initializer_list<_Tp> __il) noexcept ^ /usr/include/c++/9/bits/range_access.h:289:5: note: candidate found by name lookup is 'std::data' data(_Container& __cont) noexcept(noexcept(__cont.data())) ^ /usr/include/c++/9/bits/range_access.h:299:5: note: candidate found by name lookup is 'std::data' data(const _Container& __cont) noexcept(noexcept(__cont.data())) ^ /usr/include/c++/9/bits/range_access.h:309:5: note: candidate found by name lookup is 'std::data' data(_Tp (&__array)[_Nm]) noexcept ^
Plot data and PDF overlaid
RooPlot *xframe = x->frame(Title("Model and data read from workspace"));
data->plotOn(xframe);
model->plotOn(xframe);
input_line_63:3:1: error: reference to 'data' is ambiguous data->plotOn(xframe); ^ input_line_60:4:13: note: candidate found by name lookup is 'data' RooAbsData *data = w->data("modelData"); ^ /usr/include/c++/9/bits/range_access.h:318:5: note: candidate found by name lookup is 'std::data' data(initializer_list<_Tp> __il) noexcept ^ /usr/include/c++/9/bits/range_access.h:289:5: note: candidate found by name lookup is 'std::data' data(_Container& __cont) noexcept(noexcept(__cont.data())) ^ /usr/include/c++/9/bits/range_access.h:299:5: note: candidate found by name lookup is 'std::data' data(const _Container& __cont) noexcept(noexcept(__cont.data())) ^ /usr/include/c++/9/bits/range_access.h:309:5: note: candidate found by name lookup is 'std::data' data(_Tp (&__array)[_Nm]) noexcept ^
Overlay the background component of model with a dashed line
model->plotOn(xframe, Components("bkg"), LineStyle(kDashed));
input_line_64:2:53: error: cannot take the address of an rvalue of type 'ELineStyle' model->plotOn(xframe, Components("bkg"), LineStyle(kDashed)); ^~~~~~~ Error while creating dynamic expression for: model->plotOn(xframe, Components("bkg"), LineStyle(kDashed))
Overlay the background+sig2 components of model with a dotted line
model->plotOn(xframe, Components("bkg,sig2"), LineStyle(kDotted));
input_line_65:2:58: error: cannot take the address of an rvalue of type 'ELineStyle' model->plotOn(xframe, Components("bkg,sig2"), LineStyle(kDotted)); ^~~~~~~ Error while creating dynamic expression for: model->plotOn(xframe, Components("bkg,sig2"), LineStyle(kDotted))
Draw the frame on the canvas
new TCanvas("rf503_wspaceread", "rf503_wspaceread", 600, 600);
gPad->SetLeftMargin(0.15);
xframe->GetYaxis()->SetTitleOffset(1.4);
xframe->Draw();
IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal9EvaluateTIvEET_PNS1_15DynamicExprInfoEPN5clang11DeclContextE' unresolved while linking [cling interface function]! You are probably missing the definition of void cling::runtime::internal::EvaluateT<void>(cling::runtime::internal::DynamicExprInfo*, clang::DeclContext*) Maybe you need to load the corresponding shared library?
Draw all canvases
%jsroot on
gROOT->GetListOfCanvases()->Draw()