[Rivet-svn] r1944 - trunk/bin

blackhole at projects.hepforge.org blackhole at projects.hepforge.org
Wed Oct 21 10:41:00 BST 2009


Author: eike
Date: Wed Oct 21 10:40:59 2009
New Revision: 1944

Log:
use lighthist.py module in some more scripts

Modified:
   trunk/bin/aida2flat
   trunk/bin/compare-histos

Modified: trunk/bin/aida2flat
==============================================================================
--- trunk/bin/aida2flat	Wed Oct 21 08:00:54 2009	(r1943)
+++ trunk/bin/aida2flat	Wed Oct 21 10:40:59 2009	(r1944)
@@ -2,6 +2,8 @@
 
 import sys, os, logging
 
+import lighthisto
+
 
 ## Make "sorted" a builtin function on Python < 2.4
 if not 'sorted' in dir(__builtins__):
@@ -18,156 +20,6 @@
     logging.log = _logit
 
 
-class Histo:
-    def __init__(self):
-        self._bins = []
-        self.path = None
-        self.name = None
-        self.title = None
-        self.xlabel = None
-        self.ylabel = None
-
-    def __cmp__(self, other):
-        """Sort by $path/$name string"""
-        return self.fullPath() > other.fullPath()
-
-    def __str__(self):
-        out = "Histogram '%s' with %d bins\n" % (self.fullPath(), self.numBins())
-        out += "Title: %s\n" % self.title
-        out += "XLabel: %s\n" % self.xlabel
-        out += "YLabel: %s\n" % self.ylabel
-        out += "\n".join([str(b) for b in self.getBins()])
-        return out
-
-    def fullPath(self):
-        return os.path.join(self.path, self.name)
-
-    def header(self):
-        global headerprefix
-        out = "# BEGIN PLOT\n"
-        out += headerprefix + "LogY=1\n"
-        out += headerprefix + "Title=%s\n" % self.title
-        out += headerprefix + "XLabel=%s\n" % self.xlabel
-        out += headerprefix + "YLabel=%s\n" % self.ylabel
-        out += "# END PLOT\n"
-        return out
-
-    def asFlat(self):
-        global headerprefix
-        global opts
-        out = "# BEGIN HISTOGRAM %s\n" % self.fullPath()
-        out += headerprefix + "AidaPath=%s\n" % self.fullPath()
-        out += headerprefix + "Title=%s\n" % self.title
-        out += headerprefix + "XLabel=%s\n" % self.xlabel
-        out += headerprefix + "YLabel=%s\n" % self.ylabel
-        if self.fullPath().startswith('/REF'):
-            out += headerprefix + "PolyMarker=*\n"
-            out += headerprefix + "ErrorBars=1\n"
-        out += "## Area: %s\n" % self.area()
-        out += "## Num bins: %d\n" % self.numBins()
-        if opts.GNUPLOT:
-            out += "## xval  \tyval    \txlow    \txhigh    \tylow     \tyhigh\n"
-        else:
-            out += "## xlow  \txhigh   \tyval    \tyerrminus\tyerrplus\n"
-        out += "\n".join([b.asFlat() for b in self.getBins()])
-        out += "\n# END HISTOGRAM"
-        return out
-
-    def numBins(self):
-        return len(self._bins)
-
-    def getBins(self):
-        return sorted(self._bins)
-
-    def setBins(self, bins):
-        self._bins = bins
-        return self
-
-    def addBin(self, bin):
-        self._bins.append(bin)
-        return self
-
-    def getBin(self, index):
-        self._bins.sort()
-        return self.getBins()[index]
-
-    bins = property(getBins, setBins)
-
-    def area(self):
-        return sum([bin.area() for bin in self.bins])
-
-    def __iter__(self):
-        return iter(self.getBins())
-
-    def __len__(self):
-        return len(self._bins)
-
-    def __getitem__(self, index):
-        return self.getBin(index)
-
-
-class Bin:
-    """A simple container for a binned value with an error."""
-    def __init__(self, xlow=None, xhigh=None, yval=0, yerrplus=0, yerrminus=0, focus=None):
-        self.xlow = xlow
-        self.xhigh= xhigh
-        self.yval = yval
-        self.yerrplus = yerrplus
-        self.yerrminus = yerrminus
-        self.focus= focus
-
-    def __str__(self):
-        out = "%e to %e: %e +- %e" % (self._xlow, self._xhigh, self._yval, self._yerr)
-        return out
-
-    def asFlat(self):
-        global opts
-        if opts.GNUPLOT:
-            out = "%e\t%e\t%e\t%e\t%e\t%e" % (self.getBinCenter(), self.yval,
-                                              self.xlow, self.xhigh, 
-                                              self.yval-self.yerrminus, self.yval+self.yerrplus)
-        else:
-            out = "%e\t%e\t%e\t%e\t%e" % (self.xlow, self.xhigh, self.yval, self.yerrminus, self.yerrplus)
-        return out
-
-    def __cmp__(self, other):
-        """Sort by mean x value (yeah, I know...)"""
-        return (self.xlow + self.xhigh) > (other.xlow + other.xhigh)
-
-    def getXRange(self):
-        return (self.xlow, self.xhigh)
-
-    def setXRange(self, xlow, xhigh):
-        self.xlow = xlow
-        self.xhigh = xhigh
-        return self
-
-    def getBinCenter(self):
-        """Geometric middle of the bin range."""
-        return self.xlow + .5*(self.xhigh - self.xlow)
-
-    def getFocus(self):
-        """Mean x-value of the bin."""
-        if self.focus is not None:
-            return (self.xlow + self.xhigh)/2.0
-        else:
-            return self.focus
-
-    def area(self):
-        return self.yval * (self.xhigh - self.xlow)
-
-    def getYErr(self):
-        """Get mean of +ve and -ve y-errors."""
-        return (self.yerrplus + self.yerrminus)/2.0
-
-    def setYErr(self, yerr):
-        """Set both +ve and -ve y-errors simultaneously."""
-        self.yerrplus = yerr
-        self.yerrminus = yerr
-        return self
-
-
-
 ## Try to load faster but non-standard cElementTree module
 try:
     import xml.etree.cElementTree as ET
