Skip to content

Commit

Permalink
Refs #8534. Two-period functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
arturbekasov committed Dec 2, 2013
1 parent be76f26 commit 952d87b
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ namespace WorkflowAlgorithms

/// TODO: comment
MatrixWorkspace_sptr convertWorkspace(MatrixWorkspace_sptr ws);


/// TODO: comment
MatrixWorkspace_sptr mergePeriods(MatrixWorkspace_sptr ws1, MatrixWorkspace_sptr ws2);
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ namespace WorkflowAlgorithms
declareProperty(new WorkspaceProperty<MatrixWorkspace>("FirstPeriodWorkspace","",Direction::Input),
"First period data. If second period is not specified - the only one used.");

declareProperty(new WorkspaceProperty<MatrixWorkspace>("SecondPeriodWorkspace","",Direction::Output,PropertyMode::Optional),
"Second period data. If not spefied - first period used only.");
declareProperty(new WorkspaceProperty<MatrixWorkspace>("SecondPeriodWorkspace","",Direction::Input,
PropertyMode::Optional), "Second period data. If not spefied - first period used only.");

std::vector<std::string> allowedOperations;
allowedOperations.push_back("+");
Expand Down Expand Up @@ -97,14 +97,22 @@ namespace WorkflowAlgorithms
void MuonCalculateAsymmetry::exec()
{
MatrixWorkspace_sptr firstPeriodWS = getProperty("FirstPeriodWorkspace");
MatrixWorkspace_sptr secondPeriodWS = getProperty("SecondPeriodWorkspace");

if ( getPropertyValue("SecondPeriodWorkspace").empty() )
MatrixWorkspace_sptr firstConverted = convertWorkspace( firstPeriodWS );

if ( secondPeriodWS )
{
// Single period only
// Two periods
MatrixWorkspace_sptr secondConverted = convertWorkspace( secondPeriodWS );

MatrixWorkspace_sptr convertedWS = convertWorkspace(firstPeriodWS);
setProperty( "OutputWorkspace", mergePeriods(firstConverted, secondConverted) );
}
else
{
// Single period only

setProperty("OutputWorkspace", convertedWS);
setProperty("OutputWorkspace", firstConverted);
}
}

Expand Down Expand Up @@ -133,5 +141,34 @@ namespace WorkflowAlgorithms
return alg->getProperty("OutputWorkspace");
}
}

/**
* TODO: comment
*/
MatrixWorkspace_sptr MuonCalculateAsymmetry::mergePeriods(MatrixWorkspace_sptr ws1, MatrixWorkspace_sptr ws2)
{
std::string op = getProperty("PeriodOperation");

std::string algorithmName;

if ( op == "+" )
{
algorithmName = "Plus";
}
else if ( op == "-" )
{
algorithmName = "Minus";
}

IAlgorithm_sptr alg = createChildAlgorithm(algorithmName);
alg->initialize();
alg->setProperty("LHSWorkspace",ws1);
alg->setProperty("RHSWorkspace",ws2);
alg->execute();

MatrixWorkspace_sptr outWS = alg->getProperty("OutputWorkspace");

return outWS;
}
} // namespace WorkflowAlgorithms
} // namespace Mantid
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class MuonCalculateAsymmetryTest : public CxxTest::TestSuite
void test_groupCounts_singlePeriod()
{
// Name of the output workspace.
const std::string outWSName = outputWorkspaceName("GroupCounts");
const std::string outWSName = outputWorkspaceName("GroupCounts_SinglePeriod");

MatrixWorkspace_sptr inWS = createWorkspace();

Expand Down Expand Up @@ -69,6 +69,96 @@ class MuonCalculateAsymmetryTest : public CxxTest::TestSuite
// Remove workspace from the data service.
AnalysisDataService::Instance().remove(outWSName);
}

void test_groupCounts_twoPeriods_plus()
{
// Name of the output workspace.
const std::string outWSName = outputWorkspaceName("GroupCounts_TwoPeriods_Plus");

MatrixWorkspace_sptr inWSFirst = createWorkspace();
MatrixWorkspace_sptr inWSSecond = createWorkspace();

MuonCalculateAsymmetry alg;
alg.initialize();
TS_ASSERT_THROWS_NOTHING( alg.setProperty("FirstPeriodWorkspace", inWSFirst) );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("SecondPeriodWorkspace", inWSSecond) );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("PeriodOperation", "+") );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("OutputType", "GroupCounts") );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("GroupIndex", 1) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("OutputWorkspace", outWSName) );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );

// Retrieve the workspace from data service.
auto ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(outWSName);
TS_ASSERT(ws);

if (ws)
{
TS_ASSERT_EQUALS( ws->getNumberHistograms(), 1 );
TS_ASSERT_EQUALS( ws->blocksize(), 3 );

TS_ASSERT_EQUALS( ws->readY(0)[0], 8 );
TS_ASSERT_EQUALS( ws->readY(0)[1], 10 );
TS_ASSERT_EQUALS( ws->readY(0)[2], 12 );

TS_ASSERT_EQUALS( ws->readX(0)[0], 1 );
TS_ASSERT_EQUALS( ws->readX(0)[1], 2 );
TS_ASSERT_EQUALS( ws->readX(0)[2], 3 );

TS_ASSERT_DELTA( ws->readE(0)[0], 0.566, 0.001 );
TS_ASSERT_DELTA( ws->readE(0)[1], 0.707, 0.001 );
TS_ASSERT_DELTA( ws->readE(0)[2], 0.849, 0.001 );
}

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

void test_groupCounts_twoPeriod_minus()
{
// Name of the output workspace.
const std::string outWSName = outputWorkspaceName("GroupCounts_TwoPeriods_Minus");

MatrixWorkspace_sptr inWSFirst = createWorkspace(3);
MatrixWorkspace_sptr inWSSecond = createWorkspace();

MuonCalculateAsymmetry alg;
alg.initialize();
TS_ASSERT_THROWS_NOTHING( alg.setProperty("FirstPeriodWorkspace", inWSFirst) );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("SecondPeriodWorkspace", inWSSecond) );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("PeriodOperation", "-") );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("OutputType", "GroupCounts") );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("GroupIndex", 1) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("OutputWorkspace", outWSName) );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );

// Retrieve the workspace from data service.
auto ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(outWSName);
TS_ASSERT(ws);

if (ws)
{
TS_ASSERT_EQUALS( ws->getNumberHistograms(), 1 );
TS_ASSERT_EQUALS( ws->blocksize(), 3 );

TS_ASSERT_EQUALS( ws->readY(0)[0], 3 );
TS_ASSERT_EQUALS( ws->readY(0)[1], 3 );
TS_ASSERT_EQUALS( ws->readY(0)[2], 3 );

TS_ASSERT_EQUALS( ws->readX(0)[0], 1 );
TS_ASSERT_EQUALS( ws->readX(0)[1], 2 );
TS_ASSERT_EQUALS( ws->readX(0)[2], 3 );

TS_ASSERT_DELTA( ws->readE(0)[0], 0.806, 0.001 );
TS_ASSERT_DELTA( ws->readE(0)[1], 0.943, 0.001 );
TS_ASSERT_DELTA( ws->readE(0)[2], 1.082, 0.001 );
}

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

private:

Expand Down

0 comments on commit 952d87b

Please sign in to comment.