CMS 3D CMS Logo

Functions | Variables
python.rootplot.rootinfo Namespace Reference

Functions

def main ()
 
def recurse_thru_file (in_tfile, options, full_path='/')
 

Variables

 argv
 
 saved_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

◆ main()

def python.rootplot.rootinfo.main ( )

Definition at line 90 of file rootinfo.py.

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 

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

◆ recurse_thru_file()

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.

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 

References join(), print(), FastTimerService_cff.range, str, and ComparisonHelper.zip().

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

Variable Documentation

◆ argv

python.rootplot.rootinfo.argv

Definition at line 18 of file rootinfo.py.

◆ saved_argv

python.rootplot.rootinfo.saved_argv

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

Definition at line 17 of file rootinfo.py.

FastTimerService_cff.range
range
Definition: FastTimerService_cff.py:34
join
static std::string join(char **cmd)
Definition: RemoteFile.cc:17
str
#define str(s)
Definition: TestProcessor.cc:52
python.rootplot.rootinfo.recurse_thru_file
def recurse_thru_file(in_tfile, options, full_path='/')
Definition: rootinfo.py:22
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
ComparisonHelper::zip
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
Definition: L1TStage2CaloLayer1.h:41
python.rootplot.rootinfo.main
def main()
Definition: rootinfo.py:90