|
[yoda-svn] r458 - in trunk: include/YODA include/YODA/Utils pyext/yoda/include src tests tests/Histo1D tests/Profile1Dblackhole at projects.hepforge.org blackhole at projects.hepforge.orgWed May 2 19:10:33 BST 2012
Author: buckley Date: Wed May 2 19:10:32 2012 New Revision: 458 Log: Histo1D and tests tweaks Modified: trunk/include/YODA/Histo1D.h trunk/include/YODA/Utils/Formatting.h trunk/pyext/yoda/include/40-Histo1D.pyx trunk/src/Histo1D.cc trunk/tests/Histo1D/H1DCreate.cc trunk/tests/Profile1D/P1DModify.cc trunk/tests/TestHisto1Da.cc Modified: trunk/include/YODA/Histo1D.h ============================================================================== --- trunk/include/YODA/Histo1D.h Wed May 2 17:43:26 2012 (r457) +++ trunk/include/YODA/Histo1D.h Wed May 2 19:10:32 2012 (r458) @@ -281,6 +281,8 @@ return rtn; } + + /// Get sum of weights in histo double sumW(bool includeoverflows=true) const; @@ -295,9 +297,13 @@ /// Get the standard deviation double stdDev(bool includeoverflows=true) const { + if (includeoverflows) return _axis.totalDbn().stdDev(); return std::sqrt(variance(includeoverflows)); } + /// Get the standard error + double stdErr(bool includeoverflows=true) const; + //@} Modified: trunk/include/YODA/Utils/Formatting.h ============================================================================== --- trunk/include/YODA/Utils/Formatting.h Wed May 2 17:43:26 2012 (r457) +++ trunk/include/YODA/Utils/Formatting.h Wed May 2 19:10:32 2012 (r458) @@ -5,25 +5,30 @@ #include <unistd.h> -#define MSG(msg) \ - do { \ - std::cout << msg << std::endl; \ - } while (0) +#define MSG_(msg) do { std::cout << msg; } while (0) +#define MSG(msg) MSG_(msg << std::endl) -#define COLOR_(msg, code) \ + +#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)) +#define RED(msg) COLOR_(msg, "\033[0;31m") +#define MSG_RED_(x) MSG_(RED(x)) +#define MSG_RED(x) MSG(RED(x)) + +#define GREEN(msg) COLOR_(msg, "\033[0;32m") +#define MSG_GREEN_(x) MSG_(GREEN(x)) +#define MSG_GREEN(x) MSG(GREEN(x)) + +#define YELLOW(msg) COLOR_(msg, "\033[0;33m") +#define MSG_YELLOW_(x) MSG_(YELLOW(x)) +#define MSG_YELLOW(x) MSG(YELLOW(x)) + +#define BLUE(msg) COLOR_(msg, "\033[0;34m") +#define MSG_BLUE_(x) MSG_(BLUE(x)) +#define MSG_BLUE(x) MSG(BLUE(x)) #endif Modified: trunk/pyext/yoda/include/40-Histo1D.pyx ============================================================================== --- trunk/pyext/yoda/include/40-Histo1D.pyx Wed May 2 17:43:26 2012 (r457) +++ trunk/pyext/yoda/include/40-Histo1D.pyx Wed May 2 19:10:32 2012 (r458) @@ -26,7 +26,8 @@ cDbn1D &totalDbn() cDbn1D &underflow() cDbn1D &overflow() - #addBin() + void addBin(double low, double high) + void addBins(vector[double] &binedges) void eraseBin(size_t index) # Statistical functions @@ -37,6 +38,7 @@ double mean(bool includeoverflows) double variance(bool includeoverflows) double stdDev(bool includeoverflows) + double stdErr(bool includeoverflows) cdef extern from "shims.h": @@ -58,6 +60,9 @@ char* path = '/' char* title = '' + + # TODO: Handle more parameters (esp. path and title), and handle list[double] arguments + if len(args) == 3: nbins, lower, upper = args[0], args[1], args[2] @@ -216,6 +221,16 @@ return Dbn1D_fromptr(&self.ptr().overflow()) + def addBin(self, low, high): + self.ptr().addBin(low, high) + + + def addBins(self, binedges): + # TODO: How to map the Python list to the C++ vector<double>? + #void addBins(vector[double] &binedges) + pass + + def __delitem__(self, size_t ix): self.ptr().eraseBin(ix) @@ -229,7 +244,7 @@ s.integral([overflows]) -> float Return the total area of the histogram. If overflows is False, ignore - over-and underflow bins. + over-and underflow bins and any bin gaps. """ return self.ptr().integral(overflows) @@ -240,7 +255,7 @@ s.sumW([overflows]) -> float Return the sum of weights of the histogram. If overflows is False, - ignore over-and underflow bins. + ignore over-and underflow bins and any bin gaps. """ @@ -252,7 +267,7 @@ s.sumW2([overflows]) -> float Return the sum of weights squared. If overflows is False, ignore - over-and underflow bins. + over-and underflow bins and any bin gaps. """ return self.ptr().sumW2(overflows) @@ -263,7 +278,7 @@ s.mean([overflows]) -> float Return the mean. If overflows is False, ignore the over- and underflow - bins. + bins and any bin gaps. """ return self.ptr().mean(overflows) @@ -274,7 +289,7 @@ s.variance([overflows]) -> float Return the variance. If overflows is False, ignore the over- and - underflow bins. + underflow bins and any bin gaps. """ return self.ptr().variance(overflows) @@ -284,13 +299,26 @@ """ s.stdDev([overflows]) -> float - Return the standard deviation. If overflows is False, ignore over-and - underflow bins. + Return the standard deviation. If overflows is False, ignore over- and + underflow bins and any bin gaps. """ return self.ptr().stdDev(overflows) + def stdErr(self, bool overflows=True): + """ + s.stdErr([overflows]) -> float + + Return the standard error. If overflows is False, ignore over- and + underflow bins and any bin gaps. + + """ + return self.ptr().stdErr(overflows) + + + + def __add__(Histo1D a, Histo1D b): cdef cHisto1D *res = new cHisto1D(add_Histo1D(a.ptr()[0], b.ptr()[0])) return Histo1D_fromptr(res, True) Modified: trunk/src/Histo1D.cc ============================================================================== --- trunk/src/Histo1D.cc Wed May 2 17:43:26 2012 (r457) +++ trunk/src/Histo1D.cc Wed May 2 19:10:32 2012 (r458) @@ -26,6 +26,10 @@ } + /// @todo Improve/centralise this statistical aggregation by exposing the Dbn1D/2D objects + /// in the bins and using their native += operators to do the aggregation. + + double Histo1D::sumW(bool includeoverflows) const { if (includeoverflows) return _axis.totalDbn().sumW(); double sumw = 0; @@ -35,6 +39,7 @@ return sumw; } + double Histo1D::sumW2(bool includeoverflows) const { if (includeoverflows) return _axis.totalDbn().sumW2(); double sumw2 = 0; @@ -49,9 +54,9 @@ if (includeoverflows) return _axis.totalDbn().mean(); double sumwx = 0; double sumw = 0; - for (size_t i = 0; i < bins().size(); i++) { - sumwx += bins().at(i).sumWX(); - sumw += bins().at(i).sumW(); + foreach (const Bin& b, bins()) { + sumwx += b.sumWX(); + sumw += b.sumW(); } return sumwx/sumw; } @@ -69,6 +74,13 @@ } + double Histo1D::stdErr(bool includeoverflows) const { + if (includeoverflows) return _axis.totalDbn().stdErr(); + const double effNumEntries = sumW(false)*sumW(false)/sumW2(false); + return std::sqrt(variance(false) / effNumEntries); + } + + //////////////////////////////////////// Modified: trunk/tests/Histo1D/H1DCreate.cc ============================================================================== --- trunk/tests/Histo1D/H1DCreate.cc Wed May 2 17:43:26 2012 (r457) +++ trunk/tests/Histo1D/H1DCreate.cc Wed May 2 19:10:32 2012 (r458) @@ -1,7 +1,5 @@ #include "YODA/Histo1D.h" -#include "YODA/Utils/MathUtils.h" #include "YODA/Utils/Formatting.h" -#include <cmath> using namespace YODA; using namespace std; @@ -10,7 +8,7 @@ int main() { MSG_BLUE("Testing Histo1D constructors: "); - MSG("The most basic, linear constructor:"); + MSG_("The most basic, linear constructor:"); Histo1D h(100, 0, 100); if (h.numBins() != 100) { MSG_RED("FAIL: Wrong number of bins was created!"); @@ -31,7 +29,7 @@ MSG_GREEN("PASS"); - MSG("Explicit bin edges constructor: "); + MSG_("Explicit bin edges constructor: "); vector<double> edges; for (int i = 0; i < 101; ++i) edges.push_back(i); Histo1D h1(edges); @@ -53,7 +51,7 @@ } MSG_GREEN("PASS"); - MSG("Copy constructor: "); + MSG_("Copy constructor: "); Histo1D h2(h); if (h2.numBins() != 100) { MSG_RED("FAIL: Wrong number of bins was created!"); Modified: trunk/tests/Profile1D/P1DModify.cc ============================================================================== --- trunk/tests/Profile1D/P1DModify.cc Wed May 2 17:43:26 2012 (r457) +++ trunk/tests/Profile1D/P1DModify.cc Wed May 2 19:10:32 2012 (r458) @@ -1,82 +1,86 @@ #include "YODA/Profile1D.h" +#include "YODA/Utils/Formatting.h" -#include <iostream> -using namespace std; using namespace YODA; +using namespace std; int main() { + MSG_BLUE("Testing Profile1D: "); + + MSG_("Creating the Profile1D: "); Profile1D p(100,0,100); p.fill(1,1,2); + MSG_GREEN("PASS"); - cout << "Scaling the height: "; + MSG_("Scaling the height: "); p.scaleW(3); - if(p.sumW() != 6 || p.sumW2() != 36) { - cout << "FAIL" << endl; + if (p.sumW() != 6 || p.sumW2() != 36) { + MSG_RED("FAIL"); return -1; } - cout << "PASS" << endl; + MSG_GREEN("PASS"); - cout << "Resetting the profile: "; + MSG_("Resetting the profile: "); p.reset(); - if(p.sumW() != 0 || p.sumW2() != 0){ - cout << "FAIL" << endl; + if (p.sumW() != 0 || p.sumW2() != 0){ + MSG_RED("FAIL"); return -1; } - cout << "PASS" << endl; + MSG_GREEN("PASS"); - cout << "Merging the bins: "; + MSG_("Merging the bins: "); p.mergeBins(0,10); - if(p.bin(0).xMin() != 0 || p.bin(0).xMax() != 11){ - cout << "FAIL" << endl; + if (p.bin(0).xMin() != 0 || p.bin(0).xMax() != 11){ + MSG_RED("FAIL"); return -1; } - if(p.numBins() != 90){ - cout << "FAIL" << endl; + if (p.numBins() != 90){ + MSG_RED("FAIL"); return -1; } - cout << "PASS" << endl; + MSG_GREEN("PASS"); - cout << "Testing rebinning: "; + MSG_("Testing rebinning: "); p.rebin(2); - for(size_t i = 1; i < p.bins().size() - 1; ++i){ - if(2 != p.bin(i).width()){ - cout << "FAIL" << endl; + for (size_t i = 1; i < p.bins().size() - 1; ++i){ + if (2 != p.bin(i).width()){ + MSG_RED("FAIL"); return -1; } } - if(p.numBins() != 45){ - cout << "FAIL" << endl; + if (p.numBins() != 45) { + MSG_RED("FAIL"); return -1; } - cout << "PASS" << endl; + MSG_GREEN("PASS"); - cout << "Trying to add a bin (first method): "; + MSG_("Trying to add a bin (first method): "); p.addBin(110, 120); - if(p.numBins() != 46){ - cout << "FAIL" << endl; + if (p.numBins() != 46){ + MSG_RED("FAIL"); return -1; } - cout << "PASS" << endl; + MSG_GREEN("PASS"); - cout << "Trying to add a bin (second method): "; + MSG_("Trying to add a bin (second method): "); vector<double> test; test.push_back(120); test.push_back( 140); test.push_back(145); p.addBins(test); - if(p.numBins() != 48){ - cout << "FAIL" << endl; + if (p.numBins() != 48){ + MSG_RED("FAIL"); return -1; } - cout << "PASS" << endl; + MSG_GREEN("PASS"); - // cout << "Trying to add a bin (third method): "; + // MSG("Trying to add a bin (third method): "); // vector<pair<double,double> > test2; // test2.push_back(make_pair(180,190)); // p.addBins(test2); // if(p.numBins() != 49){ - // cout << "FAIL" << endl; + // MSG_RED("FAIL"); // return -1; // } - // cout << "PASS" << endl; + // MSG_GREEN("PASS"); return EXIT_SUCCESS; } Modified: trunk/tests/TestHisto1Da.cc ============================================================================== --- trunk/tests/TestHisto1Da.cc Wed May 2 17:43:26 2012 (r457) +++ trunk/tests/TestHisto1Da.cc Wed May 2 19:10:32 2012 (r458) @@ -19,7 +19,7 @@ h.fill(num); } - MSG("Mean value = " << h.mean() << " +- " << h.stdDev()); + MSG("Mean value = " << h.mean() << " +- " << h.stdErr()); MSG("Total area = " << h.integral()); const HistoBin1D& highestBin = *( max_element(h.bins().begin(), h.bins().end(), compareHeight) );
More information about the yoda-svn mailing list |