CMS 3D CMS Logo

diffTreeTool.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 from __future__ import print_function
4 from builtins import range
5 import optparse
6 import os
7 import re
8 from pprint import pprint
9 import six
10 
11 epsilon = 1.e-4
12 
13 def getPieceFromObject (obj, description):
14  """Returns piece from object """
15  parsed = GenObject.parseVariableTofill (description)
16  return GenObject.evaluateFunction (obj, parsed)
17 
18 def getDictFromObject (obj, varDict, prefix = ''):
19  """Given a object and a prefix, fills an return dictionary with the
20  proper values"""
21  if prefix:
22  obj = getPieceFromObject (obj, prefix)
23  retval = {}
24  for key, description in six.iteritems(varDict):
25  retval[key] = getPieceFromObject (obj, description)
26  return retval
27 
28 
29 def format (objDict, label, spacing=9, firstOnly = False):
30  '''return a formatted string for given object'''
31  value = objDict[label]
32  if firstOnly:
33  diff = 0.
34  else:
35  diff = objDict['delta_' + label]
36 
37  problem = False
38  if isinstance (value, float):
39  formatString = '%%%d.%df' % (spacing, spacing - 5)
40  retval = formatString % value
41  if abs(diff) > epsilon:
42  if options.delta:
43  retval += ' [' + formatString % (diff) + ']'
44  else:
45  retval += ' (' + formatString % (value + diff) + ')'
46  elif not firstOnly:
47  retval += ' ' * (spacing + 3)
48  return retval
49  else:
50  formatString = '%%%ds' % spacing
51  retval = formatString % value
52  if diff:
53  if isinstance (value, str):
54  retval += ' (' + formatString % diff + ')'
55  elif options.delta:
56  retval += ' [' + formatString % diff + ']'
57  else:
58  retval += ' (' + formatString % (value + diff) + ')'
59  elif not firstOnly:
60  retval += ' ' * (spacing + 3)
61  return retval
62 
63 
64 if __name__ == "__main__":
65  parser = optparse.OptionParser ("Usage: %prog bla.root lib.so var1 [var2]")
66  parser.add_option ("--delta", dest="delta",
67  action="store_true", default=False,
68  help="Show deltas when difference is large enough.")
69  parser.add_option ("--skipUndefined", dest="skipUndefined",
70  action="store_true", default=False,
71  help="Skip undefined variables without warning.")
72  options, args = parser.parse_args()
73  from Validation.Tools.GenObject import GenObject
74  if len (args) <= 2:
75  raise RuntimeError("Must provide root file, shlib location, "\
76  "and at least one variable")
77  rootFilename = args.pop(0)
78  shlib = args.pop(0)
79  variables = args
80  # play with shlib and cFile names
81  if not re.search (r'_C.so$', shlib) and not re.search (r'_C$', shlib):
82  shlib += '_C'
83  cFile = re.sub (r'_C$', r'.C', re.sub(r'\.so$','', shlib))
84  if not os.path.exists (cFile):
85  raise RuntimeError("Can not find accompying C file '%s'." % cFile)
86  if not os.path.exists (rootFilename):
87  raise RuntimeError("Can not find root file '%s'." % rootFilename)
88  # regex
89  diffContRE = re.compile (r'^class goDiffCont_(\w+)')
90  # diffRE = re.compile (r'^class goDiff_(\w+)')
91  variableREDict = {}
92  for var in variables:
93  variableREDict[var] = ( re.compile (r'\bdelta_%s\b' % var),
94  re.compile (r'\bother_%s\b' % var) )
95  source = open (cFile, 'r')
96  stringSet = set()
97  typeFoundSet = set()
98  name = ''
99 
100 
101  for line in source:
102  match = diffContRE.search (line)
103  if match:
104  if name:
105  raise RuntimeError("Currently only supported for a single"\
106  " class at a time.")
107  name = match.group(1)
108  continue
109  for key, regexTuple in six.iteritems(variableREDict):
110  if regexTuple[0].search(line):
111  typeFoundSet.add( key )
112  continue
113  if regexTuple[1].search(line):
114  typeFoundSet.add( key )
115  stringSet.add ( key )
116  if not name:
117  raise RuntimeError("Didn't find any Diff Container")
118  working = []
119  for var in variables:
120  if var not in typeFoundSet:
121  if not options.skipUndefined:
122  raise RuntimeError("Variable '%s' not found." % var)
123  else:
124  working.append (var)
125  variables = working
126  import ROOT
127  if ROOT.gSystem.Load (shlib):
128  raise RuntimeError("Can not load shilb '%s'." % shlib)
129  rootfile = ROOT.TFile.Open (rootFilename)
130  if not rootfile:
131  raise RuntimeError("Failed to open root file '%s'" % rootFilename)
132  tree = rootfile.Get ('diffTree')
133  if not tree:
134  raise RuntimeError("Failed to get 'diffTree'")
135  size = tree.GetEntries()
136  runeventDict = {'Run':'run', 'Event':'event'}
137  indexSingleDict = {'index':'index'}
138  indexDoubleDict = {'index':'index', 'delta_index':'delta_index'}
139  infoSingleDict = {}
140  infoDoubleDict = {}
141  for var in variables:
142  infoSingleDict[var] = infoDoubleDict[var] = var;
143  if var in stringSet:
144  infoDoubleDict['delta_' + var] = 'other_' + var
145  else:
146  infoDoubleDict['delta_' + var] = 'delta_' + var
147  for index in range (size):
148  tree.GetEntry (index)
149  runevent = getDictFromObject (tree, runeventDict, 'runevent')
150  pprint (runevent)
151  # first only
152  firstOnlyColl = getPieceFromObject (tree, name + '.firstOnly')
153  size = firstOnlyColl.size()
154  if size:
155  print("First Only:\n index ", end=' ')
156  for var in variables:
157  print("%-12s" % (' ' + var), end=' ')
158  print()
159  print('-' * (12 + 11 * len(variables)))
160  for index in range (size):
161  firstOnly = firstOnlyColl[index]
162  index = getDictFromObject (firstOnly, indexSingleDict)
163  print(' ', format (index, 'index', 3, firstOnly = True), end=' ')
164  info = getDictFromObject (firstOnly, infoSingleDict)
165  for var in variables:
166  print(' ', format (info, var, firstOnly = True), end=' ')
167  print()
168  print()
169  # second only
170  secondOnlyColl = getPieceFromObject (tree, name + '.secondOnly')
171  size = secondOnlyColl.size()
172  if size:
173  print("Second Only:\n index ", end=' ')
174  for var in variables:
175  print("%-12s" % (' ' + var), end=' ')
176  print()
177  print('-' * (12 + 11 * len(variables)))
178  for index in range (size):
179  secondOnly = secondOnlyColl[index]
180  index = getDictFromObject (secondOnly, indexSingleDict)
181  print(' ', format (index, 'index', 3, firstOnly = True), end=' ')
182  info = getDictFromObject (secondOnly, infoSingleDict)
183  for var in variables:
184  print(' ', format (info, var, firstOnly = True), end=' ')
185  print()
186  print()
187  # both
188  diffColl = getPieceFromObject (tree, name+'.diff')
189  size = diffColl.size()
190  if size:
191  print("Both:\n index", end=' ')
192  for var in variables:
193  print("%-24s" % (' ' + var), end=' ')
194  print()
195  print('-' * (16 + 23 * len(variables)))
196  for index in range (size):
197  diff = diffColl[index]
198  index = getDictFromObject (diff, indexDoubleDict)
199  print(' ', format (index, 'index', 3), end=' ')
200  info = getDictFromObject (diff, infoDoubleDict)
201  for var in variables:
202  print(' ', format (info, var), end=' ')
203  print()
204  print()
205 
diffTreeTool.getPieceFromObject
def getPieceFromObject(obj, description)
Definition: diffTreeTool.py:13
diffTreeTool.format
def format(objDict, label, spacing=9, firstOnly=False)
Definition: diffTreeTool.py:29
cond::persistency::search
std::vector< T >::const_iterator search(const cond::Time_t &val, const std::vector< T > &container)
Definition: IOVProxy.cc:19
edm::print
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
diffTreeTool.getDictFromObject
def getDictFromObject(obj, varDict, prefix='')
Definition: diffTreeTool.py:18
GenObject
funct::abs
Abs< T >::type abs(const T &t)
Definition: Abs.h:22