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 "Current location does not seem to be a MillePede campaign directory:",
130 mssDir =
'/store/caf/user/'+os.environ[
'USER']+
'/MPproduction/'+mpsdirname
133 eos =
'/afs/cern.ch/project/eos/installation/cms/bin/eos.select'
134 os.system(eos+
' mkdir -p '+mssDir)
143 for var
in [
'classInf',
'pedeMem',
'jobname']:
145 generalOptions[var] = config.get(
'general',var)
146 except ConfigParser.NoOptionError:
147 print "No", var,
"found in [general] section. Please check ini-file."
151 generalOptions[
'datasetdir'] =
''
152 if config.has_option(
'general',
'datasetdir'):
153 generalOptions[
'datasetdir'] = config.get(
'general',
'datasetdir')
155 os.environ[
"datasetdir"] = generalOptions[
'datasetdir']
157 print "No datasetdir given in [general] section.",
158 print "Be sure to give a full path in inputFileList."
161 for var
in [
'globaltag',
'configTemplate',
'json']:
163 generalOptions[var] = config.get(
'general',var)
164 except ConfigParser.NoOptionError:
165 print 'No default', var,
'given in [general] section.'
169 pedesettings = ([x.strip()
for x
in config.get(
"general",
"pedesettings").
split(
",")]
170 if config.has_option(
"general",
"pedesettings")
else [
None])
180 if not os.path.isdir(
"jobData"):
181 print "No jobData-folder found. Properly set up the alignment before using the -w option."
183 if not os.path.exists(
"mps.db"):
184 print "No mps.db found. Properly set up the alignment before using the -w option."
189 configTemplate = config.get(
'general',
'configTemplate')
190 except ConfigParser.NoOptionError:
191 print 'No default configTemplate given in [general] section.'
192 print 'When using -w, a default configTemplate is needed to build a merge-config.'
197 globalTag = config.get(
'general',
'globaltag')
198 except ConfigParser.NoOptionError:
199 print "No default 'globaltag' given in [general] section."
200 print "When using -w, a default configTemplate is needed to build a merge-config."
204 with open(configTemplate,
"r") as f:
207 print "The config-template '"+configTemplate+
"' cannot be found."
210 tmpFile = re.sub(
'setupGlobaltag\s*\=\s*[\"\'](.*?)[\"\']',
211 'setupGlobaltag = \"'+globalTag+
'\"',
214 thisCfgTemplate =
"tmp.py"
215 with open(thisCfgTemplate,
"w")
as f:
218 for setting
in pedesettings:
222 print "Creating pede job."
224 print "Creating pede jobs using settings from '{0}'.".
format(setting)
225 for weight_conf
in weight_confs:
228 os.system(
"mps_weight.pl -c > /dev/null")
230 for name,weight
in weight_conf:
231 os.system(
"mps_weight.pl -N "+name+
" "+weight)
234 os.system(
"mps_setupm.pl")
237 lib = mpslib.jobdatabase()
241 command =
"rm -f jobData/"+lib.JOBDIR[-1]+
"/alignment_merge.py"
246 command = (
"mps_merge.py -w "+thisCfgTemplate+
" jobData/"+
247 lib.JOBDIR[-1]+
"/alignment_merge.py jobData/"+
248 lib.JOBDIR[-1]+
" "+str(lib.nJobs))
249 if setting
is not None: command +=
" -a "+setting
252 subprocess.call(command, stderr=subprocess.STDOUT, shell=
True)
254 with open(os.devnull,
'w')
as FNULL:
255 subprocess.call(command, stdout=FNULL,
256 stderr=subprocess.STDOUT, shell=
True)
259 os.system(
"rm "+thisCfgTemplate)
266 for section
in config.sections():
267 if 'general' in section:
269 elif section.startswith(
"dataset:"):
274 datasetOptions[
'name'] = section[8:]
277 for var
in [
'inputFileList',
'collection']:
279 datasetOptions[var] = config.get(section,var)
280 except ConfigParser.NoOptionError:
281 print 'No', var,
'found in', section+
'. Please check ini-file.'
285 for var
in [
'configTemplate',
'globaltag']:
286 if config.has_option(section,var):
287 datasetOptions[var] = config.get(section,var)
290 datasetOptions[var] = generalOptions[var]
292 print "No",var,
"found in ["+section+
"]",
293 print "and no default in [general] section."
297 datasetOptions[
'cosmicsZeroTesla'] =
False
298 if config.has_option(section,
'cosmicsZeroTesla'):
299 datasetOptions[
'cosmicsZeroTesla'] = config.getboolean(section,
'cosmicsZeroTesla')
301 datasetOptions[
'cosmicsDecoMode'] =
False
302 if config.has_option(section,
'cosmicsDecoMode'):
303 datasetOptions[
'cosmicsDecoMode'] = config.getboolean(section,
'cosmicsDecoMode')
305 datasetOptions[
'primaryWidth'] = -1.0
306 if config.has_option(section,
'primaryWidth'):
307 datasetOptions[
'primaryWidth'] = config.getfloat(section,
'primaryWidth')
309 datasetOptions[
'json'] =
''
310 if config.has_option(section,
'json'):
311 datasetOptions[
'json'] = config.get(section,
'json')
314 datasetOptions[
'json'] = generalOptions[
'json']
316 print "No json given in either [general] or ["+section+
"] sections.",
317 print "Proceeding without json-file."
321 datasetOptions[
'inputFileList'] = os.path.expandvars(datasetOptions[
'inputFileList'])
324 datasetOptions[
'configTemplate'] = os.path.expandvars(datasetOptions[
'configTemplate'])
328 datasetOptions[
'njobs'] = 0
330 with open(datasetOptions[
'inputFileList'],
'r') as filelist:
331 for line
in filelist:
332 if 'CastorPool' in line:
335 if not line.strip()==
'':
336 datasetOptions[
'njobs'] += 1
338 print 'Inputfilelist', datasetOptions[
'inputFileList'],
'does not exist.'
340 if datasetOptions[
'njobs'] == 0:
341 print 'Number of jobs is 0. There may be a problem with the inputfilelist:'
342 print datasetOptions[
'inputFileList']
346 if config.has_option(section,
'njobs'):
347 if config.getint(section,
'njobs')<=datasetOptions[
'njobs']:
348 datasetOptions[
'njobs'] = config.getint(section,
'njobs')
350 print 'njobs is bigger than the default',datasetOptions[
'njobs'],
'. Using default.'
352 print 'No number of jobs specified. Using number of files in inputfilelist as the number of jobs.'
357 with open(datasetOptions[
'configTemplate'],
'r') as INFILE:
358 tmpFile = INFILE.read()
360 print 'The config-template called',datasetOptions[
'configTemplate'],
'cannot be found.'
363 tmpFile = re.sub(
'setupGlobaltag\s*\=\s*[\"\'](.*?)[\"\']',
364 'setupGlobaltag = \"'+datasetOptions[
'globaltag']+
'\"',
366 tmpFile = re.sub(
'setupCollection\s*\=\s*[\"\'](.*?)[\"\']',
367 'setupCollection = \"'+datasetOptions[
'collection']+
'\"',
369 if datasetOptions[
'cosmicsZeroTesla']:
370 tmpFile = re.sub(re.compile(
'setupCosmicsZeroTesla\s*\=\s*.*$', re.M),
371 'setupCosmicsZeroTesla = True',
373 if datasetOptions[
'cosmicsDecoMode']:
374 tmpFile = re.sub(re.compile(
'setupCosmicsDecoMode\s*\=\s*.*$', re.M),
375 'setupCosmicsDecoMode = True',
377 if datasetOptions[
'primaryWidth'] > 0.0:
378 tmpFile = re.sub(re.compile(
'setupPrimaryWidth\s*\=\s*.*$', re.M),
379 'setupPrimaryWidth = '+str(datasetOptions[
'primaryWidth']),
381 if datasetOptions[
'json'] !=
'':
382 tmpFile = re.sub(re.compile(
'setupJson\s*\=\s*.*$', re.M),
383 'setupJson = \"'+datasetOptions[
'json']+
'\"',
386 thisCfgTemplate =
'tmp.py'
387 with open(thisCfgTemplate,
'w')
as OUTFILE:
388 OUTFILE.write(tmpFile)
396 configTemplate = tmpFile
399 command =
'mps_setup.pl -m%s -M %s -N %s %s %s %s %d %s %s %s cmscafuser:%s' % (
401 generalOptions[
'pedeMem'],
402 datasetOptions[
'name'],
405 datasetOptions[
'inputFileList'],
406 datasetOptions[
'njobs'],
407 generalOptions[
'classInf'],
408 generalOptions[
'jobname'],
412 print 'Submitting dataset:', datasetOptions[
'name']
413 print 'Baseconfig: ', datasetOptions[
'configTemplate']
414 print 'Collection: ', datasetOptions[
'collection']
415 if datasetOptions[
'collection']==
'ALCARECOTkAlCosmicsCTF0T':
416 print 'cosmicsDecoMode: ', datasetOptions[
'cosmicsDecoMode']
417 print 'cosmicsZeroTesla: ', datasetOptions[
'cosmicsZeroTesla']
418 print 'Globaltag: ', datasetOptions[
'globaltag']
419 print 'Number of jobs: ', datasetOptions[
'njobs']
420 print 'Inputfilelist: ', datasetOptions[
'inputFileList']
421 if datasetOptions[
'json'] !=
'':
422 print 'Jsonfile: ', datasetOptions[
'json']
423 print 'Pass to mps_setup: ', command
427 subprocess.call(command, stderr=subprocess.STDOUT, shell=
True)
429 with open(os.devnull,
'w')
as FNULL:
430 subprocess.call(command, stdout=FNULL,
431 stderr=subprocess.STDOUT, shell=
True)
434 os.system(
"rm "+thisCfgTemplate)
437 print "No dataset section defined in '{0}'".
format(aligmentConfig)
438 print "At least one section '[dataset:<name>]' is required."
441 firstPedeConfig =
True
442 for setting
in pedesettings:
446 print "Creating pede job."
448 print "Creating pede jobs using settings from '{0}'.".
format(setting)
449 for weight_conf
in weight_confs:
452 os.system(
"mps_weight.pl -c > /dev/null")
454 for name,weight
in weight_conf:
455 os.system(
"mps_weight.pl -N "+name+
" "+weight)
458 firstPedeConfig =
False
461 os.system(
"mps_setupm.pl")
464 lib = mpslib.jobdatabase()
468 command =
"rm -f jobData/"+lib.JOBDIR[-1]+
"/alignment_merge.py"
472 thisCfgTemplate =
"tmp.py"
473 with open(thisCfgTemplate,
"w")
as f:
474 f.write(configTemplate)
477 command = (
"mps_merge.py -w "+thisCfgTemplate+
" jobData/"+lib.JOBDIR[-1]+
478 "/alignment_merge.py jobData/"+lib.JOBDIR[-1]+
" "+
480 if setting
is not None: command +=
" -a "+setting
483 subprocess.call(command, stderr=subprocess.STDOUT, shell=
True)
485 with open(os.devnull,
'w')
as FNULL:
486 subprocess.call(command, stdout=FNULL,
487 stderr=subprocess.STDOUT, shell=
True)
490 os.system(
"rm "+thisCfgTemplate)