From 080e6553f9619a0406a2fa114e939c8582cc8996 Mon Sep 17 00:00:00 2001 From: Keith Brown Date: Fri, 30 May 2014 15:43:07 +0100 Subject: [PATCH] Refs #9475 Manual Scale Factor and related tests Manual scale factor override is now implemented Tests have been ported --- .../Framework/Algorithms/src/Stitch1D.cpp | 49 ++++++++--- .../Framework/Algorithms/test/Stitch1DTest.h | 86 ++++++++++++++++++- 2 files changed, 117 insertions(+), 18 deletions(-) diff --git a/Code/Mantid/Framework/Algorithms/src/Stitch1D.cpp b/Code/Mantid/Framework/Algorithms/src/Stitch1D.cpp index 20ad512a6f01..94e3c9bde9d8 100644 --- a/Code/Mantid/Framework/Algorithms/src/Stitch1D.cpp +++ b/Code/Mantid/Framework/Algorithms/src/Stitch1D.cpp @@ -116,6 +116,9 @@ namespace Mantid "Scaling either with respect to workspace 1 or workspace 2"); declareProperty(new PropertyWithValue("UseManualScaleFactor", false, Direction::Input), "True to use a provided value for the scale factor."); + declareProperty( + new PropertyWithValue("ManualScaleFactor", 1.0, Direction::Input), + "Provided value for the scale factor. Optional."); declareProperty( new PropertyWithValue("OutScaleFactor", Mantid::EMPTY_DBL(), Direction::Output), "The actual used value for the scaling factor."); @@ -361,26 +364,44 @@ namespace Mantid bool scaleRHS = this->getProperty("ScaleRHSWorkspace"); //manualscalefactor if - - auto rhsOverlapIntegrated = integration(rebinnedRHS, startOverlap, endOverlap); - auto lhsOverlapIntegrated = integration(rebinnedLHS, startOverlap, endOverlap); - - auto y1 = lhsOverlapIntegrated->readY(0); - auto y2 = rhsOverlapIntegrated->readY(0); + bool manualScaleFactor = this->getProperty("UseManualScaleFactor"); double scaleFactor = 0; - if(scaleRHS) + + if (manualScaleFactor) { - MatrixWorkspace_sptr ratio = lhsOverlapIntegrated/rhsOverlapIntegrated; - rebinnedRHS = rebinnedRHS * ratio; - scaleFactor = y1[0]/y2[0]; + double manualScaleFactor = this->getProperty("ManualScaleFactor"); + MatrixWorkspace_sptr manualScaleFactorWS = singleValueWS(manualScaleFactor); + + if(scaleRHS) + { + rebinnedRHS = rebinnedRHS * manualScaleFactorWS; + } + else + { + rebinnedLHS = rebinnedLHS * manualScaleFactorWS; + } + scaleFactor = manualScaleFactor; } else { - MatrixWorkspace_sptr ratio = rhsOverlapIntegrated/lhsOverlapIntegrated; - rebinnedLHS = rebinnedLHS * ratio; - scaleFactor = y2[0]/y1[0]; - } + auto rhsOverlapIntegrated = integration(rebinnedRHS, startOverlap, endOverlap); + auto lhsOverlapIntegrated = integration(rebinnedLHS, startOverlap, endOverlap); + auto y1 = lhsOverlapIntegrated->readY(0); + auto y2 = rhsOverlapIntegrated->readY(0); + if(scaleRHS) + { + MatrixWorkspace_sptr ratio = lhsOverlapIntegrated/rhsOverlapIntegrated; + rebinnedRHS = rebinnedRHS * ratio; + scaleFactor = y1[0]/y2[0]; + } + else + { + MatrixWorkspace_sptr ratio = rhsOverlapIntegrated/lhsOverlapIntegrated; + rebinnedLHS = rebinnedLHS * ratio; + scaleFactor = y2[0]/y1[0]; + } + } //manualscalefactor end if int a1 = boost::tuples::get<0>(startEnd); diff --git a/Code/Mantid/Framework/Algorithms/test/Stitch1DTest.h b/Code/Mantid/Framework/Algorithms/test/Stitch1DTest.h index 01f5561e25f6..6ce14a9e3809 100644 --- a/Code/Mantid/Framework/Algorithms/test/Stitch1DTest.h +++ b/Code/Mantid/Framework/Algorithms/test/Stitch1DTest.h @@ -172,6 +172,28 @@ class Stitch1DTest: public CxxTest::TestSuite return ResultType(stitched, scaleFactor); } + ResultType do_stitch1D(MatrixWorkspace_sptr& lhs, MatrixWorkspace_sptr& rhs, bool scaleRHS, bool useManualScaleFactor, + const double& startOverlap, const double& endOverlap, const MantidVec& params, const double& manualScaleFactor) + { + Stitch1D alg; + alg.setChild(true); + alg.setRethrows(true); + alg.initialize(); + alg.setProperty("LHSWorkspace", lhs); + alg.setProperty("RHSWorkspace", rhs); + alg.setProperty("ScaleRHSWorkspace", scaleRHS); + alg.setProperty("UseManualScaleFactor", useManualScaleFactor); + alg.setProperty("StartOverlap", startOverlap); + alg.setProperty("EndOverlap", endOverlap); + alg.setProperty("Params", params); + alg.setProperty("ManualScaleFactor", manualScaleFactor); + alg.setPropertyValue("OutputWorkspace", "dummy_value"); + alg.execute(); + MatrixWorkspace_sptr stitched = alg.getProperty("OutputWorkspace"); + double scaleFactor = alg.getProperty("OutScaleFactor"); + return ResultType(stitched, scaleFactor); + } + ResultType do_stitch1D(MatrixWorkspace_sptr& lhs, MatrixWorkspace_sptr& rhs, const double& startOverlap, const double& endOverlap, const MantidVec& params, bool scaleRHS = true) { @@ -365,8 +387,6 @@ class Stitch1DTest: public CxxTest::TestSuite void test_stitching_scale_right() { - std::cout << "test_stitching_scale_right" << std::endl; - auto ret = do_stitch1D(this->b, this->a, -0.4, 0.4,boost::assign::list_of(0.2).convert_to_container()); double scale = ret.get<1>(); @@ -397,8 +417,6 @@ class Stitch1DTest: public CxxTest::TestSuite void test_stitching_scale_left() { - std::cout << "test_stitching_scale_left" << std::endl; - auto ret = do_stitch1D(this->b, this->a, -0.4, 0.4,boost::assign::list_of(0.2).convert_to_container(),false); double scale = ret.get<1>(); @@ -426,6 +444,66 @@ class Stitch1DTest: public CxxTest::TestSuite std::transform(xCopy.begin(),xCopy.end(),xCopy.begin(),roundSix); TS_ASSERT(xCopy == stitched_x); } + + void test_stitching_manual_scale_factor_scale_right() + { + auto ret = do_stitch1D(this->b, this->a, true, true, -0.4, 0.4,boost::assign::list_of(0.2).convert_to_container(),2.0/3.0); + + double scale = ret.get<1>(); + // Check the scale factor + TS_ASSERT_DELTA(scale, 2.0/3.0, 0.000000001); + // Fetch the arrays from the output workspace + MantidVec stitched_y = ret.get<0>()->readY(0); + MantidVec stitched_x = ret.get<0>()->readX(0); + MantidVec stitched_e = ret.get<0>()->readE(0); + // Check that the output Y-Values are correct. Output Y Values should all be 2 + for (auto itr = stitched_y.begin(); itr != stitched_y.end(); ++itr) + { + TS_ASSERT_DELTA(2, *itr, 0.000001); + } + // Check that the output E-Values are correct. Output Error values should all be zero + for (auto itr = stitched_e.begin(); itr != stitched_e.end(); ++itr) + { + double temp = *itr; + TS_ASSERT_EQUALS(temp,0); + } + // Check that the output X-Values are correct. + //truncate the input and oputput x values to 6 decimal places to eliminate insignificant error + MantidVec xCopy = this->x; + std::transform(stitched_x.begin(),stitched_x.end(),stitched_x.begin(),roundSix); + std::transform(xCopy.begin(),xCopy.end(),xCopy.begin(),roundSix); + TS_ASSERT(xCopy == stitched_x); + } + + void test_stitching_manual_scale_factor_scale_left() + { + auto ret = do_stitch1D(this->b, this->a, false, true, -0.4, 0.4, boost::assign::list_of(0.2).convert_to_container(), 3.0/2.0); + + double scale = ret.get<1>(); + // Check the scale factor + TS_ASSERT_DELTA(scale, 3.0/2.0, 0.000000001); + // Fetch the arrays from the output workspace + MantidVec stitched_y = ret.get<0>()->readY(0); + MantidVec stitched_x = ret.get<0>()->readX(0); + MantidVec stitched_e = ret.get<0>()->readE(0); + // Check that the output Y-Values are correct. Output Y Values should all be 3 + for (auto itr = stitched_y.begin(); itr != stitched_y.end(); ++itr) + { + TS_ASSERT_DELTA(3, *itr, 0.000001); + } + // Check that the output E-Values are correct. Output Error values should all be zero + for (auto itr = stitched_e.begin(); itr != stitched_e.end(); ++itr) + { + double temp = *itr; + TS_ASSERT_EQUALS(temp,0); + } + // Check that the output X-Values are correct. + //truncate the input and oputput x values to 6 decimal places to eliminate insignificant error + MantidVec xCopy = this->x; + std::transform(stitched_x.begin(),stitched_x.end(),stitched_x.begin(),roundSix); + std::transform(xCopy.begin(),xCopy.end(),xCopy.begin(),roundSix); + TS_ASSERT(xCopy == stitched_x); + } }; #endif /* MANTID_ALGORITHMS_STITCH1DTEST_H_ */