Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-13 03:15:35

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