pyslha.py

Wed, 11 Aug 2010 14:59:28 +0100

author
Andy Buckley <andy@insectnation.org>
date
Wed, 11 Aug 2010 14:59:28 +0100
changeset 12
558b3b392500
parent 8
c97f2f581685
child 19
3964fa60e8e3
permissions
-rw-r--r--

Added decays, and other tweaks

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@8 5 """
andy@8 6
andy@8 7 from __future__ import with_statement
andy@8 8 __author__ = "Andy Buckley <andy.buckley@cern.ch"
andy@8 9
andy@8 10
andy@1 11 import re
andy@1 12
andy@4 13 def _autotype(var):
andy@4 14 if type(var) != str:
andy@4 15 return var
andy@4 16 if var.isdigit():
andy@4 17 return int(var)
andy@4 18 try:
andy@4 19 f = float(var)
andy@4 20 return f
andy@4 21 except ValueError:
andy@4 22 return var
andy@4 23
andy@4 24
andy@12 25 class Block(object):
andy@8 26 """
andy@8 27 Object representation of any BLOCK elements read from the SLHA file. Blocks
andy@8 28 have a name, may have an associated Q value, and then a collection of data
andy@8 29 entries, stored as a recursive dictionary. Types in the dictionary are
andy@8 30 numeric (int or float) when a cast from the string in the file has been
andy@8 31 possible.
andy@8 32 """
andy@5 33 def __init__(self, name, q=None):
andy@1 34 self.name = name
andy@1 35 self.entries = {}
andy@5 36 self.q = _autotype(q)
andy@1 37
andy@1 38 def add_entry(self, entry):
andy@1 39 #print entry
andy@1 40 nextparent = self.entries
andy@1 41 if len(entry) < 2:
andy@1 42 raise Exception("Block entries must be at least a 2-tuple")
andy@4 43 #print "in", entry
andy@4 44 entry = map(_autotype, entry)
andy@4 45 #print "out", entry
andy@1 46 for e in entry[:-2]:
andy@1 47 if e is not entry[-1]:
andy@1 48 nextparent = nextparent.setdefault(e, {})
andy@1 49 nextparent[entry[-2]] = entry[-1]
andy@1 50 #print self.entries
andy@1 51
andy@1 52 def __cmp__(self, other):
andy@1 53 return self.name < other.name
andy@1 54
andy@1 55 def __str__(self):
andy@1 56 s = self.name
andy@5 57 if self.q is not None:
andy@5 58 s += " (Q=%s)" % self.q
andy@1 59 s += "\n"
andy@1 60 s += str(self.entries)
andy@1 61 return s
andy@1 62
andy@1 63
andy@12 64 class Decay(object):
andy@8 65 """
andy@8 66 Object representing a decay entry on a particle decribed by the SLHA file.
andy@8 67 'Decay' objects are not a direct representation of a DECAY block in an SLHA
andy@8 68 file... that role, somewhat confusingly, is taken by the Particle class.
andy@8 69
andy@8 70 Decay objects have three properties: a branching ratio, br, an nda number
andy@12 71 (number of daughters == len(ids)), and a tuple of PDG PIDs to which the
andy@12 72 decay occurs. The PDG ID of the particle whose decay this represents may
andy@12 73 also be stored, but this is normally known via the Particle in which the
andy@12 74 decay is stored.
andy@8 75 """
andy@8 76 def __init__(self, br, nda, ids, parentid=None):
andy@8 77 self.parentid = parentid
andy@6 78 self.br = br
andy@6 79 self.nda = nda
andy@6 80 self.ids = ids
andy@6 81
andy@6 82 def __cmp__(self, other):
andy@6 83 return self.br < other.br
andy@6 84
andy@6 85 def __str__(self):
andy@12 86 return "%e %s" % (self.br, self.ids)
andy@6 87
andy@6 88
andy@12 89 class Particle(object):
andy@8 90 """
andy@8 91 Representation of a single, specific particle, decay block from an SLHA
andy@8 92 file. These objects are not themselves called 'Decay', since that concept
andy@8 93 applies more naturally to the various decays found inside this
andy@8 94 object. Particle classes store the PDG ID (pid) of the particle being
andy@8 95 represented, and optionally the mass (mass) and total decay width
andy@8 96 (totalwidth) of that particle in the SLHA scenario. Masses may also be found
andy@8 97 via the MASS block, from which the Particle.mass property is filled, if at
andy@8 98 all. They also store a list of Decay objects (decays) which are probably the
andy@8 99 item of most interest.
andy@8 100 """
andy@6 101 def __init__(self, pid, totalwidth=None, mass=None):
andy@6 102 self.pid = pid
andy@6 103 self.totalwidth = totalwidth
andy@6 104 self.mass = mass
andy@6 105 self.decays = []
andy@6 106
andy@6 107 def add_decay(self, br, nda, ids):
andy@6 108 self.decays.append(Decay(br, nda, ids))
andy@6 109 self.decays.sort()
andy@6 110
andy@6 111 def __cmp__(self, other):
andy@6 112 if abs(self.pid) == abs(other.pid):
andy@6 113 return self.pid < other.pid
andy@6 114 return abs(self.pid) < abs(other.pid)
andy@6 115
andy@6 116 def __str__(self):
andy@6 117 s = str(self.pid)
andy@7 118 if self.mass is not None:
andy@7 119 s += " : mass = %e GeV" % self.mass
andy@6 120 if self.totalwidth is not None:
andy@7 121 s += " : total width = %e GeV" % self.totalwidth
andy@6 122 for d in self.decays:
andy@12 123 if d.br > 0.0:
andy@12 124 s += "\n %s" % d
andy@6 125 return s
andy@1 126
andy@1 127
andy@1 128 def readSpcFile(filename):
andy@1 129 blocks = {}
andy@1 130 decays = {}
andy@8 131 with open(filename, "r") as f:
andy@8 132 currentblock = None
andy@8 133 currentdecay = None
andy@8 134 for l in f:
andy@8 135 line = l.strip()[:-1]
andy@8 136 ## Handle (ignore) comment lines
andy@8 137 if line.startswith("#"):
andy@8 138 continue
andy@8 139 if "#" in line:
andy@8 140 line = line[:line.index("#")]
andy@1 141
andy@8 142 ## Handle BLOCK/DECAY start lines
andy@8 143 if line.upper().startswith("BLOCK"):
andy@8 144 match = re.match(r"BLOCK\s+(\w+)\s+(Q=\s*.+)?", line.upper())
andy@8 145 if not match:
andy@6 146 continue
andy@8 147 blockname = match.group(1)
andy@8 148 qstr = match.group(2)
andy@8 149 if qstr is not None:
andy@8 150 qstr = qstr[2:].strip()
andy@8 151 currentblock = blockname
andy@8 152 currentdecay = None
andy@8 153 blocks[blockname] = Block(blockname, q=qstr)
andy@8 154 elif line.upper().startswith("DECAY"):
andy@8 155 match = re.match(r"DECAY\s+(\d+)\s+([\d\.E+-]+).*", line.upper())
andy@8 156 if not match:
andy@8 157 continue
andy@8 158 pdgid = int(match.group(1))
andy@8 159 width = float(match.group(2))
andy@8 160 currentblock = "DECAY"
andy@8 161 currentdecay = pdgid
andy@8 162 decays[pdgid] = Particle(pdgid, width)
andy@8 163 else:
andy@8 164 ## In-block line
andy@8 165 if currentblock is not None:
andy@8 166 items = line.split()
andy@8 167 if len(items) < 2:
andy@8 168 continue
andy@8 169 # TODO: Sort out tuple item types: autoconvert integers and floats
andy@8 170 if currentblock != "DECAY":
andy@8 171 #print currentblock
andy@8 172 blocks[currentblock].add_entry(items)
andy@8 173 else:
andy@8 174 br = float(items[0])
andy@8 175 nda = int(items[1])
andy@8 176 ids = map(int, items[2:])
andy@8 177 decays[currentdecay].add_decay(br, nda, ids)
andy@1 178
andy@8 179 ## Try to populate Particle masses from the MASS block
andy@8 180 for pid in blocks["MASS"].entries.keys():
andy@8 181 if decays.has_key(pid):
andy@8 182 decays[pid].mass = blocks["MASS"].entries[pid]
andy@8 183
andy@1 184 return blocks, decays
andy@1 185
andy@1 186
andy@1 187 if __name__ == "__main__":
andy@1 188 import sys
andy@1 189 for a in sys.argv[1:]:
andy@1 190 blocks, decays = readSpcFile(a)
andy@3 191
andy@5 192 for bname, b in sorted(blocks.iteritems()):
andy@5 193 print b
andy@5 194 print
andy@3 195
andy@4 196 print blocks["MASS"].entries[25]
andy@6 197 print
andy@6 198
andy@6 199 for p in sorted(decays.values()):
andy@6 200 print p
andy@6 201 print

mercurial