From d776e34a3abc6159c41b2777dfa5e061a93c8a6b Mon Sep 17 00:00:00 2001
From: Vickie Lynch <lynchve@ornl.gov>
Date: Fri, 23 Oct 2015 16:38:29 -0400
Subject: [PATCH] Refs #14072 use LatticeFunction not LatticeErrors

---
 Framework/Crystal/CMakeLists.txt              |   2 -
 .../Crystal/inc/MantidCrystal/LatticeErrors.h |  75 ----
 .../OptimizeLatticeForCellType.h              |  17 +-
 Framework/Crystal/src/LatticeErrors.cpp       |  88 -----
 .../src/OptimizeLatticeForCellType.cpp        | 349 ++++++------------
 .../test/OptimizeLatticeForCellTypeTest.h     |   7 +-
 6 files changed, 122 insertions(+), 416 deletions(-)
 delete mode 100644 Framework/Crystal/inc/MantidCrystal/LatticeErrors.h
 delete mode 100644 Framework/Crystal/src/LatticeErrors.cpp

diff --git a/Framework/Crystal/CMakeLists.txt b/Framework/Crystal/CMakeLists.txt
index 75e9b1fbbc7..23a4ae3ce45 100644
--- a/Framework/Crystal/CMakeLists.txt
+++ b/Framework/Crystal/CMakeLists.txt
@@ -28,7 +28,6 @@ set ( SRC_FILES
 	src/IntegratePeakTimeSlices.cpp
 	src/IntegratePeaksHybrid.cpp
 	src/IntegratePeaksUsingClusters.cpp
-	src/LatticeErrors.cpp
 	src/LoadHKL.cpp
 	src/LoadIsawPeaks.cpp
 	src/LoadIsawSpectrum.cpp
@@ -102,7 +101,6 @@ set ( INC_FILES
 	inc/MantidCrystal/IntegratePeakTimeSlices.h
 	inc/MantidCrystal/IntegratePeaksHybrid.h
 	inc/MantidCrystal/IntegratePeaksUsingClusters.h
-	inc/MantidCrystal/LatticeErrors.h
 	inc/MantidCrystal/LoadHKL.h
 	inc/MantidCrystal/LoadIsawPeaks.h
 	inc/MantidCrystal/LoadIsawSpectrum.h
diff --git a/Framework/Crystal/inc/MantidCrystal/LatticeErrors.h b/Framework/Crystal/inc/MantidCrystal/LatticeErrors.h
deleted file mode 100644
index eb4216b722c..00000000000
--- a/Framework/Crystal/inc/MantidCrystal/LatticeErrors.h
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * LatticeErrors.h
- *
- *  Created on: Jan 26, 2013
- *      Author: ruth
- */
-
-#ifndef PEAKHKLERRORS_H_
-#define PEAKHKLERRORS_H_
-#include "MantidKernel/System.h"
-
-#include "MantidAPI/IFunction.h"
-#include "MantidGeometry/Instrument.h"
-#include "MantidAPI/ParamFunction.h"
-#include "MantidAPI/IFunction1D.h"
-#include "MantidDataObjects/PeaksWorkspace.h"
-#include "MantidAPI/IFunction.h"
-#include "MantidGeometry/Crystal/OrientedLattice.h"
-#include "MantidKernel/Matrix.h"
-
-namespace Mantid {
-namespace Crystal {
-
-/**
-
-  @author Ruth Mikkelson, SNS,ORNL
-  @date 01/26/2013
- Copyright &copy; 2012 ISIS Rutherford Appleton Laboratory, NScD Oak Ridge
- National Laboratory & European Spallation Source
-
- This file is part of Mantid.
-
- Mantid is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 3 of the License, or
- (at your option) any later version.
-
- Mantid is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
- File change history is stored at: <https://github.com/mantidproject/mantid>
- Code Documentation is available at: <http://doxygen.mantidproject.org>
- */
-class DLLExport LatticeErrors : public API::ParamFunction,
-                                public API::IFunction1D {
-public:
-  LatticeErrors();
-  virtual ~LatticeErrors();
-
-  std::string name() const { return std::string("LatticeErrors"); };
-
-  virtual int version() const { return 1; };
-
-  const std::string category() const { return "Crystal"; };
-
-  void function1D(double *out, const double *xValues, const size_t nData) const;
-
-  // void functionDeriv1D(Mantid::API::Jacobian* out, const double *xValues,
-  // const size_t nData);
-
-  void functionDerivLocal(API::Jacobian *, const double *, const size_t);
-  void functionDeriv(const API::FunctionDomain &domain,
-                     API::Jacobian &jacobian);
-
-  void init();
-};
-}
-}
-
-#endif /* PEAKHKLERRORS_H_ */
diff --git a/Framework/Crystal/inc/MantidCrystal/OptimizeLatticeForCellType.h b/Framework/Crystal/inc/MantidCrystal/OptimizeLatticeForCellType.h
index 75747b2ce28..36f90a39c66 100644
--- a/Framework/Crystal/inc/MantidCrystal/OptimizeLatticeForCellType.h
+++ b/Framework/Crystal/inc/MantidCrystal/OptimizeLatticeForCellType.h
@@ -41,7 +41,7 @@ namespace Crystal {
  Code Documentation is available at: <http://doxygen.mantidproject.org>
  */
 class DLLExport OptimizeLatticeForCellType : public API::Algorithm {
- public:
+public:
   /// Default constructorMatrix
   OptimizeLatticeForCellType();
   /// Destructor
@@ -59,22 +59,21 @@ class DLLExport OptimizeLatticeForCellType : public API::Algorithm {
   virtual int version() const { return 1; }
   /// Algorithm's category for identification overriding a virtual method
   virtual const std::string category() const { return "Crystal"; }
-  /// Call TOFLattice as a Child Algorithm to get statistics of the peaks
-  double optLatticeSum(std::string inname, std::string cell_type,
-                       std::vector<double> &params);
-  void optLattice(std::string inname, std::vector<double> &params, double *out);
 
- private:
+  std::vector<double> outParams(std::string cell_type, int icol,
+                                API::ITableWorkspace_sptr ParamTable);
+  std::string inParams(std::string cell_type, std::vector<double> &lat);
+
+private:
   // Overridden Algorithm methods
   void init();
   void exec();
   /// Function to find peaks near detector edge
   bool edgePixel(DataObjects::PeaksWorkspace_sptr ws, std::string bankName,
                  int col, int row, int Edge);
-  Kernel::DblMatrix aMatrix(std::vector<double> lattice);
 };
 
-}  // namespace Algorithm
-}  // namespace Mantid
+} // namespace Algorithm
+} // namespace Mantid
 
 #endif /*MANTID_CRYSTAL_OptimizeLatticeForCellType_H_*/
