test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
6 from .version import __version__
7 
8 from ROOT import Double
9 import copy
10 from . import argparse
11 import sys
12 import os
13 
14 #### Load classes from ROOT, ensuring it doesn't intercept -h or --help
15 saved_argv = sys.argv[:]
16 sys.argv = [sys.argv[0], '-b']
17 from ROOT import TFile, TH1, TDirectory, gDirectory
18 sys.argv = saved_argv
19 
20 def recurse_thru_file(in_tfile, options, full_path='/'):
21  '''Recursive function to find all contents in a given ROOT file'''
22  keys = in_tfile.GetDirectory(full_path).GetListOfKeys()
23  for key in keys:
24  name = key.GetName()
25  classname = key.GetClassName()
26  if 'TDirectory' in classname:
27  gDirectory.cd(name)
28  recurse_thru_file(in_tfile, options, '/'.join([full_path,name]))
29  gDirectory.cd("..")
30  else:
31  if options.name and name != options.name: continue
32  full_name = '/'.join([full_path,name])
33  obj = in_tfile.Get(full_name)
34  if not obj:
35  continue
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'):
42  if "entries" == arg:
43  print " %i" % obj.GetEntries(),
44  if "contents" == arg:
45  if obj.InheritsFrom('TH2'):
46  # Print contents as they would look on the 2D graph
47  # Left to right, top to bottom. Start in upper left corner.
48  for j in reversed(range(obj.GetNbinsY())):
49  print
50  print " %s" % ' '.join(
51  [str(obj.GetBinContent(i+1, j+1)) for i in range(obj.GetNbinsX())]),
52  else:
53  print " %s" % ' '.join(
54  [str(obj.GetBinContent(i+1)) for i in range(obj.GetNbinsX())]),
55  if "errors" == arg:
56  if obj.InheritsFrom('TH2'):
57  for j in reversed(range(obj.GetNbinsY())):
58  print
59  print " %s" % ' '.join(
60  [str(obj.GetBinError(i+1, j+1)) for i in range(obj.GetNbinsX())]),
61  else:
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())]),
67  if "max" == arg:
68  print " %i" % obj.GetMaximum(),
69  if "min" == arg:
70  print " %i" % obj.GetMinimum(),
71  if "overflow" == arg:
72  print " %i" % obj.GetBinContent(obj.GetNbinsX()),
73  if "underflow" == arg:
74  print " %i" % obj.GetBinContent(0),
75  if obj.InheritsFrom('TGraph'):
76  if "contents" == arg:
77  x, y = Double(0), Double(0)
78  xvals = []
79  yvals = []
80  for i in range(obj.GetN()):
81  obj.GetPoint(i, x, y)
82  xvals.append(copy.copy(x))
83  yvals.append(copy.copy(y))
84  for point in zip(xvals,yvals):
85  print " (%d, %d)" % point,
86  print ""
87 
88 def main():
89  parser = argparse.ArgumentParser(description='Print information from an SC2 replay file.')
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:
118  parser.print_help()
119  sys.exit(0)
120  for filename in filenames_from_interface:
121  if not os.path.exists(filename):
122  print "%s does not exist." % filename
123  sys.exit(0)
124  tfile = TFile(filename, "read")
125  try:
126  recurse_thru_file(tfile, arguments)
127  except IOError as e:
128  if e.errno != 32:
129  raise
130 
131 if __name__ == '__main__':
132  main()
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
Definition: main.py:1