Skip to content

Commit

Permalink
Refs #8922 Fix SaveNexus and SaveNexusProcessed to capture history.
Browse files Browse the repository at this point in the history
SaveNexus and SaveNexusProcessed wouldn't append a record to the history.
This commit fixes that issue and leaves a record on the input workspace so the result is that we get a
complete history, but we lose the execution start time and duration.
  • Loading branch information
Samuel Jackson committed May 14, 2014
1 parent 120dc06 commit 60dfa90
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 19 deletions.
8 changes: 5 additions & 3 deletions Code/Mantid/Framework/API/inc/MantidAPI/Algorithm.h
Expand Up @@ -349,6 +349,9 @@ class MANTID_API_DLL Algorithm : public IAlgorithm, public Kernel::PropertyManag
Kernel::Logger m_log;
Kernel::Logger &g_log;

/// Pointer to the parent history object (if set)
boost::shared_ptr<AlgorithmHistory> m_parentHistory;

private:
/// Private Copy constructor: NO COPY ALLOWED
Algorithm(const Algorithm&);
Expand All @@ -359,11 +362,12 @@ class MANTID_API_DLL Algorithm : public IAlgorithm, public Kernel::PropertyManag
void unlockWorkspaces();

void store();
void fillHistory();

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 Expand Up @@ -411,8 +415,6 @@ class MANTID_API_DLL Algorithm : public IAlgorithm, public Kernel::PropertyManag
size_t m_groupSize;
/// All the groups have similar names (group_1, group_2 etc.)
bool m_groupsHaveSimilarNames;
/// Pointer to the parent history object (if set)
boost::shared_ptr<AlgorithmHistory> m_parentHistory;
/// A non-recursive mutex for thread-safety
mutable Kernel::Mutex m_mutex;
};
Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/API/inc/MantidAPI/AlgorithmHistory.h
Expand Up @@ -126,9 +126,9 @@ class MANTID_API_DLL AlgorithmHistory
// Allow Algorithm::execute to change the exec count & duration after the algorithm was executed
friend class Algorithm;

