diff --git a/Code/Mantid/Framework/LiveData/src/MonitorLiveData.cpp b/Code/Mantid/Framework/LiveData/src/MonitorLiveData.cpp index 7f24610b6c42..6984e4984f8e 100644 --- a/Code/Mantid/Framework/LiveData/src/MonitorLiveData.cpp +++ b/Code/Mantid/Framework/LiveData/src/MonitorLiveData.cpp @@ -57,12 +57,16 @@ namespace LiveData } //---------------------------------------------------------------------------------------------- - /** Clone a workspace, if there is enough memory available */ + /** Clone a workspace, if there is enough memory available. + It needs to be a clone rather than a rename so that an open instrument window continues + to track the live workspace. + */ void MonitorLiveData::doClone(const std::string & originalName, const std::string & newName) { - if (AnalysisDataService::Instance().doesExist(originalName)) + auto & ads = AnalysisDataService::Instance(); + if ( ads.doesExist(originalName) ) { - Workspace_sptr original = AnalysisDataService::Instance().retrieveWS(originalName); + Workspace_sptr original = ads.retrieveWS(originalName); if (original) { size_t bytesUsed = original->getMemorySize(); @@ -71,11 +75,29 @@ namespace LiveData if (size_t(3)*bytesUsed < bytesAvail) { WriteLock _lock(*original); + + // Clone the monitor workspace, if there is one + auto originalMatrix = boost::dynamic_pointer_cast(original); + MatrixWorkspace_sptr monitorWS, newMonitorWS; + if ( originalMatrix && ( monitorWS = originalMatrix->monitorWorkspace() ) ) + { + auto monitorsCloner = createChildAlgorithm("CloneWorkspace", 0, 0, false); + monitorsCloner->setProperty("InputWorkspace", monitorWS); + monitorsCloner->executeAsChildAlg(); + Workspace_sptr outputWS = monitorsCloner->getProperty("OutputWorkspace"); + newMonitorWS = boost::dynamic_pointer_cast(outputWS); + } + Algorithm_sptr cloner = createChildAlgorithm("CloneWorkspace", 0, 0, false); cloner->setPropertyValue("InputWorkspace", originalName); cloner->setPropertyValue("OutputWorkspace", newName); cloner->setAlwaysStoreInADS(true); // We must force the ADS to be updated cloner->executeAsChildAlg(); + + if ( newMonitorWS ) // If there was a monitor workspace, set it back on the result + { + ads.retrieveWS(newName)->setMonitorWorkspace(newMonitorWS); + } } else { diff --git a/Code/Mantid/Framework/LiveData/test/MonitorLiveDataTest.h b/Code/Mantid/Framework/LiveData/test/MonitorLiveDataTest.h index 1595db78fb6f..c9b558464298 100644 --- a/Code/Mantid/Framework/LiveData/test/MonitorLiveDataTest.h +++ b/Code/Mantid/Framework/LiveData/test/MonitorLiveDataTest.h @@ -211,10 +211,16 @@ class MonitorLiveDataTest : public CxxTest::TestSuite // The first workspace got cloned to a new name (the suffix is set in the TestDataListener) EventWorkspace_sptr ws1 = AnalysisDataService::Instance().retrieveWS("fake2_999"); TS_ASSERT_EQUALS( ws1->getNumberEvents(), 4*200); + // Make sure the monitor workspace is present and correct + TS_ASSERT( ws1->monitorWorkspace() ); + TS_ASSERT_EQUALS( ws1->monitorWorkspace()->dataY(0)[0], 4 ); // And this is the current run EventWorkspace_sptr ws2 = AnalysisDataService::Instance().retrieveWS("fake2"); TS_ASSERT_EQUALS( ws2->getNumberEvents(), 200); + // Make sure the monitor workspace is present and correct + TS_ASSERT( ws2->monitorWorkspace() ); + TS_ASSERT_EQUALS( ws2->monitorWorkspace()->dataY(0)[0], 1 ); Kernel::Timer timer; while ( alg1->isRunning() && timer.elapsed_no_reset() < 0.5 ) {} diff --git a/Code/Mantid/Framework/LiveData/test/TestDataListener.cpp b/Code/Mantid/Framework/LiveData/test/TestDataListener.cpp index c59e5cb40b3b..f97dd79855c8 100644 --- a/Code/Mantid/Framework/LiveData/test/TestDataListener.cpp +++ b/Code/Mantid/Framework/LiveData/test/TestDataListener.cpp @@ -92,6 +92,9 @@ namespace LiveData m_buffer->setInstrument(inst); // Set a run number m_buffer->mutableRun().addProperty("run_number", std::string("999")); + // Add a monitor workspace + auto monitorWS = WorkspaceFactory::Instance().create(m_buffer); + m_buffer->setMonitorWorkspace(monitorWS); } boost::shared_ptr TestDataListener::extractData() @@ -107,6 +110,7 @@ namespace LiveData el1.addEventQuickly(TofEvent(m_rand->nextValue())); el2.addEventQuickly(TofEvent(m_rand->nextValue())); } + m_buffer->monitorWorkspace()->dataY(0)[0] = 1.0; // Copy the workspace pointer to a temporary variable EventWorkspace_sptr extracted = m_buffer;