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_code
8/// \macro_output
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 "TCanvas.h"
19#include "TAxis.h"
20#include "RooPlot.h"
21#include "TH1.h"
22using namespace RooFit;
23
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, 2.0);
35
36 // Upper end point shape: a Polynomial
37 RooPolynomial g2("g2", "g2", x, RooArgSet(-0.03, -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 std::unique_ptr<RooDataSet> data{lmorph.generate(x, 1000)};
92
93 // Fit pdf to toy data
94 lmorph.setCacheAlpha(true);
95 lmorph.fitTo(*data, Verbose(true), PrintLevel(-1));
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 std::unique_ptr<RooAbsReal> nll{lmorph.createNLL(*data)};
110 nll->plotOn(frame3, ShiftToZero());
111
112 lmorph.setCacheAlpha(false);
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
@ kRed
Definition Rtypes.h:66
@ kBlue
Definition Rtypes.h:66
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
#define gPad
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
Plain Gaussian p.d.f.
Definition RooGaussian.h:24
Class RooIntegralMorph is an implementation of the histogram interpolation technique described by Ale...
Plot frame and a container for graphics objects within that frame.
Definition RooPlot.h:43
static RooPlot * frame(const RooAbsRealLValue &var, double xmin, double xmax, Int_t nBins)
Create a new frame for a given variable in x.
Definition RooPlot.cxx:237
TAxis * GetYaxis() const
Definition RooPlot.cxx:1276
void Draw(Option_t *options=nullptr) override
Draw this plot and all of the elements it contains.
Definition RooPlot.cxx:649
RooPolynomial implements a polynomial p.d.f of the form.
Variable that can be changed from the outside.
Definition RooRealVar.h:37
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition TAttAxis.cxx:298
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:59
TAxis * GetZaxis()
Definition TH1.h:326
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3066
RooCmdArg YVar(const RooAbsRealLValue &var, const RooCmdArg &arg={})
RooCmdArg Bins(Int_t nbin)
RooCmdArg PrintLevel(Int_t code)
RooCmdArg Verbose(bool flag=true)
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 JSONIO.h:26
Ta Range(0, 0, 1, 1)