Logo ROOT   6.10/09
Reference Guide
rf702_efficiencyfit_2D.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook
4 /// 'SPECIAL PDFS' RooFit tutorial macro #702
5 ///
6 /// Unbinned maximum likelihood fit of an efficiency eff(x) function to
7 /// a dataset D(x,cut), where cut is a category encoding a selection whose
8 ///
9 /// \macro_image
10 /// \macro_output
11 /// \macro_code
12 /// \author //
13 
14 
15 #include "RooRealVar.h"
16 #include "RooDataSet.h"
17 #include "RooGaussian.h"
18 #include "RooConstVar.h"
19 #include "RooCategory.h"
20 #include "RooEfficiency.h"
21 #include "RooPolynomial.h"
22 #include "RooProdPdf.h"
23 #include "RooFormulaVar.h"
24 #include "TCanvas.h"
25 #include "TAxis.h"
26 #include "TH1.h"
27 #include "RooPlot.h"
28 using namespace RooFit ;
29 
30 
31 void rf702_efficiencyfit_2D(Bool_t flat=kFALSE)
32 {
33  // 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 )
34  // -----------------------------------------------------------------------
35 
36  // Declare variables x,mean,sigma with associated name, title, initial value and allowed range
37  RooRealVar x("x","x",-10,10) ;
38  RooRealVar y("y","y",-10,10) ;
39 
40  // Efficiency function eff(x;a,b)
41  RooRealVar ax("ax","ay",0.6,0,1) ;
42  RooRealVar bx("bx","by",5) ;
43  RooRealVar cx("cx","cy",-1,-10,10) ;
44 
45  RooRealVar ay("ay","ay",0.2,0,1) ;
46  RooRealVar by("by","by",5) ;
47  RooRealVar cy("cy","cy",-1,-10,10) ;
48 
49  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)) ;
50 
51  // Acceptance state cut (1 or 0)
52  RooCategory cut("cut","cutr") ;
53  cut.defineType("accept",1) ;
54  cut.defineType("reject",0) ;
55 
56 
57 
58  // 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 )
59  // ---------------------------------------------------------------------------------------------
60 
61  // Construct efficiency p.d.f eff(cut|x)
62  RooEfficiency effPdf("effPdf","effPdf",effFunc,cut,"accept") ;
63 
64 
65 
66  // 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
67  // -------------------------------------------------------------------------------
68 
69  // Construct global shape p.d.f shape(x) and product model(x,cut) = eff(cut|x)*shape(x)
70  // (These are _only_ needed to generate some toy MC here to be used later)
71  RooPolynomial shapePdfX("shapePdfX","shapePdfX",x,RooConst(flat?0:-0.095)) ;
72  RooPolynomial shapePdfY("shapePdfY","shapePdfY",y,RooConst(flat?0:+0.095)) ;
73  RooProdPdf shapePdf("shapePdf","shapePdf",RooArgSet(shapePdfX,shapePdfY)) ;
74  RooProdPdf model("model","model",shapePdf,Conditional(effPdf,cut)) ;
75 
76  // Generate some toy data from model
77  RooDataSet* data = model.generate(RooArgSet(x,y,cut),10000) ;
78 
79 
80 
81  // 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
82  // --------------------------------------------------------------------------
83 
84  // Fit conditional efficiency p.d.f to data
85  effPdf.fitTo(*data,ConditionalObservables(RooArgSet(x,y))) ;
86 
87 
88 
89  // P l o t f i t t e d , d a t a e f f i c i e n c y
90  // --------------------------------------------------------
91 
92  // Make 2D histograms of all data, selected data and efficiency function
93  TH1* hh_data_all = data->createHistogram("hh_data_all",x,Binning(8),YVar(y,Binning(8))) ;
94  TH1* hh_data_sel = data->createHistogram("hh_data_sel",x,Binning(8),YVar(y,Binning(8)),Cut("cut==cut::accept")) ;
95  TH1* hh_eff = effFunc.createHistogram("hh_eff",x,Binning(50),YVar(y,Binning(50))) ;
96 
97  // Some adjustment for good visualization
98  hh_data_all->SetMinimum(0) ;
99  hh_data_sel->SetMinimum(0) ;
100  hh_eff->SetMinimum(0) ;
101  hh_eff->SetLineColor(kBlue) ;
102 
103 
104 
105  // Draw all frames on a canvas
106  TCanvas* ca = new TCanvas("rf702_efficiency_2D","rf702_efficiency_2D",1200,400) ;
107  ca->Divide(3) ;
108  ca->cd(1) ; gPad->SetLeftMargin(0.15) ; hh_data_all->GetZaxis()->SetTitleOffset(1.8) ; hh_data_all->Draw("lego") ;
109  ca->cd(2) ; gPad->SetLeftMargin(0.15) ; hh_data_sel->GetZaxis()->SetTitleOffset(1.8) ; hh_data_sel->Draw("lego") ;
110  ca->cd(3) ; gPad->SetLeftMargin(0.15) ; hh_eff->GetZaxis()->SetTitleOffset(1.8) ; hh_eff->Draw("surf") ;
111 
112  return ;
113 
114 
115 }
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title Offset is a correction factor with respect to the "s...
Definition: TAttAxis.cxx:262
RooCmdArg Cut(const char *cutSpec)
RooProdPdf is an efficient implementation of a product of PDFs of the form.
Definition: RooProdPdf.h:31
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:679
virtual void SetMinimum(Double_t minimum=-1111)
Definition: TH1.h:376
RooCmdArg Conditional(const RooArgSet &pdfSet, const RooArgSet &depSet, Bool_t depsAreCond=kFALSE)
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
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
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.
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
The Canvas class.
Definition: TCanvas.h:31
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
TAxis * GetZaxis()
Definition: TH1.h:302
RooConstVar & RooConst(Double_t val)
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1135
#define gPad
Definition: TVirtualPad.h:284
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)
RooCmdArg Binning(const RooAbsBinning &binning)