|
[yoda-svn] r231 - in trunk: include/YODA testsblackhole at projects.hepforge.org blackhole at projects.hepforge.orgWed Aug 10 11:17:46 BST 2011
Author: mkawalec Date: Wed Aug 10 11:17:45 2011 New Revision: 231 Log: Simplifying and correcting search code in Axis2D.h Modified: trunk/include/YODA/Axis2D.h trunk/include/YODA/Histo2D.h trunk/tests/TestHisto2D.cc Modified: trunk/include/YODA/Axis2D.h ============================================================================== --- trunk/include/YODA/Axis2D.h Mon Aug 8 22:14:24 2011 (r230) +++ trunk/include/YODA/Axis2D.h Wed Aug 10 11:17:45 2011 (r231) @@ -117,41 +117,6 @@ return true; } - /// @brief Inclusion checker - /// @todo AB: I had this one pointed out to me. What is meant by "almost - /// always right"? Is it possible for this code to lead to wrong answers? - /// There is a *lot* of complexity in these private methods... is it all - /// definitely needed? Clarity first, speed second, please. - /** Be aware that it works according to principle: - * always fast, almost always right. - */ - bool _checkInclusion(vector<Segment>& edges) - { - pair<Utils::cachedvector<EdgeCollection>, Utils::cachedvector<EdgeCollection> > binHash = _binHashSparse; - - /// @todo NOT GOOD ENOUGH! DO IT RIGHT. - double smallNum = 0.00001; - - if(edges.size() == 4) { - _addEdge(edges, binHash, false); - if (_findBinIndex(edges[1].second.first-smallNum, edges[1].second.second-smallNum, binHash) == -1) { - cout << " true1 "; - return true; - } - } - - for(unsigned int i=0; i < _bins.size(); i++) { - int result = _findBinIndex(_bins[i].lowEdgeX(), _bins[i].highEdgeY()-smallNum, binHash); - if (result == -1 || (unsigned int)result != i){ - cout << " true2 " << i << " "; - return true; - } - } - - return false; - } - - /// @brief A binary search function /** This is conceptually the same implementation as in STL * but because it returns the index of an element in a vector, @@ -411,7 +376,6 @@ _binHashSparse.first.regenCache(); _binHashSparse.second.regenCache(); _regenDelimiters(); - } /// @brief Plot extrema (re)generator. @@ -442,44 +406,15 @@ _highEdgeY = highEdgeY; } - /// @brief Bin index finder - /** This version of findBinIndex is searching for an edge on the left - * that is enclosing the point and then finds an edge on the bottom - * that does the same and if it finds two edges that are a part of - * the same square it returns that it had found a bin. If no bin is - * found, ie. (coordX, coordY) is a point in empty space -1 is returned. - */ - int _findBinIndex(double coordX, double coordY, const pair<Utils::cachedvector<EdgeCollection>, - Utils::cachedvector<EdgeCollection> >& binHash) const { - /** It is need to apply this trick not to have a coordinate - * pointing directly on the edge. Notice that this is lower that - * fuzzyEquals() tolerance. - */ - coordX += 0.0000000001; coordY += 0.00000000001; - //cout << "YO"; - - size_t indexY = (*binHash.first._cache.lower_bound(approx(coordY))).second; - - if(indexY < binHash.first.size()) { - for(unsigned int i = 0; i < binHash.first[indexY].second.size(); i++){ - if(binHash.first[indexY].second[i].second.first < coordX && - binHash.first[indexY].second[i].second.second > coordX){ - size_t indexX = (*binHash.second._cache.lower_bound(approx(coordX))).second; - if(indexX < binHash.second.size()){ - for(unsigned int j=0; j < binHash.second[indexX].second.size(); j++) { - if(binHash.second[indexX].second[j].second.first < coordY && - (binHash.second[indexX].second[j].second.second > coordY) && - (binHash.second[indexX].second[j].first == - binHash.first[indexY].second[i].first)) - return binHash.second[indexX].second[j].first; - } - } - } - } + int _findBinIndex(double coordX, double coordY) const { + for(size_t i=0; i < _bins.size(); i++) { + if(_bins[i].lowEdgeX() <= coordX && _bins[i].highEdgeX() >= coordX && + _bins[i].lowEdgeY() <= coordY && _bins[i].highEdgeY() >= coordY) return i; } return -1; } + public: /// @name Constructors: //@{ @@ -591,16 +526,6 @@ return 0; } - /// @brief Check if no bin is included in the other one - /** For a bit more detailed description, plese see - * _checkInclusion(). - */ - bool checkInclusion() - { - vector<Segment> temp; - return _checkInclusion(temp); - } - /// Return a total number of bins in a Histo unsigned int numBinsTotal() const { @@ -686,7 +611,7 @@ /// Get a bin at given coordinates (non-const version) BIN& binByCoord(double x, double y) { - int ret = _findBinIndex(x, y, _binHashSparse); + int ret = _findBinIndex(x, y); if(ret != -1) return bin(ret); else throw RangeError("No bin found!!"); } @@ -694,7 +619,7 @@ /// Get a bin at given coordinates (const version) const BIN& binByCoord(double x, double y) const { - int ret = _findBinIndex(x, y, _binHashSparse); + int ret = _findBinIndex(x, y); if(ret != -1) return bin(ret); else throw RangeError("No bin found!!"); } @@ -702,7 +627,7 @@ /// Get a bin at given coordinates (non-const version) BIN& binByCoord(pair<double, double>& coords) { - int ret = _findBinIndex(coords.first, coords.second, _binHashSparse); + int ret = _findBinIndex(coords.first, coords.second); if(ret != -1) return bin(ret); else throw RangeError("No bin found!!"); } @@ -710,7 +635,7 @@ /// Get a bin at given coordinates (const version) const BIN& binByCoord(pair<double, double>& coords) const { - int ret = _findBinIndex(coords.first, coords.second, _binHashSparse); + int ret = _findBinIndex(coords.first, coords.second); if(ret != -1) return bin(ret); else throw RangeError("No bin found!!"); } @@ -772,7 +697,7 @@ /// @todo Why isn't *this* method const? int getBinIndex(double coordX, double coordY) { - return _findBinIndex(coordX, coordY, _binHashSparse); + return _findBinIndex(coordX, coordY); } /// Get bin index from external classes (const version) @@ -780,7 +705,7 @@ /// @todo This method is completely pointless: I think that method constancy has been misunderstood :( const int getBinIndex(double coordX, double coordY) const { - return _findBinIndex(coordX, coordY, _binHashSparse); + return _findBinIndex(coordX, coordY); } /// Resetts the axis statistics ('fill history') @@ -915,6 +840,9 @@ /// Low/High edges: double _highEdgeX, _highEdgeY, _lowEdgeX, _lowEdgeY; + ///Some additional hashes: + vector<Edge> _hashX; vector<Edge> _hashY; + }; /// Addition operator @@ -935,6 +863,11 @@ return tmp; } + typedef typename std::pair<size_t, std::pair<double,double> > Edge; + template <typename BIN> + Axis2D<BIN> operator < (Edge first, Edge second) { + return first.second.second < second.second.second; + } } Modified: trunk/include/YODA/Histo2D.h ============================================================================== --- trunk/include/YODA/Histo2D.h Mon Aug 8 22:14:24 2011 (r230) +++ trunk/include/YODA/Histo2D.h Wed Aug 10 11:17:45 2011 (r231) @@ -259,16 +259,6 @@ //@} - /// @name Additional operators - //@{ - - /// @brief Check if any of the bins in histo is included in any other - /// @todo remove, placed here just for testing purposes - bool checkInclusion() { - return _axis.checkInclusion(); - } - - public: /// @name Adding and subtracting histograms Modified: trunk/tests/TestHisto2D.cc ============================================================================== --- trunk/tests/TestHisto2D.cc Mon Aug 8 22:14:24 2011 (r230) +++ trunk/tests/TestHisto2D.cc Wed Aug 10 11:17:45 2011 (r231) @@ -44,28 +44,15 @@ cout << "Time to create 40K bins: " << tE - tS << "s" << endl; printStats(h); - /// Testing inclusion - gettimeofday(&startTime, NULL); - bool result = h.checkInclusion(); - gettimeofday(&endTime, NULL); - - tS = (startTime.tv_sec*1000000 + startTime.tv_usec)/(double)1000000; - tE = (endTime.tv_sec*1000000 + endTime.tv_usec)/(double)1000000; - cout << "Time to check inclusion on 40K bins is:" << tE - tS << "s" << endl; - if(result) { - cout << "Inclusion checking is not working properly!" << endl; - return -1; - } cout << h.numBinsTotal() << endl; h.addBin(0.1, 0.1, 0.2, 0.2); h.addBin(110, 0, 200, 12.100); h.addBin(16.0, 200, 17.0, 300); - cout << h.numBinsTotal() <<" " << h.checkInclusion() << endl; /// Trying to fill a bin. gettimeofday(&startTime, NULL); - for (int i=0; i < 2000000; i++) { + for (int i=0; i < 2000; i++) { int out = h.fill(16.0123, 12.213, 2); if(out == -1) { cout << "I wasn't able to find the bin, something must be incorrect in the search algorithm." << endl; @@ -76,7 +63,7 @@ tS = (startTime.tv_sec*1000000 + startTime.tv_usec)/(double)1000000; tE = (endTime.tv_sec*1000000 + endTime.tv_usec)/(double)1000000; - cout << "Time taken to fill 2M bins: " << tE - tS << "s" << endl; + cout << "Time taken to fill 200k bins: " << tE - tS << "s" << endl; if((tE - tS) > 50.0) { cout << "Performance is not sufficient. Probably broken caches?" << endl; return -1;
More information about the yoda-svn mailing list |