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_)
 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 119 of file MadGraphUtilsHelpers.py.

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

251def get_default_config_card(process_dir=MADGRAPH_GRIDPACK_LOCATION):
252
253 lo_config_card=process_dir+'/Cards/me5_configuration.txt'
254 nlo_config_card=process_dir+'/Cards/amcatnlo_configuration.txt'
255
256 if os.access(lo_config_card,os.R_OK) and not os.access(nlo_config_card,os.R_OK):
257 return lo_config_card
258 elif os.access(nlo_config_card,os.R_OK) and not os.access(lo_config_card,os.R_OK):
259 return nlo_config_card
260 elif os.access(nlo_config_card,os.R_OK) and os.access(lo_config_card,os.R_OK):
261 mglog.error('Found both types of config card in '+process_dir)
262 else:
263 mglog.error('No config card in '+process_dir)
264 raise RuntimeError('Unable to locate configuration card')
265

◆ get_runArgs_info()

python.MadGraphUtilsHelpers.get_runArgs_info ( runArgs)

Definition at line 105 of file MadGraphUtilsHelpers.py.

105def get_runArgs_info(runArgs):
106 if runArgs is None:
107 raise RuntimeError('runArgs must be provided!')
108 if hasattr(runArgs,'ecmEnergy'):
109 beamEnergy = runArgs.ecmEnergy / 2.
110 else:
111 raise RuntimeError("No center of mass energy found in runArgs.")
112 if hasattr(runArgs,'randomSeed'):
113 random_seed = runArgs.randomSeed
114 else:
115 raise RuntimeError("No random seed found in runArgs.")
116 return beamEnergy,random_seed
117
118

◆ 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 266 of file MadGraphUtilsHelpers.py.

266def is_NLO_run(process_dir=MADGRAPH_GRIDPACK_LOCATION):
267 # Very simple check based on the above config card grabbing
268 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 66 of file MadGraphUtilsHelpers.py.

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

◆ isNLO_from_run_card()

python.MadGraphUtilsHelpers.isNLO_from_run_card ( run_card)

Definition at line 96 of file MadGraphUtilsHelpers.py.

96def isNLO_from_run_card(run_card):
97 f = open(run_card,'r')
98 if "parton_shower" in f.read().lower():
99 f.close()
100 return True
101 else:
102 f.close()
103 return False
104

◆ 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 232 of file MadGraphUtilsHelpers.py.

232def setup_path_protection():
233 # Addition for models directory
234 global MADGRAPH_COMMAND_STACK
235 if 'PYTHONPATH' in os.environ:
236 if not any( [('Generators/madgraph/models' in x and 'shutil_patch' not in x) for x in os.environ['PYTHONPATH'].split(':') ]):
237 os.environ['PYTHONPATH'] += ':/cvmfs/atlas.cern.ch/repo/sw/Generators/madgraph/models/latest'
238 MADGRAPH_COMMAND_STACK += ['export PYTHONPATH=${PYTHONPATH}:/cvmfs/atlas.cern.ch/repo/sw/Generators/madgraph/models/latest']
239 # Make sure that gfortran doesn't write to somewhere it shouldn't
240 if 'GFORTRAN_TMPDIR' in os.environ:
241 return
242 if 'TMPDIR' in os.environ:
243 os.environ['GFORTRAN_TMPDIR']=os.environ['TMPDIR']
244 MADGRAPH_COMMAND_STACK += ['export GFORTRAN_TMPDIR=${TMPDIR}']
245 return
246 if 'TMP' in os.environ:
247 os.environ['GFORTRAN_TMPDIR']=os.environ['TMP']
248 MADGRAPH_COMMAND_STACK += ['export GFORTRAN_TMPDIR=${TMP}']
249 return
250

◆ 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 216 of file MadGraphUtilsHelpers.py.

216def write_test_script():
217 mglog.info('Will write a stand-alone debugging script.')
218 mglog.info('This is an attempt to provide you commands that you can use')
219 mglog.info('to reproduce the error locally. If you make additional')
220 mglog.info('modifications by hand (not using MadGraphControl) in your JO,')
221 mglog.info('make sure that you check and modify the script as needed.\n\n')
222 mglog.info('# Script start; trim off columns left of the "#"')
223 # Write offline stand-alone reproduction script
224 with open('standalone_script.sh','w') as standalone_script:
225 for command in MADGRAPH_COMMAND_STACK:
226 for line in command.split('\n'):
227 mglog.info(line)
228 standalone_script.write(line+'\n')
229 mglog.info('# Script end')
230 mglog.info('Script also written to %s/standalone_script.sh',os.getcwd())
231

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.