ATLAS Offline Software
Loading...
Searching...
No Matches
python.MadGraphUtilsHelpers Namespace Reference

Functions

 getDictFromCard (card_loc, lowercase=False)
 settingIsTrue (setting)
 totallyStripped (x)
 checkSetting (key_, value_, mydict_)
 checkSettingIsTrue (key_, mydict_)
 checkSettingExists (key_, mydict_)
 get_mg5_version ()
 is_version_or_newer (args)
 isNLO_from_run_card (run_card)
 get_runArgs_info (runArgs)
 error_check (errors_a, return_code)
 write_test_script ()
 setup_path_protection ()
 get_default_config_card (process_dir=MADGRAPH_GRIDPACK_LOCATION)
 is_NLO_run (process_dir=MADGRAPH_GRIDPACK_LOCATION)

Variables

 mglog = Logging.logging.getLogger('MadGraphUtils')
str MADGRAPH_GRIDPACK_LOCATION = 'madevent'
bool MADGRAPH_CATCH_ERRORS = True
list MADGRAPH_COMMAND_STACK = []

Function Documentation

◆ checkSetting()

python.MadGraphUtilsHelpers.checkSetting ( key_,
value_,
mydict_ )

Definition at line 44 of file MadGraphUtilsHelpers.py.

44def checkSetting(key_,value_,mydict_):
45 key=totallyStripped(key_)
46 value=totallyStripped(value_)
47 mydict={}
48 for k in mydict_:
49 mydict[totallyStripped(k)]=totallyStripped(mydict_[k])
50 return key in mydict and mydict[key]==value
51

◆ checkSettingExists()

python.MadGraphUtilsHelpers.checkSettingExists ( key_,
mydict_ )

Definition at line 59 of file MadGraphUtilsHelpers.py.

59def checkSettingExists(key_,mydict_):
60 key=totallyStripped(key_)
61 keys=[]
62 for k in mydict_:
63 keys+=[totallyStripped(k)]
64 return key in keys
65

◆ checkSettingIsTrue()

python.MadGraphUtilsHelpers.checkSettingIsTrue ( key_,
mydict_ )

Definition at line 52 of file MadGraphUtilsHelpers.py.

52def checkSettingIsTrue(key_,mydict_):
53 key=totallyStripped(key_)
54 mydict={}
55 for k in mydict_:
56 mydict[totallyStripped(k)]=totallyStripped(mydict_[k])
57 return key in mydict and mydict[key] in ['t','true']
58

◆ error_check()

python.MadGraphUtilsHelpers.error_check ( errors_a,
return_code )

Definition at line 131 of file MadGraphUtilsHelpers.py.

