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 Wednesday, April 17, 2024 at 11:18 AM.
%%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");
0x7fe3c124d130 RooAddPdf::model = 0.9/1 [Auto,Clean] 0x7fe3c14c2e40/V- RooChebychev::bkg = 0.8 [Auto,Dirty] 0x7fe3c121fb80/V- RooRealVar::x = 5 0x7fe3c15d4e50/V- RooRealVar::a0 = 0.5 0x7fe3c15d5660/V- RooRealVar::a1 = 0.2 0x7fe3c15d60c0/V- RooRealVar::bkgfrac = 0.5 0x7fe3c15ca720/V- RooAddPdf::sig = 1/1 [Auto,Clean] 0x7fe3c154db30/V- RooGaussian::sig1 = 1 [Auto,Dirty] 0x7fe3c121fb80/V- RooRealVar::x = 5 0x7fe3c115a000/V- RooRealVar::mean = 5 0x7fe3c15d4100/V- RooRealVar::sigma1 = 0.5 0x7fe3c0fac540/V- RooRealVar::sig1frac = 0.8 0x7fe3c15652b0/V- RooGaussian::sig2 = 1 [Auto,Dirty] 0x7fe3c121fb80/V- RooRealVar::x = 5 0x7fe3c115a000/V- RooRealVar::mean = 5 0x7fe3c1141370/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()