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