Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-25 02:29:03

0001 #!/usr/bin/env python3
0002 #
0003 #  produce cfg file for merging run
0004 #
0005 #  Usage:
0006 #
0007 #  mps_merge.pl [-c] inCfg mergeCfg mergeDir njobs
0008 
0009 from builtins import range
0010 import Alignment.MillePedeAlignmentAlgorithm.mpslib.Mpslibclass as mpslib
0011 import re
0012 import os
0013 import argparse
0014 
0015 lib = mpslib.jobdatabase()
0016 
0017 ## Parse arguments
0018 ## -----------------------------------------------------------------------------
0019 parser = argparse.ArgumentParser(
0020     description='Produce Config for Pede-job from Mille-config.')
0021 
0022 # optional arguments
0023 parser.add_argument('-c', '--checkok', action='store_true',
0024                     help='check which jobs are OK and write just them to the config')
0025 parser.add_argument('-w', '--checkweight', action='store_true',
0026                     help='check for weight assignments in mps.db and add them to binarylist')
0027 parser.add_argument("-a", "--append", dest="append", metavar="SNIPPET",
0028                     help="config snippet to be appended to the output config")
0029 
0030 # positional arguments
0031 parser.add_argument('inCfg', action='store',
0032                     help='path to cfg-file, that is used as base')
0033 parser.add_argument('mergeCfg', action='store',
0034                     help='path and name of the config that is produced')
0035 parser.add_argument('mergeDir', action='store',
0036                     help='path to the merge directory')
0037 parser.add_argument('nJobs', action='store', type=int,
0038                     help='number of jobs')
0039 
0040 # parse arguments
0041 args = parser.parse_args()
0042 inCfg       = args.inCfg
0043 mergeCfg    = args.mergeCfg
0044 mergeDir    = args.mergeDir
0045 nJobs       = args.nJobs
0046 checkok     = args.checkok
0047 checkweight = args.checkweight
0048 
0049 if checkok or checkweight:
0050     lib.read_db()
0051 
0052 ## create merge config
0053 ## -----------------------------------------------------------------------------
0054 
0055 # create pede dir
0056 if not os.path.isdir(mergeDir):
0057     os.system('mkdir '+mergeDir)
0058 
0059 # open base config
0060 with open(inCfg, 'r') as INFILE:
0061     body = INFILE.read()
0062 
0063 
0064 # set mode to "pede"
0065 match = re.search('setupAlgoMode\s*?\=\s*?[\"\'].*?[\"\']', body)
0066 if match:
0067     body = re.sub('setupAlgoMode\s*?\=\s*?[\"\'].*?[\"\']',
0068                   'setupAlgoMode = \"pede\"',
0069                   body)
0070 else:
0071     print('Error in mps_merge: No setupAlgoMode found in baseconfig.')
0072 
0073 
0074 # build list of binary files
0075 binaryList = ''
0076 firstentry = True
0077 for i in range(nJobs):
0078     separator = ',\n                '
0079     if firstentry:
0080         separator = '\n                '
0081     if checkok and lib.JOBSTATUS[i]!='OK':
0082         continue
0083     firstentry = False
0084 
0085     newName = 'milleBinary%03d.dat' % (i+1)
0086     if checkweight and (lib.JOBSP2[i]!='' and lib.JOBSP2[i]!='1.0'):
0087         weight = lib.JOBSP2[i]
0088         print('Adding %s to list of binary files using weight %s' % (newName,weight))
0089         binaryList = '%s%s\'%s -- %s\'' % (binaryList, separator, newName, weight)
0090     else:
0091         print('Adding %s to list of binary files' % newName)
0092         binaryList = '%s%s\'%s\'' % (binaryList, separator, newName)
0093 
0094 
0095 # replace 'placeholder_binaryList' with binaryList
0096 match = re.search('[\"\']placeholder_binaryList[\"\']', body)
0097 if match:
0098     body = re.sub('[\"\']placeholder_binaryList[\"\']',
0099                   binaryList,
0100                   body)
0101 else:
0102     print('Error in mps_merge: No \'placeholder_binaryList\' found in baseconfig.')
0103 
0104 
0105 # build list of tree files
0106 treeList =''
0107 firstentry = True
0108 for i in range(nJobs):
0109     separator = ',\n                '
0110     if firstentry:
0111         separator = '\n                '
0112     if checkok and lib.JOBSTATUS[i]!='OK':
0113         continue
0114     firstentry = False
0115 
0116     newName = 'treeFile%03d.root' % (i+1)
0117     print('Adding %s to list of tree files.' % newName)
0118     treeList = '%s%s\'%s\'' % (treeList, separator, newName)
0119 
0120 
0121 # replace 'placeholder_treeList' with binaryList
0122 match = re.search('[\"\']placeholder_treeList[\"\']', body)
0123 if match:
0124     body = re.sub('[\"\']placeholder_treeList[\"\']',
0125                   treeList,
0126                   body)
0127 else:
0128     print('Error in mps_merge: No \'placeholder_treeList\' found in baseconfig.')
0129 
0130 if args.append is not None:
0131     with open(args.append) as snippet:
0132         body += snippet.read()
0133 
0134 with open(mergeCfg, 'w') as OUTFILE:
0135     OUTFILE.write(body)