From ded598c5c6e668fec83ed807ef8c8f0c85f00459 Mon Sep 17 00:00:00 2001 From: Russell Taylor Date: Thu, 12 Jun 2014 13:04:30 -0400 Subject: [PATCH] Re #9543. Make sure monitor WS is kept if not preserving events. Also add tests for this. There's a call to ConvertToMatrixWorkspace if the option to preserve events is off in a live data run. This propagates any monitor workspace through this algorithm, though note that in the case of the SNS listener at least a Rebin or something needs to be performed on the monitor workspace to set the X boundaries. --- .../Framework/LiveData/src/LoadLiveData.cpp | 15 +++++++++++++++ .../Framework/LiveData/test/LoadLiveDataTest.h | 10 +++++++++- .../Framework/LiveData/test/MonitorLiveDataTest.h | 6 +++--- .../Framework/LiveData/test/TestDataListener.cpp | 8 ++++++-- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/Code/Mantid/Framework/LiveData/src/LoadLiveData.cpp b/Code/Mantid/Framework/LiveData/src/LoadLiveData.cpp index 58580ad5959a..2d08b77b7a2e 100644 --- a/Code/Mantid/Framework/LiveData/src/LoadLiveData.cpp +++ b/Code/Mantid/Framework/LiveData/src/LoadLiveData.cpp @@ -501,6 +501,20 @@ namespace LiveData EventWorkspace_sptr processedEvent = boost::dynamic_pointer_cast(processed); if (!PreserveEvents && processedEvent) { + // Convert the monitor workspace, if there is one and it's necessary + MatrixWorkspace_sptr monitorWS = processedEvent->monitorWorkspace(); + auto monitorEventWS = boost::dynamic_pointer_cast(monitorWS); + if ( monitorEventWS ) + { + auto monAlg = this->createChildAlgorithm("ConvertToMatrixWorkspace"); + monAlg->setProperty("InputWorkspace", monitorEventWS); + monAlg->executeAsChildAlg(); + if (!monAlg->isExecuted()) + g_log.error("Failed to convert monitors from events to histogram form."); + monitorWS = monAlg->getProperty("OutputWorkspace"); + } + + // Now do the main workspace Algorithm_sptr alg = this->createChildAlgorithm("ConvertToMatrixWorkspace"); alg->setProperty("InputWorkspace", processedEvent); std::string outputName = "__anonymous_livedata_convert_" + this->getPropertyValue("OutputWorkspace"); @@ -510,6 +524,7 @@ namespace LiveData throw std::runtime_error("Error when calling ConvertToMatrixWorkspace (since PreserveEvents=False). See log."); // Replace the "processed" workspace with the converted one. MatrixWorkspace_sptr temp = alg->getProperty("OutputWorkspace"); + if ( monitorWS ) temp->setMonitorWorkspace( monitorWS ); // Set back the monitor workspace processed = temp; } diff --git a/Code/Mantid/Framework/LiveData/test/LoadLiveDataTest.h b/Code/Mantid/Framework/LiveData/test/LoadLiveDataTest.h index ae112ed968fc..12774ba895e5 100644 --- a/Code/Mantid/Framework/LiveData/test/LoadLiveDataTest.h +++ b/Code/Mantid/Framework/LiveData/test/LoadLiveDataTest.h @@ -92,7 +92,7 @@ class LoadLiveDataTest : public CxxTest::TestSuite if ( dynamic_cast(ws.get()) ) { - TSM_ASSERT_LESS_THAN( "Run number should be non-zero", 0, alg.runNumber() ); + TSM_ASSERT_LESS_THAN( "Run number should be non-zero", 0, alg.runNumber() ); } return ws; @@ -149,6 +149,9 @@ class LoadLiveDataTest : public CxxTest::TestSuite TSM_ASSERT( "Workspace being added stayed the same pointer", ws1 == ws2 ); TSM_ASSERT( "Events are sorted", ws2->getEventList(0).isSortedByTof()); TS_ASSERT_EQUALS(AnalysisDataService::Instance().size(), 1); + + // Test monitor workspace is present + TS_ASSERT( ws2->monitorWorkspace() ); } //-------------------------------------------------------------------------------------------- @@ -205,6 +208,9 @@ class LoadLiveDataTest : public CxxTest::TestSuite TSM_ASSERT( "Workspace being added stayed the same pointer", ws1 == ws2 ); TS_ASSERT_EQUALS(AnalysisDataService::Instance().size(), 1); + + TS_ASSERT( ws1->monitorWorkspace() ); + TS_ASSERT_EQUALS( ws1->monitorWorkspace(), ws2->monitorWorkspace() ); } @@ -220,6 +226,7 @@ class LoadLiveDataTest : public CxxTest::TestSuite TS_ASSERT_EQUALS(ws->blocksize(), 20); TS_ASSERT_DELTA(ws->dataX(0)[0], 40e3, 1e-4); TS_ASSERT_EQUALS(AnalysisDataService::Instance().size(), 1); + TS_ASSERT( ws->monitorWorkspace() ); } //-------------------------------------------------------------------------------------------- @@ -233,6 +240,7 @@ class LoadLiveDataTest : public CxxTest::TestSuite TS_ASSERT_EQUALS(ws->blocksize(), 20); TS_ASSERT_DELTA(ws->dataX(0)[0], 40e3, 1e-4); TS_ASSERT_EQUALS(AnalysisDataService::Instance().size(), 1); + TS_ASSERT( ws->monitorWorkspace() ); } //-------------------------------------------------------------------------------------------- diff --git a/Code/Mantid/Framework/LiveData/test/MonitorLiveDataTest.h b/Code/Mantid/Framework/LiveData/test/MonitorLiveDataTest.h index c9b558464298..0de16dbc7d7d 100644 --- a/Code/Mantid/Framework/LiveData/test/MonitorLiveDataTest.h +++ b/Code/Mantid/Framework/LiveData/test/MonitorLiveDataTest.h @@ -209,18 +209,18 @@ class MonitorLiveDataTest : public CxxTest::TestSuite alg1->cancel(); // The first workspace got cloned to a new name (the suffix is set in the TestDataListener) - EventWorkspace_sptr ws1 = AnalysisDataService::Instance().retrieveWS("fake2_999"); + EventWorkspace_const_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 ); + TS_ASSERT_EQUALS( ws1->monitorWorkspace()->readY(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 ); + TS_ASSERT_EQUALS( ws2->monitorWorkspace()->readY(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 f97dd79855c8..ea079190dc4a 100644 --- a/Code/Mantid/Framework/LiveData/test/TestDataListener.cpp +++ b/Code/Mantid/Framework/LiveData/test/TestDataListener.cpp @@ -93,7 +93,10 @@ namespace LiveData // Set a run number m_buffer->mutableRun().addProperty("run_number", std::string("999")); // Add a monitor workspace - auto monitorWS = WorkspaceFactory::Instance().create(m_buffer); + auto monitorWS = WorkspaceFactory::Instance().create("EventWorkspace",1,2,1); + WorkspaceFactory::Instance().initializeFromParent(m_buffer,monitorWS,true); + monitorWS->dataX(0)[0] = 40000; + monitorWS->dataX(0)[1] = 60000; m_buffer->setMonitorWorkspace(monitorWS); } @@ -110,7 +113,8 @@ namespace LiveData el1.addEventQuickly(TofEvent(m_rand->nextValue())); el2.addEventQuickly(TofEvent(m_rand->nextValue())); } - m_buffer->monitorWorkspace()->dataY(0)[0] = 1.0; + auto mon_buffer = boost::dynamic_pointer_cast(m_buffer->monitorWorkspace()); + mon_buffer->getEventList(0).addEventQuickly(TofEvent(m_rand->nextValue())); // Copy the workspace pointer to a temporary variable EventWorkspace_sptr extracted = m_buffer;