11 import os,time,subprocess,glob,re,sys
12 from AthenaCommon
import Logging
13 from MadGraphControl.MadGraphUtilsHelpers
import getDictFromCard,is_version_or_newer,error_check,setup_path_protection,is_NLO_run
14 from MadGraphControl.MadGraphParamHelpers
import do_PMG_updates
15 mglog = Logging.logging.getLogger(
'MadGraphUtils')
20 MADGRAPH_GRIDPACK_LOCATION=
'madevent'
22 MADGRAPH_RUN_NAME=
'run_01'
24 MADGRAPH_CATCH_ERRORS=
True
26 MADGRAPH_PDFSETTING=
None
35 def __init__(self, process='generate p p > t t~\noutput -f', plugin=None, keepJpegs=False, usePMGSettings=False):
36 """ Generate a new process in madgraph.
37 Pass a process string.
38 Optionally request JPEGs to be kept and request for PMG settings to be used in the param card
39 Return the name of the process directory.
41 self.
mglog = Logging.logging.getLogger(
'MadGraphUtils')
54 card_loc=
'proc_card_mg5.dat'
55 mglog.info(
'Writing process card to '+card_loc)
56 a_card =
open( card_loc ,
'w' )
57 for l
in process.split(
'\n'):
63 if '-nojpeg' not in l
and not keepJpegs:
66 outline = outline.split(
'#')[0]+
' -nojpeg #'+outline.split(
'#')[1]
68 outline = outline +
' -nojpeg'
70 if MADGRAPH_DEVICES
is not None:
71 if MADGRAPH_DEVICES.lower()
in [
'madevent_simd',
'madevent_gpu']:
72 outline =
'output '+MADGRAPH_DEVICES.lower()+
' '+outline.split(
'output')[1]
73 elif MADGRAPH_DEVICES.lower() ==
'max':
74 self.
mglog.warning(
'Not fully implemented yet; setting avx')
75 outline =
'output madevent_simd '+outline.split(
'output')[1]
76 a_card.write(outline+
'\n')
79 madpath=os.environ[
'MADPATH']
85 for l
in process.split(
'\n'):
87 if 'output' not in l.split(
'#')[0].
split():
90 tmplist = l.split(
'#')[0].
split(
' -')[0]
92 if len(tmplist.split())==2:
93 process_dir = tmplist.split()[1]
95 elif len(tmplist.split())==3:
96 process_dir = tmplist.split()[2]
99 mglog.info(
'Saw that you asked for a special output directory: '+
str(process_dir))
102 mglog.info(
'Started process generation at '+
str(time.asctime()))
104 plugin_cmd =
'--mode='+plugin
if plugin
is not None else ''
111 global MADGRAPH_CATCH_ERRORS
112 generate = subprocess.Popen([python,madpath+
'/bin/mg5_aMC',plugin_cmd,card_loc],stdin=subprocess.PIPE,stderr=subprocess.PIPE
if MADGRAPH_CATCH_ERRORS
else None)
113 (out,err) = generate.communicate()
116 mglog.info(
'Finished process generation at '+
str(time.asctime()))
119 if process_dir ==
'':
120 for adir
in sorted(glob.glob( os.getcwd()+
'/*PROC*' ),reverse=
True):
121 if os.access(
'%s/SubProcesses/subproc.mg'%adir,os.R_OK):
125 mglog.warning(
'Additional possible process directory, '+adir+
' found. Had '+process_dir)
126 mglog.warning(
'Likely this is because you did not run from a clean directory, and this may cause errors later.')
128 if not os.access(
'%s/SubProcesses/subproc.mg'%process_dir,os.R_OK):
129 raise RuntimeError(
'No diagrams for this process in user-define dir='+
str(process_dir))
131 raise RuntimeError(
'No diagrams for this process from list: '+
str(
sorted(glob.glob(os.getcwd()+
'/*PROC*'),reverse=
True)))
134 needed_options = [
'ninja',
'collier',
'fastjet',
'lhapdf',
'syscalc_path']
135 in_config =
open(os.environ[
'MADPATH']+
'/input/mg5_configuration.txt',
'r')
137 for l
in in_config.readlines():
138 for o
in needed_options:
139 if o+
' =' in l.split(
'#')[0]
and 'MCGenerators' in l.split(
'#')[0]:
140 old_path = l.split(
'#')[0].
split(
'=')[1].strip().
split(
'MCGenerators')[1]
141 old_path = old_path[ old_path.find(
'/') : ]
142 if o ==
'lhapdf' and 'LHAPATH' in os.environ:
144 version = os.environ[
'LHAPATH'].
split(
'lhapdf/')[1].
split(
'/')[0]
145 old_version = old_path.split(
'lhapdf/')[1].
split(
'/')[0]
146 old_path = old_path.replace(old_version,version)
149 old_path.replace(
'gosam_contrib',
'gosam-contrib')
150 option_paths[o] = os.environ[
'MADPATH'].
split(
'madgraph5amc')[0]+old_path
152 if o+
' =' in l
and o+
' =' not in l.split(
'#')[0]:
153 mglog.info(
'Option '+o+
' appears commented out in the config file')
156 for o
in needed_options:
157 if o
not in option_paths:
158 mglog.info(
'Path for option '+o+
' not found in original config')
160 mglog.info(
'Modifying config paths to avoid use of afs:')
161 mglog.info(option_paths)
167 self.
change_config_card(process_dir=process_dir,settings=option_paths,set_commented=
False)
176 mglog.info(
'Setting default sde_strategy to old default (1)')
180 self.
change_config_card(process_dir=process_dir,settings={
'notification_center':
'False'})
183 if MADGRAPH_DEVICES
is not None:
184 if MADGRAPH_DEVICES.lower()==
'madevent_simd':
186 elif MADGRAPH_DEVICES.lower()==
'madevent_gpu':
190 os.environ[
'ALLOW_UNSUPPORTED_COMPILER_IN_CUDA'] =
'Y'
191 elif MADGRAPH_DEVICES.lower() ==
'max':
192 self.
mglog.warning(
'Not fully implemented yet; setting avx')
199 def change_run_card(self, run_card_input=None,run_card_backup=None,process_dir=MADGRAPH_GRIDPACK_LOCATION,runArgs=None,settings={},skipBaseFragment=False ):
200 self.
mglog.warning(
'Do not call this function, just update self.run_card_params')
201 self.
run_card_params += [run_card_input,run_card_backup,process_dir,runArgs,settings,skipBaseFragment ]
203 def change_config_card(self, config_card_backup=None,process_dir=MADGRAPH_GRIDPACK_LOCATION,settings={},set_commented=True ):
204 self.
mglog.warning(
'Do not call this function, just update self.run_card_params')
205 self.
run_card_params += [ config_card_backup,process_dir,settings,set_commented ]