[yoda-svn] r247 - in trunk: . include/YODA src

blackhole at projects.hepforge.org blackhole at projects.hepforge.org
Mon Aug 15 19:33:59 BST 2011


Author: buckley
Date: Mon Aug 15 19:33:59 2011
New Revision: 247

Log:
Inlining all functions in Bin1D. Note that the only reason to not completely inline Bin2D is that there are some loops over bin edges which look a bit heavy to put into the header. But I think this is more than anything an artefact of the implementation in terms of all the edge points, including fourfold duplicated of each x/y coord, being very inefficient: Michal, can you rework or justify this structure, please? If the (x,y) edge points are *really* needed internally, let's calculate them on the fly from the minimal collection of 4 x/y low/high doubles unless there is a very compelling reason to do otherwise. I nearly swallowed my tongue when I worked out what that vector of pairs of pairs was for!

Deleted:
   trunk/src/Bin1D.cc
Modified:
   trunk/TODO
   trunk/include/YODA/Bin1D.h
   trunk/include/YODA/Bin2D.h
   trunk/src/Bin2D.cc
   trunk/src/Makefile.am

Modified: trunk/TODO
==============================================================================
--- trunk/TODO	Mon Aug 15 19:15:05 2011	(r246)
+++ trunk/TODO	Mon Aug 15 19:33:59 2011	(r247)
@@ -25,8 +25,10 @@
 
 * Implement hierarchy-cascading state-setting constructor for Histo2D. (MICHAL)
 
-* Move micro-functions to be inline in the header: still needed for Bin1D.cc,
-  and probably some others. (AB)
+* Review implementation of Bin2D with storage of very redundant information --
+  why are we storing the points at the end of each edge separately, when this
+  means that we are storing each bin-defining x and y low/high value 4 times?
+  (MICHAL)
 
 * Use state-setting constructors for Histo and Profile persistency via the
   Reader classes. (HH)

Modified: trunk/include/YODA/Bin1D.h
==============================================================================
--- trunk/include/YODA/Bin1D.h	Mon Aug 15 19:15:05 2011	(r246)
+++ trunk/include/YODA/Bin1D.h	Mon Aug 15 19:33:59 2011	(r247)
@@ -10,6 +10,7 @@
 #include "YODA/Dbn1D.h"
 #include <string>
 #include <utility>
