ROOT   Reference Guide
Searching...
No Matches
rf705_linearmorph.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook
4/// Special pdf's: linear interpolation between pdf shapes using the 'Alex Read' algorithm
5///
6/// \macro_image
7/// \macro_output
8/// \macro_code
9///
10/// \date July 2008
11/// \author Wouter Verkerke
12
13#include "RooRealVar.h"
14#include "RooDataSet.h"
15#include "RooGaussian.h"
16#include "RooConstVar.h"
17#include "RooPolynomial.h"
18#include "RooIntegralMorph.h"
19#include "RooNLLVar.h"
20#include "TCanvas.h"
21#include "TAxis.h"
22#include "RooPlot.h"
23#include "TH1.h"
24using namespace RooFit;
25
26void rf705_linearmorph()
27{
28 // C r e a t e e n d p o i n t p d f s h a p e s
29 // ------------------------------------------------------
30
31 // Observable
32 RooRealVar x("x", "x", -20, 20);
33
34 // Lower end point shape: a Gaussian
35 RooRealVar g1mean("g1mean", "g1mean", -10);
36 RooGaussian g1("g1", "g1", x, g1mean, RooConst(2));
37
38 // Upper end point shape: a Polynomial
39 RooPolynomial g2("g2", "g2", x, RooArgSet(RooConst(-0.03), RooConst(-0.001)));
40
41 // C r e a t e i n t e r p o l a t i n g p d f
42 // -----------------------------------------------
43
44 // Create interpolation variable
45 RooRealVar alpha("alpha", "alpha", 0, 1.0);
46
47 // Specify sampling density on observable and interpolation variable
48 x.setBins(1000, "cache");
49 alpha.setBins(50, "cache");
50
51 // Construct interpolating pdf in (x,a) represent g1(x) at a=a_min
52 // and g2(x) at a=a_max
53 RooIntegralMorph lmorph("lmorph", "lmorph", g1, g2, x, alpha);
54
55 // 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
56 // -----------------------------------------------------------------------------
57
58 // Show end points as blue curves
59 RooPlot *frame1 = x.frame();
60 g1.plotOn(frame1);
61 g2.plotOn(frame1);
62
63 // Show interpolated shapes in red
64 alpha.setVal(0.125);
65 lmorph.plotOn(frame1, LineColor(kRed));
66 alpha.setVal(0.25);
67 lmorph.plotOn(frame1, LineColor(kRed));
68 alpha.setVal(0.375);
69 lmorph.plotOn(frame1, LineColor(kRed));
70 alpha.setVal(0.50);
71 lmorph.plotOn(frame1, LineColor(kRed));
72 alpha.setVal(0.625);
73 lmorph.plotOn(frame1, LineColor(kRed));
74 alpha.setVal(0.75);
75 lmorph.plotOn(frame1, LineColor(kRed));
76 alpha.setVal(0.875);
77 lmorph.plotOn(frame1, LineColor(kRed));
78 alpha.setVal(0.95);
79 lmorph.plotOn(frame1, LineColor(kRed));
80
81 // 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 )
82 // -----------------------------------------------------------------------
83
84 // Create 2D histogram
85 TH1 *hh = lmorph.createHistogram("hh", x, Binning(40), YVar(alpha, Binning(40)));
87
88 // 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
89 // -----------------------------------------------------------------
90
91 // Generate a toy dataset at alpha = 0.8
92 alpha = 0.8;
93 RooDataSet *data = lmorph.generate(x, 1000);
94
95 // Fit pdf to toy data
96 lmorph.setCacheAlpha(kTRUE);
97 lmorph.fitTo(*data, Verbose(kTRUE));
98
99 // Plot fitted pdf and data overlaid
100 RooPlot *frame2 = x.frame(Bins(100));
101 data->plotOn(frame2);
102 lmorph.plotOn(frame2);
103
104 // S c a n - l o g ( L ) v s a l p h a
105 // -----------------------------------------
106
107 // Show scan -log(L) of dataset w.r.t alpha
108 RooPlot *frame3 = alpha.frame(Bins(100), Range(0.1, 0.9));
109
110 // Make 2D pdf of histogram
111 RooNLLVar nll("nll", "nll", lmorph, *data);
112 nll.plotOn(frame3, ShiftToZero());
113
114 lmorph.setCacheAlpha(kFALSE);
115
116 TCanvas *c = new TCanvas("rf705_linearmorph", "rf705_linearmorph", 800, 800);
117 c->Divide(2, 2);
118 c->cd(1);
120 frame1->GetYaxis()->SetTitleOffset(1.6);
121 frame1->Draw();
122 c->cd(2);
124 hh->GetZaxis()->SetTitleOffset(2.5);
125 hh->Draw("surf");
126 c->cd(3);
128 frame3->GetYaxis()->SetTitleOffset(1.4);
129 frame3->Draw();
130 c->cd(4);
132 frame2->GetYaxis()->SetTitleOffset(1.4);
133 frame2->Draw();
134
135 return;
136}
#define c(i)
Definition RSha256.hxx:101
const Bool_t kFALSE
Definition RtypesCore.h:92
const Bool_t kTRUE
Definition RtypesCore.h:91
@ kRed
Definition Rtypes.h:66
@ kBlue
Definition Rtypes.h:66
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
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:29
RooDataSet is a container class to hold unbinned data.
Definition RooDataSet.h:33
Plain Gaussian p.d.f.
Definition RooGaussian.h:24
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:30
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:1263
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:249
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition RooPlot.cxx:691
RooPolynomial implements a polynomial p.d.f of the form.
RooRealVar represents a variable that can be changed from the outside.
Definition RooRealVar.h:39
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition TAttAxis.cxx:293
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
The Canvas class.
Definition TCanvas.h:23
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:58
TAxis * GetZaxis()
Definition TH1.h:322
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition TH1.cxx:3073
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)