CMS 3D CMS Logo

mps_splice.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import re
3 import argparse
4 import math
5 import fileinput
6 
7 # Set up argrument parser
8 helpEpilog = ''''''
9 
10 parser = argparse.ArgumentParser(description='Take card file, blank all INFI directives and insert the INFI directives from the modifier file instead.',
11  epilog=helpEpilog,
12  formatter_class=argparse.RawDescriptionHelpFormatter)
13 
14 parser.add_argument('inCfg', action='store',
15  help='name of the config-template')
16 parser.add_argument('modCfg', action='store',
17  help='name of the modifier file from mps_split')
18 parser.add_argument('outCfg', action='store',
19  help='name of modified output file')
20 parser.add_argument('isn', action='store',
21  help='number of the job (three digit number with leading zeros)')
22 parser.add_argument("--max-events", dest = "max_events", type = int,
23  help = "maximum number of events to process")
24 parser.add_argument("--skip-events", dest = "skip_events", type = int,
25  help = "number of events to skip before processing")
26 
27 # Parse arguments
28 args = parser.parse_args()
29 inCfg = args.inCfg
30 modCfg = args.modCfg
31 outCfg = args.outCfg
32 isn = args.isn
33 
34 
35 # open input file
36 with open(inCfg, 'r') as INFILE:
37  body = INFILE.read()
38 
39 # read modifier file
40 with open(modCfg, 'r') as MODFILE:
41  mods = MODFILE.read()
42 mods = mods.strip()
43 
44 # prepare the new fileNames directive. Delete first line if necessary.
45 fileNames = mods.split('\n')
46 if 'CastorPool=' in fileNames[0]:
47  del fileNames[0]
48 
49 # replace ISN number (input is a string of three digit number with leading zeros though)
50 body = re.sub(re.compile('ISN',re.M), isn, body)
51 
52 # print to outCfg
53 with open(outCfg, 'w') as OUTFILE:
54  OUTFILE.write(body)
55 
56 # Number of total files and number of extends for cms.vstring are needed
57 numberOfFiles = len(fileNames)
58 numberOfExtends = int(math.ceil(numberOfFiles/255.))
59 
60 # Create and insert the readFile.extend lines
61 for j in xrange(numberOfExtends):
62  insertBlock = "readFiles.extend([\n "
63  i=0
64  currentStart = j*255
65  while (i<255) and ((currentStart+i)<numberOfFiles):
66  entry = fileNames[currentStart+i].strip()
67  if (i==254) or ((currentStart+i+1)==numberOfFiles):
68  insertBlock += "\'"+entry+"\'])\n"
69  else:
70  insertBlock += "\'"+entry+"\',\n "
71  i+=1
72 
73  for line in fileinput.input(outCfg, inplace=1):
74  print line,
75  if re.match('readFiles\s*=\s*cms.untracked.vstring()',line):
76  print insertBlock,
77 
78 if args.skip_events is not None:
79  with open(outCfg, "a") as f:
80  f.write("process.source.skipEvents = cms.untracked.uint32({0:d})\n"
81  .format(args.skip_events))
82 
83 if args.max_events is not None:
84  with open(outCfg, "a") as f:
85  f.write("process.maxEvents = cms.untracked.PSet(input = "
86  "cms.untracked.int32({0:d}))\n".format(args.max_events))