Newer
Older
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/SpectrumInfo.h"
#include "MantidAlgorithms/CropToComponent.h"
#include "MantidGeometry/Instrument.h"
#include "MantidGeometry/Instrument/ComponentInfo.h"
#include "MantidGeometry/Instrument/DetectorInfo.h"
#include "MantidTypes/SpectrumDefinition.h"
std::vector<size_t>
getDetectorIndices(const Mantid::API::MatrixWorkspace &workspace,
const std::vector<std::string> &componentNames) {
const auto &compInfo = workspace.componentInfo();
const auto instrument = workspace.getInstrument();
std::vector<size_t> detIndices;
for (const auto &componentName : componentNames) {
const auto comp = instrument->getComponentByName(componentName);
const auto compIndex = compInfo.indexOf(comp->getComponentID());
const auto indices = compInfo.detectorsInSubtree(compIndex);
detIndices.insert(detIndices.end(), indices.begin(), indices.end());
std::vector<size_t>
getWorkspaceIndices(const Mantid::API::MatrixWorkspace &workspace,
const std::vector<size_t> &detectorIndices) {
std::vector<bool> detectorMap(workspace.detectorInfo().size(), false);
for (const auto &index : detectorIndices)
detectorMap[index] = true;
const auto &spectrumInfo = workspace.spectrumInfo();
std::vector<size_t> spectrumIndices;
for (size_t i = 0; i < spectrumInfo.size(); ++i) {
const auto &spectrumDefinition = spectrumInfo.spectrumDefinition(i);
if (spectrumDefinition.size() == 1 &&
detectorMap[spectrumDefinition[0].first])
spectrumIndices.push_back(i);
if (spectrumDefinition.size() > 1)
throw std::runtime_error("Cannot map from detector ID to workspace index "
"-- spectrum in MatrixWorkspace maps to more "
"than 1 detector.");
if (detectorIndices.size() != spectrumIndices.size())
throw std::runtime_error(
"Some of the requested detectors do not have a corresponding spectrum");
return spectrumIndices;
namespace Mantid {
namespace Algorithms {
using Mantid::Kernel::Direction;
using Mantid::API::WorkspaceProperty;
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(CropToComponent)
//----------------------------------------------------------------------------------------------
/// Algorithms name for identification. @see Algorithm::name
const std::string CropToComponent::name() const { return "CropToComponent"; }
/// Algorithm's version for identification. @see Algorithm::version
int CropToComponent::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string CropToComponent::category() const {
}
/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary
const std::string CropToComponent::summary() const {
return "Crops a workspace to a set of components.";
}
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void CropToComponent::init() {
declareProperty(
Kernel::make_unique<WorkspaceProperty<Mantid::API::MatrixWorkspace>>(
"InputWorkspace", "", Direction::Input),
"An input workspace.");
declareProperty(
Kernel::make_unique<WorkspaceProperty<Mantid::API::MatrixWorkspace>>(
"OutputWorkspace", "", Direction::Output),
"An output workspace.");
declareProperty(
Kernel::make_unique<Mantid::Kernel::ArrayProperty<std::string>>(
"ComponentNames"),
"List of component names which are used to crop the workspace."
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void CropToComponent::exec() {
// Get the names of the components
std::vector<std::string> componentNames = getProperty("ComponentNames");
Mantid::API::MatrixWorkspace_sptr inputWorkspace =
getProperty("InputWorkspace");
// Get all detectors
const auto &detectorIndices =
getDetectorIndices(*inputWorkspace, componentNames);
// Get the corresponding workspace indices from the detectors
const auto &workspaceIndices =
getWorkspaceIndices(*inputWorkspace, detectorIndices);
// Run ExtractSpectra in order to obtain the cropped workspace
auto extract_alg = Mantid::API::AlgorithmManager::Instance().createUnmanaged(
"ExtractSpectra");
extract_alg->setChild(true);
extract_alg->initialize();
extract_alg->setProperty("InputWorkspace", inputWorkspace);
extract_alg->setProperty("OutputWorkspace", "dummy");
extract_alg->setProperty("WorkspaceIndexList", workspaceIndices);
extract_alg->execute();
Mantid::API::MatrixWorkspace_sptr outputWorkspace =
extract_alg->getProperty("OutputWorkspace");
// Set the output
setProperty("OutputWorkspace", outputWorkspace);
}
std::map<std::string, std::string> CropToComponent::validateInputs() {
std::map<std::string, std::string> result;
Mantid::API::MatrixWorkspace_sptr inputWorkspace =
getProperty("InputWorkspace");
std::vector<std::string> componentNames = getProperty("ComponentNames");
// Make sure that the component exists on the input workspace
auto instrument = inputWorkspace->getInstrument();
for (auto &componentName : componentNames) {
auto detector = instrument->getComponentByName(componentName);
if (!detector) {
std::string message =
"The component name " + componentName +
" does not exist on the workspace. Specify a valid component.";
result["ComponentNames"] = message;
break;
}
}
return result;
}
} // namespace Algorithms
} // namespace Mantid