private:
// Set the execution count
void setExecCount(std::size_t execCount) { m_execCount = execCount; }
private:
/// The name of the Algorithm
std::string m_name;
/// The version of the algorithm
Expand Down
8 changes: 3 additions & 5 deletions Code/Mantid/Framework/API/src/AlgorithmHistory.cpp
Expand Up @@ -155,11 +155,9 @@ void AlgorithmHistory::printSelf(std::ostream& os, const int indent)const
{
os << std::string(indent,' ') << "Algorithm: " << m_name;
os << std::string(indent,' ') << " v" << m_version << std::endl;
if (m_executionDate != Mantid::Kernel::DateAndTime::defaultTime())
{
os << std::string(indent,' ') << "Execution Date: " << m_executionDate.toFormattedString() <<std::endl;
os << std::string(indent,' ') << "Execution Duration: "<< m_executionDuration << " seconds" << std::endl;
}
os << std::string(indent,' ') << "Execution Date: " << m_executionDate.toFormattedString() <<std::endl;
os << std::string(indent,' ') << "Execution Duration: "<< m_executionDuration << " seconds" << std::endl;

std::vector<Kernel::PropertyHistory>::const_iterator it;
os << std::string(indent,' ') << "Parameters:" <<std::endl;

Expand Down
10 changes: 5 additions & 5 deletions Code/Mantid/Framework/API/src/WorkspaceHistory.cpp
Expand Up @@ -295,7 +295,7 @@ void WorkspaceHistory::loadNestedHistory(::NeXus::File * file, AlgorithmHistory_
}
else
{
//if not parent point is supplied, asssume we're at the top
//if not parent point is supplied, assume we're at the top
//and attach the history to the workspace
this->addHistory(history);
}
Expand All @@ -321,7 +321,7 @@ std::set<int> WorkspaceHistory::findHistoryEntries(::NeXus::File* file)
std::map<std::string, std::string> entries;
file->getEntries(entries);

// Histories are numberd MantidAlgorithm_0, ..., MantidAlgorithm_10, etc.
// Histories are numbered MantidAlgorithm_0, ..., MantidAlgorithm_10, etc.
// Find all the unique numbers
for (auto it = entries.begin(); it != entries.end(); ++it)
{
Expand Down Expand Up @@ -381,21 +381,21 @@ AlgorithmHistory_sptr WorkspaceHistory::parseAlgorithmHistory(const std::string&
Poco::DateTime start_timedate;
//This is needed by the Poco parsing function
int tzdiff(-1);
Mantid::Kernel::DateAndTime utc_start;
if( !Poco::DateTimeParser::tryParse("%Y-%b-%d %H:%M:%S", date + " " + time, start_timedate, tzdiff))
{
g_log.warning() << "Error parsing start time in algorithm history entry." << "\n";
throw std::runtime_error("Malformed history record: could not parse algorithm start time.");
utc_start = Kernel::DateAndTime::defaultTime();
}
//Get the duration
getWordsInString(info[EXEC_DUR], dummy, dummy, temp, dummy);
double dur = boost::lexical_cast<double>(temp);
if ( dur < 0.0 )
{
g_log.warning() << "Error parsing start time in algorithm history entry." << "\n";
throw std::runtime_error("Malformed history record: could not parse algorithm duration.");
dur = -1.0;
}
//Convert the timestamp to time_t to DateAndTime
Mantid::Kernel::DateAndTime utc_start;
utc_start.set_from_time_t( start_timedate.timestamp().epochTime() );
//Create the algorithm history
API::AlgorithmHistory alg_hist(algName, version, utc_start, dur,Algorithm::g_execCount);
Expand Down
Expand Up @@ -76,9 +76,9 @@ namespace Mantid
static void appendEventListData( std::vector<T> events, size_t offset, double * tofs, float * weights, float * errorSquareds, int64_t * pulsetimes);

void execEvent(Mantid::NeXus::NexusFileIO * nexusFile,const bool uniformSpectra,const std::vector<int> spec);
/// sets non workspace properties for the algorithm
/// sets non workspace properties for the algorithm
void setOtherProperties(IAlgorithm* alg,const std::string & propertyName,const std::string &propertyValue,int perioidNum);

/// The name and path of the input file
std::string m_filename;
/// The name and path of the input file
Expand All @@ -91,7 +91,6 @@ namespace Mantid
DataObjects::EventWorkspace_const_sptr m_eventWorkspace;
/// Proportion of progress time expected to write initial part
double m_timeProgInit;

/// Progress bar
API::Progress * prog;

Expand Down
14 changes: 14 additions & 0 deletions Code/Mantid/Framework/DataHandling/src/SaveNexus.cpp
Expand Up @@ -195,6 +195,20 @@ void SaveNexus::runSaveNexusProcessed()
// Pass through the append property
saveNexusPro->setProperty<bool>("Append",getProperty("Append"));

// If we're tracking history, add the entry before we save it to file
if (trackingHistory())
{
m_history->setExecCount(Algorithm::g_execCount);
if (!isChild())
{
m_inputWorkspace->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);
}
}
// Now execute the Child Algorithm. Catch and log any error, but don't stop.
try
{
Expand Down
17 changes: 15 additions & 2 deletions Code/Mantid/Framework/DataHandling/src/SaveNexusProcessed.cpp
Expand Up @@ -347,10 +347,23 @@ namespace DataHandling
} // finish table workspace specifics

// Switch to the Cpp API for the algorithm history
inputWorkspace->getHistory().saveNexus(cppFile);
if (trackingHistory())
{
m_history->setExecCount(Algorithm::g_execCount);
if (!isChild())
{
inputWorkspace->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);
}
}

inputWorkspace->history().saveNexus(cppFile);

nexusFile->closeNexusFile();

delete nexusFile;

return;
Expand Down

0 comments on commit 60dfa90

Please sign in to comment.