From a2793e40a400ba38701de0759896e522ff59391c Mon Sep 17 00:00:00 2001
From: Owen Arnold <owen.arnold@stfc.ac.uk>
Date: Fri, 11 Apr 2014 09:12:02 +0100
Subject: [PATCH] refs #9257. Add clone method.

---
 .../API/inc/MantidAPI/IMDHistoWorkspace.h     |  3 ++-
 .../src/ConnectedComponentLabeling.cpp        | 20 +++++--------------
 .../inc/MantidMDEvents/MDHistoWorkspace.h     |  4 ++--
 .../MDEvents/src/MDHistoWorkspace.cpp         |  9 +++++++--
 4 files changed, 16 insertions(+), 20 deletions(-)

diff --git a/Code/Mantid/Framework/API/inc/MantidAPI/IMDHistoWorkspace.h b/Code/Mantid/Framework/API/inc/MantidAPI/IMDHistoWorkspace.h
index 5da50570e19..e379c3be288 100644
--- a/Code/Mantid/Framework/API/inc/MantidAPI/IMDHistoWorkspace.h
+++ b/Code/Mantid/Framework/API/inc/MantidAPI/IMDHistoWorkspace.h
@@ -79,7 +79,8 @@ namespace API
 
     virtual void setCoordinateSystem(const Mantid::API::SpecialCoordinateSystem coordinateSystem) = 0;
 
-    boost::shared_ptr<IMDHistoWorkspace> cloneShape() const = 0;
+    virtual boost::shared_ptr<IMDHistoWorkspace> clone() const = 0;
+
   protected:
     virtual const std::string toString() const;
   };
diff --git a/Code/Mantid/Framework/Crystal/src/ConnectedComponentLabeling.cpp b/Code/Mantid/Framework/Crystal/src/ConnectedComponentLabeling.cpp
index abca8d23869..b039e21d3e9 100644
--- a/Code/Mantid/Framework/Crystal/src/ConnectedComponentLabeling.cpp
+++ b/Code/Mantid/Framework/Crystal/src/ConnectedComponentLabeling.cpp
@@ -1,7 +1,6 @@
 #include "MantidKernel/MultiThreaded.h"
 #include "MantidKernel/Logger.h"
 #include "MantidKernel/V3D.h"
-#include "MantidAPI/AlgorithmManager.h"
 #include "MantidAPI/FrameworkManager.h"
 #include "MantidAPI/IMDHistoWorkspace.h"
 #include "MantidAPI/IMDIterator.h"
@@ -74,17 +73,8 @@ namespace Mantid
       */
       boost::shared_ptr<Mantid::API::IMDHistoWorkspace> cloneInputWorkspace(IMDHistoWorkspace_sptr& inWS)
       {
-        auto alg = AlgorithmManager::Instance().createUnmanaged("CloneWorkspace");
-        alg->initialize();
-        alg->setChild(true);
-        alg->setProperty("InputWorkspace", inWS);
-        alg->setPropertyValue("OutputWorkspace", "out_ws");
-        alg->execute();
-        Mantid::API::IMDHistoWorkspace_sptr outWS;
-        {
-          Mantid::API::Workspace_sptr temp = alg->getProperty("OutputWorkspace");
-          outWS = boost::dynamic_pointer_cast<IMDHistoWorkspace>(temp);
-        }
+        IMDHistoWorkspace_sptr outWS = inWS->clone();
+
         // Initialize to zero.
         PARALLEL_FOR_NO_WSP_CHECK()
         for(int i = 0; i < static_cast<int>(outWS->getNPoints()); ++i)
@@ -421,13 +411,11 @@ namespace Mantid
           m_logger.debug("Remove duplicates");
           m_logger.debug() << incompleteClusterVec.size() << " clusters to reconstruct" << std::endl;
             // Now combine clusters and add the resolved clusters to the clusterMap.
-            SetIds usedOwningClusterIds;
             for(size_t i = 0; i < incompleteClusterVec.size(); ++i)
             {
               const size_t label = incompleteClusterVec[i]->getLabel();
-              if(usedOwningClusterIds.find(label) == usedOwningClusterIds.end())
+              if(does_contain_key(clusterMap, label))
               {
-                usedOwningClusterIds.insert(label);
                 clusterMap.insert(std::make_pair(label,  incompleteClusterVec[i]));
               }
               else
@@ -500,7 +488,9 @@ namespace Mantid
       ClusterMap clusters = calculateDisjointTree(ws, strategy,progress);
 
       // Create the output workspace from the input workspace
+      m_logger.debug("Start cloning input workspace");
       IMDHistoWorkspace_sptr outWS = cloneInputWorkspace(ws);
+      m_logger.debug("Finish cloning input workspace");
       
       // Get the keys (label ids) first in order to do the next stage in parallel.
       VecIndexes keys;
diff --git a/Code/Mantid/Framework/MDEvents/inc/MantidMDEvents/MDHistoWorkspace.h b/Code/Mantid/Framework/MDEvents/inc/MantidMDEvents/MDHistoWorkspace.h
index d0cd3edb867..31c99fd9567 100644
--- a/Code/Mantid/Framework/MDEvents/inc/MantidMDEvents/MDHistoWorkspace.h
+++ b/Code/Mantid/Framework/MDEvents/inc/MantidMDEvents/MDHistoWorkspace.h
@@ -401,8 +401,8 @@ namespace MDEvents
     /// Get the size of an element in the HistoWorkspace.
     static size_t sizeOfElement();
 
-    /// Virtual constructor.
-    boost::shared_ptr<IMDHistoWorkspace> cloneShape() const;
+    /// Virutal constructor.
+    boost::shared_ptr<IMDHistoWorkspace> clone() const;
 
   private:
 
diff --git a/Code/Mantid/Framework/MDEvents/src/MDHistoWorkspace.cpp b/Code/Mantid/Framework/MDEvents/src/MDHistoWorkspace.cpp
index 669866466d9..28aee6a966d 100644
--- a/Code/Mantid/Framework/MDEvents/src/MDHistoWorkspace.cpp
+++ b/Code/Mantid/Framework/MDEvents/src/MDHistoWorkspace.cpp
@@ -11,6 +11,7 @@
 #include "MantidAPI/IMDWorkspace.h"
 #include "MantidAPI/IMDIterator.h"
 #include <boost/scoped_array.hpp>
+#include <boost/make_shared.hpp>
 
 using namespace Mantid::Kernel;
 using namespace Mantid::Geometry;
@@ -1308,9 +1309,13 @@ namespace MDEvents
     return (3 * sizeof(signal_t) ) + sizeof(bool);
   }
 
-  boost::shared_ptr<IMDHistoWorkspace> MDHistoWorkspace::clone() const;
+  /**
+   * Clone the workspace.
+   * @return Deep copy of existing workspace.
+   */
+  boost::shared_ptr<IMDHistoWorkspace> MDHistoWorkspace::clone() const
   {
-    return boost::make_
+    return boost::shared_ptr<IMDHistoWorkspace>(new MDHistoWorkspace(*this));
   }
 
 } // namespace Mantid
-- 
GitLab