Package models :: Module write_param_card
[hide private]
[frames] | no frames]

Source Code for Module models.write_param_card

  1  ################################################################################ 
  2  # 
  3  # Copyright (c) 2010 The MadGraph Development team and Contributors 
  4  # 
  5  # This file is a part of the MadGraph 5 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 MadGraph license which should accompany this  
 10  # distribution. 
 11  # 
 12  # For more information, please visit: http://madgraph.phys.ucl.ac.be 
 13  # 
 14  ################################################################################ 
 15  import models.model_reader as model_reader 
 16  import madgraph.core.base_objects as base_objects 
17 18 19 -class ParamCardWriterError(Exception):
20 """ a error class for this file """
21
22 -class ParamCardWriter(object):
23 """ A class for writting an update param_card for a given model """ 24 25 header = \ 26 """######################################################################\n""" + \ 27 """## PARAM_CARD AUTOMATICALY GENERATED BY MG5 FOLLOWING UFO MODEL ####\n""" + \ 28 """######################################################################\n""" 29 30 sm_pdg = [1,2,3,4,5,6,11,12,13,13,14,15,16,21,22,23,24,25] 31 qnumber_str ="""Block QNUMBERS %(pdg)d # %(name)s 32 1 %(charge)d # 3 times electric charge 33 2 %(spin)d # number of spin states (2S+1) 34 3 %(color)d # colour rep (1: singlet, 3: triplet, 8: octet) 35 4 %(antipart)d # Particle/Antiparticle distinction (0=own anti)\n""" 36 37
38 - def __init__(self, model, filepath=None):
39 """ model is a valid MG5 model, filepath is the path were to write the 40 param_card.dat """ 41 42 # Compute the value of all dependant parameter 43 if isinstance(model, model_reader.ModelReader): 44 self.model = model 45 else: 46 self.model = model_reader.ModelReader(model) 47 self.model.set_parameters_and_couplings() 48 49 50 # Organize the data 51 self.external = self.model['parameters'][('external',)] 52 self.param_dict = self.create_param_dict() 53 self.define_not_dep_param() 54 55 if filepath: 56 self.define_output_file(filepath) 57 self.write_card()
58 59
60 - def create_param_dict(self):
61 """ return {'name': parameterObject}""" 62 63 out = {} 64 for key, params in self.model['parameters'].items(): 65 for param in params: 66 out[param.name] = param 67 68 if 'ZERO' not in out.keys(): 69 zero = base_objects.ModelVariable('ZERO', '0', 'real') 70 out['ZERO'] = zero 71 return out
72 73
74 - def define_not_dep_param(self):
75 """define self.dep_mass and self.dep_width in case that they are 76 requested in the param_card.dat""" 77 78 all_particles = self.model['particles'] 79 80 # 81 self.dep_mass, self.dep_width = [] , [] 82 self.duplicate_mass, self.duplicate_width =[], [] 83 84 def_param = [] 85 # one loop for the mass 86 for p in all_particles: 87 mass = self.param_dict[p["mass"]] 88 if mass in def_param: 89 self.duplicate_mass.append((p, mass)) 90 continue 91 elif p["mass"] != 'ZERO': 92 def_param.append(mass) 93 if p['mass'] not in self.external: 94 self.dep_mass.append((p, mass)) 95 96 # one loop for the width 97 def_param = [] 98 for p in all_particles: 99 width = self.param_dict[p["width"]] 100 if width in def_param: 101 self.duplicate_width.append((p, width)) 102 continue 103 else: 104 if p["width"] != 'ZERO': 105 def_param.append(width) 106 if p['width'] not in self.external: 107 self.dep_width.append((p, width))
108 109 110 111 @staticmethod
112 - def order_param(obj1, obj2):
113 """ order parameter of a given block """ 114 115 if obj1.lhablock == obj2.lhablock: 116 pass 117 elif obj1.lhablock == 'DECAY': 118 return 1 119 elif obj2.lhablock == 'DECAY': 120 return -1 121 elif obj1.lhablock < obj2.lhablock: 122 return -1 123 elif obj1.lhablock != obj2.lhablock: 124 return 1 125 126 maxlen = min([len(obj1.lhacode), len(obj2.lhacode)]) 127 128 for i in range(maxlen): 129 if obj1.lhacode[i] < obj2.lhacode[i]: 130 return -1 131 elif obj1.lhacode[i] > obj2.lhacode[i]: 132 return 1 133 134 #identical up to the first finish 135 if len(obj1.lhacode) > len(obj2.lhacode): 136 return 1 137 elif len(obj1.lhacode) == len(obj2.lhacode): 138 return 0 139 else: 140 return -1
141
142 - def define_output_file(self, path, mode='w'):
143 """ initialize the file""" 144 145 if isinstance(path, str): 146 self.fsock = open(path, mode) 147 else: 148 self.fsock = path # prebuild file/IOstring 149 150 self.fsock.write(self.header)
151
152 - def write_card(self, path=None):
153 """schedular for writing a card""" 154 155 if path: 156 self.define_input_file(path) 157 158 # order the parameter in a smart way 159 self.external.sort(self.order_param) 160 161 cur_lhablock = '' 162 for param in self.external: 163 #check if we change of lhablock 164 if cur_lhablock != param.lhablock: 165 # check if some dependent param should be written 166 self.write_dep_param_block(cur_lhablock) 167 cur_lhablock = param.lhablock 168 # write the header of the new block 169 self.write_block(cur_lhablock) 170 #write the parameter 171 self.write_param(param, cur_lhablock) 172 self.write_dep_param_block(cur_lhablock) 173 self.write_qnumber()
174
175 - def write_block(self, name):
176 """ write a comment for a block""" 177 178 self.fsock.writelines( 179 """\n###################################""" + \ 180 """\n## INFORMATION FOR %s""" % name.upper() +\ 181 """\n###################################\n""" 182 ) 183 if name!='DECAY': 184 self.fsock.write("""Block %s \n""" % name.lower())
185
186 - def write_param(self, param, lhablock):
187 """ write the line corresponding to a given parameter """ 188 189 if hasattr(param, 'info'): 190 info = param.info 191 else: 192 info = param.name 193 194 if param.value.imag != 0: 195 raise ParamCardWriterError, 'All External Parameter should be real' 196 197 lhacode=' '.join(['%3s' % key for key in param.lhacode]) 198 if lhablock != 'DECAY': 199 text = """ %s %e # %s \n""" % (lhacode, param.value.real, info) 200 else: 201 text = '''DECAY %s %e # %s \n''' % (lhacode, param.value.real, info) 202 self.fsock.write(text)
203 204
205 - def write_dep_param_block(self, lhablock):
206 """writing the requested LHA parameter""" 207 208 if lhablock == 'MASS': 209 data = self.dep_mass 210 prefix = " " 211 elif lhablock == 'DECAY': 212 data = self.dep_width 213 prefix = "DECAY " 214 else: 215 return 216 217 text = "" 218 for part, param in data: 219 if self.model['parameter_dict'][param.name].imag: 220 raise ParamCardWriterError, 'All Mass/Width Parameter should be real' 221 value = complex(self.model['parameter_dict'][param.name]).real 222 text += """%s %s %f # %s : %s \n""" %(prefix, part["pdg_code"], 223 value, part["name"], param.expr) 224 225 # Add duplicate parameter 226 if lhablock == 'MASS': 227 data = self.duplicate_mass 228 name = 'mass' 229 elif lhablock == 'DECAY': 230 data = self.duplicate_width 231 name = 'width' 232 233 for part, param in data: 234 if self.model['parameter_dict'][param.name].imag: 235 raise ParamCardWriterError, 'All Mass/Width Parameter should be real' 236 value = complex(self.model['parameter_dict'][param.name]).real 237 text += """%s %s %f # %s : %s \n""" %(prefix, part["pdg_code"], 238 value, part["name"], part[name]) 239 240 if not text: 241 return 242 243 pretext = "## Not dependent paramater.\n" 244 pretext += "## Those values should be edited following the \n" 245 pretext += "## analytical expression. MG5 ignore those values \n" 246 pretext += "## but they are important for interfacing the output of MG5\n" 247 pretext += "## to external program such as Pythia.\n" 248 self.fsock.write(pretext + text)
249 250
251 - def write_qnumber(self):
252 """ write qnumber """ 253 254 def is_anti(logical): 255 if logical: 256 return 0 257 else: 258 return 1
259 260 261 text = "" 262 for part in self.model['particles']: 263 if part["pdg_code"] in self.sm_pdg or part["pdg_code"] < 0: 264 continue 265 text += self.qnumber_str % {'pdg': part["pdg_code"], 266 'name': part["name"], 267 'charge': 3 * part["charge"], 268 'spin': part["spin"], 269 'color': part["color"], 270 'antipart': is_anti(part['self_antipart'])} 271 272 if text: 273 pretext="""#===========================================================\n""" 274 pretext += """# QUANTUM NUMBERS OF NEW STATE(S) (NON SM PDG CODE)\n""" 275 pretext += """#===========================================================\n\n""" 276 277 self.fsock.write(pretext + text)
278 279 280 281 282 283 284 285 286 if '__main__' == __name__: 287 ParamCardWriter('./param_card.dat', generic=True) 288 print 'write ./param_card.dat' 289