7 print(
"Syntax: haddnano.py out.root input1.root input2.root ...")
12 def zeroFill(tree, brName, brObj, allowNonBool=False):
14 branch_type_dict = {
'Bool_t': (
'?',
'O'),
'Float_t': (
'f4',
'F'),
'UInt_t': (
15 'u4',
'i'),
'Long64_t': (
'i8',
'L'),
'Double_t': (
'f8',
'D')}
16 brType = brObj.GetLeaf(brName).GetTypeName()
17 if (
not allowNonBool)
and (brType !=
"Bool_t"):
18 print((
"Did not expect to back fill non-boolean branches", tree, brName, brObj.GetLeaf(br).GetTypeName()))
20 if brType
not in branch_type_dict:
21 raise RuntimeError(
'Impossible to backfill branch of type %s' % brType)
22 buff = numpy.zeros(1, dtype=numpy.dtype(branch_type_dict[brType][0]))
23 b = tree.Branch(brName, buff, brName +
"/" +
24 branch_type_dict[brType][1])
26 b.SetBasketSize(tree.GetEntries() * 2)
27 for x
in range(0, tree.GetEntries()):
36 fileHandles.append(ROOT.TFile.Open(fn))
37 if fileHandles[-1].GetCompressionSettings() != fileHandles[0].GetCompressionSettings():
39 print(
"Disabling fast merging as inputs have different compressions")
40 of = ROOT.TFile(ofname,
"recreate")
42 of.SetCompressionSettings(fileHandles[0].GetCompressionSettings())
45 for e
in fileHandles[0].GetListOfKeys():
49 cl = ROOT.TClass.GetClass(e.GetClassName())
51 isTree = obj.IsA().InheritsFrom(ROOT.TTree.Class())
53 obj = obj.CloneTree(-1,
"fast" if goFast
else "")
54 branchNames = set([x.GetName()
for x
in obj.GetListOfBranches()])
55 for fh
in fileHandles[1:]:
56 if isTree
and obj.GetName() ==
'Events' and obj.GetEntries() == 0 :
58 print(
" 'Events' tree contsins no events; skipping")
59 obj = fh.GetListOfKeys().FindObject(name).ReadObj()
60 obj = obj.CloneTree(-1,
"fast" if goFast
else "")
61 branchNames = set([x.GetName()
for x
in obj.GetListOfBranches()])
63 otherObj = fh.GetListOfKeys().FindObject(name).ReadObj()
64 if isTree
and obj.GetName() ==
'Events' and otherObj.GetEntries() == 0 :
66 print(
" 'Events' tree contains no events; skipping")
69 if isTree
and obj.GetName() ==
'Events':
70 otherObj.SetAutoFlush(0)
71 otherBranches = set([x.GetName()
72 for x
in otherObj.GetListOfBranches()])
73 missingBranches = list(branchNames - otherBranches)
74 additionalBranches = list(otherBranches - branchNames)
75 print(
"missing: " +
str(missingBranches) +
"\n Additional: " +
str(additionalBranches))
76 for br
in missingBranches:
78 zeroFill(otherObj, br, obj.GetListOfBranches().FindObject(br))
79 for br
in additionalBranches:
82 zeroFill(obj, br, otherObj.GetListOfBranches().FindObject(br))
84 if isTree
and obj.GetName() ==
'Runs':
85 otherObj.SetAutoFlush(0)
86 otherBranches = set([x.GetName()
87 for x
in otherObj.GetListOfBranches()])
88 missingBranches = list(branchNames - otherBranches)
89 additionalBranches = list(otherBranches - branchNames)
90 print(
"missing: " +
str(missingBranches) +
"\n Additional: " +
str(additionalBranches))
91 for br
in missingBranches:
93 zeroFill(otherObj, br, obj.GetListOfBranches(
94 ).FindObject(br), allowNonBool=
True)
95 for br
in additionalBranches:
98 zeroFill(obj, br, otherObj.GetListOfBranches(
99 ).FindObject(br), allowNonBool=
True)
102 obj.Merge(inputs,
"fast" if goFast
else "")
107 elif obj.IsA().InheritsFrom(ROOT.TH1.Class()):
110 elif obj.IsA().InheritsFrom(ROOT.TObjString.Class()):
112 if st.GetString() != obj.GetString():
113 print(
"Strings are not matching")
115 elif obj.IsA().InheritsFrom(ROOT.THnSparse.Class()):
119 print(
"Cannot handle " +
str(obj.IsA().GetName()))
def zeroFill(tree, brName, brObj, allowNonBool=False)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)