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

blackhole at projects.hepforge.org blackhole at projects.hepforge.org
Thu Jul 12 18:23:47 BST 2012


Author: buckley
Date: Thu Jul 12 18:23:47 2012
New Revision: 499

Log:
Adding axis locking to Axis2D.

Modified:
   trunk/ChangeLog
   trunk/include/YODA/Axis1D.h
   trunk/include/YODA/Axis2D.h
   trunk/src/Histo2D.cc
   trunk/src/WriterYODA.cc

Modified: trunk/ChangeLog
==============================================================================
--- trunk/ChangeLog	Thu Jul 12 18:08:44 2012	(r498)
+++ trunk/ChangeLog	Thu Jul 12 18:23:47 2012	(r499)
@@ -1,5 +1,7 @@
 2012-07-12  Andy Buckley  <andy.buckley at cern.ch>
 
+	* Adding axis locking to Axis2D.
+
 	* Supporting Histo2D in WriterYODA.
 
 2012-07-02  Andy Buckley  <andy.buckley at cern.ch>

Modified: trunk/include/YODA/Axis1D.h
==============================================================================
--- trunk/include/YODA/Axis1D.h	Thu Jul 12 18:08:44 2012	(r498)
+++ trunk/include/YODA/Axis1D.h	Thu Jul 12 18:23:47 2012	(r499)
@@ -48,7 +48,6 @@
     Axis1D(const std::vector<double>& binedges)
       : _locked(false)
     {
-      _locked = false;
       _addBins(binedges);
     }
 
@@ -198,7 +197,7 @@
       _dbn.reset();
       _underflow.reset();
       _overflow.reset();
-      for (size_t i = 0; i < _bins.size(); ++i) _bins[i].reset();
+      foreach(Bin& bin, _bins) bin.reset();
       _locked = false;
     }
 
