CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Functions | Variables
python.rootplot.rootinfo Namespace Reference

Functions

def main
 
def recurse_thru_file
 

Variables

list saved_argv = sys.argv[:]
 Load classes from ROOT, ensuring it doesn't intercept -h or –help. More...
 

Detailed Description

Print information about objects in a ROOT file.

Function Documentation

def python.rootplot.rootinfo.main ( )

Definition at line 90 of file rootinfo.py.

References print(), and python.rootplot.rootinfo.recurse_thru_file().

90 
91 def main():
92  parser = argparse.ArgumentParser(description='Print information from an SC2 replay file.')
93  parser.add_argument('filenames', metavar='filename', type=str, nargs='+',
94  help="Names of one or more root files")
95  parser.add_argument('--bincenter', action="store_true", default=False,
96  help="Get Bin Centers from each bin in each histogram")
97  parser.add_argument('--classname', action="store_true", default=False,
98  help="Get type from each object in root file")
99  parser.add_argument('--contents', action="store_true", default=False,
100  help="Get Bin Contents from each bin in each histogram")
101  parser.add_argument('--errors', action="store_true", default=False,
102  help="Get Bin Errors from each bin in each histogram")
103  parser.add_argument('--entries', action="store_true", default=False,
104  help="Get Entries from each histogram")
105  parser.add_argument('--max', action="store_true", default=False,
106  help="Get Maximum value from each histogram")
107  parser.add_argument('--min', action="store_true", default=False,
108  help="Get Minimum value from each histogram")
109  parser.add_argument('--name', default=None,
110  help="Get information only from object with matching name")
111  parser.add_argument('--overflow', action="store_true", default=False,
112  help="Get value of overflow bin from each histogram")
113  parser.add_argument('--underflow', action="store_true", default=False,
114  help="Get value of underflow bin from each histogram")
115  arguments = parser.parse_args()
116  for arg in arguments.filenames:
117  if arg[-5:] != ".root":
118  raise TypeError("Arguments must include root file names")
119  filenames_from_interface = [x for x in arguments.filenames if x[-5:] == ".root"]
120  if len(filenames_from_interface) == 0:
121  parser.print_help()
122  sys.exit(0)
123  for filename in filenames_from_interface:
124  if not os.path.exists(filename):
125  print("%s does not exist." % filename)
126  sys.exit(0)
127  tfile = TFile(filename, "read")
128  try:
129  recurse_thru_file(tfile, arguments)
130  except IOError as e:
131  if e.errno != 32:
132  raise
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def python.rootplot.rootinfo.recurse_thru_file (   in_tfile,
  options,
  full_path = '/' 
)
Recursive function to find all contents in a given ROOT file

Definition at line 22 of file rootinfo.py.

References join(), print(), sistrip::SpyUtilities.range(), str, and ComparisonHelper.zip().

Referenced by python.rootplot.rootinfo.main().

22 
23 def recurse_thru_file(in_tfile, options, full_path='/'):
24  '''Recursive function to find all contents in a given ROOT file'''
25  keys = in_tfile.GetDirectory(full_path).GetListOfKeys()
26  for key in keys:
27  name = key.GetName()
28  classname = key.GetClassName()
29  if 'TDirectory' in classname:
30  gDirectory.cd(name)
31  recurse_thru_file(in_tfile, options, '/'.join([full_path,name]))
32  gDirectory.cd("..")
33  else:
34  if options.name and name != options.name: continue
35  full_name = '/'.join([full_path,name])
36  obj = in_tfile.Get(full_name)
37  if not obj:
38  continue
39  simple_name = full_name[2:]
40  print("%s" % simple_name, end=' ')
41  for arg in [x[2:] for x in sys.argv if x.startswith("--")]:
42  if "classname" == arg:
43  print("%s" % classname, end=' ')
44  if obj.InheritsFrom('TH1'):
45  if "entries" == arg:
46  print(" %i" % obj.GetEntries(), end=' ')
47  if "contents" == arg:
48  if obj.InheritsFrom('TH2'):
49  # Print contents as they would look on the 2D graph
50  # Left to right, top to bottom. Start in upper left corner.
51  for j in reversed(list(range(obj.GetNbinsY()))):
52  print()
53  print(" %s" % ' '.join(
54  [str(obj.GetBinContent(i+1, j+1)) for i in range(obj.GetNbinsX())]), end=' ')
55  else:
56  print(" %s" % ' '.join(
57  [str(obj.GetBinContent(i+1)) for i in range(obj.GetNbinsX())]), end=' ')
58  if "errors" == arg:
59  if obj.InheritsFrom('TH2'):
60  for j in reversed(list(range(obj.GetNbinsY()))):
61  print()
62  print(" %s" % ' '.join(
63  [str(obj.GetBinError(i+1, j+1)) for i in range(obj.GetNbinsX())]), end=' ')
64  else:
65  print(" %s" % ' '.join(
66  [str(obj.GetBinError(i+1)) for i in range(obj.GetNbinsX())]), end=' ')
67  if "bincenter" == arg:
68  print(" %s" % ' '.join(
69  [str(obj.GetBinCenter(i+1)) for i in range(obj.GetNbinsX())]), end=' ')
70  if "max" == arg:
71  print(" %i" % obj.GetMaximum(), end=' ')
72  if "min" == arg:
73  print(" %i" % obj.GetMinimum(), end=' ')
74  if "overflow" == arg:
75  print(" %i" % obj.GetBinContent(obj.GetNbinsX()), end=' ')
76  if "underflow" == arg:
77  print(" %i" % obj.GetBinContent(0), end=' ')
78  if obj.InheritsFrom('TGraph'):
79  if "contents" == arg:
80  x, y = Double(0), Double(0)
81  xvals = []
82  yvals = []
83  for i in range(obj.GetN()):
84  obj.GetPoint(i, x, y)
85  xvals.append(copy.copy(x))
86  yvals.append(copy.copy(y))
87  for point in zip(xvals,yvals):
88  print(" (%d, %d)" % point, end=' ')
89  print("")
const uint16_t range(const Frame &aFrame)
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
static std::string join(char **cmd)
Definition: RemoteFile.cc:19
#define str(s)

Variable Documentation

list python.rootplot.rootinfo.saved_argv = sys.argv[:]

Load classes from ROOT, ensuring it doesn't intercept -h or –help.

Definition at line 17 of file rootinfo.py.