24 from __future__
import print_function
25 from builtins
import range
26 import sys, os, re, operator
27 import optparse
as opt
28 from cmsPerfCommons
import Candles, CandDesc, FileName, KeywordToCfi, CustomiseFragment, CandFname, EventContents
29 from functools
import reduce
34 THIS_PROG_NAME = os.path.basename(sys.argv[0])
35 cmsDriver =
'cmsDriver.py' 36 hypreg = re.compile(
'-')
38 DEF_STEPS = [
'GEN,SIM',
'DIGI']
39 AllSteps = [
"GEN,SIM",
"DIGI",
"L1",
"DIGI2RAW",
"HLT",
"RAW2DIGI",
"RECO"]
47 'TimingReport' :
'Timing_Parser',
48 'TimingReport @@@ reuse' :
'Timing_Parser',
49 'TimeReport' :
'Timereport_Parser',
50 'TimeReport @@@ reuse' :
'Timereport_Parser',
51 'SimpleMemReport' :
'SimpleMem_Parser',
52 'EdmSize' :
'Edm_Size',
53 'IgProfperf' :
'IgProf_perf.PERF_TICKS',
54 'IgProfperf @@@ reuse' :
'IgProf_perf.PERF_TICKS',
55 'IgProfMemTotal' :
'IgProf_mem.MEM_TOTAL',
56 'IgProfMemTotal @@@ reuse':
'IgProf_mem.MEM_TOTAL',
57 'IgProfMemLive' :
'IgProf_mem.MEM_LIVE',
58 'IgProfMemLive @@@ reuse' :
'IgProf_mem.MEM_LIVE',
59 'IgProfMemMax' :
'IgProf_mem.MEM_MAX',
60 'IgProfMemMax @@@ reuse' :
'IgProf_mem.MEM_MAX',
61 'IgProfMemAnalyse' :
'IgProf_mem.ANALYSE',
62 'valgrind' :
'ValgrindFCE',
63 'memcheck_valgrind' :
'Memcheck_Valgrind',
70 return filter(item.__eq__,list)[0]
74 for x
in range(lenlist - 1,0,-1):
79 '''Checks user steps <steps> for order against steps defined in AllSteps list. 80 If they are not, prints a warning and exits. 92 if step==
"GEN,FASTSIM":
96 split = astep.split(
"-")
97 astep = split[0].
split(
":")[0]
102 idx = AllSteps.index(astep.split(
":")[0])
103 if not ( idx == -2 ):
105 print(
"ERROR: Your user defined steps are not in a valid order")
109 lstidx = AllSteps.index(split[1].
split(
":")[0])
113 '''Analyzes the steps in <userSteps> to translate the cmsRelvalreportInput.py --step option into the cmsDriver.py one. Expanding hyphens for combined steps. 114 Handle the exceptional cases of GEN-, GEN-SIM and GEN-FASTSIM (GEN is never profiled by itself). 115 Returns a list of steps <steps>.''' 119 gsreg = re.compile(
'GEN-SIM')
120 greg = re.compile(
'GEN')
121 gfsreg = re.compile(
'GEN-FASTSIM')
122 StepsTokens = userSteps.split(
",")
124 for astep
in StepsTokens:
129 if gsreg.search(astep):
130 astep = gsreg.sub(
r"GEN,SIM", astep)
131 elif gfsreg.search(astep):
132 astep = gfsreg.sub(
r"GEN,FASTSIM", astep)
133 elif greg.search(astep):
134 astep = greg.sub(
r"GEN,SIM", astep)
148 explanations =
map(
lambda x:
" " + x, Candles)
150 for x
in range(len(explanations)):
151 explanation +=
"%-*s %s\n" % (30, explanations[x],CandDesc[x])
152 parser = opt.OptionParser(usage=(
"""%s NUM_EVENTS_PER_CFG CANDLES PROFILE [--cmsdriver=cmsDriverOptions] [--usersteps=processingStepsOption] 154 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. 157 NUM_EVENTS_PER_CFG - The number of events per config file 159 CANDLES - The simulation type to perform profiling on 161 AllCandles Run all of the candles below 164 PROFILE - the type of profiling to perform (multiple codes can be used): 177 Examples (could be obsolete!): 178 Perform Timing Report profiling for all candles, all steps and 10 events per cfg. 179 ./%s 10 AllCandles 1 \"--conditions FakeConditions --eventcontent FEVTDEBUGHLT\" 180 Perform Timing Report, Time Report and SimpleMemoryCheck profiling for HiggsZZ4LM200, all steps and 50 events. 181 ./%s 50 \"HiggsZZ4LM200\" 012 \"--conditions FakeConditions --eventcontent FEVTDEBUGHLT\" 182 Perform IgProfPerf and IgProfMemTotal profiling for TTbar, all steps and 100 events. 183 ./%s 100 \"TTbar\" 45 --cmsdriver=\"--conditions FakeConditions --eventcontent RAWRECO\" 184 Perform ValgrindFCE ValgrindMemCheck profiling for Minimum bias and 100 events. Only on GEN,SIM and DIGI steps. 185 ./%s 100 \"MinBias\" 89 --cmsdriver=\"--conditions FakeConditions --eventcontent RAWSIM\" \"--usersteps=GEN-SIM,DIGI""" 186 % ( THIS_PROG_NAME, explanation, THIS_PROG_NAME,THIS_PROG_NAME,THIS_PROG_NAME,THIS_PROG_NAME)))
188 devel = opt.OptionGroup(parser,
"Developer Options",
189 "Caution: use these options at your own risk." 190 "It is believed that some of them bite.\n")
199 help=
'Should we bypass using the HLT root file?' 207 help=
'Which steps to run',
214 dest=
'cmsDriverOptions',
215 help=
'Option for cmsDriver.py can be specified as a string to be added to all cmsDriver.py commands',
216 metavar=
'<CONDITION>',
222 dest=
'userInputFile',
223 help=
'Eventual input file (for now to be only used with HLT and RAW2DIGI-RECO workflows)',
225 metavar=
'<INPUTFILE>',
232 help=
'Do not perform profiling, ever',
241 help=
'Show debug output',
245 parser.set_defaults(debug=
False,noprof=
False)
246 parser.add_option_group(devel)
248 (options, args) = parser.parse_args()
251 _noprof = options.noprof
252 debug = options.debug
253 numofargs = len(args)
255 if ((
not numofargs == 3)
and (
not _noprof))
or (_noprof
and not (numofargs == 2)):
256 parser.error(
"There are not enough arguments specified to run this program." 257 " Please determine the correct arguments from the usage information above." 258 " Run with --help option for more information.")
261 return (options, args)
267 hypsteps = step.split(
r"-")
272 if not reduce(
lambda x,y,: x
and y,
map(
lambda x: x.split(
":")[0]
in AllSteps, hypsteps)):
274 print(
"ERROR: One of the steps you defined is invalid")
277 if (hypsteps[0] == hypsteps[1]):
278 print(
"WARNING: You should not add a hypenated step that as the same source and destination step, ignoring")
279 newsteps.append(hypsteps[0])
281 newsteps.append(hypsteps[0])
283 srt = AllSteps.index(hypsteps[0].
split(
":")[0]) + 1
284 for n
in range(srt,AllSteps.index(hypsteps[-1].
split(
":")[0])):
286 if astep
in ( hypsteps[i].
split(
":")[0]
for i
in range(1,len(hypsteps))):
288 astep=
filter(
lambda x: astep == x.split(
":")[0],hypsteps)[0]
289 if astep==hypsteps[-1].
split(
":")[0]:
291 newsteps.append(astep)
292 newsteps.append(hypsteps[-1])
294 if not (step.split(
":")[0]
in AllSteps):
295 print(
"ERROR: One of the steps you defined is invalid")
298 newsteps.append(step)
304 cmsDriverOptions =
"" 305 global AfterPileUpSteps
306 NumberOfEvents =
int(args[0])
307 WhichCandles =
str(args[1])
310 ProfileCode =
str(args[2])
312 if options.cmsDriverOptions:
314 cmsDriverOptions = options.cmsDriverOptions
315 print(
'Using user-specified cmsDriver.py options: ' + cmsDriverOptions)
317 if options.userSteps:
319 userSteps = options.userSteps
322 if WhichCandles.lower() ==
'allcandles':
324 print(
'ALL standard simulation candles will be PROCESSED:')
326 Candle = [WhichCandles]
327 print(
'Candle %s will be PROCESSED' % Candle[0])
333 print(
'The default steps will be run:')
336 print(
"You defined your own steps to run:")
341 return (NumberOfEvents, ProfileCode, cmsDriverOptions, steps, Candle, options.bypasshlt,options.userInputFile)
350 CMSSW_BASE = os.environ[
'CMSSW_BASE']
351 CMSSW_RELEASE_BASE = os.environ[
'CMSSW_RELEASE_BASE']
352 CMSSW_VERSION = os.environ[
'CMSSW_VERSION']
354 print(
'Error: An environment variable either CMSSW_{BASE, RELEASE_BASE or VERSION} is not available.')
355 print(
' Please run eval `scramv1 runtime -csh` to set your environment variables')
364 SimCandlesFile =
'SimulationCandles_%s.txt' % CMSSW_VERSION
367 simcandles = open(SimCandlesFile,
'w')
369 print(
"Couldn't open %s to save" % SimCandlesFile)
371 simcandles.write(
'#Candles file automatically generated by %s for %s\n' 372 % (THIS_PROG_NAME, CMSSW_VERSION))
373 simcandles.write(
"#CMSSW Base : %s\n" % CMSSW_BASE)
374 simcandles.write(
"#Release Base: %s\n" % CMSSW_RELEASE_BASE)
375 simcandles.write(
"#Version : %s\n\n" % CMSSW_VERSION)
401 Profile.append(AllowedProfile[-1])
406 if str(i)
in ProfileCode:
407 firstCase = (i == 0
and (
str(1)
in ProfileCode
or str(2)
in ProfileCode)) \
408 or (i == 1
and str(2)
in ProfileCode)
409 secCase = (i==4
and str(7)
in ProfileCode) \
410 or (i == 5
and (
str(6)
in ProfileCode
or str(7)
in ProfileCode)) \
411 or (i == 6
and str(7)
in ProfileCode)
413 if firstCase
or secCase:
414 Profile.append(AllowedProfile[i] +
' @@@ reuse')
417 Profile.append(
"IgProfMemMax @@@ reuse")
419 Profile.append(AllowedProfile[i])
421 Profile.append(
"IgProfMemMax")
426 simcandles.write(
'#%s\n' % FileName[acandle])
427 simcandles.write(
'#Step %s\n' % step)
432 if 'DIGI2RAW' in step:
433 SavedProfile = Profile
436 Profile = SavedProfile
438 return (Profile, SavedProfile)
444 if "--pileup" in cmsdriverOptions
and not (step ==
"HLT" or step.startswith(
"RAW2DIGI")):
445 return CustomiseFragment[
'DIGI-PILEUP']
446 elif step
in CustomiseFragment:
447 return CustomiseFragment[step]
451 return CustomiseFragment[
'DIGI']
454 def setInputFile(steps,step,acandle,stepIndex,pileup=False,bypasshlt=False):
457 if pileup
and stepIndex == 0:
458 InputFileOption =
"--filein file:%s_%s" % ( FileName[acandle],
"DIGI" )
460 InputFileOption =
"--filein file:%s_%s" % ( FileName[acandle],steps[stepIndex - 1] )
465 if 'GEN,SIM' in step:
467 if 'GEN,FASTSIM' in step:
469 elif 'HLT' in steps[stepIndex - 1]
and bypasshlt:
474 InputFileOption =
"--filein file:%s_%s" % ( FileName[acandle],steps[stepIndex - 2] )
476 if not InputFileOption ==
"" :
477 InputFileOption +=
".root " 479 return InputFileOption
481 def writeUnprofiledSteps(simcandles,CustomisePythonFragment,cmsDriverOptions,unprofiledSteps,previousOutputFile,acandle,NumberOfEvents, stepIndex, pileup,bypasshlt):
489 if bypasshlt
and unprofiledSteps[-1]==
"HLT":
490 stepsStr =
",".
join(unprofiledSteps[:-1])
491 OutputFile =
"%s_%s.root" % ( FileName[acandle],unprofiledSteps[-2])
493 stepsStr =
",".
join(unprofiledSteps)
494 OutputFile =
"%s_%s.root" % ( FileName[acandle],unprofiledSteps[-1])
495 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))
496 OutputFileOption =
"--fileout=%s" % OutputFile
508 InputFileOption =
"--filein file:" + previousOutputFile
509 if previousOutputFile ==
"":
510 InputFileOption =
setInputFile(AllSteps,unprofiledSteps[0],acandle,stepIndex,pileup=pileup,bypasshlt=bypasshlt)
514 for eventcontent
in EventContents:
515 cmsDriverOptions=re.sub(eventcontent,
'FEVTDEBUGHLT',cmsDriverOptions)
516 Command = (
"%s %s -n %s --step=%s %s %s --customise=%s %s" 518 KeywordToCfi[acandle],
523 CustomisePythonFragment,
525 simcandles.write(
"%s @@@ None @@@ None\n\n" % (Command))
533 fstIdx = AllSteps.index(steps[0].
split(
"-")[0].
split(
":")[0])
536 CustomisePythonFragment =
pythonFragment(
"GEN,SIM",cmsDriverOptions)
537 previousOutputFile=
"" 538 OutputFile =
writeUnprofiledSteps(simcandles, CustomisePythonFragment, cmsDriverOptions,AllSteps[0:fstIdx],previousOutputFile,acandle,NumberOfEvents, 0,pileup,bypasshlt)
539 return (fstIdx, OutputFile)
542 return "%s_%s.root" % ( FileName[acandle],endstep)
557 stopIndex = len(steps)
568 if (steps[0] ==
"GEN,FASTSIM"):
571 steps = [
"GEN,FASTSIM"]
573 if not (steps[0] == AllSteps[0])
and (steps[0].
split(
"-")[0] !=
"GEN,SIM"):
575 if (
"--pileup" in cmsDriverOptions)
and (steps[0]==
"HLT" or steps[0].startswith(
"RAW2DIGI")) :
576 userInputFile =
"../INPUT_PILEUP_EVENTS.root" 577 stepIndex=AllSteps.index(steps[0].
split(
"-")[0].
split(
":")[0])
580 elif userInputFile ==
"":
582 (stepIndex, rootFileStr) =
writePrerequisteSteps(simcandles,steps,acandle,NumberOfEvents,cmsDriverOptions,pileup,bypasshlt)
585 stepIndex=AllSteps.index(steps[0].
split(
"-")[0].
split(
":")[0])
593 fstROOTfileStr = rootFileStr
597 start = AllSteps.index(steps[0].
split(
"-")[0].
split(
":")[0])
602 lst = AllSteps.index(steps[-1].
split(
"-")[1].
split(
":")[0])
604 lst = AllSteps.index(steps[-1])
605 runSteps = AllSteps[start:lst]
606 numOfSteps = (lst - start) + 1
607 stopIndex = start + numOfSteps
614 stopIndex = AllSteps.index(steps[-1].
split(
"-")[1].
split(
":")[0]) + 1
617 stopIndex = AllSteps.index(steps[-1].
split(
":")[0]) + 1
622 rawreg = re.compile(
"^RAW2DIGI")
628 prevPrevOutputFile =
"" 630 if userInputFile!=
"":
631 previousOutputFile=userInputFile
633 previousOutputFile =
"" 635 for x
in range(start,stopIndex,1):
636 if stepIndex >= stopIndex:
638 step = steps[stepIndex]
651 if step
in userSteps
or reduce(
lambda x,y : x
or y,
map(
lambda x: step == x.split(
"-")[0].
split(
":")[0],userSteps)):
653 if "GEN,FASTSIM" in userSteps:
654 stepToWrite = stepToWrite +
",HLT:GRun" 655 elif "HLT" in userSteps:
656 stepToWrite = stepToWrite.replace(
"HLT",
"HLT:GRun")
659 hypMatch = [x
for x
in [x
for x
in userSteps
if step == x.split(
"-")[0]]
if "-" in x]
660 if not len(hypMatch) == 0 :
664 stepToWrite =
",".
join(hypsteps)
665 if "GEN,SIM-HLT" in userSteps:
666 stepToWrite = stepToWrite.replace(
"HLT",
"HLT:GRun")
667 befStep = hypsteps[0]
669 if bypasshlt
and hypsteps[-1]==
'HLT':
670 aftStep = hypsteps[-2]
672 aftStep = hypsteps[-1]
674 if [x
for x
in userSteps
if stepToWrite==x.split(
":")[0]]:
680 stepToWrite=
filter(
lambda x: stepToWrite==x.split(
":")[0],userSteps)[0]
686 if "GEN,SIM-HLT" or "GEN,FASTSIM" or "HLT" in userSteps:
687 stepToWrite = stepToWrite.replace(
":",
"=")
689 if '--pileup' in cmsDriverOptions:
690 outfile = stepToWrite +
"_PILEUP" 692 outfile = stepToWrite
696 if "GEN,SIM-HLT" or "GEN,FASTSIM" or "HLT" in userSteps:
697 stepToWrite = stepToWrite.replace(
"=",
":")
701 fstROOTfileStr = OutputFile
703 OutputFileOption =
"--fileout=" + OutputFile
709 if 'EdmSize' in prof:
710 EdmFile =
"%s_%s.root" % (FileName[acandle],outfile)
712 if prof == Profile[0]
and not os.path.exists(
"./" + EdmFile):
719 InputFileOption =
"--filein file:" + previousOutputFile
721 if rawreg.search(step)
and bypasshlt
and 'DIGI2RAW.root' in prevPrevOutputFile:
722 InputFileOption =
"--filein file:" + prevPrevOutputFile
723 if previousOutputFile ==
"":
724 InputFileOption =
setInputFile(steps,stepToWrite,acandle,stepIndex,pileup=pileup,bypasshlt=bypasshlt)
725 Command = (
"%s %s -n %s --step=%s %s %s --customise=%s %s" 727 KeywordToCfi[acandle],
732 CustomisePythonFragment,
734 simcandles.write(
"%s @@@ None @@@ None\n" % (Command))
739 InputFileOption =
"--filein file:" + previousOutputFile
740 if rawreg.search(step)
and bypasshlt
and 'DIGI2RAW.root' in prevPrevOutputFile:
741 InputFileOption =
"--filein file:" + prevPrevOutputFile
743 if previousOutputFile ==
"":
744 InputFileOption =
setInputFile(steps,befStep,acandle,stepIndex,pileup,bypasshlt)
748 if '--pileup' in cmsDriverOptions
and ( stepToWrite==
'GEN,SIM' or stepToWrite==
'SIM'):
749 Command = (
"%s %s -n %s --step=%s %s %s --customise=%s %s" % (
751 KeywordToCfi[acandle],
756 CustomiseFragment[
'GEN,SIM'],
757 cmsDriverOptions[:cmsDriverOptions.index(
'--pileup')]
759 elif '--pileup' in cmsDriverOptions
and (stepToWrite==
'GEN,FASTSIM' or stepToWrite==
'GEN,FASTSIM,HLT:GRun'):
761 Command = (
"%s %s -n %s --step=%s %s %s --customise=%s %s" % (
763 KeywordToCfi[acandle],
768 CustomiseFragment[
'DIGI'],
773 Command = (
"%s %s -n %s --step=%s %s %s --customise=%s %s" % (
775 KeywordToCfi[acandle],
780 CustomisePythonFragment,
785 simcandles.write(
"%s @@@ None @@@ None\n" % Command)
787 if 'valgrind' in prof:
789 if 'TimeMemoryInfo.py' in Command:
790 Command=Command.replace(
'--customise=Validation/Performance/TimeMemoryInfo.py',
'')
792 elif 'TimeMemoryG4Info.py' in Command:
793 Command=Command.replace(
'--customise=Validation/Performance/TimeMemoryG4Info.py',
'')
794 stepLabel=stepToWrite
796 if '--pileup' in cmsDriverOptions
and not "_PILEUP" in stepToWrite:
797 stepLabel = stepToWrite+
"_PILEUP" 812 IgProfProfile=
"IgProf" 815 IgProfProfile=
"IgProfMem @@@ reuse" 817 IgProfProfile=
"IgProfMem" 820 IgProfProfile=
"IgProfPerf @@@ reuse" 822 IgProfProfile=
"IgProfPerf" 825 Conditions=
"UNKNOWN_CONDITIONS" 827 EventContent=
"UNKNOWN_EVENTCONTENT" 828 tokens=cmsDriverOptions.split(
"--")
830 keywords=token.split(
" ")
831 if "=" in keywords[0]:
832 keywords=keywords[0].
split(
"=")
833 if "conditions" in keywords[0]:
836 if "auto:" in keywords[1]:
838 fileConditionsOption = autoCond[ keywords[1].
split(
':')[1] ]
839 Conditions = autoCond[ keywords[1].
split(
':')[1] ].
split(
"::")[0]
846 if "," in keywords[1]:
847 Conditions=keywords[1].
split(
",")[1].
split(
"::")[0]
849 Conditions=keywords[1].
split(
"::")[0]
850 elif "pileup" in keywords[0]:
852 elif "eventcontent" in keywords[0]:
853 EventContent=keywords[1]
856 MetaName=acandle+
"___"+stepToWrite+
"___"+PileUp+
"___"+Conditions+
"___"+EventContent+
"___"+IgProfProfile
858 if 'Analyse' not in prof
and (
lambda x:
'Analyse' in x,Profile):
860 simcandles.write(
"%s @@@ %s @@@ %s\n" % (Command,
866 analyse_command = (
"%s @@@ %s @@@ %s\n" % (Command,
867 Profiler[prof].
split(
".")[0]+
'.ANALYSE.'+Profiler[prof].
split(
".")[1],
874 if Profiler[prof] ==
'IgProf_mem.MEM_MAX' or Profiler[prof] ==
'IgProf_perf.PERF_TICKS':
875 analyse_command = analyse_command.replace(
"@@@ reuse",
"")
877 simcandles.write(analyse_command)
879 elif 'Analyse' in prof:
882 simcandles.write(
"%s @@@ %s @@@ %s_%s_%s\n" % (Command,
889 print(InputFileOption, step,
'GEN,SIM' in step,
'HLT' in steps[stepIndex - 1], steps)
890 print(
"cmsDriveroptions : " + cmsDriverOption)
891 prevPrevOutputFile = previousOutputFile
892 previousOutputFile = OutputFile
894 unprofiledSteps.append(step)
895 isNextStepForProfile =
False 898 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))
902 print(
"Error: Something is wrong we shouldn't have come this far")
905 if isNextStepForProfile:
907 OutputFile=
writeUnprofiledSteps(simcandles,CustomisePythonFragment,cmsDriverOptions,unprofiledSteps,previousOutputFile,acandle,NumberOfEvents,stepIndex,pileup,bypasshlt)
909 prevPrevOutputFile = previousOutputFile
910 previousOutputFile = OutputFile
913 stepIndex += len(hypsteps)
916 return fstROOTfileStr
918 def writeCommandsToReport(simcandles,Candle,Profile,debug,NumberOfEvents,cmsDriverOptions,steps,bypasshlt,userInputFile):
927 for acandle
in Candle:
928 print(
'*Candle ' + acandle)
961 (NumberOfEvents, ProfileCode, cmsDriverOptions, steps, Candle, bypasshlt, userInputFile ) =
setupProgramParameters(options,args)
968 (CMSSW_BASE, CMSSW_RELEASE_BASE, CMSSW_VERSION ) =
init_vars()
987 writeCommandsToReport(simcandles,Candle,Profile,debug,NumberOfEvents,cmsDriverOptions,steps,bypasshlt,userInputFile)
989 print(
"Written out cmsRelvalreport.py input file at:\n%s"%os.path.abspath(
'./'+simcandles.name))
993 if __name__ ==
"__main__":
S & print(S &os, JobReport::InputFile const &f)
static std::string join(char **cmd)