Skip to content

Commit

Permalink
Refs #10555. Re-implement how to add sample logs.
Browse files Browse the repository at this point in the history
Refined how to add sample logs to the generated MDWorkspaces.
And expanded the unit test for the sample logs.
  • Loading branch information
wdzhou committed Feb 6, 2015
1 parent f93301d commit 8760e65
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 18 deletions.
66 changes: 52 additions & 14 deletions Code/Mantid/Framework/MDAlgorithms/src/LoadHFIRPDData.cpp
Expand Up @@ -563,23 +563,41 @@ void LoadHFIRPDData::appendSampleLogs(
IMDEventWorkspace_sptr mdws,
const std::map<std::string, std::vector<double> > &logvecmap,
const std::vector<Kernel::DateAndTime> &vectimes) {

/// FIXME *** TODO *** : Do not make a long list of time series property for
/// run 1.
/// BUT assign the relevant sample values to each ExperimentInfo of each run
/// Only make the properties in the ParentWorkspace to Sample log of run 0???

// Check!
size_t numexpinfo = mdws->getNumExperimentInfo();
if (numexpinfo == 0)
throw std::runtime_error(
"There is no ExperimentInfo defined for MDWorkspace. "
"It is impossible to add any log!");

// Process the sample logs for MD workspace
ExperimentInfo_sptr ei = mdws->getExperimentInfo(0);
else if (numexpinfo != vectimes.size() + 1)
throw std::runtime_error(
"The number of ExperimentInfo should be 1 more than "
"the length of vector of time, i.e., number of matrix workspaces.");

std::map<std::string, std::vector<double> >::const_iterator miter;

// get runnumber vector
std::string runnumlogname = getProperty("RunNumberName");
miter = logvecmap.find(runnumlogname);
if (miter == logvecmap.end())
throw std::runtime_error("Impossible not to find Pt. in log vec map.");
const std::vector<double> &vecrunno = miter->second;

// Add run_start to each ExperimentInfo
for (size_t i = 0; i < vectimes.size(); ++i) {
Kernel::DateAndTime runstart = vectimes[i];
mdws->getExperimentInfo(i)->mutableRun().addLogData(
new PropertyWithValue<std::string>("run_start",
runstart.toFormattedString()));
}
mdws->getExperimentInfo(vectimes.size())->mutableRun().addLogData(
new PropertyWithValue<std::string>("run_start",
vectimes[0].toFormattedString()));

// Add sample logs
// get hold of last experiment info
ExperimentInfo_sptr eilast = mdws->getExperimentInfo(numexpinfo - 1);

for (miter = logvecmap.begin(); miter != logvecmap.end(); ++miter) {
std::string logname = miter->first;
const std::vector<double> &veclogval = miter->second;
Expand All @@ -594,23 +612,36 @@ void LoadHFIRPDData::appendSampleLogs(
continue;
}

// For N single value experiment info
for (size_t i = 0; i < veclogval.size(); ++i) {
// get ExperimentInfo
ExperimentInfo_sptr tmpei = mdws->getExperimentInfo(i);
// check run number matches
int runnumber =
atoi(tmpei->run().getProperty("run_number")->value().c_str());
if (runnumber != static_cast<int>(vecrunno[i]))
throw std::runtime_error("Run number does not match to Pt. value.");
// add property
tmpei->mutableRun().addLogData(
new PropertyWithValue<double>(logname, veclogval[i]));
}

// Create a new log
TimeSeriesProperty<double> *templog =
new TimeSeriesProperty<double>(logname);
templog->addValues(vectimes, veclogval);

// Add log to experiment info
ei->mutableRun().addLogData(templog);
}
eilast->mutableRun().addLogData(templog);

// MD workspace add experimental information
mdws->addExperimentInfo(ei);
// Add log value to each ExperimentInfo for the first N
}

return;
}

