Skip to content

Commit

Permalink
Refs #4561. Fix bug with Run summing in Plus
Browse files Browse the repository at this point in the history
The run values, charge etc, are now summed correctly regardless of
whether lhs/rhs are the output.
  • Loading branch information
martyngigg committed Jan 24, 2012
1 parent 6e3da6e commit a00aeff
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
16 changes: 14 additions & 2 deletions Code/Mantid/Framework/Algorithms/src/Plus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,20 @@ namespace Mantid
void Plus::operateOnRun(const Run& lhs, const Run& rhs, Run& ans) const
{
//The addition operator of Run will add the proton charges, append logs, etc.
ans = lhs;
ans += rhs;
// If ans=lhs or ans=rhs then we need to be careful in which order we do this
if( &rhs == &ans )
{
// The output is the same as the RHS workspace so needs to be set to that run object
ans = rhs;
ans += lhs;
}
else
{
// The output is either the same as the LHS workspace so needs to be set to that run object
// or it is a completely separate workspace meaning that it actually doesn't matter
ans = lhs;
ans += rhs;
}
}


Expand Down
39 changes: 24 additions & 15 deletions Code/Mantid/Framework/Algorithms/test/PlusMinusTest.in.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,24 @@ class @PLUSMINUSTEST_CLASS@ : public CxxTest::TestSuite
}
}

void doRunTest(const std::string & lhs, const std::string & rhs,
const std::string & output, const double expectedCharge)
{
Plus alg;
alg.initialize();
TS_ASSERT_THROWS_NOTHING(
alg.setPropertyValue("LHSWorkspace",lhs);
alg.setPropertyValue("RHSWorkspace",rhs);
alg.setPropertyValue("OutputWorkspace",output);
);
alg.execute();

MatrixWorkspace_sptr work_out1;
TS_ASSERT_THROWS_NOTHING(work_out1 = boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve(output)));

TS_ASSERT_DELTA(work_out1->run().getProtonCharge(), expectedCharge, 1e-8);

}

/// The Plus algorithm sums values in the Run object. Minus does not.
void test_RunAddition()
Expand All @@ -107,27 +125,18 @@ class @PLUSMINUSTEST_CLASS@ : public CxxTest::TestSuite
a->mutableRun().setProtonCharge(10.);
MatrixWorkspace_sptr b = WorkspaceCreationHelper::CreateWorkspaceSingleValue(2);
b->mutableRun().setProtonCharge(5.);

AnalysisDataService::Instance().add("a", a);
AnalysisDataService::Instance().add("b", b);

Plus alg;
alg.initialize();
TS_ASSERT_THROWS_NOTHING(
alg.setPropertyValue("LHSWorkspace","a");
alg.setPropertyValue("RHSWorkspace","b");
alg.setPropertyValue("OutputWorkspace","c");
)
alg.execute();

MatrixWorkspace_sptr work_out1;
TS_ASSERT_THROWS_NOTHING(work_out1 = boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve("c")));

TS_ASSERT_DELTA(work_out1->run().getProtonCharge(), 15.0, 1e-8);
doRunTest("a", "b", "c", 15.0);
AnalysisDataService::Instance().remove("c");
// In-place with LHS as output
doRunTest("a", "b", "a", 15.0);
// In-place with RHS as output
doRunTest("a", "b", "b", 20.0);

AnalysisDataService::Instance().remove("a");
AnalysisDataService::Instance().remove("b");
AnalysisDataService::Instance().remove("c");
}
}

Expand Down

0 comments on commit a00aeff

Please sign in to comment.