Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
rf308_normintegration2d.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook
4 /// Multidimensional models: normalization and integration of p.d.fs, construction of cumulative distribution functions
5 /// from p.d.f.s in two dimensions
6 ///
7 /// \macro_image
8 /// \macro_output
9 /// \macro_code
10 /// \author 07/2008 - Wouter Verkerke
11 
12 #include "RooRealVar.h"
13 #include "RooGaussian.h"
14 #include "RooConstVar.h"
15 #include "RooProdPdf.h"
16 #include "RooAbsReal.h"
17 #include "RooPlot.h"
18 #include "TCanvas.h"
19 #include "TAxis.h"
20 #include "TH1.h"
21 using namespace RooFit;
22 
23 void rf308_normintegration2d()
24 {
25  // S e t u p m o d e l
26  // ---------------------
27 
28  // Create observables x,y
29  RooRealVar x("x", "x", -10, 10);
30  RooRealVar y("y", "y", -10, 10);
31 
32  // Create p.d.f. gaussx(x,-2,3), gaussy(y,2,2)
33  RooGaussian gx("gx", "gx", x, RooConst(-2), RooConst(3));
34  RooGaussian gy("gy", "gy", y, RooConst(+2), RooConst(2));
35 
36  // Create gxy = gx(x)*gy(y)
37  RooProdPdf gxy("gxy", "gxy", RooArgSet(gx, gy));
38 
39  // R e t r i e v e r a w & n o r m a l i z e d v a l u e s o f R o o F i t p . d . f . s
40  // --------------------------------------------------------------------------------------------------
41 
42  // Return 'raw' unnormalized value of gx
43  cout << "gxy = " << gxy.getVal() << endl;
44 
45  // Return value of gxy normalized over x _and_ y in range [-10,10]
46  RooArgSet nset_xy(x, y);
47  cout << "gx_Norm[x,y] = " << gxy.getVal(&nset_xy) << endl;
48 
49  // Create object representing integral over gx
50  // which is used to calculate gx_Norm[x,y] == gx / gx_Int[x,y]
51  RooAbsReal *igxy = gxy.createIntegral(RooArgSet(x, y));
52  cout << "gx_Int[x,y] = " << igxy->getVal() << endl;
53 
54  // NB: it is also possible to do the following
55 
56  // Return value of gxy normalized over x in range [-10,10] (i.e. treating y as parameter)
57  RooArgSet nset_x(x);
58  cout << "gx_Norm[x] = " << gxy.getVal(&nset_x) << endl;
59 
60  // Return value of gxy normalized over y in range [-10,10] (i.e. treating x as parameter)
61  RooArgSet nset_y(y);
62  cout << "gx_Norm[y] = " << gxy.getVal(&nset_y) << endl;
63 
64  // I n t e g r a t e n o r m a l i z e d p d f o v e r s u b r a n g e
65  // ----------------------------------------------------------------------------
66 
67  // Define a range named "signal" in x from -5,5
68  x.setRange("signal", -5, 5);
69  y.setRange("signal", -3, 3);
70 
71  // Create an integral of gxy_Norm[x,y] over x and y in range "signal"
72  // This is the fraction of of p.d.f. gxy_Norm[x,y] which is in the
73  // range named "signal"
74  RooAbsReal *igxy_sig = gxy.createIntegral(RooArgSet(x, y), NormSet(RooArgSet(x, y)), Range("signal"));
75  cout << "gx_Int[x,y|signal]_Norm[x,y] = " << igxy_sig->getVal() << endl;
76 
77  // C o n s t r u c t c u m u l a t i v e d i s t r i b u t i o n f u n c t i o n f r o m p d f
78  // -----------------------------------------------------------------------------------------------------
79 
80  // Create the cumulative distribution function of gx
81  // i.e. calculate Int[-10,x] gx(x') dx'
82  RooAbsReal *gxy_cdf = gxy.createCdf(RooArgSet(x, y));
83 
84  // Plot cdf of gx versus x
85  TH1 *hh_cdf = gxy_cdf->createHistogram("hh_cdf", x, Binning(40), YVar(y, Binning(40)));
86  hh_cdf->SetLineColor(kBlue);
87 
88  new TCanvas("rf308_normintegration2d", "rf308_normintegration2d", 600, 600);
89  gPad->SetLeftMargin(0.15);
90  hh_cdf->GetZaxis()->SetTitleOffset(1.8);
91  hh_cdf->Draw("surf");
92 }