from __future__ import print_function
import ROOT
import math
x = ROOT.RooRealVar("x", "x", -10, 10)
y = ROOT.RooRealVar("y", "y", 0, 40)
c = ROOT.RooCategory("c", "c")
c.defineType("Plus", +1)
c.defineType("Minus", -1)
d = ROOT.RooDataSet("d", "d", {x, y, c})
for i in range(1000):
x.setVal(i / 50 - 10)
y.setVal(math.sqrt(1.0 * i))
if i % 2:
c.setLabel("Plus")
else:
c.setLabel("Minus")
if i < 3:
print(x, y, c)
d.add({x, y, c})
d.Print("v")
print("")
row = d.get()
row.Print("v")
print("")
d.get(900).Print("v")
print("")
print("\n >> d1 has only columns x,c")
d1 = d.reduce({x, c})
d1.Print("v")
print("\n >> d2 has only column y")
d2 = d.reduce({y})
d2.Print("v")
print("\n >> d3 has only the points with y>5.17")
d3 = d.reduce("y>5.17")
d3.Print("v")
print("\n >> d4 has only columns x, for data points with y>5.17")
d4 = d.reduce({x, c}, "y>5.17")
d4.Print("v")
print("\n >> merge d2(y) with d1(x,c) to form d1(x,c,y)")
d1.merge(d2)
d1.Print("v")
print("\n >> append data points of d3 to d1")
d1.append(d3)
d1.Print("v")
print(">> construct dh (binned) from d(unbinned) but only take the x and y dimensions, ")
print(">> the category 'c' will be projected in the filling process")
x.setBins(10)
y.setBins(10)
dh = ROOT.RooDataHist("dh", "binned version of d", {x, y}, d)
dh.Print("v")
yframe = y.frame(Bins=10, Title="Operations on binned datasets")
dh.plotOn(yframe)
print(">> number of bins in dh : ", dh.numEntries())
print(">> sum of weights in dh : ", dh.sum(False))
print(">> integral over histogram: ", dh.sum(True))
x.setVal(0.3)
y.setVal(20.5)
print(">> retrieving the properties of the bin enclosing coordinate (x,y) = (0.3,20.5) bin center:")
dh.get({x, y}).Print("v")
print(" weight = ", dh.weight())
print(">> Creating 1-dimensional projection on y of dh for bins with x>0")
dh2 = dh.reduce({y}, "x>0")
dh2.Print("v")
dh2.plotOn(yframe, LineColor="r", MarkerColor="r")
print("\n >> Persisting d via ROOT I/O")
f = ROOT.TFile("rf402_datahandling.root", "RECREATE")
d.Write()
f.ls()
c = ROOT.TCanvas("rf402_datahandling", "rf402_datahandling", 600, 600)
ROOT.gPad.SetLeftMargin(0.15)
yframe.GetYaxis().SetTitleOffset(1.4)
yframe.Draw()
c.SaveAs("rf402_datahandling.png")