@@ -180,42 +32,6 @@
         except:
             sys.stderr.write("Can't load the ElementTree XML parser: please install it!\n")
             sys.exit(1)
-
-
-def mkHistoFromDPS(dps):
-    """Make a mini histo representation from an AIDA dataPointSet tag."""
-    myhist = Histo()
-    myhist.name = dps.get("name")
-    myhist.title = dps.get("title")
-    myhist.path = dps.get("path")
-    axes = dps.findall("dimension")
-    if (len(axes)==2):
-        for a in axes:
-            if (a.get("dim")=="0"):
-                myhist.xlabel = a.get("title")
-            elif (a.get("dim")=="1"):
-                myhist.ylabel = a.get("title")
-    points = dps.findall("dataPoint")
-    numbins = len(points)
-    for binnum, point in enumerate(points):
-        bin = Bin()
-        for d, m in enumerate(point.findall("measurement")):
-            val  = float(m.get("value"))
-            down = float(m.get("errorMinus"))
-            up = float(m.get("errorPlus"))
-            if d == 0:
-                low  = val - down
-                high = val + up
-                bin.setXRange(low, high)
-            elif d == 1:
-                bin.yval = val
-                bin.yerrplus = up
-                bin.yerrminus = down
-        myhist.addBin(bin)
-    return myhist
-
-
-
 ##########################################################
 
 
@@ -292,7 +108,7 @@
                         useThisDps = True
                         break
             if useThisDps:
-                histos.append(mkHistoFromDPS(dps))
+                histos.append(lighthisto.Histo.fromDPS(dps))
         if len(histos) > 0:
             if opts.SPLITOUTPUT:
                 paper = os.path.basename(aidafile).replace(".aida", "")

Modified: trunk/bin/compare-histos
==============================================================================
--- trunk/bin/compare-histos	Wed Oct 21 08:00:54 2009	(r1943)
+++ trunk/bin/compare-histos	Wed Oct 21 10:40:59 2009	(r1944)
@@ -21,150 +21,7 @@
     s = s.replace('%','\\%')
     return s
 
