rf503_wspaceread

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 Sunday, November 27, 2022 at 11:07 AM.

In [1]:
%%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;

Read workspace from file

Open input file with workspace (generated by rf503_wspacewrite)

In [2]:
TFile *f = new TFile("rf502_workspace.root");

Retrieve workspace from file

In [3]:
RooWorkspace *w = (RooWorkspace *)f->Get("w");

Retrieve pdf, data from workspace

Retrieve x,model and data from workspace

In [4]:
RooRealVar *x = w->var("x");
RooAbsPdf *model = w->pdf("model");
RooAbsData *data = w->data("modelData");
input_line_61: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

In [5]:
model->Print("t");
0x7fc0353905e0 RooAddPdf::model = 0.9/1 [Auto,Clean] 
  0x7fc035655d80/V- RooChebychev::bkg = 0.8 [Auto,Dirty] 
    0x7fc03461f290/V- RooRealVar::x = 5
    0x7fc035703850/V- RooRealVar::a0 = 0.5
    0x7fc035704050/V- RooRealVar::a1 = 0.2
  0x7fc035704aa0/V- RooRealVar::bkgfrac = 0.5
  0x7fc0356faa60/V- RooAddPdf::sig = 1/1 [Auto,Clean] 
    0x7fc0356824c0/V- RooGaussian::sig1 = 1 [Auto,Dirty] 
      0x7fc03461f290/V- RooRealVar::x = 5
      0x7fc03522a6f0/V- RooRealVar::mean = 5
      0x7fc0347cdd40/V- RooRealVar::sigma1 = 0.5
    0x7fc0351d31f0/V- RooRealVar::sig1frac = 0.8
    0x7fc035509a10/V- RooGaussian::sig2 = 1 [Auto,Dirty] 
      0x7fc03461f290/V- RooRealVar::x = 5
      0x7fc03522a6f0/V- RooRealVar::mean = 5
      0x7fc03522ab10/V- RooRealVar::sigma2 = 1

Fit model to data, plot model

Fit model to data

In [6]:
model->fitTo(*data);
input_line_63:2:16: error: reference to 'data' is ambiguous
 model->fitTo(*data);
               ^
input_line_61:4:13: note: candidate found by name lookup is '__cling_N523::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

In [7]:
RooPlot *xframe = x->frame(Title("Model and data read from workspace"));
data->plotOn(xframe);
model->plotOn(xframe);
input_line_64:3:1: error: reference to 'data' is ambiguous
data->plotOn(xframe);
^
input_line_61:4:13: note: candidate found by name lookup is '__cling_N523::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

In [8]:
model->plotOn(xframe, Components("bkg"), LineStyle(kDashed));
input_line_65: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

In [9]:
model->plotOn(xframe, Components("bkg,sig2"), LineStyle(kDotted));
input_line_66: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

In [10]:
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

In [11]:
%jsroot on
gROOT->GetListOfCanvases()->Draw()