Clean-up of precision formatting in output.

Mon, 29 Apr 2013 14:54:16 +0200

author
Andy Buckley <andy@insectnation.org>
date
Mon, 29 Apr 2013 14:54:16 +0200
changeset 211
91f559c01cf7
parent 210
0f4f5472b7d8
child 212
3a6db3deedef

Clean-up of precision formatting in output.

ChangeLog file | annotate | diff | comparison | revisions
pyslha.py file | annotate | diff | comparison | revisions
     1.1 --- a/ChangeLog	Mon Apr 29 14:35:12 2013 +0200
     1.2 +++ b/ChangeLog	Mon Apr 29 14:54:16 2013 +0200
     1.3 @@ -1,5 +1,7 @@
     1.4  2013-04-29  Andy Buckley  <andy.buckley@cern.ch>
     1.5  
     1.6 +	* Clean-up of precision formatting in output.
     1.7 +
     1.8  	* Split writeSLHA implementation into distinct writeSLHA{Blocks,Decays}.
     1.9  
    1.10  2013-04-28  Andy Buckley  <andy.buckley@cern.ch>
     2.1 --- a/pyslha.py	Mon Apr 29 14:35:12 2013 +0200
     2.2 +++ b/pyslha.py	Mon Apr 29 14:54:16 2013 +0200
     2.3 @@ -36,11 +36,10 @@
     2.4  TODOs:
     2.5  
     2.6    For 2.0.0:
     2.7 -   * Output column alignment cosmetics
     2.8 -   * Precision setting obedience in SLHA output of values
     2.9 +   * Preserve comments from read -> write (needs full-line/inline comment separation?)
    2.10  
    2.11    For 2.1.0:
    2.12 -   * Preserve comments from read -> write (needs full-line/inline comment separation?)
    2.13 +   * Output column alignment cosmetics
    2.14  
    2.15    Later/maybe:
    2.16     * Identify HERWIG decay matrix element to use in ISAWIG
    2.17 @@ -383,22 +382,21 @@
    2.18  def writeSLHABlocks(blocks, precision=8):
    2.19      """Return an SLHA definition as a string, from the supplied blocks dict."""
    2.20      # TODO: Pay attention to space-padding and minus signs for column alignment
    2.21 -    fmte = "%." + str(precision) + "e"
    2.22 -    sep = "   "
    2.23 +    sep = 3 * " "
    2.24      blockstrs = []
    2.25      for bname, b in blocks.iteritems():
    2.26          namestr = b.name
    2.27          if b.q is not None:
    2.28 -            namestr += (" Q= " + fmte) % float(b.q)
    2.29 +            namestr += " Q= " + _autostr(float(b.q), precision)
    2.30          blockstr = "BLOCK %s\n" % namestr
    2.31          entrystrs = []
    2.32          for k, v in b.items():
    2.33              entrystr = ""
    2.34              if type(k) == tuple:
    2.35 -                entrystr += sep.join(_autostr(i) for i in k)
    2.36 +                entrystr += sep.join(_autostr(i, precision) for i in k)
    2.37              elif k is not None:
    2.38 -                entrystr += _autostr(k)
    2.39 -            entrystr += sep + _autostr(v) # TODO: apply precision formatting for floats
    2.40 +                entrystr += _autostr(k, precision)
    2.41 +            entrystr += sep + _autostr(v, precision)
    2.42              entrystrs.append(entrystr)
    2.43          blockstr += "\n".join(entrystrs)
    2.44          blockstrs.append(blockstr)
    2.45 @@ -408,16 +406,15 @@
    2.46  
    2.47  def writeSLHADecays(decays, ignorenobr=False, precision=8):
    2.48      """Return an SLHA decay definition as a string, from the supplied decays dict."""
    2.49 -    fmte = "%." + str(precision) + "e"
    2.50 -    sep = "   "
    2.51 +    sep = 3 * " "
    2.52      blockstrs = []
    2.53      for pid, particle in decays.iteritems():
    2.54 -        blockstr = ("DECAY %d " + fmte + "\n") % (particle.pid, particle.totalwidth or -1)
    2.55 +        blockstr = ("DECAY %d " % particle.pid) + _autostr(particle.totalwidth or -1, precision) + "\n"
    2.56          decaystrs = []
    2.57          for d in particle.decays:
    2.58              if d.br > 0.0 or not ignorenobr:
    2.59                  products_str = sep.join(map(str, d.ids))
    2.60 -                decaystr = sep + (fmte % d.br) + sep + ("%d" % len(d.ids)) + sep + products_str
    2.61 +                decaystr = sep + _autostr(d.br, precision) + sep + str(len(d.ids)) + sep + products_str
    2.62                  decaystrs.append(decaystr)
    2.63          blockstr += "\n".join(decaystrs)
    2.64          blockstrs.append(blockstr)
    2.65 @@ -769,58 +766,6 @@
    2.66      return blocks, decays
    2.67  
    2.68  
    2.69 -def writeISAJET(blocks, decays, outname, ignorenobr=False, precision=8):
    2.70 -    """
    2.71 -    Return a SUSY spectrum definition in the format required for input by ISAJET,
    2.72 -    as a string, from the supplied blocks and decays dicts.
    2.73 -
    2.74 -    The outname parameter specifies the desired output filename from ISAJET: this
    2.75 -    will appear in the first line of the return value.
    2.76 -
    2.77 -    If the ignorenobr parameter is True, do not write decay entries with a
    2.78 -    branching ratio of zero.
    2.79 -    """
    2.80 -    fmte = "%." + str(precision) + "e"
    2.81 -
    2.82 -    masses = blocks["MASS"]
    2.83 -
    2.84 -    ## Init output string
    2.85 -    out = ""
    2.86 -
    2.87 -    ## First line is the output name
    2.88 -    out += "'%s'" % outname + "\n"
    2.89 -
    2.90 -    ## Next the top mass
    2.91 -    out += fmte % masses[6] + "\n"
    2.92 -
    2.93 -    ## Next the top mass
    2.94 -    out += fmte % masses[6] + "\n"
    2.95 -
    2.96 -    ## mSUGRA parameters (one line)
    2.97 -    # e.g. 1273.78,713.286,804.721,4.82337
    2.98 -
    2.99 -    ## Masses and trilinear couplings (3 lines)
   2.100 -    # e.g. 1163.14,1114.15,1118.99,374.664,209.593
   2.101 -    # e.g. 1069.54,1112.7,919.908,374.556,209.381,-972.817,-326.745,-406.494
   2.102 -    # e.g. 1163.14,1114.15,1118.99,374.712,210.328
   2.103 -
   2.104 -    ## RPV couplings (?? lines)
   2.105 -    # e.g. 232.615,445.477
   2.106 -
   2.107 -    ## Etc ???!!!
   2.108 -    # e.g. /
   2.109 -    # e.g. n
   2.110 -    # e.g. y
   2.111 -    # e.g. y
   2.112 -    # e.g. 0.047441 3.80202e-23 0 0 0 2.17356e-22 0 0 5.23773e-09
   2.113 -    # e.g. y
   2.114 -    # 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
   2.115 -    # e.g. n
   2.116 -    # e.g. 'susy_RPV_stau_BC1scan_m560_tanb05.txt'
   2.117 -
   2.118 -    return out
   2.119 -
   2.120 -
   2.121  def writeISAWIG(blocks, decays, ignorenobr=False, precision=8):
   2.122      """
   2.123      Return a SUSY spectrum definition in the format produced by ISAWIG for inut to HERWIG
   2.124 @@ -832,8 +777,6 @@
   2.125      If the ignorenobr parameter is True, do not write decay entries with a
   2.126      branching ratio of zero.
   2.127      """
   2.128 -    fmte = "%." + str(precision) + "e"
   2.129 -
   2.130      masses = blocks["MASS"]
   2.131  
   2.132      ## Init output string
   2.133 @@ -854,7 +797,7 @@
   2.134                  lifetime = 1.0/(width * 1.51926778e24) ## lifetime in seconds == hbar/width in GeV
   2.135          except:
   2.136              pass
   2.137 -        massout += ("%d " + fmte + " " + fmte + "\n") % (pdgid2herwigid(pid), masses[pid], lifetime)
   2.138 +        massout += ("%d " % pdgid2herwigid(pid)) + _autostr(masses[pid], precision) + " " + _autostr(lifetime, precision) + "\n"
   2.139      out += "%d\n" % massout.count("\n")
   2.140      out += massout
   2.141  
   2.142 @@ -892,7 +835,7 @@
   2.143              if len(d.ids) == 3:
   2.144                  # TODO: How to determine the conditions for using 200 and 300 MEs? Enumeration of affected decays?
   2.145                  pass
   2.146 -            decayout += ("%d " + fmte + " %d ") % (hwid, d.br, nme)
   2.147 +            decayout += ("%d " % hwid) + _autostr(d.br, precision) + (" %d " % nme)
   2.148  
   2.149              def is_quark(pid):
   2.150                  return (abs(pid) in range(1, 7))
   2.151 @@ -1038,25 +981,34 @@
   2.152  
   2.153      ## Now the SUSY parameters
   2.154      ## TANB, ALPHAH:
   2.155 -    out += (fmte + " " + fmte + "\n") % (blocks["MINPAR"].value(3), blocks["ALPHA"].value())
   2.156 +    out += _autostr(blocks["MINPAR"][3], precision) + " " + _autostr(blocks["ALPHA"].value(), precision) + "\n"
   2.157      ## Neutralino mixing matrix
   2.158      nmix = blocks["NMIX"]
   2.159      for i in xrange(1, 5):
   2.160 -        out += (fmte + " " + fmte + " " + fmte + " " + fmte + "\n") % (nmix[i,1], nmix[i,2], nmix[i,3], nmix[i,4])
   2.161 +        out += _autostr(nmix[i,1], precision) + " " + \
   2.162 +               _autostr(nmix[i,2], precision) + " " + \
   2.163 +               _autostr(nmix[i,3], precision) + " " + \
   2.164 +               _autostr(nmix[i,4], precision) + "\n"
   2.165      ## Chargino mixing matrices V and U
   2.166      vmix = blocks["VMIX"]
   2.167 -    out += (fmte + " " + fmte + " " + fmte + " " + fmte + "\n") % (vmix[1,1], vmix[1,2], vmix[2,1], vmix[2,2])
   2.168 +    out += _autostr(vmix[1,1], precision) + " " + \
   2.169 +           _autostr(vmix[1,2], precision) + " " + \
   2.170 +           _autostr(vmix[2,1], precision) + " " + \
   2.171 +           _autostr(vmix[2,2], precision) + "\n"
   2.172      umix = blocks["UMIX"]
   2.173 -    out += (fmte + " " + fmte + " " + fmte + " " + fmte + "\n") % (umix[1,1], umix[1,2], umix[2,1], umix[2,2])
   2.174 +    out += _autostr(umix[1,1], precision) + " " + \
   2.175 +           _autostr(umix[1,2], precision) + " " + \
   2.176 +           _autostr(umix[2,1], precision) + " " + \
   2.177 +           _autostr(umix[2,2], precision) + "\n"
   2.178      ## THETAT,THETAB,THETAL
   2.179      import math
   2.180 -    out += (fmte + " " + fmte + " " + fmte + " " + "\n") % (math.acos(blocks["STOPMIX"][1,1]),
   2.181 -                                                            math.acos(blocks["SBOTMIX"][1,1]),
   2.182 -                                                            math.acos(blocks["STAUMIX"][1,1]))
   2.183 +    out += _autostr(math.acos(blocks["STOPMIX"][1,1]), precision) + " " + \
   2.184 +           _autostr(math.acos(blocks["SBOTMIX"][1,1]), precision) + " " + \
   2.185 +           _autostr(math.acos(blocks["STAUMIX"][1,1]), precision) + "\n"
   2.186      ## ATSS,ABSS,ALSS
   2.187 -    out += (fmte + " " + fmte + " " + fmte + " " + "\n") % (blocks["AU"][3,3],
   2.188 -                                                            blocks["AD"][3,3],
   2.189 -                                                            blocks["AE"][3,3])
   2.190 +    out += _autostr(blocks["AU"][3,3], precision) + " " + \
   2.191 +           _autostr(blocks["AD"][3,3], precision) + " " + \
   2.192 +           _autostr(blocks["AE"][3,3], precision) + "\n"
   2.193      ## MUSS == sign(mu)
   2.194      out += "%f\n" % blocks["MINPAR"][4]
   2.195  

mercurial