ATLAS Offline Software
Functions | Variables
python.MadGraphUtilsHelpers Namespace Reference

Functions

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

Variables

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

Function Documentation

◆ checkSetting()

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

Definition at line 44 of file MadGraphUtilsHelpers.py.

44 def 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()

def python.MadGraphUtilsHelpers.checkSettingExists (   key_,
  mydict_ 
)

Definition at line 59 of file MadGraphUtilsHelpers.py.

59 def 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()

def python.MadGraphUtilsHelpers.checkSettingIsTrue (   key_,
  mydict_ 
)

Definition at line 52 of file MadGraphUtilsHelpers.py.

52 def 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()

def python.MadGraphUtilsHelpers.error_check (   errors_a,
  return_code 
)

Definition at line 119 of file MadGraphUtilsHelpers.py.

119 def 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:
211  raise RuntimeError('Error detected in MadGraphControl process')
212  return
213 
214 
215 # Write a short test script for standalone debugging

◆ get_default_config_card()

def python.MadGraphUtilsHelpers.get_default_config_card (   process_dir = MADGRAPH_GRIDPACK_LOCATION)

Definition at line 251 of file MadGraphUtilsHelpers.py.

251 def 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()

def python.MadGraphUtilsHelpers.get_runArgs_info (   runArgs)

Definition at line 105 of file MadGraphUtilsHelpers.py.

105 def 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()

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

Definition at line 14 of file MadGraphUtilsHelpers.py.

14 def 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()

def python.MadGraphUtilsHelpers.is_NLO_run (   process_dir = MADGRAPH_GRIDPACK_LOCATION)

Definition at line 266 of file MadGraphUtilsHelpers.py.

266 def 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()

def python.MadGraphUtilsHelpers.is_version_or_newer (   args)

Definition at line 66 of file MadGraphUtilsHelpers.py.

66 def 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 

◆ isNLO_from_run_card()

def python.MadGraphUtilsHelpers.isNLO_from_run_card (   run_card)

Definition at line 96 of file MadGraphUtilsHelpers.py.

96 def 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()

def python.MadGraphUtilsHelpers.settingIsTrue (   setting)

Definition at line 30 of file MadGraphUtilsHelpers.py.

30 def settingIsTrue(setting):
31  if setting.replace("'",'').replace('"','').replace('.','').lower() in ['t','true']:
32  return True
33  return False
34 

◆ setup_path_protection()

def python.MadGraphUtilsHelpers.setup_path_protection ( )

Definition at line 232 of file MadGraphUtilsHelpers.py.

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()

def python.MadGraphUtilsHelpers.totallyStripped (   x)

Definition at line 35 of file MadGraphUtilsHelpers.py.

35 def 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()

def python.MadGraphUtilsHelpers.write_test_script ( )

Definition at line 216 of file MadGraphUtilsHelpers.py.

216 def 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

string 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.

replace
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition: hcg.cxx:310
python.MadGraphUtilsHelpers.isNLO_from_run_card
def isNLO_from_run_card(run_card)
Definition: MadGraphUtilsHelpers.py:96
createLinkingScheme.iter
iter
Definition: createLinkingScheme.py:62
python.MadGraphUtilsHelpers.settingIsTrue
def settingIsTrue(setting)
Definition: MadGraphUtilsHelpers.py:30
python.MadGraphUtilsHelpers.checkSettingIsTrue
def checkSettingIsTrue(key_, mydict_)
Definition: MadGraphUtilsHelpers.py:52
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
python.MadGraphUtilsHelpers.is_NLO_run
def is_NLO_run(process_dir=MADGRAPH_GRIDPACK_LOCATION)
Definition: MadGraphUtilsHelpers.py:266
python.MadGraphUtilsHelpers.get_runArgs_info
def get_runArgs_info(runArgs)
Definition: MadGraphUtilsHelpers.py:105
python.MadGraphUtilsHelpers.checkSetting
def checkSetting(key_, value_, mydict_)
Definition: MadGraphUtilsHelpers.py:44
python.MadGraphUtilsHelpers.setup_path_protection
def setup_path_protection()
Definition: MadGraphUtilsHelpers.py:232
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
python.MadGraphUtilsHelpers.write_test_script
def write_test_script()
Definition: MadGraphUtilsHelpers.py:216
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
python.MadGraphUtilsHelpers.get_default_config_card
def get_default_config_card(process_dir=MADGRAPH_GRIDPACK_LOCATION)
Definition: MadGraphUtilsHelpers.py:251
python.MadGraphUtilsHelpers.error_check
def error_check(errors_a, return_code)
Definition: MadGraphUtilsHelpers.py:119
python.MadGraphUtilsHelpers.checkSettingExists
def checkSettingExists(key_, mydict_)
Definition: MadGraphUtilsHelpers.py:59
Trk::open
@ open
Definition: BinningType.h:40
python.CaloAddPedShiftConfig.int
int
Definition: CaloAddPedShiftConfig.py:45
str
Definition: BTagTrackIpAccessor.cxx:11
python.MadGraphUtilsHelpers.totallyStripped
def totallyStripped(x)
Definition: MadGraphUtilsHelpers.py:35
Trk::split
@ split
Definition: LayerMaterialProperties.h:38
python.MadGraphUtilsHelpers.getDictFromCard
def getDictFromCard(card_loc, lowercase=False)
Definition: MadGraphUtilsHelpers.py:14
python.MadGraphUtilsHelpers.is_version_or_newer
def is_version_or_newer(args)
Definition: MadGraphUtilsHelpers.py:66