Commit 242bbf25 authored by Matt Cumber's avatar Matt Cumber
Browse files

Re # 29025 Multiple Time Zeros

Address changes requested. Clean up unit tests and split some code logic to be more readable
parent 870c6128
......@@ -181,7 +181,7 @@ void LoadMuonNexusV2::execLoader() {
// Time Zero table should be returned if found
if (!getPropertyValue("TimeZerotable").empty()) {
// Create table and set property]
// Create table and set property
auto timeZeroTable = m_loadMuonStrategy->getTimeZeroTable();
setProperty("TimeZeroTable", timeZeroTable);
}
......
......@@ -20,7 +20,7 @@ namespace DataHandling {
/**
* Creates a timezero table for the loaded detectors
* @param numSpec :: Numer of spectra (number of rows in table)
* @param numSpec :: Number of spectra (number of rows in table)
* @param timeZeros :: Vector containing time zero values for each spectra
* @return TableWorkspace of time zeros
*/
......
......@@ -128,25 +128,20 @@ public:
LoadMuonNexusV2 ld;
ld.initialize();
ld.setPropertyValue("Filename", "EMU00102347.nxs_v2");
ld.setPropertyValue("OutputWorkspace", "outWS");
ld.setPropertyValue("OutputWorkspace", "outWs");
ld.setPropertyValue("TimeZeroTable", "tzt");
ld.setRethrows(true);
auto &ads = AnalysisDataService::Instance();
TS_ASSERT_THROWS_NOTHING(ld.execute());
TS_ASSERT(ld.isExecuted());
// Verify that the output workspace exists
MatrixWorkspace_sptr output_ws;
TS_ASSERT_THROWS_NOTHING(
output_ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(
"outWS"));
ld.execute();
// Verify that the output workspaces exist
TS_ASSERT(ads.doesExist("outWs"));
TS_ASSERT(ads.doesExist("tzt"));
Workspace2D_sptr output2D =
std::dynamic_pointer_cast<Workspace2D>(output_ws);
Workspace2D_sptr output2D = std::dynamic_pointer_cast<Workspace2D>(
ads.retrieveWS<MatrixWorkspace>("outWs"));
TableWorkspace_sptr tbl = ads.retrieveWS<TableWorkspace>("tzt");
TableWorkspace_sptr tbl;
TS_ASSERT_THROWS_NOTHING(
tbl =
AnalysisDataService::Instance().retrieveWS<TableWorkspace>("tzt"));
TS_ASSERT(tbl);
// Check number of rows and columns
TS_ASSERT_EQUALS(tbl->columnCount(), 1);
TS_ASSERT_EQUALS(tbl->rowCount(), output2D->getNumberHistograms());
......
......@@ -194,7 +194,6 @@ public:
void test_time_zero_list_loaded_correctly() {
LoadPSIMuonBin alg;
alg.initialize();
alg.isInitialized();
alg.setProperty("SearchForTempFile", false);
alg.setProperty("Filename", getTestFilePath("deltat_tdc_dolly_1529.bin"));
......@@ -214,20 +213,15 @@ public:
void test_time_zero_table_loaded_correctly() {
LoadPSIMuonBin alg;
alg.initialize();
alg.isInitialized();
alg.setProperty("SearchForTempFile", false);
alg.setProperty("Filename", getTestFilePath("deltat_tdc_dolly_1529.bin"));
alg.setProperty("OutputWorkspace", "ws");
alg.setPropertyValue("TimeZeroTable", "tzt");
alg.execute();
ITableWorkspace_sptr tbl;
TS_ASSERT_THROWS_NOTHING(
tbl =
AnalysisDataService::Instance().retrieveWS<TableWorkspace>("tzt"));
auto &ads = AnalysisDataService::Instance();
ITableWorkspace_sptr tbl = ads.retrieveWS<TableWorkspace>("tzt");
TS_ASSERT(tbl);
TS_ASSERT_EQUALS(tbl->columnCount(), 1);
TS_ASSERT_EQUALS(tbl->getColumnNames(),
std::vector<std::string>{"time zero"});
......@@ -241,7 +235,6 @@ public:
void test_dead_time_table_loaded_correctly() {
LoadPSIMuonBin alg;
alg.initialize();
alg.isInitialized();
alg.setProperty("SearchForTempFile", false);
alg.setProperty("Filename", getTestFilePath("deltat_tdc_dolly_1529.bin"));
......@@ -249,12 +242,9 @@ public:
alg.setPropertyValue("DeadTimeTable", "dtt");
alg.execute();
ITableWorkspace_sptr tbl;
TS_ASSERT_THROWS_NOTHING(
tbl =
AnalysisDataService::Instance().retrieveWS<TableWorkspace>("dtt"));
auto &ads = AnalysisDataService::Instance();
ITableWorkspace_sptr tbl = ads.retrieveWS<TableWorkspace>("dtt");
TS_ASSERT(tbl);
TS_ASSERT_EQUALS(tbl->columnCount(), 2);
std::vector<std::string> colNames{"spectrum", "dead-time"};
TS_ASSERT_EQUALS(tbl->getColumnNames(), colNames);
......
......@@ -71,8 +71,20 @@ private:
/// Perform validation of inputs to the algorithm
std::map<std::string, std::string> validateInputs() override;
/// Validates the tables used in the alg (called in validateInputs)
void validateTableInputs(std::map<std::string, std::string> &errors);
/// Allow WorkspaceGroup property to function correctly.
bool checkGroups() override;
/// Crop workspace with single xMin and xMax values
MatrixWorkspace_sptr cropWithSingleValues(MatrixWorkspace_sptr ws,
const double &xMin,
const double &xMax);
/// Crop workspace with vector of doubles
MatrixWorkspace_sptr cropWithVectors(MatrixWorkspace_sptr ws,
const double &xMin, const double &xMax);
};
} // namespace Muon
......
......@@ -87,21 +87,8 @@ void MuonPreProcess::init() {
setPropertyGroup("DeadTimeTable", analysisGrp);
}
std::map<std::string, std::string> MuonPreProcess::validateInputs() {
std::map<std::string, std::string> errors;
double tmin = this->getProperty("TimeMin");
double tmax = this->getProperty("TimeMax");
if (tmin != EMPTY_DBL() && tmax != EMPTY_DBL()) {
if (tmin > tmax) {
errors["TimeMin"] = "TimeMin > TimeMax";
}
if (tmin != EMPTY_DBL() && tmin == tmax) {
errors["TimeMin"] = "TimeMin and TimeMax must be different";
}
}
// Checks for dead time table and time zero table
void MuonPreProcess::validateTableInputs(
std::map<std::string, std::string> &errors) {
Workspace_sptr inputWS = this->getProperty("InputWorkspace");
if (auto ws = std::dynamic_pointer_cast<MatrixWorkspace>(inputWS)) {
// Dead time
......@@ -123,7 +110,26 @@ std::map<std::string, std::string> MuonPreProcess::validateInputs() {
}
}
}
}
std::map<std::string, std::string> MuonPreProcess::validateInputs() {
std::map<std::string, std::string> errors;
double tmin = this->getProperty("TimeMin");
double tmax = this->getProperty("TimeMax");
if (tmin != EMPTY_DBL() && tmax != EMPTY_DBL()) {
if (tmin > tmax) {
errors["TimeMin"] = "TimeMin > TimeMax";
}
if (tmin != EMPTY_DBL() && tmin == tmax) {
errors["TimeMin"] = "TimeMin and TimeMax must be different";
}
}
// Check for and validate dead time and time zero tables
validateTableInputs(errors);
Workspace_sptr inputWS = this->getProperty("InputWorkspace");
if (auto ws = std::dynamic_pointer_cast<WorkspaceGroup>(inputWS)) {
if (ws->getNumberOfEntries() == 0) {
errors["InputWorkspace"] = "Input WorkspaceGroup is empty.";
......@@ -239,14 +245,15 @@ MatrixWorkspace_sptr
MuonPreProcess::applyTimeZeroTable(MatrixWorkspace_sptr ws,
const TableWorkspace_sptr &timeZeroTable) {
auto cloneWs = cloneWorkspace(ws);
if (timeZeroTable != nullptr) {
const auto numSpec = cloneWs->getNumberHistograms();
for (auto specNum = 0u; specNum < numSpec; ++specNum) {
auto &xData = cloneWs->mutableX(specNum);
for (auto &xValue : xData) {
API::TableRow row = timeZeroTable->getRow(specNum);
xValue -= row.Double(0);
}
if (!timeZeroTable) {
return cloneWs;
}
const auto numSpec = cloneWs->getNumberHistograms();
for (auto specNum = 0u; specNum < numSpec; ++specNum) {
auto &xData = cloneWs->mutableX(specNum);
for (auto &xValue : xData) {
API::TableRow row = timeZeroTable->getRow(specNum);
xValue -= row.Double(0);
}
}
return cloneWs;
......@@ -255,51 +262,59 @@ MuonPreProcess::applyTimeZeroTable(MatrixWorkspace_sptr ws,
MatrixWorkspace_sptr MuonPreProcess::applyCropping(MatrixWorkspace_sptr ws,
const double &xMin,
const double &xMax) {
if (xMin != EMPTY_DBL() || xMax != EMPTY_DBL()) {
if (!getPropertyValue("TimeZeroTable").empty()) {
IAlgorithm_sptr cropRagged = createChildAlgorithm("CropWorkspaceRagged");
cropRagged->setProperty("InputWorkspace", ws);
if (xMin != EMPTY_DBL()) {
std::vector<double> xMinVec(ws->getNumberHistograms(), xMin);
cropRagged->setProperty("XMin", xMinVec);
} else {
// Get min value from each spectra independantly
std::vector<double> xMinVec;
for (auto specNum = 0u; specNum < ws->getNumberHistograms();
++specNum) {
auto &xData = ws->mutableX(specNum);
xMinVec.emplace_back(xData[0]);
}
cropRagged->setProperty("XMin", xMinVec);
}
if (xMax != EMPTY_DBL()) {
std::vector<double> xMaxVec(ws->getNumberHistograms(), xMax);
cropRagged->setProperty("XMax", xMaxVec);
} else {
// Get max value from each spectra independantly
std::vector<double> xMaxVec;
for (auto specNum = 0u; specNum < ws->getNumberHistograms();
++specNum) {
auto &xData = ws->mutableX(specNum);
xMaxVec.emplace_back(xData[xData.size() - 1]);
}
cropRagged->setProperty("XMax", xMaxVec);
}
cropRagged->execute();
return cropRagged->getProperty("OutputWorkspace");
} else {
IAlgorithm_sptr crop = createChildAlgorithm("CropWorkspace");
crop->setProperty("InputWorkspace", ws);
if (xMin != EMPTY_DBL())
crop->setProperty("Xmin", xMin);
if (xMax != EMPTY_DBL())
crop->setProperty("Xmax", xMax);
crop->execute();
return crop->getProperty("OutputWorkspace");
}
} else {
if (xMin == EMPTY_DBL() && xMax == EMPTY_DBL())
return ws;
if (getPropertyValue("TimeZeroTable").empty())
return cropWithSingleValues(ws, xMin, xMax);
else
return cropWithVectors(ws, xMin, xMax);
}
MatrixWorkspace_sptr
MuonPreProcess::cropWithSingleValues(MatrixWorkspace_sptr ws,
const double &xMin, const double &xMax) {
IAlgorithm_sptr crop = createChildAlgorithm("CropWorkspace");
crop->setProperty("InputWorkspace", ws);
if (xMin != EMPTY_DBL())
crop->setProperty("Xmin", xMin);
if (xMax != EMPTY_DBL())
crop->setProperty("Xmax", xMax);
crop->execute();
return crop->getProperty("OutputWorkspace");
}
MatrixWorkspace_sptr MuonPreProcess::cropWithVectors(MatrixWorkspace_sptr ws,
const double &xMin,
const double &xMax) {
std::vector<double> xMinVec;
std::vector<double> xMaxVec;
if (xMin != EMPTY_DBL())
xMinVec.insert(xMinVec.end(), ws->getNumberHistograms(), xMin);
else {
for (auto specNum = 0u; specNum < ws->getNumberHistograms(); ++specNum) {
auto &xData = ws->mutableX(specNum);
xMinVec.emplace_back(xData[0]); // Append first value for each spectrum
}
}
if (xMax != EMPTY_DBL())
xMaxVec.insert(xMaxVec.end(), ws->getNumberHistograms(), xMax);
else {
for (auto specNum = 0u; specNum < ws->getNumberHistograms(); ++specNum) {
auto &xData = ws->mutableX(specNum);
xMaxVec.emplace_back(
xData[xData.size() - 1]); // Append last value for each spectrum
}
}
IAlgorithm_sptr cropRagged = createChildAlgorithm("CropWorkspaceRagged");
cropRagged->setProperty("InputWorkspace", ws);
cropRagged->setProperty("XMin", xMinVec);
cropRagged->setProperty("XMax", xMaxVec);
cropRagged->execute();
return cropRagged->getProperty("OutputWorkspace");
}
MatrixWorkspace_sptr
......
......@@ -233,7 +233,7 @@ public:
// --------------------------------------------------------------------------
void test_successful_execution_with_valid_time_zero_table() {
// workspace has 2 spectra, dead time table has 5 rows
// workspace has 5 spectra, time zero table has 5 rows
auto ws = createCountsWorkspace(5, 10, 0.0);
std::vector<double> timeZeros = {0.5, 1.0, 1.5, 2.0, 2.5};
ITableWorkspace_sptr timeZeroTable = createTimeZeroTable(5, timeZeros);
......@@ -244,7 +244,7 @@ public:
}
void test_cannot_execute_on_invalid_time_zero_table() {
// workspace has 2 spectra, dead time table has 5 rows
// workspace has 2 spectra, time zero table has 5 rows
auto ws = createCountsWorkspace(2, 10, 0.0);
std::vector<double> timeZeros = {0.5, 1.0, 1.5, 2.0, 2.5};
ITableWorkspace_sptr timeZeroTable = createTimeZeroTable(5, timeZeros);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment