Skip to content

Commit

Permalink
Refs #8550. MuonGroupDetectors: code to apply grouping.
Browse files Browse the repository at this point in the history
Plus some tests for it.
  • Loading branch information
arturbekasov committed Dec 6, 2013
1 parent 18982a6 commit 1d6178b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 10 deletions.
48 changes: 41 additions & 7 deletions Code/Mantid/Framework/Algorithms/src/MuonGroupDetectors.cpp
Expand Up @@ -90,27 +90,61 @@ 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<size_t> 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 )
{
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 ( 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<size_t>( 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<int>& detectors = table->cell< std::vector<int> >(*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<specid_t>(groupIndex + 1) );
}

setProperty("OutputWorkspace", outWS);
}
Expand Down
28 changes: 25 additions & 3 deletions Code/Mantid/Framework/Algorithms/test/MuonGroupDetectorsTest.h
Expand Up @@ -10,6 +10,7 @@

using Mantid::Algorithms::MuonGroupDetectors;

using namespace Mantid;
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::DataObjects;
Expand All @@ -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;
Expand All @@ -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<MatrixWorkspace>(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<detid_t> d1;
d1.insert(0); d1.insert(1);
TS_ASSERT_EQUALS( ws->getSpectrum(0)->getDetectorIDs(), d1 );

std::set<detid_t> d2;
d2.insert(2); d2.insert(3); d2.insert(4);
TS_ASSERT_EQUALS( ws->getSpectrum(1)->getDetectorIDs(), d2 );
}

// Remove workspace from the data service.
Expand All @@ -77,7 +99,7 @@ class MuonGroupDetectorsTest : public CxxTest::TestSuite
row1 << "Group" << "1" << group1;

std::vector<int> 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;

Expand Down

0 comments on commit 1d6178b

Please sign in to comment.