ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
rf707_kernelestimation.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// 'SPECIAL PDFS' RooFit tutorial macro #707
4 ///
5 /// Using non-parametric (multi-dimensional) kernel estimation p.d.f.s
6 ///
7 ///
8 ///
9 /// \macro_code
10 /// \author 07/2008 - Wouter Verkerke
11 
12 
13 #ifndef __CINT__
14 #include "RooGlobalFunc.h"
15 #endif
16 #include "RooRealVar.h"
17 #include "RooDataSet.h"
18 #include "RooGaussian.h"
19 #include "RooConstVar.h"
20 #include "RooPolynomial.h"
21 #include "RooKeysPdf.h"
22 #include "RooNDKeysPdf.h"
23 #include "RooProdPdf.h"
24 #include "TCanvas.h"
25 #include "TAxis.h"
26 #include "TH1.h"
27 #include "RooPlot.h"
28 using namespace RooFit ;
29 
30 
31 void rf707_kernelestimation()
32 {
33  // C r e a t e l o w s t a t s 1 - D d a t a s e t
34  // -------------------------------------------------------
35 
36  // Create a toy pdf for sampling
37  RooRealVar x("x","x",0,20) ;
38  RooPolynomial p("p","p",x,RooArgList(RooConst(0.01),RooConst(-0.01),RooConst(0.0004))) ;
39 
40  // Sample 500 events from p
41  RooDataSet* data1 = p.generate(x,200) ;
42 
43 
44 
45  // 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
46  // ---------------------------------------------------------------
47 
48  // Create adaptive kernel estimation pdf. In this configuration the input data
49  // is mirrored over the boundaries to minimize edge effects in distribution
50  // that do not fall to zero towards the edges
51  RooKeysPdf kest1("kest1","kest1",x,*data1,RooKeysPdf::MirrorBoth) ;
52 
53  // An adaptive kernel estimation pdf on the same data without mirroring option
54  // for comparison
55  RooKeysPdf kest2("kest2","kest2",x,*data1,RooKeysPdf::NoMirror) ;
56 
57 
58  // Adaptive kernel estimation pdf with increased bandwidth scale factor
59  // (promotes smoothness over detail preservation)
60  RooKeysPdf kest3("kest1","kest1",x,*data1,RooKeysPdf::MirrorBoth,2) ;
61 
62 
63  // Plot kernel estimation pdfs with and without mirroring over data
64  RooPlot* frame = x.frame(Title("Adaptive kernel estimation pdf with and w/o mirroring"),Bins(20)) ;
65  data1->plotOn(frame) ;
66  kest1.plotOn(frame) ;
67  kest2.plotOn(frame,LineStyle(kDashed),LineColor(kRed)) ;
68 
69 
70  // Plot kernel estimation pdfs with regular and increased bandwidth
71  RooPlot* frame2 = x.frame(Title("Adaptive kernel estimation pdf with regular, increased bandwidth")) ;
72  kest1.plotOn(frame2) ;
73  kest3.plotOn(frame2,LineColor(kMagenta)) ;
74 
75 
76 
77  // C r e a t e l o w s t a t s 2 - D d a t a s e t
78  // -------------------------------------------------------
79 
80  // Construct a 2D toy pdf for sampleing
81  RooRealVar y("y","y",0,20) ;
82  RooPolynomial py("py","py",y,RooArgList(RooConst(0.01),RooConst(0.01),RooConst(-0.0004))) ;
83  RooProdPdf pxy("pxy","pxy",RooArgSet(p,py)) ;
84  RooDataSet* data2 = pxy.generate(RooArgSet(x,y),1000) ;
85 
86 
87 
88  // 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
89  // ---------------------------------------------------------------
90 
91  // Create 2D adaptive kernel estimation pdf with mirroring
92  RooNDKeysPdf kest4("kest4","kest4",RooArgSet(x,y),*data2,"am") ;
93 
94  // Create 2D adaptive kernel estimation pdf with mirroring and double bandwidth
95  RooNDKeysPdf kest5("kest5","kest5",RooArgSet(x,y),*data2,"am",2) ;
96 
97  // Create a histogram of the data
98  TH1* hh_data = data2->createHistogram("hh_data",x,Binning(10),YVar(y,Binning(10))) ;
99 
100  // Create histogram of the 2d kernel estimation pdfs
101  TH1* hh_pdf = kest4.createHistogram("hh_pdf",x,Binning(25),YVar(y,Binning(25))) ;
102  TH1* hh_pdf2 = kest5.createHistogram("hh_pdf2",x,Binning(25),YVar(y,Binning(25))) ;
103  hh_pdf->SetLineColor(kBlue) ;
104  hh_pdf2->SetLineColor(kMagenta) ;
105 
106 
107 
108  TCanvas* c = new TCanvas("rf707_kernelestimation","rf707_kernelestimation",800,800) ;
109  c->Divide(2,2) ;
110  c->cd(1) ; gPad->SetLeftMargin(0.15) ; frame->GetYaxis()->SetTitleOffset(1.4) ; frame->Draw() ;
111  c->cd(2) ; gPad->SetLeftMargin(0.15) ; frame2->GetYaxis()->SetTitleOffset(1.8) ; frame2->Draw() ;
112  c->cd(3) ; gPad->SetLeftMargin(0.15) ; hh_data->GetZaxis()->SetTitleOffset(1.4) ; hh_data->Draw("lego") ;
113  c->cd(4) ; gPad->SetLeftMargin(0.20) ; hh_pdf->GetZaxis()->SetTitleOffset(2.4) ; hh_pdf->Draw("surf") ; hh_pdf2->Draw("surfsame") ;
114 
115 
116 }
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:245
RooCmdArg LineColor(Color_t color)
Definition: Rtypes.h:61
RooProdPdf is an efficient implementation of a product of PDFs of the form.
Definition: RooProdPdf.h:31
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1118
Float_t py
Definition: hprod.C:33
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)
friend class RooArgSet
Definition: RooAbsArg.h:469
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:626
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:37
virtual void SetLineColor(Color_t lcolor)
Definition: TAttLine.h:54
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
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
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
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.
TAxis * GetZaxis()
Definition: TH1.h:321
RooConstVar & RooConst(Double_t val)
RooCmdArg Bins(Int_t nbin)
#define gPad
Definition: TVirtualPad.h:288
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)