diff --git a/Framework/PythonInterface/mantid/api/src/Exports/MatrixWorkspace.cpp b/Framework/PythonInterface/mantid/api/src/Exports/MatrixWorkspace.cpp index fbb754ab25819191b32321b6a3133e9441408f20..b753fff6c5ea1bb6704afde331a2ad79fadd4b47 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" @@ -14,6 +15,8 @@ #include <boost/python/overloads.hpp> #include <boost/python/register_ptr_to_python.hpp> + + using namespace Mantid::API; using namespace Mantid::Geometry; using namespace Mantid::Kernel; @@ -77,6 +80,36 @@ 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)()); + if(monWS){ + if(monWS.get() == &self){ + throw std::runtime_error("To avoid memory leak, monitor workspace" + " can not be the same workspace as the host workspace"); + } + } + self.setMonitorWorkspace(monWS); +} +/** + * 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 @@ -146,6 +179,7 @@ Mantid::API::Run &getSampleDetailsDeprecated(MatrixWorkspace &self) { } } + /** Python exports of the Mantid::API::MatrixWorkspace class. */ void export_MatrixWorkspace() { /// Typedef to remove const qualifier on input detector shared_ptr. See @@ -317,7 +351,22 @@ 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",&MatrixWorkspace::monitorWorkspace, + 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..d05570c28e86cbcd87d8afe5730c1e1f736e48f6 100644 --- a/Framework/PythonInterface/test/python/mantid/api/MatrixWorkspaceTest.py +++ b/Framework/PythonInterface/test/python/mantid/api/MatrixWorkspaceTest.py @@ -344,6 +344,31 @@ 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=mtd['ws1'] + monWs = ws1.getMonitorWorkspace() + self.assertTrue(monWs is None) + + monWs = mtd['ws_mon'] + ws1.setMonitorWorkspace(monWs) + monWs.setTitle("My Fake Monitor workspace") + + monWs1 = ws1.getMonitorWorkspace(); + self.assertEquals(monWs.getTitle(), monWs1.getTitle()) + + ws1.clearMonitorWorkspace() + monWs1 = ws1.getMonitorWorkspace() + self.assertTrue(monWs1 is None) if __name__ == '__main__': unittest.main() + #Testing particular test from Mantid + #class theTester(MatrixWorkspaceTest): + # def runTest(): + # pass + #tester = theTester() + tester.test_setGetMonitorWS() \ No newline at end of file