Skip to content

Commit

Permalink
Refs #9548 Modify MergeRuns to fill history correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Jackson committed Jun 2, 2014
1 parent 65343c8 commit c95e703
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Code/Mantid/Framework/API/inc/MantidAPI/Algorithm.h
Expand Up @@ -301,6 +301,8 @@ class MANTID_API_DLL Algorithm : public IAlgorithm, public Kernel::PropertyManag

/// get whether we are tracking the history for this algorithm,
bool trackingHistory();
/// Copy workspace history for input workspaces to output workspaces and record the history for ths algorithm
virtual void fillHistory();

/// Set to true to stop execution
bool m_cancel;
Expand Down Expand Up @@ -348,8 +350,6 @@ class MANTID_API_DLL Algorithm : public IAlgorithm, public Kernel::PropertyManag
void logAlgorithmInfo() const;

bool executeAsyncImpl(const Poco::Void & i);
/// Copy workspace history for input workspaces to output workspaces and record the history for ths algorithm
void fillHistory();

// --------------------- Private Members -----------------------------------
/// Poco::ActiveMethod used to implement asynchronous execution.
Expand Down
31 changes: 31 additions & 0 deletions Code/Mantid/Framework/Algorithms/inc/MantidAlgorithms/MergeRuns.h
Expand Up @@ -73,6 +73,11 @@ class DLLExport MergeRuns : public API::MultiPeriodGroupAlgorithm
virtual const std::string category() const { return "Arithmetic";}
// Overriden MultiPeriodGroupAlgorithm method.
bool useCustomInputPropertyName() const;

protected:
/// Overriden fillHistory method to correctly store history from merged workspaces
virtual void fillHistory();

private:

// Overridden Algorithm methods
Expand All @@ -86,6 +91,30 @@ class DLLExport MergeRuns : public API::MultiPeriodGroupAlgorithm
void testCompatibility(API::MatrixWorkspace_const_sptr ws, const std::string& xUnitID, const std::string& YUnit, const bool dist, const std::string instrument) const;
/// An addition table is a list of pairs: First int = workspace index in the EW being added, Second int = workspace index to which it will be added in the OUTPUT EW. -1 if it should add a new entry at the end.
typedef std::vector< std::pair<int, int> > AdditionTable;
/// Copy the history from the input workspaces to the output workspaces
template <typename Container>
void copyHistoryFromInputWorkspaces(const Container& workspaces)
{
API::Workspace_sptr outWS = this->getProperty("OutputWorkspace");

//this is not a child algorithm. Add the history algorithm to the WorkspaceHistory object.
if (!isChild())
{
// Loop over the input workspaces, making the call that copies their history to the output ones
// (Protection against copy to self is in WorkspaceHistory::copyAlgorithmHistory)
for (auto inWS = workspaces.begin(); inWS != workspaces.end(); ++inWS)
{
outWS->history().addHistory( (*inWS)->getHistory() );
}
// Add the history for the current algorithm to all the output workspaces
outWS->history().addHistory(m_history);
}
//this is a child algorithm, but we still want to keep the history.
else if (isRecordingHistoryForChild() && m_parentHistory)
{
m_parentHistory->addChildHistory(m_history);
}
}

// Methods called by exec()
using Mantid::API::Algorithm::validateInputs;
Expand All @@ -101,6 +130,8 @@ class DLLExport MergeRuns : public API::MultiPeriodGroupAlgorithm

/// List of input EVENT workspaces
std::vector<Mantid::DataObjects::EventWorkspace_sptr> m_inEventWS;
/// List of input matrix workspace
std::list<API::MatrixWorkspace_sptr> m_inMatrixWS;
/// Addition tables for event workspaces
std::vector<boost::shared_ptr<AdditionTable>> m_tables;
};
Expand Down
30 changes: 23 additions & 7 deletions Code/Mantid/Framework/Algorithms/src/MergeRuns.cpp
Expand Up @@ -54,7 +54,8 @@ using namespace API;
using namespace DataObjects;

/// Default constructor
MergeRuns::MergeRuns() : MultiPeriodGroupAlgorithm(),m_progress(NULL){}
MergeRuns::MergeRuns() : MultiPeriodGroupAlgorithm(),m_progress(NULL), m_inEventWS(), m_inMatrixWS(), m_tables()
{}

/// Destructor
MergeRuns::~MergeRuns()
Expand All @@ -70,7 +71,7 @@ void MergeRuns::init()
declareProperty(
new ArrayProperty<std::string>("InputWorkspaces", boost::make_shared<MandatoryValidator<std::vector<std::string>>>()),
"The names of the input workspaces as a comma-separated list. You may also group workspaces using the GUI or [[GroupWorkspaces]], and specify the name of the group instead." );
declareProperty(new WorkspaceProperty<>("OutputWorkspace","",Direction::Output),
declareProperty(new WorkspaceProperty<Workspace>("OutputWorkspace","",Direction::Output),
"Name of the output workspace" );
}

Expand Down Expand Up @@ -132,16 +133,16 @@ void MergeRuns::exec()
//At least one is not event workspace ----------------

//This gets the list of workspaces
std::list<API::MatrixWorkspace_sptr> inWS = this->validateInputs(inputs);
m_inMatrixWS = this->validateInputs(inputs);

// Iterate over the collection of input workspaces
auto it = inWS.begin();
auto it = m_inMatrixWS.begin();
// Take the first input workspace as the first argument to the addition
MatrixWorkspace_sptr outWS = inWS.front();
int64_t n=inWS.size()-1;
MatrixWorkspace_sptr outWS = m_inMatrixWS.front();
int64_t n=m_inMatrixWS.size()-1;
m_progress=new Progress(this,0.0,1.0,n);
// Note that the iterator is incremented before first pass so that 1st workspace isn't added to itself
for (++it; it != inWS.end(); ++it)
for (++it; it != m_inMatrixWS.end(); ++it)
{
MatrixWorkspace_sptr addee;
// Only do a rebinning if the bins don't already match - otherwise can just add (see the 'else')
Expand Down Expand Up @@ -690,5 +691,20 @@ API::MatrixWorkspace_sptr MergeRuns::rebinInput(const API::MatrixWorkspace_sptr&
return rebin->getProperty("OutputWorkspace");
}

/** Overriden fillHistory method to correctly store history from merged workspaces.
*/
void MergeRuns::fillHistory()
{
// check if we were merging event or matrix workspaces
if(!m_inEventWS.empty())
{
copyHistoryFromInputWorkspaces<std::vector<EventWorkspace_sptr> >(m_inEventWS);
}
else
{
copyHistoryFromInputWorkspaces<std::list<MatrixWorkspace_sptr> >(m_inMatrixWS);
}
}

} // namespace Algorithm
} // namespace Mantid

0 comments on commit c95e703

Please sign in to comment.