5 from ROOT
import xAOD, TFile
8 from argparse
import ArgumentParser
10 from numpy
import array
11 from InDetMeasurementUtilities.CSV_InDetImporter
import getCSVFilename
14 def __init__(self, inputAOD, outputDir, dict_variables_types, treename="CollectionTree", nEvents=-1):
17 self.
tree = xAOD.MakeTransientTree( TFile(inputAOD), treename )
21 if nEvents < 0
or nEvents > self.
n_entries:
25 print(f
"Running on {self.nEvents} events")
39 with open(filename,
"w")
as out:
40 writer = csv.writer(out)
41 writer.writerow(dictionary.keys())
42 writer.writerows( zip(*dictionary.values()) )
43 print(f
"New file saved: {filename}")
47 arr = ArrayFloat3.data()
58 self.
tree.GetEntry(evt)
59 EventNumber = self.
tree.EventInfo.mcEventNumber()
66 tp = getattr(self.
tree, container)
68 print(
".. Missing ", container)
72 print(
".. Empty ", container)
75 for var,fmt
in dict_container.items():
77 sp = tp.getConstDataSpan[ fmt ]( var)
80 print(
"getConstDataSpan failed for variable ",var,fmt, container)
81 sp = [ getattr(element, var)()
for element
in tp ]
85 list_of_lists = [
list(std_vector)
for std_vector
in sp]
87 max_len =
max(len(ll)
for ll
in list_of_lists)
89 for ll
in list_of_lists:
90 ll.extend([np.nan] * (max_len - len(ll)))
95 sp = np.array(list_of_lists)
97 if "ArrayFloat3" in fmt
and len(sp) > 0:
100 if (
"unsigned char" in fmt
or "uint8" in fmt)
and len(sp) > 0:
101 sp = sp.view(np.int32)
107 for column
in range(sp.shape[1]):
108 dict_lists[var+f
"_at{column}"] = sp.T[column]
119 if __name__ ==
"__main__":
122 parser = ArgumentParser()
123 parser.add_argument(
'--inputAOD', type=str, default=
"", help=
"Input AOD.root file")
124 parser.add_argument(
'--outputDir', type=str, default=
"", help=
"Output directory")
125 parser.add_argument(
'--treename', type=str, default=
"CollectionTree")
126 parser.add_argument(
'--nEvents', type=int, default=-1, help=
"Number of events")
127 parser.add_argument(
'--CSV_DictFormats', type=str, default=
"InDetMeasurementUtilities.CSV_DictFormats",
128 help=
"Name of the python file (ex. local CSV_DictFormats) with variable list. Default: InDetMeasurementUtilities.CSV_DictFormats" )
129 parser.add_argument(
'--renames', type=str, default=
"", help=
"Names of collections other than default eg. InDetTrackParticles=NewColl,ITkPixelClusters=NewClusters,...")
131 args = parser.parse_args()
134 module = importlib.import_module(args.CSV_DictFormats)
135 CSV_DictFormats = module.CSV_DictFormats
137 if args.inputAOD ==
"":
138 raise Exception(
"No inputAOD was provided!")
140 if args.outputDir ==
"":
141 raise Exception(
"No outputDir was provided!")
143 if args.renames !=
"":
144 for n
in args.renames.split(
","):
145 old,new = n.split(
"=")
146 CSV_DictFormats[new] = CSV_DictFormats[old]
147 del CSV_DictFormats[old]
148 print(
"Instead of", old,
"will use", new)
151 outputDir=args.outputDir,
152 dict_variables_types=CSV_DictFormats,
153 treename=args.treename,
154 nEvents=args.nEvents)