Skip to content

Commit

Permalink
Re #11619. Copy spectra from a histo workspace.
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Apr 24, 2015
1 parent fe26fe1 commit 101f2a2
Showing 1 changed file with 49 additions and 5 deletions.
54 changes: 49 additions & 5 deletions 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 {
Expand Down Expand Up @@ -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<size_t> indices;
makeIndexList(indices, maskedWorkspace.get());

Expand All @@ -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<TextAxis *>(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<NumericAxis *>(outAxis1)
->setValue(j, inAxis1->operator()(i));
}
}
}
setProperty("OutputWorkspace", outputWorkspace);
}

//----------------------------------------------------------------------------------------------
Expand Down

0 comments on commit 101f2a2

Please sign in to comment.