Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
rf309_ndimplot.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook
4 /// Multidimensional models: making 2/3 dimensional plots of p.d.f.s and datasets
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 "RooConstVar.h"
14 #include "RooGaussian.h"
15 #include "RooProdPdf.h"
16 #include "TCanvas.h"
17 #include "TAxis.h"
18 #include "TH1.h"
19 #include "RooPlot.h"
20 using namespace RooFit;
21 
22 void rf309_ndimplot()
23 {
24 
25  // C r e a t e 2 D m o d e l a n d d a t a s e t
26  // -----------------------------------------------------
27 
28  // Create observables
29  RooRealVar x("x", "x", -5, 5);
30  RooRealVar y("y", "y", -5, 5);
31 
32  // Create parameters
33  RooRealVar a0("a0", "a0", -3.5, -5, 5);
34  RooRealVar a1("a1", "a1", -1.5, -1, 1);
35  RooRealVar sigma("sigma", "width of gaussian", 1.5);
36 
37  // Create interpreted function f(y) = a0 - a1*sqrt(10*abs(y))
38  RooFormulaVar fy("fy", "a0-a1*sqrt(10*abs(y))", RooArgSet(y, a0, a1));
39 
40  // Create gauss(x,f(y),s)
41  RooGaussian model("model", "Gaussian with shifting mean", x, fy, sigma);
42 
43  // Sample dataset from gauss(x,y)
44  RooDataSet *data = model.generate(RooArgSet(x, y), 10000);
45 
46  // M a k e 2 D p l o t s o f d a t a a n d m o d e l
47  // -------------------------------------------------------------
48 
49  // Create and fill ROOT 2D histogram (20x20 bins) with contents of dataset
50  // TH2D* hh_data = data->createHistogram("hh_data",x,Binning(20),YVar(y,Binning(20))) ;
51  TH1 *hh_data = data->createHistogram("x,y", 20, 20);
52 
53  // Create and fill ROOT 2D histogram (50x50 bins) with sampling of pdf
54  // TH2D* hh_pdf = model.createHistogram("hh_model",x,Binning(50),YVar(y,Binning(50))) ;
55  TH1 *hh_pdf = model.createHistogram("x,y", 50, 50);
56  hh_pdf->SetLineColor(kBlue);
57 
58  // C r e a t e 3 D m o d e l a n d d a t a s e t
59  // -----------------------------------------------------
60 
61  // Create observables
62  RooRealVar z("z", "z", -5, 5);
63 
64  RooGaussian gz("gz", "gz", z, RooConst(0), RooConst(2));
65  RooProdPdf model3("model3", "model3", RooArgSet(model, gz));
66 
67  RooDataSet *data3 = model3.generate(RooArgSet(x, y, z), 10000);
68 
69  // M a k e 3 D p l o t s o f d a t a a n d m o d e l
70  // -------------------------------------------------------------
71 
72  // Create and fill ROOT 2D histogram (8x8x8 bins) with contents of dataset
73  TH1 *hh_data3 = data3->createHistogram("hh_data3", x, Binning(8), YVar(y, Binning(8)), ZVar(z, Binning(8)));
74 
75  // Create and fill ROOT 2D histogram (20x20x20 bins) with sampling of pdf
76  TH1 *hh_pdf3 = model3.createHistogram("hh_model3", x, Binning(20), YVar(y, Binning(20)), ZVar(z, Binning(20)));
77  hh_pdf3->SetFillColor(kBlue);
78 
79  TCanvas *c1 = new TCanvas("rf309_2dimplot", "rf309_2dimplot", 800, 800);
80  c1->Divide(2, 2);
81  c1->cd(1);
82  gPad->SetLeftMargin(0.15);
83  hh_data->GetZaxis()->SetTitleOffset(1.4);
84  hh_data->Draw("lego");
85  c1->cd(2);
86  gPad->SetLeftMargin(0.20);
87  hh_pdf->GetZaxis()->SetTitleOffset(2.5);
88  hh_pdf->Draw("surf");
89  c1->cd(3);
90  gPad->SetLeftMargin(0.15);
91  hh_data->GetZaxis()->SetTitleOffset(1.4);
92  hh_data->Draw("box");
93  c1->cd(4);
94  gPad->SetLeftMargin(0.15);
95  hh_pdf->GetZaxis()->SetTitleOffset(2.5);
96  hh_pdf->Draw("cont3");
97 
98  TCanvas *c2 = new TCanvas("rf309_3dimplot", "rf309_3dimplot", 800, 400);
99  c2->Divide(2);
100  c2->cd(1);
101  gPad->SetLeftMargin(0.15);
102  hh_data3->GetZaxis()->SetTitleOffset(1.4);
103  hh_data3->Draw("lego");
104  c2->cd(2);
105  gPad->SetLeftMargin(0.15);
106  hh_pdf3->GetZaxis()->SetTitleOffset(1.4);
107  hh_pdf3->Draw("iso");
108 }