CMS 3D CMS Logo

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