Newer
Older
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source
// & Institut Laue - Langevin
// SPDX - License - Identifier: GPL - 3.0 +
Russell Taylor
committed
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/CommonBinsValidator.h"
#include "MantidHistogramData/HistogramMath.h"
Russell Taylor
committed
#include "MantidKernel/ArrayProperty.h"
Russell Taylor
committed
namespace Mantid {
namespace DataHandling {
Russell Taylor
committed
// Register the algorithm into the algorithm factory
DECLARE_ALGORITHM(GroupDetectors)
using namespace Kernel;
using namespace API;
void GroupDetectors::init() {
declareProperty(
std::make_unique<WorkspaceProperty<>>(
"Workspace", "", Direction::InOut,
boost::make_shared<CommonBinsValidator>()),
"The name of the workspace2D on which to perform the algorithm");
Janik Zikovsky
committed
std::make_unique<ArrayProperty<specnum_t>>("SpectraList"),
"An array containing a list of the indexes of the spectra to combine\n"
"(DetectorList and WorkspaceIndexList are ignored if this is set)");
Janik Zikovsky
committed
std::make_unique<ArrayProperty<detid_t>>("DetectorList"),
"An array of detector ID's (WorkspaceIndexList is ignored if this is\n"
"set)");
Janik Zikovsky
committed
declareProperty(std::make_unique<ArrayProperty<size_t>>("WorkspaceIndexList"),
"An array of workspace indices to combine");
Janik Zikovsky
committed
Steve Williams
committed
declareProperty("ResultIndex", -1,
"The workspace index of the summed spectrum (or -1 on error)",
Direction::Output);
Russell Taylor
committed
}
Russell Taylor
committed
// Get the input workspace
Russell Taylor
committed
const MatrixWorkspace_sptr WS = getProperty("Workspace");
Russell Taylor
committed
std::vector<size_t> indexList = getProperty("WorkspaceIndexList");
std::vector<specnum_t> spectraList = getProperty("SpectraList");
const std::vector<detid_t> detectorList = getProperty("DetectorList");
Roman Tolchenov
committed
Russell Taylor
committed
// Could create a Validator to replace the below
if (indexList.empty() && spectraList.empty() && detectorList.empty()) {
g_log.information(name() + ": WorkspaceIndexList, SpectraList, and "
"DetectorList properties are all empty, no "
"grouping done");
Steve Williams
committed
return;
Russell Taylor
committed
}
// If the spectraList property has been set, need to loop over the workspace
// looking for the appropriate spectra number and adding the indices they
// are linked to the list to be processed
indexList = WS->getIndicesFromSpectra(spectraList);
} // End dealing with spectraList
else if (!detectorList.empty()) {
// Dealing with DetectorList
// convert from detectors to workspace indices
indexList = WS->getIndicesFromDetectorIDs(detectorList);
if (indexList.empty()) {
g_log.warning("Nothing to group");
return;
Russell Taylor
committed
}
const auto firstIndex = static_cast<specnum_t>(indexList[0]);
auto &firstSpectrum = WS->getSpectrum(firstIndex);
setProperty("ResultIndex", firstIndex);
Gigg, Martyn Anthony
committed
// loop over the spectra to group
Progress progress(this, 0.0, 1.0, static_cast<int>(indexList.size() - 1));
auto outputHisto = firstSpectrum.histogram();
for (size_t i = 0; i < indexList.size() - 1; ++i) {
Janik Zikovsky
committed
// The current spectrum
const size_t currentIndex = indexList[i + 1];
auto &spec = WS->getSpectrum(currentIndex);
Janik Zikovsky
committed
// Add the current detector to belong to the first spectrum
firstSpectrum.addDetectorIDs(spec.getDetectorIDs());
Janik Zikovsky
committed
Russell Taylor
committed
// Add up all the Y spectra and store the result in the first one
outputHisto += spec.histogram();
Janik Zikovsky
committed
// Now zero the now redundant spectrum and set its spectraNo to indicate
// this (using -1)
spec.clearData();
spec.setSpectrumNo(-1);
spec.clearDetectorIDs();
Gigg, Martyn Anthony
committed
progress.report();
Russell Taylor
committed
}
firstSpectrum.setHistogram(outputHisto);
Russell Taylor
committed
}
} // namespace DataHandling
} // namespace Mantid