Loading [MathJax]/extensions/tex2jax.js
Logo ROOT   6.16/01
Reference Guide
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
rf603_multicpu.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4##
5## 'LIKELIHOOD AND MINIMIZATION' RooFit tutorial macro #603
6##
7## Setting up a multi-core parallelized unbinned maximum likelihood fit
8##
9## \macro_code
10##
11## \date February 2018
12## \author Clemens Lange
13## \author Wouter Verkerke (C version)
14
15
16import ROOT
17
18
19# Create 3D pdf and data
20# -------------------------------------------
21
22# Create observables
23x = ROOT.RooRealVar("x", "x", -5, 5)
24y = ROOT.RooRealVar("y", "y", -5, 5)
25z = ROOT.RooRealVar("z", "z", -5, 5)
26
27# Create signal pdf gauss(x)*gauss(y)*gauss(z)
28gx = ROOT.RooGaussian(
29 "gx", "gx", x, ROOT.RooFit.RooConst(0), ROOT.RooFit.RooConst(1))
30gy = ROOT.RooGaussian(
31 "gy", "gy", y, ROOT.RooFit.RooConst(0), ROOT.RooFit.RooConst(1))
32gz = ROOT.RooGaussian(
33 "gz", "gz", z, ROOT.RooFit.RooConst(0), ROOT.RooFit.RooConst(1))
34sig = ROOT.RooProdPdf("sig", "sig", ROOT.RooArgList(gx, gy, gz))
35
36# Create background pdf poly(x)*poly(y)*poly(z)
37px = ROOT.RooPolynomial("px", "px", x, ROOT.RooArgList(
38 ROOT.RooFit.RooConst(-0.1), ROOT.RooFit.RooConst(0.004)))
39py = ROOT.RooPolynomial("py", "py", y, ROOT.RooArgList(
40 ROOT.RooFit.RooConst(0.1), ROOT.RooFit.RooConst(-0.004)))
41pz = ROOT.RooPolynomial("pz", "pz", z)
42bkg = ROOT.RooProdPdf("bkg", "bkg", ROOT.RooArgList(px, py, pz))
43
44# Create composite pdf sig+bkg
45fsig = ROOT.RooRealVar("fsig", "signal fraction", 0.1, 0., 1.)
46model = ROOT.RooAddPdf("model", "model", ROOT.RooArgList(
47 sig, bkg), ROOT.RooArgList(fsig))
48
49# Generate large dataset
50data = model.generate(ROOT.RooArgSet(x, y, z), 200000)
51
52# Parallel fitting
53# -------------------------------
54
55# In parallel mode the likelihood calculation is split in N pieces,
56# that are calculated in parallel and added a posteriori before passing
57# it back to MINUIT.
58
59# Use four processes and time results both in wall time and CPU time
60model.fitTo(data, ROOT.RooFit.NumCPU(4), ROOT.RooFit.Timer(ROOT.kTRUE))
61
62# Parallel MC projections
63# ----------------------------------------------
64
65# Construct signal, likelihood projection on (y,z) observables and
66# likelihood ratio
67sigyz = sig.createProjection(ROOT.RooArgSet(x))
68totyz = model.createProjection(ROOT.RooArgSet(x))
69llratio_func = ROOT.RooFormulaVar(
70 "llratio", "log10(@0)-log10(@1)", ROOT.RooArgList(sigyz, totyz))
71
72# Calculate likelihood ratio for each event, subset of events with high
73# signal likelihood
74data.addColumn(llratio_func)
75dataSel = data.reduce(ROOT.RooFit.Cut("llratio>0.7"))
76
77# Make plot frame and plot data
78frame = x.frame(ROOT.RooFit.Title(
79 "Projection on X with LLratio(y,z)>0.7"), ROOT.RooFit.Bins(40))
80dataSel.plotOn(frame)
81
82# Perform parallel projection using MC integration of pdf using given input dataSet.
83# In self mode the data-weighted average of the pdf is calculated by splitting the
84# input dataset in N equal pieces and calculating in parallel the weighted average
85# one each subset. The N results of those calculations are then weighted into the
86# final result
87
88# Use four processes
89model.plotOn(frame, ROOT.RooFit.ProjWData(dataSel), ROOT.RooFit.NumCPU(4))
90
91c = ROOT.TCanvas("rf603_multicpu", "rf603_multicpu", 600, 600)
92ROOT.gPad.SetLeftMargin(0.15)
93frame.GetYaxis().SetTitleOffset(1.6)
94frame.Draw()
95
96c.SaveAs("rf603_multicpu.png")