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, April 17, 2024 at 11:16 AM.
%%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"
using namespace RooFit;
Declare observable x
RooRealVar x("x", "x", -20, 20);
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]
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));
Generate a toy dataset from the interpreted pdf
std::unique_ptr<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_53:2:2: warning: 'data' shadows a declaration with the same name in the 'std' namespace; use '::data' to reference this declaration std::unique_ptr<RooDataSet> data{genpdf.generate(x, 10000)}; ^
Fit the interpreted pdf to the generated data
genpdf.fitTo(*data, PrintLevel(-1));
input_line_54:2:16: error: reference to 'data' is ambiguous genpdf.fitTo(*data, PrintLevel(-1)); ^ input_line_53:2:30: note: candidate found by name lookup is 'data' std::unique_ptr<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
RooPlot *xframe = x.frame(Title("Interpreted expression pdf"));
data->plotOn(xframe);
genpdf.plotOn(xframe);
input_line_55:3:1: error: reference to 'data' is ambiguous data->plotOn(xframe); ^ input_line_53:2:30: note: candidate found by name lookup is 'data' std::unique_ptr<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 gauss(x,sqrt(mean2),sigma) from a standard RooGaussian
Construct parameter mean2 and sigma
RooRealVar mean2("mean2", "mean^2", 10, 0, 200);
RooRealVar sigma("sigma", "sigma", 3, 0.1, 10);
Construct interpreted function mean = sqrt(mean^2)
RooFormulaVar mean("mean", "mean", "sqrt(mean2)", mean2);
Construct a gaussian g2(x,sqrt(mean2),sigma) ;
RooGaussian g2("g2", "h2", x, mean, sigma);
Construct a separate gaussian g1(x,10,3) to generate a toy Gaussian dataset with mean 10 and width 3
RooGaussian g1("g1", "g1", x, 10.0, 3.0);
std::unique_ptr<RooDataSet> data2{g1.generate(x, 1000)};
Fit g2 to data from g1
std::unique_ptr<RooFitResult> fitResult{g2.fitTo(*data2, Save(), PrintLevel(-1))};
fitResult->Print();
[#1] INFO:Fitting -- RooAbsPdf::fitTo(g2_over_g2_Int[x]) fixing normalization set for coefficient determination to observables in data [#1] INFO:Fitting -- using CPU computation library compiled with -mavx2 [#1] INFO:Fitting -- RooAddition::defaultErrorLevel(nll_g2_over_g2_Int[x]_g1Data) Summation contains a RooNLLVar, using its error level [#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: activating const optimization [#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: deactivating const optimization RooFitResult: minimized FCN value: 2551.39, estimated distance to minimum: 4.39288e-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
RooPlot *xframe2 = x.frame(Title("Tailored Gaussian pdf"));
data2->plotOn(xframe2);
g2.plotOn(xframe2);
Draw all frames on a canvas
TCanvas *c = new TCanvas("rf103_interprfuncs", "rf103_interprfuncs", 800, 400);
c->Divide(2);
c->cd(1);
gPad->SetLeftMargin(0.15);
xframe->GetYaxis()->SetTitleOffset(1.4);
xframe->Draw();
c->cd(2);
gPad->SetLeftMargin(0.15);
xframe2->GetYaxis()->SetTitleOffset(1.4);
xframe2->Draw();
input_line_85: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
%jsroot on
gROOT->GetListOfCanvases()->Draw()