Numeric algorithm tuning: caching of slow numeric integrals and parameterizations of slow numeric integrals
import sys
import ROOT
def getWorkspace(mode):
w = ROOT.RooWorkspace()
if mode != 2:
w = ROOT.RooWorkspace("w", 1)
w.factory(
"EXPR::model('1/((x-a)*(x-a)+0.01)+1/((y-a)*(y-a)+0.01)+1/((z-a)*(z-a)+0.01)',x[-1,1],y[-1,1],z[-1,1],a[-5,5])")
if mode == 1:
w.pdf("model").setStringAttribute("CACHEPARMINT", "x:y:z")
normSet = ROOT.RooArgSet(w.var("x"), w.var("y"), w.var("z"))
w.pdf("model").getVal(normSet)
w.writeToFile("rf903_numintcache.root")
if (mode == 2):
f = ROOT.TFile("rf903_numintcache.root")
w = f.Get("w")
return w
mode = 0
w = getWorkspace(mode)
if mode == 1:
w.Print()
hhcache = w.expensiveObjectCache().getObj(1)
if (hhcache):
ROOT.TCanvas("rf903_numintcache", "rf903_numintcache", 600, 600)
hhcache.createHistogram(
"a").
Draw()
else:
ROOT.RooFit.Error("rf903_numintcache",
"Cached histogram is not existing in workspace")
sys.exit()
d = w.pdf("model").generate(
ROOT.RooArgSet(
w.var("x"),
w.var("y"),
w.var("z")),
1000)
w.pdf("model").fitTo(
d, ROOT.RooFit.Verbose(
ROOT.kTRUE), ROOT.RooFit.Timer(
ROOT.kTRUE))
framex = w.var("x").frame(ROOT.RooFit.Title("Projection of 3D model on X"))
d.plotOn(framex)
w.pdf("model").plotOn(framex)
c = ROOT.TCanvas("rf903_numintcache", "rf903_numintcache", 600, 600)
framex.Draw()
c.SaveAs("rf903_numintcache.png")
ROOT.gDirectory.Add(w)