# rf701_efficiencyfit¶

Special pdf's: unbinned maximum likelihood fit of an efficiency eff(x) function

to a dataset D(x,cut), where cut is a category encoding a selection, of which the efficiency as function of x should be described by eff(x)

Author: Wouter Verkerke
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Wednesday, November 30, 2022 at 11:24 AM.

In [1]:
%%cpp -d
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "RooConstVar.h"
#include "RooFormulaVar.h"
#include "RooProdPdf.h"
#include "RooEfficiency.h"
#include "RooPolynomial.h"
#include "RooCategory.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "RooPlot.h"
using namespace RooFit;


## Construct efficiency function e(x)¶

Declare variables x,mean,sigma with associated name, title, initial value and allowed range

In [2]:
RooRealVar x("x", "x", -10, 10);


Efficiency function eff(x;a,b)

In [3]:
RooRealVar a("a", "a", 0.4, 0, 1);
RooRealVar b("b", "b", 5);
RooRealVar c("c", "c", -1, -10, 10);
RooFormulaVar effFunc("effFunc", "(1-a)+a*cos((x-c)/b)", RooArgList(a, b, c, x));


## Construct conditional efficiency pdf E(cut|x)¶

Acceptance state cut (1 or 0)

In [4]:
RooCategory cut("cut", "cutr", { {"accept", 1}, {"reject", 0} });


Construct efficiency pdf eff(cut|x)

In [5]:
RooEfficiency effPdf("effPdf", "effPdf", effFunc, cut, "accept");


## Generate data (x, cut) from a toy model¶

Construct global shape pdf shape(x) and product model(x,cut) = eff(cut|x)*shape(x) (These are only needed to generate some toy MC here to be used later)

In [6]:
RooPolynomial shapePdf("shapePdf", "shapePdf", x, RooConst(-0.095));
RooProdPdf model("model", "model", shapePdf, Conditional(effPdf, cut));


Generate some toy data from model

In [7]:
RooDataSet *data = model.generate(RooArgSet(x, cut), 10000);

input_line_58:2:2: warning: 'data' shadows a declaration with the same name in the 'std' namespace; use '::data' to reference this declaration
RooDataSet *data = model.generate(RooArgSet(x, cut), 10000);
^


## Fit conditional efficiency pdf to data¶

Fit conditional efficiency pdf to data

In [8]:
effPdf.fitTo(*data, ConditionalObservables(x));

input_line_59:2:16: error: reference to 'data' is ambiguous
effPdf.fitTo(*data, ConditionalObservables(x));
^
input_line_58:2:14: note: candidate found by name lookup is '__cling_N526::data'
RooDataSet *data = model.generate(RooArgSet(x, cut), 10000);
^
/usr/include/c++/9/bits/range_access.h:318:5: note: candidate found by name lookup is 'std::data'
data(initializer_list<_Tp> __il) noexcept
^
/usr/include/c++/9/bits/range_access.h:289:5: note: candidate found by name lookup is 'std::data'
data(_Container& __cont) noexcept(noexcept(__cont.data()))
^
/usr/include/c++/9/bits/range_access.h:299:5: note: candidate found by name lookup is 'std::data'
data(const _Container& __cont) noexcept(noexcept(__cont.data()))
^
/usr/include/c++/9/bits/range_access.h:309:5: note: candidate found by name lookup is 'std::data'
data(_Tp (&__array)[_Nm]) noexcept
^


## Plot fitted, data efficiency¶

Plot distribution of all events and accepted fraction of events on frame

In [9]:
RooPlot *frame1 = x.frame(Bins(20), Title("Data (all, accepted)"));
data->plotOn(frame1);
data->plotOn(frame1, Cut("cut==cut::accept"), MarkerColor(kRed), LineColor(kRed));

input_line_60:3:1: error: reference to 'data' is ambiguous
data->plotOn(frame1);
^
input_line_58:2:14: note: candidate found by name lookup is '__cling_N526::data'
RooDataSet *data = model.generate(RooArgSet(x, cut), 10000);
^
/usr/include/c++/9/bits/range_access.h:318:5: note: candidate found by name lookup is 'std::data'
data(initializer_list<_Tp> __il) noexcept
^
/usr/include/c++/9/bits/range_access.h:289:5: note: candidate found by name lookup is 'std::data'
data(_Container& __cont) noexcept(noexcept(__cont.data()))
^
/usr/include/c++/9/bits/range_access.h:299:5: note: candidate found by name lookup is 'std::data'
data(const _Container& __cont) noexcept(noexcept(__cont.data()))
^
/usr/include/c++/9/bits/range_access.h:309:5: note: candidate found by name lookup is 'std::data'
data(_Tp (&__array)[_Nm]) noexcept
^
input_line_60:4:1: error: reference to 'data' is ambiguous
data->plotOn(frame1, Cut("cut==cut::accept"), MarkerColor(kRed), LineColor(kRed));
^
input_line_58:2:14: note: candidate found by name lookup is '__cling_N526::data'
RooDataSet *data = model.generate(RooArgSet(x, cut), 10000);
^
/usr/include/c++/9/bits/range_access.h:318:5: note: candidate found by name lookup is 'std::data'
data(initializer_list<_Tp> __il) noexcept
^
/usr/include/c++/9/bits/range_access.h:289:5: note: candidate found by name lookup is 'std::data'
data(_Container& __cont) noexcept(noexcept(__cont.data()))
^
/usr/include/c++/9/bits/range_access.h:299:5: note: candidate found by name lookup is 'std::data'
data(const _Container& __cont) noexcept(noexcept(__cont.data()))
^
/usr/include/c++/9/bits/range_access.h:309:5: note: candidate found by name lookup is 'std::data'
data(_Tp (&__array)[_Nm]) noexcept
^


Plot accept/reject efficiency on data overlay fitted efficiency curve

In [10]:
RooPlot *frame2 = x.frame(Bins(20), Title("Fitted efficiency"));
data->plotOn(frame2, Efficiency(cut)); // needs ROOT version >= 5.21
effFunc.plotOn(frame2, LineColor(kRed));

input_line_61:3:1: error: reference to 'data' is ambiguous
data->plotOn(frame2, Efficiency(cut)); // needs ROOT version >= 5.21
^
input_line_58:2:14: note: candidate found by name lookup is '__cling_N526::data'
RooDataSet *data = model.generate(RooArgSet(x, cut), 10000);
^
/usr/include/c++/9/bits/range_access.h:318:5: note: candidate found by name lookup is 'std::data'
data(initializer_list<_Tp> __il) noexcept
^
/usr/include/c++/9/bits/range_access.h:289:5: note: candidate found by name lookup is 'std::data'
data(_Container& __cont) noexcept(noexcept(__cont.data()))
^
/usr/include/c++/9/bits/range_access.h:299:5: note: candidate found by name lookup is 'std::data'
data(const _Container& __cont) noexcept(noexcept(__cont.data()))
^
/usr/include/c++/9/bits/range_access.h:309:5: note: candidate found by name lookup is 'std::data'
data(_Tp (&__array)[_Nm]) noexcept
^


Draw all frames on a canvas

In [11]:
TCanvas *ca = new TCanvas("rf701_efficiency", "rf701_efficiency", 800, 400);
ca->Divide(2);
ca->cd(1);
frame1->GetYaxis()->SetTitleOffset(1.6);
frame1->Draw();
ca->cd(2);
frame2->GetYaxis()->SetTitleOffset(1.4);
frame2->Draw();

input_line_63:2:3: error: use of undeclared identifier 'frame1'
(frame1->GetYaxis()->SetTitleOffset(1.6000000000000001))
^
Error in <HandleInterpreterException>: Error evaluating expression (frame1->GetYaxis()->SetTitleOffset(1.6000000000000001))
Execution of your code was aborted.


Draw all canvases

In [12]:
%jsroot on
gROOT->GetListOfCanvases()->Draw()