[yoda-svn] r383 - in trunk: include/YODA src tests/Histo2D

blackhole at projects.hepforge.org blackhole at projects.hepforge.org
Thu Sep 1 16:41:02 BST 2011


Author: mkawalec
Date: Thu Sep  1 16:41:02 2011
New Revision: 383

Log:
Added the merge function in Histo2D (still has an error that prevents it from working properly, but I think it is good to make its code public as it is).

Modified:
   trunk/include/YODA/Axis2D.h
   trunk/include/YODA/Histo2D.h
   trunk/src/Histo2D.cc
   trunk/tests/Histo2D/H2DCreate.cc
   trunk/tests/Histo2D/H2DModify.cc

Modified: trunk/include/YODA/Axis2D.h
==============================================================================
--- trunk/include/YODA/Axis2D.h	Wed Aug 31 19:14:33 2011	(r382)
+++ trunk/include/YODA/Axis2D.h	Thu Sep  1 16:41:02 2011	(r383)
@@ -252,6 +252,8 @@
         throw RangeError("The start bin has a greater x value than the end bin.");
       }
       if (start.midpoint().second > end.midpoint().second) {
+        std::cout << "Start: " << start.midpoint().second;
+        std::cout << " end: " << end.midpoint().second << std::endl;
         throw RangeError("The start bin has a greater y value than the end bin.");
       }
 
@@ -274,7 +276,7 @@
                fuzzyEquals(_binHashSparse.first[y].second[x].second.first, start.xMin())) &&
               (_binHashSparse.first[y].second[x].second.second < end.xMax() ||
                fuzzyEquals(_binHashSparse.first[y].second[x].second.second, end.xMax())))&&
-               std::find(toRemove.begin(), toRemove.end(), _binHashSparse.first[y].second[x].first) != toRemove.end())
+               !(std::find(toRemove.begin(), toRemove.end(), _binHashSparse.first[y].second[x].first) != toRemove.end()))
             {
               /// Merge it with the temp bin and mark it as ready for removal
               temp += bin(_binHashSparse.first[y].second[x].first);
@@ -282,13 +284,14 @@
             }
         }
       }
+
       /// Now, drop the bins to be dropped
       /// Keeping in mind that the bins must be removed from the highest index
       /// down, otherwise we will end up removing other bins that we intend to
       std::sort(toRemove.begin(), toRemove.end());
       std::reverse(toRemove.begin(), toRemove.end());
       foreach(size_t remove, toRemove) eraseBin(remove);
-
+      
       /// Add edges of our merged bin to _binHashSparse and don't create a default
       /// empty bin.
       _addEdge(temp.edges(), _binHashSparse, false);
@@ -296,6 +299,8 @@
       /// Add the actual merged bin to the Axis.
       _bins.push_back(temp);
 
+      
+
       /// And regenerate the caches on _binHashSparse
       _binHashSparse.first.regenCache();
       _binHashSparse.second.regenCache();
@@ -308,11 +313,37 @@
 
     /// Rebin by an interger factor
     void rebin(size_t factorX, size_t factorY) {
-      if (!isGrid) throw GridError("Rebinning by a factor can only act on full grids!");
-      size_t binsInColumn = _binHashSparse.first.size() -  1;
-      size_t binsInRow    = _binHashSparse.second.size() - 1;
+      if (!isGrid()) throw GridError("Rebinning by a factor can only act on full grids!");
+      if(factorX < 1 || factorY < 1) throw RangeError("Factors cannot be smaller than unity!");
+      
+      size_t binsInColumn = _binHashSparse.first.size() - 1;
+
+      std::cout << std::endl << "Bins in column: " << binsInColumn << std::endl;
+      std::cout <<  "Number of bins: " << _bins.size() << std::endl;
+      size_t startIndex = 0;
+      while(true) {
+        size_t endIndex = startIndex;
+        for(size_t i = 1; i < factorY; ++i){ 
+          if(_hasAbove(endIndex) == 1) endIndex++;
+          else break;
+          std::cout << "End index1: " << endIndex << std::endl;
+        }
+        binsInColumn -= endIndex - startIndex;
+        for(size_t i = 1; i < factorX; ++i){
+          if(endIndex + binsInColumn < _bins.size()) endIndex += binsInColumn;
+          else break;
+          std::cout << "End index2: " << endIndex << std::endl;
+        }
+        if(endIndex + 1 >= _bins.size()) break;
+        std::cout << "Start index: " << startIndex << ", end index: " << _bins.size() << std::endl;
+        mergeBins(startIndex, endIndex);
+        if(startIndex + 1 < _bins.size()) startIndex++;
+        else break;
+
+        if(_hasAbove(startIndex-1) == 0) binsInColumn = _binHashSparse.first.size() -1;
+      }
+      std::cout << "Number of bins: " << _bins.size() << std::endl;
 
-      throw ("IMPLEMENT!");
     }
 
     /// Reset the axis statistics
