Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
rf303_conditional.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook -js
4 /// Multidimensional models: use of tailored p.d.f as conditional p.d.fs.s
5 ///
6 /// pdf = gauss(x,f(y),sx | y ) with f(y) = a0 + a1*y
7 ///
8 /// \macro_image
9 /// \macro_output
10 /// \macro_code
11 /// \author 07/2008 - Wouter Verkerke
12 
13 #include "RooRealVar.h"
14 #include "RooDataSet.h"
15 #include "RooDataHist.h"
16 #include "RooGaussian.h"
17 #include "RooPolyVar.h"
18 #include "RooProdPdf.h"
19 #include "RooPlot.h"
20 #include "TRandom.h"
21 #include "TCanvas.h"
22 #include "TAxis.h"
23 #include "TH1.h"
24 
25 using namespace RooFit;
26 
27 RooDataSet *makeFakeDataXY();
28 
29 void rf303_conditional()
30 {
31  // S e t u p c o m p o s e d m o d e l g a u s s ( x , m ( y ) , s )
32  // -----------------------------------------------------------------------
33 
34  // Create observables
35  RooRealVar x("x", "x", -10, 10);
36  RooRealVar y("y", "y", -10, 10);
37 
38  // Create function f(y) = a0 + a1*y
39  RooRealVar a0("a0", "a0", -0.5, -5, 5);
40  RooRealVar a1("a1", "a1", -0.5, -1, 1);
41  RooPolyVar fy("fy", "fy", y, RooArgSet(a0, a1));
42 
43  // Create gauss(x,f(y),s)
44  RooRealVar sigma("sigma", "width of gaussian", 0.5, 0.1, 2.0);
45  RooGaussian model("model", "Gaussian with shifting mean", x, fy, sigma);
46 
47  // Obtain fake external experimental dataset with values for x and y
48  RooDataSet *expDataXY = makeFakeDataXY();
49 
50  // G e n e r a t e d a t a f r o m c o n d i t i o n a l p . d . f m o d e l ( x | y )
51  // ---------------------------------------------------------------------------------------------
52 
53  // Make subset of experimental data with only y values
54  RooDataSet *expDataY = (RooDataSet *)expDataXY->reduce(y);
55 
56  // Generate 10000 events in x obtained from _conditional_ model(x|y) with y values taken from experimental data
57  RooDataSet *data = model.generate(x, ProtoData(*expDataY));
58  data->Print();
59 
60  // F i t c o n d i t i o n a l p . d . f m o d e l ( x | y ) t o d a t a
61  // ---------------------------------------------------------------------------------------------
62 
63  model.fitTo(*expDataXY, ConditionalObservables(y));
64 
65  // P r o j e c t c o n d i t i o n a l p . d . f o n x a n d y d i m e n s i o n s
66  // ---------------------------------------------------------------------------------------------
67 
68  // Plot x distribution of data and projection of model on x = 1/Ndata sum(data(y_i)) model(x;y_i)
69  RooPlot *xframe = x.frame();
70  expDataXY->plotOn(xframe);
71  model.plotOn(xframe, ProjWData(*expDataY));
72 
73  // Speed up (and approximate) projection by using binned clone of data for projection
74  RooAbsData *binnedDataY = expDataY->binnedClone();
75  model.plotOn(xframe, ProjWData(*binnedDataY), LineColor(kCyan), LineStyle(kDotted));
76 
77  // Show effect of projection with too coarse binning
78  ((RooRealVar *)expDataY->get()->find("y"))->setBins(5);
79  RooAbsData *binnedDataY2 = expDataY->binnedClone();
80  model.plotOn(xframe, ProjWData(*binnedDataY2), LineColor(kRed));
81 
82  // Make canvas and draw RooPlots
83  new TCanvas("rf303_conditional", "rf303_conditional", 600, 460);
84  gPad->SetLeftMargin(0.15);
85  xframe->GetYaxis()->SetTitleOffset(1.2);
86  xframe->Draw();
87 }
88 
89 RooDataSet *makeFakeDataXY()
90 {
91  RooRealVar x("x", "x", -10, 10);
92  RooRealVar y("y", "y", -10, 10);
93  RooArgSet coord(x, y);
94 
95  RooDataSet *d = new RooDataSet("d", "d", RooArgSet(x, y));
96 
97  for (int i = 0; i < 10000; i++) {
98  Double_t tmpy = gRandom->Gaus(0, 10);
99  Double_t tmpx = gRandom->Gaus(0.5 * tmpy, 1);
100  if (fabs(tmpy) < 10 && fabs(tmpx) < 10) {
101  x = tmpx;
102  y = tmpy;
103  d->add(coord);
104  }
105  }
106 
107  return d;
108 }