Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 10:39:32

0001 #!/usr/bin/env python3
0002 from __future__ import print_function
0003 from builtins import range
0004 import re
0005 import argparse
0006 import math
0007 import fileinput
0008 
0009 # Set up argrument parser
0010 helpEpilog = ''''''
0011 
0012 parser = argparse.ArgumentParser(description='Take card file, blank all INFI directives and insert the INFI directives from the modifier file instead.',
0013                                  epilog=helpEpilog,
0014                                  formatter_class=argparse.RawDescriptionHelpFormatter)
0015 
0016 parser.add_argument('inCfg', action='store',
0017                     help='name of the config-template')
0018 parser.add_argument('modCfg', action='store',
0019                     help='name of the modifier file from mps_split')
0020 parser.add_argument('outCfg', action='store',
0021                     help='name of modified output file')
0022 parser.add_argument('isn', action='store',
0023                     help='number of the job (three digit number with leading zeros)')
0024 parser.add_argument("--max-events", dest = "max_events", type = int,
0025                     help = "maximum number of events to process")
0026 parser.add_argument("--skip-events", dest = "skip_events", type = int,
0027                     help = "number of events to skip before processing")
0028 
0029 # Parse arguments
0030 args = parser.parse_args()
0031 inCfg  = args.inCfg
0032 modCfg = args.modCfg
0033 outCfg = args.outCfg
0034 isn    = args.isn
0035 
0036 
0037 # open input file
0038 with open(inCfg, 'r') as INFILE:
0039     body = INFILE.read()
0040 
0041 # read modifier file
0042 with open(modCfg, 'r') as MODFILE:
0043     mods = MODFILE.read()
0044 mods = mods.strip()
0045 
0046 # prepare the new fileNames directive. Delete first line if necessary.
0047 fileNames = mods.split('\n')
0048 if 'CastorPool=' in fileNames[0]:
0049     del fileNames[0]
0050 
0051 # replace ISN number (input is a string of three digit number with leading zeros though)
0052 body = re.sub(re.compile('ISN',re.M), isn, body)
0053 
0054 # print to outCfg
0055 with open(outCfg, 'w') as OUTFILE:
0056     OUTFILE.write(body)
0057 
0058 # Number of total files and number of extends for cms.vstring are needed
0059 numberOfFiles   = len(fileNames)
0060 numberOfExtends = int(math.ceil(numberOfFiles/255.))
0061 
0062 # Create and insert the readFile.extend lines
0063 for j in range(numberOfExtends):
0064     insertBlock = "readFiles.extend([\n    "
0065     i=0
0066     currentStart = j*255
0067     while (i<255) and ((currentStart+i)<numberOfFiles):
0068         entry = fileNames[currentStart+i].strip()
0069         if (i==254) or ((currentStart+i+1)==numberOfFiles):
0070             insertBlock += "\'"+entry+"\'])\n"
0071         else:
0072             insertBlock += "\'"+entry+"\',\n    "
0073         i+=1
0074 
0075     for line in fileinput.input(outCfg, inplace=1):
0076         print(line,end='')
0077         if re.match('readFiles\s*=\s*cms.untracked.vstring()',line):
0078             print(insertBlock,end='')
0079 
0080 if args.skip_events is not None:
0081     with open(outCfg, "a") as f:
0082         f.write("process.source.skipEvents = cms.untracked.uint32({0:d})\n"
0083                 .format(args.skip_events))
0084 
0085 if args.max_events is not None:
0086     with open(outCfg, "a") as f:
0087         f.write("process.maxEvents = cms.untracked.PSet(input = "
0088                 "cms.untracked.int32({0:d}))\n".format(args.max_events))