Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
rf702_efficiencyfit_2D.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roofit
3 /// \notebook
4 /// Speecial p.d.f.'s: unbinned maximum likelihood fit of an efficiency eff(x) function to a dataset D(x,cut), where cut
5 /// is a category encoding a selection whose
6 ///
7 /// \macro_image
8 /// \macro_output
9 /// \macro_code
10 /// \author //
11 
12 #include "RooRealVar.h"
13 #include "RooDataSet.h"
14 #include "RooGaussian.h"
15 #include "RooConstVar.h"
16 #include "RooCategory.h"
17 #include "RooEfficiency.h"
18 #include "RooPolynomial.h"
19 #include "RooProdPdf.h"
20 #include "RooFormulaVar.h"
21 #include "TCanvas.h"
22 #include "TAxis.h"
23 #include "TH1.h"
24 #include "RooPlot.h"
25 using namespace RooFit;
26 
27 void rf702_efficiencyfit_2D(Bool_t flat = kFALSE)
28 {
29  // 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 )
30  // -----------------------------------------------------------------------
31 
32  // Declare variables x,mean,sigma with associated name, title, initial value and allowed range
33  RooRealVar x("x", "x", -10, 10);
34  RooRealVar y("y", "y", -10, 10);
35 
36  // Efficiency function eff(x;a,b)
37  RooRealVar ax("ax", "ay", 0.6, 0, 1);
38  RooRealVar bx("bx", "by", 5);
39  RooRealVar cx("cx", "cy", -1, -10, 10);
40 
41  RooRealVar ay("ay", "ay", 0.2, 0, 1);
42  RooRealVar by("by", "by", 5);
43  RooRealVar cy("cy", "cy", -1, -10, 10);
44 
45  RooFormulaVar effFunc("effFunc", "((1-ax)+ax*cos((x-cx)/bx))*((1-ay)+ay*cos((y-cy)/by))",
46  RooArgList(ax, bx, cx, x, ay, by, cy, y));
47 
48  // Acceptance state cut (1 or 0)
49  RooCategory cut("cut", "cutr");
50  cut.defineType("accept", 1);
51  cut.defineType("reject", 0);
52 
53  // 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 )
54  // ---------------------------------------------------------------------------------------------
55 
56  // Construct efficiency p.d.f eff(cut|x)
57  RooEfficiency effPdf("effPdf", "effPdf", effFunc, cut, "accept");
58 
59  // 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
60  // -------------------------------------------------------------------------------
61 
62  // Construct global shape p.d.f shape(x) and product model(x,cut) = eff(cut|x)*shape(x)
63  // (These are _only_ needed to generate some toy MC here to be used later)
64  RooPolynomial shapePdfX("shapePdfX", "shapePdfX", x, RooConst(flat ? 0 : -0.095));
65  RooPolynomial shapePdfY("shapePdfY", "shapePdfY", y, RooConst(flat ? 0 : +0.095));
66  RooProdPdf shapePdf("shapePdf", "shapePdf", RooArgSet(shapePdfX, shapePdfY));
67  RooProdPdf model("model", "model", shapePdf, Conditional(effPdf, cut));
68 
69  // Generate some toy data from model
70  RooDataSet *data = model.generate(RooArgSet(x, y, cut), 10000);
71 
72  // 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
73  // --------------------------------------------------------------------------
74 
75  // Fit conditional efficiency p.d.f to data
76  effPdf.fitTo(*data, ConditionalObservables(RooArgSet(x, y)));
77 
78  // P l o t f i t t e d , d a t a e f f i c i e n c y
79  // --------------------------------------------------------
80 
81  // Make 2D histograms of all data, selected data and efficiency function
82  TH1 *hh_data_all = data->createHistogram("hh_data_all", x, Binning(8), YVar(y, Binning(8)));
83  TH1 *hh_data_sel = data->createHistogram("hh_data_sel", x, Binning(8), YVar(y, Binning(8)), Cut("cut==cut::accept"));
84  TH1 *hh_eff = effFunc.createHistogram("hh_eff", x, Binning(50), YVar(y, Binning(50)));
85 
86  // Some adjustment for good visualization
87  hh_data_all->SetMinimum(0);
88  hh_data_sel->SetMinimum(0);
89  hh_eff->SetMinimum(0);
90  hh_eff->SetLineColor(kBlue);
91 
92  // Draw all frames on a canvas
93  TCanvas *ca = new TCanvas("rf702_efficiency_2D", "rf702_efficiency_2D", 1200, 400);
94  ca->Divide(3);
95  ca->cd(1);
96  gPad->SetLeftMargin(0.15);
97  hh_data_all->GetZaxis()->SetTitleOffset(1.8);
98  hh_data_all->Draw("lego");
99  ca->cd(2);
100  gPad->SetLeftMargin(0.15);
101  hh_data_sel->GetZaxis()->SetTitleOffset(1.8);
102  hh_data_sel->Draw("lego");
103  ca->cd(3);
104  gPad->SetLeftMargin(0.15);
105  hh_eff->GetZaxis()->SetTitleOffset(1.8);
106  hh_eff->Draw("surf");
107 
108  return;
109 }