diff --git a/Code/Mantid/Framework/Algorithms/src/MuonGroupDetectors.cpp b/Code/Mantid/Framework/Algorithms/src/MuonGroupDetectors.cpp index 510bdbb28c5a..86c796e861e7 100644 --- a/Code/Mantid/Framework/Algorithms/src/MuonGroupDetectors.cpp +++ b/Code/Mantid/Framework/Algorithms/src/MuonGroupDetectors.cpp @@ -90,7 +90,8 @@ namespace Algorithms 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); + std::vector groupRows; // Rows with non-empty groups + groupRows.reserve(table->rowCount()); // Most of rows will be groups // First pass to determine how many non-empty groups we have for ( size_t row = 0; row < table->rowCount(); ++row ) @@ -98,19 +99,52 @@ namespace Algorithms std::string& itemName = table->cell(row, 0); std::vector& elements = table->cell< std::vector >(row, 2); - if ( itemName == "Group" && elements.size() != 0 ) - { - numGroups++; - } + if ( itemName == "Group" && elements.size() != 0 ) + groupRows.push_back(row); + } - if ( numGroups == 0 ) + if ( groupRows.size() == 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); + MatrixWorkspace_sptr outWS = WorkspaceFactory::Instance().create( inWS, groupRows.size() ); + + // Compile the groups + for ( auto groupIt = groupRows.begin(); groupIt != groupRows.end(); ++groupIt ) + { + size_t groupIndex = static_cast( std::distance(groupRows.begin(),groupIt) ); + + // Not "detectors" as such, but workspace indices. For Muons there is only one detector for + // workspace index in the data before grouping. + std::vector& detectors = table->cell< std::vector >(*groupIt,2); + + // We will be setting them anew + outWS->getSpectrum(groupIndex)->clearDetectorIDs(); + + for(auto detIt = detectors.begin(); detIt != detectors.end(); detIt++) + { + for( size_t i = 0; i < inWS->blocksize(); ++i ) + { + // Sum the y values + outWS->dataY(groupIndex)[i] += inWS->dataY(*detIt)[i]; + + // Sum the errors in quadrature + outWS->dataE(groupIndex)[i] = + sqrt(pow(outWS->dataE(groupIndex)[i], 2) + pow(inWS->dataE(*detIt)[i], 2)); + } + + // Detectors list of the group should contain all the detectors of it's elements + outWS->getSpectrum(groupIndex)->addDetectorIDs( inWS->getSpectrum(*detIt)->getDetectorIDs() ); + } + + // Using the first detector X values + outWS->dataX(groupIndex) = inWS->dataX(detectors.front()); + + outWS->getSpectrum(groupIndex)->setSpectrumNo( static_cast(groupIndex + 1) ); + } setProperty("OutputWorkspace", outWS); } diff --git a/Code/Mantid/Framework/Algorithms/test/MuonGroupDetectorsTest.h b/Code/Mantid/Framework/Algorithms/test/MuonGroupDetectorsTest.h index 767051c33f64..8d1bce31171d 100644 --- a/Code/Mantid/Framework/Algorithms/test/MuonGroupDetectorsTest.h +++ b/Code/Mantid/Framework/Algorithms/test/MuonGroupDetectorsTest.h @@ -10,6 +10,7 @@ using Mantid::Algorithms::MuonGroupDetectors; +using namespace Mantid; using namespace Mantid::Kernel; using namespace Mantid::API; using namespace Mantid::DataObjects; @@ -36,6 +37,7 @@ class MuonGroupDetectorsTest : public CxxTest::TestSuite const std::string outWSName("MuonGroupDetectorsTest_OutputWS"); MatrixWorkspace_sptr inWS = WorkspaceCreationHelper::Create2DWorkspace123(5,3); + TableWorkspace_sptr grouping = createDetectorGroupingTable(); MuonGroupDetectors alg; @@ -47,14 +49,34 @@ class MuonGroupDetectorsTest : public CxxTest::TestSuite 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(outWSName) ); TS_ASSERT(ws); if ( ws ) { - TSM_ASSERT( "You forgot to check the results!", 0); + TS_ASSERT_EQUALS( ws->getNumberHistograms(), 2); + TS_ASSERT_EQUALS( ws->blocksize(), 3 ); + + TS_ASSERT_EQUALS( ws->readY(0)[0], 4 ); + TS_ASSERT_EQUALS( ws->readY(1)[0], 6 ); + + TS_ASSERT_EQUALS( ws->readX(0)[1], 1 ); + TS_ASSERT_EQUALS( ws->readX(1)[1], 1 ); + + TS_ASSERT_DELTA( ws->readE(0)[2], 4.243, 0.001); + TS_ASSERT_DELTA( ws->readE(1)[2], 5.196, 0.001); + + TS_ASSERT_EQUALS( ws->getSpectrum(0)->getSpectrumNo(), 1); + TS_ASSERT_EQUALS( ws->getSpectrum(1)->getSpectrumNo(), 2); + + std::set d1; + d1.insert(0); d1.insert(1); + TS_ASSERT_EQUALS( ws->getSpectrum(0)->getDetectorIDs(), d1 ); + + std::set d2; + d2.insert(2); d2.insert(3); d2.insert(4); + TS_ASSERT_EQUALS( ws->getSpectrum(1)->getDetectorIDs(), d2 ); } // Remove workspace from the data service. @@ -77,7 +99,7 @@ class MuonGroupDetectorsTest : public CxxTest::TestSuite row1 << "Group" << "1" << group1; std::vector group2; - group2.push_back(2); group2.push_back(2); group2.push_back(3); + group2.push_back(2); group2.push_back(3); group2.push_back(4); TableRow row2 = t->appendRow(); row2 << "Group" << "2" << group2;