CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/FWCore/PythonUtilities/scripts/csv2json.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import sys
00004 import optparse
00005 import re
00006 from FWCore.PythonUtilities.LumiList import LumiList
00007 
00008 
00009 if __name__ == '__main__':
00010     
00011     parser = optparse.OptionParser ("Usage: %prog input.csv")
00012     parser.add_option ('--output', dest='output', type='string',
00013                        help='Save output to file OUTPUT')
00014     parser.add_option ('--runIndex', dest='runIndex', type='int',
00015                        default = 0,
00016                        help='column to be converted to run number (default %default)')
00017     parser.add_option ('--lumiIndex', dest='lumiIndex', type='int',
00018                        default = 1,
00019                        help='column to be converted to lumi section number (default %default)')
00020     # required parameters
00021     (options, args) = parser.parse_args()
00022     if len (args) != 1:
00023         raise RuntimeError, "Must provide exactly one input file"
00024 
00025     sepRE = re.compile (r'[\s,;:]+')
00026     runLumiDict = {}    
00027     events = open (args[0], 'r')
00028     runIndex, lumiIndex = options.runIndex, options.lumiIndex
00029     minPieces = max (runIndex, lumiIndex) + 1
00030     for line in events:
00031         pieces = sepRE.split (line.strip())
00032         if len (pieces) < minPieces:
00033             continue
00034         try:
00035             run, lumi = int( pieces[runIndex] ), int( pieces[lumiIndex] )
00036         except:
00037             continue
00038         runLumiDict.setdefault (run, []).append (lumi)
00039     jsonList = LumiList (runsAndLumis = runLumiDict) 
00040     if options.output:
00041         jsonList.writeJSON (options.output)
00042     else:
00043         print jsonList