Logo ROOT   6.08/07
Reference Guide
rf706_histpdf.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook -js
4 /// 'SPECIAL PDFS' RooFit tutorial macro #706
5 ///
6 /// Histogram based p.d.f.s and functions
7 ///
8 /// \macro_image
9 /// \macro_output
10 /// \macro_code
11 /// \author 07/2008 - Wouter Verkerke
12 
13 
14 #include "RooRealVar.h"
15 #include "RooDataSet.h"
16 #include "RooGaussian.h"
17 #include "RooConstVar.h"
18 #include "RooPolynomial.h"
19 #include "RooHistPdf.h"
20 #include "TCanvas.h"
21 #include "TAxis.h"
22 #include "RooPlot.h"
23 using namespace RooFit ;
24 
25 
26 void rf706_histpdf()
27 {
28  // C r e a t e p d f f o r s a m p l i n g
29  // ---------------------------------------------
30 
31  RooRealVar x("x","x",0,20) ;
32  RooPolynomial p("p","p",x,RooArgList(RooConst(0.01),RooConst(-0.01),RooConst(0.0004))) ;
33 
34 
35 
36  // C r e a t e l o w s t a t s h i s t o g r a m
37  // ---------------------------------------------------
38 
39  // Sample 500 events from p
40  x.setBins(20) ;
41  RooDataSet* data1 = p.generate(x,500) ;
42 
43  // Create a binned dataset with 20 bins and 500 events
44  RooDataHist* hist1 = data1->binnedClone() ;
45 
46  // Represent data in dh as pdf in x
47  RooHistPdf histpdf1("histpdf1","histpdf1",x,*hist1,0) ;
48 
49  // Plot unbinned data and histogram pdf overlaid
50  RooPlot* frame1 = x.frame(Title("Low statistics histogram pdf"),Bins(100)) ;
51  data1->plotOn(frame1) ;
52  histpdf1.plotOn(frame1) ;
53 
54 
55  // C r e a t e h i g h s t a t s h i s t o g r a m
56  // -----------------------------------------------------
57 
58  // Sample 100000 events from p
59  x.setBins(10) ;
60  RooDataSet* data2 = p.generate(x,100000) ;
61 
62  // Create a binned dataset with 10 bins and 100K events
63  RooDataHist* hist2 = data2->binnedClone() ;
64 
65  // Represent data in dh as pdf in x, apply 2nd order interpolation
66  RooHistPdf histpdf2("histpdf2","histpdf2",x,*hist2,2) ;
67 
68  // Plot unbinned data and histogram pdf overlaid
69  RooPlot* frame2 = x.frame(Title("High stats histogram pdf with interpolation"),Bins(100)) ;
70  data2->plotOn(frame2) ;
71  histpdf2.plotOn(frame2) ;
72 
73 
74  TCanvas* c = new TCanvas("rf706_histpdf","rf706_histpdf",800,400) ;
75  c->Divide(2) ;
76  c->cd(1) ; gPad->SetLeftMargin(0.15) ; frame1->GetYaxis()->SetTitleOffset(1.4) ; frame1->Draw() ;
77  c->cd(2) ; gPad->SetLeftMargin(0.15) ; frame2->GetYaxis()->SetTitleOffset(1.8) ; frame2->Draw() ;
78 
79 
80 }
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title Offset is a correction factor with respect to the "s...
Definition: TAttAxis.cxx:262
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1118
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
Plot dataset on specified frame.
Definition: RooAbsData.cxx:552
return c
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
RooCmdArg Title(const char *name)
RooDataSet is a container class to hold N-dimensional binned data.
Definition: RooDataHist.h:40
Double_t x[n]
Definition: legend1.C:17
RooDataHist * binnedClone(const char *newName=0, const char *newTitle=0) const
Return binned clone of this dataset.
Definition: RooDataSet.cxx:981
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:37
RooHistPdf implements a probablity density function sampled from a multidimensional histogram...
Definition: RooHistPdf.h:28
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
A RooPlot is a plot frame and a container for graphics objects within that frame. ...
Definition: RooPlot.h:41
The Canvas class.
Definition: TCanvas.h:41
RooConstVar & RooConst(Double_t val)
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1089
RooCmdArg Bins(Int_t nbin)
#define gPad
Definition: TVirtualPad.h:289
RooPolynomial implements a polynomial p.d.f of the form By default coefficient a_0 is chosen to be 1...
Definition: RooPolynomial.h:28
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:559