Skip to content

Commit

Permalink
Refs #8550. Initial files for MuonGroupDetectors.
Browse files Browse the repository at this point in the history
  • Loading branch information
arturbekasov committed Dec 5, 2013
1 parent 69ea041 commit 18982a6
Show file tree
Hide file tree
Showing 4 changed files with 282 additions and 6 deletions.
15 changes: 9 additions & 6 deletions Code/Mantid/Framework/Algorithms/CMakeLists.txt
Expand Up @@ -46,8 +46,8 @@ set ( SRC_FILES
src/ConvertToPointData.cpp
src/ConvertUnits.cpp
src/CopyInstrumentParameters.cpp
src/CopySample.cpp
src/CopyLogs.cpp
src/CopySample.cpp
src/CorrectFlightPaths.cpp
src/CorrectKiKf.cpp
src/CorrectToFile.cpp
Expand Down Expand Up @@ -100,8 +100,8 @@ set ( SRC_FILES
src/FindDeadDetectors.cpp
src/FindDetectorsOutsideLimits.cpp
src/FindPeakBackground.cpp
src/FitPeak.cpp
src/FindPeaks.cpp
src/FitPeak.cpp
src/FixGSASInstrumentFile.cpp
src/FlatPlateAbsorption.cpp
src/GeneralisedSecondDifference.cpp
Expand Down Expand Up @@ -139,6 +139,7 @@ set ( SRC_FILES
src/MultipleScatteringCylinderAbsorption.cpp
src/Multiply.cpp
src/MultiplyRange.cpp
src/MuonGroupDetectors.cpp
src/NormaliseByCurrent.cpp
src/NormaliseByDetector.cpp
src/NormaliseToMonitor.cpp
Expand Down Expand Up @@ -266,8 +267,8 @@ set ( INC_FILES
inc/MantidAlgorithms/ConvertToPointData.h
inc/MantidAlgorithms/ConvertUnits.h
inc/MantidAlgorithms/CopyInstrumentParameters.h
inc/MantidAlgorithms/CopySample.h
inc/MantidAlgorithms/CopyLogs.h
inc/MantidAlgorithms/CopySample.h
inc/MantidAlgorithms/CorrectFlightPaths.h
inc/MantidAlgorithms/CorrectKiKf.h
inc/MantidAlgorithms/CorrectToFile.h
Expand Down Expand Up @@ -320,8 +321,8 @@ set ( INC_FILES
inc/MantidAlgorithms/FindDeadDetectors.h
inc/MantidAlgorithms/FindDetectorsOutsideLimits.h
inc/MantidAlgorithms/FindPeakBackground.h
inc/MantidAlgorithms/FitPeak.h
inc/MantidAlgorithms/FindPeaks.h
inc/MantidAlgorithms/FitPeak.h
inc/MantidAlgorithms/FixGSASInstrumentFile.h
inc/MantidAlgorithms/FlatPlateAbsorption.h
inc/MantidAlgorithms/GSLFunctions.h
Expand Down Expand Up @@ -360,6 +361,7 @@ set ( INC_FILES
inc/MantidAlgorithms/MultipleScatteringCylinderAbsorption.h
inc/MantidAlgorithms/Multiply.h
inc/MantidAlgorithms/MultiplyRange.h
inc/MantidAlgorithms/MuonGroupDetectors.h
inc/MantidAlgorithms/NormaliseByCurrent.h
inc/MantidAlgorithms/NormaliseByDetector.h
inc/MantidAlgorithms/NormaliseToMonitor.h
Expand Down Expand Up @@ -498,8 +500,8 @@ set ( TEST_FILES
ConvertToPointDataTest.h
ConvertUnitsTest.h
CopyInstrumentParametersTest.h
CopySampleTest.h
CopyLogsTest.h
CopySampleTest.h
CorrectFlightPathsTest.h
CorrectKiKfTest.h
CorrectToFileTest.h
Expand Down Expand Up @@ -546,8 +548,8 @@ set ( TEST_FILES
FindDeadDetectorsTest.h
FindDetectorsOutsideLimitsTest.h
FindPeakBackgroundTest.h
FitPeakTest.h
FindPeaksTest.h
FitPeakTest.h
FixGSASInstrumentFileTest.h
FlatPlateAbsorptionTest.h
GenerateEventsFilterTest.h
Expand Down Expand Up @@ -579,6 +581,7 @@ set ( TEST_FILES
MultipleScatteringCylinderAbsorptionTest.h
MultiplyRangeTest.h
MultiplyTest.h
MuonGroupDetectorsTest.h
NormaliseByCurrentTest.h
NormaliseByDetectorTest.h
NormaliseToMonitorTest.h
Expand Down
@@ -0,0 +1,56 @@
#ifndef MANTID_ALGORITHMS_MUONGROUPDETECTORS_H_
#define MANTID_ALGORITHMS_MUONGROUPDETECTORS_H_

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

namespace Mantid
{
namespace Algorithms
{

/** MuonGroupDetectors : applies detector grouping to a workspace. (Muon version)
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 MuonGroupDetectors : public API::Algorithm
{
public:
MuonGroupDetectors();
virtual ~MuonGroupDetectors();

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_MUONGROUPDETECTORS_H_ */
121 changes: 121 additions & 0 deletions Code/Mantid/Framework/Algorithms/src/MuonGroupDetectors.cpp
@@ -0,0 +1,121 @@
/*WIKI*
Applies detector grouping to a workspace. (Muon version).
TODO: Table format description
*WIKI*/

#include "MantidAlgorithms/MuonGroupDetectors.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidDataObjects/TableWorkspace.h"


namespace Mantid
{
namespace Algorithms
{

using namespace Kernel;
using namespace API;
using namespace DataObjects;

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

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

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


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

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

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

//----------------------------------------------------------------------------------------------
/// Sets documentation strings for this algorithm
void MuonGroupDetectors::initDocs()
{
this->setWikiSummary("Applies detector grouping to a workspace. (Muon version).");
this->setOptionalMessage("Applies detector grouping to a workspace. (Muon version).");
}

//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void MuonGroupDetectors::init()
{
declareProperty(new WorkspaceProperty<MatrixWorkspace>("InputWorkspace","",Direction::Input),
"Workspace to apply grouping to.");

declareProperty(new WorkspaceProperty<TableWorkspace>("DetectorGroupingTable","",Direction::Input),
"Table with detector grouping information. Check wiki page for table format expected.");

declareProperty(new WorkspaceProperty<MatrixWorkspace>("OutputWorkspace","",Direction::Output),
"Workspace with detectors grouped.");
}

//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void MuonGroupDetectors::exec()
{
TableWorkspace_sptr table = getProperty("DetectorGroupingTable");

// Check that table does have expected format

if ( table->columnCount() != 3 )
throw std::invalid_argument("Detector Grouping Table should have 3 columns");

if ( table->getColumn(0)->type() != "str" )
throw std::invalid_argument("Invalid type of the first column. Should be string.");

if ( table->getColumn(1)->type() != "str" )
throw std::invalid_argument("Invalid type of the second column. Should be string.");

if ( table->getColumn(2)->type() != "vector_int" )
throw std::invalid_argument("Invalid type of the third column. Should be vector of ints.");

size_t numGroups(0);

// First pass to determine how many non-empty groups we have
for ( size_t row = 0; row < table->rowCount(); ++row )
{
std::string& itemName = table->cell<std::string>(row, 0);
std::vector<int>& elements = table->cell< std::vector<int> >(row, 2);

if ( itemName == "Group" && elements.size() != 0 )
{
numGroups++;
}
}

if ( numGroups == 0 )
throw std::invalid_argument("Detector Grouping Table doesn't contain any non-empty groups");

MatrixWorkspace_sptr inWS = getProperty("InputWorkspace");

// Create output workspace with all the same parameters as an input one except number of histograms
MatrixWorkspace_sptr outWS = WorkspaceFactory::Instance().create(inWS, numGroups);

setProperty("OutputWorkspace", outWS);
}



} // namespace Algorithms
} // namespace Mantid
96 changes: 96 additions & 0 deletions Code/Mantid/Framework/Algorithms/test/MuonGroupDetectorsTest.h
@@ -0,0 +1,96 @@
#ifndef MANTID_ALGORITHMS_MUONGROUPDETECTORSTEST_H_
#define MANTID_ALGORITHMS_MUONGROUPDETECTORSTEST_H_

#include <cxxtest/TestSuite.h>

#include "MantidAPI/TableRow.h"
#include "MantidAlgorithms/MuonGroupDetectors.h"
#include "MantidDataObjects/TableWorkspace.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"

using Mantid::Algorithms::MuonGroupDetectors;

using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::DataObjects;

class MuonGroupDetectorsTest : 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 MuonGroupDetectorsTest *createSuite() { return new MuonGroupDetectorsTest(); }
static void destroySuite( MuonGroupDetectorsTest *suite ) { delete suite; }


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

void test_exec()
{
// Name of the output workspace.
const std::string outWSName("MuonGroupDetectorsTest_OutputWS");

MatrixWorkspace_sptr inWS = WorkspaceCreationHelper::Create2DWorkspace123(5,3);
TableWorkspace_sptr grouping = createDetectorGroupingTable();

MuonGroupDetectors alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setProperty("InputWorkspace", inWS) );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("DetectorGroupingTable", grouping) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("OutputWorkspace", outWSName) );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );

// Retrieve the workspace from data service. TODO: Change to your desired type
MatrixWorkspace_sptr ws;
TS_ASSERT_THROWS_NOTHING( ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(outWSName) );
TS_ASSERT(ws);

if ( ws )
{
TSM_ASSERT( "You forgot to check the results!", 0);
}

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

private:

TableWorkspace_sptr createDetectorGroupingTable()
{
auto t = boost::make_shared<TableWorkspace>();

t->addColumn("str", "ItemType");
t->addColumn("str", "ItemName");
t->addColumn("vector_int", "Elements");

std::vector<int> group1;
group1.push_back(0); group1.push_back(1);
TableRow row1 = t->appendRow();
row1 << "Group" << "1" << group1;

std::vector<int> group2;
group2.push_back(2); group2.push_back(2); group2.push_back(3);
TableRow row2 = t->appendRow();
row2 << "Group" << "2" << group2;

// Just to make sure groups are used only
std::vector<int> pair;
pair.push_back(0); pair.push_back(1);
TableRow row3 = t->appendRow();
row3 << "Pair" << "ThePair" << pair;

return t;
}

};


#endif /* MANTID_ALGORITHMS_MUONGROUPDETECTORSTEST_H_ */

0 comments on commit 18982a6

Please sign in to comment.