# rf103_interprfuncs¶

Basic functionality: interpreted 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, November 30, 2022 at 11:21 AM.

In [1]:
%%cpp -d
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "RooPlot.h"
#include "RooFitResult.h"
#include "RooGenericPdf.h"
#include "RooConstVar.h"

using namespace RooFit;


# Generic interpreted p.d.f.¶

Declare observable x

In [2]:
RooRealVar x("x", "x", -20, 20);


## Construct generic pdf from interpreted expression¶

To construct a proper pdf, the formula expression is explicitly normalized internally by dividing it by a numeric integral of the expression over x in the range [-20,20]

In [3]:
RooRealVar alpha("alpha", "alpha", 5, 0.1, 10);
RooGenericPdf genpdf("genpdf", "genpdf", "(1+0.1*abs(x)+sin(sqrt(abs(x*alpha+0.1))))", RooArgSet(x, alpha));


## Sample, fit and plot generic pdf¶

Generate a toy dataset from the interpreted pdf

In [4]:
RooDataSet *data = genpdf.generate(x, 10000);

[#1] INFO:NumericIntegration -- RooRealIntegral::init(genpdf_Int[x]) using numeric integrator RooIntegrator1D to calculate Int(x)
[#1] INFO:NumericIntegration -- RooRealIntegral::init(genpdf_Int[x]) using numeric integrator RooIntegrator1D to calculate Int(x)

input_line_55:2:2: warning: 'data' shadows a declaration with the same name in the 'std' namespace; use '::data' to reference this declaration
RooDataSet *data = genpdf.generate(x, 10000);
^


Fit the interpreted pdf to the generated data

In [5]:
genpdf.fitTo(*data);

input_line_56:2:16: error: reference to 'data' is ambiguous
genpdf.fitTo(*data);
^
input_line_55:2:14: note: candidate found by name lookup is '__cling_N523::data'
RooDataSet *data = genpdf.generate(x, 10000);
^
/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
^


Make a plot of the data and the pdf overlaid

In [6]:
RooPlot *xframe = x.frame(Title("Interpreted expression pdf"));
data->plotOn(xframe);
genpdf.plotOn(xframe);

input_line_57:3:1: error: reference to 'data' is ambiguous
data->plotOn(xframe);
^
input_line_55:2:14: note: candidate found by name lookup is '__cling_N523::data'
RooDataSet *data = genpdf.generate(x, 10000);
^
/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
^


# Standard p.d.f adjust with interpreted helper function¶

Make a gauss(x,sqrt(mean2),sigma) from a standard RooGaussian

## Construct standard pdf with formula replacing parameter¶

Construct parameter mean2 and sigma

In [7]:
RooRealVar mean2("mean2", "mean^2", 10, 0, 200);
RooRealVar sigma("sigma", "sigma", 3, 0.1, 10);


Construct interpreted function mean = sqrt(mean^2)

In [8]:
RooFormulaVar mean("mean", "mean", "sqrt(mean2)", mean2);


Construct a gaussian g2(x,sqrt(mean2),sigma) ;

In [9]:
RooGaussian g2("g2", "h2", x, mean, sigma);


## Generate toy data¶

Construct a separate gaussian g1(x,10,3) to generate a toy Gaussian dataset with mean 10 and width 3

In [10]:
RooGaussian g1("g1", "g1", x, RooConst(10), RooConst(3));
RooDataSet *data2 = g1.generate(x, 1000);


## Fit and plot tailored standard pdf¶

Fit g2 to data from g1

In [11]:
RooFitResult *r = g2.fitTo(*data2, Save());
r->Print();

[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: activating const optimization
**********
**    1 **SET PRINT           1
**********
**********
**********
PARAMETER DEFINITIONS:
NO.   NAME         VALUE      STEP SIZE      LIMITS
1 mean2        1.00000e+01  5.00000e+00    0.00000e+00  2.00000e+02
2 sigma        3.00000e+00  9.90000e-01    1.00000e-01  1.00000e+01
**********
**    3 **SET ERR         0.5
**********
**********
**    4 **SET PRINT           1
**********
**********
**    5 **SET STR           1
**********
NOW USING STRATEGY  1: TRY TO BALANCE SPEED AGAINST RELIABILITY
**********
**********
FIRST CALL TO USER FUNCTION AT NEW START POINT, WITH IFLAG=4.
START MIGRAD MINIMIZATION.  STRATEGY  1.  CONVERGENCE WHEN EDM .LT. 1.00e-03
FCN=5148.93 FROM MIGRAD    STATUS=INITIATE        8 CALLS           9 TOTAL
EDM= unknown      STRATEGY= 1      NO ERROR MATRIX
EXT PARAMETER               CURRENT GUESS       STEP         FIRST
NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
1  mean2        1.00000e+01   5.00000e+00   1.18625e-01  -5.23438e+03
2  sigma        3.00000e+00   9.90000e-01   2.22742e-01  -7.90389e+03
ERR DEF= 0.5
MIGRAD WILL VERIFY CONVERGENCE AND ERROR MATRIX.
COVARIANCE MATRIX CALCULATED SUCCESSFULLY
FCN=2551.39 FROM MIGRAD    STATUS=CONVERGED      59 CALLS          60 TOTAL
EDM=8.7852e-06    STRATEGY= 1      ERROR MATRIX ACCURATE
EXT PARAMETER                                   STEP         FIRST
NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
1  mean2        1.00100e+02   1.98019e+00   6.89576e-04   4.58015e-02
2  sigma        3.11719e+00   7.12427e-02   5.29831e-04   1.79331e-01
ERR DEF= 0.5
EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  2    ERR DEF=0.5
3.922e+00  2.826e-03
2.826e-03  5.076e-03
PARAMETER  CORRELATION COEFFICIENTS
NO.  GLOBAL      1      2
1  0.02003   1.000  0.020
2  0.02003   0.020  1.000
**********
**    7 **SET ERR         0.5
**********
**********
**    8 **SET PRINT           1
**********
**********
**    9 **HESSE        1000
**********
COVARIANCE MATRIX CALCULATED SUCCESSFULLY
FCN=2551.39 FROM HESSE     STATUS=OK             10 CALLS          70 TOTAL
EDM=8.78617e-06    STRATEGY= 1      ERROR MATRIX ACCURATE
EXT PARAMETER                                INTERNAL      INTERNAL
NO.   NAME      VALUE            ERROR       STEP SIZE       VALUE
1  mean2        1.00100e+02   1.98016e+00   1.37915e-04   1.00004e-03
2  sigma        3.11719e+00   7.12418e-02   1.05966e-04  -4.01138e-01
ERR DEF= 0.5
EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  2    ERR DEF=0.5
3.922e+00  2.730e-03
2.730e-03  5.076e-03
PARAMETER  CORRELATION COEFFICIENTS
NO.  GLOBAL      1      2
1  0.01935   1.000  0.019
2  0.01935   0.019  1.000
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: deactivating const optimization

RooFitResult: minimized FCN value: 2551.39, estimated distance to minimum: 8.78617e-06
covariance matrix quality: Full, accurate covariance matrix
Status : MINIMIZE=0 HESSE=0

Floating Parameter    FinalValue +/-  Error
--------------------  --------------------------
mean2    1.0010e+02 +/-  1.98e+00
sigma    3.1172e+00 +/-  7.12e-02



Plot data on frame and overlay projection of g2

In [12]:
RooPlot *xframe2 = x.frame(Title("Tailored Gaussian pdf"));
data2->plotOn(xframe2);
g2.plotOn(xframe2);


Draw all frames on a canvas

In [13]:
TCanvas *c = new TCanvas("rf103_interprfuncs", "rf103_interprfuncs", 800, 400);
c->Divide(2);
c->cd(1);
xframe->GetYaxis()->SetTitleOffset(1.4);
xframe->Draw();
c->cd(2);
xframe2->GetYaxis()->SetTitleOffset(1.4);
xframe2->Draw();

input_line_87:2:3: error: use of undeclared identifier 'xframe'
(xframe->GetYaxis()->SetTitleOffset(1.3999999999999999))
^
Error in <HandleInterpreterException>: Error evaluating expression (xframe->GetYaxis()->SetTitleOffset(1.3999999999999999))
Execution of your code was aborted.


Draw all canvases

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