rf106_plotdecoration

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

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

Setup model

Create observables

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

Create Gaussian

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

In [ ]:
RooDataSet *data = gauss.generate(x, 1000);

Fit pdf to data

In [ ]:
gauss.fitTo(*data);

Plot p.d.f and data

Overlay projection of gauss on data

In [ ]:
RooPlot *frame = x.frame(Name("xframe"), Title("RooPlot with decorations"), Bins(40));
data->plotOn(frame);
gauss.plotOn(frame);

Add box with pdf parameters

Left edge of box starts at 55% of Xaxis)

In [ ]:
gauss.paramOn(frame, Layout(0.55));

Add box with data statistics

X size of box is from 55% to 99% of Xaxis range, top of box is at 80% of Yaxis range)

In [ ]:
data->statOn(frame, Layout(0.55, 0.99, 0.8));

Add text and arrow

Add text to frame

In [ ]:
TText *txt = new TText(2, 100, "Signal");
txt->SetTextSize(0.04);
txt->SetTextColor(kRed);
frame->addObject(txt);

Add arrow to frame

In [ ]:
TArrow *arrow = new TArrow(2, 100, -1, 50, 0.01, "|>");
arrow->SetLineColor(kRed);
arrow->SetFillColor(kRed);
arrow->SetLineWidth(3);
frame->addObject(arrow);

Persist frame with all decorations in ROOT file

In [ ]:
TFile f("rf106_plotdecoration.root", "RECREATE");
frame->Write();
f.Close();

To read back and plot frame with all decorations in clean root session do root> TFile f("rf106_plotdecoration.root") ; root> xframe->Draw() ;

In [ ]:
new TCanvas("rf106_plotdecoration", "rf106_plotdecoration", 600, 600);
gPad->SetLeftMargin(0.15);
frame->GetYaxis()->SetTitleOffset(1.6);
frame->Draw();

Draw all canvases

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