Basic functionality: adding boxes with parameters, statistics to RooPlots, decorating with arrows, text etc...
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 "TText.h"
#include "TArrow.h"
#include "TFile.h"
using namespace RooFit;
Create observables
RooRealVar x("x", "x", -10, 10);
Create Gaussian
RooRealVar sigma("sigma", "sigma", 1, 0.1, 10);
RooRealVar mean("mean", "mean", -3, -10, 10);
RooGaussian gauss("gauss", "gauss", x, mean, sigma);
Generate a sample of 1000 events with sigma=3
std::unique_ptr<RooDataSet> data{gauss.generate(x, 1000)};
input_line_50: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, 1000)}; ^
Fit pdf to data
gauss.fitTo(*data, PrintLevel(-1));
input_line_51:2:15: error: reference to 'data' is ambiguous gauss.fitTo(*data, PrintLevel(-1)); ^ input_line_50:2:30: note: candidate found by name lookup is 'data' std::unique_ptr<RooDataSet> data{gauss.generate(x, 1000)}; ^ /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 ^
Overlay projection of gauss on data
RooPlot *frame = x.frame(Name("xframe"), Title("RooPlot with decorations"), Bins(40));
data->plotOn(frame);
gauss.plotOn(frame);
input_line_52:3:1: error: reference to 'data' is ambiguous data->plotOn(frame); ^ input_line_50:2:30: note: candidate found by name lookup is 'data' std::unique_ptr<RooDataSet> data{gauss.generate(x, 1000)}; ^ /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 ^
Left edge of box starts at 55% of Xaxis)
gauss.paramOn(frame, Layout(0.55));
input_line_53:2:23: error: cannot initialize an array element of type 'void *' with an rvalue of type 'RooCmdArg (*)(double, double, double)' gauss.paramOn(frame, Layout(0.55)); ^~~~~~
X size of box is from 55% to 99% of Xaxis range, top of box is at 80% of Yaxis range)
data->statOn(frame, Layout(0.55, 0.99, 0.8));
input_line_54:2:2: error: reference to 'data' is ambiguous data->statOn(frame, Layout(0.55, 0.99, 0.8)); ^ input_line_50:2:30: note: candidate found by name lookup is 'data' std::unique_ptr<RooDataSet> data{gauss.generate(x, 1000)}; ^ /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 ^
Add text to frame
TText *txt = new TText(2, 100, "Signal");
txt->SetTextSize(0.04);
txt->SetTextColor(kRed);
frame->addObject(txt);
[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { __cxx_global_var_initcling_module_312_, _ZN5cling7runtime8internal15DynamicExprInfoC2EPKcPPvb, _ZN12__cling_N52716__cling_Un1Qu326EPv, $.cling-module-312.__inits.0, _ZN7TObjectnwEm, __orc_init_func.cling-module-312, _ZN12__cling_N5273txtE, _ZN5cling7runtime8internal15DynamicExprInfoC1EPKcPPvb, _GLOBAL__sub_I_cling_module_312 }) } IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal9EvaluateTIvEET_PNS1_15DynamicExprInfoEPN5clang11DeclContextE' unresolved while linking [cling interface function]! You are probably missing the definition of void cling::runtime::internal::EvaluateT<void>(cling::runtime::internal::DynamicExprInfo*, clang::DeclContext*) Maybe you need to load the corresponding shared library?
Add arrow to frame
TArrow *arrow = new TArrow(2, 100, -1, 50, 0.01, "|>");
arrow->SetLineColor(kRed);
arrow->SetFillColor(kRed);
arrow->SetLineWidth(3);
frame->addObject(arrow);
cling JIT session error: Failed to materialize symbols: { (main, { _ZN7TObjectnwEm }) } [runStaticInitializersOnce]: Failed to materialize symbols: { (main, { _ZN12__cling_N5285arrowE, _GLOBAL__sub_I_cling_module_313, __cxx_global_var_initcling_module_313_, $.cling-module-313.__inits.0, _ZN12__cling_N52816__cling_Un1Qu327EPv, __orc_init_func.cling-module-313 }) }
TFile f("rf106_plotdecoration.root", "RECREATE");
frame->Write();
f.Close();
cling JIT session error: Failed to materialize symbols: { (main, { _ZN5cling7runtime8internal15DynamicExprInfoC1EPKcPPvb }) } [runStaticInitializersOnce]: Failed to materialize symbols: { (main, { __orc_init_func.cling-module-314, __cxx_global_var_initcling_module_314_, _ZN12__cling_N5291fE, $.cling-module-314.__inits.0, _ZN12__cling_N52916__cling_Un1Qu328EPv, _GLOBAL__sub_I_cling_module_314 }) }
To read back and plot frame with all decorations in clean root session do root> TFile f("rf106_plotdecoration.root") ; root> xframe->Draw() ;
new TCanvas("rf106_plotdecoration", "rf106_plotdecoration", 600, 600);
gPad->SetLeftMargin(0.15);
frame->GetYaxis()->SetTitleOffset(1.6);
frame->Draw();
[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { __orc_init_func.cling-module-312 }) } cling JIT session error: Failed to materialize symbols: { (main, { _ZN7TObjectnwEm }) }
Draw all canvases
%jsroot on
gROOT->GetListOfCanvases()->Draw()