[yoda-svn] r331 - in trunk: . include/YODA include/YODA/Utils tests

blackhole at projects.hepforge.org blackhole at projects.hepforge.org
Tue Aug 23 10:33:01 BST 2011


Author: buckley
Date: Tue Aug 23 10:33:01 2011
New Revision: 331

Log:
Adding rebinning interface to Histo1D and Profile1D, and adding a test (and a new test feature for output message formatting). Profile dbn merging does not look quite right to me, though... which is weird since the same code is doing the HistoBin1D merging, and that looks good. Hmm.

Added:
   trunk/include/YODA/Utils/Formatting.h
      - copied unchanged from r330, trunk/tests/Formatting.h
Deleted:
   trunk/tests/Formatting.h
Modified:
   trunk/ChangeLog
   trunk/TODO
   trunk/include/YODA/Axis1D.h
   trunk/include/YODA/Bin1D.h
   trunk/include/YODA/Histo1D.h
   trunk/include/YODA/Makefile.am
   trunk/include/YODA/Profile1D.h
   trunk/tests/TestHisto1Da.cc
   trunk/tests/TestProfile1Da.cc

Modified: trunk/ChangeLog
==============================================================================
--- trunk/ChangeLog	Tue Aug 23 09:51:12 2011	(r330)
+++ trunk/ChangeLog	Tue Aug 23 10:33:01 2011	(r331)
@@ -1,5 +1,8 @@
 2011-08-23  Andy Buckley  <andy at insectnation.org>
 
+	* Adding rebinning interface to Histo1D and Profile1D, and adding
+	a test (and a new test feature for output message formatting)
+
 	* Adding first implementation of 1D bin merging to Axis1D.
 
 2011-08-22  Andy Buckley  <andy at insectnation.org>

Modified: trunk/TODO
==============================================================================
--- trunk/TODO	Tue Aug 23 09:51:12 2011	(r330)
+++ trunk/TODO	Tue Aug 23 10:33:01 2011	(r331)
@@ -6,9 +6,6 @@
 * Bin division: add quadratic / binomial error treatment enum option (AB)
   AB: done... needs to be checked. Add a test that y +- 1 sigma in [0, 1]
 
-* Rebinning in 1D: merges of n adjacent bins (AB)
-  AB: Done on Axis1D... add interface to Histo1D and Profile1D.
-
 * Add copy assignment to Point/Scatter3D and Dbn1D/2D.
 
 * Add bin removal to Axis2D: should allow removal of the "ghost" mechanism and

Modified: trunk/include/YODA/Axis1D.h
==============================================================================
--- trunk/include/YODA/Axis1D.h	Tue Aug 23 09:51:12 2011	(r330)
+++ trunk/include/YODA/Axis1D.h	Tue Aug 23 10:33:01 2011	(r331)
@@ -203,17 +203,6 @@
     }
 
 
-    /// Merge every group of n bins, starting from the LHS
-    void rebin(int n) {
-      int m = 0;
-      while (m < _bins.size()) {
-        size_t end = (m + n - 1 < _bins.size()) ? m + n -1 : _bins.size() - 1;
-        mergeBins(m, end);
-        m += 1;
-      }
-    }
-
-
     /// Merge together the bin range with indices from @a from to @a to, inclusive
     void mergeBins(size_t from, size_t to) {
       // Correctness checking
@@ -221,13 +210,27 @@
       if (to < 0 || to >= numBins()) throw ("Second index is out of range!");
       if (_bins[from].xMin() > _bins[to].xMin()) throw RangeError("The starting bin is greater than ending bin!");
       BIN1D& b = _bins[from];
+      // std::cout << "a " << b.focus() << std::endl;
       for (size_t i = from+1; i <= to; ++i) {
         b.merge(_bins[i]);
       }
+      // std::cout << "b " << b.focus() << std::endl;
       eraseBins(from+1, to);
     }
 
 
+    /// Merge every group of n bins, starting from the LHS
+    void rebin(int n) {
+      size_t m = 0;
+      while (m < _bins.size()) {
+        const size_t end = (m + n - 1 < _bins.size()) ? m + n -1 : _bins.size() - 1;
+        //std::cout << m << ", " << end << ", " << _bins.size() << std::endl;
+        if (end > m) mergeBins(m, end);
+        m += 1;
+      }
+    }
+
+
     /// Add a bin, providing its low and high edge
     void addBin(double from, double to) {
       std::vector<double> binedges;

Modified: trunk/include/YODA/Bin1D.h
==============================================================================
--- trunk/include/YODA/Bin1D.h	Tue Aug 23 09:51:12 2011	(r330)
+++ trunk/include/YODA/Bin1D.h	Tue Aug 23 10:33:01 2011	(r331)
@@ -9,6 +9,7 @@
 #include "YODA/Bin.h"
 #include "YODA/Dbn1D.h"
 #include <string>
+#include <iostream>
 #include <utility>
 #include <cassert>
 
@@ -245,7 +246,9 @@
       } else {
         throw LogicError("Attempted to merge two non-adjacent bins");
       }
