Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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"
22 using namespace RooFit;
23 
24 void 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)));
84  hh->SetLineColor(kBlue);
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 }