Skip to content

Commit

Permalink
Refs #11043. Generating output tables
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Wedel committed Mar 18, 2015
1 parent 7151e2c commit 9452f4e
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 9 deletions.
Expand Up @@ -55,6 +55,11 @@ class DLLExport PawleyFit : public API::Algorithm {

Kernel::V3D getHkl(const std::string &hklString) const;

API::ITableWorkspace_sptr
getLatticeFromFunction(const PawleyFunction_sptr &pawleyFn) const;
API::ITableWorkspace_sptr
getPeakParametersFromFunction(const PawleyFunction_sptr &pawleyFn) const;

void init();
void exec();
};
Expand Down
Expand Up @@ -119,6 +119,8 @@ class PawleyFunction : public API::FunctionParameterDecorator {
API::IPeakFunction_sptr getPeakFunction(size_t i) const;
Kernel::V3D getPeakHKL(size_t i) const;

PawleyParameterFunction_sptr getPawleyParameterFunction() const;

protected:
void init();
void beforeDecoratedFunctionSet(const API::IFunction_sptr &fn);
Expand Down
85 changes: 76 additions & 9 deletions Code/Mantid/Framework/CurveFitting/src/PawleyFit.cpp
Expand Up @@ -48,9 +48,11 @@ void PawleyFit::addHKLsToFunction(PawleyFunction_sptr &pawleyFn,

try {
V3D hkl = getHkl(currentRow.String(0));
double d = boost::lexical_cast<double>(currentRow.String(1));
double height = boost::lexical_cast<double>(currentRow.String(3));
double fwhm = boost::lexical_cast<double>(currentRow.String(4)) * d;

pawleyFn->addPeak(hkl, 0.006, height);
pawleyFn->addPeak(hkl, fwhm, height);
}
catch (...) {
// do nothing.
Expand All @@ -74,6 +76,65 @@ V3D PawleyFit::getHkl(const std::string &hklString) const {
return hkl;
}

ITableWorkspace_sptr
PawleyFit::getLatticeFromFunction(const PawleyFunction_sptr &pawleyFn) const {
if (!pawleyFn) {
throw std::invalid_argument(
"Cannot extract lattice parameters from null function.");
}

ITableWorkspace_sptr latticeParameterTable =
WorkspaceFactory::Instance().createTable();

latticeParameterTable->addColumn("str", "Parameter");
latticeParameterTable->addColumn("double", "Value");
latticeParameterTable->addColumn("double", "Error");

PawleyParameterFunction_sptr parameters =
pawleyFn->getPawleyParameterFunction();

for (size_t i = 0; i < parameters->nParams(); ++i) {
TableRow newRow = latticeParameterTable->appendRow();
newRow << parameters->parameterName(i) << parameters->getParameter(i)
<< parameters->getError(i);
}

return latticeParameterTable;
}

ITableWorkspace_sptr PawleyFit::getPeakParametersFromFunction(
const PawleyFunction_sptr &pawleyFn) const {
if (!pawleyFn) {
throw std::invalid_argument(
"Cannot extract peak parameters from null function.");
}

ITableWorkspace_sptr peakParameterTable =
WorkspaceFactory::Instance().createTable();

peakParameterTable->addColumn("int", "Peak");
peakParameterTable->addColumn("V3D", "HKL");
peakParameterTable->addColumn("str", "Parameter");
peakParameterTable->addColumn("double", "Value");
peakParameterTable->addColumn("double", "Error");

for (size_t i = 0; i < pawleyFn->getPeakCount(); ++i) {

IPeakFunction_sptr currentPeak = pawleyFn->getPeakFunction(i);

int peakNumber = static_cast<int>(i + 1);
V3D peakHKL = pawleyFn->getPeakHKL(i);

for (size_t j = 0; j < currentPeak->nParams(); ++j) {
TableRow newRow = peakParameterTable->appendRow();
newRow << peakNumber << peakHKL << currentPeak->parameterName(j)
<< currentPeak->getParameter(j) << currentPeak->getError(j);
}
}

return peakParameterTable;
}

void PawleyFit::init() {
declareProperty(new WorkspaceProperty<MatrixWorkspace>("InputWorkspace", "",
Direction::Input),
Expand Down Expand Up @@ -127,6 +188,16 @@ void PawleyFit::init() {
Direction::Output),
"Workspace that contains measured spectrum, calculated "
"spectrum and difference curve.");

declareProperty(
new WorkspaceProperty<ITableWorkspace>("RefinedCellTable", "",
Direction::Output),
"TableWorkspace with refined lattice parameters, including errors.");

declareProperty(
new WorkspaceProperty<ITableWorkspace>("RefinedPeakParameterTable", "",
Direction::Output),
"TableWorkspace with refined peak parameters, including errors.");
}

void PawleyFit::exec() {
Expand All @@ -149,7 +220,7 @@ void PawleyFit::exec() {
std::vector<V3D> hkls = hklsFromString(getProperty("MillerIndices"));

const MantidVec &data = ws->readY(static_cast<size_t>(wsIndex));
pawleyFn->setPeaks(hkls, 0.008,
pawleyFn->setPeaks(hkls, 0.005,
*(std::max_element(data.begin(), data.end())));
}

Expand All @@ -168,17 +239,13 @@ void PawleyFit::exec() {
boost::const_pointer_cast<MatrixWorkspace>(ws));
fit->setProperty("WorkspaceIndex", wsIndex);
fit->setProperty("CreateOutput", true);

fit->execute();

for (size_t i = 0; i < pawleyFn->nParams(); ++i) {
std::cout << i << " " << pawleyFn->parameterName(i) << " "
<< pawleyFn->getParameter(i) << " " << pawleyFn->getError(i)
<< std::endl;
}

MatrixWorkspace_sptr output = fit->getProperty("OutputWorkspace");
setProperty("OutputWorkspace", output);
setProperty("RefinedCellTable", getLatticeFromFunction(pawleyFn));
setProperty("RefinedPeakParameterTable",
getPeakParametersFromFunction(pawleyFn));
}

} // namespace CurveFitting
Expand Down
5 changes: 5 additions & 0 deletions Code/Mantid/Framework/CurveFitting/src/PawleyFunction.cpp
Expand Up @@ -331,6 +331,11 @@ IPeakFunction_sptr PawleyFunction::getPeakFunction(size_t i) const {

Kernel::V3D PawleyFunction::getPeakHKL(size_t i) const { return m_hkls[i]; }

PawleyParameterFunction_sptr
PawleyFunction::getPawleyParameterFunction() const {
return m_pawleyParameterFunction;
}

void PawleyFunction::init() {
setDecoratedFunction("CompositeFunction");

Expand Down

0 comments on commit 9452f4e

Please sign in to comment.