+#include <cassert>
 
 namespace YODA {
 
@@ -29,14 +30,29 @@
     //@{
 
     /// Init a new, empty bin with a pair of edges.
-    Bin1D(double lowedge, double highedge);
+    Bin1D(double lowedge, double highedge)
+      : _edges( std::make_pair(lowedge, highedge) )
+    {
+      assert( _edges.second > _edges.first );
+    }
+
 
     /// Init a new, empty bin with a pair of edges.
-    Bin1D(const std::pair<double,double>& edges);
+    Bin1D(const std::pair<double,double>& edges)
+      : _edges( edges )
+    {
+      assert( _edges.second >= _edges.first );
+    }
+
 
     /// @brief Init a bin with all the components of a fill history.
     /// Mainly intended for internal persistency use.
-    Bin1D(double lowedge, double highedge, const Dbn1D& dbnx);
+    Bin1D(double lowedge, double highedge, const Dbn1D& dbnx)
+      : _edges( std::make_pair(lowedge, highedge) ),
+        _xdbn(dbnx)
+    {
+      assert( _edges.second >= _edges.first );
+    }
 
     //@}
 
@@ -45,7 +61,9 @@
     //@{
 
     /// Reset this bin
-    virtual void reset();
+    virtual void reset() {
+      _xdbn.reset();
+    }
 
     /// Scale
     void scaleX(double factor) {
@@ -63,24 +81,46 @@
     //@{
 
     /// Lower limit of the bin (inclusive).
-    double lowEdge() const;
-    double xMin() const { return lowEdge(); }
+    double lowEdge() const {
+      return _edges.first;
+    }
+    /// Synonym for lowEdge
+    double xMin() const {
+      return lowEdge();
+    }
 
     /// Upper limit of the bin (exclusive).
-    double highEdge() const;
-    double xMax() const { return highEdge(); }
+    double highEdge() const {
+      return _edges.second;
+    }
+    /// Synonym for highEdge
+    double xMax() const {
+      return highEdge();
+    }
 
     /// Get the {low,high} edges as an STL @c pair.
-    std::pair<double,double> edges() const;
+    std::pair<double,double> edges() const {
+      return _edges;
+    }
 
     /// Separation of low and high edges, i.e. high-low.
-    double width() const;
+    double width() const {
+      return _edges.second - _edges.first;
+    }
 
     /// The mean position in the bin, or the midpoint if that is not available.
-    double focus() const;
+    double focus() const {
+      if (_xdbn.sumW() != 0) {
+        return xMean();
+      } else {
+        return midpoint();
+      }
+    }
 
     /// Geometric centre of the bin, i.e. high+low/2.0
-    double midpoint() const;
+    double midpoint() const {
+      return ( _edges.second + _edges.first ) / 2;
+    }
 
     //@}
 
@@ -91,16 +131,24 @@
     //@{
 
     /// Mean value of x-values in the bin.
-    double xMean() const;
+    double xMean() const {
+      return _xdbn.mean();
+    }
 
     /// The variance of x-values in the bin.
-    double xVariance() const;
+    double xVariance() const {
+      return _xdbn.variance();
+    }
 
     /// The standard deviation (spread) of x-values in the bin.
-    double xStdDev() const;
+    double xStdDev() const {
+      return _xdbn.stdDev();
+    }
 
     /// The standard error on the bin focus.
-    double xStdError() const;
+    double xStdError() const {
+      return _xdbn.stdErr();
+    }
 
     //@}
 
@@ -111,19 +159,29 @@
     //@{
 
     /// The number of entries
-    unsigned long numEntries() const;
+    unsigned long numEntries() const {
+      return _xdbn.numEntries();
+    }
 
     /// The sum of weights
-    double sumW() const;
+    double sumW() const {
+      return _xdbn.sumW();
+    }
 
     /// The sum of weights squared
-    double sumW2() const;
+    double sumW2() const {
+      return _xdbn.sumW2();
+    }
 
     /// The sum of x*weight
-    double sumWX() const;
+    double sumWX() const {
+      return _xdbn.sumWX();
+    }
 
     /// The sum of x^2 * weight
-    double sumWX2() const;
+    double sumWX2() const {
+      return _xdbn.sumWX2();
+    }
 
     //@}
 
@@ -135,10 +193,15 @@
     //@{
 
     /// Add two bins
-    Bin1D& operator += (const Bin1D&);
+    Bin1D& operator += (const Bin1D& b) {
+      return add(b);
+    }
 
     /// Subtract one bin from another
-    Bin1D& operator -= (const Bin1D&);
+    Bin1D& operator -= (const Bin1D& b) {
+      return subtract(b);
+    }
+
     //@}
 
 
@@ -148,10 +211,19 @@
     //@{
 
     /// Add two bins (internal, explicitly named version)
-    Bin1D& add(const Bin1D&);
+    Bin1D& add(const Bin1D& b) {
+      assert(_edges == b._edges);
+      _xdbn += b._xdbn;
+      return *this;
+    }
 
     /// Subtract one bin from another (internal, explicitly named version)
-    Bin1D& subtract(const Bin1D&);
+    Bin1D& subtract(const Bin1D& b) {
+      assert(_edges == b._edges);
+      _xdbn -= b._xdbn;
+      return *this;
+    }
+
     //@}
 
 
@@ -167,10 +239,19 @@
 
 
   /// Add two bins
-  Bin1D operator + (const Bin1D& a, const Bin1D& b);
+  inline Bin1D operator + (const Bin1D& a, const Bin1D& b) {
+    Bin1D rtn = a;
+    rtn += b;
+    return rtn;
+  }
+
 
   /// Subtract one bin from another
-  Bin1D operator - (const Bin1D& a, const Bin1D& b);
+  inline Bin1D operator - (const Bin1D& a, const Bin1D& b) {
+    Bin1D rtn = a;
+    rtn -= b;
+    return rtn;
+  }
 
 
   /// Bin1Ds are compared for axis sorting by lower edge position

Modified: trunk/include/YODA/Bin2D.h
==============================================================================
--- trunk/include/YODA/Bin2D.h	Mon Aug 15 19:15:05 2011	(r246)
+++ trunk/include/YODA/Bin2D.h	Mon Aug 15 19:33:59 2011	(r247)
@@ -224,7 +224,11 @@
 
   protected:
 
+
+    /// @todo Explain! And then replace it with something sane -- this is really wasteful.
     std::vector<std::pair<std::pair<double,double>,std::pair<double,double> > > _edges;
+
+    /// Distribution of fills in this bin.
     Dbn2D _dbn;
 
   };

Modified: trunk/src/Bin2D.cc
==============================================================================
--- trunk/src/Bin2D.cc	Mon Aug 15 19:15:05 2011	(r246)
+++ trunk/src/Bin2D.cc	Mon Aug 15 19:33:59 2011	(r247)
@@ -14,6 +14,7 @@
   Bin2D::Bin2D(double lowedgeX, double lowedgeY, double highedgeX, double highedgeY) {
     assert(lowedgeX <= highedgeX && lowedgeY <= highedgeY);
 
+    /// @todo Why store with so much redundancy?
     pair<pair<double,double>, pair<double,double> > edge1 =
       make_pair(make_pair(lowedgeX, lowedgeY), make_pair(lowedgeX, highedgeY));
     pair<pair<double,double>, pair<double,double> > edge2 =
@@ -23,21 +24,25 @@
     pair<pair<double,double>, pair<double,double> > edge4 =
       make_pair(make_pair(lowedgeX, lowedgeY), make_pair(highedgeX, lowedgeY));
 
-    _edges.push_back(edge1); _edges.push_back(edge2);
-    _edges.push_back(edge3); _edges.push_back(edge4);
+    _edges.push_back(edge1);
+    _edges.push_back(edge2);
+    _edges.push_back(edge3);
+    _edges.push_back(edge4);
   }
 
 
   Bin2D::Bin2D(std::vector<std::pair<std::pair<double,double>,
                std::pair<double,double> > > edges) {
     assert(edges.size() == 4);
-    _edges.push_back(edges[0]); _edges.push_back(edges[1]);
-    _edges.push_back(edges[2]); _edges.push_back(edges[3]);
+    _edges.push_back(edges[0]);
+    _edges.push_back(edges[1]);
+    _edges.push_back(edges[2]);
+    _edges.push_back(edges[3]);
   }
 
 
   void Bin2D::scaleXY(double scaleX, double scaleY) {
-    for (unsigned int i = 0; i < _edges.size(); ++i) {
+    for (size_t i = 0; i < _edges.size(); ++i) {
       _edges[i].first.first *= scaleX;
       _edges[i].second.first *= scaleX;
       _edges[i].first.second *= scaleY;

Modified: trunk/src/Makefile.am
==============================================================================
--- trunk/src/Makefile.am	Mon Aug 15 19:15:05 2011	(r246)
+++ trunk/src/Makefile.am	Mon Aug 15 19:33:59 2011	(r247)
@@ -4,7 +4,6 @@
 
 libYODA_la_SOURCES = \
     Dbn1D.cc \
-    Bin1D.cc \
     Bin2D.cc \
     Histo1D.cc \
     Histo2D.cc \


More information about the yoda-svn mailing list