8 Example of how to open and read a Katydid output file. 15 from ROOT
import TFile, TTreeReader, TTreeReaderValue
18 def ReadKTOutputFile(filename,var,katydid=False,objectType="TMultiTrackEventData",name="multiTrackEvents:Event
"): 20 Read a ROOT file and returns specified content. 21 filename (str): name of the ROOT file 22 var (str/list): Variable to extract from the object 23 katydid (bool): Katydid (true) or Cicada (false) namespace 24 objectType (str): class name of the object to read 25 name (str:str): names of the tree and of the object to read, separated by a ":" 28 file = TFile.Open(filename)
30 raise FileNotFoundError(
"File {} does not exist".format(filename))
34 names = name.split(
":")
36 tree = file.Get(str(names[0]))
38 treeReader = TTreeReader(tree)
41 import ROOT.Katydid
as KT
43 import ROOT.Cicada
as KT
44 if hasattr(KT,objectType):
45 multiTrackEvents = TTreeReaderValue(getattr(KT,objectType))(treeReader, names[1])
47 print(
"Couldn't find type <{}> in module; exiting!".format(objectType))
51 if isinstance(var,list):
53 result.update({key:[]})
54 elif isinstance(var,str):
55 result.update({var: []})
57 print(
"{} not a list or a string; exiting!".format(var))
59 for var,_
in result.items():
61 subArg = var.split(
".")
66 while treeReader.Next():
67 function = getattr(multiTrackEvents,
"Get{}".format(subArg[0]))
68 result[var].append(function())
71 while treeReader.Next():
72 function = getattr(multiTrackEvents,
"Get{}".format(subArg[0]))
75 for i
in range(multiTrackEvents.GetTracks().GetEntries()):
76 function = getattr(getattr(obj,
"At")(i),
"Get{}".format(subArg[1]))
77 subList.append(function())
78 result[var].append(subList)
82 print(
"Error: no data found; wrong branch? or wrong namespace (Cicada/Katydid) -- maybe, try '-k'/add True as last argument?")
83 if len(result.keys())==1:
84 return result[list(result.keys())[0]]
87 if __name__ ==
"__main__":
88 print(
'\n P8 Katydid output file reader example\n')
90 p = argparse.ArgumentParser(description=
''' 91 Katydid output file reader script 93 p.add_argument(
'-i',
'--input',
94 help=
'ROOT input file',
96 p.add_argument(
'-b',
'--branch',
97 help=
'Branch to read in the TMultiTrackEventData object',
99 default=
"StartTimeInAcq")
100 p.add_argument(
'-k',
'--katydid',
101 help=
'Flag stating Katydid namespace should be used instead of Cicada',
105 p.add_argument(
'-t',
'--type',
106 help=
'Object Type to be read',
109 default=
"TMultiTrackEventData")
110 p.add_argument(
'-n',
'--name',
111 help=
'Object and Tree Name to be read, separated by a ":" (ex: multiTrackEvents:Event)',
114 default=
"multiTrackEvents:Event")
115 args = p.parse_args()
117 result =
ReadKTOutputFile(args.input, args.branch, katydid=args.katydid, objectType=args.type, name=args.name)
118 if isinstance(result,dict):
119 nElements=len(result[list(result.keys())[0]])
121 nElements = len(result)
122 print(
"Found {} elements for {}".format(nElements,args.branch))
def loadLibraries(silence=False)
def ReadKTOutputFile(filename, var, katydid=False, objectType="TMultiTrackEventData", name="multiTrackEvents:Event")