Skip to content

Commit

Permalink
Re #6159. Refactored MaskTab to use the mask workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed Nov 23, 2012
1 parent 514256f commit aa8478a
Show file tree
Hide file tree
Showing 13 changed files with 356 additions and 104 deletions.
8 changes: 8 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/IMaskWorkspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ namespace API
virtual const std::string id() const { return "IMaskWorkspace"; }
/// Total number of masked pixels
virtual std::size_t getNumberMasked() const = 0;
/// Check if a detector is masked
virtual bool isMasked(const detid_t detectorID) const = 0;
/// Check if all detectors in a set are masked
virtual bool isMasked(const std::set<detid_t> &detectorIDs) const = 0;
/// Set / remove mask of a detector
virtual void setMasked(const detid_t detectorID, const bool mask=true) = 0;
/// Set / remove masks of all detectors in a set
virtual void setMasked(const std::set<detid_t> &detectorIDs, const bool mask=true) = 0;
};

///shared pointer to the matrix workspace base class
Expand Down
4 changes: 1 addition & 3 deletions Code/Mantid/Framework/Algorithms/src/ExtractMask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ namespace Mantid
const int nHist = static_cast<int>(inputWS->getNumberHistograms());

// Create a new workspace for the results, copy from the input to ensure that we copy over the instrument and current masking
maskWS = DataObjects::MaskWorkspace_sptr(new DataObjects::MaskWorkspace(instr, true));
maskWS->initialize(nHist, 1, 1);
WorkspaceFactory::Instance().initializeFromParent(inputWS, maskWS, false);
maskWS = DataObjects::MaskWorkspace_sptr(new DataObjects::MaskWorkspace(inputWS));
maskWS->setTitle(inputWS->getTitle());

Progress prog(this,0.0,1.0,nHist);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace DataObjects
MaskWorkspace(std::size_t numvectors);
MaskWorkspace(Mantid::Geometry::Instrument_const_sptr instrument,
const bool includeMonitors=false);
MaskWorkspace(const API::MatrixWorkspace_const_sptr parent);
~MaskWorkspace();

bool isMasked(const detid_t detectorID) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace DataObjects
public:
SpecialWorkspace2D();
SpecialWorkspace2D(Geometry::Instrument_const_sptr inst, const bool includeMonitors=false);
SpecialWorkspace2D(API::MatrixWorkspace_const_sptr parent);
~SpecialWorkspace2D();

/** Gets the name of the workspace type
Expand Down
13 changes: 12 additions & 1 deletion Code/Mantid/Framework/DataObjects/src/MaskWorkspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,18 @@ namespace DataObjects
MaskWorkspace::MaskWorkspace(Mantid::Geometry::Instrument_const_sptr instrument, const bool includeMonitors)
: SpecialWorkspace2D(instrument, includeMonitors), m_hasInstrument(true)
{
this->clearMask();
this->clearMask();
}

/**
* Constructor - using a MatrixWorkspace.
* @param[in] A matrix workspace that is the base for this workspace. It must have an instrument.
* @return MaskWorkspace
*/
MaskWorkspace::MaskWorkspace(const API::MatrixWorkspace_const_sptr parent)
: SpecialWorkspace2D(parent), m_hasInstrument(true)
{
this->clearMask();
}

//--------------------------------------------------------------------------
Expand Down
24 changes: 24 additions & 0 deletions Code/Mantid/Framework/DataObjects/src/SpecialWorkspace2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "MantidAPI/SpectraDetectorMap.h"
#include "MantidAPI/SpectraAxis.h"

#include <fstream>

using Mantid::API::SpectraAxis;
using Mantid::API::SpectraDetectorMap;
using std::set;
Expand Down Expand Up @@ -55,6 +57,28 @@ namespace DataObjects
}
}

//----------------------------------------------------------------------------------------------
/** Constructor, building from a MatrixWorkspace
*
* @param parent :: input workspace that is the base for this workspace
* @return created SpecialWorkspace2D
*/
SpecialWorkspace2D::SpecialWorkspace2D(API::MatrixWorkspace_const_sptr parent)
{
this->init(parent->getNumberHistograms(), 1, 1);
API::WorkspaceFactory::Instance().initializeFromParent(parent, API::MatrixWorkspace_sptr(this,Mantid::NoDeleting()), false);
// Make the mapping, which will be used for speed later.
detID_to_WI.clear();
for (size_t wi=0; wi<m_noVectors; wi++)
{
set<detid_t> dets = getSpectrum(wi)->getDetectorIDs();
for (auto det = dets.begin(); det != dets.end(); ++det)
{
detID_to_WI[*det] = wi;
}
}
}


//----------------------------------------------------------------------------------------------
/** Destructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,69 @@ MatrixWorkspace_const_sptr InstrumentActor::getWorkspace() const
return shared_workspace;
}

/** Returns the mask workspace relating to this instrument view as a MatrixWorkspace
*/
MatrixWorkspace_sptr InstrumentActor::getMaskMatrixWorkspace() const
{
if ( !m_maskWorkspace )
{
initMaskHelper();
}
return m_maskWorkspace;
}

/**
* Returns the mask workspace relating to this instrument view as a IMaskWorkspace.
* Guarantees to return a valid pointer
*/
IMaskWorkspace_sptr InstrumentActor::getMaskWorkspace() const
{
if ( !m_maskWorkspace )
{
initMaskHelper();
}
return boost::dynamic_pointer_cast<IMaskWorkspace>( m_maskWorkspace );
}

