pyslha.py

Sun, 28 Apr 2013 22:17:25 +0200

author
Andy Buckley <andy@insectnation.org>
date
Sun, 28 Apr 2013 22:17:25 +0200
changeset 205
536967068fe2
parent 204
bef82eaef56e
child 207
2990422f1738
permissions
-rw-r--r--

Typo fix

andy@1 1 #! /usr/bin/env python
andy@1 2
andy@8 3 """\
andy@8 4 A simple but flexible parser of SUSY Les Houches Accord (SLHA) model and decay files.
andy@25 5
andy@52 6 pyslha is a parser/writer module for particle physics SUSY Les Houches Accord
andy@52 7 (SLHA) supersymmetric spectrum/decay files, and a collection of scripts which
andy@52 8 use the interface, e.g. for conversion to and from the legacy ISAWIG format, or
andy@52 9 to plot the mass spectrum and decay chains.
andy@52 10
andy@192 11 The current release supports SLHA version 1, and as far as I'm aware is also
andy@145 12 fully compatible with SLHA2: the block structures are read and accessed
andy@192 13 generically. If you have any problems, please provide an example input file and
andy@192 14 I'll investigate.
andy@52 15
andy@131 16 The plotting script provides output in PDF, EPS and PNG via LaTeX and the TikZ
andy@131 17 graphics package, and as LaTeX/TikZ source for direct embedding into documents or
andy@131 18 user-tweaking of the generated output.
andy@64 19
andy@52 20 TODOs:
andy@192 21
andy@197 22 For 2.0.0:
andy@198 23 * Output column alignment cosmetics
andy@198 24 * Precision setting obedience in SLHA output of values
andy@198 25
andy@198 26 For 2.1.0:
andy@188 27 * Preserve comments from read -> write (needs full-line/inline comment separation?)
andy@198 28 * Consider whether Block should inherit direct from dict
andy@203 29 * Split writeSLHA into writeSLHA{Blocks,Decays}
andy@192 30
andy@192 31 Later/maybe:
andy@64 32 * Identify HERWIG decay matrix element to use in ISAWIG
andy@52 33 * Handle RPV SUSY in ISAWIG
andy@8 34 """
andy@8 35
andy@8 36 __author__ = "Andy Buckley <andy.buckley@cern.ch"
andy@197 37 __version__ = "2.0.0a0"
andy@8 38
andy@1 39
andy@189 40 def _mkdict():
andy@189 41 """Try to return an empty ordered dict, but fall back to normal dict if necessary"""
andy@189 42 try:
andy@189 43 from collections import OrderedDict
andy@189 44 return OrderedDict()
andy@189 45 except:
andy@189 46 try:
andy@189 47 from ordereddict import OrderedDict
andy@189 48 return OrderedDict()
andy@189 49 except:
andy@189 50 return dict()
andy@189 51
andy@4 52 def _autotype(var):
andy@30 53 """Automatically convert strings to numerical types if possible."""
andy@30 54 if type(var) is not str:
andy@4 55 return var
andy@36 56 if var.isdigit() or (var.startswith("-") and var[1:].isdigit()):
andy@4 57 return int(var)
andy@4 58 try:
andy@4 59 f = float(var)
andy@4 60 return f
andy@4 61 except ValueError:
andy@4 62 return var
andy@4 63
andy@147 64 def _autostr(var, precision=8):
andy@192 65 """Automatically format numerical types as the right sort of string."""
andy@30 66 if type(var) is float:
andy@147 67 return ("%." + str(precision) + "e") % var
andy@30 68 return str(var)
andy@30 69
andy@30 70
andy@193 71 class AccessError(Exception):
andy@193 72 "Exception object to be raised when a SLHA block is accessed in an invalid way"
andy@193 73 def __init__(self, errmsg):
andy@193 74 self.msg = errmsg
andy@193 75 def __str__(self):
andy@193 76 return self.msg
andy@193 77
andy@166 78 class ParseError(Exception):
andy@166 79 "Exception object to be raised when a spectrum file/string is malformed"
andy@166 80 def __init__(self, errmsg):
andy@166 81 self.msg = errmsg
andy@168 82 def __str__(self):
andy@166 83 return self.msg
andy@166 84
andy@166 85
andy@4 86
andy@12 87 class Block(object):
andy@8 88 """
andy@203 89 Object representation of any BLOCK elements read from an SLHA file.
andy@203 90
andy@203 91 Blocks have a name, may have an associated Q value, and contain a collection
andy@203 92 of data entries, each indexed by one or more keys. Entries in the dictionary
andy@203 93 are stored as numeric types (int or float) when a cast from the string in
andy@203 94 the file has been possible.
andy@203 95
andy@203 96 Block is closely related to a Python dict (and, in fact, is implemented via
andy@203 97 an OrderedDict when possible). The preferred methods of entry access use the
andy@203 98 dict-like [] operator for getting and setting, and the keys() and items()
andy@203 99 methods for iteration. Purely iterating over the object behaves like keys(),
andy@203 100 as for an ordinary dict.
andy@203 101
andy@203 102 Multiple indices are possible, especially for entries in mixing matrix
andy@203 103 blocks. These are now implemented in the natural way, e.g. for access to the
andy@203 104 (1,2) element of a mxing matrix block, use bmix[1,2] = 0.123 and print
andy@203 105 bmix[1,2]. The value() and set_value() functions behave similarly.
andy@203 106
andy@203 107 It is possible, although not usual, to store unindexed values in a
andy@203 108 block. This is only supported when that entry is the only one in the block,
andy@203 109 and it is stored in the normal fashion but with None as the lookup key. The
andy@203 110 value() method may be used without a key argument to retrieve this value, if
andy@203 111 the is_single_valued() method returns True, and similarly the set_value()
andy@203 112 method may be used to set it if only one argument is supplied and the object
andy@203 113 is compatible.
andy@8 114 """
andy@204 115 def __init__(self, name, q=None, entries=None):
andy@1 116 self.name = name
andy@189 117 self.entries = _mkdict()
andy@204 118 if entries is not None:
andy@204 119 self.entries.update(entries)
andy@5 120 self.q = _autotype(q)
andy@1 121
andy@1 122 def add_entry(self, entry):
andy@203 123 """Add an entry to the block from an iterable (i.e. list or tuple) or
andy@203 124 whitespace-separated string.
andy@203 125
andy@196 126 Indexing will be determined automatically such that there is always a
andy@196 127 single-element value: multi-value or None indices may be constructed
andy@196 128 implicitly.
andy@196 129 """
andy@203 130 ## If the argument is a single string, split it and proceed
andy@203 131 if type(entry) is str:
andy@203 132 entry = entry.split()
andy@203 133 ## Check that the arg is an iterable
andy@196 134 if not hasattr(entry, "__iter__"):
andy@196 135 raise AccessError("Block entries must be iterable")
andy@203 136 ## Auto-convert the types in the list
andy@192 137 entry = map(_autotype, entry)
andy@203 138 ## Add the entry to the map, with appropriate indices
andy@196 139 if len(entry) == 1:
andy@196 140 self.entries[None] = entry[0]
andy@192 141 elif len(entry) == 2:
andy@192 142 self.entries[entry[0]] = entry[1]
andy@192 143 else:
andy@192 144 self.entries[tuple(entry[:-1])] = entry[-1]
andy@192 145
andy@196 146 def is_single_valued(self):
andy@196 147 """Return true if there is only one entry, and it has no index: the
andy@196 148 'value()' attribute may be used in that case without an argument."""
andy@199 149 return len(self.entries) == 1 and self.entries.keys()[0] is None
andy@196 150
andy@196 151 def value(self, key=None):
andy@196 152 """Get a value from the block with the supplied key.
andy@196 153
andy@196 154 If no key is given, then the block must contain only one non-indexed
andy@196 155 value otherwise an AccessError exception will be raised.\
andy@196 156 """
andy@196 157 if key == None and not self.is_single_valued():
andy@196 158 raise AccessError("Tried to access unique value of multi-value block")
andy@196 159 return self.entries[key]
andy@196 160
andy@203 161 def set_value(self, *args):
andy@203 162 """Set a value in the block with supplied key, val args (or just val for
andy@203 163 an unindexed block).
andy@203 164
andy@203 165 If no key is given, then the block must contain only one non-indexed
andy@203 166 value otherwise an AccessError exception will be raised.\
andy@203 167 """
andy@203 168 if len(args) == 0:
andy@203 169 raise AccessError("set_value() called without arguments")
andy@203 170 elif len(args) == 1:
andy@203 171 if len(self.entries) > 0 and not self.is_single_valued():
andy@203 172 raise AccessError("Tried to set a unique value on a multi-value block")
andy@203 173 self.entries[None] = args[0]
andy@203 174 else:
andy@203 175 self.entries[tuple(args[:-1])] = args[-1]
andy@203 176
andy@205 177 def has_key(self, key):
andy@205 178 """Does the block have the given key?"""
andy@205 179 return self.entries.has_key(key)
andy@205 180
andy@200 181 def keys(self):
andy@200 182 """Access the block item keys."""
andy@201 183 return self.entries.keys()
andy@200 184
andy@195 185 def items(self, key=None):
andy@196 186 """Access the block items as (key,value) tuples.
andy@196 187
andy@196 188 Note: The Python 3 dict attribute 'items()' is used rather than the
andy@196 189 'old' Python 2 'iteritems()' name for forward-looking compatibility.\
andy@196 190 """
andy@193 191 return self.entries.iteritems()
andy@193 192
andy@193 193 def __len__(self):
andy@193 194 return len(self.entries)
andy@193 195
andy@196 196 def __iter(self):
andy@196 197 return self.entries.__iter__()
andy@196 198
andy@192 199 def __getitem__(self, key):
andy@192 200 return self.entries[key]
andy@1 201
andy@201 202 def __setitem__(self, key, value):
andy@201 203 self.entries[key] = value
andy@201 204
andy@1 205 def __cmp__(self, other):
andy@196 206 return cmp(self.name, other.name) and cmp(self.entries, other.entries)
andy@1 207
andy@193 208 def __repr__(self):
andy@1 209 s = self.name
andy@5 210 if self.q is not None:
andy@5 211 s += " (Q=%s)" % self.q
andy@196 212 def _format_kv(k, v):
andy@196 213 if type(k) is not tuple:
andy@196 214 s = "%r" % k
andy@196 215 else:
andy@196 216 s = ",".join("%r" % subindex for subindex in k)
andy@196 217 s += " : %r" % v
andy@196 218 return s
andy@196 219 s += " { " + "; ".join(_format_kv(k, v) for k, v in self.items()) + " }"
andy@1 220 return s
andy@1 221
andy@1 222
andy@12 223 class Decay(object):
andy@8 224 """
andy@8 225 Object representing a decay entry on a particle decribed by the SLHA file.
andy@8 226 'Decay' objects are not a direct representation of a DECAY block in an SLHA
andy@8 227 file... that role, somewhat confusingly, is taken by the Particle class.
andy@8 228
andy@8 229 Decay objects have three properties: a branching ratio, br, an nda number
andy@12 230 (number of daughters == len(ids)), and a tuple of PDG PIDs to which the
andy@12 231 decay occurs. The PDG ID of the particle whose decay this represents may
andy@12 232 also be stored, but this is normally known via the Particle in which the
andy@12 233 decay is stored.
andy@8 234 """
andy@8 235 def __init__(self, br, nda, ids, parentid=None):
andy@8 236 self.parentid = parentid
andy@6 237 self.br = br
andy@6 238 self.nda = nda
andy@6 239 self.ids = ids
andy@29 240 assert(self.nda == len(self.ids))
andy@6 241
andy@6 242 def __cmp__(self, other):
andy@31 243 return cmp(other.br, self.br)
andy@6 244
andy@6 245 def __str__(self):
andy@147 246 return "%.8e %s" % (self.br, self.ids)
andy@6 247
andy@123 248 def __repr__(self):
andy@123 249 return self.__str__()
andy@123 250
andy@6 251
andy@12 252 class Particle(object):
andy@8 253 """
andy@8 254 Representation of a single, specific particle, decay block from an SLHA
andy@8 255 file. These objects are not themselves called 'Decay', since that concept
andy@8 256 applies more naturally to the various decays found inside this
andy@8 257 object. Particle classes store the PDG ID (pid) of the particle being
andy@8 258 represented, and optionally the mass (mass) and total decay width
andy@8 259 (totalwidth) of that particle in the SLHA scenario. Masses may also be found
andy@8 260 via the MASS block, from which the Particle.mass property is filled, if at
andy@8 261 all. They also store a list of Decay objects (decays) which are probably the
andy@8 262 item of most interest.
andy@8 263 """
andy@6 264 def __init__(self, pid, totalwidth=None, mass=None):
andy@6 265 self.pid = pid
andy@6 266 self.totalwidth = totalwidth
andy@6 267 self.mass = mass
andy@6 268 self.decays = []
andy@6 269
andy@6 270 def add_decay(self, br, nda, ids):
andy@6 271 self.decays.append(Decay(br, nda, ids))
andy@6 272 self.decays.sort()
andy@6 273
andy@6 274 def __cmp__(self, other):
andy@6 275 if abs(self.pid) == abs(other.pid):
andy@31 276 return cmp(self.pid, other.pid)
andy@31 277 return cmp(abs(self.pid), abs(other.pid))
andy@6 278
andy@6 279 def __str__(self):
andy@6 280 s = str(self.pid)
andy@7 281 if self.mass is not None:
andy@147 282 s += " : mass = %.8e GeV" % self.mass
andy@6 283 if self.totalwidth is not None:
andy@147 284 s += " : total width = %.8e GeV" % self.totalwidth
andy@6 285 for d in self.decays:
andy@12 286 if d.br > 0.0:
andy@12 287 s += "\n %s" % d
andy@6 288 return s
andy@1 289
andy@123 290 def __repr__(self):
andy@123 291 return self.__str__()
andy@123 292
andy@123 293
andy@1 294
andy@21 295
andy@21 296
andy@31 297 def readSLHA(spcstr, ignorenobr=False):
andy@21 298 """
andy@31 299 Read an SLHA definition from a string, returning dictionaries of blocks and
andy@31 300 decays.
andy@31 301
andy@31 302 If the ignorenobr parameter is True, do not store decay entries with a
andy@31 303 branching ratio of zero.
andy@21 304 """
andy@189 305 blocks = _mkdict()
andy@189 306 decays = _mkdict()
andy@21 307 #
andy@34 308 import re
andy@21 309 currentblock = None
andy@21 310 currentdecay = None
andy@21 311 for line in spcstr.splitlines():
andy@21 312 ## Handle (ignore) comment lines
andy@192 313 # TODO: Store block/entry comments
andy@21 314 if line.startswith("#"):
andy@21 315 continue
andy@21 316 if "#" in line:
andy@21 317 line = line[:line.index("#")]
andy@21 318
andy@21 319 ## Handle BLOCK/DECAY start lines
andy@21 320 if line.upper().startswith("BLOCK"):
andy@47 321 #print line
andy@141 322 match = re.match(r"BLOCK\s+(\w+)(\s+Q\s*=\s*.+)?", line.upper())
andy@21 323 if not match:
andy@8 324 continue
andy@21 325 blockname = match.group(1)
andy@21 326 qstr = match.group(2)
andy@21 327 if qstr is not None:
andy@141 328 qstr = qstr[qstr.find("=")+1:].strip()
andy@21 329 currentblock = blockname
andy@21 330 currentdecay = None
andy@21 331 blocks[blockname] = Block(blockname, q=qstr)
andy@21 332 elif line.upper().startswith("DECAY"):
andy@185 333 match = re.match(r"DECAY\s+(-?\d+)\s+([\d\.E+-]+|NAN).*", line.upper())
andy@21 334 if not match:
andy@21 335 continue
andy@21 336 pdgid = int(match.group(1))
andy@185 337 width = float(match.group(2)) if match.group(2) != "NAN" else None
andy@21 338 currentblock = "DECAY"
andy@21 339 currentdecay = pdgid
andy@21 340 decays[pdgid] = Particle(pdgid, width)
andy@21 341 else:
andy@21 342 ## In-block line
andy@21 343 if currentblock is not None:
andy@21 344 items = line.split()
andy@21 345 if len(items) < 1:
andy@6 346 continue
andy@21 347 if currentblock != "DECAY":
andy@200 348 blocks[currentblock].add_entry(items)
andy@21 349 else:
andy@185 350 br = float(items[0]) if items[0].upper() != "NAN" else None
andy@21 351 nda = int(items[1])
andy@21 352 ids = map(int, items[2:])
andy@185 353 if br > 0.0 or not ignorenobr: # br == None is < 0
andy@31 354 decays[currentdecay].add_decay(br, nda, ids)
andy@1 355
andy@8 356 ## Try to populate Particle masses from the MASS block
andy@47 357 # print blocks.keys()
andy@47 358 try:
andy@201 359 for pid in blocks["MASS"].keys():
andy@47 360 if decays.has_key(pid):
andy@201 361 decays[pid].mass = blocks["MASS"][pid]
andy@47 362 except:
andy@166 363 raise ParseError("No MASS block found: cannot populate particle masses")
andy@8 364
andy@1 365 return blocks, decays
andy@1 366
andy@1 367
andy@124 368
andy@124 369
andy@124 370 # TODO: Split writeSLHA into writeSLHA{Blocks,Decays}
andy@124 371
andy@124 372
andy@147 373 def writeSLHA(blocks, decays, ignorenobr=False, precision=8):
andy@124 374 """
andy@124 375 Return an SLHA definition as a string, from the supplied blocks and decays dicts.
andy@124 376 """
andy@192 377 # TODO: Pay attention to space-padding and minus signs for column alignment
andy@147 378 fmte = "%." + str(precision) + "e"
andy@124 379 sep = " "
andy@192 380 blockstrs = []
andy@124 381 ## Blocks
andy@189 382 for bname, b in blocks.iteritems():
andy@124 383 namestr = b.name
andy@124 384 if b.q is not None:
andy@147 385 namestr += (" Q= " + fmte) % float(b.q)
andy@192 386 blockstr = "BLOCK %s\n" % namestr
andy@192 387 entrystrs = []
andy@201 388 for k, v in b.items():
andy@192 389 entrystr = ""
andy@192 390 if type(k) == tuple:
andy@194 391 entrystr += sep.join(_autostr(i) for i in k)
andy@194 392 elif k is not None:
andy@194 393 entrystr += _autostr(k)
andy@192 394 entrystr += sep + _autostr(v) # TODO: apply precision formatting for floats
andy@192 395 entrystrs.append(entrystr)
andy@192 396 blockstr += "\n".join(entrystrs)
andy@192 397 blockstrs.append(blockstr)
andy@192 398 ##
andy@124 399 ## Decays
andy@189 400 for pid, particle in decays.iteritems():
andy@192 401 blockstr = ("DECAY %d " + fmte + "\n") % (particle.pid, particle.totalwidth or -1)
andy@192 402 decaystrs = []
andy@189 403 for d in particle.decays:
andy@124 404 if d.br > 0.0 or not ignorenobr:
andy@192 405 products_str = sep.join(map(str, d.ids))
andy@192 406 decaystr = sep + (fmte % d.br) + sep + ("%d" % len(d.ids)) + sep + products_str
andy@192 407 decaystrs.append(decaystr)
andy@192 408 blockstr += "\n".join(decaystrs)
andy@192 409 blockstrs.append(blockstr)
andy@192 410 ## Total result
andy@192 411 return "\n\n".join(blockstrs)
andy@124 412
andy@124 413
andy@124 414
andy@124 415 ###############################################################################
andy@161 416 ## PDG <-> HERWIG particle ID code translations for ISAWIG handling
andy@124 417
andy@124 418 ## Static array of HERWIG IDHW codes mapped to PDG MC ID codes, based on
andy@124 419 ## http://www.hep.phy.cam.ac.uk/~richardn/HERWIG/ISAWIG/susycodes.html
andy@124 420 ## + the IDPDG array and section 4.13 of the HERWIG manual.
andy@124 421 _HERWIGID2PDGID = {}
andy@124 422 _HERWIGID2PDGID[7] = -1
andy@124 423 _HERWIGID2PDGID[8] = -2
andy@124 424 _HERWIGID2PDGID[9] = -3
andy@124 425 _HERWIGID2PDGID[10] = -4
andy@124 426 _HERWIGID2PDGID[11] = -5
andy@124 427 _HERWIGID2PDGID[12] = -6
andy@124 428 _HERWIGID2PDGID[13] = 21
andy@124 429 _HERWIGID2PDGID[59] = 22
andy@124 430 _HERWIGID2PDGID[121] = 11
andy@124 431 _HERWIGID2PDGID[122] = 12
andy@124 432 _HERWIGID2PDGID[123] = 13
andy@124 433 _HERWIGID2PDGID[124] = 14
andy@124 434 _HERWIGID2PDGID[125] = 15
andy@124 435 _HERWIGID2PDGID[126] = 16
andy@124 436 _HERWIGID2PDGID[127] = -11
andy@124 437 _HERWIGID2PDGID[128] = -12
andy@124 438 _HERWIGID2PDGID[129] = -13
andy@124 439 _HERWIGID2PDGID[130] = -14
andy@124 440 _HERWIGID2PDGID[131] = -15
andy@124 441 _HERWIGID2PDGID[132] = -16
andy@139 442 _HERWIGID2PDGID[198] = 24 # W+
andy@139 443 _HERWIGID2PDGID[199] = -24 # W-
andy@139 444 _HERWIGID2PDGID[200] = 23 # Z0
andy@139 445 _HERWIGID2PDGID[201] = 25 ## SM HIGGS
andy@124 446 _HERWIGID2PDGID[203] = 25 ## HIGGSL0 (== PDG standard in this direction)
andy@124 447 _HERWIGID2PDGID[204] = 35 ## HIGGSH0
andy@124 448 _HERWIGID2PDGID[205] = 36 ## HIGGSA0
andy@124 449 _HERWIGID2PDGID[206] = 37 ## HIGGS+
andy@124 450 _HERWIGID2PDGID[207] = -37 ## HIGGS-
andy@124 451 _HERWIGID2PDGID[401] = 1000001 ## SSDLBR
andy@124 452 _HERWIGID2PDGID[407] = -1000001 ## SSDLBR
andy@124 453 _HERWIGID2PDGID[402] = 1000002 ## SSULBR
andy@124 454 _HERWIGID2PDGID[408] = -1000002 ## SSUL
andy@124 455 _HERWIGID2PDGID[403] = 1000003 ## SSSLBR
andy@124 456 _HERWIGID2PDGID[409] = -1000003 ## SSSL
andy@124 457 _HERWIGID2PDGID[404] = 1000004 ## SSCLBR
andy@124 458 _HERWIGID2PDGID[410] = -1000004 ## SSCL
andy@124 459 _HERWIGID2PDGID[405] = 1000005 ## SSB1BR
andy@124 460 _HERWIGID2PDGID[411] = -1000005 ## SSB1
andy@124 461 _HERWIGID2PDGID[406] = 1000006 ## SST1BR
andy@124 462 _HERWIGID2PDGID[412] = -1000006 ## SST1
andy@124 463 _HERWIGID2PDGID[413] = 2000001 ## SSDR
andy@124 464 _HERWIGID2PDGID[419] = -2000001 ## SSDRBR
andy@124 465 _HERWIGID2PDGID[414] = 2000002 ## SSUR
andy@124 466 _HERWIGID2PDGID[420] = -2000002 ## SSURBR
andy@124 467 _HERWIGID2PDGID[415] = 2000003 ## SSSR
andy@124 468 _HERWIGID2PDGID[421] = -2000003 ## SSSRBR
andy@124 469 _HERWIGID2PDGID[416] = 2000004 ## SSCR
andy@124 470 _HERWIGID2PDGID[422] = -2000004 ## SSCRBR
andy@124 471 _HERWIGID2PDGID[417] = 2000005 ## SSB2
andy@124 472 _HERWIGID2PDGID[423] = -2000005 ## SSB2BR
andy@124 473 _HERWIGID2PDGID[418] = 2000006 ## SST2
andy@124 474 _HERWIGID2PDGID[424] = -2000006 ## SST2BR
andy@124 475 _HERWIGID2PDGID[425] = 1000011 ## SSEL-
andy@124 476 _HERWIGID2PDGID[431] = -1000011 ## SSEL+
andy@124 477 _HERWIGID2PDGID[426] = 1000012 ## SSNUEL
andy@124 478 _HERWIGID2PDGID[432] = -1000012 ## SSNUELBR
andy@124 479 _HERWIGID2PDGID[427] = 1000013 ## SSMUL-
andy@124 480 _HERWIGID2PDGID[433] = -1000013 ## SSMUL+
andy@124 481 _HERWIGID2PDGID[428] = 1000014 ## SSNUMUL
andy@124 482 _HERWIGID2PDGID[434] = -1000014 ## SSNUMLBR
andy@124 483 _HERWIGID2PDGID[429] = 1000015 ## SSTAU1-
andy@124 484 _HERWIGID2PDGID[435] = -1000015 ## SSTAU1+
andy@124 485 _HERWIGID2PDGID[430] = 1000016 ## SSNUTL
andy@124 486 _HERWIGID2PDGID[436] = -1000016 ## SSNUTLBR
andy@124 487 _HERWIGID2PDGID[437] = 2000011 ## SSEL-
andy@124 488 _HERWIGID2PDGID[443] = -2000011 ## SSEL+
andy@124 489 _HERWIGID2PDGID[438] = 2000012 ## SSNUEL
andy@124 490 _HERWIGID2PDGID[444] = -2000012 ## SSNUELBR
andy@124 491 _HERWIGID2PDGID[439] = 2000013 ## SSMUL-
andy@124 492 _HERWIGID2PDGID[445] = -2000013 ## SSMUL+
andy@124 493 _HERWIGID2PDGID[440] = 2000014 ## SSNUMUL
andy@124 494 _HERWIGID2PDGID[446] = -2000014 ## SSNUMLBR
andy@124 495 _HERWIGID2PDGID[441] = 2000015 ## SSTAU1-
andy@124 496 _HERWIGID2PDGID[447] = -2000015 ## SSTAU1+
andy@124 497 _HERWIGID2PDGID[442] = 2000016 ## SSNUTL
andy@124 498 _HERWIGID2PDGID[448] = -2000016 ## SSNUTLBR
andy@124 499 _HERWIGID2PDGID[449] = 1000021 ## GLUINO
andy@124 500 _HERWIGID2PDGID[450] = 1000022 ## NTLINO1
andy@124 501 _HERWIGID2PDGID[451] = 1000023 ## NTLINO2
andy@124 502 _HERWIGID2PDGID[452] = 1000025 ## NTLINO3
andy@124 503 _HERWIGID2PDGID[453] = 1000035 ## NTLINO4
andy@124 504 _HERWIGID2PDGID[454] = 1000024 ## CHGINO1+
andy@124 505 _HERWIGID2PDGID[456] = -1000024 ## CHGINO1-
andy@124 506 _HERWIGID2PDGID[455] = 1000037 ## CHGINO2+
andy@124 507 _HERWIGID2PDGID[457] = -1000037 ## CHGINO2-
andy@124 508 _HERWIGID2PDGID[458] = 1000039 ## GRAVTINO
andy@124 509
andy@124 510 def herwigid2pdgid(hwid):
andy@124 511 """
andy@124 512 Convert a particle ID code in the HERWIG internal IDHW format (as used by
andy@124 513 ISAWIG) into its equivalent in the standard PDG ID code definition.
andy@124 514 """
andy@124 515 return _HERWIGID2PDGID.get(hwid, hwid)
andy@124 516
andy@120 517
andy@124 518 ## PDG MC ID codes mapped to HERWIG IDHW codes, based on
andy@124 519 ## http://www.hep.phy.cam.ac.uk/~richardn/HERWIG/ISAWIG/susycodes.html
andy@124 520 ## + the IDPDG array and section 4.13 of the HERWIG manual.
andy@124 521 _PDGID2HERWIGID = {}
andy@124 522 _PDGID2HERWIGID[ -1] = 7
andy@124 523 _PDGID2HERWIGID[ -2] = 8
andy@124 524 _PDGID2HERWIGID[ -3] = 9
andy@124 525 _PDGID2HERWIGID[ -4] = 10
andy@124 526 _PDGID2HERWIGID[ -5] = 11
andy@124 527 _PDGID2HERWIGID[ -6] = 12
andy@124 528 _PDGID2HERWIGID[ 21] = 13
andy@124 529 _PDGID2HERWIGID[ 22] = 59
andy@124 530 _PDGID2HERWIGID[ 11] = 121
andy@124 531 _PDGID2HERWIGID[ 12] = 122
andy@124 532 _PDGID2HERWIGID[ 13] = 123
andy@124 533 _PDGID2HERWIGID[ 14] = 124
andy@124 534 _PDGID2HERWIGID[ 15] = 125
andy@124 535 _PDGID2HERWIGID[ 16] = 126
andy@124 536 _PDGID2HERWIGID[ -11] = 127
andy@124 537 _PDGID2HERWIGID[ -12] = 128
andy@124 538 _PDGID2HERWIGID[ -13] = 129
andy@124 539 _PDGID2HERWIGID[ -14] = 130
andy@124 540 _PDGID2HERWIGID[ -15] = 131
andy@124 541 _PDGID2HERWIGID[ -16] = 132
andy@139 542 _PDGID2HERWIGID[ 24] = 198 ## W+
andy@139 543 _PDGID2HERWIGID[ -24] = 199 ## W-
andy@139 544 _PDGID2HERWIGID[ 23] = 200 ## Z
andy@139 545 _PDGID2HERWIGID[ 25] = 203 ## HIGGSL0 (added for PDG standard -> HERWIG IDHW) # TODO: should be 201?
andy@124 546 _PDGID2HERWIGID[ 26] = 203 ## HIGGSL0
andy@124 547 _PDGID2HERWIGID[ 35] = 204 ## HIGGSH0
andy@124 548 _PDGID2HERWIGID[ 36] = 205 ## HIGGSA0
andy@124 549 _PDGID2HERWIGID[ 37] = 206 ## HIGGS+
andy@124 550 _PDGID2HERWIGID[ -37] = 207 ## HIGGS-
andy@124 551 _PDGID2HERWIGID[ 1000001] = 401 ## SSDLBR
andy@124 552 _PDGID2HERWIGID[-1000001] = 407 ## SSDLBR
andy@124 553 _PDGID2HERWIGID[ 1000002] = 402 ## SSULBR
andy@124 554 _PDGID2HERWIGID[-1000002] = 408 ## SSUL
andy@124 555 _PDGID2HERWIGID[ 1000003] = 403 ## SSSLBR
andy@124 556 _PDGID2HERWIGID[-1000003] = 409 ## SSSL
andy@124 557 _PDGID2HERWIGID[ 1000004] = 404 ## SSCLBR
andy@124 558 _PDGID2HERWIGID[-1000004] = 410 ## SSCL
andy@124 559 _PDGID2HERWIGID[ 1000005] = 405 ## SSB1BR
andy@124 560 _PDGID2HERWIGID[-1000005] = 411 ## SSB1
andy@124 561 _PDGID2HERWIGID[ 1000006] = 406 ## SST1BR
andy@124 562 _PDGID2HERWIGID[-1000006] = 412 ## SST1
andy@124 563 _PDGID2HERWIGID[ 2000001] = 413 ## SSDR
andy@124 564 _PDGID2HERWIGID[-2000001] = 419 ## SSDRBR
andy@124 565 _PDGID2HERWIGID[ 2000002] = 414 ## SSUR
andy@124 566 _PDGID2HERWIGID[-2000002] = 420 ## SSURBR
andy@124 567 _PDGID2HERWIGID[ 2000003] = 415 ## SSSR
andy@124 568 _PDGID2HERWIGID[-2000003] = 421 ## SSSRBR
andy@124 569 _PDGID2HERWIGID[ 2000004] = 416 ## SSCR
andy@124 570 _PDGID2HERWIGID[-2000004] = 422 ## SSCRBR
andy@124 571 _PDGID2HERWIGID[ 2000005] = 417 ## SSB2
andy@124 572 _PDGID2HERWIGID[-2000005] = 423 ## SSB2BR
andy@124 573 _PDGID2HERWIGID[ 2000006] = 418 ## SST2
andy@124 574 _PDGID2HERWIGID[-2000006] = 424 ## SST2BR
andy@124 575 _PDGID2HERWIGID[ 1000011] = 425 ## SSEL-
andy@124 576 _PDGID2HERWIGID[-1000011] = 431 ## SSEL+
andy@124 577 _PDGID2HERWIGID[ 1000012] = 426 ## SSNUEL
andy@124 578 _PDGID2HERWIGID[-1000012] = 432 ## SSNUELBR
andy@124 579 _PDGID2HERWIGID[ 1000013] = 427 ## SSMUL-
andy@124 580 _PDGID2HERWIGID[-1000013] = 433 ## SSMUL+
andy@124 581 _PDGID2HERWIGID[ 1000014] = 428 ## SSNUMUL
andy@124 582 _PDGID2HERWIGID[-1000014] = 434 ## SSNUMLBR
andy@124 583 _PDGID2HERWIGID[ 1000015] = 429 ## SSTAU1-
andy@124 584 _PDGID2HERWIGID[-1000015] = 435 ## SSTAU1+
andy@124 585 _PDGID2HERWIGID[ 1000016] = 430 ## SSNUTL
andy@124 586 _PDGID2HERWIGID[-1000016] = 436 ## SSNUTLBR
andy@124 587 _PDGID2HERWIGID[ 2000011] = 437 ## SSEL-
andy@124 588 _PDGID2HERWIGID[-2000011] = 443 ## SSEL+
andy@124 589 _PDGID2HERWIGID[ 2000012] = 438 ## SSNUEL
andy@124 590 _PDGID2HERWIGID[-2000012] = 444 ## SSNUELBR
andy@124 591 _PDGID2HERWIGID[ 2000013] = 439 ## SSMUL-
andy@124 592 _PDGID2HERWIGID[-2000013] = 445 ## SSMUL+
andy@124 593 _PDGID2HERWIGID[ 2000014] = 440 ## SSNUMUL
andy@124 594 _PDGID2HERWIGID[-2000014] = 446 ## SSNUMLBR
andy@124 595 _PDGID2HERWIGID[ 2000015] = 441 ## SSTAU1-
andy@124 596 _PDGID2HERWIGID[-2000015] = 447 ## SSTAU1+
andy@124 597 _PDGID2HERWIGID[ 2000016] = 442 ## SSNUTL
andy@124 598 _PDGID2HERWIGID[-2000016] = 448 ## SSNUTLBR
andy@124 599 _PDGID2HERWIGID[ 1000021] = 449 ## GLUINO
andy@124 600 _PDGID2HERWIGID[ 1000022] = 450 ## NTLINO1
andy@124 601 _PDGID2HERWIGID[ 1000023] = 451 ## NTLINO2
andy@124 602 _PDGID2HERWIGID[ 1000025] = 452 ## NTLINO3
andy@124 603 _PDGID2HERWIGID[ 1000035] = 453 ## NTLINO4
andy@124 604 _PDGID2HERWIGID[ 1000024] = 454 ## CHGINO1+
andy@124 605 _PDGID2HERWIGID[-1000024] = 456 ## CHGINO1-
andy@124 606 _PDGID2HERWIGID[ 1000037] = 455 ## CHGINO2+
andy@124 607 _PDGID2HERWIGID[-1000037] = 457 ## CHGINO2-
andy@124 608 _PDGID2HERWIGID[ 1000039] = 458 ## GRAVTINO
andy@124 609
andy@124 610 def pdgid2herwigid(pdgid):
andy@29 611 """
andy@124 612 Convert a particle ID code in the standard PDG ID code definition into
andy@124 613 its equivalent in the HERWIG internal IDHW format (as used by ISAWIG).
andy@124 614 """
andy@124 615 return _PDGID2HERWIGID.get(pdgid, pdgid)
andy@31 616
andy@29 617
andy@161 618 ###############################################################################
andy@161 619 ## ISAWIG format reading/writing
andy@29 620
andy@29 621
andy@161 622 def readISAWIG(isastr, ignorenobr=False):
andy@48 623 """
andy@161 624 Read a spectrum definition from a string in the ISAWIG format, returning
andy@161 625 dictionaries of blocks and decays. While this is not an SLHA format, it is
andy@161 626 informally supported as a useful mechanism for converting ISAWIG spectra to
andy@161 627 SLHA.
andy@48 628
andy@161 629 ISAWIG parsing based on the HERWIG SUSY specification format, from
andy@161 630 http://www.hep.phy.cam.ac.uk/~richardn/HERWIG/ISAWIG/file.html
andy@48 631
andy@161 632 If the ignorenobr parameter is True, do not store decay entries with a
andy@161 633 branching ratio of zero.
andy@48 634 """
andy@161 635
andy@189 636 blocks = _mkdict()
andy@189 637 decays = _mkdict()
andy@161 638 LINES = isastr.splitlines()
andy@161 639
andy@161 640 def getnextvalidline():
andy@161 641 while LINES:
andy@161 642 s = LINES.pop(0).strip()
andy@161 643 ## Return None if EOF reached
andy@161 644 if len(s) == 0:
andy@161 645 continue
andy@161 646 ## Strip comments
andy@161 647 if "#" in s:
andy@161 648 s = s[:s.index("#")].strip()
andy@161 649 ## Return if non-empty
andy@161 650 if len(s) > 0:
andy@161 651 return s
andy@161 652
andy@161 653 def getnextvalidlineitems():
andy@161 654 return map(_autotype, getnextvalidline().split())
andy@161 655
andy@161 656 ## Populate MASS block and create decaying particle objects
andy@161 657 masses = Block("MASS")
andy@161 658 numentries = int(getnextvalidline())
andy@161 659 for i in xrange(numentries):
andy@161 660 hwid, mass, lifetime = getnextvalidlineitems()
andy@161 661 width = 1.0/(lifetime * 1.51926778e24) ## width in GeV == hbar/lifetime in seconds
andy@161 662 pdgid = herwigid2pdgid(hwid)
andy@202 663 masses[pdgid] = mass
andy@161 664 decays[pdgid] = Particle(pdgid, width, mass)
andy@161 665 #print pdgid, mass, width
andy@161 666 blocks["MASS"] = masses
andy@161 667
andy@161 668 ## Populate decays
andy@161 669 for n in xrange(numentries):
andy@161 670 numdecays = int(getnextvalidline())
andy@161 671 for d in xrange(numdecays):
andy@161 672 #print n, numentries-1, d, numdecays-1
andy@161 673 decayitems = getnextvalidlineitems()
andy@161 674 hwid = decayitems[0]
andy@161 675 pdgid = herwigid2pdgid(hwid)
andy@161 676 br = decayitems[1]
andy@161 677 nme = decayitems[2]
andy@161 678 daughter_hwids = decayitems[3:]
andy@161 679 daughter_pdgids = []
andy@161 680 for hw in daughter_hwids:
andy@161 681 if hw != 0:
andy@161 682 daughter_pdgids.append(herwigid2pdgid(hw))
andy@161 683 if not decays.has_key(pdgid):
andy@161 684 #print "Decay for unlisted particle %d, %d" % (hwid, pdgid)
andy@161 685 decays[pdgid] = Particle(pdgid)
andy@161 686 decays[pdgid].add_decay(br, len(daughter_pdgids), daughter_pdgids)
andy@161 687
andy@161 688
andy@161 689 ## Now the SUSY parameters
andy@161 690 TANB, ALPHAH = getnextvalidlineitems()
andy@161 691 blocks["MINPAR"] = Block("MINPAR")
andy@202 692 blocks["MINPAR"][3] = TANB
andy@161 693 blocks["ALPHA"] = Block("ALPHA")
andy@202 694 blocks["ALPHA"][None] = ALPHAH
andy@161 695 #
andy@161 696 ## Neutralino mixing matrix
andy@161 697 blocks["NMIX"] = Block("NMIX")
andy@161 698 for i in xrange(1, 5):
andy@161 699 nmix_i = getnextvalidlineitems()
andy@161 700 for j, v in enumerate(nmix_i):
andy@202 701 blocks["NMIX"][i, j+1] = v
andy@161 702 #
andy@161 703 ## Chargino mixing matrices V and U
andy@161 704 blocks["VMIX"] = Block("VMIX")
andy@161 705 vmix = getnextvalidlineitems()
andy@202 706 blocks["VMIX"][1, 1] = vmix[0]
andy@202 707 blocks["VMIX"][1, 2] = vmix[1]
andy@202 708 blocks["VMIX"][2, 1] = vmix[2]
andy@202 709 blocks["VMIX"][2, 2] = vmix[3]
andy@161 710 blocks["UMIX"] = Block("UMIX")
andy@161 711 umix = getnextvalidlineitems()
andy@202 712 blocks["UMIX"][1, 1] = umix[0]
andy@202 713 blocks["UMIX"][1, 2] = umix[1]
andy@202 714 blocks["UMIX"][2, 1] = umix[2]
andy@202 715 blocks["UMIX"][2, 2] = umix[3]
andy@161 716 #
andy@161 717 THETAT, THETAB, THETAL = getnextvalidlineitems()
andy@161 718 import math
andy@161 719 blocks["STOPMIX"] = Block("STOPMIX")
andy@202 720 blocks["STOPMIX"][1, 1] = math.cos(THETAT)
andy@202 721 blocks["STOPMIX"][1, 2] = -math.sin(THETAT)
andy@202 722 blocks["STOPMIX"][2, 1] = math.sin(THETAT)
andy@202 723 blocks["STOPMIX"][2, 2] = math.cos(THETAT)
andy@161 724 blocks["SBOTMIX"] = Block("SBOTMIX")
andy@202 725 blocks["SBOTMIX"][1, 1] = math.cos(THETAB)
andy@202 726 blocks["SBOTMIX"][1, 2] = -math.sin(THETAB)
andy@202 727 blocks["SBOTMIX"][2, 1] = math.sin(THETAB)
andy@202 728 blocks["SBOTMIX"][2, 2] = math.cos(THETAB)
andy@161 729 blocks["STAUMIX"] = Block("STAUMIX")
andy@202 730 blocks["STAUMIX"][1, 1] = math.cos(THETAL)
andy@202 731 blocks["STAUMIX"][1, 2] = -math.sin(THETAL)
andy@202 732 blocks["STAUMIX"][2, 1] = math.sin(THETAL)
andy@202 733 blocks["STAUMIX"][2, 2] = math.cos(THETAL)
andy@161 734 #
andy@161 735 ATSS, ABSS, ALSS = getnextvalidlineitems()
andy@161 736 blocks["AU"] = Block("AU")
andy@205 737 blocks["AU"][3, 3] = ATSS
andy@161 738 blocks["AD"] = Block("AD")
andy@205 739 blocks["AD"][3, 3] = ABSS
andy@161 740 blocks["AE"] = Block("AE")
andy@205 741 blocks["AE"][3, 3] = ALSS
andy@161 742 #
andy@161 743 MUSS = getnextvalidlineitems()[0]
andy@202 744 blocks["MINPAR"][4] = MUSS
andy@161 745 #
andy@161 746
andy@161 747 # TODO: Parse RPV boolean and couplings into SLHA2 blocks
andy@161 748
andy@161 749 return blocks, decays
andy@161 750
andy@161 751
andy@161 752 def writeISAJET(blocks, decays, outname, ignorenobr=False, precision=8):
andy@161 753 """
andy@161 754 Return a SUSY spectrum definition in the format required for input by ISAJET,
andy@161 755 as a string, from the supplied blocks and decays dicts.
andy@161 756
andy@161 757 The outname parameter specifies the desired output filename from ISAJET: this
andy@161 758 will appear in the first line of the return value.
andy@161 759
andy@161 760 If the ignorenobr parameter is True, do not write decay entries with a
andy@161 761 branching ratio of zero.
andy@161 762 """
andy@161 763 fmte = "%." + str(precision) + "e"
andy@161 764
andy@201 765 masses = blocks["MASS"]
andy@161 766
andy@161 767 ## Init output string
andy@161 768 out = ""
andy@161 769
andy@161 770 ## First line is the output name
andy@161 771 out += "'%s'" % outname + "\n"
andy@161 772
andy@161 773 ## Next the top mass
andy@161 774 out += fmte % masses[6] + "\n"
andy@161 775
andy@161 776 ## Next the top mass
andy@161 777 out += fmte % masses[6] + "\n"
andy@161 778
andy@161 779 ## mSUGRA parameters (one line)
andy@161 780 # e.g. 1273.78,713.286,804.721,4.82337
andy@161 781
andy@161 782 ## Masses and trilinear couplings (3 lines)
andy@161 783 # e.g. 1163.14,1114.15,1118.99,374.664,209.593
andy@161 784 # e.g. 1069.54,1112.7,919.908,374.556,209.381,-972.817,-326.745,-406.494
andy@161 785 # e.g. 1163.14,1114.15,1118.99,374.712,210.328
andy@161 786
andy@161 787 ## RPV couplings (?? lines)
andy@161 788 # e.g. 232.615,445.477
andy@161 789
andy@161 790 ## Etc ???!!!
andy@161 791 # e.g. /
andy@161 792 # e.g. n
andy@161 793 # e.g. y
andy@161 794 # e.g. y
andy@161 795 # e.g. 0.047441 3.80202e-23 0 0 0 2.17356e-22 0 0 5.23773e-09
andy@161 796 # e.g. y
andy@161 797 # e.g. 3.35297e-25 0 0 0 7.34125e-24 0 0 0 3.17951e-22 8.07984e-12 0 0 0 1.76906e-10 0 0 0 7.66184e-09 0 0 0 0 0 0 0 0 0
andy@161 798 # e.g. n
andy@161 799 # e.g. 'susy_RPV_stau_BC1scan_m560_tanb05.txt'
andy@161 800
andy@161 801 return out
andy@48 802
andy@48 803
andy@147 804 def writeISAWIG(blocks, decays, ignorenobr=False, precision=8):
andy@48 805 """
andy@161 806 Return a SUSY spectrum definition in the format produced by ISAWIG for inut to HERWIG
andy@161 807 as a string, from the supplied SLHA blocks and decays dicts.
andy@48 808
andy@48 809 ISAWIG parsing based on the HERWIG SUSY specification format, from
andy@48 810 http://www.hep.phy.cam.ac.uk/~richardn/HERWIG/ISAWIG/file.html
andy@48 811
andy@48 812 If the ignorenobr parameter is True, do not write decay entries with a
andy@48 813 branching ratio of zero.
andy@48 814 """
andy@147 815 fmte = "%." + str(precision) + "e"
andy@48 816
andy@201 817 masses = blocks["MASS"]
andy@48 818
andy@48 819 ## Init output string
andy@48 820 out = ""
andy@48 821
andy@48 822 ## First write out masses section:
andy@48 823 ## Number of SUSY + top particles
andy@48 824 ## IDHW, RMASS(IDHW), RLTIM(IDHW)
andy@48 825 ## repeated for each particle
andy@48 826 ## IDHW is the HERWIG identity code.
andy@48 827 ## RMASS and RTLIM are the mass in GeV, and lifetime in seconds respectively.
andy@48 828 massout = ""
andy@48 829 for pid in masses.keys():
andy@48 830 lifetime = -1
andy@48 831 try:
andy@48 832 width = decays[pid].totalwidth
andy@48 833 if width and width > 0:
andy@48 834 lifetime = 1.0/(width * 1.51926778e24) ## lifetime in seconds == hbar/width in GeV
andy@48 835 except:
andy@48 836 pass
andy@147 837 massout += ("%d " + fmte + " " + fmte + "\n") % (pdgid2herwigid(pid), masses[pid], lifetime)
andy@48 838 out += "%d\n" % massout.count("\n")
andy@48 839 out += massout
andy@48 840
andy@48 841 assert(len(masses) == len(decays))
andy@48 842
andy@48 843 ## Next each particles decay modes together with their branching ratios and matrix element codes
andy@48 844 ## Number of decay modes for a given particle (IDK)
andy@48 845 ## IDK(*), BRFRAC(*), NME(*) & IDKPRD(1-5,*)
andy@48 846 ## repeated for each mode.
andy@48 847 ## Repeated for each particle.
andy@48 848 ## IDK is the HERWIG code for the decaying particle, BRFRAC is the branching ratio of
andy@48 849 ## the decay mode. NME is a code for the matrix element to be used, either from the
andy@48 850 ## SUSY elements or the main HERWIG MEs. IDKPRD are the HERWIG identity codes of the decay products.
andy@48 851 for i, pid in enumerate(decays.keys()):
andy@48 852 # if not decays.has_key(pid):
andy@48 853 # continue
andy@124 854 hwid = pdgid2herwigid(pid)
andy@48 855 decayout = ""
andy@48 856 #decayout += "@@@@ %d %d %d\n" % (i, pid, hwid)
andy@48 857 for i_d, d in enumerate(decays[pid].decays):
andy@48 858 ## Skip decay if it has no branching ratio
andy@48 859 if ignorenobr and d.br == 0:
andy@48 860 continue
andy@71 861
andy@71 862 ## Identify decay matrix element to use
andy@59 863 ## From std HW docs, or from this pair:
andy@59 864 ## Two new matrix element codes have been added for these new decays:
andy@59 865 ## NME = 200 3 body top quark via charged Higgs
andy@59 866 ## 300 3 body R-parity violating gaugino and gluino decays
andy@71 867 nme = 0
andy@71 868 # TODO: Get correct condition for using ME 100... this guessed from some ISAWIG output
andy@71 869 if abs(pid) in (6, 12):
andy@71 870 nme = 100
andy@71 871 ## Extra SUSY MEs
andy@71 872 if len(d.ids) == 3:
andy@71 873 # TODO: How to determine the conditions for using 200 and 300 MEs? Enumeration of affected decays?
andy@71 874 pass
andy@199 875 decayout += ("%d " + fmte + " %d ") % (hwid, d.br, nme)
andy@71 876
andy@71 877 def is_quark(pid):
andy@71 878 return (abs(pid) in range(1, 7))
andy@71 879
andy@71 880 def is_lepton(pid):
andy@71 881 return (abs(pid) in range(11, 17))
andy@71 882
andy@71 883 def is_squark(pid):
andy@71 884 if abs(pid) in range(1000001, 1000007):
andy@71 885 return True
andy@71 886 if abs(pid) in range(2000001, 2000007):
andy@71 887 return True
andy@71 888 return False
andy@71 889
andy@71 890 def is_slepton(pid):
andy@71 891 if abs(pid) in range(1000011, 1000017):
andy@71 892 return True
andy@71 893 if abs(pid) in range(2000011, 2000016, 2):
andy@71 894 return True
andy@71 895 return False
andy@71 896
andy@71 897 def is_gaugino(pid):
andy@71 898 if abs(pid) in range(1000022, 1000026):
andy@71 899 return True
andy@71 900 if abs(pid) in (1000035, 1000037):
andy@71 901 return True
andy@71 902 return False
andy@71 903
andy@71 904 def is_susy(pid):
andy@71 905 return (is_squark(pid) or is_slepton(pid) or is_gaugino(pid) or pid == 1000021)
andy@71 906
andy@71 907 absids = map(abs, d.ids)
andy@71 908
andy@66 909 ## Order decay products as required by HERWIG
andy@66 910 ## Top
andy@66 911 if abs(pid) == 6:
andy@66 912 def cmp_bottomlast(a, b):
andy@66 913 """Comparison function which always puts b/bbar last"""
andy@66 914 if abs(a) == 5:
andy@66 915 return True
andy@71 916 if abs(b) == 5:
andy@66 917 return False
andy@66 918 return cmp(a, b)
andy@66 919 if len(absids) == 2:
andy@66 920 ## 2 body mode, to Higgs: Higgs; Bottom
andy@66 921 if (25 in absids or 26 in absids) and 5 in absids:
andy@199 922 d.ids = sorted(d.ids, cmp=cmp_bottomlast)
andy@66 923 elif len(absids) == 3:
andy@66 924 ## 3 body mode, via charged Higgs/W: quarks or leptons from W/Higgs; Bottom
andy@66 925 if 37 in absids or 23 in absids:
andy@199 926 d.ids = sorted(d.ids, cmp=cmp_bottomlast)
andy@66 927 ## Gluino
andy@66 928 elif abs(pid) == 1000021:
andy@66 929 if len(absids) == 2:
andy@66 930 ## 2 body mode
andy@66 931 ## without gluon: any order
andy@66 932 ## with gluon: gluon; colour neutral
andy@66 933 if 21 in absids:
andy@66 934 def cmp_gluonfirst(a, b):
andy@66 935 """Comparison function which always puts gluon first"""
andy@66 936 if a == 21:
andy@66 937 return False
andy@71 938 if b == 21:
andy@66 939 return True
andy@66 940 return cmp(a, b)
andy@199 941 d.ids = sorted(d.ids, cmp=cmp_gluonfirst)
andy@66 942 elif len(absids) == 3:
andy@66 943 ## 3-body modes, R-parity conserved: colour neutral; q or qbar
andy@70 944 def cmp_quarkslast(a, b):
andy@70 945 """Comparison function which always puts quarks last"""
andy@71 946 if is_quark(a):
andy@70 947 return True
andy@71 948 if is_quark(b):
andy@71 949 return False
andy@71 950 return cmp(a, b)
andy@199 951 d.ids = sorted(d.ids, cmp=cmp_quarkslast)
andy@71 952 ## Squark/Slepton
andy@71 953 elif is_squark(pid) or is_slepton(pid):
andy@73 954 def cmp_susy_quark_lepton(a, b):
andy@71 955 if is_susy(a):
andy@71 956 return False
andy@71 957 if is_susy(b):
andy@71 958 return True
andy@71 959 if is_quark(a):
andy@71 960 return False
andy@71 961 if is_quark(b):
andy@71 962 return True
andy@71 963 return cmp(a, b)
andy@71 964 ## 2 body modes: Gaugino/Gluino with Quark/Lepton Gaugino quark
andy@71 965 ## Gluino lepton
andy@71 966 ## 3 body modes: Weak sparticle particles from W decay
andy@71 967 ## Squark
andy@71 968 ## 2 body modes: Lepton Number Violated quark lepton
andy@71 969 ## Baryon number violated quark quark
andy@71 970 ## Slepton
andy@71 971 ## 2 body modes: Lepton Number Violated q or qbar
andy@199 972 d.ids = sorted(d.ids, cmp=cmp_bottomlast)
andy@71 973 ## Higgs
andy@71 974 elif pid in (25, 26):
andy@71 975 # TODO: Includes SUSY Higgses?
andy@71 976 ## Higgs
andy@71 977 ## 2 body modes: (s)quark-(s)qbar (s)q or (s)qbar
andy@71 978 ## (s)lepton-(s)lepton (s)l or (s)lbar
andy@71 979 ## 3 body modes: colour neutral q or qbar
andy@71 980 if len(absids) == 3:
andy@71 981 def cmp_quarkslast(a, b):
andy@71 982 """Comparison function which always puts quarks last"""
andy@71 983 if is_quark(a):
andy@71 984 return True
andy@71 985 if is_quark(b):
andy@71 986 return False
andy@71 987 return cmp(a, b)
andy@199 988 d.ids = sorted(d.ids, cmp=cmp_quarkslast)
andy@71 989 elif is_gaugino(pid):
andy@71 990 # TODO: Is there actually anything to do here?
andy@71 991 ## Gaugino
andy@71 992 ## 2 body modes: Squark-quark q or sq
andy@71 993 ## Slepton-lepton l or sl
andy@71 994 ##
andy@71 995 ## 3 body modes: R-parity conserved colour neutral q or qbar
andy@71 996 ## l or lbar
andy@71 997 if len(absids) == 3:
andy@71 998 def cmp_quarkslast(a, b):
andy@71 999 """Comparison function which always puts quarks last"""
andy@71 1000 if is_quark(a):
andy@71 1001 return True
andy@71 1002 if is_quark(b):
andy@70 1003 return False
andy@70 1004 return cmp(a, b)
andy@199 1005 d.ids = sorted(d.ids, cmp=cmp_quarkslast)
andy@66 1006
andy@71 1007 # TODO: Gaugino/Gluino
andy@67 1008 ## 3 body modes: R-parity violating: Particles in the same order as the R-parity violating superpotential
andy@66 1009
andy@66 1010 ## Pad out IDs list with zeros
andy@48 1011 ids = [0,0,0,0,0]
andy@48 1012 for i, pid in enumerate(d.ids):
andy@48 1013 ids[i] = pid
andy@48 1014 ids = map(str, ids)
andy@48 1015 decayout += " ".join(ids) + "\n"
andy@48 1016 decayout = "%d\n" % decayout.count("\n") + decayout
andy@48 1017 out += decayout
andy@48 1018
andy@48 1019 ## Now the SUSY parameters
andy@48 1020 ## TANB, ALPHAH:
andy@199 1021 out += (fmte + " " + fmte + "\n") % (blocks["MINPAR"].value(3), blocks["ALPHA"].value())
andy@48 1022 ## Neutralino mixing matrix
andy@200 1023 nmix = blocks["NMIX"]
andy@48 1024 for i in xrange(1, 5):
andy@199 1025 out += (fmte + " " + fmte + " " + fmte + " " + fmte + "\n") % (nmix[i,1], nmix[i,2], nmix[i,3], nmix[i,4])
andy@48 1026 ## Chargino mixing matrices V and U
andy@200 1027 vmix = blocks["VMIX"]
andy@199 1028 out += (fmte + " " + fmte + " " + fmte + " " + fmte + "\n") % (vmix[1,1], vmix[1,2], vmix[2,1], vmix[2,2])
andy@200 1029 umix = blocks["UMIX"]
andy@199 1030 out += (fmte + " " + fmte + " " + fmte + " " + fmte + "\n") % (umix[1,1], umix[1,2], umix[2,1], umix[2,2])
andy@160 1031 ## THETAT,THETAB,THETAL
andy@48 1032 import math
andy@200 1033 out += (fmte + " " + fmte + " " + fmte + " " + "\n") % (math.acos(blocks["STOPMIX"][1,1]),
andy@200 1034 math.acos(blocks["SBOTMIX"][1,1]),
andy@200 1035 math.acos(blocks["STAUMIX"][1,1]))
andy@160 1036 ## ATSS,ABSS,ALSS
andy@200 1037 out += (fmte + " " + fmte + " " + fmte + " " + "\n") % (blocks["AU"][3,3],
andy@200 1038 blocks["AD"][3,3],
andy@200 1039 blocks["AE"][3,3])
andy@160 1040 ## MUSS == sign(mu)
andy@201 1041 out += "%f\n" % blocks["MINPAR"][4]
andy@48 1042
andy@160 1043 ## RPV SUSY
andy@160 1044 isRPV = False
andy@160 1045 out += "%d\n" % isRPV
andy@160 1046 # TODO: Write RPV couplings if RPV is True (lambda1,2,3; 27 params in each, sci format.
andy@160 1047 # TODO: Get the index orderings right
andy@160 1048 # if isRPV: ...
andy@48 1049
andy@48 1050 return out
andy@48 1051
andy@48 1052
andy@161 1053 ###############################################################################
andy@161 1054 ## File-level functions
andy@161 1055
andy@161 1056
andy@161 1057 def readSLHAFile(spcfilename, **kwargs):
andy@161 1058 """
andy@161 1059 Read an SLHA file, returning dictionaries of blocks and decays.
andy@161 1060
andy@161 1061 Other keyword parameters are passed to readSLHA.
andy@161 1062 """
andy@161 1063 f = open(spcfilename, "r")
andy@161 1064 rtn = readSLHA(f.read(), kwargs)
andy@161 1065 f.close()
andy@161 1066 return rtn
andy@161 1067
andy@161 1068
andy@161 1069 def writeSLHAFile(spcfilename, blocks, decays, **kwargs):
andy@161 1070 """
andy@161 1071 Write an SLHA file from the supplied blocks and decays dicts.
andy@161 1072
andy@161 1073 Other keyword parameters are passed to writeSLHA.
andy@161 1074 """
andy@161 1075 f = open(spcfilename, "w")
andy@161 1076 f.write(writeSLHA(blocks, decays, kwargs))
andy@161 1077 f.close()
andy@161 1078
andy@161 1079
andy@161 1080 def readISAWIGFile(isafilename, **kwargs):
andy@161 1081 """
andy@161 1082 Read a spectrum definition from a file in the ISAWIG format, returning
andy@161 1083 dictionaries of blocks and decays. While this is not an SLHA format, it is
andy@161 1084 informally supported as a useful mechanism for converting ISAWIG spectra to
andy@161 1085 SLHA.
andy@161 1086
andy@161 1087 Other keyword parameters are passed to readSLHA.
andy@161 1088 """
andy@161 1089 f = open(isafilename, "r")
andy@161 1090 rtn = readISAWIG(f.read(), kwargs)
andy@161 1091 f.close()
andy@161 1092 return rtn
andy@161 1093
andy@161 1094
andy@161 1095 def writeISAWIGFile(isafilename, blocks, decays, **kwargs):
andy@161 1096 """
andy@161 1097 Write an ISAWIG file from the supplied blocks and decays dicts.
andy@161 1098
andy@161 1099 Other keyword parameters are passed to writeISAWIG.
andy@161 1100 """
andy@161 1101 f = open(isafilename, "w")
andy@161 1102 f.write(writeISAWIG(blocks, decays, kwargs))
andy@161 1103 f.close()
andy@161 1104
andy@161 1105
andy@161 1106 def writeISAJETFile(isafilename, blocks, decays, **kwargs):
andy@161 1107 """
andy@161 1108 Write an ISAJET file from the supplied blocks and decays dicts (see writeISAJET).
andy@161 1109
andy@161 1110 Other keyword parameters are passed to writeISAJET.
andy@161 1111 """
andy@161 1112 f = open(isafilename, "w")
andy@161 1113 f.write(writeISAWIG(blocks, decays, kwargs))
andy@161 1114 f.close()
andy@161 1115
andy@161 1116
andy@161 1117
andy@161 1118 ###############################################################################
andy@161 1119 ## Main function for module testing
andy@161 1120
andy@48 1121
andy@1 1122 if __name__ == "__main__":
andy@1 1123 import sys
andy@1 1124 for a in sys.argv[1:]:
andy@35 1125 if a.endswith(".isa"):
andy@35 1126 blocks, decays = readISAWIGFile(a)
andy@35 1127 else:
andy@35 1128 blocks, decays = readSLHAFile(a)
andy@3 1129
andy@5 1130 for bname, b in sorted(blocks.iteritems()):
andy@5 1131 print b
andy@5 1132 print
andy@3 1133
andy@47 1134 print blocks.keys()
andy@47 1135
andy@201 1136 print blocks["MASS"][25]
andy@6 1137 print
andy@6 1138
andy@6 1139 for p in sorted(decays.values()):
andy@6 1140 print p
andy@6 1141 print
andy@29 1142
andy@31 1143 print writeSLHA(blocks, decays, ignorenobr=True)

mercurial