+      // std::cout << "a " << _dbn.sumW() << std::endl;
       _dbn += b._dbn;
+      // std::cout << "b " << _dbn.sumW() << std::endl;
       return *this;
     }
 

Modified: trunk/include/YODA/Histo1D.h
==============================================================================
--- trunk/include/YODA/Histo1D.h	Tue Aug 23 09:51:12 2011	(r330)
+++ trunk/include/YODA/Histo1D.h	Tue Aug 23 10:33:01 2011	(r331)
@@ -97,17 +97,32 @@
     /// Fill histo by value and weight
     void fill(double x, double weight=1.0);
 
+
     /// @brief Reset the histogram.
+    ///
     /// Keep the binning but set all bin contents and related quantities to zero
     virtual void reset() {
       _axis.reset();
     }
 
+
     /// Rescale as if all fill weights had been different by factor @a scalefactor.
     void scaleW(double scalefactor) {
       _axis.scaleW(scalefactor);
     }
 
+
+    /// Merge together the bin range with indices from @a from to @a to, inclusive
+    void mergeBins(size_t from, size_t to) {
+      _axis.mergeBins(from, to);
+    }
+
+
+    /// Merge every group of n bins, starting from the LHS
+    void rebin(int n) {
+      _axis.rebin(n);
+    }
+
     //@}
 
 

Modified: trunk/include/YODA/Makefile.am
==============================================================================
--- trunk/include/YODA/Makefile.am	Tue Aug 23 09:51:12 2011	(r330)
+++ trunk/include/YODA/Makefile.am	Tue Aug 23 10:33:01 2011	(r331)
@@ -15,6 +15,7 @@
 nobase_pkginclude_HEADERS = \
 	WriterMethods.icc \
 	Utils/MathUtils.h \
+	Utils/Formatting.h \
 	Utils/sortedvector.h \
     Utils/cachedvector.h \
 	Utils/nvector.h \

Modified: trunk/include/YODA/Profile1D.h
==============================================================================
--- trunk/include/YODA/Profile1D.h	Tue Aug 23 09:51:12 2011	(r330)
+++ trunk/include/YODA/Profile1D.h	Tue Aug 23 10:33:01 2011	(r331)
@@ -112,17 +112,32 @@
     /// Fill histo by value and weight
     void fill(double x, double y, double weight=1.0);
 
+
     /// @brief Reset the histogram
+    ///
     /// Keep the binning but set all bin contents and related quantities to zero
     void reset() {
       _axis.reset();
     }
 
+
     /// Rescale as if all fill weights had been different by factor @a scalefactor.
     void scaleW(double scalefactor) {
       _axis.scaleW(scalefactor);
     }
 
+
+    /// Merge together the bin range with indices from @a from to @a to, inclusive
+    void mergeBins(size_t from, size_t to) {
+      _axis.mergeBins(from, to);
+    }
+
+
+    /// Merge every group of n bins, starting from the LHS
+    void rebin(int n) {
+      _axis.rebin(n);
+    }
+
     //@}
 
 
@@ -134,6 +149,7 @@
       return bins().size();
     }
 
+
     /// Access the bin vector
     std::vector<YODA::ProfileBin1D>& bins() {
       return _axis.bins();
@@ -144,6 +160,7 @@
       return _axis.bins();
     }
 
+
     /// Access a bin by index (non-const version)
     ProfileBin1D& bin(size_t index) {
       return _axis.bins()[index];
@@ -154,6 +171,7 @@
       return _axis.bins()[index];
     }
 
+
     /// Access a bin by x-coordinate.
     ProfileBin1D& binByCoord(double x) {
       return _axis.binByCoord(x);
@@ -164,6 +182,7 @@
       return _axis.binByCoord(x);
     }
 
+
     /// Access underflow (non-const version)
     Dbn2D& underflow() {
       return _axis.underflow();
@@ -174,6 +193,7 @@
       return _axis.underflow();
     }
 
