/** Copy table workspace content from one workspace to another
  * @param sourceWS :: table workspace from which the content is copied;
  * @param targetWS :: table workspace to which the content is copied;
  */
void ExtractMaskToTable::copyTableWorkspaceContent(
    TableWorkspace_sptr sourceWS, TableWorkspace_sptr targetWS) {
  // Compare the column names.  They must be exactly the same
  vector<string> sourcecolnames = sourceWS->getColumnNames();
  vector<string> targetcolnames = targetWS->getColumnNames();
  if (sourcecolnames.size() != targetcolnames.size()) {
    stringstream errmsg;
    errmsg << "Soruce table workspace " << sourceWS->name()
           << " has different number of columns (" << sourcecolnames.size()
           << ") than target table workspace's (" << targetcolnames.size()
           << ")";
    throw runtime_error(errmsg.str());
  }
  for (size_t i = 0; i < sourcecolnames.size(); ++i) {
    if (sourcecolnames[i].compare(targetcolnames[i])) {
      stringstream errss;
      errss << "Source and target have incompatible column name at column " << i
            << ". "
            << "Column name of source is " << sourcecolnames[i] << "; "
            << "Column name of target is " << targetcolnames[i];
      throw runtime_error(errss.str());
    }
  }

  // Copy over the content
  size_t numrows = sourceWS->rowCount();
  for (size_t i = 0; i < numrows; ++i) {
    double xmin, xmax;
    string speclist;
    TableRow tmprow = sourceWS->getRow(i);
    tmprow >> xmin >> xmax >> speclist;

    TableRow newrow = targetWS->appendRow();
    newrow << xmin << xmax << speclist;
  }

  return;
}
예제 #2
0
/** Process input Mask bin TableWorkspace.
  * It will convert detector IDs list to spectra list
  * @param masktblws :: TableWorkspace for mask bins
  * @param dataws :: MatrixWorkspace to mask
  */
void MaskBinsFromTable::processMaskBinWorkspace(
    TableWorkspace_sptr masktblws, API::MatrixWorkspace_sptr dataws) {
  // Check input
  if (!masktblws)
    throw std::invalid_argument("Input workspace is not a table workspace.");
  g_log.debug() << "Lines of parameters workspace = " << masktblws->rowCount()
                << '\n';

  // Check column names type and sequence
  vector<std::string> colnames = masktblws->getColumnNames();

  // check colum name order
  id_xmin = -1;
  id_xmax = -1;
  id_spec = -1;
  id_dets = -1;
  m_useDetectorID = false;
  m_useSpectrumID = false;

  for (int i = 0; i < static_cast<int>(colnames.size()); ++i) {
    string colname = colnames[i];
    transform(colname.begin(), colname.end(), colname.begin(), ::tolower);
    if (colname.compare("xmin") == 0)
      id_xmin = i;
    else if (colname.compare("xmax") == 0)
      id_xmax = i;
    else if (boost::algorithm::starts_with(colname, "spec")) {
      id_spec = i;
    } else if (boost::algorithm::starts_with(colname, "detectorid")) {
      id_dets = i;
    } else {
      g_log.warning() << "In TableWorkspace " << masktblws->name()
                      << ", column " << i << " with name " << colname
                      << " is not used by MaskBinsFromTable.";
    }
  }

  if (id_xmin < 0 || id_xmax < 0 || id_xmin == id_xmax)
    throw runtime_error("Either Xmin nor Xmax is not given. ");
  if (id_spec == id_dets)
    throw runtime_error("Neither SpectraList nor DetectorIDList is given.");
  else if (id_dets >= 0)
    m_useDetectorID = true;
  else
    m_useSpectrumID = true;

  // Construct vectors for xmin, xmax and spectra-list
  size_t numrows = masktblws->rowCount();
  for (size_t i = 0; i < numrows; ++i) {
    double xmin = masktblws->cell<double>(i, static_cast<size_t>(id_xmin));
    double xmax = masktblws->cell<double>(i, static_cast<size_t>(id_xmax));

    string spectralist;
    if (m_useSpectrumID) {
      spectralist = masktblws->cell<string>(i, static_cast<size_t>(id_spec));
    } else {
      // Convert detectors list to spectra list
      string detidslist =
          masktblws->cell<string>(i, static_cast<size_t>(id_dets));
      spectralist = convertToSpectraList(dataws, detidslist);
    }

    g_log.debug() << "Row " << i << " XMin = " << xmin << "  XMax = " << xmax
                  << " SpectraList = " << spectralist << ".\n";

    // Store to class variables
    m_xminVec.push_back(xmin);
    m_xmaxVec.push_back(xmax);
    m_spectraVec.push_back(spectralist);
  }
}