16 from itertools
import combinations,combinations_with_replacement,permutations,product
47 templateScript =
"scripts/StrongReductions_BatchScript_Template.sh"
48 ScriptArchive =
"NPBashScripts"
51 compsToSplit = [
"EtaIntercalibration_Modelling_orig",
"Flavor_Composition_orig",
"Flavor_Response_orig",
"BJES_Response"]
66 remaining =
sorted([l
for l
in range(n)
if l
not in indices])
68 secondSet =
sorted(secondSet)
69 final =
sorted([l
for l
in remaining
if l
not in secondSet])
70 newlist =
sorted([tuple(indices),tuple(secondSet),tuple(final)])
72 myCombinations.append(tuple(newlist))
74 print "Removing duplicates."
75 myCombinations =
list(
set(myCombinations))
76 print "Examined",count,
"combinations of which",len(myCombinations),
"were unique."
82 newConfName = templateFile.replace(
"template",
"index{0}".
format(index))
83 combs = {0: combinations[0], 1 : combinations[1], 2:combinations[2]}
86 with open(templateFile)
as input,
open(newConfName,
"write")
as output :
88 if "XXX" in line
or "XXY" in line :
89 if "XXY" in line
and holdVal > 0 :
90 line = line.replace(
"XXY",
"{0}".
format(holdVal))
93 for val,indices
in combs.iteritems() :
99 line = line.replace(
"XXX",
"{0}".
format(addon))
100 line = line.replace(
"XXY",
"{0}".
format(addon))
101 if count == npars :
break
114 return replacement.join(source.rsplit(target, replacements))
119 groups = [100,101,102]
124 nCombs = 2**len(compsToSplit)
128 mycombinations = tuple(combinations_with_replacement([0,1], len(compsToSplit)))
130 for i
in mycombinations :
134 for index
in range(len(mypermutations)) :
135 newname = config.replace(
".config",
"_splitV{0}.config".
format(index))
136 newname = newname.replace(
"generateConfigs",
"splitsConfigs")
137 outfiles.append(
open(newname,
'w'))
145 with open(config,
"r")
as input :
150 if not line.isspace()
and not line.startswith(
"JES") :
151 for file
in outfiles : file.write(line)
156 if continuing
and addingSplit :
157 for index, file
in enumerate(outfiles) :
159 newLine =
"JESComponent.{0}.Split: 1\n\n".
format(thisVal)
163 for bonusline
in lines :
164 bonusline = bonusline.replace(
".{0}.".
format(thisVal),
".{0}.".
format(110+splitComp))
165 if ".Group" in bonusline :
172 groupIndex = JESGroups[group].groupingAssigned
173 possible.remove(groupIndex)
174 useIndex = mypermutations[index][splitComp]
177 file.write(bonusline)
179 newLine =
"JESComponent.{0}.Split: -1\n".
format(110+splitComp)
182 for file
in outfiles : file.write(line)
186 items =
filter(
None,line.split())
187 description = items[0]
188 val = eval(description.split(
".")[1])
197 if line.startswith(
"JESComponent") :
199 if "Name" in description :
201 if any(ext
in line
for ext
in compsToSplit) :
202 print "Found component", items[1]
204 splitComp = compsToSplit.index(items[1])
207 if ".Group" in description :
208 group = eval(items[1])
212 if line.startswith(
"JESGroup") :
215 if "Name" in description :
217 newGroup.index = thisVal
218 newGroup.name = items[1]
219 JESGroups[thisVal] = newGroup
220 if ".Group" in description :
221 JESGroups[thisVal].groupID = eval(items[1])
222 if ".SubGroup" in description :
223 JESGroups[thisVal].groupingAssigned = eval(items[1])
225 for file
in outfiles :
228 for file
in outfiles : file.close()
230 return len(mypermutations)
235 batchcommand = command
238 fbatchin =
open(templateScript,
'r')
239 fbatchindata = fbatchin.read()
243 batchtempname =
'{0}/StrongReductions_BatchScript_{1}_index{2}.sh'.
format(ScriptArchive,phrase,index)
244 fbatchout =
open(batchtempname,
'w')
245 fbatchoutdata = fbatchindata.replace(
"ZZZ",batchcommand)
246 fbatchout.write(fbatchoutdata)
247 modcommand =
'chmod 744 {0}'.
format(batchtempname)
248 subprocess.call(modcommand, shell=
True)
250 submitcommand =
"qsub {0}".
format(batchtempname)
252 subprocess.call(submitcommand, shell=
True)
259 if os.path.isfile(outRootName):
260 file_time = os.path.getmtime(outRootName)
261 config_time = os.path.getmtime(config)
262 if config_time < file_time : fileNew =
True
265 command =
'jetDefinition="AntiKt4EMTopo" configFiles="{0};{1}" outFile="{2}" outHistFile="{3}" bash runMakeCorrelationMatrixPlots.sh Flexible FineGridFixedEta false'.
format(nominalFile,config,outPlotName,outRootName)
269 subprocess.call(command,shell=
True)
277 earliestCanvas = 1E25
278 canvases = glob.glob(outFileName.replace(
".png",
"*.png"))
279 for canvas
in canvases :
281 if os.path.getmtime(canvas) < earliestCanvas : earliestCanvas = os.path.getmtime(canvas)
284 for rootFileName
in [file1,file2,file3,file4] :
285 if not os.path.isfile(rootFileName) :
286 print "ERROR! Root file does not exist."
288 if os.path.getmtime(rootFileName) > latestRootFile : latestRootFile = os.path.getmtime(rootFileName)
290 if canvasExists
and latestRootFile < earliestCanvas :
291 print "Combination already calculated; skip"
294 command =
'python ../python_scripts/Make4DCorrelationMatrix.py "AntiKt4EMTopo" {0} {1} false {2} {3} {4} {5}'.
format(outFileName,plotsName,file1,file2,file3,file4)
298 subprocess.call(command,shell=
True)
302 parser = argparse.ArgumentParser()
303 parser.add_argument(
"-d",
"--dir")
304 parser.add_argument(
"-t",
"--tmp")
305 parser.add_argument(
"-n",
"--npars",default=3)
306 parser.add_argument(
"-r",
"--ref")
307 parser.add_argument(
"-b",
"--batch",action=
'store_true')
308 parser.add_argument(
"-ns",
"--nosubmit", action=
'store_true')
309 parser.add_argument(
"-rc",
"--redo-configs", action=
'store_true')
310 parser.add_argument(
"-ntm",
"--ntestsmatrices",default=100)
311 parser.add_argument(
"-ntc",
"--ntestscomparisons",default=100)
312 parser.add_argument(
"--matrixStage",action=
'store_true')
313 parser.add_argument(
"--comparisonStage",action=
'store_true')
314 parser.add_argument(
"--investigateComb",default=
"",type=str)
315 parser.add_argument(
"--investigateSplits",default=
"",type=str)
316 args = parser.parse_args()
319 shareDir = os.getcwd()+
"/../../share/"+args.dir
320 nominalFile = shareDir+
"/"+args.ref
321 templateFile = shareDir+
"/"+args.tmp
323 if "/" in templateFile :
324 for item
in templateFile.split(
"/")[:-1] :
325 newFilesDir = newFilesDir+
"/"+item
329 print "sharedir",shareDir
330 print "nominal",nominalFile
331 print "template",templateFile
332 print "new configs will go in",newFilesDir
333 print "using",nPars,
"parameters, not counting eta intercalibration non-closure."
340 if args.redo_configs
and not args.investigateSplits :
341 start_time = time.time()
344 print(
"--- %s seconds ---" % (time.time() - start_time))
345 for comb
in fullCombList :
351 nConfigs = len([os.path.join(newFilesDir, f)
for f
in os.listdir(newFilesDir)
if "index" in f])
354 outRootDir = os.getcwd()+
"/NPRootFiles/"
355 outRootFormat = outRootDir+
"matrices_{0}.root"
356 if args.matrixStage
and not args.investigateSplits :
362 useval =
int(args.ntestsmatrices)
363 indicesToUse = random.sample(
xrange(0,nConfigs), useval)
364 for index
in indicesToUse :
367 config = templateFile.replace(
"template",
"index{0}".
format(index))
371 outRootName = outRootFormat.format(index)
373 outPlotName = os.getcwd()+
"/NPPlots/matrices_{0}.pdf".
format(index)
380 if args.comparisonStage
and not args.investigateSplits :
385 rootFiles = [os.path.join(outRootDir, f)
for f
in os.listdir(outRootDir)
if "matrices" in f]
386 indicesAvailable = []
387 for file
in rootFiles :
388 index = re.findall(
r'\d+', file.split(
"/")[-1].
split(
"_")[-1])
389 indicesAvailable.append(eval(index[0]))
394 if args.investigateComb :
395 goodComb = tuple([eval(item)
for item
in args.investigateComb.split(
",")])
397 for index
in range(4) :
398 for new
in random.sample(indicesAvailable,
max(1,
int(
float(args.ntestscomparisons)/4.0))) :
399 if not new
in goodComb :
400 newComb =
list(goodComb)
402 newList.append(tuple(newComb))
403 pool = tuple(newList)
409 random.shuffle(indicesAvailable)
410 if len(indicesAvailable) > 15 :
411 indicesAvailable = indicesAvailable[:15]
419 for indices
in random.sample(pool,
min(len(pool),args.ntestscomparisons)) :
420 print "Testing combination",indices
422 outFileName = os.getcwd()+
"/NPPlots/SR-4D-{0}-{1}-{2}-{3}.png".
format(indices[0],indices[1],indices[2],indices[3])
424 runComparisonCommands(outFileName,
"NONE",outRootFormat.format(indices[0]),outRootFormat.format(indices[1],outRootFormat.format(indices[2])),outRootFormat.format(indices[2]),outRootFormat.format(indices[3]),
"{0}-{1}-{2}-{3}".
format(indices[0],indices[1],indices[2],indices[3]),args.batch)
428 if args.investigateSplits :
430 configIndices = tuple([eval(item)
for item
in args.investigateSplits.split(
",")])
432 splitconfigs = [templateFile.replace(
"template",
"index{0}".
format(index))
for index
in configIndices[1:]]
435 for configindex, config
in zip(configIndices[1:],splitconfigs) :
439 for index
in range(newIndices) :
440 outRootName = (outRootFormat.format(configindex)).
replace(
".root",
"_splitV{0}.root".
format(index))
441 newconfig = config.replace(
".config",
"_splitV{0}.config".
format(index))
442 newconfig = newconfig.replace(
"generateConfigs",
"splitsConfigs")
443 newIterables.append(
"{0}_splitV{1}".
format(configindex,index))
447 iterables.append(newIterables)
450 isdone =
not subprocess.check_output(
"qstat",shell=
True)
453 isdone =
not subprocess.check_output(
"qstat",shell=
True)
456 pool = tuple(product([configIndices[0]],iterables[0],iterables[1],iterables[2]))
458 for indices
in random.sample(pool,
min(len(pool),args.ntestscomparisons)) :
459 print "Testing combination",indices
461 outFileName = os.getcwd()+
"/NPPlots/SR-4D-{0}-{1}-{2}-{3}.png".
format(indices[0],indices[1],indices[2],indices[3])
463 runComparisonCommands(outFileName,
"NONE",outRootFormat.format(indices[0]),outRootFormat.format(indices[1],outRootFormat.format(indices[2])),outRootFormat.format(indices[2]),outRootFormat.format(indices[3]),
"{0}-{1}-{2}-{3}".
format(indices[0],indices[1],indices[2],indices[3]),args.batch)
465 if __name__ ==
"__main__":