rf101_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 Sunday, November 27, 2022 at 11:05 AM.

In [1]:
%%cpp -d
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "TCanvas.h"
#include "RooPlot.h"
#include "TAxis.h"
using namespace RooFit;

Setup model

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

In [2]:
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);

Build gaussian pdf in terms of x,mean and sigma

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

Construct plot frame in 'x'

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

Plot model and change parameter values

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

In [5]:
gauss.plotOn(xframe);

Change the value of sigma to 3

In [6]:
sigma.setVal(3);

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

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

Generate events

Generate a dataset of 1000 events in x from gauss

In [8]:
RooDataSet *data = gauss.generate(x, 10000);
input_line_56:2:2: warning: 'data' shadows a declaration with the same name in the 'std' namespace; use '::data' to reference this declaration
 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 [9]:
RooPlot *xframe2 = x.frame(Title("Gaussian pdf with data"));
data->plotOn(xframe2);
gauss.plotOn(xframe2);
input_line_57:3:1: error: reference to 'data' is ambiguous
data->plotOn(xframe2);
^
input_line_56: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 [10]:
gauss.fitTo(*data);
input_line_58:2:15: error: reference to 'data' is ambiguous
 gauss.fitTo(*data);
              ^
input_line_56: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 [11]:
mean.Print();
sigma.Print();
RooRealVar::mean = 1  L(-10 - 10) 
RooRealVar::sigma = 3  L(0.1 - 10) 

Draw all frames on a canvas

In [12]:
TCanvas *c = new TCanvas("rf101_basics", "rf101_basics", 800, 400);
c->Divide(2);
c->cd(1);
gPad->SetLeftMargin(0.15);
xframe->GetYaxis()->SetTitleOffset(1.6);
xframe->Draw();
c->cd(2);
gPad->SetLeftMargin(0.15);
xframe2->GetYaxis()->SetTitleOffset(1.6);
xframe2->Draw();
input_line_61:2:3: error: use of undeclared identifier 'xframe2'
 (xframe2->GetYaxis()->SetTitleOffset(1.6000000000000001))
  ^
Error in <HandleInterpreterException>: Error evaluating expression (xframe2->GetYaxis()->SetTitleOffset(1.6000000000000001))
Execution of your code was aborted.

Draw all canvases

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