pyslha.py

Mon, 02 Aug 2010 22:10:37 +0200

author
Andy Buckley <andy@insectnation.org>
date
Mon, 02 Aug 2010 22:10:37 +0200
changeset 6
1e9ee18259e2
parent 5
825fed016ac1
child 7
a28827356061
permissions
-rw-r--r--

Decay parsing done

andy@1 1 #! /usr/bin/env python
andy@1 2
andy@1 3 import re
andy@1 4
andy@4 5 def _autotype(var):
andy@4 6 if type(var) != str:
andy@4 7 return var
andy@4 8 if var.isdigit():
andy@4 9 return int(var)
andy@4 10 try:
andy@4 11 f = float(var)
andy@4 12 return f
andy@4 13 except ValueError:
andy@4 14 return var
andy@4 15
andy@4 16
andy@1 17 class Block:
andy@5 18 def __init__(self, name, q=None):
andy@1 19 self.name = name
andy@1 20 self.entries = {}
andy@5 21 self.q = _autotype(q)
andy@1 22
andy@1 23 def add_entry(self, entry):
andy@1 24 #print entry
andy@1 25 nextparent = self.entries
andy@1 26 if len(entry) < 2:
andy@1 27 raise Exception("Block entries must be at least a 2-tuple")
andy@4 28 #print "in", entry
andy@4 29 entry = map(_autotype, entry)
andy@4 30 #print "out", entry
andy@1 31 for e in entry[:-2]:
andy@1 32 if e is not entry[-1]:
andy@1 33 nextparent = nextparent.setdefault(e, {})
andy@1 34 nextparent[entry[-2]] = entry[-1]
andy@1 35 #print self.entries
andy@1 36
andy@1 37 def __cmp__(self, other):
andy@1 38 return self.name < other.name
andy@1 39
andy@1 40 def __str__(self):
andy@1 41 s = self.name
andy@5 42 if self.q is not None:
andy@5 43 s += " (Q=%s)" % self.q
andy@1 44 s += "\n"
andy@1 45 s += str(self.entries)
andy@1 46 return s
andy@1 47
andy@1 48
andy@1 49 class Decay:
andy@6 50 def __init__(self, br, nda, ids):
andy@6 51 self.br = br
andy@6 52 self.nda = nda
andy@6 53 self.ids = ids
andy@6 54
andy@6 55 def __cmp__(self, other):
andy@6 56 return self.br < other.br
andy@6 57
andy@6 58 def __str__(self):
andy@6 59 return "%e %d %s" % (self.br, self.nda, self.ids)
andy@6 60
andy@6 61
andy@6 62 class Particle:
andy@6 63 def __init__(self, pid, totalwidth=None, mass=None):
andy@6 64 self.pid = pid
andy@6 65 self.totalwidth = totalwidth
andy@6 66 self.mass = mass
andy@6 67 self.decays = []
andy@6 68
andy@6 69 def add_decay(self, br, nda, ids):
andy@6 70 self.decays.append(Decay(br, nda, ids))
andy@6 71 self.decays.sort()
andy@6 72
andy@6 73 def __cmp__(self, other):
andy@6 74 if abs(self.pid) == abs(other.pid):
andy@6 75 return self.pid < other.pid
andy@6 76 return abs(self.pid) < abs(other.pid)
andy@6 77
andy@6 78 def __str__(self):
andy@6 79 s = str(self.pid)
andy@6 80 if self.totalwidth is not None:
andy@6 81 s += " : total width = %e pb" % self.totalwidth
andy@6 82 for d in self.decays:
andy@6 83 s += "\n %s" % d
andy@6 84 return s
andy@1 85
andy@1 86
andy@1 87 def readSpcFile(filename):
andy@1 88 ## TODO: Use new file handling from future
andy@1 89 blocks = {}
andy@1 90 decays = {}
andy@1 91 f = open(filename, "r")
andy@1 92 currentblock = None
andy@6 93 currentdecay = None
andy@1 94 for l in f:
andy@1 95 line = l.strip()[:-1]
andy@1 96 ## Handle (ignore) comment lines
andy@1 97 if line.startswith("#"):
andy@1 98 continue
andy@1 99 if "#" in line:
andy@1 100 line = line[:line.index("#")]
andy@1 101
andy@1 102 ## Handle BLOCK/DECAY start lines
andy@3 103 if line.upper().startswith("BLOCK"):
andy@5 104 match = re.match(r"BLOCK\s+(\w+)\s+(Q=\s*.+)?", line.upper())
andy@5 105 if not match:
andy@5 106 continue
andy@5 107 blockname = match.group(1)
andy@5 108 qstr = match.group(2)
andy@5 109 if qstr is not None:
andy@5 110 qstr = qstr[2:].strip()
andy@1 111 currentblock = blockname
andy@6 112 currentdecay = None
andy@5 113 blocks[blockname] = Block(blockname, q=qstr)
andy@3 114 elif line.upper().startswith("DECAY"):
andy@6 115 match = re.match(r"DECAY\s+(\d+)\s+([\d\.E+-]+).*", line.upper())
andy@6 116 if not match:
andy@6 117 continue
andy@6 118 pdgid = int(match.group(1))
andy@6 119 width = float(match.group(2))
andy@1 120 currentblock = "DECAY"
andy@6 121 currentdecay = pdgid
andy@6 122 decays[pdgid] = Particle(pdgid, width)
andy@6 123 else:
andy@6 124 ## In-block line
andy@6 125 if currentblock is not None:
andy@6 126 items = line.split()
andy@6 127 if len(items) < 2:
andy@6 128 continue
andy@6 129 # TODO: Sort out tuple item types: autoconvert integers and floats
andy@6 130 if currentblock != "DECAY":
andy@6 131 #print currentblock
andy@6 132 blocks[currentblock].add_entry(items)
andy@6 133 else:
andy@6 134 br = float(items[0])
andy@6 135 nda = int(items[1])
andy@6 136 ids = map(int, items[2:])
andy@6 137 decays[currentdecay].add_decay(br, nda, ids)
andy@1 138
andy@1 139 f.close()
andy@1 140 return blocks, decays
andy@1 141
andy@1 142
andy@1 143 if __name__ == "__main__":
andy@1 144 import sys
andy@1 145 for a in sys.argv[1:]:
andy@1 146 blocks, decays = readSpcFile(a)
andy@3 147
andy@5 148 for bname, b in sorted(blocks.iteritems()):
andy@5 149 print b
andy@5 150 print
andy@3 151
andy@4 152 print blocks["MASS"].entries[25]
andy@6 153 print
andy@6 154
andy@6 155 for p in sorted(decays.values()):
andy@6 156 print p
andy@6 157 print

mercurial