Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
rf106_plotdecoration.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook -js
4 /// Basic functionality: adding boxes with parameters, statistics to RooPlots, decorating with arrows, text etc...
5 ///
6 /// \macro_image
7 /// \macro_output
8 /// \macro_code
9 /// \author 07/2008 - Wouter Verkerke
10 
11 #include "RooRealVar.h"
12 #include "RooDataSet.h"
13 #include "RooGaussian.h"
14 #include "TCanvas.h"
15 #include "TAxis.h"
16 #include "RooPlot.h"
17 #include "TText.h"
18 #include "TArrow.h"
19 #include "TFile.h"
20 using namespace RooFit;
21 
22 void rf106_plotdecoration()
23 {
24  // S e t u p m o d e l
25  // ---------------------
26 
27  // Create observables
28  RooRealVar x("x", "x", -10, 10);
29 
30  // Create Gaussian
31  RooRealVar sigma("sigma", "sigma", 1, 0.1, 10);
32  RooRealVar mean("mean", "mean", -3, -10, 10);
33  RooGaussian gauss("gauss", "gauss", x, mean, sigma);
34 
35  // Generate a sample of 1000 events with sigma=3
36  RooDataSet *data = gauss.generate(x, 1000);
37 
38  // Fit pdf to data
39  gauss.fitTo(*data);
40 
41  // P l o t p . d . f a n d d a t a
42  // -------------------------------------
43 
44  // Overlay projection of gauss on data
45  RooPlot *frame = x.frame(Name("xframe"), Title("RooPlot with decorations"), Bins(40));
46  data->plotOn(frame);
47  gauss.plotOn(frame);
48 
49  // A d d b o x w i t h p d f p a r a m e t e r s
50  // -----------------------------------------------------
51 
52  // Left edge of box starts at 55% of Xaxis)
53  gauss.paramOn(frame, Layout(0.55));
54 
55  // A d d b o x w i t h d a t a s t a t i s t i c s
56  // -------------------------------------------------------
57 
58  // X size of box is from 55% to 99% of Xaxis range, top of box is at 80% of Yaxis range)
59  data->statOn(frame, Layout(0.55, 0.99, 0.8));
60 
61  // A d d t e x t a n d a r r o w
62  // -----------------------------------
63 
64  // Add text to frame
65  TText *txt = new TText(2, 100, "Signal");
66  txt->SetTextSize(0.04);
67  txt->SetTextColor(kRed);
68  frame->addObject(txt);
69 
70  // Add arrow to frame
71  TArrow *arrow = new TArrow(2, 100, -1, 50, 0.01, "|>");
72  arrow->SetLineColor(kRed);
73  arrow->SetFillColor(kRed);
74  arrow->SetLineWidth(3);
75  frame->addObject(arrow);
76 
77  // 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
78  // ---------------------------------------------------------------------------------------------
79 
80  TFile f("rf106_plotdecoration.root", "RECREATE");
81  frame->Write();
82  f.Close();
83 
84  // To read back and plot frame with all decorations in clean root session do
85  // root> TFile f("rf106_plotdecoration.root") ;
86  // root> xframe->Draw() ;
87 
88  new TCanvas("rf106_plotdecoration", "rf106_plotdecoration", 600, 600);
89  gPad->SetLeftMargin(0.15);
90  frame->GetYaxis()->SetTitleOffset(1.6);
91  frame->Draw();
92 }