24 from __future__
import print_function
25 import sys, os, re, operator
26 import optparse
as opt
27 from cmsPerfCommons
import Candles, CandDesc, FileName, KeywordToCfi, CustomiseFragment, CandFname, EventContents
28 from functools
import reduce
33 THIS_PROG_NAME = os.path.basename(sys.argv[0])
34 cmsDriver =
'cmsDriver.py' 35 hypreg = re.compile(
'-')
37 DEF_STEPS = [
'GEN,SIM',
'DIGI']
38 AllSteps = [
"GEN,SIM",
"DIGI",
"L1",
"DIGI2RAW",
"HLT",
"RAW2DIGI",
"RECO"]
46 'TimingReport' :
'Timing_Parser',
47 'TimingReport @@@ reuse' :
'Timing_Parser',
48 'TimeReport' :
'Timereport_Parser',
49 'TimeReport @@@ reuse' :
'Timereport_Parser',
50 'SimpleMemReport' :
'SimpleMem_Parser',
51 'EdmSize' :
'Edm_Size',
52 'IgProfperf' :
'IgProf_perf.PERF_TICKS',
53 'IgProfperf @@@ reuse' :
'IgProf_perf.PERF_TICKS',
54 'IgProfMemTotal' :
'IgProf_mem.MEM_TOTAL',
55 'IgProfMemTotal @@@ reuse':
'IgProf_mem.MEM_TOTAL',
56 'IgProfMemLive' :
'IgProf_mem.MEM_LIVE',
57 'IgProfMemLive @@@ reuse' :
'IgProf_mem.MEM_LIVE',
58 'IgProfMemMax' :
'IgProf_mem.MEM_MAX',
59 'IgProfMemMax @@@ reuse' :
'IgProf_mem.MEM_MAX',
60 'IgProfMemAnalyse' :
'IgProf_mem.ANALYSE',
61 'valgrind' :
'ValgrindFCE',
62 'memcheck_valgrind' :
'Memcheck_Valgrind',
69 return filter(item.__eq__,list)[0]
73 for x
in range(lenlist - 1,0,-1):
78 '''Checks user steps <steps> for order against steps defined in AllSteps list. 79 If they are not, prints a warning and exits. 91 if step==
"GEN,FASTSIM":
95 split = astep.split(
"-")
96 astep = split[0].
split(
":")[0]
101 idx = AllSteps.index(astep.split(
":")[0])
102 if not ( idx == -2 ):
104 print(
"ERROR: Your user defined steps are not in a valid order")
108 lstidx = AllSteps.index(split[1].
split(
":")[0])
112 '''Analyzes the steps in <userSteps> to translate the cmsRelvalreportInput.py --step option into the cmsDriver.py one. Expanding hyphens for combined steps. 113 Handle the exceptional cases of GEN-, GEN-SIM and GEN-FASTSIM (GEN is never profiled by itself). 114 Returns a list of steps <steps>.''' 118 gsreg = re.compile(
'GEN-SIM')
119 greg = re.compile(
'GEN')
120 gfsreg = re.compile(
'GEN-FASTSIM')
121 StepsTokens = userSteps.split(
",")
123 for astep
in StepsTokens:
128 if gsreg.search(astep):
129 astep = gsreg.sub(
r"GEN,SIM", astep)
130 elif gfsreg.search(astep):
131 astep = gfsreg.sub(
r"GEN,FASTSIM", astep)
132 elif greg.search(astep):
133 astep = greg.sub(
r"GEN,SIM", astep)
147 explanations =
map(
lambda x:
" " + x, Candles)
149 for x
in range(len(explanations)):
150 explanation +=
"%-*s %s\n" % (30, explanations[x],CandDesc[x])
151 parser = opt.OptionParser(usage=(
"""%s NUM_EVENTS_PER_CFG CANDLES PROFILE [--cmsdriver=cmsDriverOptions] [--usersteps=processingStepsOption] 153 Description - This program creates a configuration file for cmsRelvalreport.py that describes the order in which cmsDriver.py should be run with which candles, steps and profiling so that time spent running cmsDriver.py is minimised. Without this correct ordering we would have to re-run parts of the profiling steps. 156 NUM_EVENTS_PER_CFG - The number of events per config file 158 CANDLES - The simulation type to perform profiling on 160 AllCandles Run all of the candles below 163 PROFILE - the type of profiling to perform (multiple codes can be used): 176 Examples (could be obsolete!): 177 Perform Timing Report profiling for all candles, all steps and 10 events per cfg. 178 ./%s 10 AllCandles 1 \"--conditions FakeConditions --eventcontent FEVTDEBUGHLT\" 179 Perform Timing Report, Time Report and SimpleMemoryCheck profiling for HiggsZZ4LM200, all steps and 50 events. 180 ./%s 50 \"HiggsZZ4LM200\" 012 \"--conditions FakeConditions --eventcontent FEVTDEBUGHLT\" 181 Perform IgProfPerf and IgProfMemTotal profiling for TTbar, all steps and 100 events. 182 ./%s 100 \"TTbar\" 45 --cmsdriver=\"--conditions FakeConditions --eventcontent RAWRECO\" 183 Perform ValgrindFCE ValgrindMemCheck profiling for Minimum bias and 100 events. Only on GEN,SIM and DIGI steps. 184 ./%s 100 \"MinBias\" 89 --cmsdriver=\"--conditions FakeConditions --eventcontent RAWSIM\" \"--usersteps=GEN-SIM,DIGI""" 185 % ( THIS_PROG_NAME, explanation, THIS_PROG_NAME,THIS_PROG_NAME,THIS_PROG_NAME,THIS_PROG_NAME)))
187 devel = opt.OptionGroup(parser,
"Developer Options",
188 "Caution: use these options at your own risk." 189 "It is believed that some of them bite.\n")
198 help=
'Should we bypass using the HLT root file?' 206 help=
'Which steps to run',
213 dest=
'cmsDriverOptions',
214 help=
'Option for cmsDriver.py can be specified as a string to be added to all cmsDriver.py commands',
215 metavar=
'<CONDITION>',
221 dest=
'userInputFile',
222 help=
'Eventual input file (for now to be only used with HLT and RAW2DIGI-RECO workflows)',
224 metavar=
'<INPUTFILE>',
231 help=
'Do not perform profiling, ever',
240 help=
'Show debug output',
244 parser.set_defaults(debug=
False,noprof=
False)
245 parser.add_option_group(devel)
247 (options, args) = parser.parse_args()
250 _noprof = options.noprof
251 debug = options.debug
252 numofargs = len(args)
254 if ((
not numofargs == 3)
and (
not _noprof))
or (_noprof
and not (numofargs == 2)):
255 parser.error(
"There are not enough arguments specified to run this program." 256 " Please determine the correct arguments from the usage information above." 257 " Run with --help option for more information.")
260 return (options, args)
266 hypsteps = step.split(
r"-")
271 if not reduce(
lambda x,y,: x
and y,
map(
lambda x: x.split(
":")[0]
in AllSteps, hypsteps)):
273 print(
"ERROR: One of the steps you defined is invalid")
276 if (hypsteps[0] == hypsteps[1]):
277 print(
"WARNING: You should not add a hypenated step that as the same source and destination step, ignoring")
278 newsteps.append(hypsteps[0])
280 newsteps.append(hypsteps[0])
282 srt = AllSteps.index(hypsteps[0].
split(
":")[0]) + 1
283 for n
in range(srt,AllSteps.index(hypsteps[-1].
split(
":")[0])):
285 if astep
in ( hypsteps[i].
split(
":")[0]
for i
in range(1,len(hypsteps))):
287 astep=
filter(
lambda x: astep == x.split(
":")[0],hypsteps)[0]
288 if astep==hypsteps[-1].
split(
":")[0]:
290 newsteps.append(astep)
291 newsteps.append(hypsteps[-1])
293 if not (step.split(
":")[0]
in AllSteps):
294 print(
"ERROR: One of the steps you defined is invalid")
297 newsteps.append(step)
303 cmsDriverOptions =
"" 304 global AfterPileUpSteps
305 NumberOfEvents =
int(args[0])
306 WhichCandles =
str(args[1])
309 ProfileCode =
str(args[2])
311 if options.cmsDriverOptions:
313 cmsDriverOptions = options.cmsDriverOptions
314 print(
'Using user-specified cmsDriver.py options: ' + cmsDriverOptions)
316 if options.userSteps:
318 userSteps = options.userSteps
321 if WhichCandles.lower() ==
'allcandles':
323 print(
'ALL standard simulation candles will be PROCESSED:')
325 Candle = [WhichCandles]
326 print(
'Candle %s will be PROCESSED' % Candle[0])
332 print(
'The default steps will be run:')
335 print(
"You defined your own steps to run:")
340 return (NumberOfEvents, ProfileCode, cmsDriverOptions, steps, Candle, options.bypasshlt,options.userInputFile)
349 CMSSW_BASE = os.environ[
'CMSSW_BASE']
350 CMSSW_RELEASE_BASE = os.environ[
'CMSSW_RELEASE_BASE']
351 CMSSW_VERSION = os.environ[
'CMSSW_VERSION']
353 print(
'Error: An environment variable either CMSSW_{BASE, RELEASE_BASE or VERSION} is not available.')
354 print(
' Please run eval `scramv1 runtime -csh` to set your environment variables')
363 SimCandlesFile =
'SimulationCandles_%s.txt' % CMSSW_VERSION
366 simcandles = open(SimCandlesFile,
'w')
368 print(
"Couldn't open %s to save" % SimCandlesFile)
370 simcandles.write(
'#Candles file automatically generated by %s for %s\n' 371 % (THIS_PROG_NAME, CMSSW_VERSION))
372 simcandles.write(
"#CMSSW Base : %s\n" % CMSSW_BASE)
373 simcandles.write(
"#Release Base: %s\n" % CMSSW_RELEASE_BASE)
374 simcandles.write(
"#Version : %s\n\n" % CMSSW_VERSION)
400 Profile.append(AllowedProfile[-1])
405 if str(i)
in ProfileCode:
406 firstCase = (i == 0
and (
str(1)
in ProfileCode
or str(2)
in ProfileCode)) \
407 or (i == 1
and str(2)
in ProfileCode)
408 secCase = (i==4
and str(7)
in ProfileCode) \
409 or (i == 5
and (
str(6)
in ProfileCode
or str(7)
in ProfileCode)) \
410 or (i == 6
and str(7)
in ProfileCode)
412 if firstCase
or secCase:
413 Profile.append(AllowedProfile[i] +
' @@@ reuse')
416 Profile.append(
"IgProfMemMax @@@ reuse")
418 Profile.append(AllowedProfile[i])
420 Profile.append(
"IgProfMemMax")
425 simcandles.write(
'#%s\n' % FileName[acandle])
426 simcandles.write(
'#Step %s\n' % step)
431 if 'DIGI2RAW' in step:
432 SavedProfile = Profile
435 Profile = SavedProfile
437 return (Profile, SavedProfile)
443 if "--pileup" in cmsdriverOptions
and not (step ==
"HLT" or step.startswith(
"RAW2DIGI")):
444 return CustomiseFragment[
'DIGI-PILEUP']
445 elif step
in CustomiseFragment:
446 return CustomiseFragment[step]
450 return CustomiseFragment[
'DIGI']
453 def setInputFile(steps,step,acandle,stepIndex,pileup=False,bypasshlt=False):
456 if pileup
and stepIndex == 0:
457 InputFileOption =
"--filein file:%s_%s" % ( FileName[acandle],
"DIGI" )
459 InputFileOption =
"--filein file:%s_%s" % ( FileName[acandle],steps[stepIndex - 1] )
464 if 'GEN,SIM' in step:
466 if 'GEN,FASTSIM' in step:
468 elif 'HLT' in steps[stepIndex - 1]
and bypasshlt:
473 InputFileOption =
"--filein file:%s_%s" % ( FileName[acandle],steps[stepIndex - 2] )
475 if not InputFileOption ==
"" :
476 InputFileOption +=
".root " 478 return InputFileOption
480 def writeUnprofiledSteps(simcandles,CustomisePythonFragment,cmsDriverOptions,unprofiledSteps,previousOutputFile,acandle,NumberOfEvents, stepIndex, pileup,bypasshlt):
488 if bypasshlt
and unprofiledSteps[-1]==
"HLT":
489 stepsStr =
",".
join(unprofiledSteps[:-1])
490 OutputFile =
"%s_%s.root" % ( FileName[acandle],unprofiledSteps[-2])
492 stepsStr =
",".
join(unprofiledSteps)
493 OutputFile =
"%s_%s.root" % ( FileName[acandle],unprofiledSteps[-1])
494 simcandles.write(
"\n#Run a %s step(s) that has not been selected for profiling but is needed to run the next step to be profiled\n" % (stepsStr))
495 OutputFileOption =
"--fileout=%s" % OutputFile
507 InputFileOption =
"--filein file:" + previousOutputFile
508 if previousOutputFile ==
"":
509 InputFileOption =
setInputFile(AllSteps,unprofiledSteps[0],acandle,stepIndex,pileup=pileup,bypasshlt=bypasshlt)
513 for eventcontent
in EventContents:
514 cmsDriverOptions=re.sub(eventcontent,
'FEVTDEBUGHLT',cmsDriverOptions)
515 Command = (
"%s %s -n %s --step=%s %s %s --customise=%s %s" 517 KeywordToCfi[acandle],
522 CustomisePythonFragment,
524 simcandles.write(
"%s @@@ None @@@ None\n\n" % (Command))
532 fstIdx = AllSteps.index(steps[0].
split(
"-")[0].
split(
":")[0])
535 CustomisePythonFragment =
pythonFragment(
"GEN,SIM",cmsDriverOptions)
536 previousOutputFile=
"" 537 OutputFile =
writeUnprofiledSteps(simcandles, CustomisePythonFragment, cmsDriverOptions,AllSteps[0:fstIdx],previousOutputFile,acandle,NumberOfEvents, 0,pileup,bypasshlt)
538 return (fstIdx, OutputFile)
541 return "%s_%s.root" % ( FileName[acandle],endstep)
556 stopIndex = len(steps)
567 if (steps[0] ==
"GEN,FASTSIM"):
570 steps = [
"GEN,FASTSIM"]
572 if not (steps[0] == AllSteps[0])
and (steps[0].
split(
"-")[0] !=
"GEN,SIM"):
574 if (
"--pileup" in cmsDriverOptions)
and (steps[0]==
"HLT" or steps[0].startswith(
"RAW2DIGI")) :
575 userInputFile =
"../INPUT_PILEUP_EVENTS.root" 576 stepIndex=AllSteps.index(steps[0].
split(
"-")[0].
split(
":")[0])
579 elif userInputFile ==
"":
581 (stepIndex, rootFileStr) =
writePrerequisteSteps(simcandles,steps,acandle,NumberOfEvents,cmsDriverOptions,pileup,bypasshlt)
584 stepIndex=AllSteps.index(steps[0].
split(
"-")[0].
split(
":")[0])
592 fstROOTfileStr = rootFileStr
596 start = AllSteps.index(steps[0].
split(
"-")[0].
split(
":")[0])
601 lst = AllSteps.index(steps[-1].
split(
"-")[1].
split(
":")[0])
603 lst = AllSteps.index(steps[-1])
604 runSteps = AllSteps[start:lst]
605 numOfSteps = (lst - start) + 1
606 stopIndex = start + numOfSteps
613 stopIndex = AllSteps.index(steps[-1].
split(
"-")[1].
split(
":")[0]) + 1
616 stopIndex = AllSteps.index(steps[-1].
split(
":")[0]) + 1
621 rawreg = re.compile(
"^RAW2DIGI")
627 prevPrevOutputFile =
"" 629 if userInputFile!=
"":
630 previousOutputFile=userInputFile
632 previousOutputFile =
"" 634 for x
in range(start,stopIndex,1):
635 if stepIndex >= stopIndex:
637 step = steps[stepIndex]
650 if step
in userSteps
or reduce(
lambda x,y : x
or y,
map(
lambda x: step == x.split(
"-")[0].
split(
":")[0],userSteps)):
652 if "GEN,FASTSIM" in userSteps:
653 stepToWrite = stepToWrite +
",HLT:GRun" 654 elif "HLT" in userSteps:
655 stepToWrite = stepToWrite.replace(
"HLT",
"HLT:GRun")
658 hypMatch = [x
for x
in [x
for x
in userSteps
if step == x.split(
"-")[0]]
if "-" in x]
659 if not len(hypMatch) == 0 :
663 stepToWrite =
",".
join(hypsteps)
664 if "GEN,SIM-HLT" in userSteps:
665 stepToWrite = stepToWrite.replace(
"HLT",
"HLT:GRun")
666 befStep = hypsteps[0]
668 if bypasshlt
and hypsteps[-1]==
'HLT':
669 aftStep = hypsteps[-2]
671 aftStep = hypsteps[-1]
673 if [x
for x
in userSteps
if stepToWrite==x.split(
":")[0]]:
679 stepToWrite=
filter(
lambda x: stepToWrite==x.split(
":")[0],userSteps)[0]
685 if "GEN,SIM-HLT" or "GEN,FASTSIM" or "HLT" in userSteps:
686 stepToWrite = stepToWrite.replace(
":",
"=")
688 if '--pileup' in cmsDriverOptions:
689 outfile = stepToWrite +
"_PILEUP" 691 outfile = stepToWrite
695 if "GEN,SIM-HLT" or "GEN,FASTSIM" or "HLT" in userSteps:
696 stepToWrite = stepToWrite.replace(
"=",
":")
700 fstROOTfileStr = OutputFile
702 OutputFileOption =
"--fileout=" + OutputFile
708 if 'EdmSize' in prof:
709 EdmFile =
"%s_%s.root" % (FileName[acandle],outfile)
711 if prof == Profile[0]
and not os.path.exists(
"./" + EdmFile):
718 InputFileOption =
"--filein file:" + previousOutputFile
720 if rawreg.search(step)
and bypasshlt
and 'DIGI2RAW.root' in prevPrevOutputFile:
721 InputFileOption =
"--filein file:" + prevPrevOutputFile
722 if previousOutputFile ==
"":
723 InputFileOption =
setInputFile(steps,stepToWrite,acandle,stepIndex,pileup=pileup,bypasshlt=bypasshlt)
724 Command = (
"%s %s -n %s --step=%s %s %s --customise=%s %s" 726 KeywordToCfi[acandle],
731 CustomisePythonFragment,
733 simcandles.write(
"%s @@@ None @@@ None\n" % (Command))
738 InputFileOption =
"--filein file:" + previousOutputFile
739 if rawreg.search(step)
and bypasshlt
and 'DIGI2RAW.root' in prevPrevOutputFile:
740 InputFileOption =
"--filein file:" + prevPrevOutputFile
742 if previousOutputFile ==
"":
743 InputFileOption =
setInputFile(steps,befStep,acandle,stepIndex,pileup,bypasshlt)
747 if '--pileup' in cmsDriverOptions
and ( stepToWrite==
'GEN,SIM' or stepToWrite==
'SIM'):
748 Command = (
"%s %s -n %s --step=%s %s %s --customise=%s %s" % (
750 KeywordToCfi[acandle],
755 CustomiseFragment[
'GEN,SIM'],
756 cmsDriverOptions[:cmsDriverOptions.index(
'--pileup')]
758 elif '--pileup' in cmsDriverOptions
and (stepToWrite==
'GEN,FASTSIM' or stepToWrite==
'GEN,FASTSIM,HLT:GRun'):
760 Command = (
"%s %s -n %s --step=%s %s %s --customise=%s %s" % (
762 KeywordToCfi[acandle],
767 CustomiseFragment[
'DIGI'],
772 Command = (
"%s %s -n %s --step=%s %s %s --customise=%s %s" % (
774 KeywordToCfi[acandle],
779 CustomisePythonFragment,
784 simcandles.write(
"%s @@@ None @@@ None\n" % Command)
786 if 'valgrind' in prof:
788 if 'TimeMemoryInfo.py' in Command:
789 Command=Command.replace(
'--customise=Validation/Performance/TimeMemoryInfo.py',
'')
791 elif 'TimeMemoryG4Info.py' in Command:
792 Command=Command.replace(
'--customise=Validation/Performance/TimeMemoryG4Info.py',
'')
793 stepLabel=stepToWrite
795 if '--pileup' in cmsDriverOptions
and not "_PILEUP" in stepToWrite:
796 stepLabel = stepToWrite+
"_PILEUP" 811 IgProfProfile=
"IgProf" 814 IgProfProfile=
"IgProfMem @@@ reuse" 816 IgProfProfile=
"IgProfMem" 819 IgProfProfile=
"IgProfPerf @@@ reuse" 821 IgProfProfile=
"IgProfPerf" 824 Conditions=
"UNKNOWN_CONDITIONS" 826 EventContent=
"UNKNOWN_EVENTCONTENT" 827 tokens=cmsDriverOptions.split(
"--")
829 keywords=token.split(
" ")
830 if "=" in keywords[0]:
831 keywords=keywords[0].
split(
"=")
832 if "conditions" in keywords[0]:
835 if "auto:" in keywords[1]:
837 fileConditionsOption = autoCond[ keywords[1].
split(
':')[1] ]
838 Conditions = autoCond[ keywords[1].
split(
':')[1] ].
split(
"::")[0]
845 if "," in keywords[1]:
846 Conditions=keywords[1].
split(
",")[1].
split(
"::")[0]
848 Conditions=keywords[1].
split(
"::")[0]
849 elif "pileup" in keywords[0]:
851 elif "eventcontent" in keywords[0]:
852 EventContent=keywords[1]
855 MetaName=acandle+
"___"+stepToWrite+
"___"+PileUp+
"___"+Conditions+
"___"+EventContent+
"___"+IgProfProfile
857 if 'Analyse' not in prof
and (
lambda x:
'Analyse' in x,Profile):
859 simcandles.write(
"%s @@@ %s @@@ %s\n" % (Command,
865 analyse_command = (
"%s @@@ %s @@@ %s\n" % (Command,
866 Profiler[prof].
split(
".")[0]+
'.ANALYSE.'+Profiler[prof].
split(
".")[1],
873 if Profiler[prof] ==
'IgProf_mem.MEM_MAX' or Profiler[prof] ==
'IgProf_perf.PERF_TICKS':
874 analyse_command = analyse_command.replace(
"@@@ reuse",
"")
876 simcandles.write(analyse_command)
878 elif 'Analyse' in prof:
881 simcandles.write(
"%s @@@ %s @@@ %s_%s_%s\n" % (Command,
888 print(InputFileOption, step,
'GEN,SIM' in step,
'HLT' in steps[stepIndex - 1], steps)
889 print(
"cmsDriveroptions : " + cmsDriverOption)
890 prevPrevOutputFile = previousOutputFile
891 previousOutputFile = OutputFile
893 unprofiledSteps.append(step)
894 isNextStepForProfile =
False 897 isNextStepForProfile = steps[stepIndex + 1]
in userSteps
or reduce(
lambda x,y : x
or y,
map(
lambda z: steps[ stepIndex + 1 ] == z.split(
"-")[0].
split(
":")[0],userSteps))
901 print(
"Error: Something is wrong we shouldn't have come this far")
904 if isNextStepForProfile:
906 OutputFile=
writeUnprofiledSteps(simcandles,CustomisePythonFragment,cmsDriverOptions,unprofiledSteps,previousOutputFile,acandle,NumberOfEvents,stepIndex,pileup,bypasshlt)
908 prevPrevOutputFile = previousOutputFile
909 previousOutputFile = OutputFile
912 stepIndex += len(hypsteps)
915 return fstROOTfileStr
917 def writeCommandsToReport(simcandles,Candle,Profile,debug,NumberOfEvents,cmsDriverOptions,steps,bypasshlt,userInputFile):
926 for acandle
in Candle:
927 print(
'*Candle ' + acandle)
960 (NumberOfEvents, ProfileCode, cmsDriverOptions, steps, Candle, bypasshlt, userInputFile ) =
setupProgramParameters(options,args)
967 (CMSSW_BASE, CMSSW_RELEASE_BASE, CMSSW_VERSION ) =
init_vars()
986 writeCommandsToReport(simcandles,Candle,Profile,debug,NumberOfEvents,cmsDriverOptions,steps,bypasshlt,userInputFile)
988 print(
"Written out cmsRelvalreport.py input file at:\n%s"%os.path.abspath(
'./'+simcandles.name))
992 if __name__ ==
"__main__":
S & print(S &os, JobReport::InputFile const &f)
static std::string join(char **cmd)