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