Skip to content

Commit

Permalink
Re #6809. Implemented ClearMaskFlag.
Browse files Browse the repository at this point in the history
  • Loading branch information
peterfpeterson committed Apr 2, 2013
1 parent d825dbd commit 5edcbf0
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/Algorithms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ set ( SRC_FILES
src/ChangePulsetime.cpp
src/CheckWorkspacesMatch.cpp
src/ChopData.cpp
src/ClearMaskFlag.cpp
src/CloneWorkspace.cpp
src/CommutativeBinaryOperation.cpp
src/ConjoinWorkspaces.cpp
Expand Down Expand Up @@ -238,6 +239,7 @@ set ( INC_FILES
inc/MantidAlgorithms/ChangePulsetime.h
inc/MantidAlgorithms/CheckWorkspacesMatch.h
inc/MantidAlgorithms/ChopData.h
inc/MantidAlgorithms/ClearMaskFlag.h
inc/MantidAlgorithms/CloneWorkspace.h
inc/MantidAlgorithms/CommutativeBinaryOperation.h
inc/MantidAlgorithms/ConjoinWorkspaces.h
Expand Down Expand Up @@ -458,6 +460,7 @@ set ( TEST_FILES
ChangePulsetimeTest.h
CheckWorkspacesMatchTest.h
ChopDataTest.h
ClearMaskFlagTest.h
CloneWorkspaceTest.h
CommutativeBinaryOperationTest.h
CompressedRebinTest.h
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#ifndef MANTID_ALGORITHMS_CLEARMASKFLAG_H_
#define MANTID_ALGORITHMS_CLEARMASKFLAG_H_

#include "MantidKernel/System.h"
#include "MantidAPI/Algorithm.h"

namespace Mantid
{
namespace Algorithms
{

/** ClearMaskFlag : TODO: DESCRIPTION
Copyright © 2013 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport ClearMaskFlag : public API::Algorithm
{
public:
ClearMaskFlag();
virtual ~ClearMaskFlag();

virtual const std::string name() const;
virtual int version() const;
virtual const std::string category() const;

private:
virtual void initDocs();
void init();
void exec();


};


} // namespace Algorithms
} // namespace Mantid

#endif /* MANTID_ALGORITHMS_CLEARMASKFLAG_H_ */
72 changes: 72 additions & 0 deletions Code/Mantid/Framework/Algorithms/src/ClearMaskFlag.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*WIKI*
This algorithm clears the mask flag/bit on all spectra of a workspace. It does not restore masked data.
*WIKI*/

#include "MantidAlgorithms/ClearMaskFlag.h"

namespace Mantid
{
namespace Algorithms
{

using namespace API;
using Kernel::Direction;

// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(ClearMaskFlag)



//----------------------------------------------------------------------------------------------
/** Constructor
*/
ClearMaskFlag::ClearMaskFlag() {}

//----------------------------------------------------------------------------------------------
/** Destructor
*/
ClearMaskFlag::~ClearMaskFlag() {}

//----------------------------------------------------------------------------------------------
/// Algorithm's name for identification. @see Algorithm::name
const std::string ClearMaskFlag::name() const { return "ClearMaskFlag";};

/// Algorithm's version for identification. @see Algorithm::version
int ClearMaskFlag::version() const { return 1;};

/// Algorithm's category for identification. @see Algorithm::category
const std::string ClearMaskFlag::category() const { return "Utility";}

//----------------------------------------------------------------------------------------------
/// Sets documentation strings for this algorithm
void ClearMaskFlag::initDocs()
{
std::string summary("Delete the mask flag/bit on all spectra in a workspace.");
this->setWikiSummary(summary);
this->setOptionalMessage(summary);
}

//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void ClearMaskFlag::init()
{
declareProperty(new WorkspaceProperty<>("Workspace","",Direction::InOut), "Workspace to clear the mask flag of.");
}

//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void ClearMaskFlag::exec()
{
MatrixWorkspace_sptr ws = getProperty("Workspace");

// Clear the mask flags
Geometry::ParameterMap & pmap = ws->instrumentParameters();
pmap.clearParametersByName("masked");
}



} // namespace Algorithms
} // namespace Mantid
98 changes: 98 additions & 0 deletions Code/Mantid/Framework/Algorithms/test/ClearMaskFlagTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#ifndef MANTID_ALGORITHMS_CLEARMASKFLAGTEST_H_
#define MANTID_ALGORITHMS_CLEARMASKFLAGTEST_H_

#include <cxxtest/TestSuite.h>
#include "MantidAlgorithms/ClearMaskFlag.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidGeometry/Instrument.h"
#include "MantidGeometry/Instrument/Detector.h"
#include "MantidTestHelpers/ComponentCreationHelper.h"

using namespace Mantid::API;
using namespace Mantid::DataObjects;
using namespace Mantid::Geometry;
using Mantid::Algorithms::ClearMaskFlag;
using Mantid::MantidVecPtr;

class ClearMaskFlagTest : public CxxTest::TestSuite
{
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static ClearMaskFlagTest *createSuite() { return new ClearMaskFlagTest(); }
static void destroySuite( ClearMaskFlagTest *suite ) { delete suite; }


void test_Init()
{
ClearMaskFlag alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
}

void test_exec()
{
// create a workspace
const int numspec = 9;
const int nummask = 5;
Instrument_sptr instr = boost::dynamic_pointer_cast<Instrument>(ComponentCreationHelper::createTestInstrumentCylindrical(1, false));
Detector *d = new Detector("det",0,0);
instr->markAsDetector(d);

// create the workspace
MatrixWorkspace_sptr space = WorkspaceFactory::Instance().create("Workspace2D",numspec,6,5);
Workspace2D_sptr space2D = boost::dynamic_pointer_cast<Workspace2D>(space);
MantidVecPtr x,vec;
x.access().resize(6,10.0);
vec.access().resize(5,1.0);
for (int j = 0; j < numspec; ++j)
{
space2D->setX(j,x);
space2D->setData(j,vec,vec);
space2D->getSpectrum(j)->setSpectrumNo(j);
space2D->getSpectrum(j)->setDetectorID(j);
}
space->setInstrument(instr);
space->generateSpectraMap();

// set the mask on a bunch of spectra
Mantid::Geometry::ParameterMap& pmap = space->instrumentParameters();
for (int j = 0; j < nummask; ++j)
{
pmap.addBool(instr->getDetector(j)->getComponentID(), "masked", true);
}


// register the workspace in the data service
std::string wsName("ClearMaskFlagTest_WS");
AnalysisDataService::Instance().addOrReplace(wsName, space);

// run the algorithm with nothing masked
ClearMaskFlag alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Workspace", wsName) );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );

// retrieve the workspace from data service. TODO: Change to your desired type
Workspace2D_sptr ws;
TS_ASSERT_THROWS_NOTHING( ws = AnalysisDataService::Instance().retrieveWS<Workspace2D>(wsName) );
TS_ASSERT(ws);
if (!ws) return;

// check the results
Instrument_const_sptr out_instr = ws->getInstrument();
for (int j = 0; j < numspec; ++j)
{
TS_ASSERT(!out_instr->isDetectorMasked(j));
}

// remove workspace from the data service.
AnalysisDataService::Instance().remove(wsName);
}

};


#endif /* MANTID_ALGORITHMS_CLEARMASKFLAGTEST_H_ */

0 comments on commit 5edcbf0

Please sign in to comment.