diff --git a/Framework/DataObjects/inc/MantidDataObjects/EventList.h b/Framework/DataObjects/inc/MantidDataObjects/EventList.h index fa60d100681af04106a7a896d2890b7fd14708ea..03bef47ac48aaf1e3c769bd869095e78ff6a78f1 100644 --- a/Framework/DataObjects/inc/MantidDataObjects/EventList.h +++ b/Framework/DataObjects/inc/MantidDataObjects/EventList.h @@ -401,10 +401,6 @@ private: /// Mutex that is locked while sorting an event list mutable std::mutex m_sortMutex; - template <class T> - static typename std::vector<T>::const_iterator - findFirstEvent(const std::vector<T> &events, const double seek_tof); - template <class T> static typename std::vector<T>::const_iterator findFirstPulseEvent(const std::vector<T> &events, @@ -416,10 +412,6 @@ private: const double seek_time, const double &tofFactor, const double &tofOffset) const; - template <class T> - static typename std::vector<T>::iterator - findFirstEvent(std::vector<T> &events, const double seek_tof); - void generateCountsHistogram(const MantidVec &X, MantidVec &Y) const; void generateCountsHistogramPulseTime(const MantidVec &X, MantidVec &Y) const; diff --git a/Framework/DataObjects/inc/MantidDataObjects/Events.h b/Framework/DataObjects/inc/MantidDataObjects/Events.h index 84d26d3769d889447d9c5fe66325c8dc7bb60755..ae02ce201515b0c221f2615b618fef16f830fd4f 100644 --- a/Framework/DataObjects/inc/MantidDataObjects/Events.h +++ b/Framework/DataObjects/inc/MantidDataObjects/Events.h @@ -131,8 +131,21 @@ public: WeightedEventNoTime(); bool operator==(const WeightedEventNoTime &rhs) const; - bool operator<(const WeightedEventNoTime &rhs) const; - bool operator<(const double rhs_tof) const; + + /** < comparison operator, using the TOF to do the comparison. + * @param rhs: the other WeightedEventNoTime to compare. + * @return true if this->m_tof < rhs.m_tof + */ + bool operator<(const WeightedEventNoTime &rhs) const { + return (this->m_tof < rhs.m_tof); + } + + /** < comparison operator, using the TOF to do the comparison. + * @param rhs_tof: the other time of flight to compare. + * @return true if this->m_tof < rhs.m_tof + */ + bool operator<(const double rhs_tof) const { return (this->m_tof < rhs_tof); } + bool equals(const WeightedEventNoTime &rhs, const double tolTof, const double tolWeight) const; diff --git a/Framework/DataObjects/src/EventList.cpp b/Framework/DataObjects/src/EventList.cpp index ee8fb7c957b239e4d3750a15f2af53faf8f53a31..5029f2cdd0e762e499f64c0c5688c6c5d20d5287 100644 --- a/Framework/DataObjects/src/EventList.cpp +++ b/Framework/DataObjects/src/EventList.cpp @@ -986,42 +986,6 @@ void EventList::setSortOrder(const EventSortType order) const { this->order = order; } -// // MergeSort from: -// http://en.literateprograms.org/Merge_sort_%28C_Plus_Plus%29#chunk%20def:merge -// template<typename IT, typename VT> void insert(IT begin, IT end, const VT -// &v) -// { -// while(begin+1!=end && *(begin+1)<v) { -// std::swap(*begin, *(begin+1)); -// ++begin; -// } -// *begin=v; -// } -// -// template<typename IT> void merge(IT begin, IT begin_right, IT end) -// { -// for(;begin<begin_right; ++begin) { -// if(*begin>*begin_right) { -// typename std::iterator_traits<IT>::value_type v(*begin); -// *begin=*begin_right; -// insert(begin_right, end, v); -// } -// } -// } -// -// template<typename IT> void mergesort(IT begin, IT end) -// { -// size_t size(end-begin); -// //std::cout << "mergesort called on " << size << "\n"; -// if(size<2) return; -// -// IT begin_right=begin+size/2; -// -// mergesort(begin, begin_right); -// mergesort(begin_right, end); -// merge(begin, begin_right, end); -// } - // -------------------------------------------------------------------------- /** Sort events by TOF in one thread */ void EventList::sortTof() const { @@ -1036,15 +1000,14 @@ void EventList::sortTof() const { switch (eventType) { case TOF: - tbb::parallel_sort(events.begin(), events.end(), compareEventTof<TofEvent>); + tbb::parallel_sort(events.begin(), events.end()); break; case WEIGHTED: - tbb::parallel_sort(weightedEvents.begin(), weightedEvents.end(), - compareEventTof<WeightedEvent>); + tbb::parallel_sort(weightedEvents.begin(), weightedEvents.end()); break; case WEIGHTED_NOTIME: - tbb::parallel_sort(weightedEventsNoTime.begin(), weightedEventsNoTime.end(), - compareEventTof<WeightedEventNoTime>); + tbb::parallel_sort(weightedEventsNoTime.begin(), + weightedEventsNoTime.end()); break; } // Save the order to avoid unnecessary re-sorting. @@ -1852,17 +1815,10 @@ void EventList::compressFatEvents( * @return iterator where the first event matching it is. */ template <class T> -typename std::vector<T>::const_iterator -EventList::findFirstEvent(const std::vector<T> &events, const double seek_tof) { - auto itev = events.cbegin(); - auto itev_end = events.cend(); // cache for speed - - // if tof < X[0], that means that you need to skip some events - while ((itev != itev_end) && (itev->tof() < seek_tof)) - itev++; - // Better fix would be to use a binary search instead of the linear one used - // here. - return itev; +typename std::vector<T>::const_iterator static findFirstEvent( + const std::vector<T> &events, T seek_tof) { + return std::find_if_not(events.cbegin(), events.cend(), + [seek_tof](const T &x) { return x < seek_tof; }); } // -------------------------------------------------------------------------- @@ -1932,17 +1888,10 @@ typename std::vector<T>::const_iterator EventList::findFirstTimeAtSampleEvent( * @return iterator where the first event matching it is. */ template <class T> -typename std::vector<T>::iterator -EventList::findFirstEvent(std::vector<T> &events, const double seek_tof) { - auto itev = events.begin(); - auto itev_end = events.end(); // cache for speed - - // if tof < X[0], that means that you need to skip some events - while ((itev != itev_end) && (itev->tof() < seek_tof)) - itev++; - // Better fix would be to use a binary search instead of the linear one used - // here. - return itev; +typename std::vector<T>::iterator static findFirstEvent(std::vector<T> &events, + T seek_tof) { + return std::find_if_not(events.begin(), events.end(), + [seek_tof](const T &x) { return x < seek_tof; }); } // -------------------------------------------------------------------------- @@ -1989,7 +1938,7 @@ void EventList::histogramForWeightsHelper(const std::vector<T> &events, // Do we even have any events to do? if (!events.empty()) { // Iterate through all events (sorted by tof) - auto itev = findFirstEvent(events, X[0]); + auto itev = findFirstEvent(events, T(X[0])); auto itev_end = events.cend(); // The above can still take you to end() if no events above X[0], so check // again. @@ -2355,45 +2304,20 @@ void EventList::generateCountsHistogram(const MantidVec &X, // Do we even have any events to do? if (!this->events.empty()) { - // Iterate through all events (sorted by tof) - std::vector<TofEvent>::const_iterator itev = - findFirstEvent(this->events, X[0]); - std::vector<TofEvent>::const_iterator itev_end = - events.end(); // cache for speed - // The above can still take you to end() if no events above X[0], so check - // again. - if (itev == itev_end) - return; - - // Find the first bin - size_t bin = 0; - - // The tof is greater the first bin boundary, so we need to find the first - // bin - double tof = itev->tof(); - while (bin < x_size - 1) { - // Within range? - if ((tof >= X[bin]) && (tof < X[bin + 1])) { - Y[bin]++; + // Iterate through all events (sorted by tof) placing them in the correct + // bin. + auto itev = findFirstEvent(this->events, TofEvent(X[0])); + // Go through all the events, + for (auto itx = X.cbegin(); itev != events.end(); ++itev) { + double tof = itev->tof(); + itx = std::find_if(itx, X.cend(), + [tof](const double x) { return tof < x; }); + if (itx == X.cend()) { break; } - ++bin; - } - // Go to the next event, we've already binned this first one. - ++itev; - - // Keep going through all the events - while ((itev != itev_end) && (bin < x_size - 1)) { - tof = itev->tof(); - while (bin < x_size - 1) { - // Within range? - if ((tof >= X[bin]) && (tof < X[bin + 1])) { - Y[bin]++; - break; - } - ++bin; - } - ++itev; + auto bin = + std::max(std::distance(X.cbegin(), itx) - 1, std::ptrdiff_t{0}); + ++Y[bin]; } } // end if (there are any events to histogram) } @@ -2469,8 +2393,7 @@ void EventList::integrateHelper(std::vector<T> &events, const double minX, lowit = std::lower_bound(events.begin(), events.end(), minX); // If the last element is higher that the xmax then search for new lowit if ((highit - 1)->tof() > maxX) { - highit = - std::upper_bound(lowit, events.end(), T(maxX), compareEventTof<T>); + highit = std::upper_bound(lowit, events.end(), T(maxX)); } } @@ -2712,13 +2635,11 @@ std::size_t EventList::maskTofHelper(std::vector<T> &events, return 0; // Find the index of the first tofMin - auto it_first = std::lower_bound(events.begin(), events.end(), tofMin, - compareEventTof<T>); + auto it_first = std::lower_bound(events.begin(), events.end(), tofMin); if ((it_first != events.end()) && (it_first->tof() < tofMax)) { // Something was found // Look for the first one > tofMax - auto it_last = - std::upper_bound(it_first, events.end(), tofMax, compareEventTof<T>); + auto it_last = std::upper_bound(it_first, events.end(), T(tofMax)); if (it_first >= it_last) { throw std::runtime_error("Event filter is all messed up"); // TODO @@ -3450,7 +3371,7 @@ void EventList::multiplyHistogramHelper(std::vector<T> &events, size_t x_size = X.size(); // Iterate through all events (sorted by tof) - auto itev = findFirstEvent(events, X[0]); + auto itev = findFirstEvent(events, T(X[0])); auto itev_end = events.end(); // The above can still take you to end() if no events above X[0], so check // again. @@ -3573,7 +3494,7 @@ void EventList::divideHistogramHelper(std::vector<T> &events, size_t x_size = X.size(); // Iterate through all events (sorted by tof) - auto itev = findFirstEvent(events, X[0]); + auto itev = findFirstEvent(events, T(X[0])); auto itev_end = events.end(); // The above can still take you to end() if no events above X[0], so check // again. diff --git a/Framework/DataObjects/src/Events.cpp b/Framework/DataObjects/src/Events.cpp index 64b8989c0ac0ca0187bc9f2ff921444a77c5da3e..4746a85b52b745e7a8a946c1d65c29895d3f8289 100644 --- a/Framework/DataObjects/src/Events.cpp +++ b/Framework/DataObjects/src/Events.cpp @@ -222,20 +222,6 @@ bool WeightedEventNoTime::operator==(const WeightedEventNoTime &rhs) const { (this->m_errorSquared == rhs.m_errorSquared); } -/** < comparison operator, using the TOF to do the comparison. - * @param rhs: the other WeightedEventNoTime to compare. - * @return true if this->m_tof < rhs.m_tof*/ -bool WeightedEventNoTime::operator<(const WeightedEventNoTime &rhs) const { - return (this->m_tof < rhs.m_tof); -} - -/** < comparison operator, using the TOF to do the comparison. - * @param rhs_tof: the other time of flight to compare. - * @return true if this->m_tof < rhs.m_tof*/ -bool WeightedEventNoTime::operator<(const double rhs_tof) const { - return (this->m_tof < rhs_tof); -} - /** * Compare two events within the specified tolerance * diff --git a/Framework/Types/inc/MantidTypes/Event/TofEvent.h b/Framework/Types/inc/MantidTypes/Event/TofEvent.h index b997d0525365490fd9ed95eeede78b8a833e4ef5..a7558c3290592b1614b80d3fe2ae75f77041c751 100644 --- a/Framework/Types/inc/MantidTypes/Event/TofEvent.h +++ b/Framework/Types/inc/MantidTypes/Event/TofEvent.h @@ -63,8 +63,18 @@ public: TofEvent(); bool operator==(const TofEvent &rhs) const; - bool operator<(const TofEvent &rhs) const; - bool operator<(const double rhs_tof) const; + /** < comparison operator, using the TOF to do the comparison. + * @param rhs: the other TofEvent to compare. + * @return true if this->m_tof < rhs.m_tof + */ + bool operator<(const TofEvent &rhs) const { + return (this->m_tof < rhs.m_tof); + } + /** < comparison operator, using the TOF to do the comparison. + * @param rhs_tof: the other time of flight to compare. + * @return true if this->m_tof < rhs.m_tof + */ + bool operator<(const double rhs_tof) const { return (this->m_tof < rhs_tof); } bool operator>(const TofEvent &rhs) const; bool equals(const TofEvent &rhs, const double tolTof, const int64_t tolPulse) const; diff --git a/Framework/Types/src/Event/TofEvent.cpp b/Framework/Types/src/Event/TofEvent.cpp index 9e92d192d3a2f2111a6756e05cbb5d2d675de561..8c50955dacb7464366959d92722de6aaf19f60b1 100644 --- a/Framework/Types/src/Event/TofEvent.cpp +++ b/Framework/Types/src/Event/TofEvent.cpp @@ -12,13 +12,6 @@ bool TofEvent::operator==(const TofEvent &rhs) const { return (this->m_tof == rhs.m_tof) && (this->m_pulsetime == rhs.m_pulsetime); } -/** < comparison operator, using the TOF to do the comparison. - * @param rhs: the other TofEvent to compare. - * @return true if this->m_tof < rhs.m_tof*/ -bool TofEvent::operator<(const TofEvent &rhs) const { - return (this->m_tof < rhs.m_tof); -} - /** < comparison operator, using the TOF to do the comparison. * @param rhs: the other TofEvent to compare. * @return true if this->m_tof < rhs.m_tof*/ @@ -26,13 +19,6 @@ bool TofEvent::operator>(const TofEvent &rhs) const { return (this->m_tof > rhs.m_tof); } -/** < comparison operator, using the TOF to do the comparison. - * @param rhs_tof: the other time of flight to compare. - * @return true if this->m_tof < rhs.m_tof*/ -bool TofEvent::operator<(const double rhs_tof) const { - return (this->m_tof < rhs_tof); -} - /** * Compare two events within the specified tolerance *