CMS 3D CMS Logo

mps_merge.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # produce cfg file for merging run
4 #
5 # Usage:
6 #
7 # mps_merge.pl [-c] inCfg mergeCfg mergeDir njobs
8 
9 from __future__ import print_function
10 import Alignment.MillePedeAlignmentAlgorithm.mpslib.Mpslibclass as mpslib
11 import re
12 import os
13 import argparse
14 
15 lib = mpslib.jobdatabase()
16 
17 ## Parse arguments
18 ## -----------------------------------------------------------------------------
19 parser = argparse.ArgumentParser(
20  description='Produce Config for Pede-job from Mille-config.')
21 
22 # optional arguments
23 parser.add_argument('-c', '--checkok', action='store_true',
24  help='check which jobs are OK and write just them to the config')
25 parser.add_argument('-w', '--checkweight', action='store_true',
26  help='check for weight assignments in mps.db and add them to binarylist')
27 parser.add_argument("-a", "--append", dest="append", metavar="SNIPPET",
28  help="config snippet to be appended to the output config")
29 
30 # positional arguments
31 parser.add_argument('inCfg', action='store',
32  help='path to cfg-file, that is used as base')
33 parser.add_argument('mergeCfg', action='store',
34  help='path and name of the config that is produced')
35 parser.add_argument('mergeDir', action='store',
36  help='path to the merge directory')
37 parser.add_argument('nJobs', action='store', type=int,
38  help='number of jobs')
39 
40 # parse arguments
41 args = parser.parse_args()
42 inCfg = args.inCfg
43 mergeCfg = args.mergeCfg
44 mergeDir = args.mergeDir
45 nJobs = args.nJobs
46 checkok = args.checkok
47 checkweight = args.checkweight
48 
49 if checkok or checkweight:
50  lib.read_db()
51 
52 ## create merge config
53 ## -----------------------------------------------------------------------------
54 
55 # create pede dir
56 if not os.path.isdir(mergeDir):
57  os.system('mkdir '+mergeDir)
58 
59 # open base config
60 with open(inCfg, 'r') as INFILE:
61  body = INFILE.read()
62 
63 
64 # set mode to "pede"
65 match = re.search('setupAlgoMode\s*?\=\s*?[\"\'].*?[\"\']', body)
66 if match:
67  body = re.sub('setupAlgoMode\s*?\=\s*?[\"\'].*?[\"\']',
68  'setupAlgoMode = \"pede\"',
69  body)
70 else:
71  print('Error in mps_merge: No setupAlgoMode found in baseconfig.')
72 
73 
74 # build list of binary files
75 binaryList = ''
76 firstentry = True
77 for i in xrange(nJobs):
78  separator = ',\n '
79  if firstentry:
80  separator = '\n '
81  if checkok and lib.JOBSTATUS[i]!='OK':
82  continue
83  firstentry = False
84 
85  newName = 'milleBinary%03d.dat' % (i+1)
86  if checkweight and (lib.JOBSP2[i]!='' and lib.JOBSP2[i]!='1.0'):
87  weight = lib.JOBSP2[i]
88  print('Adding %s to list of binary files using weight %s' % (newName,weight))
89  binaryList = '%s%s\'%s -- %s\'' % (binaryList, separator, newName, weight)
90  else:
91  print('Adding %s to list of binary files' % newName)
92  binaryList = '%s%s\'%s\'' % (binaryList, separator, newName)
93 
94 
95 # replace 'placeholder_binaryList' with binaryList
96 match = re.search('[\"\']placeholder_binaryList[\"\']', body)
97 if match:
98  body = re.sub('[\"\']placeholder_binaryList[\"\']',
99  binaryList,
100  body)
101 else:
102  print('Error in mps_merge: No \'placeholder_binaryList\' found in baseconfig.')
103 
104 
105 # build list of tree files
106 treeList =''
107 firstentry = True
108 for i in xrange(nJobs):
109  separator = ',\n '
110  if firstentry:
111  separator = '\n '
112  if checkok and lib.JOBSTATUS[i]!='OK':
113  continue
114  firstentry = False
115 
116  newName = 'treeFile%03d.root' % (i+1)
117  print('Adding %s to list of tree files.' % newName)
118  treeList = '%s%s\'%s\'' % (treeList, separator, newName)
119 
120 
121 # replace 'placeholder_treeList' with binaryList
122 match = re.search('[\"\']placeholder_treeList[\"\']', body)
123 if match:
124  body = re.sub('[\"\']placeholder_treeList[\"\']',
125  treeList,
126  body)
127 else:
128  print('Error in mps_merge: No \'placeholder_treeList\' found in baseconfig.')
129 
130 if args.append is not None:
131  with open(args.append) as snippet:
132  body += snippet.read()
133 
134 with open(mergeCfg, 'w') as OUTFILE:
135  OUTFILE.write(body)
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:65