Skip to content

Commit

Permalink
Re #9541. Add ExtractMonitorWorkspace algorithm and test.
Browse files Browse the repository at this point in the history
  • Loading branch information
RussellTaylor committed May 30, 2014
1 parent b39cf7d commit c17db86
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 16 deletions.
35 changes: 19 additions & 16 deletions Code/Mantid/Framework/DataHandling/CMakeLists.txt
Expand Up @@ -9,6 +9,8 @@ set ( SRC_FILES
src/DefineGaugeVolume.cpp
src/DeleteTableRows.cpp
src/DetermineChunking.cpp
src/ExtractMonitorWorkspace.cpp
src/FilterEventsByLogValuePreNexus.cpp
src/FindDetectorsInShape.cpp
src/FindDetectorsPar.cpp
src/GenerateGroupingPowder.cpp
Expand All @@ -31,7 +33,7 @@ set ( SRC_FILES
src/LoadEventPreNexus.cpp
src/LoadEventPreNexus2.cpp
src/LoadFullprofResolution.cpp
src/LoadGSASInstrumentFile.cpp
src/LoadGSASInstrumentFile.cpp
src/LoadGSS.cpp
src/LoadHelper.cpp
src/LoadIDFFromNexus.cpp
Expand Down Expand Up @@ -93,16 +95,16 @@ set ( SRC_FILES
src/ModifyDetectorDotDatFile.cpp
src/MoveInstrumentComponent.cpp
src/NexusTester.cpp
src/PDLoadCharacterizations.cpp
src/ProcessDasNexusLog.cpp
src/PDLoadCharacterizations.cpp
src/ProcessDasNexusLog.cpp
src/RawFileInfo.cpp
src/RemoveLogs.cpp
src/RenameLog.cpp
src/RotateInstrumentComponent.cpp
src/SNSDataArchive.cpp
src/SNSDataArchiveICAT2.cpp
src/SaveAscii.cpp
src/SaveANSTOAscii.cpp
src/SaveAscii.cpp
src/SaveAscii2.cpp
src/SaveCSV.cpp
src/SaveCalFile.cpp
Expand All @@ -114,8 +116,8 @@ set ( SRC_FILES
src/SaveFocusedXYE.cpp
src/SaveFullprofResolution.cpp
src/SaveGSS.cpp
src/SaveISISNexus.cpp
src/SaveILLCosmosAscii.cpp
src/SaveISISNexus.cpp
src/SaveIsawDetCal.cpp
src/SaveMask.cpp
src/SaveNISTDAT.cpp
Expand All @@ -124,15 +126,14 @@ set ( SRC_FILES
src/SaveNexusProcessed.cpp
src/SavePAR.cpp
src/SavePHX.cpp
src/SaveReflTBL.cpp
src/SaveRKH.cpp
src/SaveReflTBL.cpp
src/SaveSPE.cpp
src/SaveToSNSHistogramNexus.cpp
src/SaveVTK.cpp
src/SetSampleMaterial.cpp
src/SetScalingPSD.cpp
src/UpdateInstrumentFromFile.cpp
src/FilterEventsByLogValuePreNexus.cpp
)

set ( INC_FILES
Expand All @@ -146,6 +147,8 @@ set ( INC_FILES
inc/MantidDataHandling/DefineGaugeVolume.h
inc/MantidDataHandling/DeleteTableRows.h
inc/MantidDataHandling/DetermineChunking.h
inc/MantidDataHandling/ExtractMonitorWorkspace.h
inc/MantidDataHandling/FilterEventsByLogValuePreNexus.h
inc/MantidDataHandling/FindDetectorsInShape.h
inc/MantidDataHandling/FindDetectorsPar.h
inc/MantidDataHandling/GenerateGroupingPowder.h
Expand All @@ -168,7 +171,7 @@ set ( INC_FILES
inc/MantidDataHandling/LoadEventPreNexus.h
inc/MantidDataHandling/LoadEventPreNexus2.h
inc/MantidDataHandling/LoadFullprofResolution.h
inc/MantidDataHandling/LoadGSASInstrumentFile.h
inc/MantidDataHandling/LoadGSASInstrumentFile.h
inc/MantidDataHandling/LoadGSS.h
inc/MantidDataHandling/LoadHelper.h
inc/MantidDataHandling/LoadIDFFromNexus.h
Expand Down Expand Up @@ -225,8 +228,8 @@ set ( INC_FILES
inc/MantidDataHandling/ModifyDetectorDotDatFile.h
inc/MantidDataHandling/MoveInstrumentComponent.h
inc/MantidDataHandling/NexusTester.h
inc/MantidDataHandling/PDLoadCharacterizations.h
inc/MantidDataHandling/ProcessDasNexusLog.h
inc/MantidDataHandling/PDLoadCharacterizations.h
inc/MantidDataHandling/ProcessDasNexusLog.h
inc/MantidDataHandling/RawFileInfo.h
inc/MantidDataHandling/RemoveLogs.h
inc/MantidDataHandling/RenameLog.h
Expand All @@ -246,8 +249,8 @@ set ( INC_FILES
inc/MantidDataHandling/SaveFocusedXYE.h
inc/MantidDataHandling/SaveFullprofResolution.h
inc/MantidDataHandling/SaveGSS.h
inc/MantidDataHandling/SaveISISNexus.h
inc/MantidDataHandling/SaveILLCosmosAscii.h
inc/MantidDataHandling/SaveISISNexus.h
inc/MantidDataHandling/SaveIsawDetCal.h
inc/MantidDataHandling/SaveMask.h
inc/MantidDataHandling/SaveNISTDAT.h
Expand All @@ -256,15 +259,14 @@ set ( INC_FILES
inc/MantidDataHandling/SaveNexusProcessed.h
inc/MantidDataHandling/SavePAR.h
inc/MantidDataHandling/SavePHX.h
inc/MantidDataHandling/SaveReflTBL.h
inc/MantidDataHandling/SaveRKH.h
inc/MantidDataHandling/SaveReflTBL.h
inc/MantidDataHandling/SaveSPE.h
inc/MantidDataHandling/SaveToSNSHistogramNexus.h
inc/MantidDataHandling/SaveVTK.h
inc/MantidDataHandling/SetSampleMaterial.h
inc/MantidDataHandling/SetScalingPSD.h
inc/MantidDataHandling/UpdateInstrumentFromFile.h
inc/MantidDataHandling/FilterEventsByLogValuePreNexus.h
src/LoadRaw/byte_rel_comp.h
src/LoadRaw/isisraw.h
src/LoadRaw/isisraw2.h
Expand All @@ -282,6 +284,8 @@ set ( TEST_FILES
DefineGaugeVolumeTest.h
DeleteTableRowsTest.h
DetermineChunkingTest.h
ExtractMonitorWorkspaceTest.h
FilterEventsByLogValuePreNexusTest.h
FindDetectorsInShapeTest.h
FindDetectorsParTest.h
GenerateGroupingPowderTest.h
Expand Down Expand Up @@ -353,8 +357,8 @@ set ( TEST_FILES
ModifyDetectorDotDatFileTest.h
MoveInstrumentComponentTest.h
NexusTesterTest.h
PDLoadCharacterizationsTest.h
ProcessDasNexusLogTest.h
PDLoadCharacterizationsTest.h
ProcessDasNexusLogTest.h
RawFileInfoTest.h
RemoveLogsTest.h
RenameLogTest.h
Expand Down Expand Up @@ -390,7 +394,6 @@ set ( TEST_FILES
SetScalingPSDTest.h
UpdateInstrumentFromFileTest.h
XMLlogfileTest.h
FilterEventsByLogValuePreNexusTest.h
)


Expand Down
@@ -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 Code/Mantid/Framework/DataHandling/src/ExtractMonitorWorkspace.cpp
@@ -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 Code/Mantid/Framework/DataHandling/test/ExtractMonitorWorkspaceTest.h
@@ -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_ */

0 comments on commit c17db86

Please sign in to comment.