CMS 3D CMS Logo

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