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 Wednesday, April 17, 2024 at 11:16 AM.
%%cpp -d
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "TCanvas.h"
#include "RooPlot.h"
#include "TAxis.h"
using namespace RooFit;
Declare variables x,mean,sigma with associated name, title, initial value and allowed range
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
RooGaussian gauss("gauss", "gaussian PDF", x, mean, sigma);
Construct plot frame in 'x'
RooPlot *xframe = x.frame(Title("Gaussian pdf."));
Plot gauss in frame (i.e. in x)
gauss.plotOn(xframe);
Change the value of sigma to 3
sigma.setVal(3);
Plot gauss in frame (i.e. in x) and draw frame on canvas
gauss.plotOn(xframe, LineColor(kRed));
Generate a dataset of 1000 events in x from gauss
std::unique_ptr<RooDataSet> data{gauss.generate(x, 10000)};
input_line_54: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{gauss.generate(x, 10000)}; ^
Make a second plot frame in x and draw both the data and the pdf in the frame
RooPlot *xframe2 = x.frame(Title("Gaussian pdf with data"));
data->plotOn(xframe2);
gauss.plotOn(xframe2);
input_line_55:3:1: error: reference to 'data' is ambiguous data->plotOn(xframe2); ^ input_line_54:2:30: note: candidate found by name lookup is 'data' std::unique_ptr<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 pdf to data
gauss.fitTo(*data, PrintLevel(-1));
input_line_56:2:15: error: reference to 'data' is ambiguous gauss.fitTo(*data, PrintLevel(-1)); ^ input_line_54:2:30: note: candidate found by name lookup is 'data' std::unique_ptr<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)
mean.Print();
sigma.Print();
RooRealVar::mean = 1 L(-10 - 10) RooRealVar::sigma = 3 L(0.1 - 10)
Draw all frames on a canvas
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_59: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
%jsroot on
gROOT->GetListOfCanvases()->Draw()