@@ -359,6 +358,9 @@
 
     /// Add new bins to the axis
     void _addBins(const Bins& bins) {
+      if (_locked) {
+        throw LockError("Attempting to add bins to a locked axis");
+      }
       for (size_t i = 0; i < bins.size(); ++i) {
         if (_edgeInRange(bins[i].xMin(), bins[i].xMax())) {
           throw RangeError("New bin range overlaps with existing bin edges");

Modified: trunk/include/YODA/Axis2D.h
==============================================================================
--- trunk/include/YODA/Axis2D.h	Thu Jul 12 18:08:44 2012	(r498)
+++ trunk/include/YODA/Axis2D.h	Thu Jul 12 18:23:47 2012	(r499)
@@ -54,11 +54,14 @@
 
     /// Empty constructor
     Axis2D()
+      : _isPerfectGrid(true), _locked(false)
     { }
 
 
     /// A constructor with specified x and y axis bin limits.
-    Axis2D(const std::vector<double>& xedges, const std::vector<double>& yedges) {
+    Axis2D(const std::vector<double>& xedges, const std::vector<double>& yedges)
+      : _isPerfectGrid(true), _locked(false)
+    {
       _addBins(xedges, yedges);
     }
 
@@ -66,7 +69,9 @@
     /// Most standard constructor accepting X/Y ranges and number of bins
     /// on each of the axis. Both axes are divided linearly.
     Axis2D(size_t nbinsX, const std::pair<double,double>& rangeX,
-           size_t nbinsY, const std::pair<double,double>& rangeY) {
+           size_t nbinsY, const std::pair<double,double>& rangeY)
+      : _isPerfectGrid(true), _locked(false)
+    {
       _addBins(linspace(rangeX.first, rangeX.second, nbinsX),
                linspace(rangeY.first, rangeY.second, nbinsY));
     }
@@ -82,7 +87,8 @@
     Axis2D(const Bins& bins,
            const DBN& totalDbn,
            const Outflows& outflows)
-      : _bins(bins), _dbn(totalDbn), _outflows(outflows)
+      : _bins(bins), _dbn(totalDbn), _outflows(outflows),
+        _isPerfectGrid(true), _locked(false)
     {
       if (_outflows.size() != 8) {
         throw Exception("Axis2D outflow containers must have exactly 8 elements");
@@ -104,7 +110,7 @@
     void addBin(double lowX, double lowY, double highX, double highY) {
       /// @todo TODO
 
-      /// @todo Check for overlaps
+      /// @todo Check for overlaps and whether axis is locked
     }
 
     //@}
@@ -226,9 +232,19 @@
     /// Reset the axis statistics
     void reset() {
       _dbn.reset();
-      foreach(Bin bin, _bins) {
-        bin.reset();
+      for (size_t ix = -1; ix <= 1; ++ix) {
+        for (size_t iy = -1; ix <= 1; ++ix) {
+          outflow(ix, iy).reset();
+        }
       }
+      foreach(Bin& bin, _bins) bin.reset();
+      _locked = false;
+    }
+
+
+    /// Set the axis lock state
+    void _setLock(bool locked) {
+      _locked = locked;
     }
 
     //@}
@@ -520,6 +536,10 @@
 
     /// Add new bins, constructed from two sorted vectors of edges, to the axis
     void _addBins(const std::vector<double>& xbinedges, const std::vector<double>& ybinedges) {
+      if (_locked) {
+        throw LockError("Attempting to add bins to a locked axis");
+      }
+
       /// @todo Check that vectors are sorted?
 
       /// @todo Check whether there is overlap with any existing edges in either direction
@@ -540,6 +560,9 @@
 
     /// Add new bins to the axis
     void _addBins(const Bins& bins) {
+      if (_locked) {
+        throw LockError("Attempting to add bins to a locked axis");
+      }
       for (size_t i = 0; i < bins.size(); ++i) {
 
         /// @todo Check for 2D edge overlaps
@@ -573,6 +596,10 @@
       std::vector<double>::iterator ity = std::unique(yedges.begin(), yedges.end());
       yedges.resize(ity - yedges.begin());
 
+      // Guess that it's a perfect grid if (nxedge-1)*(nyedge-1) == nbins
+      /// @todo This is not an ideal check: it could be a numerical coincidence. Fix!
+      _isPerfectGrid = ( (xedges.size()-1)*(yedges.size()-1) == numBins());
+
       // Create a double-map hash based on the two sets of low edges. Initialize with null bin indices.
       _binhash.clear();
       for (size_t ix = 0; ix < xedges.size() - 1; ++ix) {
@@ -654,6 +681,12 @@
     /// Cached bin edges for searching
     BinHash _binhash;
 
+    /// Whether the binning exactly matches the hash edges
+    bool _isPerfectGrid;
+
+    /// Whether modifying bin edges is permitted
+    bool _locked;
+
     //@}
 
   };

Modified: trunk/src/Histo2D.cc
==============================================================================
--- trunk/src/Histo2D.cc	Thu Jul 12 18:08:44 2012	(r498)
+++ trunk/src/Histo2D.cc	Thu Jul 12 18:23:47 2012	(r499)
@@ -31,9 +31,8 @@
       if (y <  _axis.yMin()) iy = -1; else if (y >= _axis.yMax()) iy = 1;
       _axis.outflow(ix, iy).fill(x, y, weight);
     }
-    /// @todo Re-enable
-    // // Lock the axis now that a fill has happened
-    // _axis._setLock(true);
+    // Lock the axis now that a fill has happened
+    _axis._setLock(true);
   }
 
 

Modified: trunk/src/WriterYODA.cc
==============================================================================
--- trunk/src/WriterYODA.cc	Thu Jul 12 18:08:44 2012	(r498)
+++ trunk/src/WriterYODA.cc	Thu Jul 12 18:23:47 2012	(r499)
@@ -94,6 +94,7 @@
     os << h.totalDbn().numEntries() << "\n";
     for (size_t ix = -1; ix <= 1; ++ix) {
       for (size_t iy = -1; ix <= 1; ++ix) {
+        if (ix == 0 && iy == 0) continue;
         os << "Outflow\t" << ix << ":" << iy << "\t";
         const Dbn2D& d = h.outflow(ix, iy);
         os << d.sumW()  << "\t" << d.sumW2()  << "\t";


More information about the yoda-svn mailing list