CMS 3D CMS Logo

rootinfo.py
Go to the documentation of this file.
1 """
2 Print information about objects in a ROOT file.
3 """
4 from __future__ import absolute_import
5 from __future__ import print_function
6 
7 from builtins import range
8 from .version import __version__
9 
10 from ROOT import Double
11 import copy
12 from . import argparse
13 import sys
14 import os
15 
16 
17 saved_argv = sys.argv[:]
18 sys.argv = [sys.argv[0], '-b']
19 from ROOT import TFile, TH1, TDirectory, gDirectory
20 sys.argv = saved_argv
21 
22 def recurse_thru_file(in_tfile, options, full_path='/'):
23  '''Recursive function to find all contents in a given ROOT file'''
24  keys = in_tfile.GetDirectory(full_path).GetListOfKeys()
25  for key in keys:
26  name = key.GetName()
27  classname = key.GetClassName()
28  if 'TDirectory' in classname:
29  gDirectory.cd(name)
30  recurse_thru_file(in_tfile, options, '/'.join([full_path,name]))
31  gDirectory.cd("..")
32  else:
33  if options.name and name != options.name: continue
34  full_name = '/'.join([full_path,name])
35  obj = in_tfile.Get(full_name)
36  if not obj:
37  continue
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'):
44  if "entries" == arg:
45  print(" %i" % obj.GetEntries(), end=' ')
46  if "contents" == arg:
47  if obj.InheritsFrom('TH2'):
48  # Print contents as they would look on the 2D graph
49  # Left to right, top to bottom. Start in upper left corner.
50  for j in reversed(list(range(obj.GetNbinsY()))):
51  print()
52  print(" %s" % ' '.join(
53  [str(obj.GetBinContent(i+1, j+1)) for i in range(obj.GetNbinsX())]), end=' ')
54  else:
55  print(" %s" % ' '.join(
56  [str(obj.GetBinContent(i+1)) for i in range(obj.GetNbinsX())]), end=' ')
57  if "errors" == arg:
58  if obj.InheritsFrom('TH2'):
59  for j in reversed(list(range(obj.GetNbinsY()))):
60  print()
61  print(" %s" % ' '.join(
62  [str(obj.GetBinError(i+1, j+1)) for i in range(obj.GetNbinsX())]), end=' ')
63  else:
64  print(" %s" % ' '.join(
65  [str(obj.GetBinError(i+1)) for i in range(obj.GetNbinsX())]), end=' ')
66  if "bincenter" == arg:
67  print(" %s" % ' '.join(
68  [str(obj.GetBinCenter(i+1)) for i in range(obj.GetNbinsX())]), end=' ')
69  if "max" == arg:
70  print(" %i" % obj.GetMaximum(), end=' ')
71  if "min" == arg:
72  print(" %i" % obj.GetMinimum(), end=' ')
73  if "overflow" == arg:
74  print(" %i" % obj.GetBinContent(obj.GetNbinsX()), end=' ')
75  if "underflow" == arg:
76  print(" %i" % obj.GetBinContent(0), end=' ')
77  if obj.InheritsFrom('TGraph'):
78  if "contents" == arg:
79  x, y = Double(0), Double(0)
80  xvals = []
81  yvals = []
82  for i in range(obj.GetN()):
83  obj.GetPoint(i, x, y)
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=' ')
88  print("")
89 
90 def main():
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:
120  parser.print_help()
121  sys.exit(0)
122  for filename in filenames_from_interface:
123  if not os.path.exists(filename):
124  print("%s does not exist." % filename)
125  sys.exit(0)
126  tfile = TFile(filename, "read")
127  try:
128  recurse_thru_file(tfile, arguments)
129  except IOError as e:
130  if e.errno != 32:
131  raise
132 
133 if __name__ == '__main__':
134  main()
ALPAKA_FN_HOST_ACC ALPAKA_FN_INLINE constexpr float zip(ConstView const &tracks, int32_t i)
Definition: TracksSoA.h:90
def recurse_thru_file(in_tfile, options, full_path='/')
Definition: rootinfo.py:22
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
static std::string join(char **cmd)
Definition: RemoteFile.cc:21
Definition: main.py:1
#define str(s)