Logo ROOT   6.08/07
Reference Guide
rf707_kernelestimation.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook
4 /// 'SPECIAL PDFS' RooFit tutorial macro #707
5 ///
6 /// Using non-parametric (multi-dimensional) kernel estimation p.d.f.s
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 "RooKeysPdf.h"
20 #include "RooNDKeysPdf.h"
21 #include "RooProdPdf.h"
22 #include "TCanvas.h"
23 #include "TAxis.h"
24 #include "TH1.h"
25 #include "RooPlot.h"
26 using namespace RooFit ;
27 
28 
29 void rf707_kernelestimation()
30 {
31  // C r e a t e l o w s t a t s 1 - D d a t a s e t
32  // -------------------------------------------------------
33 
34  // Create a toy pdf for sampling
35  RooRealVar x("x","x",0,20) ;
36  RooPolynomial p("p","p",x,RooArgList(RooConst(0.01),RooConst(-0.01),RooConst(0.0004))) ;
37 
38  // Sample 500 events from p
39  RooDataSet* data1 = p.generate(x,200) ;
40 
41 
42 
43  // C r e a t e 1 - D k e r n e l e s t i m a t i o n p d f
44  // ---------------------------------------------------------------
45 
46  // Create adaptive kernel estimation pdf. In this configuration the input data
47  // is mirrored over the boundaries to minimize edge effects in distribution
48  // that do not fall to zero towards the edges
49  RooKeysPdf kest1("kest1","kest1",x,*data1,RooKeysPdf::MirrorBoth) ;
50 
51  // An adaptive kernel estimation pdf on the same data without mirroring option
52  // for comparison
53  RooKeysPdf kest2("kest2","kest2",x,*data1,RooKeysPdf::NoMirror) ;
54 
55 
56  // Adaptive kernel estimation pdf with increased bandwidth scale factor
57  // (promotes smoothness over detail preservation)
58  RooKeysPdf kest3("kest1","kest1",x,*data1,RooKeysPdf::MirrorBoth,2) ;
59 
60 
61  // Plot kernel estimation pdfs with and without mirroring over data
62  RooPlot* frame = x.frame(Title("Adaptive kernel estimation pdf with and w/o mirroring"),Bins(20)) ;
63  data1->plotOn(frame) ;
64  kest1.plotOn(frame) ;
65  kest2.plotOn(frame,LineStyle(kDashed),LineColor(kRed)) ;
66 
67 
68  // Plot kernel estimation pdfs with regular and increased bandwidth
69  RooPlot* frame2 = x.frame(Title("Adaptive kernel estimation pdf with regular, increased bandwidth")) ;
70  kest1.plotOn(frame2) ;
71  kest3.plotOn(frame2,LineColor(kMagenta)) ;
72 
73 
74 
75  // C r e a t e l o w s t a t s 2 - D d a t a s e t
76  // -------------------------------------------------------
77 
78  // Construct a 2D toy pdf for sampling
79  RooRealVar y("y","y",0,20) ;
80  RooPolynomial py("py","py",y,RooArgList(RooConst(0.01),RooConst(0.01),RooConst(-0.0004))) ;
81  RooProdPdf pxy("pxy","pxy",RooArgSet(p,py)) ;
82  RooDataSet* data2 = pxy.generate(RooArgSet(x,y),1000) ;
83 
84 
85 
86  // C r e a t e 2 - D k e r n e l e s t i m a t i o n p d f
87  // ---------------------------------------------------------------
88 
89  // Create 2D adaptive kernel estimation pdf with mirroring
90  RooNDKeysPdf kest4("kest4","kest4",RooArgSet(x,y),*data2,"am") ;
91 
92  // Create 2D adaptive kernel estimation pdf with mirroring and double bandwidth
93  RooNDKeysPdf kest5("kest5","kest5",RooArgSet(x,y),*data2,"am",2) ;
94 
95  // Create a histogram of the data
96  TH1* hh_data = data2->createHistogram("hh_data",x,Binning(10),YVar(y,Binning(10))) ;
97 
98  // Create histogram of the 2d kernel estimation pdfs
99  TH1* hh_pdf = kest4.createHistogram("hh_pdf",x,Binning(25),YVar(y,Binning(25))) ;
100  TH1* hh_pdf2 = kest5.createHistogram("hh_pdf2",x,Binning(25),YVar(y,Binning(25))) ;
101  hh_pdf->SetLineColor(kBlue) ;
102  hh_pdf2->SetLineColor(kMagenta) ;
103 
104 
105 
106  TCanvas* c = new TCanvas("rf707_kernelestimation","rf707_kernelestimation",800,800) ;
107  c->Divide(2,2) ;
108  c->cd(1) ; gPad->SetLeftMargin(0.15) ; frame->GetYaxis()->SetTitleOffset(1.4) ; frame->Draw() ;
109  c->cd(2) ; gPad->SetLeftMargin(0.15) ; frame2->GetYaxis()->SetTitleOffset(1.8) ; frame2->Draw() ;
110  c->cd(3) ; gPad->SetLeftMargin(0.15) ; hh_data->GetZaxis()->SetTitleOffset(1.4) ; hh_data->Draw("lego") ;
111  c->cd(4) ; gPad->SetLeftMargin(0.20) ; hh_pdf->GetZaxis()->SetTitleOffset(2.4) ; hh_pdf->Draw("surf") ; hh_pdf2->Draw("surfsame") ;
112 
113 }
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
RooCmdArg LineColor(Color_t color)
return c
Definition: Rtypes.h:61
RooProdPdf is an efficient implementation of a product of PDFs of the form.
Definition: RooProdPdf.h:31
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
RooCmdArg Title(const char *name)
Double_t x[n]
Definition: legend1.C:17
Class RooKeysPdf implements a one-dimensional kernel estimation p.d.f which model the distribution of...
Definition: RooKeysPdf.h:25
RooCmdArg LineStyle(Style_t style)
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:37
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:46
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
TH2F * createHistogram(const RooAbsRealLValue &var1, const RooAbsRealLValue &var2, const char *cuts="", const char *name="hist") const
Create a TH2F histogram of the distribution of the specified variable using this dataset.
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
Generic N-dimensional implementation of a kernel estimation p.d.f.
Definition: RooNDKeysPdf.h:45
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
RooCmdArg YVar(const RooAbsRealLValue &var, const RooCmdArg &arg=RooCmdArg::none())
Double_t y[n]
Definition: legend1.C:17
The TH1 histogram class.
Definition: TH1.h:80
TAxis * GetZaxis()
Definition: TH1.h:326
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
Definition: Rtypes.h:61
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:559
RooCmdArg Binning(const RooAbsBinning &binning)