CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
filterCSVwithJSON.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import sys
4 import optparse
5 import re
6 from FWCore.PythonUtilities.LumiList import LumiList
7 
8 
9 if __name__ == '__main__':
10 
11  parser = optparse.OptionParser ("Usage: %prog input.json input.csv output.csv")
12  parser.add_option ('--output', dest='output', type='string',
13  help='Save output to file OUTPUT')
14  parser.add_option ('--runIndex', dest='runIndex', type='int',
15  default = 0,
16  help='column to be converted to run number (default %default)')
17  parser.add_option ('--lumiIndex', dest='lumiIndex', type='int',
18  default = 1,
19  help='column to be converted to lumi section number (default %default)')
20  parser.add_option ('--noWarnings', dest='noWarnings', action='store_true',
21  help='do not print warnings about lines not matching run, lumi numbers')
22  # required parameters
23  (options, args) = parser.parse_args()
24  if len (args) != 3:
25  raise RuntimeError, "Must provide an input JSON file, an input CSV file, and an output CSV file"
26 
27  sepRE = re.compile (r'[\s,;:]+')
28  runLumiDict = {}
29  jsonList = LumiList (args[0])
30  source = open (args[1], 'r')
31  target = open (args[2], 'w')
32  runIndex, lumiIndex = options.runIndex, options.lumiIndex
33  minPieces = max (runIndex, lumiIndex) + 1
34  for line in source:
35  copy = line.strip()
36  pieces = sepRE.split (copy.strip())
37  if len (pieces) < minPieces:
38  if not options.noWarnings:
39  print "Saving line '%s' since no identifiable run and lumi info" \
40  % copy
41  target.write (line)
42  continue
43  try:
44  run, lumi = int( pieces[runIndex] ), int( pieces[lumiIndex] )
45  except:
46  if not options.noWarnings:
47  print "Saving line '%s' since no identifiable run,lumi info" \
48  % copy
49  target.write (line)
50  continue
51  # OK. We recognize this line as containing a valid run and
52  # lumi number. Is it part of the JSON file we provided?
53  if (run, lumi) in jsonList:
54  # Yes, it is. Take it!
55  target.write (line)
56 
57  source.close()
58  target.close()