pyslha.py

Sun, 28 Apr 2013 21:16:12 +0200

author
Andy Buckley <andy@insectnation.org>
date
Sun, 28 Apr 2013 21:16:12 +0200
changeset 200
651b0fac2163
parent 199
80d4b063103a
child 201
3ac555efdbba
permissions
-rw-r--r--

More cleaning up with use of the updated Block API, and adding a keys() method

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

mercurial