Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-23 02:25:41

0001 #! /usr/bin/env python3
0002 
0003 from __future__ import print_function
0004 from FWCore.PythonUtilities.XML2Python import xml2obj
0005 from FWCore.PythonUtilities.LumiList   import LumiList
0006 from pprint import pprint
0007 
0008 import ast
0009 import optparse
0010 import sys
0011 
0012 
0013 if __name__ == '__main__':
0014 
0015     parser = optparse.OptionParser ("Usage: %prog [--options] job1.fjr [job2.fjr...]")
0016     parser.add_option ('--output', dest='output', type='string',
0017                        help='Save output to file OUTPUT')
0018     (options, args) = parser.parse_args()
0019     if not args:
0020         raise RuntimeError("Must provide at least one input file")
0021 
0022     runsLumisDict = {}
0023     for fjr in args:
0024         try:
0025             obj = xml2obj (filename=fjr)
0026         except:
0027             print("'%s' is not an framework job report.  Skipping." % fjr)
0028             continue
0029         for inputFile in obj.InputFile:
0030             try: # Regular XML version, assume only one of these
0031                 runObjects = inputFile.Runs.Run
0032                 for run in runObjects:
0033                     runNumber = int (run.ID)
0034                     runList = runsLumisDict.setdefault (runNumber, [])
0035                     for lumiPiece in run.LumiSection:
0036                         lumi = int (lumiPiece.ID)
0037                         runList.append (lumi)
0038             except:
0039                 try: # JSON-like version in CRAB XML files, runObjects is usually a list
0040                     if isinstance(inputFile.Runs, str):
0041                         runObjects = [inputFile.Runs]
0042                     else:
0043                         runObjects = inputFile.Runs
0044 
0045                     for runObject in runObjects:
0046                         try:
0047                             runs = ast.literal_eval(runObject)
0048                             for (run, lumis) in runs.items():
0049                                 runList = runsLumisDict.setdefault (int(run), [])
0050                                 runList.extend(lumis)
0051                         except ValueError: # Old style handled above
0052                             pass
0053                 except:
0054                     print("Run missing in '%s'.  Skipping." % fjr)
0055                 continue
0056 
0057     jsonList = LumiList (runsAndLumis = runsLumisDict)
0058     if options.output:
0059         jsonList.writeJSON (options.output)
0060     else:
0061         print(jsonList)
0062