24 import sys, os, re, operator
25 import optparse
as opt
26 from cmsPerfCommons
import Candles, CandDesc, FileName, KeywordToCfi, CustomiseFragment, CandFname, EventContents
31 THIS_PROG_NAME = os.path.basename(sys.argv[0])
32 cmsDriver =
'cmsDriver.py'
33 hypreg = re.compile(
'-')
35 DEF_STEPS = [
'GEN,SIM',
'DIGI']
36 AllSteps = [
"GEN,SIM",
"DIGI",
"L1",
"DIGI2RAW",
"HLT",
"RAW2DIGI",
"RECO"]
44 'TimingReport' :
'Timing_Parser',
45 'TimingReport @@@ reuse' :
'Timing_Parser',
46 'TimeReport' :
'Timereport_Parser',
47 'TimeReport @@@ reuse' :
'Timereport_Parser',
48 'SimpleMemReport' :
'SimpleMem_Parser',
49 'EdmSize' :
'Edm_Size',
50 'IgProfperf' :
'IgProf_perf.PERF_TICKS',
51 'IgProfperf @@@ reuse' :
'IgProf_perf.PERF_TICKS',
52 'IgProfMemTotal' :
'IgProf_mem.MEM_TOTAL',
53 'IgProfMemTotal @@@ reuse':
'IgProf_mem.MEM_TOTAL',
54 'IgProfMemLive' :
'IgProf_mem.MEM_LIVE',
55 'IgProfMemLive @@@ reuse' :
'IgProf_mem.MEM_LIVE',
56 'IgProfMemMax' :
'IgProf_mem.MEM_MAX',
57 'IgProfMemMax @@@ reuse' :
'IgProf_mem.MEM_MAX',
58 'IgProfMemAnalyse' :
'IgProf_mem.ANALYSE',
59 'valgrind' :
'ValgrindFCE',
60 'memcheck_valgrind' :
'Memcheck_Valgrind',
67 return filter(item.__eq__,list)[0]
71 for x
in range(lenlist - 1,0,-1):
76 '''Checks user steps <steps> for order against steps defined in AllSteps list.
77 If they are not, prints a warning and exits.
89 if step==
"GEN,FASTSIM":
93 split = astep.split(
"-")
94 astep = split[0].
split(
":")[0]
99 idx = AllSteps.index(astep.split(
":")[0])
100 if not ( idx == -2 ):
102 print "ERROR: Your user defined steps are not in a valid order"
106 lstidx = AllSteps.index(split[1].
split(
":")[0])
110 '''Analyzes the steps in <userSteps> to translate the cmsRelvalreportInput.py --step option into the cmsDriver.py one. Expanding hyphens for combined steps.
111 Handle the exceptional cases of GEN-, GEN-SIM and GEN-FASTSIM (GEN is never profiled by itself).
112 Returns a list of steps <steps>.'''
116 gsreg = re.compile(
'GEN-SIM')
117 greg = re.compile(
'GEN')
118 gfsreg = re.compile(
'GEN-FASTSIM')
119 StepsTokens = userSteps.split(
",")
121 for astep
in StepsTokens:
126 if gsreg.search(astep):
127 astep = gsreg.sub(
r"GEN,SIM", astep)
128 elif gfsreg.search(astep):
129 astep = gfsreg.sub(
r"GEN,FASTSIM", astep)
130 elif greg.search(astep):
131 astep = greg.sub(
r"GEN,SIM", astep)
145 explanations =
map(
lambda x:
" " + x, Candles)
147 for x
in range(len(explanations)):
148 explanation +=
"%-*s %s\n" % (30, explanations[x],CandDesc[x])
149 parser = opt.OptionParser(usage=(
"""%s NUM_EVENTS_PER_CFG CANDLES PROFILE [--cmsdriver=cmsDriverOptions] [--usersteps=processingStepsOption]
151 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.
154 NUM_EVENTS_PER_CFG - The number of events per config file
156 CANDLES - The simulation type to perform profiling on
158 AllCandles Run all of the candles below
161 PROFILE - the type of profiling to perform (multiple codes can be used):
174 Examples (could be obsolete!):
175 Perform Timing Report profiling for all candles, all steps and 10 events per cfg.
176 ./%s 10 AllCandles 1 \"--conditions FakeConditions --eventcontent FEVTDEBUGHLT\"
177 Perform Timing Report, Time Report and SimpleMemoryCheck profiling for HiggsZZ4LM200, all steps and 50 events.
178 ./%s 50 \"HiggsZZ4LM200\" 012 \"--conditions FakeConditions --eventcontent FEVTDEBUGHLT\"
179 Perform IgProfPerf and IgProfMemTotal profiling for TTbar, all steps and 100 events.
180 ./%s 100 \"TTbar\" 45 --cmsdriver=\"--conditions FakeConditions --eventcontent RAWRECO\"
181 Perform ValgrindFCE ValgrindMemCheck profiling for Minimum bias and 100 events. Only on GEN,SIM and DIGI steps.
182 ./%s 100 \"MinBias\" 89 --cmsdriver=\"--conditions FakeConditions --eventcontent RAWSIM\" \"--usersteps=GEN-SIM,DIGI"""
183 % ( THIS_PROG_NAME, explanation, THIS_PROG_NAME,THIS_PROG_NAME,THIS_PROG_NAME,THIS_PROG_NAME)))
185 devel = opt.OptionGroup(parser,
"Developer Options",
186 "Caution: use these options at your own risk."
187 "It is believed that some of them bite.\n")
196 help=
'Should we bypass using the HLT root file?'
204 help=
'Which steps to run',
211 dest=
'cmsDriverOptions',
212 help=
'Option for cmsDriver.py can be specified as a string to be added to all cmsDriver.py commands',
213 metavar=
'<CONDITION>',
219 dest=
'userInputFile',
220 help=
'Eventual input file (for now to be only used with HLT and RAW2DIGI-RECO workflows)',
222 metavar=
'<INPUTFILE>',
229 help=
'Do not perform profiling, ever',
238 help=
'Show debug output',
242 parser.set_defaults(debug=
False,noprof=
False)
243 parser.add_option_group(devel)
245 (options, args) = parser.parse_args()
248 _noprof = options.noprof
249 debug = options.debug
250 numofargs = len(args)
252 if ((
not numofargs == 3)
and (
not _noprof))
or (_noprof
and not (numofargs == 2)):
253 parser.error(
"There are not enough arguments specified to run this program."
254 " Please determine the correct arguments from the usage information above."
255 " Run with --help option for more information.")
258 return (options, args)
264 hypsteps = step.split(
r"-")
269 if not reduce(
lambda x,y,: x
and y,
map(
lambda x: x.split(
":")[0]
in AllSteps, hypsteps)):
271 print "ERROR: One of the steps you defined is invalid"
274 if (hypsteps[0] == hypsteps[1]):
275 print "WARNING: You should not add a hypenated step that as the same source and destination step, ignoring"
276 newsteps.append(hypsteps[0])
278 newsteps.append(hypsteps[0])
280 srt = AllSteps.index(hypsteps[0].
split(
":")[0]) + 1
281 for n
in range(srt,AllSteps.index(hypsteps[-1].
split(
":")[0])):
283 if astep
in ( hypsteps[i].
split(
":")[0]
for i
in range(1,len(hypsteps))):
285 astep=
filter(
lambda x: astep == x.split(
":")[0],hypsteps)[0]
286 if astep==hypsteps[-1].
split(
":")[0]:
288 newsteps.append(astep)
289 newsteps.append(hypsteps[-1])
291 if not (step.split(
":")[0]
in AllSteps):
292 print "ERROR: One of the steps you defined is invalid"
295 newsteps.append(step)
301 cmsDriverOptions =
""
302 global AfterPileUpSteps
303 NumberOfEvents = int(args[0])
304 WhichCandles = str(args[1])
307 ProfileCode = str(args[2])
309 if options.cmsDriverOptions:
311 cmsDriverOptions = options.cmsDriverOptions
312 print 'Using user-specified cmsDriver.py options: ' + cmsDriverOptions
314 if options.userSteps:
316 userSteps = options.userSteps
319 if WhichCandles.lower() ==
'allcandles':
321 print 'ALL standard simulation candles will be PROCESSED:'
323 Candle = [WhichCandles]
324 print 'Candle %s will be PROCESSED' % Candle[0]
330 print 'The default steps will be run:'
333 print "You defined your own steps to run:"
338 return (NumberOfEvents, ProfileCode, cmsDriverOptions, steps, Candle, options.bypasshlt,options.userInputFile)
347 CMSSW_BASE = os.environ[
'CMSSW_BASE']
348 CMSSW_RELEASE_BASE = os.environ[
'CMSSW_RELEASE_BASE']
349 CMSSW_VERSION = os.environ[
'CMSSW_VERSION']
351 print 'Error: An environment variable either CMSSW_{BASE, RELEASE_BASE or VERSION} is not available.'
352 print ' Please run eval `scramv1 runtime -csh` to set your environment variables'
361 SimCandlesFile =
'SimulationCandles_%s.txt' % CMSSW_VERSION
364 simcandles = open(SimCandlesFile,
'w')
366 print "Couldn't open %s to save" % SimCandlesFile
368 simcandles.write(
'#Candles file automatically generated by %s for %s\n'
369 % (THIS_PROG_NAME, CMSSW_VERSION))
370 simcandles.write(
"#CMSSW Base : %s\n" % CMSSW_BASE)
371 simcandles.write(
"#Release Base: %s\n" % CMSSW_RELEASE_BASE)
372 simcandles.write(
"#Version : %s\n\n" % CMSSW_VERSION)
398 Profile.append(AllowedProfile[-1])
403 if str(i)
in ProfileCode:
404 firstCase = (i == 0
and (str(1)
in ProfileCode
or str(2)
in ProfileCode)) \
405 or (i == 1
and str(2)
in ProfileCode)
406 secCase = (i==4
and str(7)
in ProfileCode) \
407 or (i == 5
and (str(6)
in ProfileCode
or str(7)
in ProfileCode)) \
408 or (i == 6
and str(7)
in ProfileCode)
410 if firstCase
or secCase:
411 Profile.append(AllowedProfile[i] +
' @@@ reuse')
414 Profile.append(
"IgProfMemMax @@@ reuse")
416 Profile.append(AllowedProfile[i])
418 Profile.append(
"IgProfMemMax")
423 simcandles.write(
'#%s\n' % FileName[acandle])
424 simcandles.write(
'#Step %s\n' % step)
429 if 'DIGI2RAW' in step:
430 SavedProfile = Profile
433 Profile = SavedProfile
435 return (Profile, SavedProfile)
441 if "--pileup" in cmsdriverOptions
and not (step ==
"HLT" or step.startswith(
"RAW2DIGI")):
442 return CustomiseFragment[
'DIGI-PILEUP']
443 elif CustomiseFragment.has_key(step):
444 return CustomiseFragment[step]
448 return CustomiseFragment[
'DIGI']
451 def setInputFile(steps,step,acandle,stepIndex,pileup=False,bypasshlt=False):
454 if pileup
and stepIndex == 0:
455 InputFileOption =
"--filein file:%s_%s" % ( FileName[acandle],
"DIGI" )
457 InputFileOption =
"--filein file:%s_%s" % ( FileName[acandle],steps[stepIndex - 1] )
462 if 'GEN,SIM' in step:
464 if 'GEN,FASTSIM' in step:
466 elif 'HLT' in steps[stepIndex - 1]
and bypasshlt:
471 InputFileOption =
"--filein file:%s_%s" % ( FileName[acandle],steps[stepIndex - 2] )
473 if not InputFileOption ==
"" :
474 InputFileOption +=
".root "
476 return InputFileOption
478 def writeUnprofiledSteps(simcandles,CustomisePythonFragment,cmsDriverOptions,unprofiledSteps,previousOutputFile,acandle,NumberOfEvents, stepIndex, pileup,bypasshlt):
486 if bypasshlt
and unprofiledSteps[-1]==
"HLT":
487 stepsStr =
",".
join(unprofiledSteps[:-1])
488 OutputFile =
"%s_%s.root" % ( FileName[acandle],unprofiledSteps[-2])
490 stepsStr =
",".
join(unprofiledSteps)
491 OutputFile =
"%s_%s.root" % ( FileName[acandle],unprofiledSteps[-1])
492 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))
493 OutputFileOption =
"--fileout=%s" % OutputFile
505 InputFileOption =
"--filein file:" + previousOutputFile
506 if previousOutputFile ==
"":
507 InputFileOption =
setInputFile(AllSteps,unprofiledSteps[0],acandle,stepIndex,pileup=pileup,bypasshlt=bypasshlt)
511 for eventcontent
in EventContents:
512 cmsDriverOptions=re.sub(eventcontent,
'FEVTDEBUGHLT',cmsDriverOptions)
513 Command = (
"%s %s -n %s --step=%s %s %s --customise=%s %s"
515 KeywordToCfi[acandle],
520 CustomisePythonFragment,
522 simcandles.write(
"%s @@@ None @@@ None\n\n" % (Command))
530 fstIdx = AllSteps.index(steps[0].
split(
"-")[0].
split(
":")[0])
533 CustomisePythonFragment =
pythonFragment(
"GEN,SIM",cmsDriverOptions)
534 previousOutputFile=
""
535 OutputFile =
writeUnprofiledSteps(simcandles, CustomisePythonFragment, cmsDriverOptions,AllSteps[0:fstIdx],previousOutputFile,acandle,NumberOfEvents, 0,pileup,bypasshlt)
536 return (fstIdx, OutputFile)
539 return "%s_%s.root" % ( FileName[acandle],endstep)
554 stopIndex = len(steps)
565 if (steps[0] ==
"GEN,FASTSIM"):
568 steps = [
"GEN,FASTSIM"]
570 if not (steps[0] == AllSteps[0])
and (steps[0].
split(
"-")[0] !=
"GEN,SIM"):
572 if (
"--pileup" in cmsDriverOptions)
and (steps[0]==
"HLT" or steps[0].startswith(
"RAW2DIGI")) :
573 userInputFile =
"../INPUT_PILEUP_EVENTS.root"
574 stepIndex=AllSteps.index(steps[0].
split(
"-")[0].
split(
":")[0])
577 elif userInputFile ==
"":
579 (stepIndex, rootFileStr) =
writePrerequisteSteps(simcandles,steps,acandle,NumberOfEvents,cmsDriverOptions,pileup,bypasshlt)
582 stepIndex=AllSteps.index(steps[0].
split(
"-")[0].
split(
":")[0])
590 fstROOTfileStr = rootFileStr
594 start = AllSteps.index(steps[0].
split(
"-")[0].
split(
":")[0])
599 lst = AllSteps.index(steps[-1].
split(
"-")[1].
split(
":")[0])
601 lst = AllSteps.index(steps[-1])
602 runSteps = AllSteps[start:lst]
603 numOfSteps = (lst - start) + 1
604 stopIndex = start + numOfSteps
611 stopIndex = AllSteps.index(steps[-1].
split(
"-")[1].
split(
":")[0]) + 1
614 stopIndex = AllSteps.index(steps[-1].
split(
":")[0]) + 1
619 rawreg = re.compile(
"^RAW2DIGI")
625 prevPrevOutputFile =
""
627 if userInputFile!=
"":
628 previousOutputFile=userInputFile
630 previousOutputFile =
""
632 for x
in range(start,stopIndex,1):
633 if stepIndex >= stopIndex:
635 step = steps[stepIndex]
648 if step
in userSteps
or reduce(
lambda x,y : x
or y,
map(
lambda x: step == x.split(
"-")[0].
split(
":")[0],userSteps)):
650 if "GEN,FASTSIM" in userSteps:
651 stepToWrite = stepToWrite +
",HLT:GRun"
652 elif "HLT" in userSteps:
653 stepToWrite = stepToWrite.replace(
"HLT",
"HLT:GRun")
656 hypMatch =
filter(
lambda x:
"-" in x,
filter(
lambda x: step == x.split(
"-")[0],userSteps))
657 if not len(hypMatch) == 0 :
661 stepToWrite =
",".
join(hypsteps)
662 if "GEN,SIM-HLT" in userSteps:
663 stepToWrite = stepToWrite.replace(
"HLT",
"HLT:GRun")
664 befStep = hypsteps[0]
666 if bypasshlt
and hypsteps[-1]==
'HLT':
667 aftStep = hypsteps[-2]
669 aftStep = hypsteps[-1]
671 if filter(
lambda x: stepToWrite==x.split(
":")[0],userSteps):
677 stepToWrite=
filter(
lambda x: stepToWrite==x.split(
":")[0],userSteps)[0]
683 if "GEN,SIM-HLT" or "GEN,FASTSIM" or "HLT" in userSteps:
684 stepToWrite = stepToWrite.replace(
":",
"=")
686 if '--pileup' in cmsDriverOptions:
687 outfile = stepToWrite +
"_PILEUP"
689 outfile = stepToWrite
693 if "GEN,SIM-HLT" or "GEN,FASTSIM" or "HLT" in userSteps:
694 stepToWrite = stepToWrite.replace(
"=",
":")
698 fstROOTfileStr = OutputFile
700 OutputFileOption =
"--fileout=" + OutputFile
706 if 'EdmSize' in prof:
707 EdmFile =
"%s_%s.root" % (FileName[acandle],outfile)
709 if prof == Profile[0]
and not os.path.exists(
"./" + EdmFile):
716 InputFileOption =
"--filein file:" + previousOutputFile
718 if rawreg.search(step)
and bypasshlt
and 'DIGI2RAW.root' in prevPrevOutputFile:
719 InputFileOption =
"--filein file:" + prevPrevOutputFile
720 if previousOutputFile ==
"":
721 InputFileOption =
setInputFile(steps,stepToWrite,acandle,stepIndex,pileup=pileup,bypasshlt=bypasshlt)
722 Command = (
"%s %s -n %s --step=%s %s %s --customise=%s %s"
724 KeywordToCfi[acandle],
729 CustomisePythonFragment,
731 simcandles.write(
"%s @@@ None @@@ None\n" % (Command))
736 InputFileOption =
"--filein file:" + previousOutputFile
737 if rawreg.search(step)
and bypasshlt
and 'DIGI2RAW.root' in prevPrevOutputFile:
738 InputFileOption =
"--filein file:" + prevPrevOutputFile
740 if previousOutputFile ==
"":
741 InputFileOption =
setInputFile(steps,befStep,acandle,stepIndex,pileup,bypasshlt)
745 if '--pileup' in cmsDriverOptions
and ( stepToWrite==
'GEN,SIM' or stepToWrite==
'SIM'):
746 Command = (
"%s %s -n %s --step=%s %s %s --customise=%s %s" % (
748 KeywordToCfi[acandle],
753 CustomiseFragment[
'GEN,SIM'],
754 cmsDriverOptions[:cmsDriverOptions.index(
'--pileup')]
756 elif '--pileup' in cmsDriverOptions
and (stepToWrite==
'GEN,FASTSIM' or stepToWrite==
'GEN,FASTSIM,HLT:GRun'):
758 Command = (
"%s %s -n %s --step=%s %s %s --customise=%s %s" % (
760 KeywordToCfi[acandle],
765 CustomiseFragment[
'DIGI'],
770 Command = (
"%s %s -n %s --step=%s %s %s --customise=%s %s" % (
772 KeywordToCfi[acandle],
777 CustomisePythonFragment,
782 simcandles.write(
"%s @@@ None @@@ None\n" % Command)
784 if 'valgrind' in prof:
786 if 'TimeMemoryInfo.py' in Command:
787 Command=Command.replace(
'--customise=Validation/Performance/TimeMemoryInfo.py',
'')
789 elif 'TimeMemoryG4Info.py' in Command:
790 Command=Command.replace(
'--customise=Validation/Performance/TimeMemoryG4Info.py',
'')
791 stepLabel=stepToWrite
793 if '--pileup' in cmsDriverOptions
and not "_PILEUP" in stepToWrite:
794 stepLabel = stepToWrite+
"_PILEUP"
809 IgProfProfile=
"IgProf"
812 IgProfProfile=
"IgProfMem @@@ reuse"
814 IgProfProfile=
"IgProfMem"
817 IgProfProfile=
"IgProfPerf @@@ reuse"
819 IgProfProfile=
"IgProfPerf"
822 Conditions=
"UNKNOWN_CONDITIONS"
824 EventContent=
"UNKNOWN_EVENTCONTENT"
825 tokens=cmsDriverOptions.split(
"--")
827 keywords=token.split(
" ")
828 if "=" in keywords[0]:
829 keywords=keywords[0].
split(
"=")
830 if "conditions" in keywords[0]:
833 if "auto:" in keywords[1]:
834 from Configuration.AlCa.autoCond
import autoCond
835 fileConditionsOption = autoCond[ keywords[1].
split(
':')[1] ]
836 Conditions = autoCond[ keywords[1].
split(
':')[1] ].
split(
"::")[0]
843 if "," in keywords[1]:
844 Conditions=keywords[1].
split(
",")[1].
split(
"::")[0]
846 Conditions=keywords[1].
split(
"::")[0]
847 elif "pileup" in keywords[0]:
849 elif "eventcontent" in keywords[0]:
850 EventContent=keywords[1]
853 MetaName=acandle+
"___"+stepToWrite+
"___"+PileUp+
"___"+Conditions+
"___"+EventContent+
"___"+IgProfProfile
855 if 'Analyse' not in prof
and (
lambda x:
'Analyse' in x,Profile):
857 simcandles.write(
"%s @@@ %s @@@ %s\n" % (Command,
863 analyse_command = (
"%s @@@ %s @@@ %s\n" % (Command,
864 Profiler[prof].
split(
".")[0]+
'.ANALYSE.'+Profiler[prof].
split(
".")[1],
871 if Profiler[prof] ==
'IgProf_mem.MEM_MAX' or Profiler[prof] ==
'IgProf_perf.PERF_TICKS':
872 analyse_command = analyse_command.replace(
"@@@ reuse",
"")
874 simcandles.write(analyse_command)
876 elif 'Analyse' in prof:
879 simcandles.write(
"%s @@@ %s @@@ %s_%s_%s\n" % (Command,
886 print InputFileOption, step,
'GEN,SIM' in step,
'HLT' in steps[stepIndex - 1], steps
887 print "cmsDriveroptions : " + cmsDriverOption
888 prevPrevOutputFile = previousOutputFile
889 previousOutputFile = OutputFile
891 unprofiledSteps.append(step)
892 isNextStepForProfile =
False
895 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))
899 print "Error: Something is wrong we shouldn't have come this far"
902 if isNextStepForProfile:
904 OutputFile=
writeUnprofiledSteps(simcandles,CustomisePythonFragment,cmsDriverOptions,unprofiledSteps,previousOutputFile,acandle,NumberOfEvents,stepIndex,pileup,bypasshlt)
906 prevPrevOutputFile = previousOutputFile
907 previousOutputFile = OutputFile
910 stepIndex += len(hypsteps)
913 return fstROOTfileStr
915 def writeCommandsToReport(simcandles,Candle,Profile,debug,NumberOfEvents,cmsDriverOptions,steps,bypasshlt,userInputFile):
924 for acandle
in Candle:
925 print '*Candle ' + acandle
958 (NumberOfEvents, ProfileCode, cmsDriverOptions, steps, Candle, bypasshlt, userInputFile ) =
setupProgramParameters(options,args)
965 (CMSSW_BASE, CMSSW_RELEASE_BASE, CMSSW_VERSION ) =
init_vars()
984 writeCommandsToReport(simcandles,Candle,Profile,debug,NumberOfEvents,cmsDriverOptions,steps,bypasshlt,userInputFile)
986 print "Written out cmsRelvalreport.py input file at:\n%s"%os.path.abspath(
'./'+simcandles.name)
990 if __name__ ==
"__main__":
static std::string join(char **cmd)