Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for SAScollimation in SaveCanSAS1D and LoadCanSAS1D #18486

Merged
merged 4 commits into from Jan 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 58 additions & 17 deletions Framework/DataHandling/src/LoadCanSAS1D.cpp
Expand Up @@ -381,26 +381,67 @@ void LoadCanSAS1D::createSampleInformation(
sasInstrumentElement->getChildElement("SAScollimation");
check(sasCollimationElement, "<SAScollimation>");

// Get the geometry information
auto geometryElement = sasCollimationElement->getChildElement("name");
if (geometryElement) {
auto geometry = geometryElement->innerText();
auto geometryID = getGeometryID(geometry);
sample.setGeometryFlag(geometryID);
// Since we have shipped a sligthly invalid CanSAS1D format we need to
// make sure that we can read those files back in again
bool isInValidOldFormat = true;
try {
auto name = sasCollimationElement->getChildElement("name");
check(name, "name");
} catch (Kernel::Exception::NotFoundError &) {
isInValidOldFormat = false;
}

// Get the thickness information
auto widthElement = sasCollimationElement->getChildElement("X");
if (widthElement) {
double width = std::stod(widthElement->innerText());
sample.setWidth(width);
}
if (isInValidOldFormat) {
// Get the geometry information
auto geometryElement = sasCollimationElement->getChildElement("name");
if (geometryElement) {
auto geometry = geometryElement->innerText();
auto geometryID = getGeometryID(geometry);
sample.setGeometryFlag(geometryID);
}

// Get the thickness information
auto heightElement = sasCollimationElement->getChildElement("Y");
if (heightElement) {
double height = std::stod(heightElement->innerText());
sample.setHeight(height);
// Get the width information
auto widthElement = sasCollimationElement->getChildElement("X");
if (widthElement) {
double width = std::stod(widthElement->innerText());
sample.setWidth(width);
}

// Get the height information
auto heightElement = sasCollimationElement->getChildElement("Y");
if (heightElement) {
double height = std::stod(heightElement->innerText());
sample.setHeight(height);
}

} else {
// Get aperture
auto aperture = sasCollimationElement->getChildElement("aperture");
if (aperture) {
// Get geometry element
auto geometry = aperture->getAttribute("name");
if (!geometry.empty()) {
auto geometryID = getGeometryID(Poco::XML::fromXMLString(geometry));
sample.setGeometryFlag(geometryID);
}

// Get size
auto size = aperture->getChildElement("size");

// Get the width information
auto widthElement = size->getChildElement("x");
if (widthElement) {
double width = std::stod(widthElement->innerText());
sample.setWidth(width);
}

// Get the height information
auto heightElement = size->getChildElement("y");
if (heightElement) {
double height = std::stod(heightElement->innerText());
sample.setHeight(height);
}
}
}
}
}
Expand Down
42 changes: 36 additions & 6 deletions Framework/DataHandling/src/SaveCanSAS1D.cpp
Expand Up @@ -638,10 +638,28 @@ void SaveCanSAS1D::createSASProcessElement(std::string &sasProcess) {
}

/** This method creates an XML element named "SASinstrument"
*
* The structure for required(r) parts of the SASinstrument and the parts
* we want to add is:
*
* SASinstrument
* name(r)
* SASsource(r)
* radiation(r)
* SAScollimation(r)
* aperature[name]
* size
* x[units]
* y[units]
* SASdetector
* name
*
* @param sasInstrument :: string for sasinstrument element in the xml
*/
void SaveCanSAS1D::createSASInstrument(std::string &sasInstrument) {
sasInstrument = "\n\t\t<SASinstrument>";

// Set name(r)
std::string sasInstrName = "\n\t\t\t<name>";
std::string instrname = m_workspace->getInstrument()->getName();
// look for xml special characters and replace with entity refrence
Expand All @@ -650,30 +668,42 @@ void SaveCanSAS1D::createSASInstrument(std::string &sasInstrument) {
sasInstrName += "</name>";
sasInstrument += sasInstrName;

// Set SASsource(r)
std::string sasSource;
createSASSourceElement(sasSource);
sasInstrument += sasSource;

// Set SAScollimation(r)
// Add the collimation. We add the collimation information if
// either the width of the height is different from 0
double collimationHeight = getProperty("SampleHeight");
double collimationWidth = getProperty("SampleWidth");
std::string sasCollimation = "\n\t\t\t<SAScollimation/>";
if (collimationHeight > 0 || collimationWidth > 0) {
sasCollimation = "\n\t\t\t<SAScollimation>";
// Geometry

// aperture with name
std::string collimationGeometry = getProperty("Geometry");
sasCollimation += "\n\t\t\t\t<name>" + collimationGeometry + "</name>";
// Width
sasCollimation +=
"\n\t\t\t\t<X unit=\"mm\">" + std::to_string(collimationWidth) + "</X>";
"\n\t\t\t\t<aperture name=\"" + collimationGeometry + "\">";

// size
sasCollimation += "\n\t\t\t\t\t<size>";

// Width
sasCollimation += "\n\t\t\t\t\t\t<x unit=\"mm\">" +
std::to_string(collimationWidth) + "</x>";
// Height
sasCollimation += "\n\t\t\t\t<Y unit=\"mm\">" +
std::to_string(collimationHeight) + "</Y>";
sasCollimation += "\n\t\t\t\t\t\t<y unit=\"mm\">" +
std::to_string(collimationHeight) + "</y>";

sasCollimation += "\n\t\t\t\t\t</size>";
sasCollimation += "\n\t\t\t\t</aperture>";
sasCollimation += "\n\t\t\t</SAScollimation>";
}
sasInstrument += sasCollimation;

// Set SASdetector
std::string sasDet;
createSASDetectorElement(sasDet);
sasInstrument += sasDet;
Expand Down
72 changes: 70 additions & 2 deletions Framework/DataHandling/test/SaveCanSAS1dTest2.h
Expand Up @@ -4,11 +4,14 @@

#include "MantidAPI/Axis.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidAPI/Sample.h"
#include "MantidDataHandling/LoadRaw3.h"
#include "MantidDataHandling/SaveCanSAS1D2.h"
#include "MantidDataHandling/LoadCanSAS1D.h"
#include "MantidDataHandling/LoadCanSAS1D2.h"
#include "MantidGeometry/Instrument.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"

#include <Poco/Path.h>
#include <Poco/File.h>

Expand All @@ -31,7 +34,6 @@ class SaveCanSAS1dTest2 : public CxxTest::TestSuite {
m_workspace3("SaveCanSAS1dTest2_in3"), m_filename("./savecansas1d2.xml")

{

LoadRaw3 loader;
if (!loader.isInitialized())
loader.initialize();
Expand Down Expand Up @@ -280,7 +282,73 @@ class SaveCanSAS1dTest2 : public CxxTest::TestSuite {
TS_ASSERT_DELTA(ws2d->dataE(0).back(), 0, tolerance);
}

void test_that_can_save_and_load_full_collimation_information() {
std::string geometry = "Disc";
double width = 1;
double height = 2;
int expectedGeometryFlag = 3;
double expectedWidth = 1;
double expectedHeight = 2;
do_test_collimation_settings(geometry, width, height, expectedGeometryFlag,
expectedWidth, expectedHeight);
}

private:
void do_test_collimation_settings(const std::string &geometry, double width,
double height, int expectedGeometry,
double expectedWidth,
double expectedHeight) {
// Create sample workspace
auto wsIn = WorkspaceCreationHelper::create1DWorkspaceRand(3);
auto axis = wsIn->getAxis(0);
axis->unit() = UnitFactory::Instance().create("MomentumTransfer");
axis->title() = "|Q|";

AnalysisDataService::Instance().addOrReplace("test_worksapce_can_sas_1d",
wsIn);
// Save the workspace
SaveCanSAS1D2 savealg;
TS_ASSERT_THROWS_NOTHING(savealg.initialize());
TS_ASSERT(savealg.isInitialized());
savealg.setProperty("InputWorkspace", wsIn);
savealg.setPropertyValue("Filename", m_filename);
savealg.setPropertyValue("DetectorNames", "HAB");
savealg.setProperty("Geometry", geometry);
savealg.setProperty("SampleWidth", width);
savealg.setProperty("SampleHeight", height);

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

// retrieve the data that we saved to check it
LoadCanSAS1D lAlg;
TS_ASSERT_THROWS_NOTHING(lAlg.initialize());
TS_ASSERT(lAlg.isInitialized());
lAlg.setPropertyValue("OutputWorkspace",
"test_worksapce_can_sas_1d_reloaded");
lAlg.setPropertyValue("Filename", m_filename);
TS_ASSERT_THROWS_NOTHING(lAlg.execute());
TS_ASSERT(lAlg.isExecuted());
Workspace_sptr ws = AnalysisDataService::Instance().retrieve(
"test_worksapce_can_sas_1d_reloaded");
Mantid::API::MatrixWorkspace_sptr loaded =
boost::dynamic_pointer_cast<Mantid::API::MatrixWorkspace>(ws);

// Check that elements are set correctly
TS_ASSERT(loaded->sample().getGeometryFlag() == expectedGeometry);
TS_ASSERT(loaded->sample().getWidth() == expectedWidth);
TS_ASSERT(loaded->sample().getHeight() == expectedHeight);

// Delete workspaces
std::string toDeleteList[2] = {"test_worksapce_can_sas_1d",
"test_worksapce_can_sas_1d_reloaded"};
for (auto &toDelete : toDeleteList) {
if (AnalysisDataService::Instance().doesExist(toDelete)) {
AnalysisDataService::Instance().remove(toDelete);
}
}
}

std::string m_workspace1, m_workspace2, m_workspace3, m_filename;
std::string m_runNum;
MatrixWorkspace_sptr ws;
Expand Down
1 change: 1 addition & 0 deletions docs/source/release/v3.9.0/sans.rst
Expand Up @@ -22,6 +22,7 @@ Bug Fixes
- Issue where Gui changes were not picked up for batch reductions was resolved.
- Remove SaveNexusProcessed and SaveCSV as an option. Reorder options by dimensionality.
- Fix for merged reduction with phi masking.
- Fix SAScollimation issue in SaveCanSAS1D and LoadCanSAS1D.

X uncertainties (delta-Q)
-------------------------
Expand Down