131def error_check(errors_a, return_code):
132 if not MADGRAPH_CATCH_ERRORS:
133 return
134 unmasked_error = False
135 my_debug_file = None
136 bad_variables = []
137 # Make sure we are getting a string and not a byte string (python3 ftw)
138 errors = errors_a
139 if type(errors)==bytes:
140 errors = errors.decode('utf-8')
141 if len(errors):
142 mglog.info('Some errors detected by MadGraphControl - checking for serious errors')
143 for err in errors.split('\n'):
144 if len(err.strip())==0:
145 continue
146 # Errors to do with I/O... not clear on their origin yet
147 if 'Inappropriate ioctl for device' in err:
148 mglog.info(err)
149 continue
150 if 'stty: standard input: Invalid argument' in err:
151 mglog.info(err)
152 continue
153 # Errors for PDF sets that should be fixed in MG5_aMC 2.7
154 if 'PDF already installed' in err:
155 mglog.info(err)
156 continue
157 if 'Read-only file system' in err:
158 mglog.info(err)
159 continue
160 if 'HTML' in err:
161 # https://bugs.launchpad.net/mg5amcnlo/+bug/1870217
162 mglog.info(err)
163 continue
164 if 'impossible to set default multiparticles' in err:
165 # https://answers.launchpad.net/mg5amcnlo/+question/690004
166 mglog.info(err)
167 continue
168 if 'More information is found in' in err:
169 my_debug_file = err.split("'")[1]
170 if err.startswith('tar'):
171 mglog.info(err)
172 continue
173 if 'python2 support will be removed' in err:
174 mglog.info(err)
175 continue
176 if 'python3.12 support is still experimental' in err:
177 mglog.info(err)
178 continue
179 # silly ghostscript issue in 21.6.46 nightly
180 if 'required by /lib64/libfontconfig.so' in err or\
181 'required by /lib64/libgs.so' in err:
182 mglog.info(err)
183 continue
184 if 'Error: Symbol' in err and 'has no IMPLICIT type' in err:
185 bad_variables += [ err.split('Symbol ')[1].split(' at ')[0] ]
186 # error output from tqdm (progress bar)
187 if 'it/s' in err:
188 mglog.info(err)
189 continue
190 mglog.error(err)
191 unmasked_error = True
192 # This is a bit clunky, but needed because we could be several places when we get here
193 if my_debug_file is None:
194 debug_files = glob.glob('*debug.log')+glob.glob('*/*debug.log')
195 for debug_file in debug_files:
196 # This protects against somebody piping their output to my_debug.log and it being caught here
197 has_subproc = os.access(os.path.join(os.path.dirname(debug_file),'SubProcesses'),os.R_OK)
198 if has_subproc:
199 my_debug_file = debug_file
200 break
201
202 if my_debug_file is not None:
203 if not unmasked_error:
204 mglog.warning('Found a debug file at '+my_debug_file+' but no apparent error. Will terminate.')
205 mglog.error('MadGraph5_aMC@NLO appears to have crashed. Debug file output follows.')
206 with open(my_debug_file,'r') as error_output:
207 for l in error_output:
208 mglog.error(l.replace('\n',''))
209 mglog.error('End of debug file output')
210
211 if bad_variables:
212 mglog.warning('Appeared to detect variables in your run card that MadGraph did not understand:')
213 mglog.warning(' Check your run card / JO settings for %s',bad_variables)
214
215 # Check the return code
216 if return_code!=0:
217 mglog.error(f'Detected a bad return code: {return_code}')
218 unmasked_error = True
219
220 # Now raise an error if we were in either of the error states
221 if unmasked_error or my_debug_file is not None:
222 write_test_script()
223 raise RuntimeError('Error detected in MadGraphControl process')
224 return
225
226
227# Write a short test script for standalone debugging
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:177

◆ get_default_config_card()

python.MadGraphUtilsHelpers.get_default_config_card ( process_dir = MADGRAPH_GRIDPACK_LOCATION)

Definition at line 263 of file MadGraphUtilsHelpers.py.

263def get_default_config_card(process_dir=MADGRAPH_GRIDPACK_LOCATION):
264
265 lo_config_card=process_dir+'/Cards/me5_configuration.txt'
266 nlo_config_card=process_dir+'/Cards/amcatnlo_configuration.txt'
267
268 if os.access(lo_config_card,os.R_OK) and not os.access(nlo_config_card,os.R_OK):
269 return lo_config_card
270 elif os.access(nlo_config_card,os.R_OK) and not os.access(lo_config_card,os.R_OK):
271 return nlo_config_card
272 elif os.access(nlo_config_card,os.R_OK) and os.access(lo_config_card,os.R_OK):
273 mglog.error('Found both types of config card in '+process_dir)
274 else:
275 mglog.error('No config card in '+process_dir)
276 raise RuntimeError('Unable to locate configuration card')
277

◆ get_mg5_version()

python.MadGraphUtilsHelpers.get_mg5_version ( )
Return MadGraph version string (e.g. '3.5.1')

Used to include MG version in gridpack names for better traceability.
Reads version from $MADPATH/VERSION file.

Definition at line 66 of file MadGraphUtilsHelpers.py.

