From f2dacd6ed3e9aa70bb5cfb33b18b6f487416b582 Mon Sep 17 00:00:00 2001
From: Owen Arnold <owen.arnold@stfc.ac.uk>
Date: Thu, 22 May 2014 16:37:58 +0100
Subject: [PATCH] refs #9475. Stop and hand over

I have deliberately left one failing unit test.
---
 .../inc/MantidAlgorithms/Stitch1D.h           |  1 +
 .../Framework/Algorithms/src/Stitch1D.cpp     | 59 ++++++++++++++++++-
 .../plugins/algorithms/Stitch1DMany.py        |  2 +-
 3 files changed, 59 insertions(+), 3 deletions(-)

diff --git a/Code/Mantid/Framework/Algorithms/inc/MantidAlgorithms/Stitch1D.h b/Code/Mantid/Framework/Algorithms/inc/MantidAlgorithms/Stitch1D.h
index e7db22f62f6..c33366a2b7a 100644
--- a/Code/Mantid/Framework/Algorithms/inc/MantidAlgorithms/Stitch1D.h
+++ b/Code/Mantid/Framework/Algorithms/inc/MantidAlgorithms/Stitch1D.h
@@ -49,6 +49,7 @@ namespace Algorithms
     double getEndOverlap(const double& min, const double& max) const;
     Mantid::MantidVec getRebinParams(Mantid::API::MatrixWorkspace_sptr& lhsWS, Mantid::API::MatrixWorkspace_sptr& rhsWS) const;
     Mantid::API::MatrixWorkspace_sptr rebin(Mantid::API::MatrixWorkspace_sptr& input, const Mantid::MantidVec& params);
+    Mantid::API::MatrixWorkspace_sptr integration(Mantid::API::MatrixWorkspace_sptr& input, const double& start, const double& stop);
 
   };
 
diff --git a/Code/Mantid/Framework/Algorithms/src/Stitch1D.cpp b/Code/Mantid/Framework/Algorithms/src/Stitch1D.cpp
index 9c19c733dfc..20f62fff20e 100644
--- a/Code/Mantid/Framework/Algorithms/src/Stitch1D.cpp
+++ b/Code/Mantid/Framework/Algorithms/src/Stitch1D.cpp
@@ -104,7 +104,8 @@ namespace Mantid
           "Start overlap x-value in units of x-axis. Optional.");
       declareProperty(new PropertyWithValue<double>("EndOverlap", Mantid::EMPTY_DBL(), Direction::Input),
           "End overlap x-value in units of x-axis. Optional.");
-      declareProperty(new ArrayProperty<double>("Params", boost::make_shared<RebinParamsValidator>(true)),
+      declareProperty(
+          new ArrayProperty<double>("Params", boost::make_shared<RebinParamsValidator>(true)),
           "Rebinning Parameters. See Rebin for format. If only a single value is provided, start and end are taken from input workspaces.");
       declareProperty(new PropertyWithValue<bool>("ScaleRHSWorkspace", true, Direction::Input),
           "Scaling either with respect to workspace 1 or workspace 2");
@@ -217,6 +218,17 @@ namespace Mantid
       return outWS;
     }
 
+    MatrixWorkspace_sptr Stitch1D::integration(MatrixWorkspace_sptr& input, const double& start, const double& stop)
+    {
+      auto integration = this->createChildAlgorithm("Integration");
+      integration->setProperty("InputWorkspace", input);
+      integration->setProperty("RangeLower", start);
+      integration->setProperty("RangeUpper", stop);
+      integration->execute();
+      MatrixWorkspace_sptr outWS = integration->getProperty("OutputWorkspace");
+      return outWS;
+    }
+
     //----------------------------------------------------------------------------------------------
     /** Execute the algorithm.
      */
@@ -244,11 +256,54 @@ namespace Mantid
 
       MantidVec params = getRebinParams(lhsWS, rhsWS);
 
+      const double& xMin = params.front();
+      const double& xMax = params.back();
+
+      if (startOverlap < xMin)
+      {
+        std::string message =
+            boost::str(
+                boost::format(
+                    "Stitch1D StartOverlap is outside the available X range after rebinning. StartOverlap: %10.9f, X min: %10.9f")
+                    % startOverlap % xMin);
+
+        throw std::runtime_error(message);
+      }
+      if (endOverlap > xMax)
+      {
+        std::string message =
+            boost::str(
+                boost::format(
+                    "Stitch1D EndOverlap is outside the available X range after rebinning. EndOverlap: %10.9f, X max: %10.9f")
+                    % endOverlap % xMax);
+
+        throw std::runtime_error(message);
+      }
+
       auto rebinnedLHS = rebin(lhsWS, params);
       auto rebinnedRHS = rebin(rhsWS, params);
 
+      auto rhsOverlapIntegrated = integration(rebinnedRHS, startOverlap, endOverlap);
+      auto lhsOverlapIntegrated = integration(rebinnedLHS, startOverlap, endOverlap);
+
+      auto y1 = lhsOverlapIntegrated->readY(0);
+      auto y2 = lhsOverlapIntegrated->readY(0);
+      double scaleFactor = 0;
+      if(rhsOverlapIntegrated)
+      {
+        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];
+      }
+
       setProperty("OutputWorkspace", rhsWS);
-      setProperty("OutScaleFactor", 99.0);
+      setProperty("OutScaleFactor", scaleFactor);
 
     }
 
diff --git a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/Stitch1DMany.py b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/Stitch1DMany.py
index 9ec651a532e..f949838e7c2 100644
--- a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/Stitch1DMany.py
+++ b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/Stitch1DMany.py
@@ -82,7 +82,7 @@ class Stitch1DMany(PythonAlgorithm):
         alg.execute()
         out_ws = alg.getProperty("OutputWorkspace").value
         scale_factor = alg.getProperty("OutScaleFactor").value
-        
+        logger.warning(scale_factor)
        
         #out_ws, scale_factor = Stitch1D(LHSWorkspace=lhs_ws, RHSWorkspace=rhs_ws, StartOverlap=start_overlap, EndOverlap=end_overlap, 
         #                                Params=params, ScaleRHSWorkspace=scale_rhs_ws, UseManualScaleFactor=use_manual_scale_factor, ManualScaleFactor=manual_scale_factor, OutputWorkspace=out_name)
-- 
GitLab