Logo ROOT  
Reference Guide
 
Loading...
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 "RooPolynomial.h"
17#include "RooIntegralMorph.h"
18#include "RooNLLVar.h"
19#include "TCanvas.h"
20#include "TAxis.h"
21#include "RooPlot.h"
22#include "TH1.h"
23using namespace RooFit;
24
26{
27 // C r e a t e e n d p o i n t p d f s h a p e s
28 // ------------------------------------------------------
29
30 // Observable
31 RooRealVar x("x", "x", -20, 20);
32
33 // Lower end point shape: a Gaussian
34 RooRealVar g1mean("g1mean", "g1mean", -10);
35 RooGaussian g1("g1", "g1", x, g1mean, RooConst(2));
36
37 // Upper end point shape: a Polynomial
38 RooPolynomial g2("g2", "g2", x, RooArgSet(-0.03, -0.001));
39
40 // C r e a t e i n t e r p o l a t i n g p d f
41 // -----------------------------------------------
42
43 // Create interpolation variable
44 RooRealVar alpha("alpha", "alpha", 0, 1.0);
45
46 // Specify sampling density on observable and interpolation variable
47 x.setBins(1000, "cache");
48 alpha.setBins(50, "cache");
49
50 // Construct interpolating pdf in (x,a) represent g1(x) at a=a_min
51 // and g2(x) at a=a_max
52 RooIntegralMorph lmorph("lmorph", "lmorph", g1, g2, x, alpha);
53
54 // 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
55 // -----------------------------------------------------------------------------
56
57 // Show end points as blue curves
58 RooPlot *frame1 = x.frame();
59 g1.plotOn(frame1);
60 g2.plotOn(frame1);
61
62 // Show interpolated shapes in red
63 alpha.setVal(0.125);
64 lmorph.plotOn(frame1, LineColor(kRed));
65 alpha.setVal(0.25);
66 lmorph.plotOn(frame1, LineColor(kRed));
67 alpha.setVal(0.375);
68 lmorph.plotOn(frame1, LineColor(kRed));
69 alpha.setVal(0.50);
70 lmorph.plotOn(frame1, LineColor(kRed));
71 alpha.setVal(0.625);
72 lmorph.plotOn(frame1, LineColor(kRed));
73 alpha.setVal(0.75);
74 lmorph.plotOn(frame1, LineColor(kRed));
75 alpha.setVal(0.875);
76 lmorph.plotOn(frame1, LineColor(kRed));
77 alpha.setVal(0.95);
78 lmorph.plotOn(frame1, LineColor(kRed));
79
80 // 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 )
81 // -----------------------------------------------------------------------
82
83 // Create 2D histogram
84 TH1 *hh = lmorph.createHistogram("hh", x, Binning(40), YVar(alpha, Binning(40)));
86
87 // 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
88 // -----------------------------------------------------------------
89
90 // Generate a toy dataset at alpha = 0.8
91 alpha = 0.8;
92 RooDataSet *data = lmorph.generate(x, 1000);
93
94 // Fit pdf to toy data
95 lmorph.setCacheAlpha(kTRUE);
96 lmorph.fitTo(*data, Verbose(kTRUE));
97
98 // Plot fitted pdf and data overlaid
99 RooPlot *frame2 = x.frame(Bins(100));
100 data->plotOn(frame2);
101 lmorph.plotOn(frame2);
102
103 // S c a n - l o g ( L ) v s a l p h a
104 // -----------------------------------------
105
106 // Show scan -log(L) of dataset w.r.t alpha
107 RooPlot *frame3 = alpha.frame(Bins(100), Range(0.1, 0.9));
108
109 // Make 2D pdf of histogram
110 RooNLLVar nll("nll", "nll", lmorph, *data);
111 nll.plotOn(frame3, ShiftToZero());
112
113 lmorph.setCacheAlpha(kFALSE);
114
115 TCanvas *c = new TCanvas("rf705_linearmorph", "rf705_linearmorph", 800, 800);
116 c->Divide(2, 2);
117 c->cd(1);
118 gPad->SetLeftMargin(0.15);
119 frame1->GetYaxis()->SetTitleOffset(1.6);
120 frame1->Draw();
121 c->cd(2);
122 gPad->SetLeftMargin(0.20);
123 hh->GetZaxis()->SetTitleOffset(2.5);
124 hh->Draw("surf");
125 c->cd(3);
126 gPad->SetLeftMargin(0.15);
127 frame3->GetYaxis()->SetTitleOffset(1.4);
128 frame3->Draw();
129 c->cd(4);
130 gPad->SetLeftMargin(0.15);
131 frame2->GetYaxis()->SetTitleOffset(1.4);
132 frame2->Draw();
133
134 return;
135}
#define c(i)
Definition RSha256.hxx:101
const Bool_t kFALSE
Definition RtypesCore.h:101
const Bool_t kTRUE
Definition RtypesCore.h:100
@ kRed
Definition Rtypes.h:66
@ kBlue
Definition Rtypes.h:66
#define gPad
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:35
RooDataSet is a container class to hold unbinned data.
Definition RooDataSet.h:36
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:1278
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:661
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:302
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:3074
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...
Definition Common.h:18
Ta Range(0, 0, 1, 1)