Skip to content

Commit

Permalink
Merge pull request #38121 from mantidproject/remove-prompt-pulse
Browse files Browse the repository at this point in the history
Fix for Remove prompt pulse bug into ornl-next
  • Loading branch information
dmitry-ganyushin authored Oct 1, 2024
2 parents 91b9a55 + 1cf5781 commit 75614ab
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class MANTID_ALGORITHMS_DLL RemovePromptPulse : public API::Algorithm {
/// Try to get the frequency from a given name.
double getFrequency(const API::Run &run);
void getTofRange(const API::MatrixWorkspace_const_sptr &wksp, double &tmin, double &tmax);
std::vector<double> calculatePulseTimes(const double tmin, const double tmax, const double period);
std::vector<double> calculatePulseTimes(const double tmin, const double tmax, const double period,
const double width);
};

} // namespace Algorithms
Expand Down
10 changes: 7 additions & 3 deletions Framework/Algorithms/src/RemovePromptPulse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void RemovePromptPulse::exec() {
g_log.information() << "Data tmin=" << tmin << ", tmax=" << tmax << ", period=" << period << " microseconds\n";

// calculate the times for the prompt pulse
std::vector<double> pulseTimes = this->calculatePulseTimes(tmin, tmax, period);
std::vector<double> pulseTimes = this->calculatePulseTimes(tmin, tmax, period, width);
if (pulseTimes.empty()) {
g_log.notice() << "Not applying filter since prompt pulse is not in data "
"range (period = "
Expand Down Expand Up @@ -198,13 +198,17 @@ double RemovePromptPulse::getFrequency(const API::Run &run) {
* @param tmin The minimum time-of-flight measured.
* @param tmax The maximum time-of-flight measured.
* @param period The accelerator period.
* @param width The width of the time of flight (in microseconds) to remove from the data.
* @return A vector of all prompt pulse times possible within the time-of-flight
* range.
*/
std::vector<double> RemovePromptPulse::calculatePulseTimes(const double tmin, const double tmax, const double period) {
std::vector<double> RemovePromptPulse::calculatePulseTimes(const double tmin, const double tmax, const double period,
const double width) {
std::vector<double> times;
double time = 0.;

// zero pulse should be taken into account
if (tmin > 0 && tmin < width)
times.emplace_back(time);
// find when the first prompt pulse would be
while (time < tmin)
time += period;
Expand Down
81 changes: 77 additions & 4 deletions Framework/Algorithms/test/RemovePromptPulseTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ class RemovePromptPulseTest : public CxxTest::TestSuite {
std::string inWSName;
std::string outWSName;

void makeFakeEventWorkspace(const std::string &wsName) {
void makeFakeEventWorkspace(const std::string &wsName, int shift = 0) {
// Make an event workspace with 2 events in each bin.
EventWorkspace_sptr test_in =
WorkspaceCreationHelper::createEventWorkspace(NUMPIXELS, NUMBINS, NUMEVENTS, 1000., BIN_DELTA, 2);
// Fake a TOF unit in the data.
test_in->getAxis(0)->unit() = UnitFactory::Instance().create("TOF");
for (size_t i = 0; i < test_in->dataX(0).size(); ++i) {
test_in->dataX(0)[i] += shift;
}
test_in->setInstrument(ComponentCreationHelper::createTestInstrumentCylindrical(NUMPIXELS / 9));
// Make sure the detector IDs are ok
for (int i = 0; i < NUMPIXELS; i++)
Expand Down Expand Up @@ -92,14 +95,16 @@ class RemovePromptPulseTest : public CxxTest::TestSuite {
return;

// Verify the results
// drop events 36 spectra, 2 events/bin, 10 bins/pulse, 9 pulses
TS_ASSERT_EQUALS(num_events - 36 * 2 * 10 * 9, ws->getNumberEvents());
// drop events 36 spectra, 2 events/bin, 10 bins/pulse, 10 pulses
TS_ASSERT_EQUALS(num_events - 36 * 2 * 10 * 10, ws->getNumberEvents());

std::cout << "num_events = " << ws->getNumberEvents() << std::endl;

AnalysisDataService::Instance().remove(inWSName);
AnalysisDataService::Instance().remove(outWSName);
}

void test_exec_hit() {
void test_exec_hit_tmin() {
inWSName = "RemovePromptPulseTest_InputWS_hit";
outWSName = inWSName; //"RemovePromptPulseTest_OutputWS_hit";
this->makeFakeEventWorkspace(inWSName);
Expand Down Expand Up @@ -131,4 +136,72 @@ class RemovePromptPulseTest : public CxxTest::TestSuite {
// Clean up
AnalysisDataService::Instance().remove(inWSName);
}

void test_exec_miss_tmin() {
inWSName = "RemovePromptPulseTest_InputWS_miss_tmin";
outWSName = "RemovePromptPulseTest_OutputWS_miss_tmin";
this->makeFakeEventWorkspace(inWSName, -1100);

EventWorkspace_sptr ws;
TS_ASSERT_THROWS_NOTHING(ws = AnalysisDataService::Instance().retrieveWS<EventWorkspace>(inWSName));
std::size_t num_events = ws->getNumberEvents();
RemovePromptPulse alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("InputWorkspace", inWSName));
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("OutputWorkspace", outWSName));
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("Width", "1000."));
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("Frequency", "100"));
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("TMin", "-100"));

TS_ASSERT_THROWS_NOTHING(alg.execute(););
TS_ASSERT(alg.isExecuted());

// Retrieve the workspace from data service.
TS_ASSERT_THROWS_NOTHING(ws = AnalysisDataService::Instance().retrieveWS<EventWorkspace>(outWSName));
TS_ASSERT(ws);
if (!ws)
return;

// Verify the results
// drop events 36 spectra, 2 events/bin, 10 bins/pulse, 10 pulses
TS_ASSERT_EQUALS(num_events - 36 * 2 * 10 * 10, ws->getNumberEvents());

AnalysisDataService::Instance().remove(inWSName);
AnalysisDataService::Instance().remove(outWSName);
}

void test_exec_hit_negative_tmin() {
inWSName = "RemovePromptPulseTest_InputWS_tmin";
outWSName = inWSName;
this->makeFakeEventWorkspace(inWSName, -1100);

EventWorkspace_sptr ws;
TS_ASSERT_THROWS_NOTHING(ws = AnalysisDataService::Instance().retrieveWS<EventWorkspace>(inWSName));
std::size_t num_events = ws->getNumberEvents();

RemovePromptPulse alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("InputWorkspace", inWSName));
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("OutputWorkspace", outWSName));
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("Width", "1000."));
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("Frequency", "200"));
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("TMin", "-100"));

TS_ASSERT_THROWS_NOTHING(alg.execute(););
TS_ASSERT(alg.isExecuted());

// Retrieve the workspace from data service. TODO: Change to your desired
// type
TS_ASSERT_THROWS_NOTHING(ws = AnalysisDataService::Instance().retrieveWS<EventWorkspace>(outWSName));
TS_ASSERT(ws);
if (!ws)
return;

// Verify the results
TS_ASSERT(num_events > ws->getNumberEvents()); // should drop events
// Clean up
AnalysisDataService::Instance().remove(inWSName);
}
};
4 changes: 2 additions & 2 deletions Framework/WorkflowAlgorithms/test/AlignAndFocusPowderTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -786,9 +786,9 @@ class AlignAndFocusPowderTest : public CxxTest::TestSuite {
if (m_compressStartTime != "0")
align_and_focus.setProperty("CompressStartTime", m_compressStartTime);

// Remove prompt pulse; will cutoff last 6 long-TOF peaks (freq is 200 Hz)
// Remove prompt pulse; will cutoff the first peak from 6 long-TOF peaks (freq is 200 Hz)
if (m_removePromptPulse)
align_and_focus.setProperty("RemovePromptPulseWidth", 1e4);
align_and_focus.setProperty("RemovePromptPulseWidth", 2200.0);

// Filter absorption resonances - default unit is wavelength
align_and_focus.setPropertyValue("ResonanceFilterLowerLimits", m_filterResonanceLower);
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
04a9f44a284a43beb22647d4e7f3ad6e
abedd395d477ae260a181debe077bdb1
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bd4e49926e46e2c516a0ca4e2e1cff2b
6c2cfb5a54df48ed2644fb1ed1a380f7
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1b7d2fd46c0a8db3ee057405ecef9045
1987860075f1fbeddcc408e09dc3208f
Original file line number Diff line number Diff line change
@@ -1 +1 @@
f16afe97ddec63a0b275ee11bbfb1977
21df458d01d9851fd0e0c436a50a9c4d
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- :ref:`RemovePromptPulse <algm-RemovePromptPulse>` has been fixed to correctly account for the first pulse.

0 comments on commit 75614ab

Please sign in to comment.