diff --git a/DataFormats/FWLite/python/__init__.py b/DataFormats/FWLite/python/__init__.py index 048123ae959db..f09c1a032435d 100644 --- a/DataFormats/FWLite/python/__init__.py +++ b/DataFormats/FWLite/python/__init__.py @@ -1,10 +1,8 @@ #! /usr/bin/env python -from __future__ import print_function import ROOT import inspect import sys -import optparse from FWCore.ParameterSet.VarParsing import VarParsing from builtins import int diff --git a/DataFormats/FWLite/scripts/edmLumisInFiles.py b/DataFormats/FWLite/scripts/edmLumisInFiles.py index 1e537dbadbf66..1d779b41eee53 100755 --- a/DataFormats/FWLite/scripts/edmLumisInFiles.py +++ b/DataFormats/FWLite/scripts/edmLumisInFiles.py @@ -1,24 +1,19 @@ #! /usr/bin/env python3 -from __future__ import print_function -from FWCore.PythonUtilities.LumiList import LumiList -import optparse - +from FWCore.PythonUtilities.LumiList import LumiList +from argparse import ArgumentParser if __name__ == '__main__': - - parser = optparse.OptionParser ("Usage: %prog [--options] edm1.root [edm2.root...]", - description='Runs over input EDM files and prints out a list of contained lumi sections') - parser.add_option ('--intLumi', dest='intLumi', action='store_true', - help='print out total recorded and delivered integrated luminosity') - parser.add_option ('--output', dest='output', type='string', - help='save lumi sections output to file OUTPUT') - (options, args) = parser.parse_args() + parser = ArgumentParser(description='Runs over input EDM files and prints out a list of contained lumi sections') + parser.add_argument('--intLumi', dest='intLumi', action='store_true', + help='print out total recorded and delivered integrated luminosity') + parser.add_argument('--output', dest='output', type=str, + help='save lumi sections output to file OUTPUT') + parser.add_argument("edm", metavar="edm.root", type=str, nargs='+') + options = parser.parse_args() # put this here after parsing the arguments since ROOT likes to # grab command line arguments even when it shouldn't. from DataFormats.FWLite import Lumis, Handle - if not args: - raise RuntimeError("Must provide at least one input file") # do we want to get the luminosity summary? if options.intLumi: @@ -28,7 +23,7 @@ handle, lable = None, None runsLumisDict = {} - lumis = Lumis (args) + lumis = Lumis (options.edm) delivered = recorded = 0 for lum in lumis: runList = runsLumisDict.setdefault (lum.aux().run(), []) diff --git a/DataFormats/FWLite/test/chainEvent_python.py b/DataFormats/FWLite/test/chainEvent_python.py index febb7eb48fc00..81337732ceb33 100644 --- a/DataFormats/FWLite/test/chainEvent_python.py +++ b/DataFormats/FWLite/test/chainEvent_python.py @@ -3,7 +3,6 @@ from __future__ import print_function from builtins import range from DataFormats.FWLite import Events, Handle -import optparse print("starting python test") diff --git a/PhysicsTools/FWLite/scripts/newFWLiteAna.py b/PhysicsTools/FWLite/scripts/newFWLiteAna.py index b8a126a37f244..3612b46d46a90 100755 --- a/PhysicsTools/FWLite/scripts/newFWLiteAna.py +++ b/PhysicsTools/FWLite/scripts/newFWLiteAna.py @@ -1,7 +1,7 @@ #! /usr/bin/env python3 from __future__ import print_function -import optparse +from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter import os import sys import re @@ -95,17 +95,16 @@ def addBuildPiece (targetBuild, buildPiece): print("Error: You must have already setup a CMSSW release. Aborting.") sys.exit() # setup the options - parser = optparse.OptionParser('usage: %prog [options] ' - 'Package/SubPackage/name\n' - 'Creates new analysis code') - parser.add_option ('--copy', dest='copy', type='string', default = 'blank', - help='Copies example. COPY should either be a file' - ' _or_ an example in PhysicsTools/FWLite/examples') - parser.add_option ('--newPackage', dest='newPackage', action='store_true', - help='Will create Package/Subpackage folders if necessary') - parser.add_option ('--toTest', dest='toTest', action='store_true', - help='Will create files in test/ instead of bin/') - (options, args) = parser.parse_args() + parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter, description='Creates new analysis code') + parser.add_argument('--copy', dest='copy', type=str, default = 'blank', + help='Copies example. COPY should either be a file' + ' _or_ an example in PhysicsTools/FWLite/examples') + parser.add_argument('--newPackage', dest='newPackage', action='store_true', + help='Will create Package/Subpackage folders if necessary') + parser.add_argument('--toTest', dest='toTest', action='store_true', + help='Will create files in test/ instead of bin/') + parser.add_argument("name", metavar="Package/SubPackage/name", type=str) + options = parser.parse_args() # get the name of the copy file and make sure we can find everything copy = options.copy if not re.search ('\.cc$', copy): @@ -130,7 +129,7 @@ def addBuildPiece (targetBuild, buildPiece): found = True # Is there a Buildfile too? if not os.path.exists (build): - print("Error: Found '%s', but no accompying " % name, \ + print("Error: Found '%s', but no accompanying " % name, \ "Buildfile '%s'. Aborting" % build) sys.exit() fullName = name @@ -139,10 +138,7 @@ def addBuildPiece (targetBuild, buildPiece): if not found: print("Error: Did not find '%s' to copy. Aborting." % copy) sys.exit() - if len (args) != 1: - parser.print_usage() - sys.exit() - pieces = args[0].split('/') + pieces = options.name.split('/') if len (pieces) != 3: print("Error: Need to provide 'Package/SubPackage/name'") sys.exit() diff --git a/PhysicsTools/NanoAOD/test/compare_branches.py b/PhysicsTools/NanoAOD/test/compare_branches.py index 4a5b2770fdb26..c5976e4407207 100755 --- a/PhysicsTools/NanoAOD/test/compare_branches.py +++ b/PhysicsTools/NanoAOD/test/compare_branches.py @@ -1,15 +1,16 @@ #!/usr/bin/env python3 from ROOT import TFile, TTree,TCut, gROOT,TH1F, TCanvas import glob, re -from optparse import OptionParser -parser = OptionParser(usage="%prog [options] job1 job2 ...") -parser.add_option("--base", dest="base", default="step2.root", help="name of root file") -parser.add_option("--ref", dest="ref", default="ref/", help="path to the reference files") -parser.add_option("--png", dest="png", default="./", help="path to the plots") -parser.add_option("--onlydiff", dest="diff", default=False, action="store_true", help="print only the histograms with differences") -parser.add_option("--selection", dest="selection", default="", help="a selection of events to draw") -parser.add_option("--branch", dest="branch", default=".*", help="a regexp for selecting branches") -(options, args) = parser.parse_args() +from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter +parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter) +parser.add_argument("--base", dest="base", default="step2.root", help="name of root file") +parser.add_argument("--ref", dest="ref", default="ref/", help="path to the reference files") +parser.add_argument("--png", dest="png", default="./", help="path to the plots") +parser.add_argument("--onlydiff", dest="diff", default=False, action="store_true", help="print only the histograms with differences") +parser.add_argument("--selection", dest="selection", default="", help="a selection of events to draw") +parser.add_argument("--branch", dest="branch", default=".*", help="a regexp for selecting branches") +parser.add_argument("job", type=str, nargs='+') +options = parser.parse_args() def drawInOrder(t1,h1n, t2,h2n): N1 = t1.Draw(f'{vname} >> {h1n}', options.selection, '') @@ -33,7 +34,7 @@ def binContents( h ): def emptyHist( h ): return all([ b==0 for b in binContents(h) ]) -for job in args: +for job in options.job: fconn = glob.glob(f'{job}*/{options.base}')[0] fcon = TFile.Open(fconn) con = fcon.Get('Events') diff --git a/PhysicsTools/NanoAOD/test/compare_sizes_json.py b/PhysicsTools/NanoAOD/test/compare_sizes_json.py index e4feedfd7873e..5c661ac095f8b 100755 --- a/PhysicsTools/NanoAOD/test/compare_sizes_json.py +++ b/PhysicsTools/NanoAOD/test/compare_sizes_json.py @@ -1,13 +1,14 @@ #!/usr/bin/env python3 import json -from optparse import OptionParser -parser = OptionParser(usage="%prog [options] job1 job2 ...") -parser.add_option("-f", "--format", dest="fmt", default="txt", help="output format") -parser.add_option("-H", "--header", dest="header", action="store_true", default=False, help="print headers") -parser.add_option("--base", dest="base", default="{}.json,{}_timing_report.json", help="coma separated base name for size and timing files") -parser.add_option("--ref", dest="ref", default="ref/", help="path to the reference files") -(options, args) = parser.parse_args() +from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter +parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter) +parser.add_argument("-f", "--format", dest="fmt", default="txt", help="output format") +parser.add_argument("-H", "--header", dest="header", action="store_true", default=False, help="print headers") +parser.add_argument("--base", dest="base", default="{}.json,{}_timing_report.json", help="comma-separated base name for size and timing files") +parser.add_argument("--ref", dest="ref", default="ref/", help="path to the reference files") +parser.add_argument("job", type=str, nargs='+') +options = parser.parse_args() headers = [ 'Sample' , 'kb/ev' , 'ref kb/ev' , 'diff kb/ev' , 'ev/s/thd' , 'ref ev/s/thd' , 'diff rate' , 'mem/thd' , 'ref mem/thd' ] start, sep, end = "", "\t", "" @@ -20,7 +21,7 @@ def prow(x): first = True size_pattern,timing_pattern = options.base.split(',') -for job in args: +for job in options.job: label = job size_json=size_pattern.format(job) diff --git a/PhysicsTools/NanoAOD/test/get_timing_from_jobreport.py b/PhysicsTools/NanoAOD/test/get_timing_from_jobreport.py index 5df27e127e6db..d85dc2b767184 100755 --- a/PhysicsTools/NanoAOD/test/get_timing_from_jobreport.py +++ b/PhysicsTools/NanoAOD/test/get_timing_from_jobreport.py @@ -4,25 +4,26 @@ import json import os -from optparse import OptionParser -parser = OptionParser(usage="%prog input.xml output.json") -(options, args) = parser.parse_args() -if len(args)!=2: raise RuntimeError +from argparse import ArgumentParser +parser = ArgumentParser() +parser.add_argument("input", metavar="input.xml", type=str) +parser.add_argument("output", metavar="output.json", type=str) +options = parser.parse_args() timing={} try: - tree = xml.etree.ElementTree.parse(args[0]) + tree = xml.etree.ElementTree.parse(options.input) timing['NumberEvents']=int(tree.find("./PerformanceReport/PerformanceSummary[@Metric='ProcessingSummary']/Metric[@Name='NumberEvents']").get('Value')) for x in tree.findall("./PerformanceReport/PerformanceSummary[@Metric='Timing']/Metric"): timing['Timing/'+x.get('Name')]=float(x.get('Value')) for x in tree.findall("./PerformanceReport/PerformanceSummary[@Metric='ApplicationMemory']/Metric"): timing['ApplicationMemory/'+x.get('Name')]=float(x.get('Value')) except Exception as e: - print("Could not parse job report %s, content is:" % args[0]) - os.system("cat %s" % args[0]) + print("Could not parse job report %s, content is:" % options.input) + os.system("cat %s" % options.input) raise e -with open(args[1],'w') as f: +with open(options.output,'w') as f: json.dump(timing,f) diff --git a/PhysicsTools/NanoAOD/test/inspectNanoFile.py b/PhysicsTools/NanoAOD/test/inspectNanoFile.py index d05154a5aab72..7bbd859e49117 100755 --- a/PhysicsTools/NanoAOD/test/inspectNanoFile.py +++ b/PhysicsTools/NanoAOD/test/inspectNanoFile.py @@ -429,21 +429,21 @@ def _maybeOpen(filename): return open(filename, 'w') if filename != "-" else sys.stdout if __name__ == '__main__': - from optparse import OptionParser - parser = OptionParser(usage="%prog [options] inputFile") - parser.add_option("-j", "--json", dest="json", type="string", default=None, help="Write out json file") - parser.add_option("-d", "--doc", dest="doc", type="string", default=None, help="Write out html doc") - parser.add_option("-s", "--size", dest="size", type="string", default=None, help="Write out html size report") - parser.add_option("--docmd", dest="docmd", type="string", default=None, help="Write out markdown doc") - parser.add_option("--sizemd", dest="sizemd", type="string", default=None, help="Write out markdown size report") - (options, args) = parser.parse_args() - if len(args) != 1: raise RuntimeError("Please specify one input file") + from argparse import ArgumentParser + parser = ArgumentParser() + parser.add_argument("-j", "--json", dest="json", type=str, default=None, help="Write out json file") + parser.add_argument("-d", "--doc", dest="doc", type=str, default=None, help="Write out html doc") + parser.add_argument("-s", "--size", dest="size", type=str, default=None, help="Write out html size report") + parser.add_argument("--docmd", dest="docmd", type=str, default=None, help="Write out markdown doc") + parser.add_argument("--sizemd", dest="sizemd", type=str, default=None, help="Write out markdown size report") + parser.add_argument("inputFile", type=str) + options = parser.parse_args() - if args[0].endswith(".root"): - filedata = FileData(inspectRootFile(args[0])) - elif args[0].endswith(".json"): - filedata = FileData(json.load(open(args[0],'r'))) - else: raise RuntimeError("Input file %s is not a root or json file" % args[0]) + if options.inputFile.endswith(".root"): + filedata = FileData(inspectRootFile(options.inputFile)) + elif options.inputFile.endswith(".json"): + filedata = FileData(json.load(open(options.inputFile,'r'))) + else: raise RuntimeError("Input file %s is not a root or json file" % options.inputFile) if options.json: json.dump(filedata._json, _maybeOpen(options.json), indent=4) diff --git a/PhysicsTools/NanoAODTools/scripts/nano_postproc.py b/PhysicsTools/NanoAODTools/scripts/nano_postproc.py index e788c10ab1930..9b24293d8db68 100755 --- a/PhysicsTools/NanoAODTools/scripts/nano_postproc.py +++ b/PhysicsTools/NanoAODTools/scripts/nano_postproc.py @@ -7,54 +7,49 @@ ROOT.PyConfig.IgnoreCommandLineOptions = True if __name__ == "__main__": - from optparse import OptionParser - parser = OptionParser(usage="%prog [options] outputDir inputFiles") - parser.add_option("-s", "--postfix", dest="postfix", type="string", default=None, - help="Postfix which will be appended to the file name (default: _Friend for friends, _Skim for skims)") - parser.add_option("-J", "--json", dest="json", type="string", - default=None, help="Select events using this JSON file") - parser.add_option("-c", "--cut", dest="cut", type="string", - default=None, help="Cut string") - parser.add_option("-b", "--branch-selection", dest="branchsel", - type="string", default=None, help="Branch selection") - parser.add_option("--bi", "--branch-selection-input", dest="branchsel_in", - type="string", default=None, help="Branch selection input") - parser.add_option("--bo", "--branch-selection-output", dest="branchsel_out", - type="string", default=None, help="Branch selection output") - parser.add_option("--friend", dest="friend", action="store_true", default=False, - help="Produce friend trees in output (current default is to produce full trees)") - parser.add_option("--full", dest="friend", action="store_false", default=False, - help="Produce full trees in output (this is the current default)") - parser.add_option("--noout", dest="noOut", action="store_true", - default=False, help="Do not produce output, just run modules") - parser.add_option("-P", "--prefetch", dest="prefetch", action="store_true", default=False, - help="Prefetch input files locally instead of accessing them via xrootd") - parser.add_option("--long-term-cache", dest="longTermCache", action="store_true", default=False, - help="Keep prefetched files across runs instead of deleting them at the end") - parser.add_option("-N", "--max-entries", dest="maxEntries", type="long", default=None, - help="Maximum number of entries to process from any single given input tree") - parser.add_option("--first-entry", dest="firstEntry", type="long", default=0, - help="First entry to process in the three (to be used together with --max-entries)") - parser.add_option("--justcount", dest="justcount", default=False, - action="store_true", help="Just report the number of selected events") - parser.add_option("-I", "--import", dest="imports", type="string", default=[], action="append", - nargs=2, help="Import modules (python package, comma-separated list of ") - parser.add_option("-z", "--compression", dest="compression", type="string", - default=("LZMA:9"), help="Compression: none, or (algo):(level) ") - - (options, args) = parser.parse_args() + from argparse import ArgumentParser + parser = ArgumentParser() + parser.add_argument("-s", "--postfix", dest="postfix", type=str, default=None, + help="Postfix which will be appended to the file name (default: _Friend for friends, _Skim for skims)") + parser.add_argument("-J", "--json", dest="json", type=str, + default=None, help="Select events using this JSON file") + parser.add_argument("-c", "--cut", dest="cut", type=str, + default=None, help="Cut string") + parser.add_argument("-b", "--branch-selection", dest="branchsel", + type=str, default=None, help="Branch selection") + parser.add_argument("--bi", "--branch-selection-input", dest="branchsel_in", + type=str, default=None, help="Branch selection input") + parser.add_argument("--bo", "--branch-selection-output", dest="branchsel_out", + type=str, default=None, help="Branch selection output") + parser.add_argument("--friend", dest="friend", action="store_true", default=False, + help="Produce friend trees in output (current default is to produce full trees)") + parser.add_argument("--full", dest="friend", action="store_false", default=False, + help="Produce full trees in output (this is the current default)") + parser.add_argument("--noout", dest="noOut", action="store_true", + default=False, help="Do not produce output, just run modules") + parser.add_argument("-P", "--prefetch", dest="prefetch", action="store_true", default=False, + help="Prefetch input files locally instead of accessing them via xrootd") + parser.add_argument("--long-term-cache", dest="longTermCache", action="store_true", default=False, + help="Keep prefetched files across runs instead of deleting them at the end") + parser.add_argument("-N", "--max-entries", dest="maxEntries", type=int, default=None, + help="Maximum number of entries to process from any single given input tree") + parser.add_argument("--first-entry", dest="firstEntry", type=int, default=0, + help="First entry to process in the three (to be used together with --max-entries)") + parser.add_argument("--justcount", dest="justcount", default=False, + action="store_true", help="Just report the number of selected events") + parser.add_argument("-I", "--import", dest="imports", type=str, default=[], action="append", + nargs=2, help="Import modules (python package, comma-separated list of ") + parser.add_argument("-z", "--compression", dest="compression", type=str, + default=("LZMA:9"), help="Compression: none, or (algo):(level) ") + parser.add_argument("outputDir", type=str) + parser.add_argument("inputFile", type=str, nargs='+') + options = parser.parse_args() if options.friend: if options.cut or options.json: raise RuntimeError( "Can't apply JSON or cut selection when producing friends") - if len(args) < 2: - parser.print_help() - sys.exit(1) - outdir = args[0] - args = args[1:] - modules = [] for mod, names in options.imports: import_module(mod) @@ -76,7 +71,7 @@ if options.branchsel != None: options.branchsel_in = options.branchsel options.branchsel_out = options.branchsel - p = PostProcessor(outdir, args, + p = PostProcessor(options.outputDir, options.inputFile, cut=options.cut, branchsel=options.branchsel_in, modules=modules, diff --git a/PhysicsTools/PythonAnalysis/python/diffProv.py b/PhysicsTools/PythonAnalysis/python/diffProv.py index 91487aa23b4a9..582544bc2dec6 100644 --- a/PhysicsTools/PythonAnalysis/python/diffProv.py +++ b/PhysicsTools/PythonAnalysis/python/diffProv.py @@ -4,7 +4,7 @@ class difference : def __init__(self,v): - self.verbose = v + self.verbose = int(v) self._diffprocess=[] self._sameprocess=() def list_diff(self,aList1, aList2, string1, string2): @@ -14,7 +14,7 @@ def list_diff(self,aList1, aList2, string1, string2): for j in range(2,len(aList2)): if (i==j) and (aList1[i]!=aList2[j]): if aList1[i][:(aList1[i].index('=')+1)] == aList2[j][:(aList2[j].index('=')+1)]: - if self.verbose==str(2) or self.verbose==str(1): + if self.verbose==2 or self.verbose==1: str1 = aList1[i][2:aList1[i].index('=')+1] + aList1[i][aList1[i].index('=')+1:]+' ['+ string1+']' str2 = len(aList1[i][2:aList1[i].index('=')+1])*' '+aList2[j][aList2[j].index('=')+1:]+' ['+string2+']' print(str1,'\n',str2,'\n') @@ -80,7 +80,7 @@ def onefilemodules(self,module1,module2,string): for name, value in module1[i] : if (name not in labelList2): print('Process: '+'"'+i+'"'+'\n'+'Module: '+'"'+name+'"') - if self.verbose==str(2): + if self.verbose==2: for k in value[1:]: print(k) diff --git a/PhysicsTools/PythonAnalysis/python/diff_provenance.py b/PhysicsTools/PythonAnalysis/python/diff_provenance.py deleted file mode 100644 index 0a99eec684c2f..0000000000000 --- a/PhysicsTools/PythonAnalysis/python/diff_provenance.py +++ /dev/null @@ -1,53 +0,0 @@ -from __future__ import print_function -from builtins import range -class difference : - - def __init__(self,v): - self.verbose = v - - def list_diff(self,aList1, aList2, string1, string2): - "Searches for differences between two modules of the same kind" - for i in range(2,len(aList1)): - for j in range(2,len(aList2)): - if (i==j) and (aList1[i]!=aList2[j]): - if aList1[i][:(aList1[i].index('=')+1)] == aList2[j][:(aList2[j].index('=')+1)]: - if self.verbose==str(2) or self.verbose==str(1): - print(aList1[i][2:aList1[i].index('=')+1] + aList1[i][aList1[i].index('=')+1:]+' ['+ string1+']') - print(len(aList1[i][2:aList1[i].index('=')+1])*' '+aList2[j][aList2[j].index('=')+1:]+' ['+string2+']') - print('') - - - def module_diff(self,module1,module2, string1, string2): - "Searches for modules which are in both the files but whose parameters are setted at different values" - modulesfile1=[] - modulesfile2=[] - print('\nList of modules present in both the files with different parameter values\n') - for i in module1.keys(): - for j in module2.keys(): - if (i==j) and (module1[i]!=module2[j]): - print('Module: '+'"'+i+'"') - d=difference(self.verbose) - d.module=i - d.firstvalue=module1[i] - d.secondvalue=module2[j] - self.list_diff(d.firstvalue,d.secondvalue, string1, string2) - else: pass - - self.onefilemodules(module1,module2,'first') - self.onefilemodules(module2,module1,'second') - - - def onefilemodules(self,module1,module2,string): - "Searches for modules present only in one of the two files" - onlyonefile=False - for i in module1.keys(): - if i not in module2: - if not onlyonefile: - print('\nModule present only in the '+string+ ' file:'+'\n') - onlyonefile = True - print('Module: '+'"'+i+'"') - if self.verbose==str(2): - for k in range(1,len(module1[i])): - print(module1[i][k]) - - diff --git a/PhysicsTools/PythonAnalysis/python/read_provenance.py b/PhysicsTools/PythonAnalysis/python/read_provenance.py deleted file mode 100644 index 35b33482c6d52..0000000000000 --- a/PhysicsTools/PythonAnalysis/python/read_provenance.py +++ /dev/null @@ -1,47 +0,0 @@ -class filereader: - - def __init__(self): - self.aList=['Module', 'ESSource'] - - def startswith(self,line): - "Checks if the first word of the line starts with any of the aList elements" - for item in self.aList: - if line.startswith(item): - return True - return False - - def readfile(self,nomefile): - "Reads the file line by line and searches for the begin and the end of each Module block" - aFile = open(nomefile) - module = [] - source = [] - file_modules = {} - insideModuleBlock = False - insideParameterBlock = False - for line in aFile.readlines(): - if self.startswith(line): - if insideParameterBlock: - file_modules[key]=module - insideParameterBlock = False - #print line[:-1] - module=[] - module.append(line[:-1]) - key=line[line.index(':')+2:-1] - #print key - insideModuleBlock = True - insideParameterBlock = False - elif (line.startswith(' parameters')) and insideModuleBlock: - insideParameterBlock = True - module.append(line[:-1]) - #print line[:-1] - elif line.startswith('ESModule') and insideParameterBlock: - file_modules[key]=module - insideParameterBlock = False - insideModuleBlock = False - elif (insideParameterBlock): - module.append(line[:-1]) - #print line[:-1] - - - return file_modules - diff --git a/PhysicsTools/PythonAnalysis/scripts/edmProvDiff b/PhysicsTools/PythonAnalysis/scripts/edmProvDiff index 94015cc85be57..c55b41027d9a0 100755 --- a/PhysicsTools/PythonAnalysis/scripts/edmProvDiff +++ b/PhysicsTools/PythonAnalysis/scripts/edmProvDiff @@ -9,20 +9,18 @@ # # author: Annapaola de Cosa -from optparse import OptionParser +from argparse import ArgumentParser import sys from subprocess import Popen, PIPE, STDOUT from PhysicsTools.PythonAnalysis.readProv import * from PhysicsTools.PythonAnalysis.diffProv import * - -usage = "usage: %prog filename1 filename2" -parser = OptionParser(usage=usage, version="%prog 0.1") -parser.add_option("-v", "--verbosity_level", dest="verbose", help="[0] to print short message [1], to print details about the differences of modules common to both files, [2] to print all the details about the differences between the two files") -(options, args) = parser.parse_args() - -if len(args) != 2: - parser.error('Please specify two EDM files to compare') +parser = ArgumentParser() +parser.add_argument('--version', action='version', version='%(prog)s 0.1') +parser.add_argument("-v", "--verbosity_level", dest="verbose", choices=(0,1,2), help="[0] to print short message [1], to print details about the differences of modules common to both files, [2] to print all the details about the differences between the two files") +parser.add_argument("filename1",type=str) +parser.add_argument("filename2",type=str) +options = parser.parse_args() def provenance(args): cmd="edmProvDump "+args @@ -37,15 +35,11 @@ def provenance(args): file.write(provenance) return s -try: - prov1=provenance(args[0]) - prov2=provenance(args[1]) -except IndexError: - print "Specify input file names\nType 'edmProvDiff -h' to visualize the arguments needed" + +prov1=provenance(options.filename1) +prov2=provenance(options.filename2) f=filereader() module1=f.readfile(prov1) module2=f.readfile(prov2) d=difference(options.verbose) -d.module_diff(module1,module2,args[0],args[1]) - - +d.module_diff(module1,module2,options.filename1,options.filename2) diff --git a/PhysicsTools/PythonAnalysis/test/test.py b/PhysicsTools/PythonAnalysis/test/test.py index f34e5ed4d761e..27e742816d541 100755 --- a/PhysicsTools/PythonAnalysis/test/test.py +++ b/PhysicsTools/PythonAnalysis/test/test.py @@ -10,7 +10,7 @@ class testEdmProvDiff(unittest.TestCase): def setUp(self): self._r=filereader() - self._d=difference(str(2)) + self._d=difference(2) def testStartswith(self): """ Check the method startswith() of class filereader diff --git a/PhysicsTools/TagAndProbe/python/cropTnPTrees.py b/PhysicsTools/TagAndProbe/python/cropTnPTrees.py index 1b7de44f2bf07..4cced842b2422 100644 --- a/PhysicsTools/TagAndProbe/python/cropTnPTrees.py +++ b/PhysicsTools/TagAndProbe/python/cropTnPTrees.py @@ -1,28 +1,19 @@ #!/usr/bin/env python -from __future__ import print_function import ROOT ROOT.gROOT.SetBatch(True) import sys -from optparse import OptionParser -parser = OptionParser(usage = "usage: %prog [options] inputFile fraction outputFile", - version = "%prog $Id:$") -(options, args) = parser.parse_args() +from argparse import ArgumentParser +parser = ArgumentParser() +parser.add_argument("inputFile", type=str) +parser.add_argument("fraction", type=float) +parser.add_argument("outputFile", type=str) +options = parser.parse_args() -if len(args) <= 2: - parser.print_usage() - sys.exit(2) - -try: - frac = float(args[1]) -except TypeError: - parser.print_usage() - print("fraction must be a floating point number (e.g. 0.5)") - sys.exit(2) - -input = ROOT.TFile(args[0]) -output = ROOT.TFile(args[2], "RECREATE") +frac = options.fraction +input = ROOT.TFile(options.inputFile) +output = ROOT.TFile(options.outputFile, "RECREATE") for k in input.GetListOfKeys(): print(k.GetName(), k.GetClassName()) if k.GetClassName() == "TDirectoryFile": diff --git a/PhysicsTools/Utilities/scripts/edmPickEvents.py b/PhysicsTools/Utilities/scripts/edmPickEvents.py index d5db7e111edd6..83653be0cd62f 100755 --- a/PhysicsTools/Utilities/scripts/edmPickEvents.py +++ b/PhysicsTools/Utilities/scripts/edmPickEvents.py @@ -6,13 +6,12 @@ # Volker Adler Apr 16, 2014 # Raman Khurana June 18, 2015 # Dinko Ferencek June 27, 2015 -from __future__ import print_function import os import sys -import optparse +from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter import re -from FWCore.PythonUtilities.LumiList import LumiList +from FWCore.PythonUtilities.LumiList import LumiList import json from pprint import pprint from datetime import datetime @@ -235,47 +234,41 @@ def setupCrabDict (options): if __name__ == "__main__": email = guessEmail() - parser = optparse.OptionParser ("Usage: %prog [options] dataset events_or_events.txt", description='''This program + parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter, description='''This program facilitates picking specific events from a data set. For full details, please visit -https://twiki.cern.ch/twiki/bin/view/CMSPublic/WorkBookPickEvents ''') - parser.add_option ('--output', dest='base', type='string', - default='pickevents', - help='Base name to use for output files (root, JSON, run and event list, etc.; default "%default")') - parser.add_option ('--runInteractive', dest='runInteractive', action='store_true', - help = 'Call "cmsRun" command if possible. Can take a long time.') - parser.add_option ('--printInteractive', dest='printInteractive', action='store_true', - help = 'Print "cmsRun" command instead of running it.') - parser.add_option ('--maxEventsInteractive', dest='maxEventsInteractive', type='int', - default=20, - help = 'Maximum number of events allowed to be processed interactively.') - parser.add_option ('--crab', dest='crab', action='store_true', - help = 'Force CRAB setup instead of interactive mode') - parser.add_option ('--crabCondor', dest='crabCondor', action='store_true', - help = 'Tell CRAB to use Condor scheduler (FNAL or OSG sites).') - parser.add_option ('--email', dest='email', type='string', - default='', - help="Specify email for CRAB (default '%s')" % email ) +https://twiki.cern.ch/twiki/bin/view/CMSPublic/WorkBookPickEvents''') + parser.add_argument('--output', dest='base', type=str, + default='pickevents', + help='Base name to use for output files (root, JSON, run and event list, etc.)")') + parser.add_argument('--runInteractive', dest='runInteractive', action='store_true', + help = 'Call "cmsRun" command if possible. Can take a long time.') + parser.add_argument('--printInteractive', dest='printInteractive', action='store_true', + help = 'Print "cmsRun" command instead of running it.') + parser.add_argument('--maxEventsInteractive', dest='maxEventsInteractive', type=int, + default=20, + help = 'Maximum number of events allowed to be processed interactively.') + parser.add_argument('--crab', dest='crab', action='store_true', + help = 'Force CRAB setup instead of interactive mode') + parser.add_argument('--crabCondor', dest='crabCondor', action='store_true', + help = 'Tell CRAB to use Condor scheduler (FNAL or OSG sites).') + parser.add_argument('--email', dest='email', type=str, + default=email, + help="Specify email for CRAB") das_cli = '' - parser.add_option ('--das-client', dest='das_cli', type='string', - default=das_cli, - help="Specify das client to use (default '%s')" % das_cli ) - (options, args) = parser.parse_args() - - - if len(args) < 2: - parser.print_help() - sys.exit(0) - - if not options.email: - options.email = email - - Event.dataset = args.pop(0) + parser.add_argument('--das-client', dest='das_cli', type=str, + default=das_cli, + help="Specify das client to use") + parser.add_argument("dataset", type=str) + parser.add_argument("events", metavar="events_or_events.txt", type=str, nargs='+') + options = parser.parse_args() + + Event.dataset = options.dataset commentRE = re.compile (r'#.+$') colonRE = re.compile (r':') eventList = [] - if len (args) > 1 or colonRE.search (args[0]): + if len (options.events) > 1 or colonRE.search (options.events[0]): # events are coming in from the command line - for piece in args: + for piece in options.events: try: event = Event (piece) except: @@ -283,7 +276,7 @@ def setupCrabDict (options): eventList.append (event) else: # read events from file - source = open(args[0], 'r') + source = open(options.events[0], 'r') for line in source: line = commentRE.sub ('', line) try: diff --git a/PhysicsTools/Utilities/scripts/pileupDistInMC.py b/PhysicsTools/Utilities/scripts/pileupDistInMC.py index a90f6c2b2cb5a..6c479dd01d4dd 100755 --- a/PhysicsTools/Utilities/scripts/pileupDistInMC.py +++ b/PhysicsTools/Utilities/scripts/pileupDistInMC.py @@ -1,30 +1,29 @@ #! /usr/bin/env python3 -from __future__ import print_function -import optparse +from argparse import ArgumentParser import re from pprint import pprint commentRE = re.compile (r'#.*$') if __name__ == "__main__": - parser = optparse.OptionParser ("Usage: %prog file1.root [file2.root...]") - parser.add_option ('--loadFromFile', dest='loadFromFile', default=[], - type='string', - action='append', - help="Name of text file containing filenames" ) - parser.add_option ('--prefix', dest='prefix', type='string', - default='', - help="Prefix to add to files" ) - - parser.add_option ('--bx', dest='bx', type='int', - default='0', - help="Bunch crossing to check (0 = in-time)" ) - (options, args) = parser.parse_args() - import ROOT # stupid ROOT takes the arugments error + parser = ArgumentParser() + parser.add_argument('--loadFromFile', dest='loadFromFile', default=[], + type=str, + action='append', + help="Name of text file containing filenames" ) + parser.add_argument('--prefix', dest='prefix', type=str, + default='', + help="Prefix to add to files" ) + parser.add_argument('--bx', dest='bx', type=int, + default='0', + help="Bunch crossing to check (0 = in-time)" ) + parser.add_argument("file", metavar="file.root", type=str, nargs='*') + options = parser.parse_args() + import ROOT # stupid ROOT takes the arguments error from DataFormats.FWLite import Events, Handle - listOfFiles = args[:] + listOfFiles = options.file for filename in options.loadFromFile: source = open (filename, 'r') for line in source: