/** Parse profile table workspace to a map (the new ...
  */
void SaveGSASInstrumentFile::parseProfileTableWorkspace(
    ITableWorkspace_sptr ws,
    map<unsigned int, map<string, double>> &profilemap) {
  size_t numbanks = ws->columnCount() - 1;
  size_t numparams = ws->rowCount();
  vector<map<string, double>> vec_maptemp(numbanks);
  vector<unsigned int> vecbankindex(numbanks);

  // Check
  vector<string> colnames = ws->getColumnNames();
  if (colnames[0].compare("Name"))
    throw runtime_error("The first column must be Name");

  // Parse
  for (size_t irow = 0; irow < numparams; ++irow) {
    TableRow tmprow = ws->getRow(irow);
    string parname;
    tmprow >> parname;
    if (parname.compare("BANK")) {
      for (size_t icol = 0; icol < numbanks; ++icol) {
        double tmpdbl;
        tmprow >> tmpdbl;
        vec_maptemp[icol].insert(make_pair(parname, tmpdbl));
      }
    } else {
      for (size_t icol = 0; icol < numbanks; ++icol) {
        double tmpint;
        tmprow >> tmpint;
        vecbankindex[icol] = static_cast<unsigned int>(tmpint);
      }
    }
  }
int PoldiFitPeaks1D2::getBestChebyshevPolynomialDegree(
    const Workspace2D_sptr &dataWorkspace, const RefinedRange_sptr &range) {
  double chiSquareMin = 1e10;
  int nMin = -1;

  try {
    int n = 0;

    while ((n < 3)) {
      IAlgorithm_sptr fit = getFitAlgorithm(dataWorkspace, range, n);
      bool fitSuccess = fit->execute();

      if (fitSuccess) {
        ITableWorkspace_sptr fitCharacteristics =
            fit->getProperty("OutputParameters");
        TableRow row =
            fitCharacteristics->getRow(fitCharacteristics->rowCount() - 1);

        double chiSquare = row.Double(1);

        if (fabs(chiSquare - 1) < fabs(chiSquareMin - 1)) {
          chiSquareMin = chiSquare;
          nMin = n;
        }
      }

      ++n;
    }
  } catch (std::runtime_error) {
    nMin = -1;
  }

  if (nMin == -1) {
    g_log.information() << "Range [" << range->getXStart() << " - "
                        << range->getXEnd() << "] is excluded.";
  } else {
    g_log.information() << "Chi^2 for range [" << range->getXStart() << " - "
                        << range->getXEnd() << "] is minimal at n = " << nMin
                        << " with Chi^2 = " << chiSquareMin << std::endl;
  }

  return nMin;
}