File indexing completed on 2023-10-25 10:07:40
0001
0002
0003 from __future__ import print_function
0004 from builtins import range
0005 import optparse
0006 import os
0007 import re
0008 from pprint import pprint
0009
0010 epsilon = 1.e-4
0011
0012 def getPieceFromObject (obj, description):
0013 """Returns piece from object """
0014 parsed = GenObject.parseVariableTofill (description)
0015 return GenObject.evaluateFunction (obj, parsed)
0016
0017 def getDictFromObject (obj, varDict, prefix = ''):
0018 """Given a object and a prefix, fills an return dictionary with the
0019 proper values"""
0020 if prefix:
0021 obj = getPieceFromObject (obj, prefix)
0022 retval = {}
0023 for key, description in varDict.items():
0024 retval[key] = getPieceFromObject (obj, description)
0025 return retval
0026
0027
0028 def format (objDict, label, spacing=9, firstOnly = False):
0029 '''return a formatted string for given object'''
0030 value = objDict[label]
0031 if firstOnly:
0032 diff = 0.
0033 else:
0034 diff = objDict['delta_' + label]
0035
0036 problem = False
0037 if isinstance (value, float):
0038 formatString = '%%%d.%df' % (spacing, spacing - 5)
0039 retval = formatString % value
0040 if abs(diff) > epsilon:
0041 if options.delta:
0042 retval += ' [' + formatString % (diff) + ']'
0043 else:
0044 retval += ' (' + formatString % (value + diff) + ')'
0045 elif not firstOnly:
0046 retval += ' ' * (spacing + 3)
0047 return retval
0048 else:
0049 formatString = '%%%ds' % spacing
0050 retval = formatString % value
0051 if diff:
0052 if isinstance (value, str):
0053 retval += ' (' + formatString % diff + ')'
0054 elif options.delta:
0055 retval += ' [' + formatString % diff + ']'
0056 else:
0057 retval += ' (' + formatString % (value + diff) + ')'
0058 elif not firstOnly:
0059 retval += ' ' * (spacing + 3)
0060 return retval
0061
0062
0063 if __name__ == "__main__":
0064 parser = optparse.OptionParser ("Usage: %prog bla.root lib.so var1 [var2]")
0065 parser.add_option ("--delta", dest="delta",
0066 action="store_true", default=False,
0067 help="Show deltas when difference is large enough.")
0068 parser.add_option ("--skipUndefined", dest="skipUndefined",
0069 action="store_true", default=False,
0070 help="Skip undefined variables without warning.")
0071 options, args = parser.parse_args()
0072 from Validation.Tools.GenObject import GenObject
0073 if len (args) <= 2:
0074 raise RuntimeError("Must provide root file, shlib location, "\
0075 "and at least one variable")
0076 rootFilename = args.pop(0)
0077 shlib = args.pop(0)
0078 variables = args
0079
0080 if not re.search (r'_C.so$', shlib) and not re.search (r'_C$', shlib):
0081 shlib += '_C'
0082 cFile = re.sub (r'_C$', r'.C', re.sub(r'\.so$','', shlib))
0083 if not os.path.exists (cFile):
0084 raise RuntimeError("Can not find accompying C file '%s'." % cFile)
0085 if not os.path.exists (rootFilename):
0086 raise RuntimeError("Can not find root file '%s'." % rootFilename)
0087
0088 diffContRE = re.compile (r'^class goDiffCont_(\w+)')
0089
0090 variableREDict = {}
0091 for var in variables:
0092 variableREDict[var] = ( re.compile (r'\bdelta_%s\b' % var),
0093 re.compile (r'\bother_%s\b' % var) )
0094 source = open (cFile, 'r')
0095 stringSet = set()
0096 typeFoundSet = set()
0097 name = ''
0098
0099
0100 for line in source:
0101 match = diffContRE.search (line)
0102 if match:
0103 if name:
0104 raise RuntimeError("Currently only supported for a single"\
0105 " class at a time.")
0106 name = match.group(1)
0107 continue
0108 for key, regexTuple in variableREDict.items():
0109 if regexTuple[0].search(line):
0110 typeFoundSet.add( key )
0111 continue
0112 if regexTuple[1].search(line):
0113 typeFoundSet.add( key )
0114 stringSet.add ( key )
0115 if not name:
0116 raise RuntimeError("Didn't find any Diff Container")
0117 working = []
0118 for var in variables:
0119 if var not in typeFoundSet:
0120 if not options.skipUndefined:
0121 raise RuntimeError("Variable '%s' not found." % var)
0122 else:
0123 working.append (var)
0124 variables = working
0125 import ROOT
0126 if ROOT.gSystem.Load (shlib):
0127 raise RuntimeError("Can not load shilb '%s'." % shlib)
0128 rootfile = ROOT.TFile.Open (rootFilename)
0129 if not rootfile:
0130 raise RuntimeError("Failed to open root file '%s'" % rootFilename)
0131 tree = rootfile.Get ('diffTree')
0132 if not tree:
0133 raise RuntimeError("Failed to get 'diffTree'")
0134 size = tree.GetEntries()
0135 runeventDict = {'Run':'run', 'Event':'event'}
0136 indexSingleDict = {'index':'index'}
0137 indexDoubleDict = {'index':'index', 'delta_index':'delta_index'}
0138 infoSingleDict = {}
0139 infoDoubleDict = {}
0140 for var in variables:
0141 infoSingleDict[var] = infoDoubleDict[var] = var;
0142 if var in stringSet:
0143 infoDoubleDict['delta_' + var] = 'other_' + var
0144 else:
0145 infoDoubleDict['delta_' + var] = 'delta_' + var
0146 for index in range (size):
0147 tree.GetEntry (index)
0148 runevent = getDictFromObject (tree, runeventDict, 'runevent')
0149 pprint (runevent)
0150
0151 firstOnlyColl = getPieceFromObject (tree, name + '.firstOnly')
0152 size = firstOnlyColl.size()
0153 if size:
0154 print("First Only:\n index ", end=' ')
0155 for var in variables:
0156 print("%-12s" % (' ' + var), end=' ')
0157 print()
0158 print('-' * (12 + 11 * len(variables)))
0159 for index in range (size):
0160 firstOnly = firstOnlyColl[index]
0161 index = getDictFromObject (firstOnly, indexSingleDict)
0162 print(' ', format (index, 'index', 3, firstOnly = True), end=' ')
0163 info = getDictFromObject (firstOnly, infoSingleDict)
0164 for var in variables:
0165 print(' ', format (info, var, firstOnly = True), end=' ')
0166 print()
0167 print()
0168
0169 secondOnlyColl = getPieceFromObject (tree, name + '.secondOnly')
0170 size = secondOnlyColl.size()
0171 if size:
0172 print("Second Only:\n index ", end=' ')
0173 for var in variables:
0174 print("%-12s" % (' ' + var), end=' ')
0175 print()
0176 print('-' * (12 + 11 * len(variables)))
0177 for index in range (size):
0178 secondOnly = secondOnlyColl[index]
0179 index = getDictFromObject (secondOnly, indexSingleDict)
0180 print(' ', format (index, 'index', 3, firstOnly = True), end=' ')
0181 info = getDictFromObject (secondOnly, infoSingleDict)
0182 for var in variables:
0183 print(' ', format (info, var, firstOnly = True), end=' ')
0184 print()
0185 print()
0186
0187 diffColl = getPieceFromObject (tree, name+'.diff')
0188 size = diffColl.size()
0189 if size:
0190 print("Both:\n index", end=' ')
0191 for var in variables:
0192 print("%-24s" % (' ' + var), end=' ')
0193 print()
0194 print('-' * (16 + 23 * len(variables)))
0195 for index in range (size):
0196 diff = diffColl[index]
0197 index = getDictFromObject (diff, indexDoubleDict)
0198 print(' ', format (index, 'index', 3), end=' ')
0199 info = getDictFromObject (diff, infoDoubleDict)
0200 for var in variables:
0201 print(' ', format (info, var), end=' ')
0202 print()
0203 print()
0204