Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re #9541. Add ExtractMonitorWorkspace algorithm and test.
- Loading branch information
1 parent
b39cf7d
commit c17db86
Showing
4 changed files
with
243 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/ExtractMonitorWorkspace.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#ifndef MANTID_DATAHANDLING_EXTRACTMONITORWORKSPACE_H_ | ||
#define MANTID_DATAHANDLING_EXTRACTMONITORWORKSPACE_H_ | ||
|
||
#include "MantidKernel/System.h" | ||
#include "MantidAPI/Algorithm.h" | ||
|
||
namespace Mantid | ||
{ | ||
namespace DataHandling | ||
{ | ||
/** Looks for an internally-stored monitor workspace on the input workspace and | ||
sets it as the output workspace if found. The input workspace will no longer | ||
hold a reference to the monitor workspace after running this algorithm. | ||
If no monitor workspace is present the algorithm will fail. | ||
Copyright © 2014 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 ExtractMonitorWorkspace : public API::Algorithm | ||
{ | ||
public: | ||
ExtractMonitorWorkspace(); | ||
virtual ~ExtractMonitorWorkspace(); | ||
|
||
virtual const std::string name() const; | ||
virtual int version() const; | ||
virtual const std::string category() const; | ||
virtual const std::string summary() const; | ||
|
||
private: | ||
void init(); | ||
void exec(); | ||
}; | ||
|
||
} // namespace DataHandling | ||
} // namespace Mantid | ||
|
||
#endif /* MANTID_DATAHANDLING_EXTRACTMONITORWORKSPACE_H_ */ |
67 changes: 67 additions & 0 deletions
67
Code/Mantid/Framework/DataHandling/src/ExtractMonitorWorkspace.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/*WIKI* | ||
This algorithm looks for an internally-stored monitor workspace on the input workspace and | ||
sets it as the output workspace if found. The input workspace will no longer hold a reference | ||
to the monitor workspace after this algorithm has been run on it. | ||
If no monitor workspace is present the algorithm will fail. | ||
*WIKI*/ | ||
|
||
#include "MantidDataHandling/ExtractMonitorWorkspace.h" | ||
|
||
namespace Mantid | ||
{ | ||
namespace DataHandling | ||
{ | ||
// Register the algorithm into the AlgorithmFactory | ||
DECLARE_ALGORITHM(ExtractMonitorWorkspace) | ||
|
||
using namespace Mantid::Kernel; | ||
using namespace Mantid::API; | ||
|
||
ExtractMonitorWorkspace::ExtractMonitorWorkspace() | ||
{} | ||
|
||
ExtractMonitorWorkspace::~ExtractMonitorWorkspace() | ||
{} | ||
|
||
/// Algorithm's name for identification. @see Algorithm::name | ||
const std::string ExtractMonitorWorkspace::name() const { return "ExtractMonitorWorkspace";}; | ||
|
||
/// Algorithm's version for identification. @see Algorithm::version | ||
int ExtractMonitorWorkspace::version() const { return 1;}; | ||
|
||
/// Algorithm's category for identification. @see Algorithm::category | ||
const std::string ExtractMonitorWorkspace::category() const { return "DataHandling";} | ||
|
||
/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary | ||
const std::string ExtractMonitorWorkspace::summary() const | ||
{ | ||
return "Retrieves a workspace of monitor data held within the input workspace, if present."; | ||
} | ||
|
||
/** Initialize the algorithm's properties. | ||
*/ | ||
void ExtractMonitorWorkspace::init() | ||
{ | ||
declareProperty(new WorkspaceProperty<>("InputWorkspace","",Direction::Input), "An input workspace."); | ||
declareProperty(new WorkspaceProperty<>("MonitorWorkspace","",Direction::Output), "An output workspace."); | ||
} | ||
|
||
/** Execute the algorithm. | ||
*/ | ||
void ExtractMonitorWorkspace::exec() | ||
{ | ||
MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace"); | ||
auto monitorWS = inputWS->monitorWorkspace(); | ||
|
||
if ( ! monitorWS ) | ||
{ | ||
throw std::invalid_argument("The input workspace does not hold a monitor workspace"); | ||
} | ||
|
||
setProperty("MonitorWorkspace", monitorWS); | ||
// Now clear off the pointer on the input workspace | ||
inputWS->setMonitorWorkspace(MatrixWorkspace_sptr()); | ||
} | ||
|
||
} // namespace DataHandling | ||
} // namespace Mantid |
102 changes: 102 additions & 0 deletions
102
Code/Mantid/Framework/DataHandling/test/ExtractMonitorWorkspaceTest.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#ifndef MANTID_DATAHANDLING_EXTRACTMONITORWORKSPACETEST_H_ | ||
#define MANTID_DATAHANDLING_EXTRACTMONITORWORKSPACETEST_H_ | ||
|
||
#include <cxxtest/TestSuite.h> | ||
|
||
#include "MantidDataHandling/ExtractMonitorWorkspace.h" | ||
#include "MantidTestHelpers/WorkspaceCreationHelper.h" | ||
|
||
using Mantid::DataHandling::ExtractMonitorWorkspace; | ||
using namespace Mantid::API; | ||
|
||
class ExtractMonitorWorkspaceTest : public CxxTest::TestSuite | ||
{ | ||
public: | ||
// This pair of boilerplate methods prevent the suite being created statically | ||
// This means the constructor isn't called when running other tests | ||
static ExtractMonitorWorkspaceTest *createSuite() { return new ExtractMonitorWorkspaceTest(); } | ||
static void destroySuite( ExtractMonitorWorkspaceTest *suite ) { delete suite; } | ||
|
||
ExtractMonitorWorkspaceTest() : outWSName("ExtractMonitorWorkspaceTest_OutputWS") | ||
{} | ||
|
||
void test_init() | ||
{ | ||
ExtractMonitorWorkspace alg; | ||
TS_ASSERT_THROWS_NOTHING( alg.initialize() ) | ||
TS_ASSERT( alg.isInitialized() ) | ||
} | ||
|
||
void test_fails_if_no_monitor_workspace() | ||
{ | ||
auto inws = WorkspaceCreationHelper::Create1DWorkspaceRand(1); | ||
|
||
ExtractMonitorWorkspace alg; | ||
TS_ASSERT_THROWS_NOTHING( alg.initialize() ) | ||
TS_ASSERT_THROWS_NOTHING( alg.setProperty("InputWorkspace", inws) ); | ||
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("MonitorWorkspace", outWSName) ); | ||
TS_ASSERT_THROWS_NOTHING( alg.execute(); ); | ||
TS_ASSERT( ! alg.isExecuted() ); | ||
} | ||
|
||
void doTest(MatrixWorkspace_sptr inws, MatrixWorkspace_sptr monws) | ||
{ | ||
inws->setMonitorWorkspace(monws); | ||
TS_ASSERT_EQUALS( inws->monitorWorkspace(), monws ); | ||
|
||
ExtractMonitorWorkspace alg; | ||
TS_ASSERT_THROWS_NOTHING( alg.initialize() ) | ||
TS_ASSERT_THROWS_NOTHING( alg.setProperty("InputWorkspace", inws) ); | ||
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("MonitorWorkspace", outWSName) ); | ||
TS_ASSERT_THROWS_NOTHING( alg.execute(); ); | ||
TS_ASSERT( alg.isExecuted() ); | ||
|
||
MatrixWorkspace_sptr ws; | ||
TS_ASSERT_THROWS_NOTHING( ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(outWSName) ); | ||
TS_ASSERT(ws); | ||
if (!ws) return; | ||
|
||
TS_ASSERT_EQUALS( ws, monws ); | ||
TSM_ASSERT( "The monitor workspace should have been wiped off the input workspace", | ||
! inws->monitorWorkspace() ); | ||
|
||
// Remove workspace from the data service. | ||
AnalysisDataService::Instance().remove(outWSName); | ||
} | ||
|
||
void test_2D_2D() | ||
{ | ||
auto inws = WorkspaceCreationHelper::Create1DWorkspaceRand(1); | ||
auto monws = WorkspaceCreationHelper::Create1DWorkspaceFib(1); | ||
doTest(inws, monws); | ||
} | ||
|
||
// These tests demonstrate that the workspaces don't have to be of the same type | ||
|
||
void test_2D_event() | ||
{ | ||
auto inws = WorkspaceCreationHelper::Create1DWorkspaceRand(1); | ||
auto monws = WorkspaceCreationHelper::CreateEventWorkspace2(1,1); | ||
doTest(inws, monws); | ||
} | ||
|
||
void test_event_2D() | ||
{ | ||
auto inws = WorkspaceCreationHelper::CreateEventWorkspace2(1,1); | ||
auto monws = WorkspaceCreationHelper::Create1DWorkspaceRand(1); | ||
doTest(inws, monws); | ||
} | ||
|
||
void test_event_event() | ||
{ | ||
auto inws = WorkspaceCreationHelper::CreateEventWorkspace2(1,1); | ||
auto monws = WorkspaceCreationHelper::CreateEventWorkspace2(1,1); | ||
doTest(inws, monws); | ||
} | ||
|
||
private: | ||
std::string outWSName; | ||
}; | ||
|
||
|
||
#endif /* MANTID_DATAHANDLING_EXTRACTMONITORWORKSPACETEST_H_ */ |