Logo ROOT  
Reference Guide
rf106_plotdecoration.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook -js
4 ///
5 /// Basic functionality: adding boxes with parameters, statistics to RooPlots, decorating with arrows, text etc...
6 ///
7 /// \macro_image
8 /// \macro_output
9 /// \macro_code
10 ///
11 /// \date 07/2008
12 /// \author Wouter Verkerke
13 
14 #include "RooRealVar.h"
15 #include "RooDataSet.h"
16 #include "RooGaussian.h"
17 #include "TCanvas.h"
18 #include "TAxis.h"
19 #include "RooPlot.h"
20 #include "TText.h"
21 #include "TArrow.h"
22 #include "TFile.h"
23 using namespace RooFit;
24 
26 {
27  // S e t u p m o d e l
28  // ---------------------
29 
30  // Create observables
31  RooRealVar x("x", "x", -10, 10);
32 
33  // Create Gaussian
34  RooRealVar sigma("sigma", "sigma", 1, 0.1, 10);
35  RooRealVar mean("mean", "mean", -3, -10, 10);
36  RooGaussian gauss("gauss", "gauss", x, mean, sigma);
37 
38  // Generate a sample of 1000 events with sigma=3
39  RooDataSet *data = gauss.generate(x, 1000);
40 
41  // Fit pdf to data
42  gauss.fitTo(*data);
43 
44  // P l o t p . d . f a n d d a t a
45  // -------------------------------------
46 
47  // Overlay projection of gauss on data
48  RooPlot *frame = x.frame(Name("xframe"), Title("RooPlot with decorations"), Bins(40));
49  data->plotOn(frame);
50  gauss.plotOn(frame);
51 
52  // A d d b o x w i t h p d f p a r a m e t e r s
53  // -----------------------------------------------------
54 
55  // Left edge of box starts at 55% of Xaxis)
56  gauss.paramOn(frame, Layout(0.55));
57 
58  // A d d b o x w i t h d a t a s t a t i s t i c s
59  // -------------------------------------------------------
60 
61  // X size of box is from 55% to 99% of Xaxis range, top of box is at 80% of Yaxis range)
62  data->statOn(frame, Layout(0.55, 0.99, 0.8));
63 
64  // A d d t e x t a n d a r r o w
65  // -----------------------------------
66 
67  // Add text to frame
68  TText *txt = new TText(2, 100, "Signal");
69  txt->SetTextSize(0.04);
70  txt->SetTextColor(kRed);
71  frame->addObject(txt);
72 
73  // Add arrow to frame
74  TArrow *arrow = new TArrow(2, 100, -1, 50, 0.01, "|>");
75  arrow->SetLineColor(kRed);
76  arrow->SetFillColor(kRed);
77  arrow->SetLineWidth(3);
78  frame->addObject(arrow);
79 
80  // P e r s i s t f r a m e w i t h a l l d e c o r a t i o n s i n R O O T f i l e
81  // ---------------------------------------------------------------------------------------------
82 
83  TFile f("rf106_plotdecoration.root", "RECREATE");
84  frame->Write();
85  f.Close();
86 
87  // To read back and plot frame with all decorations in clean root session do
88  // root> TFile f("rf106_plotdecoration.root") ;
89  // root> xframe->Draw() ;
90 
91  new TCanvas("rf106_plotdecoration", "rf106_plotdecoration", 600, 600);
92  gPad->SetLeftMargin(0.15);
93  frame->GetYaxis()->SetTitleOffset(1.6);
94  frame->Draw();
95 }
TArrow.h
RooPlot::Draw
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:691
f
#define f(i)
Definition: RSha256.hxx:122
RooFit::Bins
RooCmdArg Bins(Int_t nbin)
Definition: RooGlobalFunc.cxx:174
RooGaussian.h
x
Double_t x[n]
Definition: legend1.C:17
RooGaussian
Definition: RooGaussian.h:25
TCanvas.h
RooDataSet.h
TFile.h
RooPlot::addObject
void addObject(TObject *obj, Option_t *drawOptions="", Bool_t invisible=kFALSE)
Add a generic object to this plot.
Definition: RooPlot.cxx:422
rf106_plotdecoration
Definition: rf106_plotdecoration.py:1
RooFit::Layout
RooCmdArg Layout(Double_t xmin, Double_t xmax=0.99, Double_t ymin=0.95)
Definition: RooGlobalFunc.cxx:217
TGeant4Unit::gauss
static constexpr double gauss
Definition: TGeant4SystemOfUnits.h:269
RooFit
Definition: RooCFunction1Binding.h:29
RooAbsData::plotOn
virtual RooPlot * plotOn(RooPlot *frame, const RooCmdArg &arg1=RooCmdArg::none(), const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none()) const
Definition: RooAbsData.cxx:547
RooFit::Name
RooCmdArg Name(const char *name)
Definition: RooGlobalFunc.cxx:62
RooPlot.h
RooPlot::GetYaxis
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1256
RooPlot
Definition: RooPlot.h:44
RooRealVar.h
kRed
@ kRed
Definition: Rtypes.h:66
TFile
Definition: TFile.h:54
sigma
const Double_t sigma
Definition: h1analysisProxy.h:11
TText
Definition: TText.h:22
TCanvas
Definition: TCanvas.h:23
arrow
Definition: RArrowDS.hxx:9
TObject::Write
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TObject.cxx:795
TAxis.h
TArrow
Definition: TArrow.h:29
gPad
#define gPad
Definition: TVirtualPad.h:287
RooDataSet
Definition: RooDataSet.h:33
TAttAxis::SetTitleOffset
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:293
RooRealVar
Definition: RooRealVar.h:35
RooAbsData::statOn
virtual RooPlot * statOn(RooPlot *frame, const RooCmdArg &arg1=RooCmdArg::none(), const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none())
Add a box with statistics information to the specified frame.
Definition: RooAbsData.cxx:1165
TText.h
RooFit::Title
RooCmdArg Title(const char *name)
Definition: RooGlobalFunc.cxx:173