Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs #8550. Initial files for MuonGroupDetectors.
- Loading branch information
1 parent
69ea041
commit 18982a6
Showing
4 changed files
with
282 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
Code/Mantid/Framework/Algorithms/inc/MantidAlgorithms/MuonGroupDetectors.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
121
Code/Mantid/Framework/Algorithms/src/MuonGroupDetectors.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
96
Code/Mantid/Framework/Algorithms/test/MuonGroupDetectorsTest.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_ */ |