Logo ROOT   6.10/09
Reference Guide
rf702_efficiencyfit_2D.cxx
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////
2 //
3 // 'SPECIAL PDFS' RooFit tutorial macro #702
4 //
5 // Unbinned maximum likelihood fit of an efficiency eff(x) function to
6 // a dataset D(x,cut), where cut is a category encoding a selection whose
7 // efficiency as function of x should be described by eff(x)
8 //
9 //
10 /////////////////////////////////////////////////////////////////////////
11 
12 #ifndef __CINT__
13 #include "RooGlobalFunc.h"
14 #endif
15 #include "RooRealVar.h"
16 #include "RooDataSet.h"
17 #include "RooGaussian.h"
18 #include "RooCategory.h"
19 #include "RooEfficiency.h"
20 #include "RooPolynomial.h"
21 #include "RooProdPdf.h"
22 #include "RooFormulaVar.h"
23 #include "TCanvas.h"
24 #include "TH1.h"
25 #include "RooPlot.h"
26 using namespace RooFit ;
27 
28 
29 // Elementary operations on a gaussian PDF
30 class TestBasic702 : public RooFitTestUnit
31 {
32 public:
33  TestBasic702(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Efficiency operator p.d.f. 2D",refFile,writeRef,verbose) {} ;
34  Bool_t testCode() {
35 
36  Bool_t flat=kFALSE ;
37 
38  // C o n s t r u c t e f f i c i e n c y f u n c t i o n e ( x , y )
39  // -----------------------------------------------------------------------
40 
41  // Declare variables x,mean,sigma with associated name, title, initial value and allowed range
42  RooRealVar x("x","x",-10,10) ;
43  RooRealVar y("y","y",-10,10) ;
44 
45  // Efficiency function eff(x;a,b)
46  RooRealVar ax("ax","ay",0.6,0,1) ;
47  RooRealVar bx("bx","by",5) ;
48  RooRealVar cx("cx","cy",-1,-10,10) ;
49 
50  RooRealVar ay("ay","ay",0.2,0,1) ;
51  RooRealVar by("by","by",5) ;
52  RooRealVar cy("cy","cy",-1,-10,10) ;
53 
54  RooFormulaVar effFunc("effFunc","((1-ax)+ax*cos((x-cx)/bx))*((1-ay)+ay*cos((y-cy)/by))",RooArgList(ax,bx,cx,x,ay,by,cy,y)) ;
55 
56  // Acceptance state cut (1 or 0)
57  RooCategory cut("cut","cutr") ;
58  cut.defineType("accept",1) ;
59  cut.defineType("reject",0) ;
60 
61 
62 
63  // C o n s t r u c t c o n d i t i o n a l e f f i c i e n c y p d f E ( c u t | x , y )
64  // ---------------------------------------------------------------------------------------------
65 
66  // Construct efficiency p.d.f eff(cut|x)
67  RooEfficiency effPdf("effPdf","effPdf",effFunc,cut,"accept") ;
68 
69 
70 
71  // G e n e r a t e d a t a ( x , y , c u t ) f r o m a t o y m o d e l
72  // -------------------------------------------------------------------------------
73 
74  // Construct global shape p.d.f shape(x) and product model(x,cut) = eff(cut|x)*shape(x)
75  // (These are _only_ needed to generate some toy MC here to be used later)
76  RooPolynomial shapePdfX("shapePdfX","shapePdfX",x,RooConst(flat?0:-0.095)) ;
77  RooPolynomial shapePdfY("shapePdfY","shapePdfY",y,RooConst(flat?0:+0.095)) ;
78  RooProdPdf shapePdf("shapePdf","shapePdf",RooArgSet(shapePdfX,shapePdfY)) ;
79  RooProdPdf model("model","model",shapePdf,Conditional(effPdf,cut)) ;
80 
81  // Generate some toy data from model
82  RooDataSet* data = model.generate(RooArgSet(x,y,cut),10000) ;
83 
84 
85 
86  // F i t c o n d i t i o n a l e f f i c i e n c y p d f t o d a t a
87  // --------------------------------------------------------------------------
88 
89  // Fit conditional efficiency p.d.f to data
90  effPdf.fitTo(*data,ConditionalObservables(RooArgSet(x,y))) ;
91 
92 
93 
94  // P l o t f i t t e d , d a t a e f f i c i e n c y
95  // --------------------------------------------------------
96 
97  // Make 2D histograms of all data, selected data and efficiency function
98  TH1* hh_data_all = data->createHistogram("hh_data_all",x,Binning(8),YVar(y,Binning(8))) ;
99  TH1* hh_data_sel = data->createHistogram("hh_data_sel",x,Binning(8),YVar(y,Binning(8)),Cut("cut==cut::accept")) ;
100  TH1* hh_eff = effFunc.createHistogram("hh_eff",x,Binning(50),YVar(y,Binning(50))) ;
101 
102  // Some adjustsment for good visualization
103  hh_data_all->SetMinimum(0) ;
104  hh_data_sel->SetMinimum(0) ;
105  hh_eff->SetMinimum(0) ;
106  hh_eff->SetLineColor(kBlue) ;
107 
108  regTH(hh_data_all,"rf702_hh_data_all") ;
109  regTH(hh_data_sel,"rf702_hh_data_sel") ;
110  regTH(hh_eff,"rf702_hh_eff") ;
111 
112  delete data ;
113 
114  return kTRUE;
115 
116  }
117 } ;
RooCmdArg Cut(const char *cutSpec)
RooProdPdf is an efficient implementation of a product of PDFs of the form.
Definition: RooProdPdf.h:31
virtual void SetMinimum(Double_t minimum=-1111)
Definition: TH1.h:376
RooCmdArg Conditional(const RooArgSet &pdfSet, const RooArgSet &depSet, Bool_t depsAreCond=kFALSE)
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
Double_t x[n]
Definition: legend1.C:17
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
TH2F * createHistogram(const RooAbsRealLValue &var1, const RooAbsRealLValue &var2, const char *cuts="", const char *name="hist") const
Create a TH2F histogram of the distribution of the specified variable using this dataset.
bool verbose
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
RooCategory represents a fundamental (non-derived) discrete value object.
Definition: RooCategory.h:24
const Bool_t kFALSE
Definition: RtypesCore.h:92
RooCmdArg YVar(const RooAbsRealLValue &var, const RooCmdArg &arg=RooCmdArg::none())
Double_t y[n]
Definition: legend1.C:17
The TH1 histogram class.
Definition: TH1.h:56
RooConstVar & RooConst(Double_t val)
RooPolynomial implements a polynomial p.d.f of the form By default coefficient a_0 is chosen to be 1...
Definition: RooPolynomial.h:28
RooEfficiency is a PDF helper class to fit efficiencies parameterized by a supplied function F...
Definition: RooEfficiency.h:27
Definition: Rtypes.h:56
RooCmdArg ConditionalObservables(const RooArgSet &set)
const Bool_t kTRUE
Definition: RtypesCore.h:91
RooCmdArg Binning(const RooAbsBinning &binning)