Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle failure in CreateGroupingWorkspace where group count > spectra count #262

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 13 additions & 5 deletions Code/Mantid/Framework/Algorithms/src/CreateGroupingWorkspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <queue>
#include <fstream>
#include "MantidAPI/FileProperty.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/ListValidator.h"

namespace {
Expand Down Expand Up @@ -92,9 +93,10 @@ void CreateGroupingWorkspace::init() {
declareProperty("MaxRecursionDepth", 5,
"Number of levels to search into the instrument (default=5)");

declareProperty("FixedGroupCount", 0, "Used to distribute the detectors of a "
"given component into a fixed number "
"of groups");
declareProperty("FixedGroupCount", 0,
boost::make_shared<BoundedValidator<int> >(0, INT_MAX),
"Used to distribute the detectors of a given component into "
"a fixed number of groups");
declareProperty("ComponentName", "", "Specify the instrument component to "
"group into a fixed number of groups");

Expand Down Expand Up @@ -169,12 +171,18 @@ void makeGroupingByNumGroups(const std::string compName, int numGroups,
// Get detectors for given instument component
std::vector<IDetector_const_sptr> detectors;
inst->getDetectorsInBank(detectors, compName);
size_t numDetectors = detectors.size();

// Sanity check for following calculation
if(numGroups > static_cast<int>(numDetectors))
throw std::runtime_error("Number of groups must be less than or "
"equal to number of detectors");

// Calculate number of detectors per group
int detectorsPerGroup = static_cast<int>(detectors.size()) / numGroups;
int detectorsPerGroup = static_cast<int>(numDetectors) / numGroups;

// Map detectors to group
for (unsigned int detIndex = 0; detIndex < detectors.size(); detIndex++) {
for (unsigned int detIndex = 0; detIndex < numDetectors; detIndex++) {
int detectorID = detectors[detIndex]->getID();
int groupNum = (detIndex / detectorsPerGroup) + 1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,26 @@ class CreateGroupingWorkspaceTest : public CxxTest::TestSuite
AnalysisDataService::Instance().remove(outWSName);
}

void test_exec_WithFixedGroups_FailOnGroupsGreaterThanDet()
{
// Name of the output workspace.
std::string outWSName("CreateGroupingWorkspaceTest_OutputWS_fail");

CreateGroupingWorkspace alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("InstrumentName", "IRIS") );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("FixedGroupCount", 52) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("ComponentName", "graphite") );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("OutputWorkspace", outWSName) );
TS_ASSERT_THROWS_NOTHING( alg.execute() );

// Should fail as IRIS graphite component has only 51 spectra
TS_ASSERT( !alg.isExecuted() );

AnalysisDataService::Instance().remove(outWSName);
}

};


Expand Down