//---------------------------------------------------------------------------------
/** Append Experiment Info
/** Add Experiment Info to the MDWorkspace. Add 1+N ExperimentInfo
* @brief LoadHFIRPDData::addExperimentInfos
* @param mdws
* @param vec_ws2d
Expand All @@ -620,6 +651,7 @@ void LoadHFIRPDData::addExperimentInfos(
API::IMDEventWorkspace_sptr mdws,
const std::vector<API::MatrixWorkspace_sptr> vec_ws2d,
const int &init_runnumber) {
// Add N experiment info as there are N measurment points
for (size_t i = 0; i < vec_ws2d.size(); ++i) {
// Create an ExperimentInfo object
ExperimentInfo_sptr tmp_expinfo = boost::make_shared<ExperimentInfo>();
Expand All @@ -633,6 +665,12 @@ void LoadHFIRPDData::addExperimentInfos(
mdws->addExperimentInfo(tmp_expinfo);
}

// Add one additional in order to contain the combined sample logs
ExperimentInfo_sptr combine_expinfo = boost::make_shared<ExperimentInfo>();
combine_expinfo->mutableRun().addProperty(
new PropertyWithValue<int>("run_number", init_runnumber - 1));
mdws->addExperimentInfo(combine_expinfo);

return;
}
} // namespace DataHandling
Expand Down
46 changes: 42 additions & 4 deletions Code/Mantid/Framework/MDAlgorithms/test/LoadHFIRPDDataTest.h
Expand Up @@ -10,13 +10,16 @@
#include "MantidDataHandling/LoadInstrument.h"
#include "MantidAPI/IMDIterator.h"
#include "MantidGeometry/IComponent.h"
#include "MantidKernel/Property.h"
#include "MantidKernel/TimeSeriesProperty.h"

using Mantid::MDAlgorithms::LoadHFIRPDData;
using Mantid::DataHandling::LoadInstrument;
using Mantid::DataHandling::LoadSpiceAscii;

using namespace Mantid::API;
using namespace Mantid::DataObjects;
using namespace Mantid::Kernel;

class LoadHFIRPDDataTest : public CxxTest::TestSuite {
public:
Expand Down Expand Up @@ -142,18 +145,53 @@ class LoadHFIRPDDataTest : public CxxTest::TestSuite {
TS_ASSERT_DELTA(lastx, 1.57956, 0.0001);

// Experiment information
/// FIXME - This is wrong! why 62? where the +1 comes from?
uint16_t numexpinfo = mdws->getNumExperimentInfo();
// TS_ASSERT_EQUALS(numexpinfo, 61);
TS_ASSERT_EQUALS(numexpinfo, 61 + 1);

// Check run number
ExperimentInfo_const_sptr expinfo0 = mdws->getExperimentInfo(0);
TS_ASSERT(expinfo0);
TS_ASSERT_EQUALS(expinfo0->getRunNumber(), 1);

/// FIXME - Run number is not right!
ExperimentInfo_const_sptr expinfo61 = mdws->getExperimentInfo(61);
TS_ASSERT(expinfo61);
TS_ASSERT_EQUALS(expinfo61->getRunNumber(), 124);
TS_ASSERT_EQUALS(expinfo61->getRunNumber(), 0);

// Check log and comparing with run_start
Mantid::Kernel::Property *tempa = expinfo61->run().getProperty("temp_a");
TS_ASSERT(tempa);
Mantid::Kernel::TimeSeriesProperty<double> *timeseriestempa =
dynamic_cast<Mantid::Kernel::TimeSeriesProperty<double> *>(tempa);
TS_ASSERT(timeseriestempa);

std::vector<DateAndTime> times = timeseriestempa->timesAsVector();
TS_ASSERT_EQUALS(times.size(), 61);
DateAndTime time0 = times[0];
TS_ASSERT_EQUALS(time0.toFormattedString(), "2012-Aug-13 11:57:33");
DateAndTime time1 = times[1];
TS_ASSERT_EQUALS(time1.toFormattedString(), "2012-Aug-13 11:58:03");

// Examine Monitor MDWorkspace
IMDWorkspace_const_sptr monmdws = boost::dynamic_pointer_cast<IMDWorkspace>(
AnalysisDataService::Instance().retrieve("MonitorMDW"));

// Check the IMDEvent workspace generated
numevents = monmdws->getNEvents();
TS_ASSERT_EQUALS(numevents, 44 * 61);

mditer = monmdws->createIterator();
TS_ASSERT_EQUALS(mditer->getNumEvents(), 44 * 61);

y0 = mditer->getInnerSignal(0);
TS_ASSERT_DELTA(y0, 31964.000, 0.1);
yl = mditer->getInnerSignal(44 * 61 - 1);
TS_ASSERT_DELTA(yl, 31968.0, 0.1);

// Remove workspaces
AnalysisDataService::Instance().remove("DataTable");
AnalysisDataService::Instance().remove("LogParentWS");
AnalysisDataService::Instance().remove("HB2A_MD");
AnalysisDataService::Instance().remove("MonitorMDW");
}
};

Expand Down

0 comments on commit 8760e65

Please sign in to comment.