Logo ROOT  
Reference Guide
rf303_conditional.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook -js
4 ///
5 /// Multidimensional models: use of tailored p.d.f as conditional p.d.fs.s
6 ///
7 /// `pdf = gauss(x,f(y),sx | y )` with `f(y) = a0 + a1*y`
8 ///
9 /// \macro_image
10 /// \macro_output
11 /// \macro_code
12 ///
13 /// \date 07/2008
14 /// \author Wouter Verkerke
15 
16 #include "RooRealVar.h"
17 #include "RooDataSet.h"
18 #include "RooDataHist.h"
19 #include "RooGaussian.h"
20 #include "RooPolyVar.h"
21 #include "RooProdPdf.h"
22 #include "RooPlot.h"
23 #include "TRandom.h"
24 #include "TCanvas.h"
25 #include "TAxis.h"
26 #include "TH1.h"
27 
28 using namespace RooFit;
29 
30 RooDataSet *makeFakeDataXY();
31 
32 void rf303_conditional()
33 {
34  // 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 )
35  // -----------------------------------------------------------------------
36 
37  // Create observables
38  RooRealVar x("x", "x", -10, 10);
39  RooRealVar y("y", "y", -10, 10);
40 
41  // Create function f(y) = a0 + a1*y
42  RooRealVar a0("a0", "a0", -0.5, -5, 5);
43  RooRealVar a1("a1", "a1", -0.5, -1, 1);
44  RooPolyVar fy("fy", "fy", y, RooArgSet(a0, a1));
45 
46  // Create gauss(x,f(y),s)
47  RooRealVar sigma("sigma", "width of gaussian", 0.5, 0.1, 2.0);
48  RooGaussian model("model", "Gaussian with shifting mean", x, fy, sigma);
49 
50  // Obtain fake external experimental dataset with values for x and y
51  RooDataSet *expDataXY = makeFakeDataXY();
52 
53  // 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 )
54  // ---------------------------------------------------------------------------------------------
55 
56  // Make subset of experimental data with only y values
57  RooDataSet *expDataY = (RooDataSet *)expDataXY->reduce(y);
58 
59  // Generate 10000 events in x obtained from _conditional_ model(x|y) with y values taken from experimental data
60  RooDataSet *data = model.generate(x, ProtoData(*expDataY));
61  data->Print();
62 
63  // 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
64  // ---------------------------------------------------------------------------------------------
65 
66  model.fitTo(*expDataXY, ConditionalObservables(y));
67 
68  // 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
69  // ---------------------------------------------------------------------------------------------
70 
71  // Plot x distribution of data and projection of model on x = 1/Ndata sum(data(y_i)) model(x;y_i)
72  RooPlot *xframe = x.frame();
73  expDataXY->plotOn(xframe);
74  model.plotOn(xframe, ProjWData(*expDataY));
75 
76  // Speed up (and approximate) projection by using binned clone of data for projection
77  RooAbsData *binnedDataY = expDataY->binnedClone();
78  model.plotOn(xframe, ProjWData(*binnedDataY), LineColor(kCyan), LineStyle(kDotted));
79 
80  // Show effect of projection with too coarse binning
81  ((RooRealVar *)expDataY->get()->find("y"))->setBins(5);
82  RooAbsData *binnedDataY2 = expDataY->binnedClone();
83  model.plotOn(xframe, ProjWData(*binnedDataY2), LineColor(kRed));
84 
85  // Make canvas and draw RooPlots
86  new TCanvas("rf303_conditional", "rf303_conditional", 600, 460);
87  gPad->SetLeftMargin(0.15);
88  xframe->GetYaxis()->SetTitleOffset(1.2);
89  xframe->Draw();
90 }
91 
92 RooDataSet *makeFakeDataXY()
93 {
94  RooRealVar x("x", "x", -10, 10);
95  RooRealVar y("y", "y", -10, 10);
96  RooArgSet coord(x, y);
97 
98  RooDataSet *d = new RooDataSet("d", "d", RooArgSet(x, y));
99 
100  for (int i = 0; i < 10000; i++) {
101  Double_t tmpy = gRandom->Gaus(0, 10);
102  Double_t tmpx = gRandom->Gaus(0.5 * tmpy, 1);
103  if (fabs(tmpy) < 10 && fabs(tmpx) < 10) {
104  x = tmpx;
105  y = tmpy;
106  d->add(coord);
107  }
108  }
109 
110  return d;
111 }
RooPlot::Draw
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:691
TRandom::Gaus
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition: TRandom.cxx:263
RooFit::ProjWData
RooCmdArg ProjWData(const RooAbsData &projData, Bool_t binData=kFALSE)
Definition: RooGlobalFunc.cxx:46
RooAbsData
Definition: RooAbsData.h:46
RooGaussian.h
TRandom.h
RooAbsData::Print
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
Definition: RooAbsData.h:177
RooAbsCollection::find
RooAbsArg * find(const char *name) const
Find object with given name in list.
Definition: RooAbsCollection.cxx:813
x
Double_t x[n]
Definition: legend1.C:17
RooGaussian
Definition: RooGaussian.h:25
TCanvas.h
RooDataSet.h
RooDataSet::get
virtual const RooArgSet * get(Int_t index) const override
Return RooArgSet with coordinates of event 'index'.
Definition: RooDataSet.cxx:1038
RooPolyVar.h
ROOT::Math::fabs
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
Definition: UnaryOperators.h:131
kCyan
@ kCyan
Definition: Rtypes.h:66
RooFit::ProtoData
RooCmdArg ProtoData(const RooDataSet &protoData, Bool_t randomizeOrder=kFALSE, Bool_t resample=kFALSE)
Definition: RooGlobalFunc.cxx:228
RooProdPdf.h
RooFit
Definition: RooCFunction1Binding.h:29
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
RooDataHist.h
RooPlot.h
RooPlot::GetYaxis
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1256
gRandom
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
RooPlot
Definition: RooPlot.h:44
y
Double_t y[n]
Definition: legend1.C:17
RooRealVar.h
RooFit::ConditionalObservables
RooCmdArg ConditionalObservables(const RooArgSet &set)
Definition: RooGlobalFunc.cxx:196
kRed
@ kRed
Definition: Rtypes.h:66
RooPolyVar
Definition: RooPolyVar.h:28
sigma
const Double_t sigma
Definition: h1analysisProxy.h:11
RooFit::LineColor
RooCmdArg LineColor(Color_t color)
Definition: RooGlobalFunc.cxx:56
Double_t
double Double_t
Definition: RtypesCore.h:59
TCanvas
Definition: TCanvas.h:23
RooAbsData::reduce
RooAbsData * reduce(const RooCmdArg &arg1, const RooCmdArg &arg2=RooCmdArg(), const RooCmdArg &arg3=RooCmdArg(), const RooCmdArg &arg4=RooCmdArg(), const RooCmdArg &arg5=RooCmdArg(), const RooCmdArg &arg6=RooCmdArg(), const RooCmdArg &arg7=RooCmdArg(), const RooCmdArg &arg8=RooCmdArg())
Create a reduced copy of this dataset.
Definition: RooAbsData.cxx:382
TAxis.h
RooDataSet::binnedClone
RooDataHist * binnedClone(const char *newName=0, const char *newTitle=0) const
Return binned clone of this dataset.
Definition: RooDataSet.cxx:972
d
#define d(i)
Definition: RSha256.hxx:120
gPad
#define gPad
Definition: TVirtualPad.h:287
RooDataSet
Definition: RooDataSet.h:33
make_cnn_model.model
model
Definition: make_cnn_model.py:6
RooFit::LineStyle
RooCmdArg LineStyle(Style_t style)
Definition: RooGlobalFunc.cxx:57
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
kDotted
@ kDotted
Definition: TAttLine.h:48
RooArgSet
Definition: RooArgSet.h:28