CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/FWCore/PythonUtilities/scripts/filterCSVwithJSON.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.json input.csv output.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     parser.add_option ('--noWarnings', dest='noWarnings', action='store_true',
00021                        help='do not print warnings about lines not matching run, lumi numbers')
00022     # required parameters
00023     (options, args) = parser.parse_args()
00024     if len (args) != 3:
00025         raise RuntimeError, "Must provide an input JSON file, an input CSV file, and an output CSV file"
00026 
00027     sepRE = re.compile (r'[\s,;:]+')
00028     runLumiDict = {}
00029     jsonList = LumiList (args[0])
00030     source = open (args[1], 'r')
00031     target = open (args[2], 'w')
00032     runIndex, lumiIndex = options.runIndex, options.lumiIndex
00033     minPieces = max (runIndex, lumiIndex) + 1
00034     for line in source:
00035         copy = line.strip()
00036         pieces = sepRE.split (copy.strip())
00037         if len (pieces) < minPieces:
00038             if not options.noWarnings:
00039                 print "Saving line '%s' since no identifiable run and lumi info" \
00040                       % copy
00041             target.write (line)
00042             continue
00043         try:
00044             run, lumi = int( pieces[runIndex] ), int( pieces[lumiIndex] )
00045         except:
00046             if not options.noWarnings:
00047                 print "Saving line '%s' since no identifiable run,lumi info" \
00048                       % copy
00049             target.write (line)
00050             continue
00051         # OK.  We recognize this line as containing a valid run and
00052         # lumi number.  Is it part of the JSON file we provided?
00053         if (run, lumi) in jsonList:
00054             # Yes, it is.  Take it!
00055             target.write (line)
00056             
00057     source.close()
00058     target.close()