Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:03:35

0001 #!/usr/bin/env python3
0002 
0003 from __future__ import print_function
0004 import sys
0005 import optparse
0006 from FWCore.PythonUtilities.LumiList import LumiList
0007 
0008 
0009 if __name__ == '__main__':
0010     
0011     parser = optparse.OptionParser ("Usage: %prog --command [--options] alpha.json beta.json [output.json]")
0012     # required parameters
0013     cmdGroup = optparse.OptionGroup (parser, "Command Options ")
0014     cmdGroup.add_option ('--and', dest='command', action='store_const',
0015                          const='and', 
0016                          help = '"and" (i.e., take intersection) of two files')
0017     cmdGroup.add_option ('--or', dest='command', action='store_const',
0018                          const='or',
0019                          help = '"or" (i.e., take union) of two files')
0020     cmdGroup.add_option ('--sub', dest='command', action='store_const',
0021                          const='sub',
0022                          help = '"subtraction" (i.e., lumi sections in alpha not in beta) of two files')
0023     cmdGroup.add_option ('--diff', dest='command', action='store_const',
0024                          const='diff',
0025                          help = '"All differences (i.e., alpha - beta AND beta - alpha) of two files. Output will only be to screen (not proper JSON format).')
0026     parser.add_option_group (cmdGroup)
0027     (options, args) = parser.parse_args()
0028     if len (args) < 2 or len (args) > 3:
0029         raise RuntimeError("Two input filenames with one optional output filename must be provided.")
0030     if not options.command:
0031         raise RuntimeError("Exactly one command option must be specified")
0032 
0033     alphaList = LumiList (filename = args[0])  # Read in first  JSON file
0034     betaList  = LumiList (filename = args[1])  # Read in second JSON file
0035 
0036     ##################
0037     ## Diff Command ##
0038     ##################
0039     if options.command == 'diff':
0040         if len (args) >= 3:
0041             raise RuntimeError("Can not output to file with '--diff' option.  The output is not standard JSON.")
0042         firstOnly  = alphaList - betaList
0043         secondOnly = betaList  - alphaList
0044         if not firstOnly and not secondOnly:
0045             print("Files '%s' and '%s' are the same." % (args[0], args[1]))
0046             sys.exit()
0047         print("'%s'-only lumis:" % args[0])
0048         if firstOnly:
0049             print(firstOnly)
0050         else:
0051             print("None")
0052         print("\n'%s'-only lumis:" % args[1])
0053         if secondOnly:
0054             print(secondOnly)
0055         else:
0056             print("None")
0057         sys.exit()
0058     
0059     ########################
0060     ## All other commands ##
0061     ########################
0062     if options.command == 'and':
0063         outputList = alphaList & betaList
0064 
0065     if options.command == 'or':
0066         outputList = alphaList | betaList
0067 
0068     if options.command == 'sub':
0069         outputList = alphaList - betaList
0070 
0071     if len (args) >= 3:
0072         outputList.writeJSON (args[2])
0073     else:
0074         # print to screen
0075         print(outputList)
0076