Rf 6 0 3_Multicpu

Likelihood and minimization: setting up a multi-core parallelized unbinned maximum likelihood fit

Author: Wouter Verkerke
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Monday, January 17, 2022 at 10:07 AM.

In [1]:
%%cpp -d
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "RooConstVar.h"
#include "RooPolynomial.h"
#include "RooAddPdf.h"
#include "RooProdPdf.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "RooPlot.h"
In [2]:
%%cpp -d
// This is a workaround to make sure the namespace is used inside functions
using namespace RooFit;

Create 3d pdf and data

Create observables

In [3]:
RooRealVar x("x", "x", -5, 5);
RooRealVar y("y", "y", -5, 5);
RooRealVar z("z", "z", -5, 5);
RooFit v3.60 -- Developed by Wouter Verkerke and David Kirkby 
                Copyright (C) 2000-2013 NIKHEF, University of California & Stanford University
                All rights reserved, please read http://roofit.sourceforge.net/license.txt

Create signal pdf gauss(x)gauss(y)gauss(z)

In [4]:
RooGaussian gx("gx", "gx", x, RooConst(0), RooConst(1));
RooGaussian gy("gy", "gy", y, RooConst(0), RooConst(1));
RooGaussian gz("gz", "gz", z, RooConst(0), RooConst(1));
RooProdPdf sig("sig", "sig", RooArgSet(gx, gy, gz));

Create background pdf poly(x)poly(y)poly(z)

In [5]:
RooPolynomial px("px", "px", x, RooArgSet(-0.1, 0.004));
RooPolynomial py("py", "py", y, RooArgSet(0.1, -0.004));
RooPolynomial pz("pz", "pz", z);
RooProdPdf bkg("bkg", "bkg", RooArgSet(px, py, pz));

Create composite pdf sig+bkg

In [6]:
RooRealVar fsig("fsig", "signal fraction", 0.1, 0., 1.);
RooAddPdf model("model", "model", RooArgList(sig, bkg), fsig);

Generate large dataset

In [7]:
RooDataSet *data = model.generate(RooArgSet(x, y, z), 200000);

Parallel fitting

In parallel mode the likelihood calculation is split in n pieces, that are calculated in parallel and added a posteriori before passing it back to MINUIT.

Use four processes and time results both in wall time and cpu time

In [8]:
model.fitTo(*data, NumCPU(4), Timer(kTRUE));
input_line_58:2:15: error: reference to 'data' is ambiguous
 model.fitTo(*data, NumCPU(4), Timer(kTRUE));
              ^
input_line_57:2:14: note: candidate found by name lookup is '__cling_N525::data'
 RooDataSet *data = model.generate(RooArgSet(x, y, z), 200000);
             ^
/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
    ^

Parallel mc projections

Construct signal, total likelihood projection on (y,z) observables and likelihood ratio

In [9]:
RooAbsPdf *sigyz = sig.createProjection(x);
RooAbsPdf *totyz = model.createProjection(x);
RooFormulaVar llratio_func("llratio", "log10(@0)-log10(@1)", RooArgList(*sigyz, *totyz));

Calculate likelihood ratio for each event, define subset of events with high signal likelihood

In [10]:
data->addColumn(llratio_func);
RooDataSet *dataSel = (RooDataSet *)data->reduce(Cut("llratio>0.7"));
input_line_63:2:2: error: reference to 'data' is ambiguous
 data->addColumn(llratio_func);
 ^
input_line_57:2:14: note: candidate found by name lookup is '__cling_N525::data'
 RooDataSet *data = model.generate(RooArgSet(x, y, z), 200000);
             ^
/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_63:3:37: error: reference to 'data' is ambiguous
RooDataSet *dataSel = (RooDataSet *)data->reduce(Cut("llratio>0.7"));
                                    ^
input_line_57:2:14: note: candidate found by name lookup is '__cling_N525::data'
 RooDataSet *data = model.generate(RooArgSet(x, y, z), 200000);
             ^
/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
    ^

Make plot frame and plot data

In [11]:
RooPlot *frame = x.frame(Title("Projection on X with LLratio(y,z)>0.7"), Bins(40));
dataSel->plotOn(frame);
input_line_65:2:3: error: use of undeclared identifier 'dataSel'
 (dataSel->plotOn(((*(class RooPlot **)0x7fc2f6019bc8))))
  ^
Error in <HandleInterpreterException>: Error evaluating expression (dataSel->plotOn(((*(class RooPlot **)0x7fc2f6019bc8))))
Execution of your code was aborted.

Perform parallel projection using mc integration of pdf using given input dataset. In this mode the data-weighted average of the pdf is calculated by splitting the input dataset in N equal pieces and calculating in parallel the weighted average one each subset. The N results of those calculations are then weighted into the final result

Use four processes

In [12]:
model.plotOn(frame, ProjWData(*dataSel), NumCPU(4));

new TCanvas("rf603_multicpu", "rf603_multicpu", 600, 600);
gPad->SetLeftMargin(0.15);
frame->GetYaxis()->SetTitleOffset(1.6);
frame->Draw();
input_line_66:2:43: error: cannot initialize an array element of type 'void *' with an rvalue of type 'RooCmdArg (*)(Int_t, Int_t)' (aka 'RooCmdArg (*)(int, int)')
 model.plotOn(frame, ProjWData(*dataSel), NumCPU(4));
                                          ^~~~~~

Draw all canvases

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