CMS 3D CMS Logo

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