CMS 3D CMS Logo

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