Skip to content

Commit

Permalink
refs #7539. ClearUB algorithm introduced.
Browse files Browse the repository at this point in the history
Algorithm exposes the clearUB capabilities on the sample. Works with both singular experiment infos and multiple experiment infos. Unit tests provided to cover implemented functionality.
  • Loading branch information
OwenArnold committed Aug 5, 2013
1 parent a31edc5 commit 3f17b5a
Show file tree
Hide file tree
Showing 4 changed files with 328 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/Crystal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set ( SRC_FILES
src/CalculatePeaksHKL.cpp
src/CalculateUMatrix.cpp
src/CentroidPeaks.cpp
src/ClearUB.cpp
src/CombinePeaksWorkspaces.cpp
src/DiffPeaksWorkspaces.cpp
src/FilterPeaks.cpp
Expand Down Expand Up @@ -56,6 +57,7 @@ set ( INC_FILES
inc/MantidCrystal/CalculatePeaksHKL.h
inc/MantidCrystal/CalculateUMatrix.h
inc/MantidCrystal/CentroidPeaks.h
inc/MantidCrystal/ClearUB.h
inc/MantidCrystal/CombinePeaksWorkspaces.h
inc/MantidCrystal/DiffPeaksWorkspaces.h
inc/MantidCrystal/FilterPeaks.h
Expand Down Expand Up @@ -108,6 +110,7 @@ set ( TEST_FILES
CalculatePeaksHKLTest.h
CalculateUMatrixTest.h
CentroidPeaksTest.h
ClearUBTest.h
CombinePeaksWorkspacesTest.h
DiffPeaksWorkspacesTest.h
FilterPeaksTest.h
Expand Down
57 changes: 57 additions & 0 deletions Code/Mantid/Framework/Crystal/inc/MantidCrystal/ClearUB.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#ifndef MANTID_CRYSTAL_CLEARUB_H_
#define MANTID_CRYSTAL_CLEARUB_H_

#include "MantidKernel/System.h"
#include "MantidAPI/Algorithm.h"

namespace Mantid
{
namespace Crystal
{

/** ClearUB : Clear the UB matrix from a workspace by removing the oriented lattice.
Copyright © 2013 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport ClearUB : public API::Algorithm
{
public:
ClearUB();
virtual ~ClearUB();

virtual const std::string name() const;
virtual int version() const;
virtual const std::string category() const;

private:
void clearSingleExperimentInfo(Mantid::API::ExperimentInfo * const experimentInfo) const;
virtual void initDocs();
void init();
void exec();


};


} // namespace Crystal
} // namespace Mantid

#endif /* MANTID_CRYSTAL_CLEARUB_H_ */
111 changes: 111 additions & 0 deletions Code/Mantid/Framework/Crystal/src/ClearUB.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*WIKI*
Clears the OrientedLattice of each ExperimentInfo attached to the intput [[Workspace]]. Works with both single ExperimentInfos and MultipleExperimentInfo instances.
*WIKI*/

#include "MantidCrystal/ClearUB.h"
#include "MantidAPI/MultipleExperimentInfos.h"

using namespace Mantid::Kernel;
using namespace Mantid::API;

namespace Mantid
{
namespace Crystal
{

// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(ClearUB)



//----------------------------------------------------------------------------------------------
/** Constructor
*/
ClearUB::ClearUB()
{
}

//----------------------------------------------------------------------------------------------
/** Destructor
*/
ClearUB::~ClearUB()
{
}


//----------------------------------------------------------------------------------------------
/// Algorithm's name for identification. @see Algorithm::name
const std::string ClearUB::name() const { return "ClearUB";};

/// Algorithm's version for identification. @see Algorithm::version
int ClearUB::version() const { return 1;};

/// Algorithm's category for identification. @see Algorithm::category
const std::string ClearUB::category() const { return "Crystal";}

//----------------------------------------------------------------------------------------------
/// Sets documentation strings for this algorithm
void ClearUB::initDocs()
{
this->setWikiSummary("Clears the UB by removing the oriented lattice from the sample.");
this->setOptionalMessage(this->getWikiSummary());
}

//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void ClearUB::init()
{
declareProperty(new WorkspaceProperty<Workspace>("Workspace","",Direction::InOut), "Workspace to clear the UB from.");
}

/**
* Clear the Oriented Lattice from a single experiment info.
* @param experimentInfo
*/
void ClearUB::clearSingleExperimentInfo(ExperimentInfo * const experimentInfo) const
{
Sample& sampleObject = experimentInfo->mutableSample();
if(!sampleObject.hasOrientedLattice())
{
this->g_log.information("Workspace has no oriented lattice to clear.");
}
else
{
sampleObject.clearOrientedLattice();
}
}

//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void ClearUB::exec()
{
Workspace_sptr ws = getProperty("Workspace");
ExperimentInfo_sptr experimentInfo = boost::dynamic_pointer_cast<ExperimentInfo>(ws);
if(experimentInfo)
{
clearSingleExperimentInfo(experimentInfo.get());
}
else
{
MultipleExperimentInfos_sptr experimentInfos = boost::dynamic_pointer_cast<MultipleExperimentInfos>(ws);
if(!experimentInfos)
{
throw std::runtime_error("Input workspace is neither of type ExperimentInfo or MultipleExperimentInfo, cannot process.");
}
const uint16_t nInfos = experimentInfos->getNumExperimentInfo();
for(uint16_t i = 0; i < nInfos; ++i)
{
ExperimentInfo_sptr info = experimentInfos->getExperimentInfo(i);
clearSingleExperimentInfo(info.get());
}
}
}



} // namespace Crystal
} // namespace Mantid
157 changes: 157 additions & 0 deletions Code/Mantid/Framework/Crystal/test/ClearUBTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#ifndef MANTID_CRYSTAL_CLEARUBTEST_H_
#define MANTID_CRYSTAL_CLEARUBTEST_H_

#include <cxxtest/TestSuite.h>
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidGeometry/Crystal/OrientedLattice.h"
#include "MantidCrystal/ClearUB.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
#include "MantidTestHelpers/MDEventsTestHelper.h"

using Mantid::Crystal::ClearUB;
using namespace Mantid::MDEvents;
using namespace Mantid::API;
using namespace Mantid::Geometry;

class ClearUBTest : public CxxTest::TestSuite
{

private:

// Helper method to create a matrix workspace.
std::string createMatrixWorkspace(bool withOrientedLattice = true)
{
auto ws = WorkspaceCreationHelper::Create2DWorkspace(1, 2);
if(withOrientedLattice)
{
OrientedLattice *latt = new OrientedLattice(1.0,2.0,3.0, 90, 90, 90);
ws->mutableSample().setOrientedLattice(latt);
}
const std::string wsName = "TestWorkspace";
AnalysisDataService::Instance().addOrReplace(wsName, ws);

return wsName;
}

// Helper method to create a MDHW
std::string createMDHistoWorkspace()
{
const std::string wsName = "TestWorkspace";
auto ws = MDEventsTestHelper::makeFakeMDHistoWorkspace(1, 1, 10, 10, 1, wsName);

ExperimentInfo_sptr experimentInfo1 = boost::make_shared<ExperimentInfo>();
experimentInfo1->mutableSample().setOrientedLattice(new OrientedLattice(1.0,2.0,3.0, 90, 90, 90));
ExperimentInfo_sptr experimentInfo2 = boost::make_shared<ExperimentInfo>();
experimentInfo2->mutableSample().setOrientedLattice(new OrientedLattice(1.0,2.0,3.0, 90, 90, 90));

ws->addExperimentInfo(experimentInfo1);
ws->addExperimentInfo(experimentInfo2);
AnalysisDataService::Instance().addOrReplace(wsName, ws);
return wsName;
}

// Execute the algorithm
ExperimentInfo_sptr doExecute(const std::string& wsName)
{
// Create and run the algorithm
ClearUB alg;
alg.setRethrows(true);
TS_ASSERT_THROWS_NOTHING( alg.initialize())
TS_ASSERT( alg.isInitialized())
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Workspace", wsName));
alg.execute();
TS_ASSERT( alg.isExecuted());

//Check results
auto expInfo = AnalysisDataService::Instance().retrieveWS<ExperimentInfo>(wsName);
return expInfo;
}

// Execute the algorithm
MultipleExperimentInfos_sptr doExecuteMultiInfo(const std::string& wsName)
{
// Create and run the algorithm
ClearUB alg;
alg.setRethrows(true);
TS_ASSERT_THROWS_NOTHING( alg.initialize())
TS_ASSERT( alg.isInitialized())
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Workspace", wsName));
alg.execute();
TS_ASSERT( alg.isExecuted());

//Check results
auto expInfos = AnalysisDataService::Instance().retrieveWS<MultipleExperimentInfos>(wsName);
return expInfos;
}

public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static ClearUBTest *createSuite() { return new ClearUBTest(); }
static void destroySuite( ClearUBTest *suite ) { delete suite; }


void test_Init()
{
ClearUB alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
}

void test_removeOrientedLattice()
{
// Name of the output workspace.
const std::string wsName = createMatrixWorkspace();
auto ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(wsName);
TSM_ASSERT("OrientedLattice should be present!", ws->sample().hasOrientedLattice());

auto expInfo = doExecute(wsName);

TS_ASSERT( expInfo)
TSM_ASSERT("OrientedLattice should be gone!", !expInfo->sample().hasOrientedLattice());

// Clean up.
AnalysisDataService::Instance().remove(wsName);
}

void test_removeOrientedLatticeMDHW()
{
// Name of the output workspace.
const std::string wsName = createMDHistoWorkspace();

auto expInfo = doExecuteMultiInfo(wsName);

TS_ASSERT( expInfo)

// Check that every experiment info has been cleared.
const uint16_t nInfos = expInfo->getNumExperimentInfo();
for (uint16_t i = 0; i < nInfos; ++i)
{
TSM_ASSERT("OrientedLattice should be gone!", !expInfo->getExperimentInfo(i)->sample().hasOrientedLattice());
}

// Clean up.
AnalysisDataService::Instance().remove(wsName);
}

void test_safely_continue_if_noOrientedLattice()
{
// Name of the output workspace.
const bool createOrientedLattice = false;
const std::string wsName = createMatrixWorkspace(createOrientedLattice);
auto ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(wsName);
TSM_ASSERT("No oriented lattice to begin with", !ws->sample().hasOrientedLattice());

auto expInfo = doExecute(wsName);

TS_ASSERT( expInfo)
TSM_ASSERT("OrientedLattice should be gone!", !expInfo->sample().hasOrientedLattice());

// Clean up.
AnalysisDataService::Instance().remove(wsName);
}

};


#endif /* MANTID_CRYSTAL_CLEARUBTEST_H_ */

0 comments on commit 3f17b5a

Please sign in to comment.