CMS 3D CMS Logo

compareJSON.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 from __future__ import print_function
4 import sys
5 import optparse
6 from FWCore.PythonUtilities.LumiList import LumiList
7 
8 
9 if __name__ == '__main__':
10 
11  parser = optparse.OptionParser ("Usage: %prog --command [--options] alpha.json beta.json [output.json]")
12  # required parameters
13  cmdGroup = optparse.OptionGroup (parser, "Command Options ")
14  cmdGroup.add_option ('--and', dest='command', action='store_const',
15  const='and',
16  help = '"and" (i.e., take intersection) of two files')
17  cmdGroup.add_option ('--or', dest='command', action='store_const',
18  const='or',
19  help = '"or" (i.e., take union) of two files')
20  cmdGroup.add_option ('--sub', dest='command', action='store_const',
21  const='sub',
22  help = '"subtraction" (i.e., lumi sections in alpha not in beta) of two files')
23  cmdGroup.add_option ('--diff', dest='command', action='store_const',
24  const='diff',
25  help = '"All differences (i.e., alpha - beta AND beta - alpha) of two files. Output will only be to screen (not proper JSON format).')
26  parser.add_option_group (cmdGroup)
27  (options, args) = parser.parse_args()
28  if len (args) < 2 or len (args) > 3:
29  raise RuntimeError("Two input filenames with one optional output filename must be provided.")
30  if not options.command:
31  raise RuntimeError("Exactly one command option must be specified")
32 
33  alphaList = LumiList (filename = args[0]) # Read in first JSON file
34  betaList = LumiList (filename = args[1]) # Read in second JSON file
35 
36 
39  if options.command == 'diff':
40  if len (args) >= 3:
41  raise RuntimeError("Can not output to file with '--diff' option. The output is not standard JSON.")
42  firstOnly = alphaList - betaList
43  secondOnly = betaList - alphaList
44  if not firstOnly and not secondOnly:
45  print("Files '%s' and '%s' are the same." % (args[0], args[1]))
46  sys.exit()
47  print("'%s'-only lumis:" % args[0])
48  if firstOnly:
49  print(firstOnly)
50  else:
51  print("None")
52  print("\n'%s'-only lumis:" % args[1])
53  if secondOnly:
54  print(secondOnly)
55  else:
56  print("None")
57  sys.exit()
58 
59 
62  if options.command == 'and':
63  outputList = alphaList & betaList
64 
65  if options.command == 'or':
66  outputList = alphaList | betaList
67 
68  if options.command == 'sub':
69  outputList = alphaList - betaList
70 
71  if len (args) >= 3:
72  outputList.writeJSON (args[2])
73  else:
74  # print to screen
75  print(outputList)
76 
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46