CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
csv2json.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 import re
7 from FWCore.PythonUtilities.LumiList import LumiList
8 
9 
10 if __name__ == '__main__':
11 
12  parser = optparse.OptionParser ("Usage: %prog input.csv")
13  parser.add_option ('--output', dest='output', type='string',
14  help='Save output to file OUTPUT')
15  parser.add_option ('--runIndex', dest='runIndex', type='int',
16  default = 0,
17  help='column to be converted to run number (default %default)')
18  parser.add_option ('--lumiIndex', dest='lumiIndex', type='int',
19  default = 1,
20  help='column to be converted to lumi section number (default %default)')
21  # required parameters
22  (options, args) = parser.parse_args()
23  if len (args) != 1:
24  raise RuntimeError("Must provide exactly one input file")
25 
26  sepRE = re.compile (r'[\s,;:]+')
27  runLumiDict = {}
28  events = open (args[0], 'r')
29  runIndex, lumiIndex = options.runIndex, options.lumiIndex
30  minPieces = max (runIndex, lumiIndex) + 1
31  for line in events:
32  pieces = sepRE.split (line.strip())
33  if len (pieces) < minPieces:
34  continue
35  try:
36  run, lumi = int( pieces[runIndex] ), int( pieces[lumiIndex] )
37  except:
38  continue
39  runLumiDict.setdefault (run, []).append (lumi)
40  jsonList = LumiList (runsAndLumis = runLumiDict)
41  if options.output:
42  jsonList.writeJSON (options.output)
43  else:
44  print(jsonList)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47