CMS 3D CMS Logo

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