9 import os,time,subprocess,glob,re,sys
10 from AthenaCommon
import Logging
11 mglog = Logging.logging.getLogger(
'MadGraphUtils')
16 MADGRAPH_GRIDPACK_LOCATION=
'madevent'
18 MADGRAPH_RUN_NAME=
'run_01'
20 MADGRAPH_CATCH_ERRORS=
True
22 MADGRAPH_PDFSETTING=
None
23 MADGRAPH_COMMAND_STACK = []
25 patched_shutil_loc=
'/cvmfs/atlas.cern.ch/repo/sw/Generators/madgraph/models/latest/shutil_patch'
26 if 'PYTHONPATH' in os.environ
and patched_shutil_loc
not in os.environ[
'PYTHONPATH']:
29 os.environ[
'PYTHONPATH'] = patched_shutil_loc+
':'+os.environ[
'PYTHONPATH']
30 MADGRAPH_COMMAND_STACK += [
'export PYTHONPATH='+patched_shutil_loc+
':${PYTHONPATH}']
32 if 'shutil' in sys.modules:
33 sys.modules.pop(
'shutil')
35 sys.path.insert(0,patched_shutil_loc)
38 from MadGraphControl.MadGraphUtilsHelpers
import checkSettingExists,checkSetting,checkSettingIsTrue,getDictFromCard,get_runArgs_info,get_physics_short,is_version_or_newer
39 from MadGraphControl.MadGraphParamHelpers
import do_PMG_updates,check_PMG_updates
42 global MADGRAPH_COMMAND_STACK
43 MADGRAPH_COMMAND_STACK += [
' '.
join(command)]
44 return subprocess.Popen(command,**kwargs)
49 global MADGRAPH_COMMAND_STACK
50 if 'PYTHONPATH' in os.environ:
51 if not any( [(
'Generators/madgraph/models' in x
and 'shutil_patch' not in x)
for x
in os.environ[
'PYTHONPATH'].
split(
':') ]):
52 os.environ[
'PYTHONPATH'] +=
':/cvmfs/atlas.cern.ch/repo/sw/Generators/madgraph/models/latest'
53 MADGRAPH_COMMAND_STACK += [
'export PYTHONPATH=${PYTHONPATH}:/cvmfs/atlas.cern.ch/repo/sw/Generators/madgraph/models/latest']
55 if 'GFORTRAN_TMPDIR' in os.environ:
57 if 'TMPDIR' in os.environ:
58 os.environ[
'GFORTRAN_TMPDIR']=os.environ[
'TMPDIR']
59 MADGRAPH_COMMAND_STACK += [
'export GFORTRAN_TMPDIR=${TMPDIR}']
61 if 'TMP' in os.environ:
62 os.environ[
'GFORTRAN_TMPDIR']=os.environ[
'TMP']
63 MADGRAPH_COMMAND_STACK += [
'export GFORTRAN_TMPDIR=${TMP}']
69 from __main__
import opts
71 mglog.info(
'Athena running on config only mode: not executing MadGraph')
79 global MADGRAPH_COMMAND_STACK
80 if not os.access(
'Cards_bkup',os.R_OK):
81 shutil.copytree(process_dir+
'/Cards',
'Cards_bkup')
82 shutil.copyfile(process_dir+
'/Source/make_opts',
'Cards_bkup/make_opts_bkup')
83 MADGRAPH_COMMAND_STACK += [
'# In case this fails, Cards_bkup should be in your original run directory']
84 MADGRAPH_COMMAND_STACK += [
'# And ${MGaMC_PROCESS_DIR} can be replaced with whatever process directory exists in your stand-alone test']
85 MADGRAPH_COMMAND_STACK += [
'cp '+os.getcwd()+
'/Cards_bkup/*dat ${MGaMC_PROCESS_DIR}/Cards/']
86 MADGRAPH_COMMAND_STACK += [
'cp '+os.getcwd()+
'/Cards_bkup/make_opts_bkup ${MGaMC_PROCESS_DIR}/Source/make_opts']
88 mglog.warning(
'Found Cards_bkup directory existing. Suggests you are either running generation twice (a little funny) or are not using a clean directory.')
90 while os.access(
'Cards_bkup_'+
str(bkup_v),os.R_OK)
and bkup_v<100:
93 shutil.copytree(process_dir+
'/Cards',
'Cards_bkup_'+
str(bkup_v))
94 shutil.copyfile(process_dir+
'/Source/make_opts',
'Cards_bkup_'+
str(bkup_v)+
'/make_opts_bkup')
95 MADGRAPH_COMMAND_STACK += [
'# In case this fails, Cards_bkup should be in your original run directory']
96 MADGRAPH_COMMAND_STACK += [
'# And ${MGaMC_PROCESS_DIR} can be replaced with whatever process directory exists in your stand-alone test']
97 MADGRAPH_COMMAND_STACK += [
'cp '+os.getcwd()+
'/Cards_bkup_'+
str(bkup_v)+
'/*dat ${MGaMC_PROCESS_DIR}/Cards/']
98 MADGRAPH_COMMAND_STACK += [
'cp '+os.getcwd()+
'/Cards_bkup_'+
str(bkup_v)+
'/make_opts_bkup ${MGaMC_PROCESS_DIR}/Source/make_opts']
100 mglog.warning(
'Way too many Cards_bkup* directories found. Giving up -- standalone script may not work.')
104 global MADGRAPH_CATCH_ERRORS
105 if not MADGRAPH_CATCH_ERRORS:
107 unmasked_error =
False
112 if type(errors)==bytes:
113 errors = errors.decode(
'utf-8')
115 mglog.info(
'Some errors detected by MadGraphControl - checking for serious errors')
116 for err
in errors.split(
'\n'):
117 if len(err.strip())==0:
120 if 'Inappropriate ioctl for device' in err:
123 if 'stty: standard input: Invalid argument' in err:
127 if 'PDF already installed' in err:
130 if 'Read-only file system' in err:
137 if 'impossible to set default multiparticles' in err:
141 if 'More information is found in' in err:
142 my_debug_file = err.split(
"'")[1]
143 if err.startswith(
'tar'):
146 if 'python2 support will be removed' in err:
150 if 'required by /lib64/libfontconfig.so' in err
or\
151 'required by /lib64/libgs.so' in err:
154 if 'Error: Symbol' in err
and 'has no IMPLICIT type' in err:
155 bad_variables += [ err.split(
'Symbol ')[1].
split(
' at ')[0] ]
161 unmasked_error =
True
163 if my_debug_file
is None:
164 debug_files = glob.glob(
'*debug.log')+glob.glob(
'*/*debug.log')
165 for debug_file
in debug_files:
167 has_subproc = os.access(os.path.join(os.path.dirname(debug_file),
'SubProcesses'),os.R_OK)
169 my_debug_file = debug_file
172 if my_debug_file
is not None:
173 if not unmasked_error:
174 mglog.warning(
'Found a debug file at '+my_debug_file+
' but no apparent error. Will terminate.')
175 mglog.error(
'MadGraph5_aMC@NLO appears to have crashed. Debug file output follows.')
176 with open(my_debug_file,
'r')
as error_output:
177 for l
in error_output:
178 mglog.error(l.replace(
'\n',
''))
179 mglog.error(
'End of debug file output')
182 mglog.warning(
'Appeared to detect variables in your run card that MadGraph did not understand:')
183 mglog.warning(
' Check your run card / JO settings for %s',bad_variables)
187 mglog.error(f
'Detected a bad return code: {return_code}')
188 unmasked_error =
True
191 if unmasked_error
or my_debug_file
is not None:
193 raise RuntimeError(
'Error detected in MadGraphControl process')
199 mglog.info(
'Will write a stand-alone debugging script.')
200 mglog.info(
'This is an attempt to provide you commands that you can use')
201 mglog.info(
'to reproduce the error locally. If you make additional')
202 mglog.info(
'modifications by hand (not using MadGraphControl) in your JO,')
203 mglog.info(
'make sure that you check and modify the script as needed.\n\n')
204 global MADGRAPH_COMMAND_STACK
205 mglog.info(
'# Script start; trim off columns left of the "#"')
207 with open(
'standalone_script.sh',
'w')
as standalone_script:
208 for command
in MADGRAPH_COMMAND_STACK:
209 for line
in command.split(
'\n'):
211 standalone_script.write(line+
'\n')
212 mglog.info(
'# Script end')
213 mglog.info(
'Script also written to %s/standalone_script.sh',os.getcwd())
216 def new_process(process='generate p p > t t~\noutput -f', plugin=None, keepJpegs=False, usePMGSettings=False):
217 """ Generate a new process in madgraph.
218 Pass a process string.
219 Optionally request JPEGs to be kept and request for PMG settings to be used in the param card
220 Return the name of the process directory.
225 os.makedirs(
'dummy_proc/Cards')
232 return MADGRAPH_GRIDPACK_LOCATION
235 card_loc=
'proc_card_mg5.dat'
236 mglog.info(
'Writing process card to '+card_loc)
237 a_card =
open( card_loc ,
'w' )
238 for l
in process.split(
'\n'):
239 if 'output' not in l:
241 elif '-nojpeg' in l
or keepJpegs:
244 a_card.write(l.split(
'#')[0]+
' -nojpeg #'+l.split(
'#')[1]+
'\n')
246 a_card.write(l+
' -nojpeg\n')
249 madpath=os.environ[
'MADPATH']
255 for l
in process.split(
'\n'):
257 if 'output' not in l.split(
'#')[0].
split():
260 tmplist = l.split(
'#')[0].
split(
' -')[0]
262 if len(tmplist.split())==2:
263 process_dir = tmplist.split()[1]
265 elif len(tmplist.split())==3:
266 process_dir = tmplist.split()[2]
269 mglog.info(
'Saw that you asked for a special output directory: '+
str(process_dir))
272 mglog.info(
'Started process generation at '+
str(time.asctime()))
274 plugin_cmd =
'--mode='+plugin
if plugin
is not None else ''
277 global MADGRAPH_COMMAND_STACK
278 MADGRAPH_COMMAND_STACK += [
'# All jobs should start in a clean directory']
279 MADGRAPH_COMMAND_STACK += [
'mkdir standalone_test; cd standalone_test']
280 MADGRAPH_COMMAND_STACK += [
' '.
join([python,madpath+
'/bin/mg5_aMC '+plugin_cmd+
' << EOF\n'+process+
'\nEOF\n'])]
281 global MADGRAPH_CATCH_ERRORS
282 generate = subprocess.Popen([python,madpath+
'/bin/mg5_aMC',plugin_cmd,card_loc],stdin=subprocess.PIPE,stderr=subprocess.PIPE
if MADGRAPH_CATCH_ERRORS
else None)
283 (out,err) = generate.communicate()
286 mglog.info(
'Finished process generation at '+
str(time.asctime()))
289 if process_dir ==
'':
290 for adir
in sorted(glob.glob( os.getcwd()+
'/*PROC*' ),reverse=
True):
291 if os.access(
'%s/SubProcesses/subproc.mg'%adir,os.R_OK):
295 mglog.warning(
'Additional possible process directory, '+adir+
' found. Had '+process_dir)
296 mglog.warning(
'Likely this is because you did not run from a clean directory, and this may cause errors later.')
298 if not os.access(
'%s/SubProcesses/subproc.mg'%process_dir,os.R_OK):
299 raise RuntimeError(
'No diagrams for this process in user-define dir='+
str(process_dir))
301 raise RuntimeError(
'No diagrams for this process from list: '+
str(
sorted(glob.glob(os.getcwd()+
'/*PROC*'),reverse=
True)))
304 needed_options = [
'ninja',
'collier',
'fastjet',
'lhapdf',
'syscalc_path']
305 in_config =
open(os.environ[
'MADPATH']+
'/input/mg5_configuration.txt',
'r')
307 for l
in in_config.readlines():
308 for o
in needed_options:
309 if o+
' =' in l.split(
'#')[0]
and 'MCGenerators' in l.split(
'#')[0]:
310 old_path = l.split(
'#')[0].
split(
'=')[1].strip().
split(
'MCGenerators')[1]
311 old_path = old_path[ old_path.find(
'/') : ]
312 if o ==
'lhapdf' and 'LHAPATH' in os.environ:
314 version = os.environ[
'LHAPATH'].
split(
'lhapdf/')[1].
split(
'/')[0]
315 old_version = old_path.split(
'lhapdf/')[1].
split(
'/')[0]
316 old_path = old_path.replace(old_version,version)
319 old_path.replace(
'gosam_contrib',
'gosam-contrib')
320 option_paths[o] = os.environ[
'MADPATH'].
split(
'madgraph5amc')[0]+old_path
322 if o+
' =' in l
and o+
' =' not in l.split(
'#')[0]:
323 mglog.info(
'Option '+o+
' appears commented out in the config file')
326 for o
in needed_options:
327 if o
not in option_paths:
328 mglog.warning(
'Path for option '+o+
' not found in original config')
330 mglog.info(
'Modifying config paths to avoid use of afs:')
331 mglog.info(option_paths)
343 mglog.info(
'Setting default sde_strategy to old default (1)')
344 my_settings = {
'sde_strategy':1}
345 modify_run_card(process_dir=process_dir,settings=my_settings,skipBaseFragment=
True)
348 MADGRAPH_COMMAND_STACK += [
'export MGaMC_PROCESS_DIR='+os.path.basename(process_dir)]
354 """ Copy the default runcard from one of several locations
355 to a local file with name run_card.tmp.dat"""
356 output_name =
'run_card.tmp.dat'
358 mglog.info(
'Athena running on config only mode: grabbing run card the old way, as there will be no proc dir')
359 mglog.info(
'Fetching default LO run_card.dat')
360 if os.access(os.environ[
'MADPATH']+
'/Template/LO/Cards/run_card.dat',os.R_OK):
361 shutil.copy(os.environ[
'MADPATH']+
'/Template/LO/Cards/run_card.dat',output_name)
362 return 'run_card.dat'
363 elif os.access(os.environ[
'MADPATH']+
'/Template/Cards/run_card.dat',os.R_OK):
364 shutil.copy(os.environ[
'MADPATH']+
'/Template/Cards/run_card.dat',output_name)
367 raise RuntimeError(
'Cannot find default LO run_card.dat!')
370 run_card=process_dir+
'/Cards/run_card.dat'
371 if os.access(run_card,os.R_OK):
372 mglog.info(
'Copying default run_card.dat from '+
str(run_card))
373 shutil.copy(run_card,output_name)
376 run_card=process_dir+
'/Cards/run_card_default.dat'
377 mglog.info(
'Fetching default run_card.dat from '+
str(run_card))
378 if os.access(run_card,os.R_OK):
379 shutil.copy(run_card,output_name)
382 raise RuntimeError(
'Cannot find default run_card.dat or run_card_default.dat! I was looking here: %s'%run_card)
385 def generate(process_dir='PROC_mssm_0', grid_pack=False, gridpack_compile=False, extlhapath=None, required_accuracy=0.01, runArgs=None, bias_module=None, requirePMGSettings=False):
395 if 'ATHENA_PROC_NUMBER' in os.environ
and int(os.environ[
'ATHENA_PROC_NUMBER'])>0:
396 njobs =
int(os.environ[
'ATHENA_PROC_NUMBER'])
397 mglog.info(
'Lucky you - you are running on a full node queue. Will re-configure for '+
str(njobs)+
' jobs.')
401 if cluster_type
is not None:
405 mglog.info(
'Running event generation from gridpack (using smarter mode from generate() function)')
406 generate_from_gridpack(runArgs=runArgs,extlhapath=extlhapath,gridpack_compile=gridpack_compile,requirePMGSettings=requirePMGSettings)
409 mglog.info(
'Did not identify an input gridpack.')
411 mglog.info(
'The grid_pack flag is set, so I am expecting to create a gridpack in this job')
425 from distutils.spawn
import find_executable
426 if find_executable(
'f2py')
is not None:
427 mglog.info(
'Found f2py, will use it for reweighting')
429 raise RuntimeError(
'Could not find f2py, needed for reweighting')
434 mglog.info(
'Started generating gridpack at '+
str(time.asctime()))
435 mglog.warning(
' >>>>>> THIS KIND OF JOB SHOULD ONLY BE RUN LOCALLY - NOT IN GRID JOBS <<<<<<')
438 my_settings = {
'nevents':
'1000'}
440 my_settings[
'req_acc']=
str(required_accuracy)
442 my_settings = {
'gridpack':
'true'}
443 modify_run_card(process_dir=process_dir,settings=my_settings,skipBaseFragment=
True)
447 mglog.info(
'Started generating at '+
str(time.asctime()))
449 mglog.info(
'Run '+MADGRAPH_RUN_NAME+
' will be performed in mode '+
str(mode)+
' with '+
str(njobs)+
' jobs in parallel.')
452 if not os.access(process_dir,os.R_OK):
453 raise RuntimeError(
'No process directory found at '+process_dir)
454 if not os.access(process_dir+
'/bin/generate_events',os.R_OK):
455 raise RuntimeError(
'No generate_events module found in '+process_dir)
457 mglog.info(
'For your information, the libraries available are (should include LHAPDF):')
458 ls_dir(process_dir+
'/lib')
461 if bias_module
is not None:
464 mglog.info(
'Now I will hack the make files a bit. Apologies, but there seems to be no good way around this.')
465 shutil.copyfile(process_dir+
'/Source/make_opts',process_dir+
'/Source/make_opts_old')
466 old_opts =
open(process_dir+
'/Source/make_opts_old',
'r')
467 new_opts =
open(process_dir+
'/Source/make_opts',
'w')
468 for aline
in old_opts:
470 mglog.info(
'Configuring the fancy gfortran compiler instead of g77 / f77')
471 new_opts.write(
' FC=gfortran\n')
472 elif 'FFLAGS+= -ffixed-line-length-132' in aline
and 'i686' in os.environ[
'CMTCONFIG']:
473 mglog.info(
'Setting you up for a 32-bit compilation')
474 new_opts.write(
'FFLAGS+= -ffixed-line-length-132 -m32\n')
476 new_opts.write(aline)
479 mglog.info(
'Make file hacking complete.')
483 os.chdir(process_dir)
485 global MADGRAPH_COMMAND_STACK
486 MADGRAPH_COMMAND_STACK += [
'cd ${MGaMC_PROCESS_DIR}' ]
497 if requirePMGSettings
and code!=0:
498 raise RuntimeError(
'Settings are not compliant with PMG defaults! Please use do_PMG_updates function to get PMG default params.')
502 command = [python,
'bin/generate_events']
504 command += [
'--name='+MADGRAPH_RUN_NAME]
505 mglog.info(
'Removing Cards/shower_card.dat to ensure we get parton level events only')
506 os.unlink(
'Cards/shower_card.dat')
508 command += [MADGRAPH_RUN_NAME]
510 setNCores(process_dir=os.getcwd(), Ncores=njobs)
513 mglog.info(
'Setting up cluster running')
515 if cluster_type==
'pbs':
516 mglog.info(
'Modifying bin/internal/cluster.py for PBS cluster running')
517 os.system(
"sed -i \"s:text += prog:text += './'+prog:g\" bin/internal/cluster.py")
519 mglog.info(
'Setting up multi-core running on '+os.environ[
'ATHENA_PROC_NUMBER']+
' cores')
521 mglog.info(
'Setting up serial generation.')
524 global MADGRAPH_CATCH_ERRORS
525 generate =
stack_subprocess(command,stdin=subprocess.PIPE, stderr=subprocess.PIPE
if MADGRAPH_CATCH_ERRORS
else None)
526 (out,err) = generate.communicate()
531 MADGRAPH_COMMAND_STACK += [
'cd -' ]
535 energy =
'%1.1f'%(beamEnergy*2./1000.)
536 energy = energy.replace(
'.0',
'').
replace(
'.',
'p')
538 mglog.info(
'Tidying up gridpack '+gridpack_name)
542 MADGRAPH_COMMAND_STACK += [
'cp '+glob.glob(process_dir+
'/'+MADGRAPH_RUN_NAME+
'_*gridpack.tar.gz')[0]+
' '+gridpack_name]
543 shutil.copy(glob.glob(process_dir+
'/'+MADGRAPH_RUN_NAME+
'_*gridpack.tar.gz')[0],gridpack_name)
546 MADGRAPH_COMMAND_STACK += [
'mkdir tmp%i/'%os.getpid(),
'cd tmp%i/'%os.getpid()]
547 os.mkdir(
'tmp%i/'%os.getpid())
548 os.chdir(
'tmp%i/'%os.getpid())
549 mglog.info(
'untar gridpack')
552 mglog.info(
'compile and clean up')
553 MADGRAPH_COMMAND_STACK += [
'cd madevent']
554 os.chdir(
'madevent/')
555 compilep =
stack_subprocess([
'./bin/compile'],stderr=subprocess.PIPE
if MADGRAPH_CATCH_ERRORS
else None)
556 (out,err) = compilep.communicate()
558 clean =
stack_subprocess([
'./bin/clean4grid'],stderr=subprocess.PIPE
if MADGRAPH_CATCH_ERRORS
else None)
559 (out,err) = clean.communicate()
562 MADGRAPH_COMMAND_STACK += [
'cd ..',
'rm ../'+gridpack_name]
564 mglog.info(
'remove old tarball')
565 os.unlink(
'../'+gridpack_name)
566 mglog.info(
'Package up new tarball')
567 tar =
stack_subprocess([
'tar',
'cvzf',
'../'+gridpack_name,
'--exclude=SubProcesses/P*/G*/*_results.dat',
'--exclude=SubProcesses/P*/G*/*.log',
'--exclude=SubProcesses/P*/G*/*.txt',
'.'])
569 MADGRAPH_COMMAND_STACK += [
'cd ..',
'rm -r tmp%i/'%os.getpid()]
571 mglog.info(
'Remove temporary directory')
572 shutil.rmtree(
'tmp%i/'%os.getpid())
573 mglog.info(
'Tidying up complete!')
578 mglog.info(
'Package up process_dir')
579 MADGRAPH_COMMAND_STACK += [
'mv '+process_dir+
' '+MADGRAPH_GRIDPACK_LOCATION]
580 os.rename(process_dir,MADGRAPH_GRIDPACK_LOCATION)
581 tar =
stack_subprocess([
'tar',
'czf',gridpack_name,MADGRAPH_GRIDPACK_LOCATION,
'--exclude=Events/*/*events*gz',
'--exclude=SubProcesses/P*/G*/log*txt',
'--exclude=SubProcesses/P*/G*/events.lhe*',
'--exclude=*/*.o',
'--exclude=*/*/*.o',
'--exclude=*/*/*/*.o',
'--exclude=*/*/*/*/*.o'])
583 MADGRAPH_COMMAND_STACK += [
'mv '+MADGRAPH_GRIDPACK_LOCATION+
' '+process_dir]
584 os.rename(MADGRAPH_GRIDPACK_LOCATION,process_dir)
586 mglog.info(
'Gridpack sucessfully created, exiting the transform')
587 if hasattr(runArgs,
'outputTXTFile'):
588 mglog.info(
'Touching output TXT (LHE) file for the transform')
589 open(runArgs.outputTXTFile,
'w').close()
590 from AthenaCommon.AppMgr
import theApp
594 mglog.info(
'Finished at '+
str(time.asctime()))
606 isNLO=
is_NLO_run(process_dir=MADGRAPH_GRIDPACK_LOCATION)
611 gridpack_run_name =
'GridRun_'+
str(random_seed)
614 if os.access(MADGRAPH_GRIDPACK_LOCATION+
'/Cards/madspin_card.dat',os.R_OK):
615 os.rename(MADGRAPH_GRIDPACK_LOCATION+
'/Cards/madspin_card.dat',MADGRAPH_GRIDPACK_LOCATION+
'/Cards/backup_madspin_card.dat')
625 if requirePMGSettings
and code!=0:
626 raise RuntimeError(
'Settings are not compliant with PMG defaults! Please use do_PMG_updates function to get PMG default params.')
629 settings={
'iseed':
str(random_seed)}
631 settings[
'python_seed']=
str(random_seed)
632 modify_run_card(process_dir=MADGRAPH_GRIDPACK_LOCATION,settings=settings,skipBaseFragment=
True)
634 mglog.info(
'Generating events from gridpack')
637 if not os.path.exists(MADGRAPH_GRIDPACK_LOCATION):
638 raise RuntimeError(
'Gridpack directory not found at '+MADGRAPH_GRIDPACK_LOCATION)
640 nevents =
getDictFromCard(MADGRAPH_GRIDPACK_LOCATION+
'/Cards/run_card.dat')[
'nevents']
641 mglog.info(
'>>>> FOUND GRIDPACK <<<< <- This will be used for generation')
642 mglog.info(
'Generation of '+
str(
int(nevents))+
' events will be performed using the supplied gridpack with random seed '+
str(random_seed))
643 mglog.info(
'Started generating events at '+
str(time.asctime()))
646 if os.access(MADGRAPH_GRIDPACK_LOCATION+
'/bin/internal/addmasses.py',os.R_OK):
647 os.remove(MADGRAPH_GRIDPACK_LOCATION+
'/bin/internal/addmasses.py')
652 setNCores(process_dir=MADGRAPH_GRIDPACK_LOCATION)
653 global MADGRAPH_CATCH_ERRORS
657 ls_dir(MADGRAPH_GRIDPACK_LOCATION)
667 run_card_dict=
getDictFromCard(MADGRAPH_GRIDPACK_LOCATION+
'/Cards/run_card.dat')
668 systematics_settings=
None
669 if checkSetting(
'systematics_program',
'systematics',run_card_dict):
671 raise RuntimeError(
'Trying to run NLO systematics but reweight info not stored')
673 systematics_settings=MadGraphControl.MadGraphSystematicsUtils.parse_systematics_arguments(run_card_dict[
'systematics_arguments'])
675 systematics_settings={}
676 mglog.info(
'Turning off systematics for now, running standalone later')
677 modify_run_card(process_dir=MADGRAPH_GRIDPACK_LOCATION,settings={
'systematics_program':
'none'},skipBaseFragment=
True)
679 global MADGRAPH_COMMAND_STACK
682 if not os.access(MADGRAPH_GRIDPACK_LOCATION+
'/bin/gridrun',os.R_OK):
683 mglog.error(
'/bin/gridrun not found at '+MADGRAPH_GRIDPACK_LOCATION)
684 raise RuntimeError(
'Could not find gridrun executable')
686 mglog.info(
'Found '+MADGRAPH_GRIDPACK_LOCATION+
'/bin/gridrun, starting generation.')
689 mglog.info(
"Now generating {} events with random seed {} and granularity {}".
format(
int(nevents),
int(random_seed),granularity))
691 new_ld_path=
":".
join([os.environ[
'LD_LIBRARY_PATH'],os.getcwd()+
'/'+MADGRAPH_GRIDPACK_LOCATION+
'/madevent/lib',os.getcwd()+
'/'+MADGRAPH_GRIDPACK_LOCATION+
'/HELAS/lib'])
692 os.environ[
'LD_LIBRARY_PATH']=new_ld_path
693 MADGRAPH_COMMAND_STACK+=[
"export LD_LIBRARY_PATH="+
":".
join([
'${LD_LIBRARY_PATH}',new_ld_path])]
694 generate =
stack_subprocess([python,MADGRAPH_GRIDPACK_LOCATION+
'/bin/gridrun',
str(
int(nevents)),
str(
int(random_seed)),
str(granularity)],stdin=subprocess.PIPE,stderr=subprocess.PIPE
if MADGRAPH_CATCH_ERRORS
else None)
695 (out,err) = generate.communicate()
697 gp_events=MADGRAPH_GRIDPACK_LOCATION+
"/Events/GridRun_{}/unweighted_events.lhe.gz".
format(
int(random_seed))
698 if not os.path.exists(gp_events):
699 mglog.error(
'Error in gp generation, did not find events at '+gp_events)
703 if reweight_card
is not None:
704 pythonpath_backup=os.environ[
'PYTHONPATH']
706 os.environ[
'PYTHONPATH']=
':'.
join([p
for p
in pythonpath_backup.split(
':')
if 'madgraph5amc' not in p])
708 os.environ[
'PYTHONPATH']=pythonpath_backup
710 shutil.move(gp_events,
'events.lhe.gz')
714 if not os.access(MADGRAPH_GRIDPACK_LOCATION+
'/bin/generate_events',os.R_OK):
715 raise RuntimeError(
'Could not find generate_events executable at '+MADGRAPH_GRIDPACK_LOCATION)
717 mglog.info(
'Found '+MADGRAPH_GRIDPACK_LOCATION+
'/bin/generate_events, starting generation.')
719 ls_dir(MADGRAPH_GRIDPACK_LOCATION+
'/Events/')
720 if os.access(MADGRAPH_GRIDPACK_LOCATION+
'/Events/'+gridpack_run_name, os.F_OK):
721 mglog.info(
'Removing '+MADGRAPH_GRIDPACK_LOCATION+
'/Events/'+gridpack_run_name+
' directory from gridpack generation')
722 MADGRAPH_COMMAND_STACK += [
'rm -rf '+MADGRAPH_GRIDPACK_LOCATION+
'/Events/'+gridpack_run_name]
723 shutil.rmtree(MADGRAPH_GRIDPACK_LOCATION+
'/Events/'+gridpack_run_name)
726 if os.access(MADGRAPH_GRIDPACK_LOCATION+
'/Events/'+gridpack_run_name+
'_decayed_1', os.F_OK):
727 mglog.info(
'Removing '+MADGRAPH_GRIDPACK_LOCATION+
'/Events/'+gridpack_run_name+
'_decayed_1 directory from gridpack generation')
728 MADGRAPH_COMMAND_STACK += [
'rm -rf '+MADGRAPH_GRIDPACK_LOCATION+
'/Events/'+gridpack_run_name+
'_decayed_1']
729 shutil.rmtree(MADGRAPH_GRIDPACK_LOCATION+
'/Events/'+gridpack_run_name+
'_decayed_1')
731 ls_dir(MADGRAPH_GRIDPACK_LOCATION+
'/Events/')
733 if not gridpack_compile:
734 mglog.info(
'Copying make_opts from Template')
735 shutil.copy(os.environ[
'MADPATH']+
'/Template/LO/Source/make_opts',MADGRAPH_GRIDPACK_LOCATION+
'/Source/')
738 generate =
stack_subprocess([python,MADGRAPH_GRIDPACK_LOCATION+
'/bin/generate_events',
'--parton',
'--nocompile',
'--only_generation',
'-f',
'--name='+gridpack_run_name],stdin=subprocess.PIPE,stderr=subprocess.PIPE
if MADGRAPH_CATCH_ERRORS
else None)
739 (out,err) = generate.communicate()
742 mglog.info(
'Allowing recompilation of gridpack')
743 if os.path.islink(MADGRAPH_GRIDPACK_LOCATION+
'/lib/libLHAPDF.a'):
744 mglog.info(
'Unlinking '+MADGRAPH_GRIDPACK_LOCATION+
'/lib/libLHAPDF.a')
745 os.unlink(MADGRAPH_GRIDPACK_LOCATION+
'/lib/libLHAPDF.a')
748 generate =
stack_subprocess([python,MADGRAPH_GRIDPACK_LOCATION+
'/bin/generate_events',
'--parton',
'--only_generation',
'-f',
'--name='+gridpack_run_name],stdin=subprocess.PIPE,stderr=subprocess.PIPE
if MADGRAPH_CATCH_ERRORS
else None)
749 (out,err) = generate.communicate()
751 if isNLO
and systematics_settings
is not None:
753 mglog.info(
'Running systematics standalone')
754 systematics_path=MADGRAPH_GRIDPACK_LOCATION+
'/bin/internal/systematics.py'
755 events_location=MADGRAPH_GRIDPACK_LOCATION+
'/Events/'+gridpack_run_name+
'/events.lhe.gz'
756 syst_cmd=[python,systematics_path]+[events_location]*2+[
"--"+k+
"="+systematics_settings[k]
for k
in systematics_settings]
757 mglog.info(
'running: '+
' '.
join(syst_cmd))
763 if not os.access(
'events.lhe.gz',os.R_OK):
764 mglog.info(
'Copying generated events to '+currdir)
765 if not os.path.exists(MADGRAPH_GRIDPACK_LOCATION+
'Events/'+gridpack_run_name):
766 shutil.copy(MADGRAPH_GRIDPACK_LOCATION+
'/Events/'+gridpack_run_name+
'/events.lhe.gz',
'events.lhe.gz')
768 mglog.info(
'Events were already in place')
772 mglog.info(
'Moving generated events to be in correct format for arrange_output().')
773 mglog.info(
'Unzipping generated events.')
777 mglog.info(
'Moving file over to '+MADGRAPH_GRIDPACK_LOCATION+
'/Events/'+gridpack_run_name+
'/unweighted_events.lhe')
778 mkdir =
stack_subprocess([
'mkdir',
'-p',(MADGRAPH_GRIDPACK_LOCATION+
'/Events/'+gridpack_run_name)])
780 shutil.move(
'events.lhe',MADGRAPH_GRIDPACK_LOCATION+
'/Events/'+gridpack_run_name+
'/unweighted_events.lhe')
782 mglog.info(
'Re-zipping into dataset name '+MADGRAPH_GRIDPACK_LOCATION+
'/Events/'+gridpack_run_name+
'/unweighted_events.lhe.gz')
783 rezip =
stack_subprocess([
'gzip',MADGRAPH_GRIDPACK_LOCATION+
'/Events/'+gridpack_run_name+
'/unweighted_events.lhe'])
791 os.rename(MADGRAPH_GRIDPACK_LOCATION+
'/Cards/backup_madspin_card.dat',MADGRAPH_GRIDPACK_LOCATION+
'/Cards/madspin_card.dat')
792 mglog.info(
'Decaying with MadSpin.')
793 add_madspin(process_dir=MADGRAPH_GRIDPACK_LOCATION)
795 mglog.info(
'Finished at '+
str(time.asctime()))
804 mglog.info(
'Path to fastjet install dir: '+os.environ[
'FASTJETPATH'])
805 fastjetconfig = os.environ[
'FASTJETPATH']+
'/bin/fastjet-config'
807 mglog.info(
'fastjet-config --version: '+
str(subprocess.Popen([fastjetconfig,
'--version'],stdout = subprocess.PIPE).stdout.read().strip()))
808 mglog.info(
'fastjet-config --prefix: '+
str(subprocess.Popen([fastjetconfig,
'--prefix'],stdout = subprocess.PIPE).stdout.read().strip()))
811 config_card=process_dir+
'/Cards/me5_configuration.txt'
813 config_card=process_dir+
'/Cards/amcatnlo_configuration.txt'
815 oldcard =
open(config_card,
'r')
816 newcard =
open(config_card+
'.tmp',
'w')
819 if 'fastjet = ' in line:
820 newcard.write(
'fastjet = '+fastjetconfig+
'\n')
821 mglog.info(
'Setting fastjet = '+fastjetconfig+
' in '+config_card)
826 shutil.move(config_card+
'.tmp',config_card)
838 for p
in os.environ[
'LHAPATH'].
split(
':')+os.environ[
'LHAPDF_DATA_PATH'].
split(
':'):
839 if os.path.exists(p+
"/../../lib/")
and LHAPATH
is None:
841 for p
in os.environ[
'LHAPDF_DATA_PATH'].
split(
':')+os.environ[
'LHAPATH'].
split(
':'):
842 if os.path.exists(p)
and LHADATAPATH
is None and p!=LHAPATH:
844 if LHADATAPATH
is None:
847 mglog.error(
'Could not find path to LHAPDF installation')
848 return LHAPATH,LHADATAPATH
856 pdflist =
open(LHADATAPATH+
'/pdfsets.index',
'r')
857 if isinstance(pdf,int)
or pdf.isdigit():
861 splitline=line.split()
862 if int(splitline[0]) == pdfid:
868 splitline=line.split()
869 if splitline[1] == pdfname:
870 pdfid=
int(splitline[0])
875 err=
'Couldn\'t find PDF name associated to ID %i in %s.'%(pdfid,LHADATAPATH+
'/pdfsets.index')
877 raise RuntimeError(err)
879 err=
'Couldn\'t find PDF ID associated to name %s in %s.'%(pdfname,LHADATAPATH+
'/pdfsets.index')
881 raise RuntimeError(err)
886 def setupLHAPDF(process_dir=None, extlhapath=None, allow_links=True):
890 origLHAPATH=os.environ[
'LHAPATH']
891 origLHAPDF_DATA_PATH=os.environ[
'LHAPDF_DATA_PATH']
899 run_card=process_dir+
'/Cards/run_card.dat'
902 if mydict[
"pdlabel"].
replace(
"'",
"") ==
'lhapdf':
904 mglog.info(
'creating local LHAPDF dir: MGC_LHAPDF/')
905 if os.path.islink(
'MGC_LHAPDF/'):
906 os.unlink(
'MGC_LHAPDF/')
907 elif os.path.isdir(
'MGC_LHAPDF/'):
908 shutil.rmtree(
'MGC_LHAPDF/')
910 newMGCLHA=
'MGC_LHAPDF/'
912 mkdir = subprocess.Popen([
'mkdir',
'-p',newMGCLHA])
915 pdfs_used=[
int(x)
for x
in mydict[
'lhaid'].
replace(
' ',
',').
split(
',') ]
917 if 'sys_pdf' in mydict:
923 pdfs_used.append(idx)
926 if 'systematics_arguments' in mydict:
927 systematics_arguments=MadGraphControl.MadGraphSystematicsUtils.parse_systematics_arguments(mydict[
'systematics_arguments'])
928 if 'pdf' in systematics_arguments:
934 pdfs_used.append(idx)
937 for pdf
in pdfs_used:
938 if isinstance(pdf,str)
and (pdf.lower()==
'errorset' or pdf.lower()==
'central'):
942 mglog.info(
"Found LHAPDF ID="+
str(pdfid)+
", name="+pdfname)
944 if not os.path.exists(newMGCLHA+pdfname)
and not os.path.lexists(newMGCLHA+pdfname):
945 if not os.path.exists(LHADATAPATH+
'/'+pdfname):
946 mglog.warning(
'PDF not installed at '+LHADATAPATH+
'/'+pdfname)
948 mglog.info(
'linking '+LHADATAPATH+
'/'+pdfname+
' --> '+newMGCLHA+pdfname)
949 os.symlink(LHADATAPATH+
'/'+pdfname,newMGCLHA+pdfname)
951 mglog.info(
'copying '+LHADATAPATH+
'/'+pdfname+
' --> '+newMGCLHA+pdfname)
952 shutil.copytree(LHADATAPATH+
'/'+pdfname,newMGCLHA+pdfname)
955 mglog.info(
'linking '+LHADATAPATH+
'/pdfsets.index --> '+newMGCLHA+
'pdfsets.index')
956 os.symlink(LHADATAPATH+
'/pdfsets.index',newMGCLHA+
'pdfsets.index')
958 atlasLHADATAPATH=LHADATAPATH.replace(
'sft.cern.ch/lcg/external/lhapdfsets/current',
'atlas.cern.ch/repo/sw/Generators/lhapdfsets/current')
959 mglog.info(
'linking '+atlasLHADATAPATH+
'/lhapdf.conf --> '+newMGCLHA+
'lhapdf.conf')
960 os.symlink(atlasLHADATAPATH+
'/lhapdf.conf',newMGCLHA+
'lhapdf.conf')
962 mglog.info(
'copying '+LHADATAPATH+
'/pdfsets.index --> '+newMGCLHA+
'pdfsets.index')
963 shutil.copy2(LHADATAPATH+
'/pdfsets.index',newMGCLHA+
'pdfsets.index')
965 atlasLHADATAPATH=LHADATAPATH.replace(
'sft.cern.ch/lcg/external/lhapdfsets/current',
'atlas.cern.ch/repo/sw/Generators/lhapdfsets/current')
966 mglog.info(
'copying '+atlasLHADATAPATH+
'/lhapdf.conf -->'+newMGCLHA+
'lhapdf.conf')
967 shutil.copy2(atlasLHADATAPATH+
'/lhapdf.conf',newMGCLHA+
'lhapdf.conf')
970 LHADATAPATH=os.getcwd()+
'/MGC_LHAPDF'
973 mglog.info(
'Not using LHAPDF')
974 return (LHAPATH,origLHAPATH,origLHAPDF_DATA_PATH)
978 os.environ[
'LHAPDF_DATA_PATH']=LHADATAPATH
980 mglog.info(
'Path to LHAPDF install dir: '+LHAPATH)
981 mglog.info(
'Path to LHAPDF data dir: '+LHADATAPATH)
982 if not os.path.isdir(LHADATAPATH):
983 raise RuntimeError(
'LHAPDF data dir not accesible: '+LHADATAPATH)
984 if not os.path.isdir(LHAPATH):
985 raise RuntimeError(
'LHAPDF path dir not accesible: '+LHAPATH)
989 lhapdfconfig=extlhapath
990 if not os.access(lhapdfconfig,os.X_OK):
991 raise RuntimeError(
'Failed to find valid external lhapdf-config at '+lhapdfconfig)
992 LHADATAPATH=subprocess.Popen([lhapdfconfig,
'--datadir'],stdout = subprocess.PIPE).stdout.read().strip()
993 mglog.info(
'Changing LHAPDF_DATA_PATH to '+LHADATAPATH)
994 os.environ[
'LHAPDF_DATA_PATH']=LHADATAPATH
996 getlhaconfig = subprocess.Popen([
'get_files',
'-data',
'lhapdf-config'])
999 if not os.access(os.getcwd()+
'/lhapdf-config',os.X_OK):
1000 mglog.error(
'Failed to get lhapdf-config from MadGraphControl')
1002 lhapdfconfig = os.getcwd()+
'/lhapdf-config'
1004 mglog.info(
'lhapdf-config --version: '+
str(subprocess.Popen([lhapdfconfig,
'--version'],stdout = subprocess.PIPE).stdout.read().strip()))
1005 mglog.info(
'lhapdf-config --prefix: '+
str(subprocess.Popen([lhapdfconfig,
'--prefix'],stdout = subprocess.PIPE).stdout.read().strip()))
1006 mglog.info(
'lhapdf-config --libdir: '+
str(subprocess.Popen([lhapdfconfig,
'--libdir'],stdout = subprocess.PIPE).stdout.read().strip()))
1007 mglog.info(
'lhapdf-config --datadir: '+
str(subprocess.Popen([lhapdfconfig,
'--datadir'],stdout = subprocess.PIPE).stdout.read().strip()))
1008 mglog.info(
'lhapdf-config --pdfsets-path: '+
str(subprocess.Popen([lhapdfconfig,
'--pdfsets-path'],stdout = subprocess.PIPE).stdout.read().strip()))
1010 modify_config_card(process_dir=process_dir,settings={
'lhapdf':lhapdfconfig,
'lhapdf_py3':lhapdfconfig})
1012 mglog.info(
'Creating links for LHAPDF')
1013 if os.path.islink(process_dir+
'/lib/PDFsets'):
1014 os.unlink(process_dir+
'/lib/PDFsets')
1015 elif os.path.isdir(process_dir+
'/lib/PDFsets'):
1016 shutil.rmtree(process_dir+
'/lib/PDFsets')
1018 os.symlink(LHADATAPATH,process_dir+
'/lib/PDFsets')
1020 shutil.copytree(LHADATAPATH,process_dir+
'/lib/PDFsets')
1021 mglog.info(
'Available PDFs are:')
1022 mglog.info(
sorted( [ x
for x
in os.listdir(process_dir+
'/lib/PDFsets')
if ".tar.gz" not in x ] ) )
1024 global MADGRAPH_COMMAND_STACK
1025 MADGRAPH_COMMAND_STACK += [
'# Copy the LHAPDF files locally' ]
1026 MADGRAPH_COMMAND_STACK += [
'cp -r '+os.getcwd()+
'/MGC_LHAPDF .' ]
1027 MADGRAPH_COMMAND_STACK += [
'cp -r '+process_dir+
'/lib/PDFsets ${MGaMC_PROCESS_DIR}/lib/' ]
1029 return (LHAPATH,origLHAPATH,origLHAPDF_DATA_PATH)
1035 my_runMode = 2
if 'ATHENA_PROC_NUMBER' in os.environ
else 0
1036 if Ncores
is None and 'ATHENA_PROC_NUMBER' in os.environ
and int(os.environ[
'ATHENA_PROC_NUMBER'])>0:
1037 my_Ncores =
int(os.environ[
'ATHENA_PROC_NUMBER'])
1039 if my_Ncores
is None:
1040 mglog.info(
'Setting up for serial run')
1043 modify_config_card(process_dir=process_dir,settings={
'nb_core':my_Ncores,
'run_mode':my_runMode,
'automatic_html_opening':
'False'})
1047 mglog.info(
'Restoring original LHAPDF env variables:')
1048 os.environ[
'LHAPATH']=origLHAPATH
1049 os.environ[
'LHAPDF_DATA_PATH']=origLHAPDF_DATA_PATH
1050 mglog.info(
'LHAPATH='+os.environ[
'LHAPATH'])
1051 mglog.info(
'LHAPDF_DATA_PATH='+os.environ[
'LHAPDF_DATA_PATH'])
1055 madpath=os.environ[
'MADPATH']
1056 if not os.access(madpath+
'/bin/mg5_aMC',os.R_OK):
1057 raise RuntimeError(
'mg5_aMC executable not found in '+madpath)
1058 return madpath+
'/bin/mg5_aMC'
1062 """ Add lifetimes to the generated LHE file. Should be
1063 called after generate_events is called.
1070 if len(glob.glob(process_dir+
'/Events/*'))<1:
1071 mglog.error(
'Process dir '+process_dir+
' does not contain events?')
1072 run = glob.glob(process_dir+
'/Events/*')[0].
split(
'/')[-1]
1077 tof_c =
open(
'time_of_flight_exec_card',
'w')
1078 tof_c.write(
'launch '+process_dir+
''' -i
1079 add_time_of_flight '''+run+((
' --threshold='+
str(threshold))
if threshold
is not None else ''))
1082 mglog.info(
'Started adding time of flight info '+
str(time.asctime()))
1084 global MADGRAPH_CATCH_ERRORS
1085 generate =
stack_subprocess([python,me_exec,
'time_of_flight_exec_card'],stdin=subprocess.PIPE,stderr=subprocess.PIPE
if MADGRAPH_CATCH_ERRORS
else None)
1086 (out,err) = generate.communicate()
1089 mglog.info(
'Finished adding time of flight information at '+
str(time.asctime()))
1092 lhe_gz = glob.glob(process_dir+
'/Events/*/*lhe.gz')[0]
1093 if not os.access(lhe_gz,os.R_OK):
1094 mglog.info(
'LHE file needs to be zipped')
1095 lhe = glob.glob(process_dir+
'/Events/*/*lhe.gz')[0]
1098 mglog.info(
'Zipped')
1100 mglog.info(
'LHE file zipped by MadGraph automatically. Nothing to do')
1105 def add_madspin(madspin_card=None,process_dir=MADGRAPH_GRIDPACK_LOCATION):
1106 """ Run madspin on the generated LHE file. Should be
1107 run when you have inputGeneratorFile set.
1108 Only requires a simplified process with the same model that you are
1109 interested in (needed to set up a process directory for MG5_aMC)
1116 if madspin_card
is not None:
1117 shutil.copyfile(madspin_card,process_dir+
'/Cards/madspin_card.dat')
1119 if len(glob.glob(process_dir+
'/Events/*'))<1:
1120 mglog.error(
'Process dir '+process_dir+
' does not contain events?')
1121 proc_dir_list = glob.glob(process_dir+
'/Events/*')
1123 for adir
in proc_dir_list:
1124 if 'GridRun_' in adir:
1125 run=adir.split(
'/')[-1]
1128 run=proc_dir_list[0].
split(
'/')[-1]
1133 ms_c =
open(
'madspin_exec_card',
'w')
1134 ms_c.write(
'launch '+process_dir+
''' -i
1135 decay_events '''+run)
1138 mglog.info(
'Started running madspin at '+
str(time.asctime()))
1140 global MADGRAPH_CATCH_ERRORS
1141 generate =
stack_subprocess([python,me_exec,
'madspin_exec_card'],stdin=subprocess.PIPE,stderr=subprocess.PIPE
if MADGRAPH_CATCH_ERRORS
else None)
1142 (out,err) = generate.communicate()
1144 if len(glob.glob(process_dir+
'/Events/'+run+
'_decayed_*/')) == 0:
1145 mglog.error(
'No '+process_dir+
'/Events/'+run+
'_decayed_*/ can be found')
1146 raise RuntimeError(
'Problem while running MadSpin')
1148 mglog.info(
'Finished running madspin at '+
str(time.asctime()))
1151 lhe_gz = glob.glob(process_dir+
'/Events/*/*lhe.gz')[0]
1152 if not os.access(lhe_gz,os.R_OK):
1153 mglog.info(
'LHE file needs to be zipped')
1154 lhe = glob.glob(process_dir+
'/Events/*/*lhe.gz')[0]
1157 mglog.info(
'Zipped')
1159 mglog.info(
'LHE file zipped by MadGraph automatically. Nothing to do')
1163 """ Run MadSpin on an input LHE file. Takes the process
1164 from the LHE file, so you don't need to have a process directory
1165 set up in advance. Runs MadSpin and packs the LHE file up appropriately
1166 Needs runArgs for the file handling"""
1167 if not os.access(input_LHE,os.R_OK):
1168 raise RuntimeError(
'Could not find LHE file '+input_LHE)
1169 if not os.access(madspin_card,os.R_OK):
1170 raise RuntimeError(
'Could not find input MadSpin card '+madspin_card)
1172 shutil.copy(input_LHE,input_LHE+
'.original')
1173 mglog.info(
'Put backup copy of LHE file at '+input_LHE+
'.original')
1175 madspin_exec_card =
open(
'madspin_exec_card',
'w')
1176 madspin_exec_card.write(
'import '+input_LHE+
'\n')
1178 input_madspin_card =
open(madspin_card,
'r')
1180 for l
in input_madspin_card.readlines():
1181 commands = l.split(
'#')[0].
split()
1183 if len(commands)>1
and 'import'==commands[0]
and not 'model'==commands[1]:
1186 if len(commands)>0
and 'launch' == commands[0]:
1188 madspin_exec_card.write(l.strip()+
'\n')
1190 madspin_exec_card.write(
'launch\n')
1191 madspin_exec_card.close()
1192 input_madspin_card.close()
1194 madpath=os.environ[
'MADPATH']
1195 if not os.access(madpath+
'/MadSpin/madspin',os.R_OK):
1196 raise RuntimeError(
'madspin executable not found in '+madpath)
1197 mglog.info(
'Starting madspin at '+
str(time.asctime()))
1198 global MADGRAPH_CATCH_ERRORS
1199 generate =
stack_subprocess([python,madpath+
'/MadSpin/madspin',
'madspin_exec_card'],stdin=subprocess.PIPE,stderr=subprocess.PIPE
if MADGRAPH_CATCH_ERRORS
else None)
1200 (out,err) = generate.communicate()
1202 mglog.info(
'Done with madspin at '+
str(time.asctime()))
1206 if os.path.exists(os.getcwd()+
'/events.lhe'):
1207 os.remove(os.getcwd()+
'/events.lhe')
1209 mglog.info(
'Unzipping generated events.')
1213 mglog.info(
'Putting a copy in place for the transform.')
1214 mod_output =
open(os.getcwd()+
'/events.lhe',
'w')
1218 with open(input_LHE,
'r')
as fileobject:
1219 for line
in fileobject:
1221 mod_output.write(line)
1226 mglog.info(
'Removed '+
str(nEmpty)+
' empty lines from LHEF')
1230 raise RuntimeError(
'Must provide runArgs to madspin_on_lhe')
1232 outputDS = runArgs.outputTXTFile
if hasattr(runArgs,
'outputTXTFile')
else 'tmp_LHE_events'
1234 mglog.info(
'Moving file over to '+outputDS.split(
'.tar.gz')[0]+
'.events')
1235 shutil.move(os.getcwd()+
'/events.lhe',outputDS.split(
'.tar.gz')[0]+
'.events')
1237 mglog.info(
'Re-zipping into dataset name '+outputDS)
1238 rezip =
stack_subprocess([
'tar',
'cvzf',outputDS,outputDS.split(
'.tar.gz')[0]+
'.events'])
1242 if hasattr(runArgs,
'outputTXTFile')
and runArgs.outputTXTFile
is not None:
1243 outputDS = outputDS.split(
'.TXT')[0]
1245 if runArgs
is not None:
1246 mglog.debug(
'Setting inputGenerator file to '+outputDS)
1247 runArgs.inputGeneratorFile=outputDS
1250 def arrange_output(process_dir=MADGRAPH_GRIDPACK_LOCATION,lhe_version=None,saveProcDir=False,runArgs=None,fixEventWeightsForBridgeMode=False):
1257 if len(glob.glob(os.path.join(process_dir,
'Events',
'*')))<1:
1258 mglog.error(
'Process dir '+process_dir+
' does not contain events?')
1259 proc_dir_list = glob.glob(os.path.join(process_dir,
'Events',
'*'))
1262 for adir
in proc_dir_list:
1263 if 'decayed' in adir:
1266 if 'GridRun_' in adir:
1269 elif os.path.join(process_dir,
'Events',MADGRAPH_RUN_NAME)
in adir:
1271 if not os.access(this_run_name,os.R_OK):
1272 raise RuntimeError(
'Unable to locate run directory')
1274 hasUnweighted = os.access(this_run_name+
'/unweighted_events.lhe.gz',os.R_OK)
1277 madspinDirs=
sorted(glob.glob(this_run_name+
'_decayed_*/'))
1278 if len(madspinDirs):
1280 if hasRunMadSpin
and not hasUnweighted:
1282 hasUnweighted = os.access(madspinDirs[-1]+
'/unweighted_events.lhe.gz',os.R_OK)
1284 global MADGRAPH_COMMAND_STACK
1286 if len(madspinDirs):
1293 if os.path.exists(madspinDirs[-1]+
'/unweighted_events.lhe.gz'):
1294 MADGRAPH_COMMAND_STACK += [
'mv '+madspinDirs[-1]+
'/unweighted_events.lhe.gz'+
' '+this_run_name+
'/unweighted_events.lhe.gz']
1295 shutil.move(madspinDirs[-1]+
'/unweighted_events.lhe.gz',this_run_name+
'/unweighted_events.lhe.gz')
1296 mglog.info(
'Moving MadSpin events from '+madspinDirs[-1]+
'/unweighted_events.lhe.gz to '+this_run_name+
'/unweighted_events.lhe.gz')
1297 elif os.path.exists(madspinDirs[-1]+
'/events.lhe.gz'):
1298 MADGRAPH_COMMAND_STACK += [
'mv '+madspinDirs[-1]+
'/events.lhe.gz'+
' '+this_run_name+
'/unweighted_events.lhe.gz']
1299 shutil.move(madspinDirs[-1]+
'/events.lhe.gz',this_run_name+
'/unweighted_events.lhe.gz')
1300 mglog.info(
'Moving MadSpin events from '+madspinDirs[-1]+
'/events.lhe.gz to '+this_run_name+
'/unweighted_events.lhe.gz')
1302 raise RuntimeError(
'MadSpin was run but can\'t find files :(')
1305 MADGRAPH_COMMAND_STACK += [
'mv '+madspinDirs[-1]+
'/events.lhe.gz '+this_run_name+
'/events.lhe.gz']
1306 shutil.move(madspinDirs[-1]+
'/events.lhe.gz',this_run_name+
'/events.lhe.gz')
1307 mglog.info(
'Moving MadSpin events from '+madspinDirs[-1]+
'/events.lhe.gz to '+this_run_name+
'/events.lhe.gz')
1310 mglog.error(
'MadSpin was run but can\'t find output folder '+(this_run_name+
'_decayed_1/'))
1311 raise RuntimeError(
'MadSpin was run but can\'t find output folder '+(this_run_name+
'_decayed_1/'))
1313 if fixEventWeightsForBridgeMode:
1314 mglog.info(
"Fixing event weights after MadSpin... initial checks.")
1322 eventsfilename=
"unweighted_events"
1324 eventsfilename=
"events"
1325 unzip =
stack_subprocess([
'gunzip',
'-f',this_run_name+
'/%s.lhe.gz' % eventsfilename])
1328 for line
in open(process_dir+
'/Events/'+MADGRAPH_RUN_NAME+
'/%s.lhe'%eventsfilename):
1329 if "Number of Events" in line:
1331 MGnumevents=
int(sline[-1])
1332 elif "Integrated weight (pb)" in line:
1334 MGintweight=
float(sline[-1])
1335 elif "set spinmode none" in line:
1337 elif "</header>" in line:
1340 if spinmodenone
and MGnumevents>0
and MGintweight>0:
1341 mglog.info(
"Fixing event weights after MadSpin... modifying LHE file.")
1342 newlhe=
open(this_run_name+
'/%s_fixXS.lhe'%eventsfilename,
'w')
1350 event_norm_setting=
"average"
1352 for line
in open(this_run_name+
'/%s.lhe'%eventsfilename):
1355 if "<init>" in line:
1358 elif "</init>" in line:
1360 elif inInit
and initlinecount==0:
1364 if abs(
int(sline[-2])) == 3:
1365 event_norm_setting=
"sum"
1366 elif abs(
int(sline[-2])) == 4:
1367 event_norm_setting=
"average"
1368 elif inInit
and initlinecount==1:
1372 sline[0]=
str(MGintweight)
1373 sline[1]=
str(
float(sline[0])*relunc)
1374 if event_norm_setting==
"sum":
1375 sline[2]=
str(MGintweight/MGnumevents)
1376 elif event_norm_setting==
"average":
1377 sline[2]=
str(MGintweight)
1378 newline=
' '.
join(sline)
1381 elif inInit
and initlinecount>1:
1383 elif "<event>" in line:
1386 elif "</event>" in line:
1388 elif inEvent
and eventlinecount==0:
1391 if event_norm_setting==
"sum":
1392 sline[2]=
str(MGintweight/MGnumevents)
1393 elif event_norm_setting==
"average":
1394 sline[2]=
str(MGintweight)
1395 newline=
' '.
join(sline)
1398 newlhe.write(newline)
1401 mglog.info(
"Fixing event weights after MadSpin... cleaning up.")
1402 shutil.copyfile(this_run_name+
'/%s.lhe' % eventsfilename,
1403 this_run_name+
'/%s_badXS.lhe' % eventsfilename)
1405 shutil.move(this_run_name+
'/%s_fixXS.lhe' % eventsfilename,
1406 this_run_name+
'/%s.lhe' % eventsfilename)
1408 rezip =
stack_subprocess([
'gzip',this_run_name+
'/%s.lhe' % eventsfilename])
1411 rezip =
stack_subprocess([
'gzip',this_run_name+
'/%s_badXS.lhe' % eventsfilename])
1415 if os.path.exists(os.getcwd()+
'/events.lhe'):
1416 os.remove(os.getcwd()+
'/events.lhe')
1418 mglog.info(
'Unzipping generated events.')
1420 unzip =
stack_subprocess([
'gunzip',
'-f',this_run_name+
'/unweighted_events.lhe.gz'])
1426 mglog.info(
'Putting a copy in place for the transform.')
1428 orig_input = this_run_name+
'/unweighted_events.lhe'
1429 mod_output =
open(os.getcwd()+
'/events.lhe',
'w')
1431 orig_input = this_run_name+
'/events.lhe'
1432 mod_output =
open(os.getcwd()+
'/events.lhe',
'w')
1439 with open(orig_input,
'r')
as fileobject:
1440 for line
in fileobject:
1444 if '#' not in newline:
1446 elif '>' not in newline[ newline.find(
'#'): ]:
1449 mglog.warning(
'Found bad LHE line with an XML mark in a comment: "'+newline.strip()+
'"')
1450 newline=newline[:newline.find(
'#')]+
'#'+ (newline[newline.find(
'#'):].
replace(
'>',
'-'))
1452 if initrwgt
is False:
1454 elif "</initrwgt>" in newline:
1456 elif "<initrwgt>" in newline:
1458 elif initrwgt
is not None:
1459 newline=newline.replace(
'_DYNSCALE-1',
'')
1460 if '</weight>' in newline:
1461 iend=newline.find(
'</weight>')
1462 istart=newline[:iend].rfind(
'>')
1463 lhe_weights+=[newline[istart+1:iend].strip()]
1464 mod_output.write(newline)
1468 mglog.info(
'Removed '+
str(nEmpty)+
' empty lines from LHEF')
1470 mglog.info(
"The following "+
str(len(lhe_weights))+
" weights have been written to the LHE file: "+
",".
join(lhe_weights))
1473 mglog.info(
"Checking whether the following expected weights are in LHE file: "+
",".
join(expected_weights))
1474 for w
in expected_weights:
1475 if w
not in lhe_weights:
1476 raise RuntimeError(
"Did not find expected weight "+w+
" in lhe file. Did the reweight or systematics module crash?")
1477 mglog.info(
"Found all required weights!")
1480 mod_output2 =
open(os.getcwd()+
'/events.lhe',
'r')
1481 test=mod_output2.readline()
1482 if 'version="' in test:
1483 mglog.info(
'Applying LHE version hack')
1484 final_file =
open(os.getcwd()+
'/events.lhe.copy',
'w')
1485 final_file.write(
'<LesHouchesEvents version="%i.0">\n'%lhe_version)
1486 shutil.copyfileobj(mod_output2, final_file)
1488 shutil.copy(os.getcwd()+
'/events.lhe.copy',os.getcwd()+
'/events.lhe')
1490 os.remove(os.getcwd()+
'/events.lhe.copy')
1495 raise RuntimeError(
'Must provide runArgs to arrange_output')
1497 if hasattr(runArgs,
'outputTXTFile'):
1498 outputDS = runArgs.outputTXTFile
1500 outputDS =
'tmp_LHE_events'
1502 mglog.info(
'Moving file over to '+outputDS.split(
'.tar.gz')[0]+
'.events')
1504 shutil.move(os.getcwd()+
'/events.lhe',outputDS.split(
'.tar.gz')[0]+
'.events')
1506 mglog.info(
'Re-zipping into dataset name '+outputDS)
1507 rezip =
stack_subprocess([
'tar',
'cvzf',outputDS,outputDS.split(
'.tar.gz')[0]+
'.events'])
1511 mglog.info(
'Removing the process directory')
1512 shutil.rmtree(process_dir,ignore_errors=
True)
1514 if os.path.isdir(
'MGC_LHAPDF/'):
1515 shutil.rmtree(
'MGC_LHAPDF/',ignore_errors=
True)
1518 if hasattr(runArgs,
'outputTXTFile')
and runArgs.outputTXTFile
is not None:
1519 outputDS = outputDS.split(
'.TXT')[0]
1521 if runArgs
is not None:
1522 mglog.debug(
'Setting inputGenerator file to '+outputDS)
1523 runArgs.inputGeneratorFile=outputDS
1525 mglog.info(
'All done with output arranging!')
1529 if reweight_card_loc
is None:
1532 f_rw=
open(reweight_card_loc)
1534 if 'launch' not in line:
1536 match=re.match(
r'launch.*--rwgt_info\s*=\s*(\S+).*',line.strip())
1537 if len(match.groups())!=1:
1538 raise RuntimeError(
'Unexpected format of reweight card in line'+line)
1540 names+=[match.group(1)]
1546 if syst_setting
is None or 'central_pdf' not in syst_setting:
1547 mglog.warning(
"Systematics have not been defined via base fragment or 'MADGRAPH_PDFSETTING', cannot check for expected weights")
1549 if 'pdf_variations' in syst_setting
and isinstance(syst_setting[
'pdf_variations'],list):
1550 names+=[MadGraphControl.MadGraphSystematicsUtils.SYSTEMATICS_WEIGHT_INFO%{
'mur':1.0,
'muf':1.0,
'pdf':syst_setting[
'central_pdf']}]
1551 for pdf
in syst_setting[
'pdf_variations']:
1552 names+=[MadGraphControl.MadGraphSystematicsUtils.SYSTEMATICS_WEIGHT_INFO%{
'mur':1.0,
'muf':1.0,
'pdf':pdf+1}]
1553 if 'alternative_pdfs' in syst_setting
and isinstance(syst_setting[
'alternative_pdfs'],list):
1554 for pdf
in syst_setting[
'alternative_pdfs']:
1555 names+=[MadGraphControl.MadGraphSystematicsUtils.SYSTEMATICS_WEIGHT_INFO%{
'mur':1.0,
'muf':1.0,
'pdf':pdf}]
1556 if 'scale_variations' in syst_setting
and isinstance(syst_setting[
'scale_variations'],list):
1557 for mur
in syst_setting[
'scale_variations']:
1558 for muf
in syst_setting[
'scale_variations']:
1559 names+=[MadGraphControl.MadGraphSystematicsUtils.SYSTEMATICS_WEIGHT_INFO%{
'mur':mur,
'muf':muf,
'pdf':syst_setting[
'central_pdf']}]
1563 run_card = process_dir+
'/Cards/run_card.dat'
1564 if isinstance(bias_module,tuple):
1565 mglog.info(
'Using bias module '+bias_module[0])
1566 the_run_card =
open(run_card,
'r')
1567 for line
in the_run_card:
1568 if 'bias_module' in line
and not bias_module[0]
in line:
1569 raise RuntimeError(
'You need to add the bias module '+bias_module[0]+
' to the run card to actually run it')
1570 the_run_card.close()
1571 if len(bias_module)!=3:
1572 raise RuntimeError(
'Please give a 3-tuple of strings containing bias module name, bias module, and makefile. Alternatively, give path to bias module tarball.')
1573 bias_module_newpath=process_dir+
'/Source/BIAS/'+bias_module[0]
1574 os.makedirs(bias_module_newpath)
1575 bias_module_file=
open(bias_module_newpath+
'/'+bias_module[0]+
'.f',
'w')
1576 bias_module_file.write(bias_module[1])
1577 bias_module_file.close()
1578 bias_module_make_file=
open(bias_module_newpath+
'/Makefile',
'w')
1579 bias_module_make_file.write(bias_module[2])
1580 bias_module_make_file.close()
1582 mglog.info(
'Using bias module '+bias_module)
1583 bias_module_name=bias_module.split(
'/')[-1].
replace(
'.gz',
'')
1584 bias_module_name=bias_module_name.replace(
'.tar',
'')
1585 the_run_card =
open(run_card,
'r')
1586 for line
in the_run_card:
1587 if 'bias_module' in line
and bias_module_name
not in line:
1588 raise RuntimeError(
'You need to add the bias module '+bias_module_name+
' to the run card to actually run it')
1589 the_run_card.close()
1591 if os.path.exists(bias_module+
'.tar.gz'):
1592 bias_module_path=bias_module+
'.tar.gz'
1593 elif os.path.exists(bias_module+
'.gz'):
1594 bias_module_path=bias_module+
'.gz'
1595 elif os.path.exists(bias_module):
1596 bias_module_path=bias_module
1598 mglog.error(
'Did not find bias module '+bias_module+
' , this path should point to folder or tarball. Alternatively give a tuple of strings containing module name, module, and makefile')
1600 bias_module_newpath=process_dir+
'/Source/BIAS/'+bias_module_path.split(
'/')[-1]
1601 mglog.info(
'Copying bias module into place: '+bias_module_newpath)
1602 shutil.copy(bias_module_path,bias_module_newpath)
1603 mglog.info(
'Unpacking bias module')
1604 if bias_module_newpath.endswith(
'.tar.gz'):
1605 untar =
stack_subprocess([
'tar',
'xvzf',bias_module_newpath,
'--directory='+process_dir+
'/Source/BIAS/'])
1607 elif bias_module_path.endswith(
'.gz'):
1613 if os.access(process_dir+
'/Cards/reweight_card.dat',os.R_OK):
1614 return process_dir+
'/Cards/reweight_card.dat'
1620 shutil.move(reweight_card,reweight_card+
'.old')
1621 oldcard =
open(reweight_card+
'.old',
'r')
1622 newcard =
open(reweight_card,
'w')
1624 info_expression=
r'launch.*--rwgt_info\s*=\s*(\S+).*'
1625 name_expression=info_expression.replace(
'info',
'name')
1626 goodname_expression=
r'^[A-Za-z0-9_\-.]+$'
1627 for line
in oldcard:
1629 if not line.strip().startswith(
'launch') :
1632 rwgt_name_match=re.match(name_expression,line.strip())
1633 rwgt_info_match=re.match(info_expression,line.strip())
1634 if rwgt_name_match
is None and rwgt_info_match
is None:
1635 raise RuntimeError(
'Every reweighting should have a --rwgt_info (see https://cp3.irmp.ucl.ac.be/projects/madgraph/wiki/Reweight), please update your reweight_card accordingly. Line to fix: '+line)
1636 for match
in [rwgt_info_match,rwgt_name_match]:
1639 if len(match.groups())!=1:
1640 raise RuntimeError(
'Unexpected format of reweight card in line: '+line)
1641 if not re.match(goodname_expression,match.group(1)):
1642 raise RuntimeError(
'No special character in reweighting info/name, only allowing '+goodname_expression)
1643 if rwgt_info_match
is not None:
1645 elif rwgt_name_match
is not None:
1646 newcard.write(line.strip()+
' --rwgt_info={0}\n'.
format(rwgt_name_match.group(1)))
1649 mglog.info(
'Updated reweight_card')
1656 # Define multiparticle labels
1657 define p = g u c d s u~ c~ d~ s~
1658 define j = g u c d s u~ c~ d~ s~
1659 define pb = g u c d s b u~ c~ d~ s~ b~
1660 define jb = g u c d s b u~ c~ d~ s~ b~
1663 define vl = ve vm vt
1664 define vl~ = ve~ vm~ vt~
1665 define fu = u c e+ mu+ ta+
1666 define fu~ = u~ c~ e- mu- ta-
1667 define fd = d s ve~ vm~ vt~
1668 define fd~ = d~ s~ ve vm vt
1669 define susystrong = go ul ur dl dr cl cr sl sr t1 t2 b1 b2 ul~ ur~ dl~ dr~ cl~ cr~ sl~ sr~ t1~ t2~ b1~ b2~
1670 define susyweak = el- el+ er- er+ mul- mul+ mur- mur+ ta1- ta1+ ta2- ta2+ n1 n2 n3 n4 x1- x1+ x2- x2+ sve sve~ svm svm~ svt svt~
1671 define susylq = ul ur dl dr cl cr sl sr
1672 define susylq~ = ul~ ur~ dl~ dr~ cl~ cr~ sl~ sr~
1673 define susysq = ul ur dl dr cl cr sl sr t1 t2 b1 b2
1674 define susysq~ = ul~ ur~ dl~ dr~ cl~ cr~ sl~ sr~ t1~ t2~ b1~ b2~
1675 define susysl = el- el+ er- er+ mul- mul+ mur- mur+ ta1- ta1+ ta2- ta2+
1676 define susyv = sve svm svt
1677 define susyv~ = sve~ svm~ svt~
1683 if ktdurham
is None:
1685 if process
is not None:
1686 id_map = {
'go':
'1000021',
'dl':
'1000001',
'ul':
'1000002',
'sl':
'1000003',
'cl':
'1000004',
'b1':
'1000005',
't1':
'1000006',
1687 'dr':
'2000001',
'ur':
'2000002',
'sr':
'2000003',
'cr':
'2000004',
'b2':
'2000005',
't2':
'2000006',
1688 'n1':
'1000022',
'n2':
'1000023',
'x1':
'1000024',
'x2':
'1000037',
'n3':
'1000025',
'n4':
'1000035',
1689 'el':
'1000011',
'mul':
'1000013',
'ta1':
'1000015',
'sve':
'1000012',
'svm':
'1000014',
'svt':
'1000016',
1690 'er':
'2000011',
'mur':
'2000013',
'ta2':
'2000015'}
1692 if 'generate' in l
or 'add process' in l:
1694 for particle
in clean_proc.split():
1695 if particle
not in id_map:
1696 mglog.info(f
'Particle {particle} not found in PDG ID map - skipping')
1698 prod_particles += id_map[particle]
1702 if len(prod_particles)>0:
1703 for x
in prod_particles:
1705 my_mass =
min(my_mass,abs(
float(masses[x])))
1707 mglog.info(f
'Seem to ask for production of PDG ID {x}, but {x} not in mass dictionary?')
1709 strong_ids = [
'1000001',
'1000002',
'1000003',
'1000004',
'1000005',
'1000006',
'2000001',
'2000002',
'2000003',
'2000004',
'2000005',
'2000006',
'1000021']
1710 weak_ids = [
'1000023',
'1000024',
'1000025',
'1000011',
'1000013',
'1000015',
'2000011',
'2000013',
'2000015',
'1000012',
'1000014',
'1000016']
1712 my_mass =
min([abs(
float(masses[x]))
for x
in strong_ids
if x
in masses])
1716 my_mass =
max([abs(
float(masses[x]))
for x
in weak_ids
if x
in masses
and float(masses[x])<10000.])
1718 if my_mass>10000.
and '1000022' in masses:
1719 my_mass = masses[
'1000022']
1721 raise RuntimeError(
'Could not understand which mass to use for matching cut in '+
str(masses))
1724 ktdurham =
min(my_mass*0.25,500)
1726 ktdurham =
max(ktdurham,15)
1727 if syst_mod
is not None and 'qup' in syst_mod.lower():
1728 ktdurham = ktdurham*2.
1729 elif syst_mod
is not None and 'qdown' in syst_mod.lower():
1730 ktdurham = ktdurham*0.5
1732 mglog.info(
'For matching, will use ktdurham of '+
str(ktdurham))
1736 if syst_mod
is not None and 'alpsfactup' in syst_mod.lower():
1738 elif syst_mod
is not None and 'alpsfactdown' in syst_mod.lower():
1741 if syst_mod
is not None and 'scalefactup' in syst_mod.lower():
1743 elif syst_mod
is not None and 'scalefactdown' in syst_mod.lower():
1746 return abs(ktdurham) , alpsfact , scalefact
1751 if 'import model' in process:
1752 mglog.info(
'Assuming that you have specified the model in your process string already')
1754 for l
in process.split(
'\n'):
1755 if 'import model' in l:
1759 if 'MSSM_SLHA2' in full_proc:
1761 for l
in process.split(
'\n'):
1762 if 'import model' not in l:
1765 # Output processes to MadEvent directory
1770 # Specify process(es) to run
1773 # Output processes to MadEvent directory
1780 syst_mod=None, keepOutput=False, param_card=None, writeGridpack=False,\
1781 madspin_card=None, run_settings={}, params={}, fixEventWeightsForBridgeMode=False, add_lifetimes_lhe=False, usePMGSettings=True):
1785 usePMGSettings (bool): See :py:func:`new_process`. Will set SM parameters to the appropriate values. Default: True.
1787 ktdurham = run_settings[
'ktdurham']
if 'ktdurham' in run_settings
else None
1788 ktdurham , alpsfact , scalefact =
get_SUSY_variations( process, params[
'MASS'] , syst_mod , ktdurham=ktdurham )
1790 process_dir = MADGRAPH_GRIDPACK_LOCATION
1793 process_dir =
new_process(full_proc, plugin=plugin, usePMGSettings=usePMGSettings)
1794 mglog.info(
'Using process directory '+
str(process_dir))
1797 modify_param_card(param_card_input=param_card,process_dir=process_dir,params=params)
1800 settings = {
'ktdurham':ktdurham,
'scalefact':scalefact,
'alpsfact':alpsfact}
1801 settings.update(run_settings)
1804 modify_run_card(process_dir=process_dir,runArgs=runArgs,settings=settings)
1807 if madspin_card
is not None:
1808 if not os.access(madspin_card,os.R_OK):
1809 raise RuntimeError(
'Could not locate madspin card at '+
str(madspin_card))
1810 shutil.copy(madspin_card,process_dir+
'/Cards/madspin_card.dat')
1817 generate(runArgs=runArgs,process_dir=process_dir,grid_pack=writeGridpack)
1820 if add_lifetimes_lhe :
1821 mglog.info(
'Requested addition of lifetimes to LHE files: doing so now.')
1828 arrange_output(process_dir=process_dir,saveProcDir=keepOutput,runArgs=runArgs,fixEventWeightsForBridgeMode=fixEventWeightsForBridgeMode)
1830 mglog.info(
'All done generating events!!')
1831 return settings[
'ktdurham']
1834 def update_lhe_file(lhe_file_old,param_card_old=None,lhe_file_new=None,masses={},delete_old_lhe=True):
1835 """Build a new LHE file from an old one and an updated param card.
1836 The masses of some particles can be changed via the masses dictionary. No particles that appear in the events
1837 may have their masses changed.
1838 If the param card is provided, the decay block in the LHE file will be replaced with the one in the param card.
1839 By default, the old LHE file is removed.
1840 If None is provided as a new LHE file name, the new file will replace the old one."""
1842 lhe_file_new_tmp = lhe_file_new
if lhe_file_new
is not None else lhe_file_old+
'.tmp'
1844 if not os.access(lhe_file_old,os.R_OK):
1845 raise RuntimeError(
'Could not access old LHE file at '+
str(lhe_file_old)+
'. Please check the file location.')
1847 if param_card_old
is not None:
1848 paramcard = subprocess.Popen([
'get_files',
'-data',param_card_old])
1850 if not os.access(param_card_old,os.R_OK):
1851 raise RuntimeError(
'Could not get param card '+param_card_old)
1853 if os.access(lhe_file_new_tmp,os.R_OK):
1854 raise RuntimeError(
'Old file at'+
str(lhe_file_new_tmp)+
' in the current directory. Dont want to clobber it. Please move it first.')
1856 newlhe =
open(lhe_file_new_tmp,
'w')
1860 particles_in_events = []
1863 with open(lhe_file_old,
'r')
as fileobject:
1864 for line
in fileobject:
1865 if decayEdit
and '</slha>' not in line:
1867 if decayEdit
and '</slha>' in line:
1869 if line.strip().
upper().startswith(
'BLOCK')
or line.strip().
upper().startswith(
'DECAY')\
1870 and len(line.strip().
split()) > 1:
1871 pos = 0
if line.strip().startswith(
'DECAY')
else 1
1872 blockName = line.strip().
upper().
split()[pos]
1875 if blockName !=
'DECAY' and len(line.strip().
split()) > 0:
1876 akey = line.strip().
split()[0]
1877 elif blockName ==
'DECAY' and len(line.strip().
split()) > 1:
1878 akey = line.strip().
split()[1]
1881 if akey
is not None and blockName ==
'MASS' and akey
in masses:
1882 newlhe.write(
' '+akey+
' '+
str(masses[akey])+
' # \n')
1883 mglog.info(
' '+akey+
' '+
str(masses[akey])+
' #')
1888 if blockName ==
'DECAY' and param_card_old
is not None:
1890 oldparam =
open(param_card_old,
'r')
1892 for old_line
in oldparam.readlines():
1894 if old_line.strip().
upper().startswith(
'DECAY')
and len(old_line.strip().
split()) > 1:
1895 newBlockName = line.strip().
upper().
split()[pos]
1897 newlhe.write(old_line)
1898 elif newBlockName ==
'DECAY':
1900 newlhe.write(old_line)
1908 if not eventRead
and '<event>' in line:
1911 if len(line.split())==11:
1912 aparticle = line.split()[0]
1913 if aparticle
not in particles_in_events:
1914 particles_in_events += [aparticle]
1921 if akey
in particles_in_events:
1922 mglog.error(
'Attempted to change mass of a particle that was in an LHE event! This is not allowed!')
1929 if lhe_file_new
is None:
1930 os.remove(lhe_file_old)
1931 shutil.move(lhe_file_new_tmp,lhe_file_old)
1932 lhe_file_new_tmp = lhe_file_old
1934 elif delete_old_lhe:
1935 os.remove(lhe_file_old)
1937 return lhe_file_new_tmp
1941 """Update the PDG IDs used in an LHE file. This is a bit finicky, as we have to
1942 both touch the LHE file metadata _and_ modify the events themselves. But since this
1943 is "just" a remapping, it should be safe assuming Pythia8 is told the correct thing
1944 afterwards and can get the showering right."""
1946 lhe_file_new_tmp = lhe_file_new
if lhe_file_new
is not None else lhe_file_old+
'.tmp'
1948 if not os.access(lhe_file_old,os.R_OK):
1949 raise RuntimeError(
'Could not access old LHE file at '+
str(lhe_file_old)+
'. Please check the file location.')
1952 pdgid_map_str = {
str(x) :
str(pdgid_map[x])
for x
in pdgid_map }
1954 pdgid_map_str.update( {
'-'+
str(x) :
'-'+
str(pdgid_map[x])
for x
in pdgid_map
if '-'+
str(x)
not in pdgid_map } )
1956 newlhe =
open(lhe_file_new_tmp,
'w')
1959 with open(lhe_file_old,
'r')
as fileobject:
1960 for line
in fileobject:
1962 if line.strip().
upper().startswith(
'BLOCK')
or line.strip().
upper().startswith(
'DECAY')\
1963 and len(line.strip().
split()) > 1:
1964 pos = 0
if line.strip().startswith(
'DECAY')
else 1
1965 blockName = line.strip().
upper().
split()[pos]
1966 elif '</slha>' in line:
1969 if len(line.split(
'#')[0].strip())==0:
1971 for pdgid
in pdgid_map_str:
1972 if pdgid
in line_mod.split():
1973 line_mod = line_mod.replace( pdgid , pdgid_map_str[pdgid] )
1974 newlhe.write(line_mod)
1977 if blockName==
'MASS' and line.split()[0]
in pdgid_map_str:
1978 newlhe.write( line.replace( line.split()[0] , pdgid_map_str[ line.split()[0] ] , 1 ) )
1980 if blockName==
'DECAY' and line.split()[1]
in pdgid_map_str:
1981 newlhe.write( line.replace( line.split()[1] , pdgid_map_str[ line.split()[1] ] , 1 ) )
1983 if blockName==
'QNUMBERS' and line.split()[2]
in pdgid_map_str:
1984 newlhe.write( line.replace( line.split()[2] , pdgid_map_str[ line.split()[2] ] , 1 ) )
1986 if '<event>' in line:
1988 if eventRead
and len(line.split())==13
and line.split()[0]
in pdgid_map_str:
1989 newlhe.write( line.replace( line.split()[0] , pdgid_map_str[ line.split()[0] ] , 1 ) )
1996 if lhe_file_new
is None:
1997 os.remove(lhe_file_old)
1998 shutil.move(lhe_file_new_tmp,lhe_file_old)
1999 lhe_file_new_tmp = lhe_file_old
2001 elif delete_old_lhe:
2002 os.remove(lhe_file_old)
2004 return lhe_file_new_tmp
2008 """ Helper function when looking at param cards
2009 In some cases it's tricky to match keys - they may differ
2010 only in white space. This tries to sort out when we have
2011 a match, and then uses the one in blockParams afterwards.
2012 In the case of no match, it returns the original key.
2015 for key
in dictionary:
2017 if mod_key==test_key:
2022 def modify_param_card(param_card_input=None,param_card_backup=None,process_dir=MADGRAPH_GRIDPACK_LOCATION,params={},output_location=None):
2023 """Build a new param_card.dat from an existing one.
2024 Params should be a dictionary of dictionaries. The first key is the block name, and the second in the param name.
2025 Keys can include MASS (for masses) and DECAY X (for decays of particle X)"""
2029 if param_card_input
is None:
2030 param_card_input=process_dir+
'/Cards/param_card.dat'
2031 elif param_card_input
is not None and not os.access(param_card_input,os.R_OK):
2032 paramcard = subprocess.Popen([
'get_files',
'-data',param_card_input])
2034 if not os.access(param_card_input,os.R_OK):
2035 raise RuntimeError(
'Could not get param card '+param_card_input)
2036 mglog.info(
'Using input param card at '+param_card_input)
2040 for blockName
in list(params.keys()):
2041 paramsUpper[blockName.upper()] = {}
2042 for paramName
in list(params[blockName].
keys()):
2043 paramsUpper[blockName.upper()][paramName.upper()] = params[blockName][paramName]
2045 if param_card_backup
is not None:
2046 mglog.info(
'Keeping backup of original param card at '+param_card_backup)
2047 param_card_old = param_card_backup
2049 param_card_old = param_card_input+
'.old_to_be_deleted'
2050 if os.path.isfile(param_card_old):
2051 os.unlink(param_card_old)
2052 os.rename(param_card_input, param_card_old)
2054 oldcard =
open(param_card_old,
'r')
2055 param_card_location= process_dir+
'/Cards/param_card.dat' if output_location
is None else output_location
2056 newcard =
open(param_card_location,
'w')
2060 for linewithcomment
in oldcard:
2061 line=linewithcomment.split(
'#')[0]
2062 if line.strip().
upper().startswith(
'BLOCK')
or line.strip().
upper().startswith(
'DECAY')\
2063 and len(line.strip().
split()) > 1:
2064 if decayEdit
and blockName ==
'DECAY':
2066 pos = 0
if line.strip().startswith(
'DECAY')
else 1
2067 if blockName==
'MASS' and 'MASS' in paramsUpper:
2069 if "MASS" in doneParams:
2070 leftOvers = [ x
for x
in paramsUpper[
'MASS']
if x
not in doneParams[
'MASS'] ]
2072 leftOvers = [ x
for x
in paramsUpper[
'MASS'] ]
2074 for pdg_id
in leftOvers:
2075 mglog.warning(
'Adding mass line for '+
str(pdg_id)+
' = '+
str(paramsUpper[
'MASS'][pdg_id])+
' which was not in original param card')
2076 newcard.write(
' '+
str(pdg_id)+
' '+
str(paramsUpper[
'MASS'][pdg_id])+
'\n')
2077 doneParams[
'MASS'][pdg_id]=
True
2078 if blockName==
'DECAY' and 'DECAY' not in line.strip().
upper()
and 'DECAY' in paramsUpper:
2080 leftOvers = [ x
for x
in paramsUpper[
'DECAY']
if x
not in doneParams[
'DECAY'] ]
2081 for pdg_id
in leftOvers:
2082 mglog.warning(
'Adding decay for pdg id '+
str(pdg_id)+
' which was not in the original param card')
2083 newcard.write( paramsUpper[
'DECAY'][pdg_id].strip()+
'\n' )
2084 doneParams[
'DECAY'][pdg_id]=
True
2085 blockName = line.strip().
upper().
split()[pos]
2090 if blockName !=
'DECAY' and len(line.strip().
split()) > 0:
2093 if len(line.split())==2:
2094 akey = line.upper().strip().
split()[0]
2097 akey = line.upper().strip()[:line.strip().rfind(
' ')].strip()
2098 elif blockName ==
'DECAY' and len(line.strip().
split()) > 1:
2099 akey = line.strip().
split()[1]
2101 newcard.write(linewithcomment)
2105 if blockName
not in paramsUpper:
2106 newcard.write(linewithcomment)
2108 blockParams = paramsUpper[blockName]
2114 if '#' in linewithcomment:
2115 stringkey = linewithcomment[linewithcomment.find(
'#')+1:].strip()
2116 if len(stringkey.split()) > 0:
2117 stringkey = stringkey.split()[0].
upper()
2119 if akey
not in blockParams
and not (stringkey
is not None and stringkey
in blockParams):
2120 newcard.write(linewithcomment)
2123 if akey
in blockParams
and (stringkey
is not None and stringkey
in blockParams):
2124 raise RuntimeError(
'Conflicting use of numeric and string keys '+akey+
' and '+stringkey)
2126 theParam = blockParams.get(akey,blockParams[stringkey]
if stringkey
in blockParams
else None)
2127 if blockName
not in doneParams:
2128 doneParams[blockName] = {}
2129 if akey
in blockParams:
2130 doneParams[blockName][akey]=
True
2131 elif stringkey
is not None and stringkey
in blockParams:
2132 doneParams[blockName][stringkey]=
True
2135 if blockName==
"DECAY":
2136 if theParam.splitlines()[0].
split()[0].
upper()==
"DECAY":
2138 for newline
in theParam.splitlines():
2139 newcard.write(newline+
'\n')
2143 newcard.write(
'DECAY '+akey+
' '+
str(theParam)+
' # '+(linewithcomment[linewithcomment.find(
'#')+1:].strip()
if linewithcomment.find(
'#')>0
else "")+
'\n')
2144 mglog.info(
'DECAY '+akey+
' '+
str(theParam)+
' # '+(linewithcomment[linewithcomment.find(
'#')+1:].strip()
if linewithcomment.find(
'#')>0
else "")+
'\n')
2146 elif blockName==
'QNUMBERS':
2148 for newline
in theParam.splitlines():
2149 newcard.write(newline+
'\n')
2153 newcard.write(
' '+akey+
' '+
str(theParam)+
' # '+(linewithcomment[linewithcomment.find(
'#')+1:].strip()
if linewithcomment.find(
'#')>0
else "")+
'\n')
2154 mglog.info(
' '+akey+
' '+
str(theParam)+
' # '+(linewithcomment[linewithcomment.find(
'#')+1:].strip()
if linewithcomment.find(
'#')>0
else "")+
'\n')
2158 for blockName
in paramsUpper:
2159 if blockName
not in doneParams
and len(paramsUpper[blockName].
keys())>0:
2160 raise RuntimeError(
'Did not find any of the parameters for block '+blockName+
' in param_card')
2161 for paramName
in paramsUpper[blockName]:
2162 if paramName
not in doneParams[blockName]:
2163 raise RuntimeError(
'Was not able to replace parameter '+paramName+
' in param_card')
2170 def modify_run_card(run_card_input=None,run_card_backup=None,process_dir=MADGRAPH_GRIDPACK_LOCATION,runArgs=None,settings={},skipBaseFragment=False):
2171 """Build a new run_card.dat from an existing one.
2172 This function can get a fresh runcard from DATAPATH or start from the process directory.
2173 Settings is a dictionary of keys (no spaces needed) and values to replace.
2176 mglog.info(
'Running config-only. No proc card, so not operating on the run card.')
2181 for s
in list(settings.keys()):
2182 settings_lower[s.lower()] = settings[s]
2185 if run_card_input
is None:
2187 elif run_card_input
is not None and not os.access(run_card_input,os.R_OK):
2188 runcard = subprocess.Popen([
'get_files',
'-data',run_card_input])
2190 if not os.access(run_card_input,os.R_OK):
2191 raise RuntimeError(
'Could not get run card '+run_card_input)
2196 if not skipBaseFragment:
2197 MadGraphControl.MadGraphSystematicsUtils.setup_pdf_and_systematic_weights(MADGRAPH_PDFSETTING,settings_lower,isNLO)
2200 if runArgs
is not None:
2202 if 'iseed' not in settings_lower:
2203 settings_lower[
'iseed']=rand_seed
2204 if not isNLO
and 'python_seed' not in settings_lower:
2205 settings_lower[
'python_seed']=rand_seed
2206 if 'beamenergy' in settings_lower:
2207 mglog.warning(
'Do not set beam energy in MG settings. The variables are ebeam1 and ebeam2. Will use your setting of '+
str(settings_lower[
'beamenergy']))
2208 beamEnergy=settings_lower[
'beamenergy']
2209 settings_lower.pop(
'beamenergy')
2210 if 'ebeam1' not in settings_lower:
2211 settings_lower[
'ebeam1']=beamEnergy
2212 if 'ebeam2' not in settings_lower:
2213 settings_lower[
'ebeam2']=beamEnergy
2215 if 'nevents' in settings_lower:
2216 settings_lower[
'nevents'] =
int(settings_lower[
'nevents'])
2218 mglog.info(
'Modifying run card located at '+run_card_input)
2219 if run_card_backup
is not None:
2220 mglog.info(
'Keeping backup of original run card at '+run_card_backup)
2221 run_card_old = run_card_backup
2223 run_card_old = run_card_input+
'.old_to_be_deleted'
2224 mglog.debug(
'Modifying runcard settings: '+
str(settings_lower))
2225 if os.path.isfile(run_card_old):
2226 os.unlink(run_card_old)
2227 os.rename(run_card_input, run_card_old)
2229 oldCard =
open(run_card_old,
'r')
2230 newCard =
open(process_dir+
'/Cards/run_card.dat',
'w')
2232 for line
in iter(oldCard):
2233 if not line.strip().startswith(
'#'):
2234 command = line.split(
'!', 1)[0]
2235 comment = line.split(
'!', 1)[1]
if '!' in line
else ''
2237 setting = command.split(
'=')[-1]
2238 stripped_setting = setting.strip()
2239 oldValue =
'='.
join(command.split(
'=')[:-1])
2240 if stripped_setting.lower()
in settings_lower:
2242 if settings_lower[stripped_setting.lower()]
is None:
2244 mglog.info(
'Removing '+stripped_setting+
'.')
2245 used_settings += [ stripped_setting.lower() ]
2247 line = oldValue.replace(oldValue.strip(),
str(settings_lower[stripped_setting.lower()]))+
'='+setting
2249 line +=
' !' + comment
2250 mglog.info(
'Setting '+stripped_setting+
' = '+
str(settings_lower[stripped_setting.lower()]))
2251 used_settings += [ stripped_setting.lower() ]
2252 newCard.write(line.strip()+
'\n')
2255 if 'mcatnlo_delta' in settings_lower:
2256 if settings_lower[
'mcatnlo_delta'] ==
'True':
2257 modify_config_card(process_dir=process_dir,settings={
'pythia8_path':os.getenv(
"PY8PATH")})
2260 for asetting
in settings_lower:
2261 if asetting
in used_settings:
2263 if settings_lower[asetting]
is None:
2265 mglog.warning(
'Option '+asetting+
' was not in the default run_card. Adding by hand a setting to '+
str(settings_lower[asetting]) )
2266 newCard.write(
' '+
str(settings_lower[asetting])+
' = '+
str(asetting)+
'\n')
2270 mglog.info(
'Finished modification of run card.')
2271 if run_card_backup
is None:
2272 os.unlink(run_card_old)
2275 def modify_config_card(config_card_backup=None,process_dir=MADGRAPH_GRIDPACK_LOCATION,settings={},set_commented=True):
2276 """Build a new configuration from an existing one.
2277 This function can get a fresh runcard from DATAPATH or start from the process directory.
2278 Settings is a dictionary of keys (no spaces needed) and values to replace.
2284 mglog.info(
'Modifying config card located at '+config_card)
2285 if config_card_backup
is not None:
2286 mglog.info(
'Keeping backup of original config card at '+config_card_backup)
2287 config_card_old = config_card_backup
2289 config_card_old = config_card+
'.old_to_be_deleted'
2290 mglog.debug(
'Modifying config card settings: '+
str(settings))
2291 if os.path.isfile(config_card_old):
2292 os.unlink(config_card_old)
2293 os.rename(config_card, config_card_old)
2295 oldCard =
open(config_card_old,
'r')
2296 newCard =
open(config_card,
'w')
2298 for line
in iter(oldCard):
2299 lmod = line
if set_commented
else line.split(
'#')[0]
2302 for setting
in settings:
2303 if setting
not in lmod:
2306 mglog.info(
'Setting '+setting.strip()+
' to '+
str(settings[setting]))
2307 newCard.write(
' '+
str(setting.strip())+
' = '+
str(settings[setting])+
'\n')
2308 used_settings += [ setting.strip() ]
2316 for asetting
in settings:
2317 if asetting
in used_settings:
2319 if settings[asetting]
is None:
2321 mglog.warning(
'Option '+asetting+
' was not in the default config card. Adding by hand a setting to '+
str(settings[asetting]) )
2322 newCard.write(
' '+
str(asetting)+
' = '+
str(settings[asetting])+
'\n')
2326 mglog.info(
'Finished modification of config card.')
2327 if config_card_backup
is None:
2328 os.unlink(config_card_old)
2332 card_dir=process_dir+
'/Cards/'
2333 print_cards(proc_card=card_dir+
'proc_card_mg5.dat',run_card=card_dir+
'run_card.dat',param_card=card_dir+
'param_card.dat',\
2334 madspin_card=card_dir+
'madspin_card.dat',reweight_card=card_dir+
'reweight_card.dat',warn_on_missing=
False)
2337 def print_cards(proc_card='proc_card_mg5.dat',run_card=None,param_card=None,madspin_card=None,reweight_card=None,warn_on_missing=True):
2338 if os.access(proc_card,os.R_OK):
2339 mglog.info(
"proc_card:")
2340 procCard = subprocess.Popen([
'cat',proc_card])
2342 elif warn_on_missing:
2343 mglog.warning(
'No proc_card: '+proc_card+
' found')
2345 if run_card
is not None and os.access(run_card,os.R_OK):
2346 mglog.info(
"run_card:")
2347 runCard = subprocess.Popen([
'cat',run_card])
2349 elif run_card
is not None and warn_on_missing:
2350 mglog.warning(
'No run_card: '+run_card+
' found')
2352 mglog.info(
'Default run card in use')
2354 if param_card
is not None and os.access(param_card,os.R_OK):
2355 mglog.info(
"param_card:")
2356 paramCard = subprocess.Popen([
'cat',param_card])
2358 elif param_card
is not None and warn_on_missing:
2359 mglog.warning(
'No param_card: '+param_card+
' found')
2361 mglog.info(
'Default param card in use')
2363 if madspin_card
is not None and os.access(madspin_card,os.R_OK):
2364 mglog.info(
"madspin_card:")
2365 madspinCard = subprocess.Popen([
'cat',madspin_card])
2367 elif madspin_card
is not None and warn_on_missing:
2368 mglog.warning(
'No madspin_card: '+madspin_card+
' found')
2370 mglog.info(
'No madspin card in use')
2372 if reweight_card
is not None and os.access(reweight_card,os.R_OK):
2373 mglog.info(
"reweight_card:")
2374 madspinCard = subprocess.Popen([
'cat',reweight_card])
2376 elif reweight_card
is not None and warn_on_missing:
2377 mglog.warning(
'No reweight_card: '+reweight_card+
' found')
2379 mglog.info(
'No reweight card in use')
2383 """ Simple function for checking if there is a grid pack.
2384 Relies on the specific location of the unpacked gridpack (madevent)
2385 which is here set as a global variable. The gridpack is untarred by
2386 the transform (Gen_tf.py) and no sign is sent to the job itself
2387 that there is a gridpack in use except the file's existence"""
2388 if os.access(MADGRAPH_GRIDPACK_LOCATION,os.R_OK):
2389 mglog.info(
'Located input grid pack area')
2396 mglog.info(
'Athena running on config only mode: grabbing config card the old way, as there will be no proc dir')
2397 if os.access(os.environ[
'MADPATH']+
'/input/mg5_configuration.txt',os.R_OK):
2398 shutil.copy(os.environ[
'MADPATH']+
'/input/mg5_configuration.txt',
'local_mg5_configuration.txt')
2399 return 'local_mg5_configuration.txt'
2401 lo_config_card=process_dir+
'/Cards/me5_configuration.txt'
2402 nlo_config_card=process_dir+
'/Cards/amcatnlo_configuration.txt'
2404 if os.access(lo_config_card,os.R_OK)
and not os.access(nlo_config_card,os.R_OK):
2405 return lo_config_card
2406 elif os.access(nlo_config_card,os.R_OK)
and not os.access(lo_config_card,os.R_OK):
2407 return nlo_config_card
2408 elif os.access(nlo_config_card,os.R_OK)
and os.access(lo_config_card,os.R_OK):
2409 mglog.error(
'Found both types of config card in '+process_dir)
2411 mglog.error(
'No config card in '+process_dir)
2412 raise RuntimeError(
'Unable to locate configuration card')
2417 for l
in card_in.readlines():
2418 if 'cluster_type' not in l.split(
'#')[0]:
2420 cluster_type = l.split(
'#')[0].
split(
'=')[1]
2421 mglog.info(
'Returning cluster type: '+cluster_type)
2434 cardpath=process_dir+
'/Cards/run_card.dat'
2440 modify_run_card(process_dir=process_dir,settings={
'event_norm':
'average'},skipBaseFragment=
True)
2441 mglog.warning(
"setting event_norm to average, there is basically no use case where event_norm=sum is a good idea")
2445 if 'ktdurham' in mydict
and float(mydict[
'ktdurham']) > 0
and int(mydict[
'ickkw']) != 0:
2446 log=
'Bad combination of settings for CKKW-L merging! ktdurham=%s and ickkw=%s.'%(mydict[
'ktdurham'],mydict[
'ickkw'])
2448 raise RuntimeError(log)
2451 if 'systematics_program' not in mydict
or mydict[
'systematics_program']==
'systematics':
2452 syscalc_settings=[
'sys_pdf',
'sys_scalefact',
'sys_alpsfact',
'sys_matchscale']
2453 found_syscalc_setting=
False
2454 for s
in syscalc_settings:
2456 mglog.warning(
'Using syscalc setting '+s+
' with new systematics script. Systematics script is default from 2.6.2 and steered differently (https://cp3.irmp.ucl.ac.be/projects/madgraph/wiki/Systematics#Systematicspythonmodule)')
2457 found_syscalc_setting=
True
2458 if found_syscalc_setting:
2459 syst_arguments=MadGraphControl.MadGraphSystematicsUtils.convertSysCalcArguments(mydict)
2460 mglog.info(
'Converted syscalc arguments to systematics arguments: '+syst_arguments)
2461 syst_settings_update={
'systematics_arguments':syst_arguments}
2462 for s
in syscalc_settings:
2463 syst_settings_update[s]=
None
2464 modify_run_card(process_dir=process_dir,settings=syst_settings_update,skipBaseFragment=
True)
2469 mglog.info(
'Checking PDF and systematics settings')
2470 if not MadGraphControl.MadGraphSystematicsUtils.base_fragment_setup_check(MADGRAPH_PDFSETTING,mydict,isNLO):
2472 syst_settings=MadGraphControl.MadGraphSystematicsUtils.get_pdf_and_systematic_settings(MADGRAPH_PDFSETTING,isNLO)
2473 modify_run_card(process_dir=process_dir,settings=syst_settings,skipBaseFragment=
True)
2476 if 'systematics_arguments' in mydict_new:
2477 systematics_arguments=MadGraphControl.MadGraphSystematicsUtils.parse_systematics_arguments(mydict_new[
'systematics_arguments'])
2478 if 'weight_info' not in systematics_arguments:
2479 mglog.info(
'Enforcing systematic weight name convention')
2481 if '--dyn' in systematics_arguments
or ' dyn' in systematics_arguments:
2482 if '--dyn' in systematics_arguments:
2483 dyn = systematics_arguments.split(
'--dyn')[1]
2484 if ' dyn' in systematics_arguments:
2485 dyn = systematics_arguments.split(
' dyn')[1]
2486 dyn = dyn.replace(
'\'',
' ').
replace(
'=',
' ').
split()[0]
2487 if dyn
is not None and len(dyn.split(
','))>1:
2488 systematics_arguments[
'weight_info']=MadGraphControl.MadGraphSystematicsUtils.SYSTEMATICS_WEIGHT_INFO_ALTDYNSCALES
2490 systematics_arguments[
'weight_info']=MadGraphControl.MadGraphSystematicsUtils.SYSTEMATICS_WEIGHT_INFO
2491 modify_run_card(process_dir=process_dir,settings={
'systematics_arguments':MadGraphControl.MadGraphSystematicsUtils.write_systematics_arguments(systematics_arguments)},skipBaseFragment=
True)
2494 if 'python_seed' not in mydict:
2495 mglog.warning(
'No python seed set in run_card -- adding one with same value as iseed')
2496 modify_run_card(process_dir=process_dir,settings={
'python_seed':mydict[
'iseed']},skipBaseFragment=
True)
2498 mglog.info(
'Finished checking run card - All OK!')
2501 mglog.info(
'Running reweighting module on existing events')
2502 if reweight_card
is not None:
2503 mglog.info(
'Copying new reweight card from '+reweight_card)
2504 shutil.move(reweight_card,process_dir+
'/Cards/reweight_card.dat')
2505 reweight_cmd=
'{}/bin/madevent reweight {} -f'.
format(process_dir,run_name)
2506 global MADGRAPH_CATCH_ERRORS
2507 reweight =
stack_subprocess([python]+reweight_cmd.split(),stdin=subprocess.PIPE,stderr=subprocess.PIPE
if MADGRAPH_CATCH_ERRORS
else None)
2508 (out,err) = reweight.communicate()
2510 mglog.info(
'Finished reweighting')
2513 if 'ATHENA_PROC_NUMBER' in os.environ
and int(os.environ[
'ATHENA_PROC_NUMBER'])>0:
2514 mglog.info(
'Noticed that you have run with an athena MP-like whole-node setup. Will re-configure now to make sure that the remainder of the job runs serially.')
2516 if not hasattr(opts,
'nprocs'):
2517 mglog.warning(
'Did not see option!')
2520 mglog.debug(
str(opts))
2524 mglog.info(
'For your information, ls of '+directory+
':')
2525 mglog.info(
sorted( os.listdir( directory ) ) )
2528 import MadGraphControl.MadGraphSystematicsUtils
2532 makefile_fks=process_dir+
'/SubProcesses/makefile_fks_dir'
2533 mglog.info(
'Fixing '+makefile_fks)
2534 shutil.move(makefile_fks,makefile_fks+
'_orig')
2535 fin=
open(makefile_fks+
'_orig')
2536 fout=
open(makefile_fks,
'w')
2539 if 'FKSParams.mod' in line:
2540 fout.write(line.replace(
'FKSParams.mod',
'FKSParams.o'))
2542 elif edit
and 'driver_mintFO' in line:
2543 fout.write(
'driver_mintFO.o: weight_lines.o mint_module.o FKSParams.o\n')
2544 elif edit
and 'genps_fks.o' in line:
2545 fout.write(
'genps_fks.o: mint_module.o FKSParams.o\n')
2546 elif edit
and 'test_soft_col_limits' in line:
2548 fout.write(
'madfks_plot.o: mint_module.o\n')
2549 fout.write(
'cluster.o: weight_lines.o\n')