CMS 3D CMS Logo

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