Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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_code
10/// \macro_output
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
27using namespace RooFit;
28
29RooDataSet *makeFakeDataXY();
30
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 std::unique_ptr<RooAbsData> expAbsDataY{expDataXY->reduce(y)};
57 RooDataSet *expDataY = static_cast<RooDataSet*>(expAbsDataY.get());
58
59 // Generate 10000 events in x obtained from _conditional_ model(x|y) with y values taken from experimental data
60 std::unique_ptr<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), PrintLevel(-1));
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 std::unique_ptr<RooDataHist> 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 std::unique_ptr<RooDataHist> 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
92RooDataSet *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 tmpy = gRandom->Gaus(0, 10);
102 double tmpx = gRandom->Gaus(0.5 * tmpy, 1);
103 if (fabs(tmpy) < 10 && fabs(tmpx) < 10) {
104 x.setVal(tmpx);
105 y.setVal(tmpy);
106 d->add(coord);
107 }
108 }
109
110 return d;
111}
#define d(i)
Definition RSha256.hxx:102
@ kRed
Definition Rtypes.h:66
@ kCyan
Definition Rtypes.h:66
@ kDotted
Definition TAttLine.h:48
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
#define gPad
RooAbsArg * find(const char *name) const
Find object with given name in list.
virtual RooPlot * plotOn(RooPlot *frame, const RooCmdArg &arg1={}, const RooCmdArg &arg2={}, const RooCmdArg &arg3={}, const RooCmdArg &arg4={}, const RooCmdArg &arg5={}, const RooCmdArg &arg6={}, const RooCmdArg &arg7={}, const RooCmdArg &arg8={}) const
RooFit::OwningPtr< RooAbsData > reduce(const RooCmdArg &arg1, const RooCmdArg &arg2={}, const RooCmdArg &arg3={}, const RooCmdArg &arg4={}, const RooCmdArg &arg5={}, const RooCmdArg &arg6={}, const RooCmdArg &arg7={}, const RooCmdArg &arg8={}) const
Create a reduced copy of this dataset.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
Container class to hold unbinned data.
Definition RooDataSet.h:33
const RooArgSet * get(Int_t index) const override
Return RooArgSet with coordinates of event 'index'.
RooFit::OwningPtr< RooDataHist > binnedClone(const char *newName=nullptr, const char *newTitle=nullptr) const
Return binned clone of this dataset.
Plain Gaussian p.d.f.
Definition RooGaussian.h:24
Plot frame and a container for graphics objects within that frame.
Definition RooPlot.h:45
static RooPlot * frame(const RooAbsRealLValue &var, double xmin, double xmax, Int_t nBins)
Create a new frame for a given variable in x.
Definition RooPlot.cxx:225
TAxis * GetYaxis() const
Definition RooPlot.cxx:1264
void Draw(Option_t *options=nullptr) override
Draw this plot and all of the elements it contains.
Definition RooPlot.cxx:637
A RooAbsReal implementing a polynomial in terms of a list of RooAbsReal coefficients.
Definition RooPolyVar.h:25
Variable that can be changed from the outside.
Definition RooRealVar.h:37
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition TAttAxis.cxx:298
The Canvas class.
Definition TCanvas.h:23
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:275
RooCmdArg PrintLevel(Int_t code)
RooCmdArg ConditionalObservables(Args_t &&... argsOrArgSet)
Create a RooCmdArg to declare conditional observables.
RooCmdArg ProtoData(const RooDataSet &protoData, bool randomizeOrder=false, bool resample=false)
RooCmdArg ProjWData(const RooAbsData &projData, bool binData=false)
RooCmdArg LineColor(Color_t color)
RooCmdArg LineStyle(Style_t style)
const Double_t sigma
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition JSONIO.h:26