20 import Alignment.MillePedeAlignmentAlgorithm.mpslib.Mpslibclass
as mpslib
21 from Alignment.MillePedeAlignmentAlgorithm.alignmentsetup.helper
import checked_out_MPS
25 """Extracts different weight configurations from `config`.
28 - `config`: ConfigParser object containing the alignment configuration
31 weight_dict = collections.OrderedDict()
35 for section
in config.sections():
36 if 'general' in section:
38 elif section ==
"weights":
39 for option
in config.options(section):
40 common_weights[option] = [x.strip()
for x
in
41 config.get(section, option).
split(
",")]
42 elif section.startswith(
"dataset:"):
44 if config.has_option(section,
'weight'):
45 weight_dict[name] = [x.strip()
for x
in
46 config.get(section,
"weight").
split(
",")]
48 weight_dict[name] = [
'1.0']
50 weights_list = [[(name, weight)
for weight
in weight_dict[name]]
51 for name
in weight_dict]
53 common_weights_list = [[(name, weight)
for weight
in common_weights[name]]
54 for name
in common_weights]
55 common_weights_dicts = []
56 for item
in itertools.product(*common_weights_list):
58 for name,weight
in item:
60 common_weights_dicts.append(d)
63 for weight_conf
in itertools.product(*weights_list):
64 if len(common_weights) > 0:
65 for common_weight
in common_weights_dicts:
66 configs.append([(dataset[0],
67 reduce(
lambda x,y: x.replace(y, common_weight[y]),
68 common_weight, dataset[1]))
69 for dataset
in weight_conf])
71 configs.append(weight_conf)
80 helpEpilog =
'''Builds the config-templates from a universal config-template for each
81 dataset specified in .ini-file that is passed to this script.
82 Then calls mps_setup.pl for all datasets.'''
84 description=
'Setup the alignment as configured in the alignment_config file.',
87 parser.add_argument(
'-v',
'--verbose', action=
'store_true',
88 help=
'display detailed output of mps_setup')
90 parser.add_argument(
'-w',
'--weight', action=
'store_true',
91 help=
'create an additional mergejob with (possibly new) weights from .ini-config')
93 parser.add_argument(
'alignmentConfig', action=
'store',
94 help=
'name of the .ini config file that specifies the datasets to be used')
96 args = parser.parse_args()
97 aligmentConfig = args.alignmentConfig
100 config = ConfigParser.ConfigParser()
101 config.optionxform = str
102 config.read(aligmentConfig)
110 mpsScriptsDir = os.path.join(
"src",
"Alignment",
"MillePedeAlignmentAlgorithm",
"scripts")
112 mpsScriptsDir = os.path.join(os.environ[
"CMSSW_BASE"], mpsScriptsDir)
114 mpsScriptsDir = os.path.join(os.environ[
"CMSSW_RELEASE_BASE"], mpsScriptsDir)
115 milleScript = os.path.join(mpsScriptsDir,
"mps_runMille_template.sh")
116 pedeScript = os.path.join(mpsScriptsDir,
"mps_runPede_rfcp_template.sh")
119 currentDir = os.getcwd()
121 match = re.search(re.compile(
'mpproduction\/mp(.+?)$', re.M|re.I),currentDir)
123 mpsdirname =
'mp'+match.group(1)
125 print 'there seems to be a problem to determine the current directory name:',currentDir
129 mssDir =
'/store/caf/user/'+os.environ[
'USER']+
'/MPproduction/'+mpsdirname
132 eos =
'/afs/cern.ch/project/eos/installation/cms/bin/eos.select'
133 os.system(eos+
' mkdir -p '+mssDir)
142 for var
in [
'classInf',
'pedeMem',
'jobname']:
144 generalOptions[var] = config.get(
'general',var)
145 except ConfigParser.NoOptionError:
146 print "No", var,
"found in [general] section. Please check ini-file."
150 generalOptions[
'datasetdir'] =
''
151 if config.has_option(
'general',
'datasetdir'):
152 generalOptions[
'datasetdir'] = config.get(
'general',
'datasetdir')
154 os.environ[
"datasetdir"] = generalOptions[
'datasetdir']
156 print "No datasetdir given in [general] section.",
157 print "Be sure to give a full path in inputFileList."
160 for var
in [
'globaltag',
'configTemplate',
'json']:
162 generalOptions[var] = config.get(
'general',var)
163 except ConfigParser.NoOptionError:
164 print 'No default', var,
'given in [general] section.'
168 pedesettings = ([x.strip()
for x
in config.get(
"general",
"pedesettings").
split(
",")]
169 if config.has_option(
"general",
"pedesettings")
else [
None])
179 if not os.path.isdir(
"jobData"):
180 print "No jobData-folder found. Properly set up the alignment before using the -w option."
182 if not os.path.exists(
"mps.db"):
183 print "No mps.db found. Properly set up the alignment before using the -w option."
188 configTemplate = config.get(
'general',
'configTemplate')
189 except ConfigParser.NoOptionError:
190 print 'No default configTemplate given in [general] section.'
191 print 'When using -w, a default configTemplate is needed to build a merge-config.'
196 globalTag = config.get(
'general',
'globaltag')
197 except ConfigParser.NoOptionError:
198 print "No default 'globaltag' given in [general] section."
199 print "When using -w, a default configTemplate is needed to build a merge-config."
203 with open(configTemplate,
"r") as f:
206 print "The config-template '"+configTemplate+
"' cannot be found."
209 tmpFile = re.sub(
'setupGlobaltag\s*\=\s*[\"\'](.*?)[\"\']',
210 'setupGlobaltag = \"'+globalTag+
'\"',
213 thisCfgTemplate =
"tmp.py"
214 with open(thisCfgTemplate,
"w")
as f:
217 for setting
in pedesettings:
221 print "Creating pede job."
223 print "Creating pede jobs using settings from '{0}'.".
format(setting)
224 for weight_conf
in weight_confs:
227 os.system(
"mps_weight.pl -c > /dev/null")
229 for name,weight
in weight_conf:
230 os.system(
"mps_weight.pl -N "+name+
" "+weight)
233 os.system(
"mps_setupm.pl")
236 lib = mpslib.jobdatabase()
240 command =
"rm -f jobData/"+lib.JOBDIR[-1]+
"/alignment_merge.py"
245 command = (
"mps_merge.py -w "+thisCfgTemplate+
" jobData/"+
246 lib.JOBDIR[-1]+
"/alignment_merge.py jobData/"+
247 lib.JOBDIR[-1]+
" "+str(lib.nJobs))
248 if setting
is not None: command +=
" -a "+setting
251 subprocess.call(command, stderr=subprocess.STDOUT, shell=
True)
253 with open(os.devnull,
'w')
as FNULL:
254 subprocess.call(command, stdout=FNULL,
255 stderr=subprocess.STDOUT, shell=
True)
258 os.system(
"rm "+thisCfgTemplate)
265 for section
in config.sections():
266 if 'general' in section:
268 elif section.startswith(
"dataset:"):
273 datasetOptions[
'name'] = section[8:]
276 for var
in [
'inputFileList',
'collection']:
278 datasetOptions[var] = config.get(section,var)
279 except ConfigParser.NoOptionError:
280 print 'No', var,
'found in', section+
'. Please check ini-file.'
284 for var
in [
'configTemplate',
'globaltag']:
285 if config.has_option(section,var):
286 datasetOptions[var] = config.get(section,var)
289 datasetOptions[var] = generalOptions[var]
291 print "No",var,
"found in ["+section+
"]",
292 print "and no default in [general] section."
296 datasetOptions[
'cosmicsZeroTesla'] =
False
297 if config.has_option(section,
'cosmicsZeroTesla'):
298 datasetOptions[
'cosmicsZeroTesla'] = config.getboolean(section,
'cosmicsZeroTesla')
300 datasetOptions[
'cosmicsDecoMode'] =
False
301 if config.has_option(section,
'cosmicsDecoMode'):
302 datasetOptions[
'cosmicsDecoMode'] = config.getboolean(section,
'cosmicsDecoMode')
304 datasetOptions[
'primaryWidth'] = -1.0
305 if config.has_option(section,
'primaryWidth'):
306 datasetOptions[
'primaryWidth'] = config.getfloat(section,
'primaryWidth')
308 datasetOptions[
'json'] =
''
309 if config.has_option(section,
'json'):
310 datasetOptions[
'json'] = config.get(section,
'json')
313 datasetOptions[
'json'] = generalOptions[
'json']
315 print "No json given in either [general] or ["+section+
"] sections.",
316 print "Proceeding without json-file."
320 datasetOptions[
'inputFileList'] = os.path.expandvars(datasetOptions[
'inputFileList'])
323 datasetOptions[
'configTemplate'] = os.path.expandvars(datasetOptions[
'configTemplate'])
327 datasetOptions[
'njobs'] = 0
329 with open(datasetOptions[
'inputFileList'],
'r') as filelist:
330 for line
in filelist:
331 if 'CastorPool' in line:
334 if not line.strip()==
'':
335 datasetOptions[
'njobs'] += 1
337 print 'Inputfilelist', datasetOptions[
'inputFileList'],
'does not exist.'
339 if datasetOptions[
'njobs'] == 0:
340 print 'Number of jobs is 0. There may be a problem with the inputfilelist:'
341 print datasetOptions[
'inputFileList']
345 if config.has_option(section,
'njobs'):
346 if config.getint(section,
'njobs')<=datasetOptions[
'njobs']:
347 datasetOptions[
'njobs'] = config.getint(section,
'njobs')
349 print 'njobs is bigger than the default',datasetOptions[
'njobs'],
'. Using default.'
351 print 'No number of jobs specified. Using number of files in inputfilelist as the number of jobs.'
356 with open(datasetOptions[
'configTemplate'],
'r') as INFILE:
357 tmpFile = INFILE.read()
359 print 'The config-template called',datasetOptions[
'configTemplate'],
'cannot be found.'
362 tmpFile = re.sub(
'setupGlobaltag\s*\=\s*[\"\'](.*?)[\"\']',
363 'setupGlobaltag = \"'+datasetOptions[
'globaltag']+
'\"',
365 tmpFile = re.sub(
'setupCollection\s*\=\s*[\"\'](.*?)[\"\']',
366 'setupCollection = \"'+datasetOptions[
'collection']+
'\"',
368 if datasetOptions[
'cosmicsZeroTesla']:
369 tmpFile = re.sub(re.compile(
'setupCosmicsZeroTesla\s*\=\s*.*$', re.M),
370 'setupCosmicsZeroTesla = True',
372 if datasetOptions[
'cosmicsDecoMode']:
373 tmpFile = re.sub(re.compile(
'setupCosmicsDecoMode\s*\=\s*.*$', re.M),
374 'setupCosmicsDecoMode = True',
376 if datasetOptions[
'primaryWidth'] > 0.0:
377 tmpFile = re.sub(re.compile(
'setupPrimaryWidth\s*\=\s*.*$', re.M),
378 'setupPrimaryWidth = '+str(datasetOptions[
'primaryWidth']),
380 if datasetOptions[
'json'] !=
'':
381 tmpFile = re.sub(re.compile(
'setupJson\s*\=\s*.*$', re.M),
382 'setupJson = \"'+datasetOptions[
'json']+
'\"',
385 thisCfgTemplate =
'tmp.py'
386 with open(thisCfgTemplate,
'w')
as OUTFILE:
387 OUTFILE.write(tmpFile)
395 configTemplate = tmpFile
398 command =
'mps_setup.pl -m%s -M %s -N %s %s %s %s %d %s %s %s cmscafuser:%s' % (
400 generalOptions[
'pedeMem'],
401 datasetOptions[
'name'],
404 datasetOptions[
'inputFileList'],
405 datasetOptions[
'njobs'],
406 generalOptions[
'classInf'],
407 generalOptions[
'jobname'],
411 print 'Submitting dataset:', datasetOptions[
'name']
412 print 'Baseconfig: ', datasetOptions[
'configTemplate']
413 print 'Collection: ', datasetOptions[
'collection']
414 if datasetOptions[
'collection']==
'ALCARECOTkAlCosmicsCTF0T':
415 print 'cosmicsDecoMode: ', datasetOptions[
'cosmicsDecoMode']
416 print 'cosmicsZeroTesla: ', datasetOptions[
'cosmicsZeroTesla']
417 print 'Globaltag: ', datasetOptions[
'globaltag']
418 print 'Number of jobs: ', datasetOptions[
'njobs']
419 print 'Inputfilelist: ', datasetOptions[
'inputFileList']
420 if datasetOptions[
'json'] !=
'':
421 print 'Jsonfile: ', datasetOptions[
'json']
422 print 'Pass to mps_setup: ', command
426 subprocess.call(command, stderr=subprocess.STDOUT, shell=
True)
428 with open(os.devnull,
'w')
as FNULL:
429 subprocess.call(command, stdout=FNULL,
430 stderr=subprocess.STDOUT, shell=
True)
433 os.system(
"rm "+thisCfgTemplate)
436 print "No dataset section defined in '{0}'".
format(aligmentConfig)
437 print "At least one section '[dataset:<name>]' is required."
440 firstPedeConfig =
True
441 for setting
in pedesettings:
445 print "Creating pede job."
447 print "Creating pede jobs using settings from '{0}'.".
format(setting)
448 for weight_conf
in weight_confs:
451 os.system(
"mps_weight.pl -c > /dev/null")
453 for name,weight
in weight_conf:
454 os.system(
"mps_weight.pl -N "+name+
" "+weight)
457 firstPedeConfig =
False
460 os.system(
"mps_setupm.pl")
463 lib = mpslib.jobdatabase()
467 command =
"rm -f jobData/"+lib.JOBDIR[-1]+
"/alignment_merge.py"
471 thisCfgTemplate =
"tmp.py"
472 with open(thisCfgTemplate,
"w")
as f:
473 f.write(configTemplate)
476 command = (
"mps_merge.py -w "+thisCfgTemplate+
" jobData/"+lib.JOBDIR[-1]+
477 "/alignment_merge.py jobData/"+lib.JOBDIR[-1]+
" "+
479 if setting
is not None: command +=
" -a "+setting
482 subprocess.call(command, stderr=subprocess.STDOUT, shell=
True)
484 with open(os.devnull,
'w')
as FNULL:
485 subprocess.call(command, stdout=FNULL,
486 stderr=subprocess.STDOUT, shell=
True)
489 os.system(
"rm "+thisCfgTemplate)