CMS 3D CMS Logo

compareJSON.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 import sys
4 from argparse import ArgumentParser
5 from FWCore.PythonUtilities.LumiList import LumiList
6 
7 
8 if __name__ == '__main__':
9 
10  parser = ArgumentParser()
11  # required parameters
12  cmdGroupTitle = parser.add_argument_group("Command Options")
13  cmdGroup = cmdGroupTitle.add_mutually_exclusive_group(required=True)
14  cmdGroup.add_argument('--and', dest='command', action='store_const',
15  const='and',
16  help = '"and" (i.e., take intersection) of two files')
17  cmdGroup.add_argument('--or', dest='command', action='store_const',
18  const='or',
19  help = '"or" (i.e., take union) of two files')
20  cmdGroup.add_argument('--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_argument('--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_argument("alpha", metavar="alpha.json", type=str)
27  parser.add_argument("beta", metavar="beta.json", type=str)
28  parser.add_argument("output", metavar="output.json", type=str, nargs='?', default=None)
29  options = parser.parse_args()
30  if not options.command:
31  parser.error("Exactly one command option must be specified")
32 
33  alphaList = LumiList (filename = options.alpha) # Read in first JSON file
34  betaList = LumiList (filename = options.beta) # Read in second JSON file
35 
36 
39  if options.command == 'diff':
40  if options.output is not None:
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." % (options.alpha, options.beta))
46  sys.exit()
47  print("'%s'-only lumis:" % options.alpha)
48  if firstOnly:
49  print(firstOnly)
50  else:
51  print("None")
52  print("\n'%s'-only lumis:" % options.beta)
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 options.output is not None:
72  outputList.writeJSON(options.output)
73  else:
74  # print to screen
75  print(outputList)
76 
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47