Rf 1 0 1_Basics¶

Basic functionality: fitting, plotting, toy data generation on one-dimensional PDFs.

pdf = gauss(x,m,s)

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 09:48 AM.

In :
%%cpp -d
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "TCanvas.h"
#include "RooPlot.h"
#include "TAxis.h"
In :
%%cpp -d
// This is a workaround to make sure the namespace is used inside functions
using namespace RooFit;

Setup model¶

Declare variables x,mean,sigma with associated name, title, initial value and allowed range

In :
RooRealVar x("x", "x", -10, 10);
RooRealVar mean("mean", "mean of gaussian", 1, -10, 10);
RooRealVar sigma("sigma", "width of gaussian", 1, 0.1, 10);
RooFit v3.60 -- Developed by Wouter Verkerke and David Kirkby
Copyright (C) 2000-2013 NIKHEF, University of California & Stanford University

Build gaussian pdf in terms of x,mean and sigma

In :
RooGaussian gauss("gauss", "gaussian PDF", x, mean, sigma);

Construct plot frame in 'x'

In :
RooPlot *xframe = x.frame(Title("Gaussian pdf."));

Plot model and change parameter values¶

Plot gauss in frame (i.e. in x)

In :
gauss.plotOn(xframe);

Change the value of sigma to 3

In :
sigma.setVal(3);

Plot gauss in frame (i.e. in x) and draw frame on canvas

In :
gauss.plotOn(xframe, LineColor(kRed));

Generate events¶

Generate a dataset of 1000 events in x from gauss

In :
RooDataSet *data = gauss.generate(x, 10000);

Make a second plot frame in x and draw both the data and the pdf in the frame

In :
RooPlot *xframe2 = x.frame(Title("Gaussian pdf with data"));
data->plotOn(xframe2);
gauss.plotOn(xframe2);
input_line_60:3:1: error: reference to 'data' is ambiguous
data->plotOn(xframe2);
^
input_line_59:2:14: note: candidate found by name lookup is '__cling_N527::data'
RooDataSet *data = gauss.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
^

Fit model to data¶

Fit pdf to data

In :
gauss.fitTo(*data);
input_line_61:2:15: error: reference to 'data' is ambiguous
gauss.fitTo(*data);
^
input_line_59:2:14: note: candidate found by name lookup is '__cling_N527::data'
RooDataSet *data = gauss.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
^

Print values of mean and sigma (that now reflect fitted values and errors)

In :
mean.Print();
sigma.Print();
RooRealVar::mean = 1  L(-10 - 10)
RooRealVar::sigma = 3  L(0.1 - 10)

Draw all frames on a canvas

In :
TCanvas *c = new TCanvas("rf101_basics", "rf101_basics", 800, 400);
c->Divide(2);
c->cd(1);
xframe->GetYaxis()->SetTitleOffset(1.6);
xframe->Draw();
c->cd(2);