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