-
-##############################################################
-## HISTO HACKS UNTIL YODA ARRIVES...
-#from professor.rivetreader import *
-
-
-class Histo:
-    def __init__(self):
-        self._bins = []
-        self.path = None
-        self.name = None
-        self.title = None
-        self.xlabel = ''
-        self.ylabel = ''
-        self.isdata = False
-        #self.isref = False
-
-    def __cmp__(self, other):
-        """Sort by $path/$name string"""
-        return self.fullPath() > other.fullPath()
-
-    def __str__(self):
-        out = "Histogram '%s' with %d bins\n" % (self.fullPath(), self.numBins())
-        out += "Title: %s\n" % self.title
-        out += "XLabel: %s\n" % self.xlabel
-        out += "YLabel: %s\n" % self.ylabel
-        out += "\n".join([str(b) for b in self.getBins()])
-        return out
-
-    def fullPath(self):
-        return os.path.join(self.path, self.name)
-
-    def asFlat(self):
-        global headerprefix
-        global opts
-        out = "# BEGIN HISTOGRAM %s\n" % self.fullPath()
-        out += headerprefix + "AidaPath=%s\n" % self.fullPath()
-        out += headerprefix + "Title=%s\n" % self.title
-        out += headerprefix + "XLabel=%s\n" % self.xlabel
-        out += headerprefix + "YLabel=%s\n" % self.ylabel
-        out += "## Area: %s\n" % self.area()
-        out += "## Num bins: %d\n" % self.numBins()
-        if opts.GNUPLOT:
-            out += "## xval  \tyval    \txlow    \txhigh    \tylow     \tyhigh\n"
-        else:
-            out += "## xlow  \txhigh   \tyval    \tyerrminus\tyerrplus\n"
-        out += "\n".join([b.asFlat() for b in self.getBins()])
-        out += "\n# END HISTOGRAM"
-        return out
-
-    def numBins(self):
-        return len(self._bins)
-
-    def getBins(self):
-        return sorted(self._bins)
-
-    def setBins(self, bins):
-        self._bins = bins
-        return self
-
-    def addBin(self, bin):
-        self._bins.append(bin)
-        return self
-
-    def getBin(self, index):
-        self._bins.sort()
-        return self.getBins()[index]
-
-    bins = property(getBins, setBins)
-
-    def area(self):
-        return sum([bin.area() for bin in self.bins])
-
-    def __iter__(self):
-        return iter(self.getBins())
-
-    def __len__(self):
-        return len(self._bins)
-
-    def __getitem__(self, index):
-        return self.getBin(index)
-
-
-class Bin:
-    """A simple container for a binned value with an error."""
-    def __init__(self, xlow=None, xhigh=None, yval=0, yerrplus=0, yerrminus=0, focus=None):
-        self.xlow = xlow
-        self.xhigh= xhigh
-        self.yval = yval
-        self.yerrplus = yerrplus
-        self.yerrminus = yerrminus
-        self.focus= focus
-
-    def __str__(self):
-        out = "%e to %e: %e +- %e" % (self._xlow, self._xhigh, self._yval, self._yerr)
-        return out
-
-    def asFlat(self):
-        global opts
-        if opts.GNUPLOT:
-            out = "%e\t%e\t%e\t%e\t%e\t%e" % (self.getBinCenter(), self.yval,
-                                              self.xlow, self.xhigh, 
-                                              self.yval-self.yerrminus, self.yval+self.yerrplus)
-        else:
-            out = "%e\t%e\t%e\t%e\t%e" % (self.xlow, self.xhigh, self.yval, self.yerrminus, self.yerrplus)
-        return out
-
-    def __cmp__(self, other):
-        """Sort by mean x value (yeah, I know...)"""
-        return (self.xlow + self.xhigh) > (other.xlow + other.xhigh)
-
-    def getXRange(self):
-        return (self.xlow, self.xhigh)
-
-    def setXRange(self, xlow, xhigh):
-        self.xlow = xlow
-        self.xhigh = xhigh
-        return self
-
-    def getBinCenter(self):
-        """Geometric middle of the bin range."""
-        return self.xlow + .5*(self.xhigh - self.xlow)
-
-    def getFocus(self):
-        """Mean x-value of the bin."""
-        if self.focus is not None:
-            return (self.xlow + self.xhigh)/2.0
-        else:
-            return self.focus
-
-    def area(self):
-        return self.yval * (self.xhigh - self.xlow)
-
-    def getYErr(self):
-        """Get mean of +ve and -ve y-errors."""
-        return (self.yerrplus + self.yerrminus)/2.0
-
-    def setYErr(self, yerr):
-        """Set both +ve and -ve y-errors simultaneously."""
-        self.yerrplus = yerr
-        self.yerrminus = yerr
-        return self
-
-    yerr = property(getYErr, setYErr)
+from lighthisto import Histo
 
 
 ## Try to load faster but non-standard cElementTree module
@@ -179,41 +36,6 @@
         except:
             sys.stderr.write("Can't load the ElementTree XML parser: please install it!\n")
             sys.exit(1)
-
-
-def mkHistoFromDPS(dps):
-    """Make a mini histo representation from an AIDA dataPointSet tag."""
-    myhist = Histo()
-    myhist.name = dps.get("name")
-    myhist.title = dps.get("title")
-    myhist.path = dps.get("path")
-    axes = dps.findall("dimension")
-    if (len(axes)==2):
-        for a in axes:
-            if (a.get("dim")=="0"):
-                myhist.xlabel = a.get("title")
-            elif (a.get("dim")=="1"):
-                myhist.ylabel = a.get("title")
-    points = dps.findall("dataPoint")
-    numbins = len(points)
-    for binnum, point in enumerate(points):
-        bin = Bin()
-        for d, m in enumerate(point.findall("measurement")):
-            val  = float(m.get("value"))
-            down = float(m.get("errorMinus"))
-            up = float(m.get("errorPlus"))
-            if d == 0:
-                low  = val - down
-                high = val + up
-                bin.setXRange(low, high)
-            elif d == 1:
-                bin.yval = val
-                bin.yerrplus = up
-                bin.yerrminus = down
-        myhist.addBin(bin)
-    return myhist
-
-
 ## regex patterns ##
 import re
 pat_begin_block = re.compile('^# BEGIN ([A-Z0-9_]+) ?(\S+)?')
@@ -350,7 +172,7 @@
             ## Get this histogram's path name
             dpsname = os.path.join(dps.get("path"), dps.get("name"))
             ## Is it a data histo?
-            h = mkHistoFromDPS(dps)
+            h = Histo.fromDPS(dps)
             h.isdata = dpsname.upper().startswith("/REF")
             if h.isdata:
                 dpsname = dpsname.replace("/REF", "")


More information about the Rivet-svn mailing list