Logo ROOT  
Reference Guide
rf705_linearmorph.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook
4///
5/// Special p.d.f.'s: linear interpolation between p.d.f shapes using the 'Alex Read' algorithm
6///
7/// \macro_image
8/// \macro_output
9/// \macro_code
10///
11/// \date 07/2008
12/// \author Wouter Verkerke
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
27void rf705_linearmorph()
28{
29 // C r e a t e e n d p o i n t p d f s h a p e s
30 // ------------------------------------------------------
31
32 // Observable
33 RooRealVar x("x", "x", -20, 20);
34
35 // Lower end point shape: a Gaussian
36 RooRealVar g1mean("g1mean", "g1mean", -10);
37 RooGaussian g1("g1", "g1", x, g1mean, RooConst(2));
38
39 // Upper end point shape: a Polynomial
40 RooPolynomial g2("g2", "g2", x, RooArgSet(RooConst(-0.03), RooConst(-0.001)));
41
42 // C r e a t e i n t e r p o l a t i n g p d f
43 // -----------------------------------------------
44
45 // Create interpolation variable
46 RooRealVar alpha("alpha", "alpha", 0, 1.0);
47
48 // Specify sampling density on observable and interpolation variable
49 x.setBins(1000, "cache");
50 alpha.setBins(50, "cache");
51
52 // Construct interpolating pdf in (x,a) represent g1(x) at a=a_min
53 // and g2(x) at a=a_max
54 RooIntegralMorph lmorph("lmorph", "lmorph", g1, g2, x, alpha);
55
56 // 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
57 // -----------------------------------------------------------------------------
58
59 // Show end points as blue curves
60 RooPlot *frame1 = x.frame();
61 g1.plotOn(frame1);
62 g2.plotOn(frame1);
63
64 // Show interpolated shapes in red
65 alpha.setVal(0.125);
66 lmorph.plotOn(frame1, LineColor(kRed));
67 alpha.setVal(0.25);
68 lmorph.plotOn(frame1, LineColor(kRed));
69 alpha.setVal(0.375);
70 lmorph.plotOn(frame1, LineColor(kRed));
71 alpha.setVal(0.50);
72 lmorph.plotOn(frame1, LineColor(kRed));
73 alpha.setVal(0.625);
74 lmorph.plotOn(frame1, LineColor(kRed));
75 alpha.setVal(0.75);
76 lmorph.plotOn(frame1, LineColor(kRed));
77 alpha.setVal(0.875);
78 lmorph.plotOn(frame1, LineColor(kRed));
79 alpha.setVal(0.95);
80 lmorph.plotOn(frame1, LineColor(kRed));
81
82 // 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 )
83 // -----------------------------------------------------------------------
84
85 // Create 2D histogram
86 TH1 *hh = lmorph.createHistogram("hh", x, Binning(40), YVar(alpha, Binning(40)));
88
89 // 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
90 // -----------------------------------------------------------------
91
92 // Generate a toy dataset at alpha = 0.8
93 alpha = 0.8;
94 RooDataSet *data = lmorph.generate(x, 1000);
95
96 // Fit pdf to toy data
97 lmorph.setCacheAlpha(kTRUE);
98 lmorph.fitTo(*data, Verbose(kTRUE));
99
100 // Plot fitted pdf and data overlaid
101 RooPlot *frame2 = x.frame(Bins(100));
102 data->plotOn(frame2);
103 lmorph.plotOn(frame2);
104
105 // S c a n - l o g ( L ) v s a l p h a
106 // -----------------------------------------
107
108 // Show scan -log(L) of dataset w.r.t alpha
109 RooPlot *frame3 = alpha.frame(Bins(100), Range(0.1, 0.9));
110
111 // Make 2D pdf of histogram
112 RooNLLVar nll("nll", "nll", lmorph, *data);
113 nll.plotOn(frame3, ShiftToZero());
114
115 lmorph.setCacheAlpha(kFALSE);
116
117 TCanvas *c = new TCanvas("rf705_linearmorph", "rf705_linearmorph", 800, 800);
118 c->Divide(2, 2);
119 c->cd(1);
120 gPad->SetLeftMargin(0.15);
121 frame1->GetYaxis()->SetTitleOffset(1.6);
122 frame1->Draw();
123 c->cd(2);
124 gPad->SetLeftMargin(0.20);
125 hh->GetZaxis()->SetTitleOffset(2.5);
126 hh->Draw("surf");
127 c->cd(3);
128 gPad->SetLeftMargin(0.15);
129 frame3->GetYaxis()->SetTitleOffset(1.4);
130 frame3->Draw();
131 c->cd(4);
132 gPad->SetLeftMargin(0.15);
133 frame2->GetYaxis()->SetTitleOffset(1.4);
134 frame2->Draw();
135
136 return;
137}
#define c(i)
Definition: RSha256.hxx:101
const Bool_t kFALSE
Definition: RtypesCore.h:90
const Bool_t kTRUE
Definition: RtypesCore.h:89
@ kRed
Definition: Rtypes.h:64
@ kBlue
Definition: Rtypes.h:64
#define gPad
Definition: TVirtualPad.h:287
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
Calls RooPlot* plotOn(RooPlot* frame, const RooLinkedList& cmdList) const ;.
Definition: RooAbsData.cxx:546
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:33
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 -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:44
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1277
static RooPlot * frame(const RooAbsRealLValue &var, Double_t xmin, Double_t xmax, Int_t nBins)
Create a new frame for a given variable in x.
Definition: RooPlot.cxx:277
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:712
RooPolynomial implements a polynomial p.d.f of the form.
Definition: RooPolynomial.h:28
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:35
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:294
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
The Canvas class.
Definition: TCanvas.h:27
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:2998
RooCmdArg YVar(const RooAbsRealLValue &var, const RooCmdArg &arg=RooCmdArg::none())
RooConstVar & RooConst(Double_t val)
RooCmdArg Bins(Int_t nbin)
RooCmdArg Verbose(Bool_t flag=kTRUE)
RooCmdArg Binning(const RooAbsBinning &binning)
RooCmdArg ShiftToZero()
RooCmdArg LineColor(Color_t color)
Double_t x[n]
Definition: legend1.C:17
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Ta Range(0, 0, 1, 1)