Skip to content

Commit

Permalink
Merge pull request #38738 from rosswhitfield/fix_LoadErrorEventsNexus…
Browse files Browse the repository at this point in the history
…_zero_events

Fix LoadErrorEventsNexus/PulseIndexer when there are zero events
  • Loading branch information
peterfpeterson authored Jan 30, 2025
2 parents 7cfebde + 2da9fbc commit cba714b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
5 changes: 4 additions & 1 deletion Framework/DataHandling/src/LoadErrorEventsNexus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ void LoadErrorEventsNexus::exec() {
g_log.information() << "Loaded " << numEvents << " events with TOF min = " << min_tof << ", max = " << max_tof
<< "\n";

eventWS->setAllX(HistogramData::BinEdges{min_tof, max_tof});
if (min_tof < max_tof)
eventWS->setAllX(HistogramData::BinEdges{min_tof, max_tof});
else
eventWS->setAllX(HistogramData::BinEdges{0, 16666.7});

outWS->getAxis(0)->setUnit("TOF");
outWS->setYUnit("Counts");
Expand Down
6 changes: 3 additions & 3 deletions Framework/DataHandling/src/PulseIndexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ PulseIndexer::PulseIndexer(std::shared_ptr<std::vector<uint64_t>> event_index, c
// determine if should trim the front end to remove empty pulses
auto firstPulseIndex = m_roi.front();
auto eventRange = this->getEventIndexRange(firstPulseIndex);
while (eventRange.first == eventRange.second) {
while (eventRange.first == eventRange.second && eventRange.first < m_numEvents) {
++firstPulseIndex;
eventRange = this->getEventIndexRange(firstPulseIndex);
}

// determine if should trim the back end to remove empty pulses
auto lastPulseIndex = m_roi.back();
eventRange = this->getEventIndexRange(lastPulseIndex - 1);
while (eventRange.first == eventRange.second) {
while (eventRange.first == eventRange.second && eventRange.second > 0) {
--lastPulseIndex;
eventRange = this->getEventIndexRange(lastPulseIndex - 1);
}
Expand Down Expand Up @@ -146,7 +146,7 @@ size_t PulseIndexer::getLastPulseIndex() const { return m_roi.back(); }
std::pair<size_t, size_t> PulseIndexer::getEventIndexRange(const size_t pulseIndex) const {
const auto start = this->getStartEventIndex(pulseIndex);
// return early if the start is too big
if (start > m_numEvents)
if (start >= m_numEvents)
return std::make_pair(start, m_numEvents);

// get the end index
Expand Down
20 changes: 20 additions & 0 deletions Framework/DataHandling/test/LoadErrorEventsNexusTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@ class LoadErrorEventsNexusTest : public CxxTest::TestSuite {
TS_ASSERT_EQUALS(events[0].pulseTime(), Mantid::Types::Core::DateAndTime("2021-10-06T14:25:29.962441733-04:00"))
}

void test_CG3_zeroEvents() {
// This file contains no events in bank_error_events so should create an EventWorkspace with 0 events
LoadErrorEventsNexus alg;
alg.setChild(true);
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("OutputWorkspace", "unused"))
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("Filename", "CG3_960.nxs.h5"))
TS_ASSERT(alg.execute());

Mantid::DataObjects::EventWorkspace_sptr outputWS = alg.getProperty("OutputWorkspace");
TS_ASSERT(outputWS);

TS_ASSERT_EQUALS(outputWS->blocksize(), 1)
TS_ASSERT_EQUALS(outputWS->getNumberHistograms(), 1)
TS_ASSERT_EQUALS(outputWS->getNumberEvents(), 0)
TS_ASSERT_EQUALS(outputWS->readX(0)[0], 0)
TS_ASSERT_DELTA(outputWS->readX(0)[1], 16666.7, 1e-9)
}

void test_HYSA() {
// this should fail to load as bank_error_events does not exist in this file

Expand Down
13 changes: 13 additions & 0 deletions Framework/DataHandling/test/PulseIndexerTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,19 @@ class PulseIndexerTest : public CxxTest::TestSuite {
run_test(eventIndices, start_event_index, total_events, first_pulse_index);
}

void test_zerosEvents() {
// test that the pulse indexer can handle no events
auto eventIndices = std::make_shared<std::vector<uint64_t>>();
eventIndices->push_back(0);
eventIndices->push_back(0);

constexpr size_t start_event_index{0};
constexpr size_t total_events{0};
constexpr size_t first_pulse_index{1};

run_test(eventIndices, start_event_index, total_events, first_pulse_index);
}

void test_invalidPulseROI() {
auto eventIndices = generate_nonConstant();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fixed a bug in :ref:`LoadErrorEventsNexus <algm-LoadErrorEventsNexus>` that would cause it to hang when the error bank had zero events.

0 comments on commit cba714b

Please sign in to comment.