+
     /// Access overflow (non-const version)
     Dbn2D& overflow() {
       return _axis.overflow();

Copied: trunk/include/YODA/Utils/Formatting.h (from r330, trunk/tests/Formatting.h)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/include/YODA/Utils/Formatting.h	Tue Aug 23 10:33:01 2011	(r331, copy of r330, trunk/tests/Formatting.h)
@@ -0,0 +1,29 @@
+#ifndef YODA_FORMATTING_H
+#define YODA_FORMATTING_H
+
+#include <iostream>
+#include <unistd.h>
+
+
+#define MSG(msg)                                \
+  do {                                          \
+    std::cout << msg  << std::endl;             \
+  } while (0)
+
+
+#define COLOR_(msg, code)                                           \
+  (isatty(1) ? code : "") << msg << (isatty(1) ? "\033[0m" : "")
+
+
+#define RED(msg)     COLOR_(msg, "\033[0;31m")
+#define GREEN(msg)   COLOR_(msg, "\033[0;32m")
+#define YELLOW(msg)  COLOR_(msg, "\033[0;33m")
+#define BLUE(msg)    COLOR_(msg, "\033[0;34m")
+
+#define MSG_RED(x)     MSG_(RED(x))
+#define MSG_GREEN(x)   MSG_(GREEN(x))
+#define MSG_YELLOW(x)  MSG_(YELLOW(x))
+#define MSG_BLUE(x)    MSG_(BLUE(x))
+
+
+#endif

Modified: trunk/tests/TestHisto1Da.cc
==============================================================================
--- trunk/tests/TestHisto1Da.cc	Tue Aug 23 09:51:12 2011	(r330)
+++ trunk/tests/TestHisto1Da.cc	Tue Aug 23 10:33:01 2011	(r331)
@@ -1,5 +1,5 @@
 #include "YODA/Histo1D.h"
-#include "Formatting.h"
+#include "YODA/Utils/Formatting.h"
 #include <cmath>
 #include <iostream>
 
@@ -16,20 +16,22 @@
   Histo1D h(20, 0.0, 1.0);
   for (size_t n = 0; n < 1000; ++n) {
     const double num = rand()/static_cast<double>(RAND_MAX);
-    //cout << "Filling with " << num << endl;
     h.fill(num);
   }
 
-  cout << "Mean value = " << h.mean() << " +- " << h.stdDev() << endl;
-  cout << "Total area = " << h.integral() << endl;
+  MSG("Mean value = " << h.mean() << " +- " << h.stdDev());
+  MSG("Total area = " << h.integral());
 
   const HistoBin1D& highestBin = *( max_element(h.bins().begin(), h.bins().end(), compareHeight) );
   const double maxHeight = highestBin.height();
 
-  cout << "Histo:" << endl;
-  for (vector<HistoBin1D>::const_iterator b = h.bins().begin(); b != h.bins().end(); ++b) {
-    const int numElements = static_cast<int>(round(20 * b->height()/maxHeight));
-    MSG(string().insert(0, numElements, '=') << "  " << RED(b->height()));
+  for (int i = 0; i < 4; ++i) {
+    if (i > 0) h.rebin(2);
+    MSG("Histo (rebinning #" << i << ", num bins = " << h.numBins() << ")");
+    foreach (const HistoBin1D& b, h.bins()) {
+      const int numElements = static_cast<int>(round(20 * b.height()/maxHeight));
+      MSG(string().insert(0, numElements, '=') << "  " << RED(b.height()));
+    }
   }
 
   return EXIT_SUCCESS;

Modified: trunk/tests/TestProfile1Da.cc
==============================================================================
--- trunk/tests/TestProfile1Da.cc	Tue Aug 23 09:51:12 2011	(r330)
+++ trunk/tests/TestProfile1Da.cc	Tue Aug 23 10:33:01 2011	(r331)
@@ -1,4 +1,5 @@
 #include "YODA/Profile1D.h"
+#include "YODA/Utils/Formatting.h"
 #include <cmath>
 #include <iostream>
 #include <unistd.h>
@@ -16,9 +17,13 @@
     h.fill(x, y, 2);
   }
 
-  const vector<ProfileBin1D>& bins = h.bins();
-  for (vector<ProfileBin1D>::const_iterator b = bins.begin(); b != bins.end(); ++b) {
-    cout << b->mean() << ", " << b->stdDev() << ", " << b->stdErr() << endl;
+  for (int i = 0; i < 4; ++i) {
+    if (i > 0) h.rebin(2);
+    MSG("Profile (rebinning #" << i << ", num bins = " << h.numBins() << ")");
+    foreach (const ProfileBin1D& b, h.bins()) {
+      MSG(b.xMin() << "-" << b.xMax() << ": "
+          << RED(b.mean()) << ", " << BLUE(b.stdDev()) << ", " << RED(b.stdErr()));
+    }
   }
 
   return EXIT_SUCCESS;


More information about the yoda-svn mailing list