diff --git a/Framework/DataObjects/src/EventList.cpp b/Framework/DataObjects/src/EventList.cpp index 90c811d08f185085ad4418e0c895f643d52bbf99..0688b1d53e46478b590735a41fb26c14ecc9a112 100644 --- a/Framework/DataObjects/src/EventList.cpp +++ b/Framework/DataObjects/src/EventList.cpp @@ -29,17 +29,18 @@ const size_t NUM_EVENTS_PARALLEL_THRESHOLD = 500000; /** * Calculate the corrected full time in nanoseconds - * @param totalNanoseconds : Time in nanoseconds - * @param tof : Time of flight + * @param event : The event with pulse time and time-of-flight * @param tofFactor : Time of flight coefficient factor * @param tofShift : Tof shift in seconds * @return Corrected full time at sample in Nanoseconds. */ -int64_t calculateCorrectedFullTime(const int64_t &totalNanoseconds, - const double &tof, const double &tofFactor, - const double &tofShift) { - return totalNanoseconds + - static_cast<int64_t>(tofFactor * (tof * 1.0E3) + (tofShift * 1.0E9)); +template <typename EventType> +int64_t calculateCorrectedFullTime(const EventType &event, + const double tofFactor, + const double tofShift) { + return event.pulseTime().totalNanoseconds() + + static_cast<int64_t>(tofFactor * (event.tof() * 1.0E3) + + (tofShift * 1.0E9)); } /** @@ -53,7 +54,7 @@ private: const double m_tofShift; public: - CompareTimeAtSample(const double &tofFactor, const double &tofShift) + CompareTimeAtSample(const double tofFactor, const double tofShift) : m_tofFactor(tofFactor), m_tofShift(tofShift) {} /** @@ -66,10 +67,10 @@ public: * @return True if first event evaluates to be < second event, otherwise false */ bool operator()(const EventType &e1, const EventType &e2) const { - const auto tAtSample1 = calculateCorrectedFullTime( - e1.pulseTime().totalNanoseconds(), e1.tof(), m_tofFactor, m_tofShift); - const auto tAtSample2 = calculateCorrectedFullTime( - e2.pulseTime().totalNanoseconds(), e2.tof(), m_tofFactor, m_tofShift); + const auto tAtSample1 = + calculateCorrectedFullTime(e1, m_tofFactor, m_tofShift); + const auto tAtSample2 = + calculateCorrectedFullTime(e2, m_tofFactor, m_tofShift); return (tAtSample1 < tAtSample2); } }; @@ -1875,7 +1876,8 @@ EventList::findFirstPulseEvent(const std::vector<T> &events, // if tof < X[0], that means that you need to skip some events while ((itev != itev_end) && - (itev->pulseTime().totalNanoseconds() < seek_pulsetime)) + (static_cast<double>(itev->pulseTime().totalNanoseconds()) < + seek_pulsetime)) itev++; // Better fix would be to use a binary search instead of the linear one used // here. @@ -1903,10 +1905,8 @@ typename std::vector<T>::const_iterator EventList::findFirstTimeAtSampleEvent( 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) && - (calculateCorrectedFullTime(itev->pulseTime().totalNanoseconds(), - itev->tof(), tofFactor, - tofOffset) < seek_time)) + while ((itev != itev_end) && (static_cast<double>(calculateCorrectedFullTime( + *itev, tofFactor, tofOffset)) < seek_time)) itev++; // Better fix would be to use a binary search instead of the linear one used // here. @@ -2271,7 +2271,7 @@ void EventList::generateCountsHistogramTimeAtSample( const MantidVec &X, MantidVec &Y, const double &tofFactor, const double &tofOffset) const { // For slight speed=up. - size_t x_size = X.size(); + const size_t x_size = X.size(); if (x_size <= 1) { // X was not set. Return an empty array. @@ -2301,9 +2301,8 @@ void EventList::generateCountsHistogramTimeAtSample( // Find the first bin size_t bin = 0; - int64_t tAtSample = - calculateCorrectedFullTime(itev->pulseTime().totalNanoseconds(), - itev->tof(), tofFactor, tofOffset); + double tAtSample = static_cast<double>( + calculateCorrectedFullTime(*itev, tofFactor, tofOffset)); while (bin < x_size - 1) { // Within range? if ((tAtSample >= X[bin]) && (tAtSample < X[bin + 1])) { @@ -2317,9 +2316,8 @@ void EventList::generateCountsHistogramTimeAtSample( // Keep going through all the events while ((itev != itev_end) && (bin < x_size - 1)) { - tAtSample = - calculateCorrectedFullTime(itev->pulseTime().totalNanoseconds(), - itev->tof(), tofFactor, tofOffset); + tAtSample = static_cast<double>( + calculateCorrectedFullTime(*itev, tofFactor, tofOffset)); while (bin < x_size - 1) { // Within range? if ((tAtSample >= X[bin]) && (tAtSample < X[bin + 1])) { @@ -3206,17 +3204,14 @@ DateAndTime EventList::getTimeAtSampleMax(const double &tofFactor, if (this->order == TIMEATSAMPLE_SORT) { switch (eventType) { case TOF: - return calculateCorrectedFullTime( - this->events.rbegin()->pulseTime().totalNanoseconds(), - this->events.rbegin()->tof(), tofFactor, tofOffset); + return calculateCorrectedFullTime(*(this->events.rbegin()), tofFactor, + tofOffset); case WEIGHTED: - return calculateCorrectedFullTime( - this->weightedEvents.rbegin()->pulseTime().totalNanoseconds(), - this->weightedEvents.rbegin()->tof(), tofFactor, tofOffset); + return calculateCorrectedFullTime(*(this->weightedEvents.rbegin()), + tofFactor, tofOffset); case WEIGHTED_NOTIME: - return calculateCorrectedFullTime( - this->weightedEventsNoTime.rbegin()->pulseTime().totalNanoseconds(), - this->weightedEventsNoTime.rbegin()->tof(), tofFactor, tofOffset); + return calculateCorrectedFullTime(*(this->weightedEventsNoTime.rbegin()), + tofFactor, tofOffset); } } @@ -3226,19 +3221,15 @@ DateAndTime EventList::getTimeAtSampleMax(const double &tofFactor, for (size_t i = 0; i < numEvents; i++) { switch (eventType) { case TOF: - temp = calculateCorrectedFullTime( - this->events[i].pulseTime().totalNanoseconds(), this->events[i].tof(), - tofFactor, tofOffset); + temp = calculateCorrectedFullTime(this->events[i], tofFactor, tofOffset); break; case WEIGHTED: - temp = calculateCorrectedFullTime( - this->weightedEvents[i].pulseTime().totalNanoseconds(), - this->weightedEvents[i].tof(), tofFactor, tofOffset); + temp = calculateCorrectedFullTime(this->weightedEvents[i], tofFactor, + tofOffset); break; case WEIGHTED_NOTIME: - temp = calculateCorrectedFullTime( - this->weightedEventsNoTime[i].pulseTime().totalNanoseconds(), - this->weightedEventsNoTime[i].tof(), tofFactor, tofOffset); + temp = calculateCorrectedFullTime(this->weightedEventsNoTime[i], + tofFactor, tofOffset); break; } if (temp > tMax) @@ -3260,17 +3251,14 @@ DateAndTime EventList::getTimeAtSampleMin(const double &tofFactor, if (this->order == TIMEATSAMPLE_SORT) { switch (eventType) { case TOF: - return calculateCorrectedFullTime( - this->events.begin()->pulseTime().totalNanoseconds(), - this->events.begin()->tof(), tofFactor, tofOffset); + return calculateCorrectedFullTime(*(this->events.begin()), tofFactor, + tofOffset); case WEIGHTED: - return calculateCorrectedFullTime( - this->weightedEvents.begin()->pulseTime().totalNanoseconds(), - this->weightedEvents.begin()->tof(), tofFactor, tofOffset); + return calculateCorrectedFullTime(*(this->weightedEvents.begin()), + tofFactor, tofOffset); case WEIGHTED_NOTIME: - return calculateCorrectedFullTime( - this->weightedEventsNoTime.begin()->pulseTime().totalNanoseconds(), - this->weightedEventsNoTime.begin()->tof(), tofFactor, tofOffset); + return calculateCorrectedFullTime(*(this->weightedEventsNoTime.begin()), + tofFactor, tofOffset); } } @@ -3280,19 +3268,15 @@ DateAndTime EventList::getTimeAtSampleMin(const double &tofFactor, for (size_t i = 0; i < numEvents; i++) { switch (eventType) { case TOF: - temp = calculateCorrectedFullTime( - this->events[i].pulseTime().totalNanoseconds(), this->events[i].tof(), - tofFactor, tofOffset); + temp = calculateCorrectedFullTime(this->events[i], tofFactor, tofOffset); break; case WEIGHTED: - temp = calculateCorrectedFullTime( - this->weightedEvents[i].pulseTime().totalNanoseconds(), - this->weightedEvents[i].tof(), tofFactor, tofOffset); + temp = calculateCorrectedFullTime(this->weightedEvents[i], tofFactor, + tofOffset); break; case WEIGHTED_NOTIME: - temp = calculateCorrectedFullTime( - this->weightedEventsNoTime[i].pulseTime().totalNanoseconds(), - this->weightedEventsNoTime[i].tof(), tofFactor, tofOffset); + temp = calculateCorrectedFullTime(this->weightedEventsNoTime[i], + tofFactor, tofOffset); break; } if (temp < tMin) @@ -3798,15 +3782,13 @@ void EventList::filterByTimeAtSampleHelper(std::vector<T> &events, auto itev_end = events.end(); // Find the first event with m_pulsetime >= start while ((itev != itev_end) && - (calculateCorrectedFullTime(itev->m_pulsetime.totalNanoseconds(), - itev->tof(), tofFactor, - tofOffset) < start.totalNanoseconds())) + (calculateCorrectedFullTime(*itev, tofFactor, tofOffset) < + start.totalNanoseconds())) itev++; while ((itev != itev_end) && - (calculateCorrectedFullTime(itev->m_pulsetime.totalNanoseconds(), - itev->tof(), tofFactor, - tofOffset) < stop.totalNanoseconds())) { + (calculateCorrectedFullTime(*itev, tofFactor, tofOffset) < + stop.totalNanoseconds())) { // Add the copy to the output output.push_back(*itev); ++itev; @@ -4141,9 +4123,7 @@ void EventList::splitByFullTimeHelper(Kernel::TimeSplitterType &splitter, while (itev != itev_end) { int64_t fulltime; if (docorrection) - fulltime = - calculateCorrectedFullTime(itev->m_pulsetime.totalNanoseconds(), - itev->m_tof, toffactor, tofshift); + fulltime = calculateCorrectedFullTime(*itev, toffactor, tofshift); else fulltime = itev->m_pulsetime.totalNanoseconds() + static_cast<int64_t>(itev->m_tof * 1000); diff --git a/Framework/Kernel/src/DateAndTime.cpp b/Framework/Kernel/src/DateAndTime.cpp index 02f4c1b7f0927ae3d1c959d552f43eff3d807482..a64a2c1cb98005fa746768919f734fc60e6f1e5d 100644 --- a/Framework/Kernel/src/DateAndTime.cpp +++ b/Framework/Kernel/src/DateAndTime.cpp @@ -171,11 +171,12 @@ DateAndTime::DateAndTime(const boost::posix_time::ptime _ptime) * @param nanoseconds :: nanoseconds to add to the number of seconds */ DateAndTime::DateAndTime(const double seconds, const double nanoseconds) { - double nano = seconds * 1e9 + nanoseconds; - // Limit times - if (nano > MAX_NANOSECONDS) + double nano = seconds * 1.e9 + nanoseconds; + constexpr double minimum = static_cast<double>(MIN_NANOSECONDS); + constexpr double maximum = static_cast<double>(MAX_NANOSECONDS); + if (nano > maximum) _nanoseconds = MAX_NANOSECONDS; - else if (nano < MIN_NANOSECONDS) + else if (nano < minimum) _nanoseconds = MIN_NANOSECONDS; else _nanoseconds = static_cast<int64_t>(nano); @@ -837,11 +838,13 @@ time_duration DateAndTime::durationFromNanoseconds(int64_t dur) { * @return int64 of the number of nanoseconds */ int64_t DateAndTime::nanosecondsFromSeconds(double sec) { - double nano = sec * 1e9; + const double nano = sec * 1e9; + constexpr double minimum = static_cast<double>(MIN_NANOSECONDS); + constexpr double maximum = static_cast<double>(MAX_NANOSECONDS); // Use these limits to avoid integer overflows - if (nano > MAX_NANOSECONDS) + if (nano > maximum) return MAX_NANOSECONDS; - else if (nano < MIN_NANOSECONDS) + else if (nano < minimum) return MIN_NANOSECONDS; else return int64_t(nano);