8 Example of how to open and read a Katydid output file. 13 CicadaPy.loadLibraries(
True)
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 if not file.GetListOfKeys().Contains(str(names[0])):
37 print(
"Error: no tree {} in file".format(str(names[0])))
40 tree = file.Get(str(names[0]))
42 treeReader = TTreeReader(tree)
45 import ROOT.Katydid
as KT
47 import ROOT.Cicada
as KT
48 if hasattr(KT,objectType):
49 multiTrackEvents = TTreeReaderValue(getattr(KT,objectType))(treeReader, names[1])
51 print(
"Couldn't find type <{}> in module; exiting!".format(objectType))
55 if isinstance(var,list):
57 result.update({key:[]})
58 elif isinstance(var,str):
59 result.update({var: []})
61 print(
"{} not a list or a string; exiting!".format(var))
63 for var,_
in result.items():
65 subArg = var.split(
".")
70 while treeReader.Next():
71 function = getattr(multiTrackEvents,
"Get{}".format(subArg[0]))
72 result[var].append(function())
75 while treeReader.Next():
76 function = getattr(multiTrackEvents,
"Get{}".format(subArg[0]))
79 for i
in range(multiTrackEvents.GetTracks().GetEntries()):
80 function = getattr(getattr(obj,
"At")(i),
"Get{}".format(subArg[1]))
81 subList.append(function())
82 result[var].append(subList)
86 print(
"Error: no data found; wrong branch? or wrong namespace (Cicada/Katydid) -- maybe, try '-k'/add True as last argument?")
87 if len(result.keys())==1:
88 return result[list(result.keys())[0]]
91 if __name__ ==
"__main__":
92 print(
'\n P8 Katydid output file reader example\n')
94 p = argparse.ArgumentParser(description=
''' 95 Katydid output file reader script 97 p.add_argument(
'-i',
'--input',
98 help=
'ROOT input file',
100 p.add_argument(
'-b',
'--branch',
101 help=
'Branch to read in the TMultiTrackEventData object',
103 default=
"StartTimeInAcq")
104 p.add_argument(
'-k',
'--katydid',
105 help=
'Flag stating Katydid namespace should be used instead of Cicada',
109 p.add_argument(
'-t',
'--type',
110 help=
'Object Type to be read',
113 default=
"TMultiTrackEventData")
114 p.add_argument(
'-n',
'--name',
115 help=
'Object and Tree Name to be read, separated by a ":" (ex: multiTrackEvents:Event)',
118 default=
"multiTrackEvents:Event")
119 args = p.parse_args()
121 result =
ReadKTOutputFile(args.input, args.branch, katydid=args.katydid, objectType=args.type, name=args.name)
122 if isinstance(result,dict):
123 nElements=len(result[list(result.keys())[0]])
125 nElements = len(result)
126 print(
"Found {} elements for {}".format(nElements,args.branch))
def ReadKTOutputFile(filename, var, katydid=False, objectType="TMultiTrackEventData", name="multiTrackEvents:Event")