pyslha.py: Add precision arguments to the write* functions, with default FP precision set to .8 as suggested by Diego Restrepo.

Mon, 20 Jun 2011 10:08:55 +0100

author
Andy Buckley <andy@insectnation.org>
date
Mon, 20 Jun 2011 10:08:55 +0100
changeset 147
55fc50e4ac53
parent 146
fcabf2fc2ceb
child 148
9b827f9aa0e7

pyslha.py: Add precision arguments to the write* functions, with default FP precision set to .8 as suggested by Diego Restrepo.

ChangeLog file | annotate | diff | comparison | revisions
pyslha.py file | annotate | diff | comparison | revisions
     1.1 --- a/ChangeLog	Sat May 28 23:21:04 2011 +0100
     1.2 +++ b/ChangeLog	Mon Jun 20 10:08:55 2011 +0100
     1.3 @@ -1,3 +1,9 @@
     1.4 +2011-06-20  Andy Buckley  <andy@insectnation.org>
     1.5 +
     1.6 +	* pyslha.py: Add "precision" arguments to the write* functions,
     1.7 +	with default FP precision set to ".8" as suggested by Diego
     1.8 +	Restrepo.
     1.9 +
    1.10  2011-04-10  Andy Buckley  <andy@insectnation.org>
    1.11  
    1.12  	* slhaplot: Add a warning about lack of provided decays if decays
     2.1 --- a/pyslha.py	Sat May 28 23:21:04 2011 +0100
     2.2 +++ b/pyslha.py	Mon Jun 20 10:08:55 2011 +0100
     2.3 @@ -39,10 +39,10 @@
     2.4      except ValueError:
     2.5          return var
     2.6  
     2.7 -def _autostr(var):
     2.8 +def _autostr(var, precision=8):
     2.9      """Automatically numerical types to the right sort of string."""
    2.10      if type(var) is float:
    2.11 -        return "%e" % var
    2.12 +        return ("%." + str(precision) + "e") % var
    2.13      return str(var)
    2.14  
    2.15  
    2.16 @@ -112,7 +112,7 @@
    2.17          return cmp(other.br, self.br)
    2.18  
    2.19      def __str__(self):
    2.20 -        return "%e %s" % (self.br, self.ids)
    2.21 +        return "%.8e %s" % (self.br, self.ids)
    2.22  
    2.23      def __repr__(self):
    2.24          return self.__str__()
    2.25 @@ -148,9 +148,9 @@
    2.26      def __str__(self):
    2.27          s = str(self.pid)
    2.28          if self.mass is not None:
    2.29 -            s += " : mass = %e GeV" % self.mass
    2.30 +            s += " : mass = %.8e GeV" % self.mass
    2.31          if self.totalwidth is not None:
    2.32 -            s += " : total width = %e GeV" % self.totalwidth
    2.33 +            s += " : total width = %.8e GeV" % self.totalwidth
    2.34          for d in self.decays:
    2.35              if d.br > 0.0:
    2.36                  s += "\n  %s" % d
    2.37 @@ -277,10 +277,12 @@
    2.38  # TODO: Split writeSLHA into writeSLHA{Blocks,Decays}
    2.39  
    2.40  
    2.41 -def writeSLHA(blocks, decays, ignorenobr=False):
    2.42 +def writeSLHA(blocks, decays, ignorenobr=False, precision=8):
    2.43      """
    2.44      Return an SLHA definition as a string, from the supplied blocks and decays dicts.
    2.45      """
    2.46 +    fmte = "%." + str(precision) + "e"
    2.47 +
    2.48      sep = "   "
    2.49      out = ""
    2.50      def dict_hier_strs(d, s=""):
    2.51 @@ -294,18 +296,18 @@
    2.52      for bname, b in sorted(blocks.iteritems()):
    2.53          namestr = b.name
    2.54          if b.q is not None:
    2.55 -            namestr += " Q= %e" % float(b.q)
    2.56 +            namestr += (" Q= " + fmte) % float(b.q)
    2.57          out += "BLOCK %s\n" % namestr
    2.58          for s in dict_hier_strs(b.entries):
    2.59              out += sep + s + "\n"
    2.60          out += "\n"
    2.61      ## Decays
    2.62      for pid, particle in sorted(decays.iteritems()):
    2.63 -        out += "DECAY %d %e\n" % (particle.pid, particle.totalwidth or -1)
    2.64 +        out += ("DECAY %d " + fmte + "\n") % (particle.pid, particle.totalwidth or -1)
    2.65          for d in sorted(particle.decays):
    2.66              if d.br > 0.0 or not ignorenobr:
    2.67                  products_str = "   ".join(map(str, d.ids))
    2.68 -                out += sep + "%e" % d.br + sep + "%d" % len(d.ids) + sep + products_str + "\n"
    2.69 +                out += sep + fmte % d.br + sep + "%d" % len(d.ids) + sep + products_str + "\n"
    2.70          out += "\n"
    2.71      return out
    2.72  
    2.73 @@ -657,7 +659,7 @@
    2.74      f.close()
    2.75  
    2.76  
    2.77 -def writeISAWIG(blocks, decays, ignorenobr=False):
    2.78 +def writeISAWIG(blocks, decays, ignorenobr=False, precision=8):
    2.79      """
    2.80      Return an ISAWIG definition as a string, from the supplied blocks and decays dicts.
    2.81  
    2.82 @@ -667,6 +669,7 @@
    2.83      If the ignorenobr parameter is True, do not write decay entries with a
    2.84      branching ratio of zero.
    2.85      """
    2.86 +    fmte = "%." + str(precision) + "e"
    2.87  
    2.88      masses = blocks["MASS"].entries
    2.89  
    2.90 @@ -688,7 +691,7 @@
    2.91                  lifetime = 1.0/(width * 1.51926778e24) ## lifetime in seconds == hbar/width in GeV
    2.92          except:
    2.93              pass
    2.94 -        massout += "%d %e %e\n" % (pdgid2herwigid(pid), masses[pid], lifetime)
    2.95 +        massout += ("%d " + fmte + " " + fmte + "\n") % (pdgid2herwigid(pid), masses[pid], lifetime)
    2.96      out += "%d\n" % massout.count("\n")
    2.97      out += massout
    2.98  
    2.99 @@ -726,7 +729,7 @@
   2.100              if len(d.ids) == 3:
   2.101                  # TODO: How to determine the conditions for using 200 and 300 MEs? Enumeration of affected decays?
   2.102                  pass
   2.103 -            decayout += "%d %e %d " % (hwid, d.br, nme)
   2.104 +            decayout += "%d " + fmte + " %d " % (hwid, d.br, nme)
   2.105  
   2.106              def is_quark(pid):
   2.107                  return (abs(pid) in range(1, 7))
   2.108 @@ -872,25 +875,25 @@
   2.109  
   2.110      ## Now the SUSY parameters
   2.111      ## TANB, ALPHAH:
   2.112 -    out += "%e %e\n" % (blocks["MINPAR"].entries[3], blocks["ALPHA"].entries)
   2.113 +    out += (fmte + " " + fmte + "\n") % (blocks["MINPAR"].entries[3], blocks["ALPHA"].entries)
   2.114      ## Neutralino mixing matrix
   2.115      nmix = blocks["NMIX"].entries
   2.116      for i in xrange(1, 5):
   2.117 -        out += "%e %e %e %e\n" % (nmix[i][1], nmix[i][2], nmix[i][3], nmix[i][4])
   2.118 +        out += (fmte + " " + fmte + " " + fmte + " " + fmte + "\n") % (nmix[i][1], nmix[i][2], nmix[i][3], nmix[i][4])
   2.119      ## Chargino mixing matrices V and U
   2.120      vmix = blocks["VMIX"].entries
   2.121 -    out += "%e %e %e %e\n" % (vmix[1][1], vmix[1][2], vmix[2][1], vmix[2][2])
   2.122 +    out += (fmte + " " + fmte + " " + fmte + " " + fmte + "\n") % (vmix[1][1], vmix[1][2], vmix[2][1], vmix[2][2])
   2.123      umix = blocks["UMIX"].entries
   2.124 -    out += "%e %e %e %e\n" % (umix[1][1], umix[1][2], umix[2][1], umix[2][2])
   2.125 +    out += (fmte + " " + fmte + " " + fmte + " " + fmte + "\n") % (umix[1][1], umix[1][2], umix[2][1], umix[2][2])
   2.126      # THETAT,THETAB,THETAL
   2.127      import math
   2.128 -    out += "%e %e %e\n" % (math.acos(blocks["STOPMIX"].entries[1][1]),
   2.129 -                           math.acos(blocks["SBOTMIX"].entries[1][1]),
   2.130 -                           math.acos(blocks["STAUMIX"].entries[1][1]))
   2.131 +    out += (fmte + " " + fmte + " " + fmte + " " + "\n") % (math.acos(blocks["STOPMIX"].entries[1][1]),
   2.132 +                                                            math.acos(blocks["SBOTMIX"].entries[1][1]),
   2.133 +                                                            math.acos(blocks["STAUMIX"].entries[1][1]))
   2.134      # ATSS,ABSS,ALSS
   2.135 -    out += "%e %e %e\n" % (blocks["AU"].entries[3][3],
   2.136 -                           blocks["AD"].entries[3][3],
   2.137 -                           blocks["AE"].entries[3][3])
   2.138 +    out += (fmte + " " + fmte + " " + fmte + " " + "\n") % (blocks["AU"].entries[3][3],
   2.139 +                                                            blocks["AD"].entries[3][3],
   2.140 +                                                            blocks["AE"].entries[3][3])
   2.141      # MUSS == sign(mu)
   2.142      out += "%f\n" % blocks["MINPAR"].entries[4]
   2.143  

mercurial