Logo ROOT  
Reference Guide
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 pdf as conditional pdfs.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 ///
12 /// \date July 2008
13 /// \author Wouter Verkerke
14 
15 #include "RooRealVar.h"
16 #include "RooDataSet.h"
17 #include "RooDataHist.h"
18 #include "RooGaussian.h"
19 #include "RooPolyVar.h"
20 #include "RooProdPdf.h"
21 #include "RooPlot.h"
22 #include "TRandom.h"
23 #include "TCanvas.h"
24 #include "TAxis.h"
25 #include "TH1.h"
26 
27 using namespace RooFit;
28 
29 RooDataSet *makeFakeDataXY();
30 
31 void rf303_conditional()
32 {
33  // 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 )
34  // -----------------------------------------------------------------------
35 
36  // Create observables
37  RooRealVar x("x", "x", -10, 10);
38  RooRealVar y("y", "y", -10, 10);
39 
40  // Create function f(y) = a0 + a1*y
41  RooRealVar a0("a0", "a0", -0.5, -5, 5);
42  RooRealVar a1("a1", "a1", -0.5, -1, 1);
43  RooPolyVar fy("fy", "fy", y, RooArgSet(a0, a1));
44 
45  // Create gauss(x,f(y),s)
46  RooRealVar sigma("sigma", "width of gaussian", 0.5, 0.1, 2.0);
47  RooGaussian model("model", "Gaussian with shifting mean", x, fy, sigma);
48 
49  // Obtain fake external experimental dataset with values for x and y
50  RooDataSet *expDataXY = makeFakeDataXY();
51 
52  // 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 )
53  // ---------------------------------------------------------------------------------------------
54 
55  // Make subset of experimental data with only y values
56  RooDataSet *expDataY = (RooDataSet *)expDataXY->reduce(y);
57 
58  // Generate 10000 events in x obtained from _conditional_ model(x|y) with y values taken from experimental data
59  RooDataSet *data = model.generate(x, ProtoData(*expDataY));
60  data->Print();
61 
62  // 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
63  // ---------------------------------------------------------------------------------------------
64 
65  model.fitTo(*expDataXY, ConditionalObservables(y));
66 
67  // 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
68  // ---------------------------------------------------------------------------------------------
69 
70  // Plot x distribution of data and projection of model on x = 1/Ndata sum(data(y_i)) model(x;y_i)
71  RooPlot *xframe = x.frame();
72  expDataXY->plotOn(xframe);
73  model.plotOn(xframe, ProjWData(*expDataY));
74 
75  // Speed up (and approximate) projection by using binned clone of data for projection
76  RooAbsData *binnedDataY = expDataY->binnedClone();
77  model.plotOn(xframe, ProjWData(*binnedDataY), LineColor(kCyan), LineStyle(kDotted));
78 
79  // Show effect of projection with too coarse binning
80  ((RooRealVar *)expDataY->get()->find("y"))->setBins(5);
81  RooAbsData *binnedDataY2 = expDataY->binnedClone();
82  model.plotOn(xframe, ProjWData(*binnedDataY2), LineColor(kRed));
83 
84  // Make canvas and draw RooPlots
85  new TCanvas("rf303_conditional", "rf303_conditional", 600, 460);
86  gPad->SetLeftMargin(0.15);
87  xframe->GetYaxis()->SetTitleOffset(1.2);
88  xframe->Draw();
89 }
90 
91 RooDataSet *makeFakeDataXY()
92 {
93  RooRealVar x("x", "x", -10, 10);
94  RooRealVar y("y", "y", -10, 10);
95  RooArgSet coord(x, y);
96 
97  RooDataSet *d = new RooDataSet("d", "d", RooArgSet(x, y));
98 
99  for (int i = 0; i < 10000; i++) {
100  Double_t tmpy = gRandom->Gaus(0, 10);
101  Double_t tmpx = gRandom->Gaus(0.5 * tmpy, 1);
102  if (fabs(tmpy) < 10 && fabs(tmpx) < 10) {
103  x = tmpx;
104  y = tmpy;
105  d->add(coord);
106  }
107  }
108 
109  return d;
110 }
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:274
RooFit::ProjWData
RooCmdArg ProjWData(const RooAbsData &projData, Bool_t binData=kFALSE)
Definition: RooGlobalFunc.cxx:47
RooAbsData
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:47
RooGaussian.h
TRandom.h
RooAbsData::Print
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
Definition: RooAbsData.h:191
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
Plain Gaussian p.d.f.
Definition: RooGaussian.h:24
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:1056
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:234
RooProdPdf.h
RooFit
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
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:1258
gRandom
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
RooPlot
A RooPlot is a plot frame and a container for graphics objects within that frame.
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:199
kRed
@ kRed
Definition: Rtypes.h:66
RooPolyVar
Class RooPolyVar is a RooAbsReal implementing a polynomial in terms of a list of RooAbsReal coefficie...
Definition: RooPolyVar.h:28
sigma
const Double_t sigma
Definition: h1analysisProxy.h:11
RooFit::LineColor
RooCmdArg LineColor(Color_t color)
Definition: RooGlobalFunc.cxx:57
Double_t
double Double_t
Definition: RtypesCore.h:59
TCanvas
The Canvas class.
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:975
d
#define d(i)
Definition: RSha256.hxx:102
gPad
#define gPad
Definition: TVirtualPad.h:287
RooDataSet
RooDataSet is a container class to hold unbinned data.
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:58
TAttAxis::SetTitleOffset
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:293
RooRealVar
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:37
TH1.h
kDotted
@ kDotted
Definition: TAttLine.h:48
RooArgSet
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:29