Skip to content

Commit

Permalink
handle zero-events nanoAOD files
Browse files Browse the repository at this point in the history
  • Loading branch information
namapane committed Dec 9, 2023
1 parent ce34e99 commit 31bbfd0
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions PhysicsTools/NanoAOD/scripts/haddnano.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def zeroFill(tree, brName, brObj, allowNonBool=False):
fileHandles = []
goFast = True
for fn in files:
print("Adding file" + str(fn))
print("Adding file", str(fn))
fileHandles.append(ROOT.TFile.Open(fn))
if fileHandles[-1].GetCompressionSettings() != fileHandles[0].GetCompressionSettings():
goFast = False
Expand All @@ -44,7 +44,7 @@ def zeroFill(tree, brName, brObj, allowNonBool=False):

for e in fileHandles[0].GetListOfKeys():
name = e.GetName()
print("Merging" + str(name))
print("Merging", str(name))
obj = e.ReadObj()
cl = ROOT.TClass.GetClass(e.GetClassName())
inputs = ROOT.TList()
Expand All @@ -53,15 +53,26 @@ def zeroFill(tree, brName, brObj, allowNonBool=False):
obj = obj.CloneTree(-1, "fast" if goFast else "")
branchNames = set([x.GetName() for x in obj.GetListOfBranches()])
for fh in fileHandles[1:]:
if isTree and obj.GetName() == 'Events' and obj.GetEntries() == 0 :
# Zero-events first file. Skip to avoid messing up branches.
print(" 'Events' tree contsins no events; skipping")
obj = fh.GetListOfKeys().FindObject(name).ReadObj()
obj = obj.CloneTree(-1, "fast" if goFast else "")
branchNames = set([x.GetName() for x in obj.GetListOfBranches()])
continue
otherObj = fh.GetListOfKeys().FindObject(name).ReadObj()
if isTree and obj.GetName() == 'Events' and otherObj.GetEntries() == 0 :
# Zero-events file; skip
print(" 'Events' tree contains no events; skipping")
continue
inputs.Add(otherObj)
if isTree and obj.GetName() == 'Events':
otherObj.SetAutoFlush(0)
otherBranches = set([x.GetName()
for x in otherObj.GetListOfBranches()])
missingBranches = list(branchNames - otherBranches)
additionalBranches = list(otherBranches - branchNames)
print("missing: " + str(missingBranches) + "\n Additional:" + str(additionalBranches))
print("missing: " + str(missingBranches) + "\n Additional: " + str(additionalBranches))
for br in missingBranches:
# fill "Other"
zeroFill(otherObj, br, obj.GetListOfBranches().FindObject(br))
Expand All @@ -76,7 +87,7 @@ def zeroFill(tree, brName, brObj, allowNonBool=False):
for x in otherObj.GetListOfBranches()])
missingBranches = list(branchNames - otherBranches)
additionalBranches = list(otherBranches - branchNames)
print("missing: " + str(missingBranches) + "\n Additional:" + str(additionalBranches))
print("missing: " + str(missingBranches) + "\n Additional: " + str(additionalBranches))
for br in missingBranches:
# fill "Other"
zeroFill(otherObj, br, obj.GetListOfBranches(
Expand Down

0 comments on commit 31bbfd0

Please sign in to comment.