From 101f2a27f5f0b782061f2b25bedac48d0607e5bf Mon Sep 17 00:00:00 2001 From: Roman Tolchenov Date: Fri, 24 Apr 2015 11:25:18 +0100 Subject: [PATCH] Re #11619. Copy spectra from a histo workspace. --- .../Algorithms/src/RemoveMaskedSpectra.cpp | 54 +++++++++++++++++-- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/Code/Mantid/Framework/Algorithms/src/RemoveMaskedSpectra.cpp b/Code/Mantid/Framework/Algorithms/src/RemoveMaskedSpectra.cpp index fc6abed8ff18..e4f23e3285eb 100644 --- a/Code/Mantid/Framework/Algorithms/src/RemoveMaskedSpectra.cpp +++ b/Code/Mantid/Framework/Algorithms/src/RemoveMaskedSpectra.cpp @@ -1,6 +1,9 @@ #include "MantidAlgorithms/RemoveMaskedSpectra.h" -#include "MantidDataObjects/MaskWorkspace.h" + +#include "MantidAPI/NumericAxis.h" +#include "MantidAPI/TextAxis.h" #include "MantidAPI/WorkspaceFactory.h" +#include "MantidDataObjects/MaskWorkspace.h" namespace Mantid { namespace Algorithms { @@ -73,6 +76,7 @@ void RemoveMaskedSpectra::exec() { throw std::runtime_error("Masked workspace has a different number of spectra."); } + // Find indices of the unmasked spectra. std::vector indices; makeIndexList(indices, maskedWorkspace.get()); @@ -82,13 +86,53 @@ void RemoveMaskedSpectra::exec() { size_t nBins = inputWorkspace->blocksize(); size_t histogram = inputWorkspace->isHistogramData() ? 1 : 0; - // Create the output workspace + // Create the output workspace. MatrixWorkspace_sptr outputWorkspace = WorkspaceFactory::Instance().create( - inputWorkspace, nSpectra, nBins, nBins - histogram); + inputWorkspace, nSpectra, nBins + histogram, nBins); + + // If this is a Workspace2D, get the spectra axes for copying in the spectraNo + // later. + Axis *inAxis1(NULL), *outAxis1(NULL); + TextAxis *outTxtAxis(NULL); + if (inputWorkspace->axes() > 1) { + inAxis1 = inputWorkspace->getAxis(1); + outAxis1 = outputWorkspace->getAxis(1); + outTxtAxis = dynamic_cast(outAxis1); + } + + // Check for common boundaries in input workspace. + bool commonBoundaries = WorkspaceHelpers::commonBoundaries(inputWorkspace); - for (size_t i = 0; i < nSpectra; ++i) { - auto j = indices[i]; + MantidVecPtr newX; + if (commonBoundaries) newX = inputWorkspace->refX(0); + + // Add spectra to the output workspace. + for (size_t j = 0; j < nSpectra; ++j) { + auto i = indices[j]; + // copy the x bins + if (commonBoundaries) { + outputWorkspace->setX(j, newX); + } else { + outputWorkspace->setX(j, inputWorkspace->refX(i)); + } + // Copy the y values and errors. + outputWorkspace->getSpectrum(j)->setData(inputWorkspace->readY(i), inputWorkspace->readE(i)); + // Copy spectrum number & detectors + outputWorkspace->getSpectrum(j) + ->copyInfoFrom(*inputWorkspace->getSpectrum(i)); + // Copy over the axis entry for each spectrum, regardless of the type of + // axes present. + if (inAxis1) { + if (outAxis1->isText()) { + outTxtAxis->setLabel(j, inAxis1->label(i)); + } else if (!outAxis1->isSpectra()) // handled by copyInfoFrom line + { + dynamic_cast(outAxis1) + ->setValue(j, inAxis1->operator()(i)); + } + } } + setProperty("OutputWorkspace", outputWorkspace); } //----------------------------------------------------------------------------------------------