Skip to content

Commit

Permalink
Re #9543. Ensure the monitor workspace is kept at end of run.
Browse files Browse the repository at this point in the history
If 'renaming' the run at the end is chosen, the Clone algorithm is
run - override the default loss of the monitor workspace in this
algorithm by pulling it out and cloning it as well (and then putting
it back in).
  • Loading branch information
RussellTaylor committed Jun 9, 2014
1 parent 7b67204 commit e077ee6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
28 changes: 25 additions & 3 deletions Code/Mantid/Framework/LiveData/src/MonitorLiveData.cpp
Expand Up @@ -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<Workspace>(originalName);
Workspace_sptr original = ads.retrieveWS<Workspace>(originalName);
if (original)
{
size_t bytesUsed = original->getMemorySize();
Expand All @@ -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<MatrixWorkspace>(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<MatrixWorkspace>(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<MatrixWorkspace>(newName)->setMonitorWorkspace(newMonitorWS);
}
}
else
{
Expand Down
6 changes: 6 additions & 0 deletions Code/Mantid/Framework/LiveData/test/MonitorLiveDataTest.h
Expand Up @@ -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<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 );

// 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 );

Kernel::Timer timer;
while ( alg1->isRunning() && timer.elapsed_no_reset() < 0.5 ) {}
Expand Down
4 changes: 4 additions & 0 deletions Code/Mantid/Framework/LiveData/test/TestDataListener.cpp
Expand Up @@ -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<Workspace> TestDataListener::extractData()
Expand All @@ -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;
Expand Down

0 comments on commit e077ee6

Please sign in to comment.