@@ -666,6 +697,16 @@
 
   private:
 
+    int _hasAbove(size_t index){
+      if(index == _bins.size() - 1) return -1;
+      if(fuzzyEquals(_bins[index].yMax(), _bins[index+1].yMin()) &&
+         fuzzyEquals(_bins[index].xMin(), _bins[index+1].xMin()) &&
+         fuzzyEquals(_bins[index].xMax(), _bins[index+1].xMax())) return 1;
+      if(fuzzyEquals(_bins[index].xMax(), _bins[index+1].xMin())) return 0;
+      return 1500;
+    }
+
+
     /// @brief Outflows pre-setter
     /// Sets the correct number of bins in each of the outflows.
     void _setOutflows() {

Modified: trunk/include/YODA/Histo2D.h
==============================================================================
--- trunk/include/YODA/Histo2D.h	Wed Aug 31 19:14:33 2011	(r382)
+++ trunk/include/YODA/Histo2D.h	Thu Sep  1 16:41:02 2011	(r383)
@@ -63,7 +63,7 @@
 
 
     /// Copy constructor with optional new path
-    Histo2D(const Histo2D& h, const std::string& path="");
+    Histo2D(const Histo2D& h){}
       //: AnalysisObject("Histo2D", (path.size() == 0) ? h.path() : path, h, h.title())
    // {
       //_axis = h._axis;
@@ -131,6 +131,12 @@
     void eraseBin(size_t index) {
       _axis.eraseBin(index);
     }
+    
+    /// Rebin the whole histo by a @a factorX in the X direction and
+    /// @a factorY in the Y direction
+    void rebin(size_t factorX, size_t factorY){
+      _axis.rebin(factorX, factorY);
+    }
 
     //@}
 

Modified: trunk/src/Histo2D.cc
==============================================================================
--- trunk/src/Histo2D.cc	Wed Aug 31 19:14:33 2011	(r382)
+++ trunk/src/Histo2D.cc	Thu Sep  1 16:41:02 2011	(r383)
@@ -12,9 +12,6 @@
 namespace YODA {
 
 
-  /// A copy constructor with optional new path
-  Histo2D::Histo2D(const Histo2D& h, const std::string& path)
-  { }
 
   int Histo2D::fill(double x, double y, double weight) {
     return _axis.fill(x, y, weight);

Modified: trunk/tests/Histo2D/H2DCreate.cc
==============================================================================
--- trunk/tests/Histo2D/H2DCreate.cc	Wed Aug 31 19:14:33 2011	(r382)
+++ trunk/tests/Histo2D/H2DCreate.cc	Thu Sep  1 16:41:02 2011	(r383)
@@ -14,7 +14,7 @@
   second.fill(1,1,1);
   cout << "PASS" << endl;
 
-  /*cout << "Testing the copy constructor:            ";
+  cout << "Testing the copy constructor:            ";
   Histo2D copyTest(first);
   cout << "PASS" << endl;
 
@@ -23,6 +23,6 @@
   Histo2D subtracted(first-second);
   Scatter3D divided(first/second);
   cout << "PASS" << endl;
-*/
+
   return EXIT_SUCCESS;
 }

Modified: trunk/tests/Histo2D/H2DModify.cc
==============================================================================
--- trunk/tests/Histo2D/H2DModify.cc	Wed Aug 31 19:14:33 2011	(r382)
+++ trunk/tests/Histo2D/H2DModify.cc	Thu Sep  1 16:41:02 2011	(r383)
@@ -53,6 +53,11 @@
     }
     cout << "PASS" << endl;
 
+    cout << "Does rebinning work?                     ";
+    Histo2D h2(20, 0, 100, 20, 0, 100);
+    h2.rebin(2,3);
+    cout << "PASS" << endl;
+
     // Now, adding a square that is in a non-overlapping location:
     cout << "Does bin addition work?                  ";
     beforeAdd = h.numBins();


More information about the yoda-svn mailing list