Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-06-16 03:20:21

0001 #! /usr/bin/env python3
0002 
0003 from __future__ import print_function
0004 if __name__ == "__main__":
0005     import optparse
0006     parser = optparse.OptionParser("usage: %prog [options] config.txt file1.root [file2.root]\nVisit https://twiki.cern.ch/twiki/bin/view/CMS/SWGuidePhysicsToolsEdmOneToOneComparison\nfor full documentation.")
0007     modeGroup    = optparse.OptionGroup (parser, "Mode Conrols")
0008     tupleGroup   = optparse.OptionGroup (parser, "Tuple Controls")
0009     optionsGroup = optparse.OptionGroup (parser, "Options") 
0010     # mode group
0011     modeGroup.add_option ('--compare', dest='compare', action='store_true',
0012                           help='Compare tuple1 to tuple2')
0013     modeGroup.add_option ('--saveAs', dest='saveAs', type='string',
0014                           help='Save tuple1 as GO Root file')    
0015     modeGroup.add_option ('--printTuple', dest='printTuple',
0016                           action='store_true',
0017                           help='Print out all events in tuple1')
0018     modeGroup.add_option ('--interactive', dest='interactive',
0019                           action='store_true',
0020                           help='Loads files and prepares "event" '
0021                           'for interactive mode')
0022     # tuple group
0023     tupleGroup.add_option ('--tuple', dest='tuple', type='string',
0024                            default='',
0025                            help="Tuple type of 1st and 2nd tuple")
0026     tupleGroup.add_option ('--tuple1', dest='tuple1', type='string',
0027                            default='reco',
0028                            help="Tuple type of 1st tuple")
0029     tupleGroup.add_option ('--tuple2', dest='tuple2', type='string',
0030                            default='reco',
0031                            help="Tuple type of 2nd tuple")
0032     tupleGroup.add_option ('--file', dest='file', type='string',
0033                            default="",
0034                            help="1st and 2nd tuple file (debugging only)")
0035     tupleGroup.add_option ('--file1', dest='file1', type='string',
0036                            default="",
0037                            help="1st tuple file")
0038     tupleGroup.add_option ('--file2', dest='file2', type='string',
0039                            default="",
0040                            help="2nd tuple file")
0041     tupleGroup.add_option ('--numEvents', dest='numEvents', type='int',
0042                            default=0,
0043                            help="number of events for first and second file")
0044     tupleGroup.add_option ('--numEvents1', dest='numEvents1', type='int',
0045                            default=0,
0046                            help="number of events for first file")
0047     tupleGroup.add_option ('--numEvents2', dest='numEvents2', type='int',
0048                            default=0,
0049                            help="number of events for second file")
0050     tupleGroup.add_option ('--alias', dest='alias', type='string',
0051                            action='append',
0052                            help="Change alias ('tuple:object:alias')")
0053     tupleGroup.add_option ('--label', dest='label', type='string',
0054                            action='append',
0055                            help="Change label ('tuple^object^label')")
0056     tupleGroup.add_option ('--changeVariable', dest='changeVar', type='string',
0057                            action='append',
0058                            help="Change variable filling "
0059                            "('tuple:objName:varName:def')")
0060     # options group
0061     optionsGroup.add_option ('--config', dest='config', type='string',
0062                              default='config.txt',
0063                              help="Configuration file (default: '%default')")
0064     optionsGroup.add_option ('--printEvent', dest='printEvent',
0065                              action='store_true',
0066                              help='Prints loaded event to screen')
0067     optionsGroup.add_option ('--printGlobal', dest='printGlobal',
0068                              action='store_true',
0069                              help='Prints out global information' +
0070                              ' (for development)')
0071     optionsGroup.add_option ('--blur1', dest='blur', type='float',
0072                              default=0.,
0073                              help="Randomly changes values by 'BLUR'  " +\
0074                              "from tuple1.  For debugging only.")
0075     optionsGroup.add_option ('--blurRate', dest='blurRate', type='float',
0076                              default=0.02,
0077                              help="Rate at which objects will be changed. " + \
0078                              "(%default default)")
0079     optionsGroup.add_option ('--compRoot', dest='compRoot', type='string',
0080                              default='',
0081                              help="Write out root file for file comparisons")
0082     optionsGroup.add_option ('--debug', dest='debug', action='store_true',
0083                              help="Print debugging information")
0084     optionsGroup.add_option ('--strictPairing', dest='strictPairing',
0085                              action='store_true',
0086                              help="Objects are paired uniquely by order in collection")
0087     optionsGroup.add_option ('--relative', dest='relative',
0088                              action='store_true', default=True,
0089                              help='Precision is checked against relative difference')
0090     optionsGroup.add_option ('--absolute', dest='relative',
0091                              action='store_false',
0092                              help='Precision is checked against absolute difference')
0093     optionsGroup.add_option 
0094     parser.add_option_group (modeGroup)
0095     parser.add_option_group (tupleGroup)
0096     parser.add_option_group (optionsGroup)
0097     (options, args) = parser.parse_args()
0098     from Validation.Tools.GenObject import *
0099     ROOT.gROOT.SetBatch()
0100 
0101     lenArgs = len (args)
0102     if lenArgs >= 1:
0103         options.config = args[0]
0104     if lenArgs >= 2:
0105         options.file1  = args[1]
0106     if lenArgs == 3:
0107         options.file2  = args[2]
0108     if lenArgs > 3:
0109         raise RuntimeError("Too many arguments")
0110     
0111     # Here we go
0112     random.seed( os.getpid() )
0113     GenObject.loadConfigFile (options.config)
0114     ROOT.gSystem.Load("libFWCoreFWLite.so")
0115     ROOT.FWLiteEnabler.enable()
0116     # Let's parse any args
0117     doubleColonRE = re.compile (r'(.+):(.+):(.+)')
0118     if options.alias:
0119         for arg in options.alias:
0120             aliasMatch = doubleColonRE.match (arg)
0121             if aliasMatch:
0122                 print("aM", aliasMatch)
0123                 GenObject.changeAlias (aliasMatch.group (1),
0124                                        aliasMatch.group (2),
0125                                        aliasMatch.group (3))
0126                 continue
0127             # if we're here, then we have an argument that we don't understand
0128             raise RuntimeError("Unknown alias format '%s'" % arg)
0129     tripleColonRE = re.compile (r'(.+):(.+):(.+):(.+)')
0130     if options.changeVar:
0131         for arg in options.changeVar:
0132             changeMatch = tripleColonRE.match (arg)
0133             if changeMatch:
0134                 GenObject.changeVariable (changeMatch.group (1),
0135                                           changeMatch.group (2),
0136                                          changeMatch.group (3),
0137                                           changeMatch.group (4))
0138                 continue
0139             # if we're here, then we have an argument that we don't understand
0140             raise RuntimeError("Unknown changeVar format '%s'" % arg)
0141     if options.label:
0142         for label in options.label:            
0143             pieces = label.split('^')
0144             if len (pieces) != 3:
0145                 raise RuntimeError("Can't process label command '%s'" \
0146                       % options.label)
0147             GenObject.changeLabel (*pieces)
0148     # We don't want to use options beyond the main code, so let the
0149     # kitchen sink know what we want
0150     GenObject.setGlobalFlag ('printEvent',    options.printEvent)
0151     GenObject.setGlobalFlag ('debug',         options.debug)
0152     GenObject.setGlobalFlag ('relative',      options.relative)
0153     GenObject.setGlobalFlag ('strictPairing', options.strictPairing)
0154     if options.blur:
0155         GenObject.setGlobalFlag ('blur',      options.blur)
0156         GenObject.setGlobalFlag ('blurRate',  options.blurRate)
0157     # take care of any 'double' options now
0158     if options.tuple:
0159         options.tuple1 = options.tuple2 = options.tuple
0160     if options.file:
0161         options.file1 = options.file2 = options.file
0162     if options.numEvents:
0163         options.numEvents1 = options.numEvents2 = options.numEvents
0164     if options.compare:
0165         # Compare two files
0166         chain1 = GenObject.prepareTuple (options.tuple1, options.file1,
0167                                          options.numEvents1)
0168         chain2 = GenObject.prepareTuple (options.tuple2, options.file2,
0169                                          options.numEvents2)
0170         problems = \
0171                  GenObject.compareTwoTrees (chain1, chain2,
0172                                             diffOutputName = options.compRoot)
0173         print("Summary")
0174         pprint.pprint (problems)
0175     if options.saveAs:
0176         chain1 = GenObject.prepareTuple (options.tuple1, options.file1,
0177                                          options.numEvents1)
0178         GenObject.saveTupleAs (chain1, options.saveAs)
0179     if options.printTuple:
0180         print("printing tuple")
0181         GenObject.setGlobalFlag ('printEvent', True)
0182         chain1 = GenObject.prepareTuple (options.tuple1, options.file1,
0183                                          options.numEvents1)
0184         GenObject.printTuple (chain1)
0185         #GenObject.saveTupleAs (chain1, options.saveAs)
0186     if options.printGlobal:
0187         GenObject.printGlobal()
0188     if options.interactive:
0189         chain1 = chain2 = 0
0190         if len (options.file1):
0191             chain1 = GenObject.prepareTuple (options.tuple1, options.file1,
0192                                              options.numEvents1)
0193         if len (options.file2):
0194             chain2 = GenObject.prepareTuple (options.tuple2, options.file2)
0195         #############################################
0196         ## Load and save command line history when ##
0197         ## running interactively.                  ##
0198         #############################################
0199         import os, readline
0200         import atexit
0201         historyPath = os.path.expanduser("~/.pyhistory")
0202 
0203         def save_history (historyPath=historyPath):
0204             import readline
0205             readline.write_history_file(historyPath)
0206             if os.path.exists(historyPath):
0207                 readline.read_history_file(historyPath)
0208 
0209         atexit.register(save_history)
0210         readline.parse_and_bind("set show-all-if-ambiguous on")
0211         readline.parse_and_bind("tab: complete")
0212         if os.path.exists (historyPath) :
0213             readline.read_history_file(historyPath)
0214             readline.set_history_length(-1)
0215