rf107_plotstyles

Basic functionality: various plotting styles of data, functions in a RooPlot

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"
using namespace RooFit;

Setup model

Create observables

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

Create Gaussian

In [3]:
RooRealVar sigma("sigma", "sigma", 3, 0.1, 10);
RooRealVar mean("mean", "mean", -3, -10, 10);
RooGaussian gauss("gauss", "gauss", x, mean, sigma);

Generate a sample of 100 events with sigma=3

In [4]:
RooDataSet *data = gauss.generate(x, 100);
input_line_52: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, 100);
 ^

Fit pdf to data

In [5]:
gauss.fitTo(*data);
input_line_53:2:15: error: reference to 'data' is ambiguous
 gauss.fitTo(*data);
              ^
input_line_52:2:14: note: candidate found by name lookup is '__cling_N523::data'
 RooDataSet *data = gauss.generate(x, 100);
             ^
/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 plot frames

Make four plot frames to demonstrate various plotting features

In [6]:
RooPlot *frame1 = x.frame(Name("xframe"), Title("Red Curve / SumW2 Histo errors"), Bins(20));
RooPlot *frame2 = x.frame(Name("xframe"), Title("Dashed Curve / No XError bars"), Bins(20));
RooPlot *frame3 = x.frame(Name("xframe"), Title("Filled Curve / Blue Histo"), Bins(20));
RooPlot *frame4 = x.frame(Name("xframe"), Title("Partial Range / Filled Bar chart"), Bins(20));

Data plotting styles

Use sqrt(sum(weights^2)) error instead of Poisson errors

In [7]:
data->plotOn(frame1, DataError(RooAbsData::SumW2));
input_line_55:2:2: error: reference to 'data' is ambiguous
 data->plotOn(frame1, DataError(RooAbsData::SumW2));
 ^
input_line_52:2:14: note: candidate found by name lookup is '__cling_N523::data'
 RooDataSet *data = gauss.generate(x, 100);
             ^
/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
    ^

Remove horizontal error bars

In [8]:
data->plotOn(frame2, XErrorSize(0));
input_line_56:2:2: error: reference to 'data' is ambiguous
 data->plotOn(frame2, XErrorSize(0));
 ^
input_line_52:2:14: note: candidate found by name lookup is '__cling_N523::data'
 RooDataSet *data = gauss.generate(x, 100);
             ^
/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
    ^

Blue markers and error bors

In [9]:
data->plotOn(frame3, MarkerColor(kBlue), LineColor(kBlue));
input_line_57:2:2: error: reference to 'data' is ambiguous
 data->plotOn(frame3, MarkerColor(kBlue), LineColor(kBlue));
 ^
input_line_52:2:14: note: candidate found by name lookup is '__cling_N523::data'
 RooDataSet *data = gauss.generate(x, 100);
             ^
/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
    ^

Filled bar chart

In [10]:
data->plotOn(frame4, DrawOption("B"), DataError(RooAbsData::None), XErrorSize(0), FillColor(kGray));
input_line_58:2:2: error: reference to 'data' is ambiguous
 data->plotOn(frame4, DrawOption("B"), DataError(RooAbsData::None), XErrorSize(0), FillColor(kGray));
 ^
input_line_52:2:14: note: candidate found by name lookup is '__cling_N523::data'
 RooDataSet *data = gauss.generate(x, 100);
             ^
/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
    ^

Function plotting styles

Change line color to red

In [11]:
gauss.plotOn(frame1, LineColor(kRed));

Change line style to dashed

In [12]:
gauss.plotOn(frame2, LineStyle(kDashed));

Filled shapes in green color

In [13]:
gauss.plotOn(frame3, DrawOption("F"), FillColor(kOrange), MoveToBack());
In [14]:
gauss.plotOn(frame4, Range(-8, 3), LineColor(kMagenta));

TCanvas *c = new TCanvas("rf107_plotstyles", "rf107_plotstyles", 800, 800);
c->Divide(2, 2);
c->cd(1);
gPad->SetLeftMargin(0.15);
frame1->GetYaxis()->SetTitleOffset(1.6);
frame1->Draw();
c->cd(2);
gPad->SetLeftMargin(0.15);
frame2->GetYaxis()->SetTitleOffset(1.6);
frame2->Draw();
c->cd(3);
gPad->SetLeftMargin(0.15);
frame3->GetYaxis()->SetTitleOffset(1.6);
frame3->Draw();
c->cd(4);
gPad->SetLeftMargin(0.15);
frame4->GetYaxis()->SetTitleOffset(1.6);
frame4->Draw();
[#1] INFO:Eval -- RooRealVar::setRange(x) new range named 'plotRange' created with bounds [-8,3]

Draw all canvases

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