Package madgraph :: Package various :: Module shower_card
[hide private]
[frames] | no frames]

Source Code for Module madgraph.various.shower_card

  1  ################################################################################ 
  2  # 
  3  # Copyright (c) 2011 The MadGraph5_aMC@NLO Development team and Contributors 
  4  # 
  5  # This file is a part of the MadGraph5_aMC@NLO project, an application which  
  6  # automatically generates Feynman diagrams and matrix elements for arbitrary 
  7  # high-energy processes in the Standard Model and beyond. 
  8  # 
  9  # It is subject to the MadGraph5_aMC@NLO license which should accompany this  
 10  # distribution. 
 11  # 
 12  # For more information, visit madgraph.phys.ucl.ac.be and amcatnlo.web.cern.ch 
 13  # 
 14  ################################################################################ 
 15  """A File for splitting""" 
 16   
 17  import sys 
 18  import re 
 19  import os 
 20  import logging 
 21   
 22  logger = logging.getLogger('madgraph.shower_card')  
 23   
 24  pjoin = os.path.join 
 25   
26 -class ShowerCardError(Exception):
27 pass
28
29 -class ShowerCard(dict):
30 """ """ 31 true = ['.true.', 't', 'true', '1'] 32 false = ['.false.', 'f', 'false', '0'] 33 logical_vars = ['ue_enabled', 'hadronize', 'b_stable', 'pi_stable', 'wp_stable', 34 'wm_stable', 'z_stable', 'h_stable', 'tap_stable', 'tam_stable', 35 'mup_stable', 'mum_stable', 'is_4lep', 'is_bbar', 'combine_td'] 36 string_vars = ['extralibs', 'extrapaths', 'includepaths', 'analyse'] 37 for i in range(1,100): 38 string_vars.append('dm_'+str(i)) 39 int_vars = ['nsplit_jobs', 'maxprint', 'nevents', 'pdfcode', 'rnd_seed', 'rnd_seed2', 'njmax'] 40 float_vars = ['maxerrs', 'lambda_5', 'b_mass', 'qcut'] 41 42 # names_dict has the following structure: 43 # var : {PYTHIA6: varpy6, HERWIG6: varhw6, HERWIGPP: varhwpp, PYTHIA8: varpy8} 44 # where varpy, varhw6 and varhwpp are mc_dependent names 45 # if a mc is not there, that variable is not supposed to be 46 # used / written for thar mc 47 names_dict = {\ 48 'ue_enabled' : {'HERWIG6':'lhsoft', 'PYTHIA6': 'mstp_81', 'HERWIGPP': 'ue_hwpp', 'PYTHIA8': 'ue_py8'}, 49 'pdfcode' : {'HERWIG6':'pdfcode', 'PYTHIA6': 'pdfcode', 'HERWIGPP': 'pdfcode', 'PYTHIA8': 'pdfcode'}, 50 'nevents' : {'HERWIG6':'nevents', 'PYTHIA6': 'nevents', 'HERWIGPP': 'nevents', 'PYTHIA8': 'nevents'}, 51 'hadronize' : {'PYTHIA6': 'mstp_111', 'HERWIGPP': 'hadronize_hwpp', 'PYTHIA8': 'hadronize_py8'}, 52 'b_stable' : {'HERWIG6':'b_stable_hw', 'PYTHIA6': 'b_stable_py', 'HERWIGPP': 'b_stable_hwpp', 'PYTHIA8': 'b_stable_py8'}, 53 'pi_stable' : {'HERWIG6':'pi_stable_hw', 'PYTHIA6': 'pi_stable_py', 'HERWIGPP': 'pi_stable_hwpp', 'PYTHIA8': 'pi_stable_py8'}, 54 'wp_stable' : {'HERWIG6':'wp_stable_hw', 'PYTHIA6': 'wp_stable_py', 'HERWIGPP': 'wp_stable_hwpp', 'PYTHIA8': 'wp_stable_py8'}, 55 'wm_stable' : {'HERWIG6':'wm_stable_hw', 'PYTHIA6': 'wm_stable_py', 'HERWIGPP': 'wm_stable_hwpp', 'PYTHIA8': 'wm_stable_py8'}, 56 'z_stable' : {'HERWIG6':'z_stable_hw', 'PYTHIA6': 'z_stable_py', 'HERWIGPP': 'z_stable_hwpp', 'PYTHIA8': 'z_stable_py8'}, 57 'h_stable' : {'HERWIG6':'h_stable_hw', 'PYTHIA6': 'h_stable_py', 'HERWIGPP': 'h_stable_hwpp', 'PYTHIA8': 'h_stable_py8'}, 58 'tap_stable' : {'HERWIG6':'taup_stable_hw', 'PYTHIA6': 'taup_stable_py', 'HERWIGPP': 'taup_stable_hwpp', 'PYTHIA8': 'taup_stable_py8'}, 59 'tam_stable' : {'HERWIG6':'taum_stable_hw', 'PYTHIA6': 'taum_stable_py', 'HERWIGPP': 'taum_stable_hwpp', 'PYTHIA8': 'taum_stable_py8'}, 60 'mup_stable' : {'HERWIG6':'mup_stable_hw', 'PYTHIA6': 'mup_stable_py', 'HERWIGPP': 'mup_stable_hwpp', 'PYTHIA8': 'mup_stable_py8'}, 61 'mum_stable' : {'HERWIG6':'mum_stable_hw', 'PYTHIA6': 'mum_stable_py', 'HERWIGPP': 'mum_stable_hwpp', 'PYTHIA8': 'mum_stable_py8'}, 62 'is_4lep' : {'PYTHIA6':'is_4l_py'}, 63 'is_bbar' : {'HERWIG6':'is_bb_hw'}, 64 'maxprint' : {'HERWIG6':'maxpr_hw', 'PYTHIA6': 'maxpr_py', 'HERWIGPP': 'maxpr_hwpp', 'PYTHIA8': 'maxpr_py8'}, 65 'rnd_seed' : {'HERWIG6':'rndevseed1_hw', 'PYTHIA6': 'rndevseed_py', 'HERWIGPP': 'rndevseed_hwpp', 'PYTHIA8': 'rndevseed_py8'}, 66 'rnd_seed2' : {'HERWIG6':'rndevseed2_hw'}, 67 'maxerrs' : {'HERWIG6':'err_fr_hw', 'PYTHIA6': 'err_fr_py', 'HERWIGPP': 'err_fr_hwpp', 'PYTHIA8': 'err_fr_py8'}, 68 'lambda_5' : {'HERWIG6':'lambdaherw', 'PYTHIA6': 'lambdapyth', 'HERWIGPP': 'lambdaherw', 'PYTHIA8': 'lambdapyth'}, 69 'b_mass' : {'HERWIG6':'b_mass', 'PYTHIA6': 'b_mass', 'HERWIGPP': 'b_mass', 'PYTHIA8': 'b_mass'}, 70 'analyse' : {'HERWIG6':'hwuti', 'PYTHIA6':'pyuti', 'HERWIGPP':'hwpputi', 'PYTHIA8':'py8uti'}, 71 'qcut' : {'PYTHIA8':'qcut'}, 72 'njmax' : {'PYTHIA8':'njmax'}} 73 74 stdhep_dict = {'HERWIG6':'mcatnlo_hwan_stdhep.o', 'PYTHIA6':'mcatnlo_pyan_stdhep.o'} 75
76 - def __init__(self, card=None, testing=False):
77 """ if testing, card is the content""" 78 self.testing = testing 79 dict.__init__(self) 80 self.keylist = self.keys() 81 82 if card: 83 self.read_card(card)
84 85
86 - def read_card(self, card_path):
87 """read the shower_card, if testing card_path is the content""" 88 if not self.testing: 89 content = open(card_path).read() 90 else: 91 content = card_path 92 lines = content.split('\n') 93 list_dm = [] 94 for l in lines: 95 if '#' in l: 96 l = l.split('#',1)[0] 97 if '=' not in l: 98 continue 99 args = l.split('=',1) # here the 1 is important in case of string passed 100 key = args[0].strip().lower() 101 value = args[1].strip() 102 self.set_param(key, value) 103 if str(key).upper().startswith('DM'): 104 list_dm.append(int(key.split('_',1)[1])) 105 #special case for DM_* 106 for i in range(1,100): 107 if not i in list_dm: 108 self['dm_'+str(i)] = '' 109 110 self.text=content
111 112
113 - def set_param(self, key, value, write_to = ''):
114 """set the param key to value. 115 if write_to is passed then write the new shower_card: 116 if not testing write_to is an input path, if testing the text is 117 returned by the function 118 """ 119 if key in self.logical_vars: 120 if str(value).lower() in self.true: 121 self[key] = True 122 elif str(value).lower() in self.false: 123 self[key] = False 124 else: 125 raise ShowerCardError('%s is not a valid value for %s' % \ 126 (value, key)) 127 elif key in self.string_vars: 128 if value.lower() == 'none': 129 self[key] = '' 130 else: 131 self[key] = value 132 elif key in self.int_vars: 133 try: 134 self[key] = int(value) 135 except ValueError: 136 raise ShowerCardError('%s is not a valid value for %s. An integer number is expected' % \ 137 (value, key)) 138 elif key in self.float_vars: 139 try: 140 self[key] = float(value) 141 except ValueError: 142 raise ShowerCardError('%s is not a valid value for %s. A floating point number is expected' % \ 143 (value, key)) 144 else: 145 raise ShowerCardError('Unknown entry: %s = %s' % (key, value)) 146 self.keylist.append(key) 147 148 #then update self.text and write the new card 149 if write_to: 150 logger.info('modify parameter %s of the shower_card.dat to %s' % (key, value)) 151 key_re = re.compile('^(\s*)%s\s*=\s*(.+)\s*$' % key , re.IGNORECASE) 152 newlines = [] 153 for line in self.text.split('\n'): 154 key_match = key_re.match(line) 155 if key_match and not ( str(key).upper().startswith('DM') ): 156 try: 157 comment = line.split('#')[1] 158 except: 159 comment = '' 160 if key not in self.logical_vars: 161 newlines.append('%s = %s #%s' % (key, value, comment)) 162 else: 163 if key: 164 newlines.append('%s = %s #%s' % (key, 'T', comment)) 165 else: 166 newlines.append('%s = %s #%s' % (key, 'F', comment)) 167 elif key_match and ( str(key).upper().startswith('DM') ): 168 pass 169 else: 170 newlines.append(line) 171 172 if str(key).upper().startswith('DM') and not value.lower() in ['','none','default']: 173 newlines.append('%s = %s' % (str(key).upper(), value[0:len(value)])) 174 logger.info('please specify a decay through set DM_1 decay; see shower_card.dat for details') 175 176 self.text = '\n'.join(newlines) + '\n' 177 178 if self.testing: 179 return self.text 180 else: 181 open(write_to, 'w').write(self.text) 182 return '' 183 else: 184 return ''
185 186 187
188 - def write_card(self, shower, card_path):
189 """write the shower_card for shower in card_path. 190 if self.testing, card_path takes the value of the string""" 191 192 shower = shower.upper() 193 if shower.startswith('PYTHIA6'): 194 self.shower = 'PYTHIA6' 195 else: 196 self.shower = shower 197 lines = [] 198 bool_dict = {True: '.true.', False: '.false.'} 199 bool_dict_num = {True: '1', False: '0'} 200 201 for key in self.keylist: 202 value = self[key] 203 if key in self.logical_vars: 204 # deal with special case for pythia: 205 if key in ['ue_enabled', 'hadronize'] and self.shower == 'PYTHIA6': 206 value = bool_dict_num[value] 207 else: 208 value = bool_dict[value] 209 elif key in self.string_vars: 210 # deal in a special way with analyse 211 if key == 'analyse': 212 if value is None or not value: 213 try: 214 value = self.stdhep_dict[self.shower] 215 except KeyError: 216 pass 217 try: 218 line = '%s="%s"' % (self.names_dict[key][self.shower].upper(), value) 219 lines.append(line) 220 continue 221 except KeyError: 222 continue 223 if value is None or not value: 224 value = '' 225 else: 226 value = '"%s"' % value 227 228 line = '%s=%s' % (key.upper(), value) 229 lines.append(line) 230 continue 231 elif key in self.int_vars: 232 value = '%d' % value 233 elif key in self.float_vars: 234 value = '%4.3f' % value 235 else: 236 raise ShowerCardError('Unknown key: %s = %s' % (key, value)) 237 try: 238 line = '%s=%s' % (self.names_dict[key][self.shower].upper(), value.upper()) 239 lines.append(line) 240 except KeyError: 241 pass 242 243 if self.testing: 244 return ('\n'.join(lines) + '\n') 245 else: 246 open(card_path, 'w').write(('\n'.join(lines) + '\n'))
247