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