|
[yoda-svn] r236 - in trunk: . include/YODA src testsblackhole at projects.hepforge.org blackhole at projects.hepforge.orgFri Aug 12 20:16:51 BST 2011
Author: buckley Date: Fri Aug 12 20:16:50 2011 New Revision: 236 Log: Tidy and properly comment Dbn2D -- this needs to be converted to be based on 2 Dbn1Ds, to avoid duplicating some complex code. Also adding the state-setting persistency constructor chain for Profile1D and fixing a couple of bugs. Modified: trunk/ChangeLog trunk/TODO trunk/include/YODA/Axis1D.h trunk/include/YODA/Dbn1D.h trunk/include/YODA/Dbn2D.h trunk/include/YODA/Profile1D.h trunk/src/Dbn1D.cc trunk/src/Dbn2D.cc trunk/tests/Makefile.am Modified: trunk/ChangeLog ============================================================================== --- trunk/ChangeLog Fri Aug 12 02:18:35 2011 (r235) +++ trunk/ChangeLog Fri Aug 12 20:16:50 2011 (r236) @@ -1,3 +1,15 @@ +2011-08-12 Andy Buckley <andy at insectnation.org> + + * Adding proper Doxygen structures and full descriptive comments + to Dbn2D. + + * Adding the persistency state-setting constructors for Profile1D + and Dbn2D. + + * Inlining lots of methods on Dbn1D. Same should be done for + Dbn2D, but first it needs to be reimplemented in terms of two + Dbn1Ds + the cross-term. + 2011-08-11 Andy Buckley <andy at insectnation.org> * Various typo fixes and comments relating to persistency Modified: trunk/TODO ============================================================================== --- trunk/TODO Fri Aug 12 02:18:35 2011 (r235) +++ trunk/TODO Fri Aug 12 20:16:50 2011 (r236) @@ -3,44 +3,38 @@ NOW -* Histo2D & Dbn2D: add Python interface mapping. (MICHAL) - (done, compilation is fine, not tested yet, though) +* Histo2D & Dbn2D: add Python interface mapping. (MICHAL) (done, compilation is + fine, not tested yet, though) + AB: is this done? Remove the TODO item when you're completely happy (and have + checked that it's ok -- email the yoda at projects list and we'll take a look). + +* Reimplement Dbn2D in terms of two Dbn1Ds + the cross-term. (ANDY) * Remove getBinHash from Axis2D and use private bin hash member for equality comparisons (MICHAL) + AB: Is this done? -* Implement hierarchy-cascading state-setting constructors for Profile1D and - Histo2D. (AB) +* Implement hierarchy-cascading state-setting constructor for Histo2D. (MICHAL) * Use state-setting constructors for Histo and Profile persistency via the - Reader classes. (AB) + Reader classes. (HH) * Test that a y-binned Histo2D has the same per-y-column stats as an equivalently x-binned Profile1D. Perhaps use this as an opportunity to implement one of the the conversion functions mentioned in the NEXT section... (MICHAL) -* Axis1D::scaleX(d), Axis2D::scaleX/Y(d), Axis2D::scaleXY(d, d), - Scatter2D::scaleX/Y(d), Scatter2D::scaleXY(d, d). (MICHAL) - (done, also added scaling for Scatter3D. Scaling of errors is done in a - veeery crude way. AB: Elaborate?) - * Conversion functions to build Histo1D and Profile1D objects for slicings and marginalisations along both X and Y directions of 2D histos. Throw an exception if binnings are not complete grids. (MICHAL) (done -- to be reviewed) * Rebinning: merges of n adjacent bins and global rebinning by integer factor - (on widths or on bin groups?) (AB) + (on widths or on bin groups?) (AB for 1D, MICHAL for 2D) * New "flat" file format: adapt WriterYODA and fill ReaderYODA. (HH) Need to persist the whole-histo Dbn1D/Dbn2Ds, and over/underflows. -* Multiple errors on Scatter2D -- via a vector of Error2D? How to implement - flexible error combination? (DANIEL) - -* WriterFlat and ReaderFlat, for Scatter2D representations only. (HH) - NEXT @@ -51,6 +45,11 @@ * Profile2D & Dbn3D +* Multiple errors on Scatter2D -- via a vector of Error2D? How to implement + flexible error combination? (DANIEL) + +* WriterFlat and ReaderFlat, for Scatter2D representations only. (HH) + * Perhaps ProfileBin1D would be better if it contained a Dbn1D rather than two Dbn2Ds, since the over/underflow now *are* Dbn2Ds... Modified: trunk/include/YODA/Axis1D.h ============================================================================== --- trunk/include/YODA/Axis1D.h Fri Aug 12 02:18:35 2011 (r235) +++ trunk/include/YODA/Axis1D.h Fri Aug 12 20:16:50 2011 (r236) @@ -30,18 +30,18 @@ typedef typename Utils::sortedvector<BIN1D> Bins; - /// @name Helper functions to make bin edge vectors (see @file MathUtils.h) - //@{ + // /// @name Helper functions to make bin edge vectors (see @file MathUtils.h) + // //@{ - static inline std::vector<double> mkBinEdgesLin(double start, double end, size_t nbins) { - return linspace(start, end, nbins); - } + // static inline std::vector<double> mkBinEdgesLin(double start, double end, size_t nbins) { + // return linspace(start, end, nbins); + // } - static inline std::vector<double> mkBinEdgesLog(double start, double end, size_t nbins) { - return logspace(start, end, nbins); - } + // static inline std::vector<double> mkBinEdgesLog(double start, double end, size_t nbins) { + // return logspace(start, end, nbins); + // } - //@} + // //@} public: @@ -80,7 +80,7 @@ /// @brief State-setting constructor /// Principally intended for internal persistency use. - Axis1D(const Bins& bins, const Dbn1D& dbn_tot, const Dbn1D& dbn_uflow, const Dbn1D& dbn_oflow) + Axis1D(const Bins& bins, const DBN& dbn_tot, const DBN& dbn_uflow, const DBN& dbn_oflow) : _dbn(dbn_tot), _underflow(dbn_uflow), _overflow(dbn_oflow) { assert(!bins.empty()); Modified: trunk/include/YODA/Dbn1D.h ============================================================================== --- trunk/include/YODA/Dbn1D.h Fri Aug 12 02:18:35 2011 (r235) +++ trunk/include/YODA/Dbn1D.h Fri Aug 12 20:16:50 2011 (r236) @@ -7,6 +7,7 @@ #define YODA_Dbn1D_h #include "YODA/Exceptions.h" +#include <cmath> namespace YODA { @@ -93,15 +94,20 @@ //@{ /// Weighted mean, \f$ \bar{x} \f$, of distribution. - double mean() const; + double mean() const { + // This is ok, even for negative sum(w) + return _sumWX/_sumW; + } /// Weighted variance, \f$ \sigma^2 \f$, of distribution. double variance() const; /// Weighted standard deviation, \f$ \sigma \f$, of distribution. - double stdDev() const; + double stdDev() const { + return std::sqrt(variance()); + } - /// Weighted standard error, \f$ \sim \sigma/\sqrt{N-1} \f$, of distribution. + /// Weighted standard error on the mean, \f$ \sim \sigma/\sqrt{N-1} \f$, of distribution. double stdErr() const; //@} @@ -111,32 +117,52 @@ //@{ /// Number of entries (number of times @c fill was called, ignoring weights) - unsigned long numEntries() const; + unsigned long numEntries() const { + return _numFills; + } /// Effective number of entries \f$ = (\sum w)^2 / \sum w^2 \f$ - double effNumEntries() const; + double effNumEntries() const { + return _sumW*_sumW / _sumW2; + } /// The sum of weights - double sumW() const; + double sumW() const { + return _sumW; + } /// The sum of weights squared - double sumW2() const; + double sumW2() const { + return _sumW2; + } /// The sum of x*weight - double sumWX() const; + double sumWX() const { + return _sumWX; + } /// The sum of x^2 * weight - double sumWX2() const; + double sumWX2() const { + return _sumWX2; + } //@} - public: + + /// @name Operators + //@{ /// Add two dbns - Dbn1D& operator += (const Dbn1D&); + Dbn1D& operator += (const Dbn1D& d) { + return add(d); + } /// Subtract one dbn from another - Dbn1D& operator -= (const Dbn1D&); + Dbn1D& operator -= (const Dbn1D& d) { + return subtract(d); + } + + //@} protected: @@ -160,10 +186,18 @@ /// Add two dbns - Dbn1D operator + (const Dbn1D& a, const Dbn1D& b); + inline Dbn1D operator + (const Dbn1D& a, const Dbn1D& b) { + Dbn1D rtn = a; + rtn += b; + return rtn; + } /// Subtract one dbn from another - Dbn1D operator - (const Dbn1D& a, const Dbn1D& b); + inline Dbn1D operator - (const Dbn1D& a, const Dbn1D& b) { + Dbn1D rtn = a; + rtn -= b; + return rtn; + } } Modified: trunk/include/YODA/Dbn2D.h ============================================================================== --- trunk/include/YODA/Dbn2D.h Fri Aug 12 02:18:35 2011 (r235) +++ trunk/include/YODA/Dbn2D.h Fri Aug 12 20:16:50 2011 (r236) @@ -4,84 +4,176 @@ #include "YODA/Exceptions.h" namespace YODA { - class Dbn2D { - public: - - Dbn2D() { - reset(); - } - /// fill() requesting coordinates as two diferent numbers - void fill(double valX, double valY, double weight=1.0); - - /// fill() accepting coordinates as a pair - void fill(std::pair<double,double> val, double weight=1.0); - void reset(); - - /// Scaling the height of a bin - void scaleW(double sf) { - _sumW *= sf; - _sumW2 *= sf*sf; - _sumWX *= sf; - _sumWX2 *= sf*sf; - _sumWY *= sf; - _sumWY2 *= sf*sf; - _sumWXY *= sf; - } - - /// Scaling the dimensions of a distribution - void scale(double scaleX, double scaleY) { - _sumWX *= scaleX; - _sumWX2 *= scaleX*scaleX; - _sumWY *= scaleY; - _sumWY2 *= scaleY*scaleY; - _sumWXY *= scaleX*scaleY; - } - - /// Some in-distribution variables. Should be self-evident. - double xMean() const; - double yMean() const; - - double xVariance() const; - double yVariance() const; - - double xStdDev() const; - double yStdDev() const; - - double xStdErr() const; - double yStdErr() const; - - unsigned long numEntries() const; - double effNumEntries() const; - double sumW() const; - double sumW2() const; - double sumWX() const; - double sumWY() const; - double sumWXY() const; - double sumWX2() const; - double sumWY2() const; - - Dbn2D& operator += (const Dbn2D&); - Dbn2D& operator -= (const Dbn2D&); - - protected: - Dbn2D& add(const Dbn2D&); - Dbn2D& substract(const Dbn2D&); - - private: - - unsigned long _numFills; - double _sumW; - double _sumW2; - double _sumWX; - double _sumWY; - double _sumWX2; - double _sumWY2; - double _sumWXY; - }; - Dbn2D operator + (const Dbn2D& a, const Dbn2D& b); - Dbn2D operator - (const Dbn2D& a, const Dbn2D& b); + /// A representation of the vital statistics of a 2D distribution + class Dbn2D { + public: + + /// @name Constructors + //@{ + + Dbn2D() { + reset(); + } + + /// @brief Constructor to set a distribution with a pre-filled state. + /// Principally designed for internal persistency use. + Dbn2D(unsigned int numEntries, double sumW, double sumW2, + double sumWX, double sumWX2, double sumWY, double sumWY2, double sumWXY) { + _numFills = numEntries; + _sumW = sumW; + _sumW2 = sumW2; + _sumWX = sumWX; + _sumWX2 = sumWX2; + _sumWY = sumWY; + _sumWY2 = sumWY2; + _sumWXY = sumWXY; + } + + //@} + + + /// @name Modifiers + //@{ + + /// Fill, providing the fill coordinates as two different numbers. + void fill(double valX, double valY, double weight=1.0); + + /// Fill, providing the fill coordinates as a pair. + void fill(std::pair<double,double> val, double weight=1.0); + + /// Reset the distribution to an unfilled state. + void reset(); + + /// Rescale as if all fill weights had been different by factor @a scalefactor. + void scaleW(double scalefactor) { + const double sf = scalefactor; + _sumW *= sf; + _sumW2 *= sf*sf; + _sumWX *= sf; + _sumWX2 *= sf*sf; + _sumWY *= sf; + _sumWY2 *= sf*sf; + _sumWXY *= sf; + } + + /// Rescale x and y: needed if histo bin edges are rescaled. + void scale(double scaleX, double scaleY) { + _sumWX *= scaleX; + _sumWX2 *= scaleX*scaleX; + _sumWY *= scaleY; + _sumWY2 *= scaleY*scaleY; + _sumWXY *= scaleX*scaleY; + } + + //@} + + + /// @name Distribution statistics + //@{ + + /// Weighted mean, \f$ \bar{x} \f$, of distribution. + double xMean() const; + + /// Weighted mean, \f$ \bar{y} \f$, of distribution. + double yMean() const; + + /// Weighted \f$ x \f$ variance, \f$ \sigma_x^2 \f$, of distribution. + double xVariance() const; + + /// Weighted \f$ y \f$ variance, \f$ \sigma_y^2 \f$, of distribution. + double yVariance() const; + + /// Weighted \f$ x \f$ standard deviation, \f$ \sigma_x \f$, of distribution. + double xStdDev() const; + + /// Weighted \f$ y \f$ standard deviation, \f$ \sigma_y \f$, of distribution. + double yStdDev() const; + + /// Weighted standard error on the \f$ x \f$ mean, \f$ \sim \sigma_x/\sqrt{N-1} \f$, of distribution. + double xStdErr() const; + + /// Weighted standard error on the \f$ y \f$ mean, \f$ \sim \sigma_y/\sqrt{N-1} \f$, of distribution. + double yStdErr() const; + + //@} + + + /// @name Raw distribution running sums + //@{ + + /// Number of entries (number of times @c fill was called, ignoring weights) + unsigned long numEntries() const; + + /// Effective number of entries \f$ = (\sum w)^2 / \sum w^2 \f$ + double effNumEntries() const; + + /// The sum of weights + double sumW() const; + + /// The sum of weights squared + double sumW2() const; + + /// The sum of x*weight + double sumWX() const; + + /// The sum of y*weight + double sumWY() const; + + /// The sum of x*x*weight + double sumWX2() const; + + /// The sum of y*y*weight + double sumWY2() const; + + /// The sum of x*y*weight + double sumWXY() const; + + //@} + + + /// @name Operators + //@{ + + /// Add two dbns + Dbn2D& operator += (const Dbn2D&); + + /// Subtract one dbn from another + Dbn2D& operator -= (const Dbn2D&); + + //@} + + + protected: + + /// Add two dbns (internal, explicitly named version) + Dbn2D& add(const Dbn2D&); + + /// Subtract one dbn from another (internal, explicitly named version) + Dbn2D& subtract(const Dbn2D&); + + + private: + + unsigned long _numFills; + double _sumW; + double _sumW2; + double _sumWX; + double _sumWY; + double _sumWX2; + double _sumWY2; + double _sumWXY; + + }; + + + /// Add two dbns + inline Dbn2D operator + (const Dbn2D& a, const Dbn2D& b); + + /// Subtract one dbn from another + inline Dbn2D operator - (const Dbn2D& a, const Dbn2D& b); + } #endif Modified: trunk/include/YODA/Profile1D.h ============================================================================== --- trunk/include/YODA/Profile1D.h Fri Aug 12 02:18:35 2011 (r235) +++ trunk/include/YODA/Profile1D.h Fri Aug 12 20:16:50 2011 (r236) @@ -52,15 +52,7 @@ const std::string& path="", const std::string& title="") : AnalysisObject("Profile1D", path, title), _axis(xbinedges) - { } - - /// Constructor giving a vector of bins - Profile1D(const std::vector<ProfileBin1D>& xbins, - const std::string& path="", const std::string& title="") - : AnalysisObject("Profile1D", path, title), - _axis(xbins) - { } - + { } /// Copy constructor with optional new path Profile1D(const Profile1D& p, const std::string& path=""); @@ -71,6 +63,14 @@ /// Constructor from a Histo1D's binning, with optional new path Profile1D(const Histo1D& h, const std::string& path=""); + /// @brief State-setting constructor. + /// Intended principally for internal persistency use. + Profile1D(const std::vector<ProfileBin1D>& bins, + const Dbn2D& dbn_tot, const Dbn2D& dbn_uflow, const Dbn2D& dbn_oflow, + const std::string& path="", const std::string& title="") + : AnalysisObject("Profile1D", path, title), + _axis(bins, dbn_tot, dbn_uflow, dbn_oflow) + { } //@} Modified: trunk/src/Dbn1D.cc ============================================================================== --- trunk/src/Dbn1D.cc Fri Aug 12 02:18:35 2011 (r235) +++ trunk/src/Dbn1D.cc Fri Aug 12 20:16:50 2011 (r236) @@ -4,7 +4,6 @@ // Copyright (C) 2008-2011 The YODA collaboration (see AUTHORS for details) // #include "YODA/Dbn1D.h" -#include <cmath> #include <iostream> namespace YODA { @@ -30,42 +29,6 @@ } - unsigned long Dbn1D::numEntries() const { - return _numFills; - } - - - double Dbn1D::effNumEntries() const { - return _sumW*_sumW / _sumW2; - } - - - double Dbn1D::sumW() const { - return _sumW; - } - - - double Dbn1D::sumW2() const { - return _sumW2; - } - - - double Dbn1D::sumWX() const { - return _sumWX; - } - - - double Dbn1D::sumWX2() const { - return _sumWX2; - } - - - double Dbn1D::mean() const { - // This is ok, even for negative sum(w) - return _sumWX/_sumW; - } - - double Dbn1D::variance() const { // Weighted variance defined as // sig2 = ( sum(wx**2) * sum(w) - sum(wx)**2 ) / ( sum(w)**2 - sum(w**2) ) @@ -89,11 +52,6 @@ } - double Dbn1D::stdDev() const { - return std::sqrt(variance()); - } - - double Dbn1D::stdErr() const { // Handle zero/negative sum weight if (effNumEntries() == 0) { @@ -124,28 +82,4 @@ } - Dbn1D& Dbn1D::operator += (const Dbn1D& d) { - return add(d); - } - - - Dbn1D& Dbn1D::operator -= (const Dbn1D& d) { - return subtract(d); - } - - - Dbn1D operator + (const Dbn1D& a, const Dbn1D& b) { - Dbn1D rtn = a; - rtn += b; - return rtn; - } - - - Dbn1D operator - (const Dbn1D& a, const Dbn1D& b) { - Dbn1D rtn = a; - rtn -= b; - return rtn; - } - - } Modified: trunk/src/Dbn2D.cc ============================================================================== --- trunk/src/Dbn2D.cc Fri Aug 12 02:18:35 2011 (r235) +++ trunk/src/Dbn2D.cc Fri Aug 12 20:16:50 2011 (r236) @@ -21,9 +21,9 @@ _sumWX2 += weight*valX*valX; _sumWY +=weight*valY; _sumWY2 += weight*valY*valY; - _sumWXY += weight*valX*valY; + _sumWXY += weight*valX*valY; } - + void Dbn2D::fill(std::pair<double,double> coords, double weight) { Dbn2D::fill(coords.first, coords.second, weight); } @@ -68,7 +68,7 @@ double Dbn2D::sumWX2() const { return _sumWX2; } - + double Dbn2D::sumWY() const { return _sumWY; } @@ -90,6 +90,9 @@ return _sumWY/_sumW; } + /// @todo We should NOT be duplicating this code. An implementation of Dbn2D + /// in terms of 2 x Dbn1D + the cross-term would be much better. We should + /// DEFINITELY do that. double Dbn2D::xVariance() const { // Weighted variance defined as // sig2 = ( sum(wx**2) * sum(w) - sum(wx)**2 ) / ( sum(w)**2 - sum(w**2) ) @@ -110,7 +113,7 @@ } return num/den; } - + double Dbn2D::yVariance() const { if(effNumEntries() == 0.0) { throw LowStatsError("Requested width of a distribution with no net will weights"); @@ -126,14 +129,17 @@ return num/den; } + double Dbn2D::xStdDev() const { return std::sqrt(xVariance()); } - + + double Dbn2D::yStdDev() const { return std::sqrt(yVariance()); } - + + double Dbn2D::xStdErr() const { // Handle zero/negative sum weight if (effNumEntries() == 0) { @@ -143,6 +149,7 @@ return std::sqrt(xVariance() / effNumEntries()); } + double Dbn2D::yStdErr() const { if(effNumEntries() == 0) { throw LowStatsError("Requested std error of a distribution with no net fill weights"); @@ -150,6 +157,7 @@ return std::sqrt(xVariance() / effNumEntries()); } + Dbn2D& Dbn2D::add(const Dbn2D& d) { _numFills += d._numFills; _sumW += d._sumW; @@ -163,7 +171,7 @@ } - Dbn2D& Dbn2D::substract(const Dbn2D& d) { + Dbn2D& Dbn2D::subtract(const Dbn2D& d) { _numFills += d._numFills; //< @todo Hmm, what's best?!? _sumW -= d._sumW; _sumW2 -= d._sumW2; @@ -182,7 +190,7 @@ Dbn2D& Dbn2D::operator -= (const Dbn2D& d) { - return substract(d); + return subtract(d); } Modified: trunk/tests/Makefile.am ============================================================================== --- trunk/tests/Makefile.am Fri Aug 12 02:18:35 2011 (r235) +++ trunk/tests/Makefile.am Fri Aug 12 20:16:50 2011 (r236) @@ -18,7 +18,7 @@ TESTS_ENVIRONMENT = \ LD_LIBRARY_PATH=$(top_builddir)/src:$(top_builddir)/src/.libs:$(LD_LIBRARY_PATH) \ - PYTHON_BUILD_DIR=$(top_builddir)/pyext/build \ + PYTHONPATH=$(top_builddir)/pyext/build \ PATH=$(top_builddir)/bin:$(PATH) TESTS = \
More information about the yoda-svn mailing list |