Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
rf701_efficiencyfit.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook -js
4 /// Speecial p.d.f.'s: unbinned maximum likelihood fit of an efficiency eff(x) function
5 ///
6 /// to a dataset D(x,cut), where cut is a category encoding a selection, of which the efficiency as function of x should
7 /// be described by eff(x)
8 ///
9 /// \macro_image
10 /// \macro_output
11 /// \macro_code
12 /// \author 07/2008 - Wouter Verkerke
13 
14 #include "RooRealVar.h"
15 #include "RooDataSet.h"
16 #include "RooGaussian.h"
17 #include "RooConstVar.h"
18 #include "RooFormulaVar.h"
19 #include "RooProdPdf.h"
20 #include "RooEfficiency.h"
21 #include "RooPolynomial.h"
22 #include "RooCategory.h"
23 #include "TCanvas.h"
24 #include "TAxis.h"
25 #include "RooPlot.h"
26 using namespace RooFit;
27 
28 void rf701_efficiencyfit()
29 {
30  // 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 )
31  // -------------------------------------------------------------------
32 
33  // Declare variables x,mean,sigma with associated name, title, initial value and allowed range
34  RooRealVar x("x", "x", -10, 10);
35 
36  // Efficiency function eff(x;a,b)
37  RooRealVar a("a", "a", 0.4, 0, 1);
38  RooRealVar b("b", "b", 5);
39  RooRealVar c("c", "c", -1, -10, 10);
40  RooFormulaVar effFunc("effFunc", "(1-a)+a*cos((x-c)/b)", RooArgList(a, b, c, x));
41 
42  // 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 )
43  // ------------------------------------------------------------------------------------------
44 
45  // Acceptance state cut (1 or 0)
46  RooCategory cut("cut", "cutr");
47  cut.defineType("accept", 1);
48  cut.defineType("reject", 0);
49 
50  // Construct efficiency p.d.f eff(cut|x)
51  RooEfficiency effPdf("effPdf", "effPdf", effFunc, cut, "accept");
52 
53  // 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
54  // -----------------------------------------------------------------------------
55 
56  // Construct global shape p.d.f shape(x) and product model(x,cut) = eff(cut|x)*shape(x)
57  // (These are _only_ needed to generate some toy MC here to be used later)
58  RooPolynomial shapePdf("shapePdf", "shapePdf", x, RooConst(-0.095));
59  RooProdPdf model("model", "model", shapePdf, Conditional(effPdf, cut));
60 
61  // Generate some toy data from model
62  RooDataSet *data = model.generate(RooArgSet(x, cut), 10000);
63 
64  // 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
65  // --------------------------------------------------------------------------
66 
67  // Fit conditional efficiency p.d.f to data
68  effPdf.fitTo(*data, ConditionalObservables(x));
69 
70  // P l o t f i t t e d , d a t a e f f i c i e n c y
71  // --------------------------------------------------------
72 
73  // Plot distribution of all events and accepted fraction of events on frame
74  RooPlot *frame1 = x.frame(Bins(20), Title("Data (all, accepted)"));
75  data->plotOn(frame1);
76  data->plotOn(frame1, Cut("cut==cut::accept"), MarkerColor(kRed), LineColor(kRed));
77 
78  // Plot accept/reject efficiency on data overlay fitted efficiency curve
79  RooPlot *frame2 = x.frame(Bins(20), Title("Fitted efficiency"));
80  data->plotOn(frame2, Efficiency(cut)); // needs ROOT version >= 5.21
81  effFunc.plotOn(frame2, LineColor(kRed));
82 
83  // Draw all frames on a canvas
84  TCanvas *ca = new TCanvas("rf701_efficiency", "rf701_efficiency", 800, 400);
85  ca->Divide(2);
86  ca->cd(1);
87  gPad->SetLeftMargin(0.15);
88  frame1->GetYaxis()->SetTitleOffset(1.6);
89  frame1->Draw();
90  ca->cd(2);
91  gPad->SetLeftMargin(0.15);
92  frame2->GetYaxis()->SetTitleOffset(1.4);
93  frame2->Draw();
94 }