diff --git a/Framework/API/src/MatrixWorkspace.cpp b/Framework/API/src/MatrixWorkspace.cpp index 9e0a2d7917dc9f0237286363bc36947d5888ff7c..0d523b4a07c898f67bd9781d66df6e293fb683fe 100644 --- a/Framework/API/src/MatrixWorkspace.cpp +++ b/Framework/API/src/MatrixWorkspace.cpp @@ -1135,6 +1135,11 @@ MatrixWorkspace::maskedBins(const size_t &workspaceIndex) const { */ void MatrixWorkspace::setMonitorWorkspace( const boost::shared_ptr<MatrixWorkspace> &monitorWS) { + if (monitorWS.get() == this) { + throw std::runtime_error( + "To avoid memory leak, monitor workspace" + " can not be the same workspace as the host workspace"); + } m_monitorWorkspace = monitorWS; } diff --git a/Framework/PythonInterface/mantid/api/src/Exports/MatrixWorkspace.cpp b/Framework/PythonInterface/mantid/api/src/Exports/MatrixWorkspace.cpp index fbb754ab25819191b32321b6a3133e9441408f20..2f6cdb9bddca741008f2c17cede38cb09f0985dd 100644 --- a/Framework/PythonInterface/mantid/api/src/Exports/MatrixWorkspace.cpp +++ b/Framework/PythonInterface/mantid/api/src/Exports/MatrixWorkspace.cpp @@ -2,6 +2,7 @@ #include "MantidAPI/WorkspaceOpOverloads.h" #include "MantidPythonInterface/api/CloneMatrixWorkspace.h" +#include "MantidPythonInterface/api/ExtractWorkspace.h" #include "MantidPythonInterface/kernel/Converters/WrapWithNumpy.h" #include "MantidPythonInterface/kernel/Policies/RemoveConst.h" #include "MantidPythonInterface/kernel/Policies/VectorToNumpy.h" @@ -77,6 +78,37 @@ void setSpectrumFromPyObject(MatrixWorkspace &self, data_modifier accessor, } } +/** + * Set a workspace as monitor workspace for current workspace. + * + * @param self :: A reference to the calling object + * @param value :: The python pointer to the workspace to set + */ +void setMonitorWorkspace(MatrixWorkspace &self, + const boost::python::object &value) { + + MatrixWorkspace_sptr monWS = boost::dynamic_pointer_cast<MatrixWorkspace>( + Mantid::PythonInterface::ExtractWorkspace(value)()); + self.setMonitorWorkspace(monWS); +} +/** +* @param self :: A reference to the calling object +* +*@return weak pointer to monitor workspace used by python +*/ +boost::weak_ptr<Workspace> getMonitorWorkspace(MatrixWorkspace &self) { + return boost::weak_ptr<Workspace>(self.monitorWorkspace()); +} +/** + * Clear monitor workspace attached to for current workspace. + * + * @param self :: A reference to the calling object +*/ +void clearMonitorWorkspace(MatrixWorkspace &self) { + MatrixWorkspace_sptr monWS; + self.setMonitorWorkspace(monWS); +} + /** * Set the X values from an python array-style object * @param self :: A reference to the calling object @@ -317,7 +349,19 @@ void export_MatrixWorkspace() { //----------------------------------- .def("equals", &Mantid::API::equals, args("self", "other", "tolerance"), "Performs a comparison operation on two workspaces, using the " - "CheckWorkspacesMatch algorithm"); + "CheckWorkspacesMatch algorithm") + //--------- monitor workspace -------------------------------------- + .def("getMonitorWorkspace", &getMonitorWorkspace, args("self"), + "Return internal monitor workspace bound to current workspace.") + .def("setMonitorWorkspace", &setMonitorWorkspace, + args("self", "MonitorWS"), + "Set specified workspace as monitor workspace for" + "current workspace. " + "Note: The workspace does not have to contain monitors though " + "some subsequent algorithms may expect it to be " + "monitor workspace later.") + .def("clearMonitorWorkspace", &clearMonitorWorkspace, args("self"), + "Forget about monitor workspace, attached to the current workspace"); RegisterWorkspacePtrToPython<MatrixWorkspace>(); } diff --git a/Framework/PythonInterface/test/python/mantid/api/MatrixWorkspaceTest.py b/Framework/PythonInterface/test/python/mantid/api/MatrixWorkspaceTest.py index 7b27a63bd91e8261f9859a5d8ef894e5f21f4dc5..e11d821084ad9cc76bfaef575fb13c721c4b49ba 100644 --- a/Framework/PythonInterface/test/python/mantid/api/MatrixWorkspaceTest.py +++ b/Framework/PythonInterface/test/python/mantid/api/MatrixWorkspaceTest.py @@ -1,4 +1,4 @@ -import unittest +import unittest import sys import math from testhelpers import create_algorithm, run_algorithm, can_be_instantiated, WorkspaceCreationHelper @@ -344,6 +344,48 @@ class MatrixWorkspaceTest(unittest.TestCase): ws1.setComment(comment) self.assertEquals(comment, ws1.getComment()) AnalysisDataService.remove(ws1.getName()) + + def test_setGetMonitorWS(self): + run_algorithm('CreateWorkspace', OutputWorkspace='ws1',DataX=[1.,2.,3.], DataY=[2.,3.], DataE=[2.,3.],UnitX='TOF') + run_algorithm('CreateWorkspace', OutputWorkspace='ws_mon',DataX=[1.,2.,3.], DataY=[2.,3.], DataE=[2.,3.],UnitX='TOF') + + ws1=AnalysisDataService.retrieve('ws1') + try: + monWs = ws1.getMonitorWorkspace() + GotIt = True + except RuntimeError: + GotIt = False + self.assertFalse(GotIt) + + monWs = AnalysisDataService.retrieve('ws_mon') + ws1.setMonitorWorkspace(monWs) + monWs.setTitle("My Fake Monitor workspace") + + monWs1 = ws1.getMonitorWorkspace(); + self.assertEquals(monWs.getTitle(), monWs1.getTitle()) + + ws1.clearMonitorWorkspace() + try: + monWs1 = ws1.getMonitorWorkspace() + GotIt = True + except RuntimeError: + GotIt = False + self.assertFalse(GotIt) + + # Check weak pointer issues + ws1.setMonitorWorkspace(monWs) + wms=ws1.getMonitorWorkspace() + + allFine = False + try: + ws1.setMonitorWorkspace(wms) + allFine = True + except ValueError: + pass + self.assertTrue(allFine) if __name__ == '__main__': unittest.main() + #Testing particular test from Mantid + #tester=MatrixWorkspaceTest('test_setGetMonitorWS') + #tester.test_setGetMonitorWS()