CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/Validation/Tools/scripts/diffTreeTool.py

Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 import optparse
00004 import os
00005 import re
00006 from pprint import pprint
00007 
00008 epsilon = 1.e-4
00009 
00010 def getPieceFromObject (obj, description):
00011     """Returns piece from object """
00012     parsed = GenObject.parseVariableTofill (description)
00013     return GenObject.evaluateFunction (obj, parsed)
00014 
00015 def getDictFromObject (obj, varDict, prefix = ''):
00016     """Given a object and a prefix, fills an return dictionary with the
00017     proper values"""
00018     if prefix:
00019         obj = getPieceFromObject (obj, prefix)
00020     retval = {}
00021     for key, description in varDict.iteritems():
00022         retval[key] = getPieceFromObject (obj, description)
00023     return retval
00024 
00025 
00026 def format (objDict, label, spacing=9, firstOnly = False):
00027     '''return a formatted string for given object'''
00028     value = objDict[label]
00029     if firstOnly:
00030         diff = 0.
00031     else:
00032         diff  = objDict['delta_' + label]
00033         
00034     problem = False
00035     if isinstance (value, float):
00036         formatString = '%%%d.%df' % (spacing, spacing - 5)
00037         retval = formatString % value
00038         if abs(diff) > epsilon:
00039             if options.delta:
00040                 retval += ' [' + formatString % (diff) + ']'
00041             else:
00042                 retval += ' (' + formatString % (value + diff) + ')'
00043         elif not firstOnly:
00044             retval += ' ' * (spacing + 3)
00045         return retval
00046     else:
00047         formatString = '%%%ds' % spacing
00048         retval = formatString % value
00049         if diff:
00050             if isinstance (value, str):
00051                 retval += ' (' + formatString % diff + ')'
00052             elif options.delta:
00053                 retval += ' [' + formatString % diff + ']'
00054             else:
00055                 retval += ' (' + formatString % (value + diff) + ')'
00056         elif not firstOnly:
00057             retval += ' ' * (spacing + 3)
00058         return retval
00059     
00060 
00061 if __name__ == "__main__":
00062     parser = optparse.OptionParser ("Usage: %prog bla.root lib.so var1 [var2]")
00063     parser.add_option ("--delta", dest="delta",
00064                        action="store_true", default=False,
00065                        help="Show deltas when difference is large enough.")
00066     parser.add_option ("--skipUndefined", dest="skipUndefined",
00067                        action="store_true", default=False,
00068                        help="Skip undefined variables without warning.")
00069     options, args = parser.parse_args()
00070     from Validation.Tools.GenObject import GenObject
00071     if len (args) <= 2:
00072         raise RuntimeError, "Must provide root file, shlib location, "\
00073               "and at least one variable"
00074     rootFilename  = args.pop(0)
00075     shlib     = args.pop(0)
00076     variables = args
00077     # play with shlib and cFile names
00078     if not re.search (r'_C.so$', shlib) and not re.search (r'_C$', shlib):
00079         shlib += '_C'
00080     cFile = re.sub (r'_C$', r'.C', re.sub(r'\.so$','', shlib))
00081     if not os.path.exists (cFile):
00082         raise RuntimeError, "Can not find accompying C file '%s'."  % cFile
00083     if not os.path.exists (rootFilename):
00084         raise RuntimeError, "Can not find root file '%s'."  % rootFilename
00085     # regex
00086     diffContRE  = re.compile (r'^class goDiffCont_(\w+)')
00087     # diffRE      = re.compile (r'^class goDiff_(\w+)')
00088     variableREDict = {}
00089     for var in variables:
00090         variableREDict[var] = ( re.compile (r'\bdelta_%s\b' % var),
00091                                 re.compile (r'\bother_%s\b' % var) ) 
00092     source = open (cFile, 'r')
00093     stringSet    = set()
00094     typeFoundSet = set()
00095     name         = ''
00096 
00097     
00098     for line in source:
00099         match = diffContRE.search (line)
00100         if match:
00101             if name:
00102                 raise RuntimeError, "Currently only supported for a single"\
00103                       " class at a time."
00104             name = match.group(1)
00105             continue
00106         for key, regexTuple in variableREDict.iteritems():
00107             if regexTuple[0].search(line):
00108                 typeFoundSet.add( key )
00109                 continue
00110             if regexTuple[1].search(line):
00111                 typeFoundSet.add( key )
00112                 stringSet.add   ( key )
00113     if not name:
00114         raise RuntimeError, "Didn't find any Diff Container"
00115     working = []
00116     for var in variables:
00117         if var not in typeFoundSet:
00118             if not options.skipUndefined:
00119                 raise RuntimeError, "Variable '%s' not found." % var
00120         else:
00121             working.append (var)
00122     variables = working
00123     import ROOT
00124     if ROOT.gSystem.Load (shlib):
00125         raise RuntimeError, "Can not load shilb '%s'." % shlib
00126     rootfile = ROOT.TFile.Open (rootFilename)
00127     if not rootfile:
00128         raise RuntimeError, "Failed to open root file '%s'" % rootFilename
00129     tree = rootfile.Get ('diffTree')
00130     if not tree:
00131         raise RuntimeError, "Failed to get 'diffTree'"
00132     size = tree.GetEntries()
00133     runeventDict = {'Run':'run', 'Event':'event'}
00134     indexSingleDict = {'index':'index'}
00135     indexDoubleDict = {'index':'index', 'delta_index':'delta_index'}
00136     infoSingleDict = {}
00137     infoDoubleDict = {}
00138     for var in variables:
00139         infoSingleDict[var] = infoDoubleDict[var] = var;        
00140         if var in stringSet:
00141             infoDoubleDict['delta_' + var] = 'other_' + var
00142         else:
00143             infoDoubleDict['delta_' + var] = 'delta_' + var
00144     for index in range (size):
00145         tree.GetEntry (index)
00146         runevent = getDictFromObject (tree, runeventDict, 'runevent')
00147         pprint (runevent)
00148         # first only
00149         firstOnlyColl  = getPieceFromObject (tree, name + '.firstOnly')
00150         size = firstOnlyColl.size()
00151         if size:            
00152             print "First Only:\n   index    ",
00153             for var in variables:
00154                 print "%-12s" % (' ' + var),
00155             print
00156             print '-' * (12 + 11 * len(variables))
00157         for index in range (size):
00158             firstOnly = firstOnlyColl[index]
00159             index = getDictFromObject (firstOnly, indexSingleDict)
00160             print '  ', format (index, 'index', 3, firstOnly = True),
00161             info = getDictFromObject (firstOnly, infoSingleDict)
00162             for var in variables:
00163                 print '  ', format (info, var, firstOnly = True),
00164             print
00165         print
00166         # second only
00167         secondOnlyColl = getPieceFromObject (tree, name + '.secondOnly')
00168         size = secondOnlyColl.size()
00169         if size:            
00170             print "Second Only:\n   index    ",
00171             for var in variables:
00172                 print "%-12s" % (' ' + var),
00173             print
00174             print '-' * (12 + 11 * len(variables))
00175         for index in range (size):
00176             secondOnly = secondOnlyColl[index]
00177             index = getDictFromObject (secondOnly, indexSingleDict)
00178             print '  ', format (index, 'index', 3, firstOnly = True),
00179             info = getDictFromObject (secondOnly, infoSingleDict)
00180             for var in variables:
00181                 print '  ', format (info, var, firstOnly = True),
00182             print
00183         print
00184         # both
00185         diffColl = getPieceFromObject (tree, name+'.diff')
00186         size = diffColl.size()
00187         if size:            
00188             print "Both:\n   index",
00189             for var in variables:
00190                 print "%-24s" % ('          ' + var),
00191             print
00192             print '-' * (16 + 23 * len(variables))
00193         for index in range (size):
00194             diff = diffColl[index]
00195             index = getDictFromObject (diff, indexDoubleDict)
00196             print '  ', format (index, 'index', 3),
00197             info = getDictFromObject (diff, infoDoubleDict)
00198             for var in variables:
00199                 print '  ', format (info, var),
00200             print
00201         print
00202