Logo ROOT   6.10/09
Reference Guide
rf303_conditional.cxx
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////////
2 //
3 // 'MULTIDIMENSIONAL MODELS' RooFit tutorial macro #303
4 //
5 // 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 //
10 // 07/2008 - Wouter Verkerke
11 //
12 /////////////////////////////////////////////////////////////////////////
13 
14 #ifndef __CINT__
15 #include "RooGlobalFunc.h"
16 #endif
17 #include "RooRealVar.h"
18 #include "RooDataSet.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 "TH1.h"
26 using namespace RooFit ;
27 
28 
29 class TestBasic303 : public RooFitTestUnit
30 {
31 public:
32 
33 RooDataSet* makeFakeDataXY()
34 {
35  RooRealVar x("x","x",-10,10) ;
36  RooRealVar y("y","y",-10,10) ;
37  RooArgSet coord(x,y) ;
38 
39  RooDataSet* d = new RooDataSet("d","d",RooArgSet(x,y)) ;
40 
41  for (int i=0 ; i<10000 ; i++) {
42  Double_t tmpy = gRandom->Gaus(0,10) ;
43  Double_t tmpx = gRandom->Gaus(0.5*tmpy,1) ;
44  if (fabs(tmpy)<10 && fabs(tmpx)<10) {
45  x = tmpx ;
46  y = tmpy ;
47  d->add(coord) ;
48  }
49 
50  }
51 
52  return d ;
53 }
54 
55 
56 
57  TestBasic303(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Conditional use of F(x|y)",refFile,writeRef,verbose) {} ;
58  Bool_t testCode() {
59 
60  // 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 )
61  // -----------------------------------------------------------------------
62 
63  // Create observables
64  RooRealVar x("x","x",-10,10) ;
65  RooRealVar y("y","y",-10,10) ;
66 
67  // Create function f(y) = a0 + a1*y
68  RooRealVar a0("a0","a0",-0.5,-5,5) ;
69  RooRealVar a1("a1","a1",-0.5,-1,1) ;
70  RooPolyVar fy("fy","fy",y,RooArgSet(a0,a1)) ;
71 
72  // Creat gauss(x,f(y),s)
73  RooRealVar sigma("sigma","width of gaussian",0.5,0.1,2.0) ;
74  RooGaussian model("model","Gaussian with shifting mean",x,fy,sigma) ;
75 
76 
77  // Obtain fake external experimental dataset with values for x and y
78  RooDataSet* expDataXY = makeFakeDataXY() ;
79 
80 
81 
82  // 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 )
83  // ---------------------------------------------------------------------------------------------
84 
85  // Make subset of experimental data with only y values
86  RooDataSet* expDataY= (RooDataSet*) expDataXY->reduce(y) ;
87 
88  // Generate 10000 events in x obtained from _conditional_ model(x|y) with y values taken from experimental data
89  RooDataSet *data = model.generate(x,ProtoData(*expDataY)) ;
90 
91 
92 
93  // 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
94  // ---------------------------------------------------------------------------------------------
95 
96  model.fitTo(*expDataXY,ConditionalObservables(y)) ;
97 
98 
99 
100  // 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
101  // ---------------------------------------------------------------------------------------------
102 
103  // Plot x distribution of data and projection of model on x = 1/Ndata sum(data(y_i)) model(x;y_i)
104  RooPlot* xframe = x.frame() ;
105  expDataXY->plotOn(xframe) ;
106  model.plotOn(xframe,ProjWData(*expDataY)) ;
107 
108 
109  // Speed up (and approximate) projection by using binned clone of data for projection
110  RooAbsData* binnedDataY = expDataY->binnedClone() ;
111  model.plotOn(xframe,ProjWData(*binnedDataY),LineColor(kCyan),LineStyle(kDotted),Name("Alt1")) ;
112 
113 
114  // Show effect of projection with too coarse binning
115  ((RooRealVar*)expDataY->get()->find("y"))->setBins(5) ;
116  RooAbsData* binnedDataY2 = expDataY->binnedClone() ;
117  model.plotOn(xframe,ProjWData(*binnedDataY2),LineColor(kRed),Name("Alt2")) ;
118 
119 
120  regPlot(xframe,"rf303_plot1") ;
121 
122  delete binnedDataY ;
123  delete binnedDataY2 ;
124  delete expDataXY ;
125  delete expDataY ;
126  delete data ;
127 
128  return kTRUE ;
129  }
130 } ;
131 
132 
133 
134 
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
Plot dataset on specified frame.
Definition: RooAbsData.cxx:552
RooCmdArg LineColor(Color_t color)
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:235
Definition: Rtypes.h:56
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
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:343
RooCmdArg ProjWData(const RooAbsData &projData, Bool_t binData=kFALSE)
Double_t x[n]
Definition: legend1.C:17
RooDataHist * binnedClone(const char *newName=0, const char *newTitle=0) const
Return binned clone of this dataset.
Definition: RooDataSet.cxx:981
RooCmdArg LineStyle(Style_t style)
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
const Double_t sigma
Class RooPolyVar is a RooAbsReal implementing a polynomial in terms of a list of RooAbsReal coefficie...
Definition: RooPolyVar.h:28
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
RooCmdArg Name(const char *name)
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
bool verbose
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:37
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
virtual const RooArgSet * get(Int_t index) const
Return RooArgSet with coordinates of event &#39;index&#39;.
virtual void add(const RooArgSet &row, Double_t weight=1.0, Double_t weightError=0)
Add a data point, with its coordinates specified in the &#39;data&#39; argset, to the data set...
A RooPlot is a plot frame and a container for graphics objects within that frame. ...
Definition: RooPlot.h:41
RooAbsArg * find(const char *name) const
Find object with given name in list.
double Double_t
Definition: RtypesCore.h:55
RooCmdArg ProtoData(const RooDataSet &protoData, Bool_t randomizeOrder=kFALSE, Bool_t resample=kFALSE)
Double_t y[n]
Definition: legend1.C:17
Definition: Rtypes.h:56
RooCmdArg ConditionalObservables(const RooArgSet &set)
const Bool_t kTRUE
Definition: RtypesCore.h:91