Example #1
0
/* Convert spectra mask into det-id mask using workspace as source of
*spectra-detector maps
*
* @param sourceWS       -- the workspace containing source spectra-detector map
*                          to use on masks
* @param maskedSpecID   -- vector of spectra id to mask
* @param singleDetIds   -- output vector of detector ids to mask
*/
void LoadMask::convertSpMasksToDetIDs(const API::MatrixWorkspace &sourceWS,
                                      const std::vector<int32_t> &maskedSpecID,
                                      std::vector<int32_t> &singleDetIds) {

  spec2index_map s2imap = sourceWS.getSpectrumToWorkspaceIndexMap();
  detid2index_map sourceDetMap =
      sourceWS.getDetectorIDToWorkspaceIndexMap(false);

  std::multimap<size_t, Mantid::detid_t> spectr2index_map;
  for (auto it = sourceDetMap.begin(); it != sourceDetMap.end(); it++) {
    spectr2index_map.insert(
        std::pair<size_t, Mantid::detid_t>(it->second, it->first));
  }
  spec2index_map new_map;
  for (size_t i = 0; i < maskedSpecID.size(); i++) {
    // find spectra number from spectra ID for the source workspace
    const auto itSpec = s2imap.find(maskedSpecID[i]);
    if (itSpec == s2imap.end()) {
      throw std::runtime_error(
          "Can not find spectra with ID: " +
          boost::lexical_cast<std::string>(maskedSpecID[i]) +
          " in the workspace" + sourceWS.getName());
    }
    size_t specN = itSpec->second;

    // find detector range related to this spectra id in the source workspace
    const auto source_range = spectr2index_map.equal_range(specN);
    if (source_range.first == spectr2index_map.end()) {
      throw std::runtime_error("Can not find spectra N: " +
                               boost::lexical_cast<std::string>(specN) +
                               " in the workspace" + sourceWS.getName());
    }
    // add detectors to the masked det-id list
    for (auto it = source_range.first; it != source_range.second; ++it) {
      singleDetIds.push_back(it->second);
    }
  }
}