Logo ROOT   6.10/09
Reference Guide
rf701_efficiencyfit.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook -js
4 /// 'SPECIAL PDFS' RooFit tutorial macro #701
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, of which
8 /// the efficiency as function of x should be described by eff(x)
9 ///
10 /// \macro_image
11 /// \macro_output
12 /// \macro_code
13 /// \author 07/2008 - Wouter Verkerke
14 
15 
16 #include "RooRealVar.h"
17 #include "RooDataSet.h"
18 #include "RooGaussian.h"
19 #include "RooConstVar.h"
20 #include "RooFormulaVar.h"
21 #include "RooProdPdf.h"
22 #include "RooEfficiency.h"
23 #include "RooPolynomial.h"
24 #include "RooCategory.h"
25 #include "TCanvas.h"
26 #include "TAxis.h"
27 #include "RooPlot.h"
28 using namespace RooFit ;
29 
30 
31 void rf701_efficiencyfit()
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 )
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 
39  // Efficiency function eff(x;a,b)
40  RooRealVar a("a","a",0.4,0,1) ;
41  RooRealVar b("b","b",5) ;
42  RooRealVar c("c","c",-1,-10,10) ;
43  RooFormulaVar effFunc("effFunc","(1-a)+a*cos((x-c)/b)",RooArgList(a,b,c,x)) ;
44 
45 
46 
47  // 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 )
48  // ------------------------------------------------------------------------------------------
49 
50  // Acceptance state cut (1 or 0)
51  RooCategory cut("cut","cutr") ;
52  cut.defineType("accept",1) ;
53  cut.defineType("reject",0) ;
54 
55  // Construct efficiency p.d.f eff(cut|x)
56  RooEfficiency effPdf("effPdf","effPdf",effFunc,cut,"accept") ;
57 
58 
59 
60  // G e n e r a t e d a t a ( x , c u t ) f r o m a t o y m o d e l
61  // -----------------------------------------------------------------------------
62 
63  // Construct global shape p.d.f shape(x) and product model(x,cut) = eff(cut|x)*shape(x)
64  // (These are _only_ needed to generate some toy MC here to be used later)
65  RooPolynomial shapePdf("shapePdf","shapePdf",x,RooConst(-0.095)) ;
66  RooProdPdf model("model","model",shapePdf,Conditional(effPdf,cut)) ;
67 
68  // Generate some toy data from model
69  RooDataSet* data = model.generate(RooArgSet(x,cut),10000) ;
70 
71 
72 
73  // 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
74  // --------------------------------------------------------------------------
75 
76  // Fit conditional efficiency p.d.f to data
77  effPdf.fitTo(*data,ConditionalObservables(x)) ;
78 
79 
80 
81  // P l o t f i t t e d , d a t a e f f i c i e n c y
82  // --------------------------------------------------------
83 
84  // Plot distribution of all events and accepted fraction of events on frame
85  RooPlot* frame1 = x.frame(Bins(20),Title("Data (all, accepted)")) ;
86  data->plotOn(frame1) ;
87  data->plotOn(frame1,Cut("cut==cut::accept"),MarkerColor(kRed),LineColor(kRed)) ;
88 
89  // Plot accept/reject efficiency on data overlay fitted efficiency curve
90  RooPlot* frame2 = x.frame(Bins(20),Title("Fitted efficiency")) ;
91  data->plotOn(frame2,Efficiency(cut)) ; // needs ROOT version >= 5.21
92  effFunc.plotOn(frame2,LineColor(kRed)) ;
93 
94 
95 
96  // Draw all frames on a canvas
97  TCanvas* ca = new TCanvas("rf701_efficiency","rf701_efficiency",800,400) ;
98  ca->Divide(2) ;
99  ca->cd(1) ; gPad->SetLeftMargin(0.15) ; frame1->GetYaxis()->SetTitleOffset(1.6) ; frame1->Draw() ;
100  ca->cd(2) ; gPad->SetLeftMargin(0.15) ; frame2->GetYaxis()->SetTitleOffset(1.4) ; frame2->Draw() ;
101 
102 
103 }
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
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1118
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 Cut(const char *cutSpec)
RooCmdArg LineColor(Color_t color)
Definition: Rtypes.h:56
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
RooCmdArg Conditional(const RooArgSet &pdfSet, const RooArgSet &depSet, Bool_t depsAreCond=kFALSE)
TArc * a
Definition: textangle.C:12
RooCmdArg Title(const char *name)
Double_t x[n]
Definition: legend1.C:17
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
RooCmdArg Efficiency(const RooCategory &cat)
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
A RooPlot is a plot frame and a container for graphics objects within that frame. ...
Definition: RooPlot.h:41
The Canvas class.
Definition: TCanvas.h:31
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
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
RooCmdArg Bins(Int_t nbin)
#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
RooCmdArg MarkerColor(Color_t color)
RooCmdArg ConditionalObservables(const RooArgSet &set)
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:559