Skip to content

Instantly share code, notes, and snippets.

@lgray
Created November 14, 2022 17:54
Show Gist options
  • Select an option

  • Save lgray/e6cea132684eaf224feac7e28ccd8fd7 to your computer and use it in GitHub Desktop.

Select an option

Save lgray/e6cea132684eaf224feac7e28ccd8fd7 to your computer and use it in GitHub Desktop.
import uproot
import awkward
import hist
import numpy as np
import os
import math
def get_root_parent(index, mcparticles):
parents = mcparticles[index].member("parents").tolist()
if len(parents) == 0:
return index
elif len(parents) == 1:
return get_root_parent(parents.pop(), mcparticles)
else:
raise Error("this cannot/should not happen")
if __name__ == '__main__':
h1 = hist.Hist(hist.axis.Regular(bins=150, start=0, stop=45000, name="parent"))
h2 = hist.Hist(hist.axis.Regular(bins=100, start=-70, stop=70, name="z"),
hist.axis.Regular(bins=100, start=0, stop=70, name="r"),
)
x = uproot.open("C3SiD_300552.root")
mcParticles = x["EVENT"]["MCParticles"].array(library="np")
siVertexBarrelHits = x["EVENT"]["SiVertexBarrelHits"].array(library="np")
for index in range(len(mcParticles[0])):
parent = get_root_parent(index, mcParticles[0])
h1.fill(parent=parent)
for i in siVertexBarrelHits[0]:
x = i.member("position").member("fCoordinates").member("fX")
y = i.member("position").member("fCoordinates").member("fY")
z = i.member("position").member("fCoordinates").member("fZ")
h2.fill(z=z, r=math.sqrt(x*x+y*y))
with uproot.recreate("parentHist.root") as f:
f["h1"] = h1
f["h2"] = h2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment