2 Print information about objects in a ROOT file.
4 from __future__
import absolute_import
5 from __future__
import print_function
7 from builtins
import range
8 from .version
import __version__
10 from ROOT
import Double
12 from .
import argparse
17 saved_argv = sys.argv[:]
18 sys.argv = [sys.argv[0],
'-b']
19 from ROOT
import TFile, TH1, TDirectory, gDirectory
23 '''Recursive function to find all contents in a given ROOT file'''
24 keys = in_tfile.GetDirectory(full_path).GetListOfKeys()
27 classname = key.GetClassName()
28 if 'TDirectory' in classname:
33 if options.name
and name != options.name:
continue
34 full_name =
'/'.
join([full_path,name])
35 obj = in_tfile.Get(full_name)
38 simple_name = full_name[2:]
39 print(
"%s" % simple_name, end=
' ')
40 for arg
in [x[2:]
for x
in sys.argv
if x.startswith(
"--")]:
41 if "classname" == arg:
42 print(
"%s" % classname, end=
' ')
43 if obj.InheritsFrom(
'TH1'):
45 print(
" %i" % obj.GetEntries(), end=
' ')
47 if obj.InheritsFrom(
'TH2'):
50 for j
in reversed(list(
range(obj.GetNbinsY()))):
53 [
str(obj.GetBinContent(i+1, j+1))
for i
in range(obj.GetNbinsX())]), end=
' ')
56 [
str(obj.GetBinContent(i+1))
for i
in range(obj.GetNbinsX())]), end=
' ')
58 if obj.InheritsFrom(
'TH2'):
59 for j
in reversed(list(
range(obj.GetNbinsY()))):
62 [
str(obj.GetBinError(i+1, j+1))
for i
in range(obj.GetNbinsX())]), end=
' ')
65 [
str(obj.GetBinError(i+1))
for i
in range(obj.GetNbinsX())]), end=
' ')
66 if "bincenter" == arg:
68 [
str(obj.GetBinCenter(i+1))
for i
in range(obj.GetNbinsX())]), end=
' ')
70 print(
" %i" % obj.GetMaximum(), end=
' ')
72 print(
" %i" % obj.GetMinimum(), end=
' ')
74 print(
" %i" % obj.GetBinContent(obj.GetNbinsX()), end=
' ')
75 if "underflow" == arg:
76 print(
" %i" % obj.GetBinContent(0), end=
' ')
77 if obj.InheritsFrom(
'TGraph'):
79 x, y = Double(0), Double(0)
82 for i
in range(obj.GetN()):
84 xvals.append(copy.copy(x))
85 yvals.append(copy.copy(y))
86 for point
in zip(xvals,yvals):
87 print(
" (%d, %d)" % point, end=
' ')
91 parser = argparse.ArgumentParser(description=
'Print information from an SC2 replay file.')
92 parser.add_argument(
'filenames', metavar=
'filename', type=str, nargs=
'+',
93 help=
"Names of one or more root files")
94 parser.add_argument(
'--bincenter', action=
"store_true", default=
False,
95 help=
"Get Bin Centers from each bin in each histogram")
96 parser.add_argument(
'--classname', action=
"store_true", default=
False,
97 help=
"Get type from each object in root file")
98 parser.add_argument(
'--contents', action=
"store_true", default=
False,
99 help=
"Get Bin Contents from each bin in each histogram")
100 parser.add_argument(
'--errors', action=
"store_true", default=
False,
101 help=
"Get Bin Errors from each bin in each histogram")
102 parser.add_argument(
'--entries', action=
"store_true", default=
False,
103 help=
"Get Entries from each histogram")
104 parser.add_argument(
'--max', action=
"store_true", default=
False,
105 help=
"Get Maximum value from each histogram")
106 parser.add_argument(
'--min', action=
"store_true", default=
False,
107 help=
"Get Minimum value from each histogram")
108 parser.add_argument(
'--name', default=
None,
109 help=
"Get information only from object with matching name")
110 parser.add_argument(
'--overflow', action=
"store_true", default=
False,
111 help=
"Get value of overflow bin from each histogram")
112 parser.add_argument(
'--underflow', action=
"store_true", default=
False,
113 help=
"Get value of underflow bin from each histogram")
114 arguments = parser.parse_args()
115 for arg
in arguments.filenames:
116 if arg[-5:] !=
".root":
117 raise TypeError(
"Arguments must include root file names")
118 filenames_from_interface = [x
for x
in arguments.filenames
if x[-5:] ==
".root"]
119 if len(filenames_from_interface) == 0:
122 for filename
in filenames_from_interface:
123 if not os.path.exists(filename):
124 print(
"%s does not exist." % filename)
126 tfile = TFile(filename,
"read")
133 if __name__ ==
'__main__':