/** Convert a list of detector IDs list (string) to a list of spectra/workspace
 * indexes list
  * @param dataws :: MatrixWorkspace to mask
  * @param detidliststr :: list of detector IDs in string format
  * @return :: list of spectra/workspace index IDs in string format
  */
std::string
MaskBinsFromTable::convertToSpectraList(API::MatrixWorkspace_sptr dataws,
                                        std::string detidliststr) {
  // Use array property to get a list of detectors
  vector<int> detidvec;
  ArrayProperty<int> parser("detids", detidliststr);

  size_t numitems = parser.size();
  for (size_t i = 0; i < numitems; ++i) {
    detidvec.push_back(parser.operator()()[i]);
    g_log.debug() << "[DB] DetetorID = " << detidvec.back() << " to mask.";
  }

  // Get workspace index from
  vector<size_t> wsindexvec;

  detid2index_map refermap = dataws->getDetectorIDToWorkspaceIndexMap(false);
  for (size_t i = 0; i < numitems; ++i) {
    detid_t detid = detidvec[i];
    detid2index_map::const_iterator fiter = refermap.find(detid);
    if (fiter != refermap.end()) {
      size_t wsindex = fiter->second;
      wsindexvec.push_back(wsindex);
    } else {
      g_log.warning() << "Detector ID " << detid
                      << " cannot be mapped to any workspace index/spectrum."
                      << ".\n";
    }
  }

  // Sort the vector
  if (wsindexvec.empty())
    throw runtime_error("There is no spectrum found for input detectors list.");

  sort(wsindexvec.begin(), wsindexvec.end());

  // Convert the spectra to a string
  stringstream rss;
  size_t headid = wsindexvec[0];
  size_t previd = wsindexvec[0];

  for (size_t i = 1; i < wsindexvec.size(); ++i) {
    size_t currid = wsindexvec[i];
    if (currid > previd + 1) {
      // skipped.  previous region should be written, and a new region start
      if (headid == previd)
        rss << headid << ", ";
      else
        rss << headid << "-" << previd << ", ";
      headid = currid;
    } else {
      // Equal to continuous
      ;
    }

    // Update
    previd = currid;
  }

  // Finalize
  if (headid == previd)
    rss << headid;
  else
    rss << headid << "-" << previd;

  string spectraliststr(rss.str());

  return spectraliststr;
}