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 otherObj = fh.GetListOfKeys().FindObject(name).ReadObj()
58 if isTree
and obj.GetName() ==
'Events':
59 otherObj.SetAutoFlush(0)
60 otherBranches = set([x.GetName()
61 for x
in otherObj.GetListOfBranches()])
62 missingBranches = list(branchNames - otherBranches)
63 additionalBranches = list(otherBranches - branchNames)
64 print(
"missing: " +
str(missingBranches) +
"\n Additional:" +
str(additionalBranches))
65 for br
in missingBranches:
67 zeroFill(otherObj, br, obj.GetListOfBranches().FindObject(br))
68 for br
in additionalBranches:
71 zeroFill(obj, br, otherObj.GetListOfBranches().FindObject(br))
73 if isTree
and obj.GetName() ==
'Runs':
74 otherObj.SetAutoFlush(0)
75 otherBranches = set([x.GetName()
76 for x
in otherObj.GetListOfBranches()])
77 missingBranches = list(branchNames - otherBranches)
78 additionalBranches = list(otherBranches - branchNames)
79 print(
"missing: " +
str(missingBranches) +
"\n Additional:" +
str(additionalBranches))
80 for br
in missingBranches:
82 zeroFill(otherObj, br, obj.GetListOfBranches(
83 ).FindObject(br), allowNonBool=
True)
84 for br
in additionalBranches:
87 zeroFill(obj, br, otherObj.GetListOfBranches(
88 ).FindObject(br), allowNonBool=
True)
91 obj.Merge(inputs,
"fast" if goFast
else "")
96 elif obj.IsA().InheritsFrom(ROOT.TH1.Class()):
99 elif obj.IsA().InheritsFrom(ROOT.TObjString.Class()):
101 if st.GetString() != obj.GetString():
102 print(
"Strings are not matching")
105 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)