Logo ROOT   6.10/09
Reference Guide
rf701_efficiencyfit.cxx
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////
2 //
3 // 'SPECIAL PDFS' RooFit tutorial macro #701
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, of which
7 // the efficiency as function of x should be described by eff(x)
8 //
9 // 07/2008 - Wouter Verkerke
10 //
11 /////////////////////////////////////////////////////////////////////////
12 
13 #ifndef __CINT__
14 #include "RooGlobalFunc.h"
15 #endif
16 #include "RooRealVar.h"
17 #include "RooDataSet.h"
18 #include "RooGaussian.h"
19 #include "RooFormulaVar.h"
20 #include "RooProdPdf.h"
21 #include "RooEfficiency.h"
22 #include "RooPolynomial.h"
23 #include "RooCategory.h"
24 #include "TCanvas.h"
25 #include "RooPlot.h"
26 using namespace RooFit ;
27 
28 
29 // Elementary operations on a gaussian PDF
30 class TestBasic701 : public RooFitTestUnit
31 {
32 public:
33  TestBasic701(TFile* refFile, Bool_t writeRef, Int_t verbose) : RooFitTestUnit("Efficiency operator p.d.f. 1D",refFile,writeRef,verbose) {} ;
34  Bool_t testCode() {
35 
36  // 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 )
37  // -------------------------------------------------------------------
38 
39  // Declare variables x,mean,sigma with associated name, title, initial value and allowed range
40  RooRealVar x("x","x",-10,10) ;
41 
42  // Efficiency function eff(x;a,b)
43  RooRealVar a("a","a",0.4,0,1) ;
44  RooRealVar b("b","b",5) ;
45  RooRealVar c("c","c",-1,-10,10) ;
46  RooFormulaVar effFunc("effFunc","(1-a)+a*cos((x-c)/b)",RooArgList(a,b,c,x)) ;
47 
48 
49 
50  // 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 )
51  // ------------------------------------------------------------------------------------------
52 
53  // Acceptance state cut (1 or 0)
54  RooCategory cut("cut","cutr") ;
55  cut.defineType("accept",1) ;
56  cut.defineType("reject",0) ;
57 
58  // Construct efficiency p.d.f eff(cut|x)
59  RooEfficiency effPdf("effPdf","effPdf",effFunc,cut,"accept") ;
60 
61 
62 
63  // 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
64  // -----------------------------------------------------------------------------
65 
66  // Construct global shape p.d.f shape(x) and product model(x,cut) = eff(cut|x)*shape(x)
67  // (These are _only_ needed to generate some toy MC here to be used later)
68  RooPolynomial shapePdf("shapePdf","shapePdf",x,RooConst(-0.095)) ;
69  RooProdPdf model("model","model",shapePdf,Conditional(effPdf,cut)) ;
70 
71  // Generate some toy data from model
72  RooDataSet* data = model.generate(RooArgSet(x,cut),10000) ;
73 
74 
75 
76  // 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
77  // --------------------------------------------------------------------------
78 
79  // Fit conditional efficiency p.d.f to data
80  effPdf.fitTo(*data,ConditionalObservables(x)) ;
81 
82 
83 
84  // P l o t f i t t e d , d a t a e f f i c i e n c y
85  // --------------------------------------------------------
86 
87  // Plot distribution of all events and accepted fraction of events on frame
88  RooPlot* frame1 = x.frame(Bins(20),Title("Data (all, accepted)")) ;
89  data->plotOn(frame1) ;
90  data->plotOn(frame1,Cut("cut==cut::accept"),MarkerColor(kRed),LineColor(kRed)) ;
91 
92  // Plot accept/reject efficiency on data overlay fitted efficiency curve
93  RooPlot* frame2 = x.frame(Bins(20),Title("Fitted efficiency")) ;
94  data->plotOn(frame2,Efficiency(cut)) ; // needs ROOT version >= 5.21
95  effFunc.plotOn(frame2,LineColor(kRed)) ;
96 
97 
98  regPlot(frame1,"rf701_plot1") ;
99  regPlot(frame2,"rf701_plot2") ;
100 
101 
102  delete data ;
103 
104  return kTRUE ;
105  }
106 } ;
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
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
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)
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
A RooPlot is a plot frame and a container for graphics objects within that frame. ...
Definition: RooPlot.h:41
RooConstVar & RooConst(Double_t val)
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)
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)
const Bool_t kTRUE
Definition: RtypesCore.h:91