/**
* Returns the mask workspace relating to this instrument view as a IMaskWorkspace
* if it exists or empty pointer if it doesn't.
*/
IMaskWorkspace_sptr InstrumentActor::getMaskWorkspaceIfExists() const
{
if ( !m_maskWorkspace ) return IMaskWorkspace_sptr();
return boost::dynamic_pointer_cast<IMaskWorkspace>( m_maskWorkspace );
}

/**
* Apply mask stored in the helper mask workspace to the data workspace.
*/
void InstrumentActor::applyMaskWorkspace()
{
if ( !m_maskWorkspace ) return;
Mantid::API::IAlgorithm * alg = Mantid::API::FrameworkManager::Instance().createAlgorithm("MaskDetectors",-1);
alg->setPropertyValue( "Workspace", getWorkspace()->name() );
alg->setProperty( "MaskedWorkspace", m_maskWorkspace );
alg->execute();
}

/**
* Removes the mask workspace.
*/
void InstrumentActor::clearMaskWorkspace()
{
bool needColorRecalc = false;
if ( m_maskWorkspace )
{
needColorRecalc = getMaskWorkspace()->getNumberMasked() > 0;
}
m_maskWorkspace.reset();
if ( needColorRecalc )
{
resetColors();
}
}

Instrument_const_sptr InstrumentActor::getInstrument() const
{
Instrument_const_sptr retval;
Expand Down Expand Up @@ -311,6 +374,7 @@ void InstrumentActor::resetColors()
auto shared_workspace = getWorkspace();

Instrument_const_sptr inst = getInstrument();
IMaskWorkspace_sptr mask = getMaskWorkspaceIfExists();

//PARALLEL_FOR1(m_workspace)
for (int iwi=0; iwi < int(m_specIntegrs.size()); iwi++)
Expand All @@ -321,7 +385,16 @@ void InstrumentActor::resetColors()
{
// Find if the detector is masked
const std::set<detid_t>& dets = shared_workspace->getSpectrum(wi)->getDetectorIDs();
bool masked = inst->isDetectorMasked(dets);
bool masked = false;

if ( mask )
{
masked = mask->isMasked( dets );
}
else
{
masked = inst->isDetectorMasked(dets);
}

if (masked)
{
Expand Down Expand Up @@ -515,7 +588,7 @@ void InstrumentActor::setAutoscaling(bool on)
/**
* Initialize the helper mask workspace with the mask from the data workspace.
*/
void InstrumentActor::initMaskHelper()
void InstrumentActor::initMaskHelper() const
{
if ( m_maskWorkspace ) return;
// extract the mask (if any) from the data to the mask workspace
Expand All @@ -527,7 +600,7 @@ void InstrumentActor::initMaskHelper()

try
{
m_maskWorkspace = boost::dynamic_pointer_cast<Mantid::API::IMaskWorkspace>(Mantid::API::AnalysisDataService::Instance().retrieve(maskName));
m_maskWorkspace = boost::dynamic_pointer_cast<Mantid::API::MatrixWorkspace>(Mantid::API::AnalysisDataService::Instance().retrieve(maskName));
Mantid::API::AnalysisDataService::Instance().remove( maskName );
}
catch( ... )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ class InstrumentActor: public QObject, public GLActor

boost::shared_ptr<const Mantid::Geometry::Instrument> getInstrument() const;
boost::shared_ptr<const Mantid::API::MatrixWorkspace> getWorkspace() const;
boost::shared_ptr<Mantid::API::MatrixWorkspace> getMaskMatrixWorkspace() const;
boost::shared_ptr<Mantid::API::IMaskWorkspace> getMaskWorkspace() const;
void applyMaskWorkspace();
void clearMaskWorkspace();

const MantidColorMap & getColorMap() const;
void loadColorMap(const QString& ,bool reset_colors = true);
Expand Down Expand Up @@ -123,7 +127,7 @@ class InstrumentActor: public QObject, public GLActor

/* Masking */

void initMaskHelper();
void initMaskHelper() const;
signals:
void colorMapChanged();
protected:
Expand All @@ -133,11 +137,12 @@ class InstrumentActor: public QObject, public GLActor
void saveSettings();

size_t push_back_detid(Mantid::detid_t)const;
boost::shared_ptr<Mantid::API::IMaskWorkspace> getMaskWorkspaceIfExists() const;

/// The workspace whose data are shown
const boost::weak_ptr<const Mantid::API::MatrixWorkspace> m_workspace;
/// The helper masking workspace keeping the mask build in the mask tab but not applied to the data workspace.
boost::shared_ptr<Mantid::API::IMaskWorkspace> m_maskWorkspace;
mutable boost::shared_ptr<Mantid::API::MatrixWorkspace> m_maskWorkspace;
/// The colormap
MantidColorMap m_colorMap;
/// integrated spectra
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,7 @@ void InstrumentWindow::updateInstrumentView()
/// Recalculate the colours and redraw the instrument view
void InstrumentWindow::updateInstrumentDetectors()
{
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
if ( isGLEnabled() )
{
m_InstrumentDisplay->updateDetectors();
Expand All @@ -1476,6 +1477,7 @@ void InstrumentWindow::updateInstrumentDetectors()
{
m_simpleDisplay->updateDetectors();
}
QApplication::restoreOverrideCursor();
}

/**
Expand Down

0 comments on commit aa8478a

Please sign in to comment.