diff --git a/Framework/Crystal/src/LatticeErrors.cpp b/Framework/Crystal/src/LatticeErrors.cpp
deleted file mode 100644
index a0a5306a38b..00000000000
--- a/Framework/Crystal/src/LatticeErrors.cpp
+++ /dev/null
@@ -1,88 +0,0 @@
-/*
- * LatticeErrors.cpp
- *
-
- */
-#include "MantidAPI/FunctionFactory.h"
-#include "MantidAPI/IConstraint.h"
-#include "MantidAPI/IFunction1D.h"
-#include "MantidGeometry/Crystal/IPeak.h"
-#include "MantidAPI/ParamFunction.h"
-#include "MantidCrystal/LatticeErrors.h"
-#include "MantidCrystal/SCDPanelErrors.h"
-#include "MantidCrystal/OptimizeLatticeForCellType.h"
-
-using namespace Mantid::DataObjects;
-using namespace Mantid::API;
-using namespace Mantid::Kernel;
-using namespace Mantid::Geometry;
-using Mantid::Geometry::CompAssembly;
-using Mantid::Geometry::IObjComponent_const_sptr;
-
-namespace Mantid {
-
-namespace Crystal {
-namespace {
-/// static logger
-Kernel::Logger g_log("LatticeErrors");
-}
-
-DECLARE_FUNCTION(LatticeErrors)
-
-LatticeErrors::LatticeErrors() : ParamFunction(), IFunction1D() {}
-
-LatticeErrors::~LatticeErrors() {}
-
-void LatticeErrors::init() {
-  declareParameter("p0", 0.0, "a lattice param");
-  declareParameter("p1", 0.0, "b lattice param");
-  declareParameter("p2", 0.0, "c lattice param");
-  declareParameter("p3", 0.0, "alpha lattice param");
-  declareParameter("p4", 0.0, "beta lattice param");
-  declareParameter("p5", 0.0, "gamma lattice param");
-}
-
-/**
- * Calculates the h,k, and l offsets from an integer for (some of )the peaks,
- *given the parameter values.
- *
- * @param out  For each peak element in this array.
- * @param xValues  xValues give the index in the PeaksWorkspace for the peak.
- *For each peak considered there are
- *              three consecutive entries all with the same index
- * @param nData The size of the xValues and out arrays
- */
-void LatticeErrors::function1D(double *out, const double *xValues,
-                               const size_t nData) const {
-  UNUSED_ARG(xValues);
-  std::string PeakWorkspaceName = "_peaks";
-  std::vector<double> Params;
-  Params.push_back(getParameter("p0"));
-  Params.push_back(getParameter("p1"));
-  Params.push_back(getParameter("p2"));
-  Params.push_back(getParameter("p3"));
-  Params.push_back(getParameter("p4"));
-  Params.push_back(getParameter("p5"));
-
-  Mantid::Crystal::OptimizeLatticeForCellType u;
-  u.optLattice(PeakWorkspaceName, Params, out);
-  double ChiSqTot = 0;
-  for (size_t i = 0; i < nData; i++) {
-    ChiSqTot += out[i];
-  }
-
-  g_log.debug() << "    Chi**2 = " << ChiSqTot << "     nData = " << nData
-                << std::endl;
-}
-void LatticeErrors::functionDerivLocal(API::Jacobian *, const double *,
-                                       const size_t) {
-  throw Mantid::Kernel::Exception::NotImplementedError(
-      "functionDerivLocal is not implemented for LatticeErrors.");
-}
-
-void LatticeErrors::functionDeriv(const API::FunctionDomain &domain,
-                                  API::Jacobian &jacobian) {
-  calNumericalDeriv(domain, jacobian);
-}
-}
-}
diff --git a/Framework/Crystal/src/OptimizeLatticeForCellType.cpp b/Framework/Crystal/src/OptimizeLatticeForCellType.cpp
index f580bdbd8e0..83f7bb62841 100644
--- a/Framework/Crystal/src/OptimizeLatticeForCellType.cpp
+++ b/Framework/Crystal/src/OptimizeLatticeForCellType.cpp
@@ -45,9 +45,6 @@ void OptimizeLatticeForCellType::init() {
   cellTypes.push_back(ReducedCell::HEXAGONAL());
   cellTypes.push_back(ReducedCell::RHOMBOHEDRAL());
   cellTypes.push_back(ReducedCell::MONOCLINIC());
-  cellTypes.push_back("Monoclinic ( a unique )");
-  cellTypes.push_back("Monoclinic ( b unique )");
-  cellTypes.push_back("Monoclinic ( c unique )");
   cellTypes.push_back(ReducedCell::TRICLINIC());
   declareProperty("CellType", cellTypes[0],
                   boost::make_shared<StringListValidator>(cellTypes),
@@ -95,7 +92,7 @@ void OptimizeLatticeForCellType::exec() {
   runWS.push_back(ws);
 
   if (perRun) {
-    std::vector<std::pair<std::string, bool>> criteria;
+    std::vector<std::pair<std::string, bool> > criteria;
     // Sort by run number
     criteria.push_back(std::pair<std::string, bool>("runnumber", true));
     ws->sort(criteria);
@@ -104,7 +101,7 @@ void OptimizeLatticeForCellType::exec() {
     int count = 0;
     for (size_t i = 0; i < peaks_all.size(); i++) {
       if (peaks_all[i].getRunNumber() != run) {
-        count++;  // first entry in runWS is input workspace
+        count++; // first entry in runWS is input workspace
         DataObjects::PeaksWorkspace_sptr cloneWS(new PeaksWorkspace());
         cloneWS->setInstrument(ws->getInstrument());
         cloneWS->copyExperimentInfoFrom(ws.get());
@@ -126,19 +123,8 @@ void OptimizeLatticeForCellType::exec() {
     const DblMatrix UB = peakWS->sample().getOrientedLattice().getUB();
     std::vector<double> lat(6);
     IndexingUtils::GetLatticeParameters(UB, lat);
-    // initialize parameters for optimization
-    size_t n_peaks = peakWS->getNumberPeaks();
-    MatrixWorkspace_sptr data = WorkspaceFactory::Instance().create(
-        std::string("Workspace2D"), 1, n_peaks, n_peaks);
-    for (size_t i = 0; i < data->blocksize(); i++) {
-      data->dataX(0)[i] = static_cast<double>(i);
-      data->dataY(0)[i] = 0.0;
-      data->dataE(0)[i] = 1.0;
-    }
 
-    std::ostringstream fun_str;
-    fun_str << "name=LatticeErrors";
-    for (size_t i = 0; i < 6; i++) fun_str << ",p" << i << "=" << lat[i];
+    std::string fun_str = inParams(cell_type, lat);
 
     IAlgorithm_sptr fit_alg;
     try {
@@ -149,43 +135,9 @@ void OptimizeLatticeForCellType::exec() {
       throw;
     }
 
-    fit_alg->setPropertyValue("Function", fun_str.str());
-    if (cell_type == ReducedCell::CUBIC()) {
-      std::ostringstream tie_str;
-      tie_str << "p1=p0,p2=p0,p3=90,p4=90,p5=90";
-      fit_alg->setProperty("Ties", tie_str.str());
-    } else if (cell_type == ReducedCell::TETRAGONAL()) {
-      std::ostringstream tie_str;
-      tie_str << "p1=p0,p3=90,p4=90,p5=90";
-      fit_alg->setProperty("Ties", tie_str.str());
-    } else if (cell_type == ReducedCell::ORTHORHOMBIC()) {
-      std::ostringstream tie_str;
-      tie_str << "p3=90,p4=90,p5=90";
-      fit_alg->setProperty("Ties", tie_str.str());
-    } else if (cell_type == ReducedCell::RHOMBOHEDRAL()) {
-      std::ostringstream tie_str;
-      tie_str << "p1=p0,p2=p0,p4=p3,p5=p3";
-      fit_alg->setProperty("Ties", tie_str.str());
-    } else if (cell_type == ReducedCell::HEXAGONAL()) {
-      std::ostringstream tie_str;
-      tie_str << "p1=p0,p3=90,p4=90,p5=120";
-      fit_alg->setProperty("Ties", tie_str.str());
-    } else if (cell_type == "Monoclinic ( a unique )") {
-      std::ostringstream tie_str;
-      tie_str << "p4=90,p5=90";
-      fit_alg->setProperty("Ties", tie_str.str());
-    } else if (cell_type == ReducedCell::MONOCLINIC() ||
-               cell_type == "Monoclinic ( b unique )") {
-      std::ostringstream tie_str;
-      tie_str << "p3=90,p5=90";
-      fit_alg->setProperty("Ties", tie_str.str());
-    } else if (cell_type == "Monoclinic ( c unique )") {
-      std::ostringstream tie_str;
-      tie_str << "p3=90,p4=90";
-      fit_alg->setProperty("Ties", tie_str.str());
-    }
-    fit_alg->setProperty("InputWorkspace", data);
-    fit_alg->setProperty("WorkspaceIndex", 0);
+    fit_alg->setPropertyValue("Function", fun_str);
+    fit_alg->setProperty("Ties", "ZeroShift=0.0");
+    fit_alg->setProperty("InputWorkspace", peakWS);
     fit_alg->setProperty("CostFunction", "Unweighted least squares");
     fit_alg->setProperty("CreateOutput", true);
     fit_alg->setProperty("Output", "fit");
@@ -193,26 +145,13 @@ void OptimizeLatticeForCellType::exec() {
 
     double chisq = fit_alg->getProperty("OutputChi2overDoF");
     ITableWorkspace_sptr ParamTable = fit_alg->getProperty("OutputParameters");
+    std::vector<double> Params = outParams(cell_type, 1, ParamTable);
 
-    std::vector<double> Params;
-    IFunction_sptr out = fit_alg->getProperty("Function");
-
-    Params.push_back(out->getParameter("p0"));
-    Params.push_back(out->getParameter("p1"));
-    Params.push_back(out->getParameter("p2"));
-    Params.push_back(out->getParameter("p3"));
-    Params.push_back(out->getParameter("p4"));
-    Params.push_back(out->getParameter("p5"));
-
-    std::vector<double> sigabc(Params.size());
-    OrientedLattice latt = peakWS->mutableSample().getOrientedLattice();
-    DblMatrix UBnew = latt.getUB();
-
-    for (size_t i = 0; i < 6; i++) sigabc[i] = ParamTable->Double(i, 2);
+    std::vector<double> sigabc = outParams(cell_type, 2, ParamTable);
 
     OrientedLattice o_lattice;
-    o_lattice.setUB(UBnew);
-
+    o_lattice.set(Params[0], Params[1], Params[2], Params[3], Params[4],
+                  Params[5]);
     o_lattice.setError(sigabc[0], sigabc[1], sigabc[2], sigabc[3], sigabc[4],
                        sigabc[5]);
 
@@ -233,14 +172,15 @@ void OptimizeLatticeForCellType::exec() {
     AnalysisDataService::Instance().remove("_peaks");
     if (perRun) {
       std::string outputdir = getProperty("OutputDirectory");
-      if (outputdir[outputdir.size() - 1] != '/') outputdir += "/";
+      if (outputdir[outputdir.size() - 1] != '/')
+        outputdir += "/";
       // Save Peaks
       Mantid::API::IAlgorithm_sptr savePks_alg =
           createChildAlgorithm("SaveIsawPeaks");
       savePks_alg->setPropertyValue("InputWorkspace", runWS[i_run]->getName());
-      savePks_alg->setProperty(
-          "Filename",
-          outputdir + "ls" + runWS[i_run]->getName() + ".integrate");
+      savePks_alg->setProperty("Filename", outputdir + "ls" +
+                                               runWS[i_run]->getName() +
+                                               ".integrate");
       savePks_alg->executeAsChildAlg();
       g_log.notice() << "See output file: " << outputdir + "ls" +
                                                    runWS[i_run]->getName() +
@@ -250,8 +190,8 @@ void OptimizeLatticeForCellType::exec() {
       Mantid::API::IAlgorithm_sptr saveUB_alg =
           createChildAlgorithm("SaveIsawUB");
       saveUB_alg->setPropertyValue("InputWorkspace", runWS[i_run]->getName());
-      saveUB_alg->setProperty(
-          "Filename", outputdir + "ls" + runWS[i_run]->getName() + ".mat");
+      saveUB_alg->setProperty("Filename", outputdir + "ls" +
+                                              runWS[i_run]->getName() + ".mat");
       saveUB_alg->executeAsChildAlg();
       // Show the names of files written
       g_log.notice() << "See output file: "
@@ -262,151 +202,123 @@ void OptimizeLatticeForCellType::exec() {
 }
 //-----------------------------------------------------------------------------------------
 /**
-  @param  inname       Name of Filename containing peaks
   @param  cell_type    cell type to optimize
-  @param  params       optimized cell parameters
-  @return  chisq of optimization
+  @param  lat             optimized cell parameters
+  @return  fun_str       string of parameters for fitting
 */
-double OptimizeLatticeForCellType::optLatticeSum(std::string inname,
-                                                 std::string cell_type,
-                                                 std::vector<double> &params) {
+std::string OptimizeLatticeForCellType::inParams(std::string cell_type,
+                                                 std::vector<double> &lat) {
+  std::ostringstream fun_str;
+  fun_str << "name=LatticeFunction,CrystalSystem=" << cell_type;
+
   std::vector<double> lattice_parameters;
   lattice_parameters.assign(6, 0);
   if (cell_type == ReducedCell::CUBIC()) {
-    lattice_parameters[0] = params[0];
-    lattice_parameters[1] = params[0];
-    lattice_parameters[2] = params[0];
+    fun_str << ",a=" << lat[0];
+  } else if (cell_type == ReducedCell::TETRAGONAL()) {
+    fun_str << ",a=" << lat[0];
+    fun_str << ",c=" << lat[2];
+  } else if (cell_type == ReducedCell::ORTHORHOMBIC()) {
+    fun_str << ",a=" << lat[0];
+    fun_str << ",b=" << lat[1];
+    fun_str << ",c=" << lat[2];
+  } else if (cell_type == ReducedCell::RHOMBOHEDRAL()) {
+    fun_str << ",a=" << lat[0];
+    fun_str << ",Alpha=" << lat[3];
+  } else if (cell_type == ReducedCell::HEXAGONAL()) {
+    fun_str << ",a=" << lat[0];
+    fun_str << ",c=" << lat[2];
+  } else if (cell_type == ReducedCell::MONOCLINIC()) {
+    fun_str << ",a=" << lat[0];
+    fun_str << ",b=" << lat[1];
+    fun_str << ",c=" << lat[2];
+    fun_str << ",Beta=" << lat[4];
+  } else if (cell_type == ReducedCell::TRICLINIC()) {
+    fun_str << ",a=" << lat[0];
+    fun_str << ",b=" << lat[1];
+    fun_str << ",c=" << lat[2];
+    fun_str << ",Alpha=" << lat[3];
+    fun_str << ",Beta=" << lat[4];
+    fun_str << ",Gamma=" << lat[5];
+  }
 
+  return fun_str.str();
+}
+//-----------------------------------------------------------------------------------------
+/**
+  @param  cell_type           cell type to optimize
+  @param  col                     1 for parameter 2 for error
+  @param  ParamTable       optimized cell parameters
+  @return  chisq of optimization
+*/
+std::vector<double>
+OptimizeLatticeForCellType::outParams(std::string cell_type, int icol,
+                                      ITableWorkspace_sptr ParamTable) {
+  std::vector<double> lattice_parameters;
+  lattice_parameters.assign(6, 0);
+
+  if (cell_type == ReducedCell::CUBIC()) {
+    lattice_parameters[0] = ParamTable->Double(0, icol);
+    lattice_parameters[1] = ParamTable->Double(0, icol);
+    lattice_parameters[2] = ParamTable->Double(0, icol);
+    if (icol == 2)
+      return lattice_parameters;
     lattice_parameters[3] = 90;
     lattice_parameters[4] = 90;
     lattice_parameters[5] = 90;
   } else if (cell_type == ReducedCell::TETRAGONAL()) {
-    lattice_parameters[0] = params[0];
-    lattice_parameters[1] = params[0];
-    lattice_parameters[2] = params[1];
-
+    lattice_parameters[0] = ParamTable->Double(0, icol);
+    lattice_parameters[1] = ParamTable->Double(0, icol);
+    lattice_parameters[2] = ParamTable->Double(1, icol);
+    if (icol == 2)
+      return lattice_parameters;
     lattice_parameters[3] = 90;
     lattice_parameters[4] = 90;
     lattice_parameters[5] = 90;
   } else if (cell_type == ReducedCell::ORTHORHOMBIC()) {
-    lattice_parameters[0] = params[0];
-    lattice_parameters[1] = params[1];
-    lattice_parameters[2] = params[2];
-
+    lattice_parameters[0] = ParamTable->Double(0, icol);
+    lattice_parameters[1] = ParamTable->Double(1, icol);
+    lattice_parameters[2] = ParamTable->Double(2, icol);
+    if (icol == 2)
+      return lattice_parameters;
     lattice_parameters[3] = 90;
     lattice_parameters[4] = 90;
     lattice_parameters[5] = 90;
   } else if (cell_type == ReducedCell::RHOMBOHEDRAL()) {
-    lattice_parameters[0] = params[0];
-    lattice_parameters[1] = params[0];
-    lattice_parameters[2] = params[0];
-
-    lattice_parameters[3] = params[1];
-    lattice_parameters[4] = params[1];
-    lattice_parameters[5] = params[1];
+    lattice_parameters[0] = ParamTable->Double(0, icol);
+    lattice_parameters[1] = ParamTable->Double(0, icol);
+    lattice_parameters[2] = ParamTable->Double(0, icol);
+    lattice_parameters[3] = ParamTable->Double(1, icol);
+    lattice_parameters[4] = ParamTable->Double(1, icol);
+    lattice_parameters[5] = ParamTable->Double(1, icol);
   } else if (cell_type == ReducedCell::HEXAGONAL()) {
-    lattice_parameters[0] = params[0];
-    lattice_parameters[1] = params[0];
-    lattice_parameters[2] = params[1];
-
+    lattice_parameters[0] = ParamTable->Double(0, icol);
+    lattice_parameters[1] = ParamTable->Double(0, icol);
+    lattice_parameters[2] = ParamTable->Double(1, icol);
+    if (icol == 2)
+      return lattice_parameters;
     lattice_parameters[3] = 90;
     lattice_parameters[4] = 90;
     lattice_parameters[5] = 120;
-  } else if (cell_type == "Monoclinic ( a unique )") {
-    lattice_parameters[0] = params[0];
-    lattice_parameters[1] = params[1];
-    lattice_parameters[2] = params[2];
-
-    lattice_parameters[3] = params[3];
-    lattice_parameters[4] = 90;
-    lattice_parameters[5] = 90;
-  } else if (cell_type == ReducedCell::MONOCLINIC() ||
-             cell_type == "Monoclinic ( b unique )") {
-    lattice_parameters[0] = params[0];
-    lattice_parameters[1] = params[1];
-    lattice_parameters[2] = params[2];
-
+  } else if (cell_type == ReducedCell::MONOCLINIC()) {
+    lattice_parameters[0] = ParamTable->Double(0, icol);
+    lattice_parameters[1] = ParamTable->Double(1, icol);
+    lattice_parameters[2] = ParamTable->Double(2, icol);
+    lattice_parameters[4] = ParamTable->Double(3, icol);
+    if (icol == 2)
+      return lattice_parameters;
     lattice_parameters[3] = 90;
-    lattice_parameters[4] = params[3];
     lattice_parameters[5] = 90;
-  } else if (cell_type == "Monoclinic ( c unique )") {
-    lattice_parameters[0] = params[0];
-    lattice_parameters[1] = params[1];
-    lattice_parameters[2] = params[2];
-
-    lattice_parameters[3] = 90;
-    lattice_parameters[4] = 90;
-    lattice_parameters[5] = params[3];
   } else if (cell_type == ReducedCell::TRICLINIC()) {
-    lattice_parameters[0] = params[0];
-    lattice_parameters[1] = params[1];
-    lattice_parameters[2] = params[2];
-
-    lattice_parameters[3] = params[3];
-    lattice_parameters[4] = params[4];
-    lattice_parameters[5] = params[5];
+    lattice_parameters[0] = ParamTable->Double(0, icol);
+    lattice_parameters[1] = ParamTable->Double(1, icol);
+    lattice_parameters[2] = ParamTable->Double(2, icol);
+    lattice_parameters[3] = ParamTable->Double(3, icol);
+    lattice_parameters[4] = ParamTable->Double(4, icol);
+    lattice_parameters[5] = ParamTable->Double(5, icol);
   }
 
-  PeaksWorkspace_sptr ws = boost::dynamic_pointer_cast<PeaksWorkspace>(
-      AnalysisDataService::Instance().retrieve(inname));
-  size_t n_peaks = ws->getNumberPeaks();
-  double *out = new double[n_peaks];
-  optLattice(inname, lattice_parameters, out);
-  double ChiSqTot = 0;
-  for (size_t i = 0; i < n_peaks; i++) ChiSqTot += out[i];
-  delete[] out;
-  return ChiSqTot;
-}
-//-----------------------------------------------------------------------------------------
-/**
-  @param  inname       Name of workspace containing peaks
-  @param  params       optimized cell parameters
-  @param  out          residuals from optimization
-*/
-void OptimizeLatticeForCellType::optLattice(std::string inname,
-                                            std::vector<double> &params,
-                                            double *out) {
-  PeaksWorkspace_sptr ws = boost::dynamic_pointer_cast<PeaksWorkspace>(
-      AnalysisDataService::Instance().retrieve(inname));
-  const std::vector<Peak> &peaks = ws->getPeaks();
-  size_t n_peaks = ws->getNumberPeaks();
-  std::vector<V3D> q_vector;
-  std::vector<V3D> hkl_vector;
-
-  for (size_t i = 0; i < params.size(); i++) params[i] = std::abs(params[i]);
-  for (size_t i = 0; i < n_peaks; i++) {
-    q_vector.push_back(peaks[i].getQSampleFrame());
-    hkl_vector.push_back(peaks[i].getHKL());
-  }
-
-  Mantid::API::IAlgorithm_sptr alg = createChildAlgorithm("CalculateUMatrix");
-  alg->setPropertyValue("PeaksWorkspace", inname);
-  alg->setProperty("a", params[0]);
-  alg->setProperty("b", params[1]);
-  alg->setProperty("c", params[2]);
-  alg->setProperty("alpha", params[3]);
-  alg->setProperty("beta", params[4]);
-  alg->setProperty("gamma", params[5]);
-  alg->executeAsChildAlg();
-
-  ws = alg->getProperty("PeaksWorkspace");
-  OrientedLattice latt = ws->mutableSample().getOrientedLattice();
-  DblMatrix UB = latt.getUB();
-  DblMatrix A = aMatrix(params);
-  DblMatrix Bc = A;
-  Bc.Invert();
-  DblMatrix U1_B1 = UB * A;
-  OrientedLattice o_lattice;
-  o_lattice.setUB(U1_B1);
-  DblMatrix U1 = o_lattice.getU();
-  DblMatrix U1_Bc = U1 * Bc;
-
-  for (size_t i = 0; i < hkl_vector.size(); i++) {
-    V3D error = U1_Bc * hkl_vector[i] - q_vector[i] / (2.0 * M_PI);
-    out[i] = error.norm2();
-  }
-
-  return;
+  return lattice_parameters;
 }
 //-----------------------------------------------------------------------------------------
 /**
@@ -420,7 +332,8 @@ void OptimizeLatticeForCellType::optLattice(std::string inname,
 bool OptimizeLatticeForCellType::edgePixel(PeaksWorkspace_sptr ws,
                                            std::string bankName, int col,
                                            int row, int Edge) {
-  if (bankName.compare("None") == 0) return false;
+  if (bankName.compare("None") == 0)
+    return false;
   Geometry::Instrument_const_sptr Iptr = ws->getInstrument();
   boost::shared_ptr<const IComponent> parent =
       Iptr->getComponentByName(bankName);
@@ -453,46 +366,6 @@ bool OptimizeLatticeForCellType::edgePixel(PeaksWorkspace_sptr ws,
   }
   return false;
 }
-//-----------------------------------------------------------------------------------------
-/**
-  @param  lattice       lattice parameters
-  @return the A matrix calculated
-*/
-DblMatrix OptimizeLatticeForCellType::aMatrix(std::vector<double> lattice) {
-  double degrees_to_radians = M_PI / 180;
-  double alpha = lattice[3] * degrees_to_radians;
-  double beta = lattice[4] * degrees_to_radians;
-  double gamma = lattice[5] * degrees_to_radians;
-
-  double l1 = lattice[0];
-  double l2 = 0;
-  double l3 = 0;
-
-  double m1 = lattice[1] * std::cos(gamma);
-  double m2 = lattice[1] * std::sin(gamma);
-  double m3 = 0;
-
-  double n1 = std::cos(beta);
-  double n2 =
-      (std::cos(alpha) - std::cos(beta) * std::cos(gamma)) / std::sin(gamma);
-  double n3 = std::sqrt(1 - n1 * n1 - n2 * n2);
-  n1 *= lattice[2];
-  n2 *= lattice[2];
-  n3 *= lattice[2];
-
-  DblMatrix result(3, 3);
-  result[0][0] = l1;
-  result[0][1] = l2;
-  result[0][2] = l3;
-  result[1][0] = m1;
-  result[1][1] = m2;
-  result[1][2] = m3;
-  result[2][0] = n1;
-  result[2][1] = n2;
-  result[2][2] = n3;
-
-  return result;
-}
 
-}  // namespace Algorithm
-}  // namespace Mantid
+} // namespace Algorithm
+} // namespace Mantid
diff --git a/Framework/Crystal/test/OptimizeLatticeForCellTypeTest.h b/Framework/Crystal/test/OptimizeLatticeForCellTypeTest.h
index cfb87881032..b6a3c5a0b7f 100644
--- a/Framework/Crystal/test/OptimizeLatticeForCellTypeTest.h
+++ b/Framework/Crystal/test/OptimizeLatticeForCellTypeTest.h
@@ -59,8 +59,7 @@ public:
     TS_ASSERT_THROWS_NOTHING(alg.initialize())
     TS_ASSERT(alg.isInitialized())
     TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("PeaksWorkspace", WSName));
-    TS_ASSERT_THROWS_NOTHING(
-        alg.setPropertyValue("CellType", "Monoclinic ( a unique )"));
+    TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("CellType", "Monoclinic"));
     TS_ASSERT_THROWS_NOTHING(alg.execute(););
     TS_ASSERT(alg.isExecuted());
 
@@ -69,8 +68,8 @@ public:
     // Check that the UB matrix is the same as in TOPAZ_3007.mat
     OrientedLattice latt = ws->mutableSample().getOrientedLattice();
 
-    double correct_UB[] = {-0.0477, 0.0413, -0.0005, -0.0055, -0.0090,
-                           0.1250,  0.0610, 0.0314,  0.0110};
+    double correct_UB[] = { -0.0477, 0.0413, -0.0005, -0.0055, -0.0090,
+                            0.1250,  0.0610, 0.0314,  0.0110 };
 
     std::vector<double> UB_calculated = latt.getUB().getVector();
 
-- 
GitLab