Rf 5 0 3_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 Monday, January 17, 2022 at 10:04 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"
In [2]:
%%cpp -d
// This is a workaround to make sure the namespace is used inside functions
using namespace RooFit;

Read workspace from file

Open input file with workspace (generated by rf503_wspacewrite)

In [3]:
TFile *f = new TFile("rf502_workspace.root");
RooFit v3.60 -- Developed by Wouter Verkerke and David Kirkby 
                Copyright (C) 2000-2013 NIKHEF, University of California & Stanford University
                All rights reserved, please read http://roofit.sourceforge.net/license.txt

Retrieve workspace from file

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

Retrieve pdf, data from workspace

Retrieve x,model and data from workspace

In [5]:
RooRealVar *x = w->var("x");
RooAbsPdf *model = w->pdf("model");
RooAbsData *data = w->data("modelData");

Print structure of composite pdf

In [6]:
model->Print("t");
0x7f4841cd4930 RooAddPdf::model = 0.9 [Auto,Dirty] 
  0x7f4841ea4470/V- RooChebychev::bkg = 0.8 [Auto,Dirty] 
    0x7f4841aa6ff0/V- RooRealVar::x = 5
    0x7f4841f77570/V- RooRealVar::a0 = 0.5
    0x7f4841f77d90/V- RooRealVar::a1 = 0.2
  0x7f4841f787f0/V- RooRealVar::bkgfrac = 0.5
  0x7f4841f6dfd0/V- RooAddPdf::sig = 1 [Auto,Dirty] 
    0x7f4841f01200/V- RooGaussian::sig1 = 1 [Auto,Dirty] 
      0x7f4841aa6ff0/V- RooRealVar::x = 5
      0x7f4841f0c090/V- RooRealVar::mean = 5
      0x7f4841f767e0/V- RooRealVar::sigma1 = 0.5
    0x7f4841f6f790/V- RooRealVar::sig1frac = 0.8
    0x7f4841f0c550/V- RooGaussian::sig2 = 1 [Auto,Dirty] 
      0x7f4841aa6ff0/V- RooRealVar::x = 5
      0x7f4841f0c090/V- RooRealVar::mean = 5
      0x7f4841f0d2b0/V- RooRealVar::sigma2 = 1

Fit model to data, plot model

Fit model to data

In [7]:
model->fitTo(*data);
input_line_65:2:16: error: reference to 'data' is ambiguous
 model->fitTo(*data);
               ^
input_line_63: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 [8]:
RooPlot *xframe = x->frame(Title("Model and data read from workspace"));
data->plotOn(xframe);
model->plotOn(xframe);
input_line_66:3:1: error: reference to 'data' is ambiguous
data->plotOn(xframe);
^
input_line_63: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 [9]:
model->plotOn(xframe, Components("bkg"), LineStyle(kDashed));
input_line_67: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 [10]:
model->plotOn(xframe, Components("bkg,sig2"), LineStyle(kDotted));
input_line_68: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 [11]:
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 [12]:
%jsroot on
gROOT->GetListOfCanvases()->Draw()