Skip to content

Commit

Permalink
Re #9543. Make sure monitor WS is kept if not preserving events.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
RussellTaylor committed Jun 12, 2014
1 parent d36bf3f commit e29cf97
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
15 changes: 15 additions & 0 deletions Code/Mantid/Framework/LiveData/src/LoadLiveData.cpp
Expand Up @@ -501,6 +501,20 @@ namespace LiveData
EventWorkspace_sptr processedEvent = boost::dynamic_pointer_cast<EventWorkspace>(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<EventWorkspace>(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");
Expand All @@ -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;
}

Expand Down
8 changes: 8 additions & 0 deletions Code/Mantid/Framework/LiveData/test/LoadLiveDataTest.h
Expand Up @@ -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() );
}

//--------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -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() );
}


Expand All @@ -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() );
}

//--------------------------------------------------------------------------------------------
Expand All @@ -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() );
}

//--------------------------------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions Code/Mantid/Framework/LiveData/test/MonitorLiveDataTest.h
Expand Up @@ -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<EventWorkspace>("fake2_999");
EventWorkspace_const_sptr ws1 = AnalysisDataService::Instance().retrieveWS<EventWorkspace>("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<EventWorkspace>("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 ) {}
Expand Down
8 changes: 6 additions & 2 deletions Code/Mantid/Framework/LiveData/test/TestDataListener.cpp
Expand Up @@ -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);
}

Expand All @@ -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<EventWorkspace>(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;
Expand Down

0 comments on commit e29cf97

Please sign in to comment.