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"
25 using namespace RooFit;
26 
27 void 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)));
87  hh->SetLineColor(kBlue);
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 }
c
#define c(i)
Definition: RSha256.hxx:119
RooPlot::Draw
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:691
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
RooFit::Bins
RooCmdArg Bins(Int_t nbin)
Definition: RooGlobalFunc.cxx:174
RooIntegralMorph.h
RooGaussian.h
RooFit::ShiftToZero
RooCmdArg ShiftToZero()
Definition: RooGlobalFunc.cxx:50
x
Double_t x[n]
Definition: legend1.C:17
RooGaussian
Definition: RooGaussian.h:25
TCanvas.h
RooFit::YVar
RooCmdArg YVar(const RooAbsRealLValue &var, const RooCmdArg &arg=RooCmdArg::none())
Definition: RooGlobalFunc.cxx:240
RooFit::Binning
RooCmdArg Binning(const RooAbsBinning &binning)
Definition: RooGlobalFunc.cxx:82
TH1::GetZaxis
TAxis * GetZaxis()
Definition: TH1.h:319
RooDataSet.h
RooNLLVar.h
RooPolynomial.h
RooPlot::frame
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
RooFit
Definition: RooCFunction1Binding.h:29
RooPolynomial
Definition: RooPolynomial.h:28
RooAbsData::plotOn
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
Definition: RooAbsData.cxx:547
RooFit::Verbose
RooCmdArg Verbose(Bool_t flag=kTRUE)
Definition: RooGlobalFunc.cxx:186
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
RooNLLVar
Definition: RooNLLVar.h:27
RooPlot.h
RooPlot::GetYaxis
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1256
RooPlot
Definition: RooPlot.h:44
RooRealVar.h
kRed
@ kRed
Definition: Rtypes.h:66
RooConstVar.h
RooFit::LineColor
RooCmdArg LineColor(Color_t color)
Definition: RooGlobalFunc.cxx:56
TCanvas
Definition: TCanvas.h:23
TAxis.h
TH1
Definition: TH1.h:57
RooFit::Range
RooCmdArg Range(const char *rangeName, Bool_t adjustNorm=kTRUE)
Definition: RooGlobalFunc.cxx:52
kBlue
@ kBlue
Definition: Rtypes.h:66
gPad
#define gPad
Definition: TVirtualPad.h:287
RooDataSet
Definition: RooDataSet.h:33
TAttAxis::SetTitleOffset
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:293
RooRealVar
Definition: RooRealVar.h:35
TH1.h
RooArgSet
Definition: RooArgSet.h:28
RooIntegralMorph
Definition: RooIntegralMorph.h:26
TH1::Draw
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2997
RooFit::RooConst
RooConstVar & RooConst(Double_t val)
Definition: RooGlobalFunc.cxx:341