Created
November 14, 2022 17:54
-
-
Save lgray/e6cea132684eaf224feac7e28ccd8fd7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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