66def get_mg5_version():
67 """Return MadGraph version string (e.g. '3.5.1')
68
69 Used to include MG version in gridpack names for better traceability.
70 Reads version from $MADPATH/VERSION file.
71 """
72 with open(os.environ['MADPATH']+'/VERSION','r') as version_file:
73 for line in version_file:
74 if 'version' in line:
75 return line.split('=')[1].strip()
76 raise RuntimeError('Failed to find MadGraph/MadGraph5_aMC@NLO version')
77

◆ get_runArgs_info()

python.MadGraphUtilsHelpers.get_runArgs_info ( runArgs)

Definition at line 117 of file MadGraphUtilsHelpers.py.

117def get_runArgs_info(runArgs):
118 if runArgs is None:
119 raise RuntimeError('runArgs must be provided!')
120 if hasattr(runArgs,'ecmEnergy'):
121 beamEnergy = runArgs.ecmEnergy / 2.
122 else:
123 raise RuntimeError("No center of mass energy found in runArgs.")
124 if hasattr(runArgs,'randomSeed'):
125 random_seed = runArgs.randomSeed
126 else:
127 raise RuntimeError("No random seed found in runArgs.")
128 return beamEnergy,random_seed
129
130

◆ getDictFromCard()

python.MadGraphUtilsHelpers.getDictFromCard ( card_loc,
lowercase = False )

Definition at line 14 of file MadGraphUtilsHelpers.py.

14def getDictFromCard(card_loc,lowercase=False):
15 card=open(card_loc)
16 mydict={}
17 for line in iter(card):
18 if not line.strip().startswith('#'): # line commented out
19 command = line.split('!', 1)[0]
20 if '=' in command:
21 setting = command.split('=')[-1].strip()
22 value = '='.join(command.split('=')[:-1]).strip()
23 if lowercase:
24 value=value.lower()
25 setting=setting.lower()
26 mydict[setting]=value
27 card.close()
28 return mydict
29

◆ is_NLO_run()

python.MadGraphUtilsHelpers.is_NLO_run ( process_dir = MADGRAPH_GRIDPACK_LOCATION)

Definition at line 278 of file MadGraphUtilsHelpers.py.

278def is_NLO_run(process_dir=MADGRAPH_GRIDPACK_LOCATION):
279 # Very simple check based on the above config card grabbing
280 return get_default_config_card(process_dir=process_dir)==process_dir+'/Cards/amcatnlo_configuration.txt'

◆ is_version_or_newer()

python.MadGraphUtilsHelpers.is_version_or_newer ( args)

Definition at line 78 of file MadGraphUtilsHelpers.py.

78def is_version_or_newer(args):
79 # also need to find out the version (copied from generate)
80 import os
81 version=None
82 version_file = open(os.environ['MADPATH']+'/VERSION','r')
83
84 for line in version_file:
85 if 'version' in line:
86 version=line.split('=')[1].strip()
87 version_file.close()
88
89 if not version:
90 raise RuntimeError('Failed to find MadGraph/MadGraph5_aMC@NLO version in '+version_file)
91
92 vs=[int(v) for v in version.split('.')]
93
94 # this is lazy, let's hope there wont be a subversion > 100...
95 y=int(100**max(len(vs),len(args)))
96 testnumber=0
97 for x in args:
98 testnumber+=x*y
99 y/=100
100
101 y=int(100**max(len(vs),len(args)))
102 versionnumber=0
103 for x in vs:
104 versionnumber+=x*y
105 y/=100
106 return versionnumber>=testnumber
107
#define max(a, b)
Definition cfImp.cxx:41

◆ isNLO_from_run_card()

python.MadGraphUtilsHelpers.isNLO_from_run_card ( run_card)

Definition at line 108 of file MadGraphUtilsHelpers.py.

108def isNLO_from_run_card(run_card):
109 f = open(run_card,'r')
110 if "parton_shower" in f.read().lower():
111 f.close()
112 return True
113 else:
114 f.close()
115 return False
116

◆ settingIsTrue()

python.MadGraphUtilsHelpers.settingIsTrue ( setting)

Definition at line 30 of file MadGraphUtilsHelpers.py.

30def settingIsTrue(setting):
31 if setting.replace("'",'').replace('"','').replace('.','').lower() in ['t','true']:
32 return True
33 return False
34
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition hcg.cxx:310

◆ setup_path_protection()

python.MadGraphUtilsHelpers.setup_path_protection ( )

Definition at line 244 of file MadGraphUtilsHelpers.py.

244def setup_path_protection():
245 # Addition for models directory
246 global MADGRAPH_COMMAND_STACK
247 if 'PYTHONPATH' in os.environ:
248 if not any( [('Generators/madgraph/models' in x and 'shutil_patch' not in x) for x in os.environ['PYTHONPATH'].split(':') ]):
249 os.environ['PYTHONPATH'] += ':/cvmfs/atlas.cern.ch/repo/sw/Generators/madgraph/models/latest'
250 MADGRAPH_COMMAND_STACK += ['export PYTHONPATH=${PYTHONPATH}:/cvmfs/atlas.cern.ch/repo/sw/Generators/madgraph/models/latest']
251 # Make sure that gfortran doesn't write to somewhere it shouldn't
252 if 'GFORTRAN_TMPDIR' in os.environ:
253 return
254 if 'TMPDIR' in os.environ:
255 os.environ['GFORTRAN_TMPDIR']=os.environ['TMPDIR']
256 MADGRAPH_COMMAND_STACK += ['export GFORTRAN_TMPDIR=${TMPDIR}']
257 return
258 if 'TMP' in os.environ:
259 os.environ['GFORTRAN_TMPDIR']=os.environ['TMP']
260 MADGRAPH_COMMAND_STACK += ['export GFORTRAN_TMPDIR=${TMP}']
261 return
262

◆ totallyStripped()

python.MadGraphUtilsHelpers.totallyStripped ( x)

Definition at line 35 of file MadGraphUtilsHelpers.py.

35def totallyStripped(x):
36 y=str(x).lower().strip()
37 # remove leading and trailing "/'
38 while len(y)>0 and (y[0]=='"' or y[0]=="'"):
39 y=y[1:]
40 while len(y)>0 and (y[-1]=='"' or y[-1]=="'"):
41 y=y[:-1]
42 return y
43

◆ write_test_script()

python.MadGraphUtilsHelpers.write_test_script ( )

Definition at line 228 of file MadGraphUtilsHelpers.py.

228def write_test_script():
229 mglog.info('Will write a stand-alone debugging script.')
230 mglog.info('This is an attempt to provide you commands that you can use')
231 mglog.info('to reproduce the error locally. If you make additional')
232 mglog.info('modifications by hand (not using MadGraphControl) in your JO,')
233 mglog.info('make sure that you check and modify the script as needed.\n\n')
234 mglog.info('# Script start; trim off columns left of the "#"')
235 # Write offline stand-alone reproduction script
236 with open('standalone_script.sh','w') as standalone_script:
237 for command in MADGRAPH_COMMAND_STACK:
238 for line in command.split('\n'):
239 mglog.info(line)
240 standalone_script.write(line+'\n')
241 mglog.info('# Script end')
242 mglog.info('Script also written to %s/standalone_script.sh',os.getcwd())
243

Variable Documentation

◆ MADGRAPH_CATCH_ERRORS

bool python.MadGraphUtilsHelpers.MADGRAPH_CATCH_ERRORS = True

Definition at line 11 of file MadGraphUtilsHelpers.py.

◆ MADGRAPH_COMMAND_STACK

list python.MadGraphUtilsHelpers.MADGRAPH_COMMAND_STACK = []

Definition at line 12 of file MadGraphUtilsHelpers.py.

◆ MADGRAPH_GRIDPACK_LOCATION

str python.MadGraphUtilsHelpers.MADGRAPH_GRIDPACK_LOCATION = 'madevent'

Definition at line 9 of file MadGraphUtilsHelpers.py.

◆ mglog

python.MadGraphUtilsHelpers.mglog = Logging.logging.getLogger('MadGraphUtils')

Definition at line 6 of file MadGraphUtilsHelpers.py.