CMS 3D CMS Logo

csv2json.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 import sys
4 from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
5 import re
6 from FWCore.PythonUtilities.LumiList import LumiList
7 
8 if __name__ == '__main__':
9 
10  parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
11  parser.add_argument('--output', dest='output', type=str,
12  help='Save output to file OUTPUT')
13  parser.add_argument('--runIndex', dest='runIndex', type=int,
14  default = 0,
15  help='column to be converted to run number')
16  parser.add_argument('--lumiIndex', dest='lumiIndex', type=int,
17  default = 1,
18  help='column to be converted to lumi section number')
19  parser.add_argument("input", metavar="input.csv", type=str)
20  options = parser.parse_args()
21 
22  sepRE = re.compile (r'[\s,;:]+')
23  runLumiDict = {}
24  events = open (options.input, 'r')
25  runIndex, lumiIndex = options.runIndex, options.lumiIndex
26  minPieces = max (runIndex, lumiIndex) + 1
27  for line in events:
28  pieces = sepRE.split (line.strip())
29  if len (pieces) < minPieces:
30  continue
31  try:
32  run, lumi = int( pieces[runIndex] ), int( pieces[lumiIndex] )
33  except:
34  continue
35  runLumiDict.setdefault (run, []).append (lumi)
36  jsonList = LumiList (runsAndLumis = runLumiDict)
37  if options.output:
38  jsonList.writeJSON (options.output)
39  else:
40  print(jsonList)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47