pyslha.py

changeset 147
55fc50e4ac53
parent 145
718d8a3355f0
child 148
9b827f9aa0e7
     1.1 --- a/pyslha.py	Sat May 28 23:21:04 2011 +0100
     1.2 +++ b/pyslha.py	Mon Jun 20 10:08:55 2011 +0100
     1.3 @@ -39,10 +39,10 @@
     1.4      except ValueError:
     1.5          return var
     1.6  
     1.7 -def _autostr(var):
     1.8 +def _autostr(var, precision=8):
     1.9      """Automatically numerical types to the right sort of string."""
    1.10      if type(var) is float:
    1.11 -        return "%e" % var
    1.12 +        return ("%." + str(precision) + "e") % var
    1.13      return str(var)
    1.14  
    1.15  
    1.16 @@ -112,7 +112,7 @@
    1.17          return cmp(other.br, self.br)
    1.18  
    1.19      def __str__(self):
    1.20 -        return "%e %s" % (self.br, self.ids)
    1.21 +        return "%.8e %s" % (self.br, self.ids)
    1.22  
    1.23      def __repr__(self):
    1.24          return self.__str__()
    1.25 @@ -148,9 +148,9 @@
    1.26      def __str__(self):
    1.27          s = str(self.pid)
    1.28          if self.mass is not None:
    1.29 -            s += " : mass = %e GeV" % self.mass
    1.30 +            s += " : mass = %.8e GeV" % self.mass
    1.31          if self.totalwidth is not None:
    1.32 -            s += " : total width = %e GeV" % self.totalwidth
    1.33 +            s += " : total width = %.8e GeV" % self.totalwidth
    1.34          for d in self.decays:
    1.35              if d.br > 0.0:
    1.36                  s += "\n  %s" % d
    1.37 @@ -277,10 +277,12 @@
    1.38  # TODO: Split writeSLHA into writeSLHA{Blocks,Decays}
    1.39  
    1.40  
    1.41 -def writeSLHA(blocks, decays, ignorenobr=False):
    1.42 +def writeSLHA(blocks, decays, ignorenobr=False, precision=8):
    1.43      """
    1.44      Return an SLHA definition as a string, from the supplied blocks and decays dicts.
    1.45      """
    1.46 +    fmte = "%." + str(precision) + "e"
    1.47 +
    1.48      sep = "   "
    1.49      out = ""
    1.50      def dict_hier_strs(d, s=""):
    1.51 @@ -294,18 +296,18 @@
    1.52      for bname, b in sorted(blocks.iteritems()):
    1.53          namestr = b.name
    1.54          if b.q is not None:
    1.55 -            namestr += " Q= %e" % float(b.q)
    1.56 +            namestr += (" Q= " + fmte) % float(b.q)
    1.57          out += "BLOCK %s\n" % namestr
    1.58          for s in dict_hier_strs(b.entries):
    1.59              out += sep + s + "\n"
    1.60          out += "\n"
    1.61      ## Decays
    1.62      for pid, particle in sorted(decays.iteritems()):
    1.63 -        out += "DECAY %d %e\n" % (particle.pid, particle.totalwidth or -1)
    1.64 +        out += ("DECAY %d " + fmte + "\n") % (particle.pid, particle.totalwidth or -1)
    1.65          for d in sorted(particle.decays):
    1.66              if d.br > 0.0 or not ignorenobr:
    1.67                  products_str = "   ".join(map(str, d.ids))
    1.68 -                out += sep + "%e" % d.br + sep + "%d" % len(d.ids) + sep + products_str + "\n"
    1.69 +                out += sep + fmte % d.br + sep + "%d" % len(d.ids) + sep + products_str + "\n"
    1.70          out += "\n"
    1.71      return out
    1.72  
    1.73 @@ -657,7 +659,7 @@
    1.74      f.close()
    1.75  
    1.76  
    1.77 -def writeISAWIG(blocks, decays, ignorenobr=False):
    1.78 +def writeISAWIG(blocks, decays, ignorenobr=False, precision=8):
    1.79      """
    1.80      Return an ISAWIG definition as a string, from the supplied blocks and decays dicts.
    1.81  
    1.82 @@ -667,6 +669,7 @@
    1.83      If the ignorenobr parameter is True, do not write decay entries with a
    1.84      branching ratio of zero.
    1.85      """
    1.86 +    fmte = "%." + str(precision) + "e"
    1.87  
    1.88      masses = blocks["MASS"].entries
    1.89  
    1.90 @@ -688,7 +691,7 @@
    1.91                  lifetime = 1.0/(width * 1.51926778e24) ## lifetime in seconds == hbar/width in GeV
    1.92          except:
    1.93              pass
    1.94 -        massout += "%d %e %e\n" % (pdgid2herwigid(pid), masses[pid], lifetime)
    1.95 +        massout += ("%d " + fmte + " " + fmte + "\n") % (pdgid2herwigid(pid), masses[pid], lifetime)
    1.96      out += "%d\n" % massout.count("\n")
    1.97      out += massout
    1.98  
    1.99 @@ -726,7 +729,7 @@
   1.100              if len(d.ids) == 3:
   1.101                  # TODO: How to determine the conditions for using 200 and 300 MEs? Enumeration of affected decays?
   1.102                  pass
   1.103 -            decayout += "%d %e %d " % (hwid, d.br, nme)
   1.104 +            decayout += "%d " + fmte + " %d " % (hwid, d.br, nme)
   1.105  
   1.106              def is_quark(pid):
   1.107                  return (abs(pid) in range(1, 7))
   1.108 @@ -872,25 +875,25 @@
   1.109  
   1.110      ## Now the SUSY parameters
   1.111      ## TANB, ALPHAH:
   1.112 -    out += "%e %e\n" % (blocks["MINPAR"].entries[3], blocks["ALPHA"].entries)
   1.113 +    out += (fmte + " " + fmte + "\n") % (blocks["MINPAR"].entries[3], blocks["ALPHA"].entries)
   1.114      ## Neutralino mixing matrix
   1.115      nmix = blocks["NMIX"].entries
   1.116      for i in xrange(1, 5):
   1.117 -        out += "%e %e %e %e\n" % (nmix[i][1], nmix[i][2], nmix[i][3], nmix[i][4])
   1.118 +        out += (fmte + " " + fmte + " " + fmte + " " + fmte + "\n") % (nmix[i][1], nmix[i][2], nmix[i][3], nmix[i][4])
   1.119      ## Chargino mixing matrices V and U
   1.120      vmix = blocks["VMIX"].entries
   1.121 -    out += "%e %e %e %e\n" % (vmix[1][1], vmix[1][2], vmix[2][1], vmix[2][2])
   1.122 +    out += (fmte + " " + fmte + " " + fmte + " " + fmte + "\n") % (vmix[1][1], vmix[1][2], vmix[2][1], vmix[2][2])
   1.123      umix = blocks["UMIX"].entries
   1.124 -    out += "%e %e %e %e\n" % (umix[1][1], umix[1][2], umix[2][1], umix[2][2])
   1.125 +    out += (fmte + " " + fmte + " " + fmte + " " + fmte + "\n") % (umix[1][1], umix[1][2], umix[2][1], umix[2][2])
   1.126      # THETAT,THETAB,THETAL
   1.127      import math
   1.128 -    out += "%e %e %e\n" % (math.acos(blocks["STOPMIX"].entries[1][1]),
   1.129 -                           math.acos(blocks["SBOTMIX"].entries[1][1]),
   1.130 -                           math.acos(blocks["STAUMIX"].entries[1][1]))
   1.131 +    out += (fmte + " " + fmte + " " + fmte + " " + "\n") % (math.acos(blocks["STOPMIX"].entries[1][1]),
   1.132 +                                                            math.acos(blocks["SBOTMIX"].entries[1][1]),
   1.133 +                                                            math.acos(blocks["STAUMIX"].entries[1][1]))
   1.134      # ATSS,ABSS,ALSS
   1.135 -    out += "%e %e %e\n" % (blocks["AU"].entries[3][3],
   1.136 -                           blocks["AD"].entries[3][3],
   1.137 -                           blocks["AE"].entries[3][3])
   1.138 +    out += (fmte + " " + fmte + " " + fmte + " " + "\n") % (blocks["AU"].entries[3][3],
   1.139 +                                                            blocks["AD"].entries[3][3],
   1.140 +                                                            blocks["AE"].entries[3][3])
   1.141      # MUSS == sign(mu)
   1.142      out += "%f\n" % blocks["MINPAR"].entries[4]
   1.143  

mercurial