Rf 1 0 5_Funcbinding

Basic functionality: binding ROOT math functions as RooFit functions and pdfs

Author: Wouter Verkerke
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Wednesday, January 19, 2022 at 10:10 AM.

In [1]:
%%cpp -d
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "RooPlot.h"
#include "TMath.h"
#include "TF1.h"
#include "Math/DistFunc.h"
#include "RooTFnBinding.h"
In [2]:
%%cpp -d
// This is a workaround to make sure the namespace is used inside functions
using namespace RooFit;

Bind tmath::erf c function

Bind one-dimensional tmath::erf function as rooabsreal function

In [3]:
RooRealVar x("x", "x", -3, 3);
RooAbsReal *errorFunc = bindFunction("erf", TMath::Erf, x);
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

Print erf definition

In [4]:
errorFunc->Print();
RooCFunction1Binding<double,double>::erf[ function=TMath::Erf x=x ] = 0

Plot erf on frame

In [5]:
RooPlot *frame1 = x.frame(Title("TMath::Erf bound as RooFit function"));
errorFunc->plotOn(frame1);

Bind root::math::beta_pdf c function

Bind pdf root::math::beta with three variables as rooabspdf function

In [6]:
RooRealVar x2("x2", "x2", 0, 0.999);
RooRealVar a("a", "a", 5, 0, 10);
RooRealVar b("b", "b", 2, 0, 10);
RooAbsPdf *beta = bindPdf("beta", ROOT::Math::beta_pdf, x2, a, b);

Perf beta definition

In [7]:
beta->Print();
input_line_57:2:2: error: reference to 'beta' is ambiguous
 beta->Print();
 ^
input_line_56:5:12: note: candidate found by name lookup is '__cling_N524::beta'
RooAbsPdf *beta = bindPdf("beta", ROOT::Math::beta_pdf, x2, a, b);
           ^
/usr/include/c++/9/bits/specfun.h:343:5: note: candidate found by name lookup is 'std::beta'
    beta(_Tpa __a, _Tpb __b)
    ^

Generate some events and fit

In [8]:
RooDataSet *data = beta->generate(x2, 10000);
beta->fitTo(*data);
input_line_58:2:21: error: reference to 'beta' is ambiguous
 RooDataSet *data = beta->generate(x2, 10000);
                    ^
input_line_56:5:12: note: candidate found by name lookup is '__cling_N524::beta'
RooAbsPdf *beta = bindPdf("beta", ROOT::Math::beta_pdf, x2, a, b);
           ^
/usr/include/c++/9/bits/specfun.h:343:5: note: candidate found by name lookup is 'std::beta'
    beta(_Tpa __a, _Tpb __b)
    ^
input_line_58:3:1: error: reference to 'beta' is ambiguous
beta->fitTo(*data);
^
input_line_56:5:12: note: candidate found by name lookup is '__cling_N524::beta'
RooAbsPdf *beta = bindPdf("beta", ROOT::Math::beta_pdf, x2, a, b);
           ^
/usr/include/c++/9/bits/specfun.h:343:5: note: candidate found by name lookup is 'std::beta'
    beta(_Tpa __a, _Tpb __b)
    ^

Plot data and pdf on frame

In [9]:
RooPlot *frame2 = x2.frame(Title("ROOT::Math::Beta bound as RooFit pdf"));
data->plotOn(frame2);
beta->plotOn(frame2);
input_line_59:3:1: error: reference to overloaded function could not be resolved; did you mean to call it?
data->plotOn(frame2);
^~~~
/usr/include/c++/9/bits/range_access.h:289:5: note: possible target for call
    data(_Container& __cont) noexcept(noexcept(__cont.data()))
    ^
/usr/include/c++/9/bits/range_access.h:299:5: note: possible target for call
    data(const _Container& __cont) noexcept(noexcept(__cont.data()))
    ^
/usr/include/c++/9/bits/range_access.h:309:5: note: possible target for call
    data(_Tp (&__array)[_Nm]) noexcept
    ^
/usr/include/c++/9/bits/range_access.h:318:5: note: possible target for call
    data(initializer_list<_Tp> __il) noexcept
    ^
input_line_59:4:1: error: reference to 'beta' is ambiguous
beta->plotOn(frame2);
^
input_line_56:5:12: note: candidate found by name lookup is '__cling_N524::beta'
RooAbsPdf *beta = bindPdf("beta", ROOT::Math::beta_pdf, x2, a, b);
           ^
/usr/include/c++/9/bits/specfun.h:343:5: note: candidate found by name lookup is 'std::beta'
    beta(_Tpa __a, _Tpb __b)
    ^

Bind root tf1 as roofit function

Create a root tf1 function

In [10]:
TF1 *fa1 = new TF1("fa1", "sin(x)/x", 0, 10);

Create an observable

In [11]:
RooRealVar x3("x3", "x3", 0.01, 20);

Create binding of tf1 object to above observable

In [12]:
RooAbsReal *rfa1 = bindFunction(fa1, x3);

Print rfa1 definition

In [13]:
rfa1->Print();
RooTFnBinding::fa1[ TFn={fa1=sin(x)/x} obs=(x3) ] = -0.0547936

Make plot frame in observable, plot tf1 binding function

In [14]:
RooPlot *frame3 = x3.frame(Title("TF1 bound as RooFit function"));
rfa1->plotOn(frame3);

TCanvas *c = new TCanvas("rf105_funcbinding", "rf105_funcbinding", 1200, 400);
c->Divide(3);
c->cd(1);
gPad->SetLeftMargin(0.15);
frame1->GetYaxis()->SetTitleOffset(1.6);
frame1->Draw();
c->cd(2);
gPad->SetLeftMargin(0.15);
frame2->GetYaxis()->SetTitleOffset(1.6);
frame2->Draw();
c->cd(3);
gPad->SetLeftMargin(0.15);
frame3->GetYaxis()->SetTitleOffset(1.6);
frame3->Draw();
input_line_68:2:3: error: use of undeclared identifier 'frame2'
 (frame2->GetYaxis()->SetTitleOffset(1.6000000000000001))
  ^
Error in <HandleInterpreterException>: Error evaluating expression (frame2->GetYaxis()->SetTitleOffset(1.6000000000000001))
Execution of your code was aborted.

Draw all canvases

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