pyslha.py

Mon, 29 Apr 2013 15:26:46 +0200

author
Andy Buckley <andy@insectnation.org>
date
Mon, 29 Apr 2013 15:26:46 +0200
changeset 215
8a64587e884f
parent 214
fa07ed634b18
child 218
dc6b80cfe4ac
permissions
-rw-r--r--

Fixing __str__ / __repr__ on Particle and Decay

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

mercurial