2 Print information about objects in a ROOT file.
4 from __future__
import absolute_import
6 from .version
import __version__
8 from ROOT
import Double
10 from .
import argparse
15 saved_argv = sys.argv[:]
16 sys.argv = [sys.argv[0],
'-b']
17 from ROOT
import TFile, TH1, TDirectory, gDirectory
21 '''Recursive function to find all contents in a given ROOT file'''
22 keys = in_tfile.GetDirectory(full_path).GetListOfKeys()
25 classname = key.GetClassName()
26 if 'TDirectory' in classname:
31 if options.name
and name != options.name:
continue
32 full_name =
'/'.
join([full_path,name])
33 obj = in_tfile.Get(full_name)
36 simple_name = full_name[2:]
37 print "%s" % simple_name,
38 for arg
in [x[2:]
for x
in sys.argv
if x.startswith(
"--")]:
39 if "classname" == arg:
40 print "%s" % classname,
41 if obj.InheritsFrom(
'TH1'):
43 print " %i" % obj.GetEntries(),
45 if obj.InheritsFrom(
'TH2'):
48 for j
in reversed(range(obj.GetNbinsY())):
50 print " %s" %
' '.
join(
51 [str(obj.GetBinContent(i+1, j+1))
for i
in range(obj.GetNbinsX())]),
53 print " %s" %
' '.
join(
54 [str(obj.GetBinContent(i+1))
for i
in range(obj.GetNbinsX())]),
56 if obj.InheritsFrom(
'TH2'):
57 for j
in reversed(range(obj.GetNbinsY())):
59 print " %s" %
' '.
join(
60 [str(obj.GetBinError(i+1, j+1))
for i
in range(obj.GetNbinsX())]),
62 print " %s" %
' '.
join(
63 [str(obj.GetBinError(i+1))
for i
in range(obj.GetNbinsX())]),
64 if "bincenter" == arg:
65 print " %s" %
' '.
join(
66 [str(obj.GetBinCenter(i+1))
for i
in range(obj.GetNbinsX())]),
68 print " %i" % obj.GetMaximum(),
70 print " %i" % obj.GetMinimum(),
72 print " %i" % obj.GetBinContent(obj.GetNbinsX()),
73 if "underflow" == arg:
74 print " %i" % obj.GetBinContent(0),
75 if obj.InheritsFrom(
'TGraph'):
77 x, y = Double(0), Double(0)
80 for i
in range(obj.GetN()):
82 xvals.append(copy.copy(x))
83 yvals.append(copy.copy(y))
84 for point
in zip(xvals,yvals):
85 print " (%d, %d)" % point,
90 parser.add_argument(
'filenames', metavar=
'filename', type=str, nargs=
'+',
91 help=
"Names of one or more root files")
92 parser.add_argument(
'--bincenter', action=
"store_true", default=
False,
93 help=
"Get Bin Centers from each bin in each histogram")
94 parser.add_argument(
'--classname', action=
"store_true", default=
False,
95 help=
"Get type from each object in root file")
96 parser.add_argument(
'--contents', action=
"store_true", default=
False,
97 help=
"Get Bin Contents from each bin in each histogram")
98 parser.add_argument(
'--errors', action=
"store_true", default=
False,
99 help=
"Get Bin Errors from each bin in each histogram")
100 parser.add_argument(
'--entries', action=
"store_true", default=
False,
101 help=
"Get Entries from each histogram")
102 parser.add_argument(
'--max', action=
"store_true", default=
False,
103 help=
"Get Maximum value from each histogram")
104 parser.add_argument(
'--min', action=
"store_true", default=
False,
105 help=
"Get Minimum value from each histogram")
106 parser.add_argument(
'--name', default=
None,
107 help=
"Get information only from object with matching name")
108 parser.add_argument(
'--overflow', action=
"store_true", default=
False,
109 help=
"Get value of overflow bin from each histogram")
110 parser.add_argument(
'--underflow', action=
"store_true", default=
False,
111 help=
"Get value of underflow bin from each histogram")
112 arguments = parser.parse_args()
113 for arg
in arguments.filenames:
114 if arg[-5:] !=
".root":
115 raise TypeError(
"Arguments must include root file names")
116 filenames_from_interface = [x
for x
in arguments.filenames
if x[-5:] ==
".root"]
117 if len(filenames_from_interface) == 0:
120 for filename
in filenames_from_interface:
121 if not os.path.exists(filename):
122 print "%s does not exist." % filename
124 tfile = TFile(filename,
"read")
131 if __name__ ==
'__main__':
static std::string join(char **cmd)