Logo ROOT   6.16/01
Reference Guide
rf705_linearmorph.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook
4/// 'SPECIAL PDFS' RooFit tutorial macro #705
5///
6/// Linear interpolation between p.d.f shapes using the 'Alex Read' algorithm
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 "RooIntegralMorph.h"
20#include "RooNLLVar.h"
21#include "TCanvas.h"
22#include "TAxis.h"
23#include "RooPlot.h"
24#include "TH1.h"
25using namespace RooFit ;
26
27
28void rf705_linearmorph()
29{
30 // C r e a t e e n d p o i n t p d f s h a p e s
31 // ------------------------------------------------------
32
33 // Observable
34 RooRealVar x("x","x",-20,20) ;
35
36 // Lower end point shape: a Gaussian
37 RooRealVar g1mean("g1mean","g1mean",-10) ;
38 RooGaussian g1("g1","g1",x,g1mean,RooConst(2)) ;
39
40 // Upper end point shape: a Polynomial
41 RooPolynomial g2("g2","g2",x,RooArgSet(RooConst(-0.03),RooConst(-0.001))) ;
42
43
44
45 // C r e a t e i n t e r p o l a t i n g p d f
46 // -----------------------------------------------
47
48 // Create interpolation variable
49 RooRealVar alpha("alpha","alpha",0,1.0) ;
50
51 // Specify sampling density on observable and interpolation variable
52 x.setBins(1000,"cache") ;
53 alpha.setBins(50,"cache") ;
54
55 // Construct interpolating pdf in (x,a) represent g1(x) at a=a_min
56 // and g2(x) at a=a_max
57 RooIntegralMorph lmorph("lmorph","lmorph",g1,g2,x,alpha) ;
58
59
60
61 // P l o t i n t e r p o l a t i n g p d f a t v a r i o u s a l p h a
62 // -----------------------------------------------------------------------------
63
64 // Show end points as blue curves
65 RooPlot* frame1 = x.frame() ;
66 g1.plotOn(frame1) ;
67 g2.plotOn(frame1) ;
68
69 // Show interpolated shapes in red
70 alpha.setVal(0.125) ;
71 lmorph.plotOn(frame1,LineColor(kRed)) ;
72 alpha.setVal(0.25) ;
73 lmorph.plotOn(frame1,LineColor(kRed)) ;
74 alpha.setVal(0.375) ;
75 lmorph.plotOn(frame1,LineColor(kRed)) ;
76 alpha.setVal(0.50) ;
77 lmorph.plotOn(frame1,LineColor(kRed)) ;
78 alpha.setVal(0.625) ;
79 lmorph.plotOn(frame1,LineColor(kRed)) ;
80 alpha.setVal(0.75) ;
81 lmorph.plotOn(frame1,LineColor(kRed)) ;
82 alpha.setVal(0.875) ;
83 lmorph.plotOn(frame1,LineColor(kRed)) ;
84 alpha.setVal(0.95) ;
85 lmorph.plotOn(frame1,LineColor(kRed)) ;
86
87
88
89 // S h o w 2 D d i s t r i b u t i o n o f p d f ( x , a l p h a )
90 // -----------------------------------------------------------------------
91
92 // Create 2D histogram
93 TH1* hh = lmorph.createHistogram("hh",x,Binning(40),YVar(alpha,Binning(40))) ;
94 hh->SetLineColor(kBlue) ;
95
96
97 // F i t p d f t o d a t a s e t w i t h a l p h a = 0 . 8
98 // -----------------------------------------------------------------
99
100 // Generate a toy dataset at alpha = 0.8
101 alpha=0.8 ;
102 RooDataSet* data = lmorph.generate(x,1000) ;
103
104 // Fit pdf to toy data
105 lmorph.setCacheAlpha(kTRUE) ;
106 lmorph.fitTo(*data,Verbose(kTRUE)) ;
107
108 // Plot fitted pdf and data overlaid
109 RooPlot* frame2 = x.frame(Bins(100)) ;
110 data->plotOn(frame2) ;
111 lmorph.plotOn(frame2) ;
112
113
114 // S c a n - l o g ( L ) v s a l p h a
115 // -----------------------------------------
116
117 // Show scan -log(L) of dataset w.r.t alpha
118 RooPlot* frame3 = alpha.frame(Bins(100),Range(0.1,0.9)) ;
119
120 // Make 2D pdf of histogram
121 RooNLLVar nll("nll","nll",lmorph,*data) ;
122 nll.plotOn(frame3,ShiftToZero()) ;
123
124 lmorph.setCacheAlpha(kFALSE) ;
125
126
127
128 TCanvas* c = new TCanvas("rf705_linearmorph","rf705_linearmorph",800,800) ;
129 c->Divide(2,2) ;
130 c->cd(1) ; gPad->SetLeftMargin(0.15) ; frame1->GetYaxis()->SetTitleOffset(1.6) ; frame1->Draw() ;
131 c->cd(2) ; gPad->SetLeftMargin(0.20) ; hh->GetZaxis()->SetTitleOffset(2.5) ; hh->Draw("surf") ;
132 c->cd(3) ; gPad->SetLeftMargin(0.15) ; frame3->GetYaxis()->SetTitleOffset(1.4) ; frame3->Draw() ;
133 c->cd(4) ; gPad->SetLeftMargin(0.15) ; frame2->GetYaxis()->SetTitleOffset(1.4) ; frame2->Draw() ;
134
135
136 return ;
137
138}
#define c(i)
Definition: RSha256.hxx:101
const Bool_t kFALSE
Definition: RtypesCore.h:88
const Bool_t kTRUE
Definition: RtypesCore.h:87
@ kRed
Definition: Rtypes.h:63
@ kBlue
Definition: Rtypes.h:63
#define gPad
Definition: TVirtualPad.h:286
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:31
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
Class RooIntegralMorph is an implementation of the histogram interpolation technique described by Ale...
Class RooNLLVar implements a a -log(likelihood) calculation from a dataset and a PDF.
Definition: RooNLLVar.h:26
A RooPlot is a plot frame and a container for graphics objects within that frame.
Definition: RooPlot.h:41
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1123
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:558
RooPolynomial implements a polynomial p.d.f of the form.
Definition: RooPolynomial.h:28
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
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:294
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
The Canvas class.
Definition: TCanvas.h:31
The TH1 histogram class.
Definition: TH1.h:56
TAxis * GetZaxis()
Definition: TH1.h:318
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2974
Double_t x[n]
Definition: legend1.C:17
RooCmdArg Binning(const RooAbsBinning &binning)
RooCmdArg YVar(const RooAbsRealLValue &var, const RooCmdArg &arg=RooCmdArg::none())
RooCmdArg ShiftToZero()
RooConstVar & RooConst(Double_t val)
RooCmdArg LineColor(Color_t color)
RooCmdArg Verbose(Bool_t flag=kTRUE)
RooCmdArg Bins(Int_t nbin)
Ta Range(0, 0, 1, 1)