ROOT logo

From $ROOTSYS/tutorials/roofit/rf106_plotdecoration.C

//////////////////////////////////////////////////////////////////////////
//
// 'BASIC FUNCTIONALITY' RooFit tutorial macro #106
// 
//  Adding boxes with parameters, statistics to RooPlots.
//  Decorating RooPlots with arrows, text etc...
//
//
// 07/2008 - Wouter Verkerke 
// 
/////////////////////////////////////////////////////////////////////////

#ifndef __CINT__
#include "RooGlobalFunc.h"
#endif
#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 ;


void rf106_plotdecoration()
{

  // S e t u p   m o d e l 
  // ---------------------

  // 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
  RooDataSet* data = gauss.generate(x,1000) ;

  // Fit pdf to data
  gauss.fitTo(*data) ;


  // P l o t   p . d . f   a n d   d a t a 
  // -------------------------------------

  // Overlay projection of gauss on data
  RooPlot* frame = x.frame(Name("xframe"),Title("RooPlot with decorations"),Bins(40)) ;
  data->plotOn(frame) ;
  gauss.plotOn(frame) ;


  // A d d   b o x   w i t h   p d f   p a r a m e t e r s 
  // -----------------------------------------------------

  // Left edge of box starts at 55% of Xaxis)
  gauss.paramOn(frame,Layout(0.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
  // -------------------------------------------------------  

  // 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)) ;


  // A d d   t e x t   a n d   a r r o w 
  // -----------------------------------

  // Add text to frame
  TText* txt = new TText(2,100,"Signal") ;
  txt->SetTextSize(0.04) ;
  txt->SetTextColor(kRed) ;
  frame->addObject(txt) ;

  // 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) ;


  // 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
  // ---------------------------------------------------------------------------------------------

  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() ;

  new TCanvas("rf106_plotdecoration","rf106_plotdecoration",600,600) ;
  gPad->SetLeftMargin(0.15) ; frame->GetYaxis()->SetTitleOffset(1.6) ; frame->Draw() ;
  
}
 rf106_plotdecoration.C:1
 rf106_plotdecoration.C:2
 rf106_plotdecoration.C:3
 rf106_plotdecoration.C:4
 rf106_plotdecoration.C:5
 rf106_plotdecoration.C:6
 rf106_plotdecoration.C:7
 rf106_plotdecoration.C:8
 rf106_plotdecoration.C:9
 rf106_plotdecoration.C:10
 rf106_plotdecoration.C:11
 rf106_plotdecoration.C:12
 rf106_plotdecoration.C:13
 rf106_plotdecoration.C:14
 rf106_plotdecoration.C:15
 rf106_plotdecoration.C:16
 rf106_plotdecoration.C:17
 rf106_plotdecoration.C:18
 rf106_plotdecoration.C:19
 rf106_plotdecoration.C:20
 rf106_plotdecoration.C:21
 rf106_plotdecoration.C:22
 rf106_plotdecoration.C:23
 rf106_plotdecoration.C:24
 rf106_plotdecoration.C:25
 rf106_plotdecoration.C:26
 rf106_plotdecoration.C:27
 rf106_plotdecoration.C:28
 rf106_plotdecoration.C:29
 rf106_plotdecoration.C:30
 rf106_plotdecoration.C:31
 rf106_plotdecoration.C:32
 rf106_plotdecoration.C:33
 rf106_plotdecoration.C:34
 rf106_plotdecoration.C:35
 rf106_plotdecoration.C:36
 rf106_plotdecoration.C:37
 rf106_plotdecoration.C:38
 rf106_plotdecoration.C:39
 rf106_plotdecoration.C:40
 rf106_plotdecoration.C:41
 rf106_plotdecoration.C:42
 rf106_plotdecoration.C:43
 rf106_plotdecoration.C:44
 rf106_plotdecoration.C:45
 rf106_plotdecoration.C:46
 rf106_plotdecoration.C:47
 rf106_plotdecoration.C:48
 rf106_plotdecoration.C:49
 rf106_plotdecoration.C:50
 rf106_plotdecoration.C:51
 rf106_plotdecoration.C:52
 rf106_plotdecoration.C:53
 rf106_plotdecoration.C:54
 rf106_plotdecoration.C:55
 rf106_plotdecoration.C:56
 rf106_plotdecoration.C:57
 rf106_plotdecoration.C:58
 rf106_plotdecoration.C:59
 rf106_plotdecoration.C:60
 rf106_plotdecoration.C:61
 rf106_plotdecoration.C:62
 rf106_plotdecoration.C:63
 rf106_plotdecoration.C:64
 rf106_plotdecoration.C:65
 rf106_plotdecoration.C:66
 rf106_plotdecoration.C:67
 rf106_plotdecoration.C:68
 rf106_plotdecoration.C:69
 rf106_plotdecoration.C:70
 rf106_plotdecoration.C:71
 rf106_plotdecoration.C:72
 rf106_plotdecoration.C:73
 rf106_plotdecoration.C:74
 rf106_plotdecoration.C:75
 rf106_plotdecoration.C:76
 rf106_plotdecoration.C:77
 rf106_plotdecoration.C:78
 rf106_plotdecoration.C:79
 rf106_plotdecoration.C:80
 rf106_plotdecoration.C:81
 rf106_plotdecoration.C:82
 rf106_plotdecoration.C:83
 rf106_plotdecoration.C:84
 rf106_plotdecoration.C:85
 rf106_plotdecoration.C:86
 rf106_plotdecoration.C:87
 rf106_plotdecoration.C:88
 rf106_plotdecoration.C:89
 rf106_plotdecoration.C:90
 rf106_plotdecoration.C:91
 rf106_plotdecoration.C:92
 rf106_plotdecoration.C:93
 rf106_plotdecoration.C:94
 rf106_plotdecoration.C:95
 rf106_plotdecoration.C:96
 rf106_plotdecoration.C:97
 rf106_plotdecoration.C:98
 rf106_plotdecoration.C:99
 rf106_plotdecoration.C:100
 rf106_plotdecoration.C:101
 rf106_plotdecoration.C:102
 rf106_plotdecoration.C:103
 rf106_plotdecoration.C:104