From 6565b65bdeaead38c14a2d445a5e72418b7b6a8f Mon Sep 17 00:00:00 2001
From: Owen Arnold <owen.arnold@stfc.ac.uk>
Date: Thu, 21 Jun 2012 09:43:06 +0100
Subject: [PATCH] refs #5489 #5492. Fix warnings and newly introduced error.

---
 Code/Mantid/Framework/MDEvents/CMakeLists.txt |   8 +-
 .../MantidMDEvents/ImportMDHistoWorkspace.h   |   4 +-
 .../ImportMDHistoWorkspaceBase.h              |  67 ++++++++++
 .../MDEvents/src/ImportMDHistoWorkspace.cpp   |  82 ++----------
 .../src/ImportMDHistoWorkspaceBase.cpp        | 121 ++++++++++++++++++
 5 files changed, 205 insertions(+), 77 deletions(-)
 create mode 100644 Code/Mantid/Framework/MDEvents/inc/MantidMDEvents/ImportMDHistoWorkspaceBase.h
 create mode 100644 Code/Mantid/Framework/MDEvents/src/ImportMDHistoWorkspaceBase.cpp

diff --git a/Code/Mantid/Framework/MDEvents/CMakeLists.txt b/Code/Mantid/Framework/MDEvents/CMakeLists.txt
index 95eb88b8b06..b79b8dc1456 100644
--- a/Code/Mantid/Framework/MDEvents/CMakeLists.txt
+++ b/Code/Mantid/Framework/MDEvents/CMakeLists.txt
@@ -23,7 +23,8 @@ set ( SRC_FILES
 	src/CreateMDWorkspace.cpp
 	src/FakeMDEventData.cpp
 	src/FindPeaksMD.cpp
-  src/ImportMDHistoWorkspace.cpp
+	src/ImportMDHistoWorkspace.cpp
+	src/ImportMDHistoWorkspaceBase.cpp
 	src/IntegratePeaksMD.cpp
 	src/LoadMD.cpp
 	src/LoadSQW.cpp
@@ -91,7 +92,8 @@ set ( INC_FILES
 	inc/MantidMDEvents/CreateMDWorkspace.h
 	inc/MantidMDEvents/FakeMDEventData.h
 	inc/MantidMDEvents/FindPeaksMD.h
-  inc/MantidMDEvents/ImportMDHistoWorkspace.h
+	inc/MantidMDEvents/ImportMDHistoWorkspace.h
+	inc/MantidMDEvents/ImportMDHistoWorkspaceBase.h
 	inc/MantidMDEvents/IntegratePeaksMD.h
 	inc/MantidMDEvents/LoadMD.h
 	inc/MantidMDEvents/LoadSQW.h
@@ -153,7 +155,7 @@ set ( TEST_FILES
 	test/CreateMDWorkspaceTest.h
 	test/FakeMDEventDataTest.h
 	test/FindPeaksMDTest.h
-  test/ImportMDHistoWorkspaceTest.h
+	test/ImportMDHistoWorkspaceTest.h
 	test/IntegratePeaksMDTest.h
 	test/LoadMDTest.h
 	test/LoadSQWTest.h
diff --git a/Code/Mantid/Framework/MDEvents/inc/MantidMDEvents/ImportMDHistoWorkspace.h b/Code/Mantid/Framework/MDEvents/inc/MantidMDEvents/ImportMDHistoWorkspace.h
index 59d03c3aebe..12fa3b17080 100644
--- a/Code/Mantid/Framework/MDEvents/inc/MantidMDEvents/ImportMDHistoWorkspace.h
+++ b/Code/Mantid/Framework/MDEvents/inc/MantidMDEvents/ImportMDHistoWorkspace.h
@@ -2,7 +2,7 @@
 #define MANTID_MDEVENTS_IMPORTMDHISTOWORKSPACE_H_
 
 #include "MantidKernel/System.h"
-#include "MantidAPI/Algorithm.h"
+#include "MantidMDEvents/ImportMDHistoWorkspaceBase.h"
 
 namespace Mantid
 {
@@ -34,7 +34,7 @@ namespace MDEvents
     File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>
     Code Documentation is available at: <http://doxygen.mantidproject.org>
   */
-  class DLLExport ImportMDHistoWorkspace  : public API::Algorithm
+  class DLLExport ImportMDHistoWorkspace  : public ImportMDHistoWorkspaceBase
   {
   public:
     ImportMDHistoWorkspace();
diff --git a/Code/Mantid/Framework/MDEvents/inc/MantidMDEvents/ImportMDHistoWorkspaceBase.h b/Code/Mantid/Framework/MDEvents/inc/MantidMDEvents/ImportMDHistoWorkspaceBase.h
new file mode 100644
index 00000000000..219465b0c13
--- /dev/null
+++ b/Code/Mantid/Framework/MDEvents/inc/MantidMDEvents/ImportMDHistoWorkspaceBase.h
@@ -0,0 +1,67 @@
+#ifndef MANTID_MDEVENTS_IMPORTMDHISTOWORKSPACEBASE_H_
+#define MANTID_MDEVENTS_IMPORTMDHISTOWORKSPACEBASE_H_
+
+#include "MantidKernel/System.h"
+#include "MantidAPI/Algorithm.h"
+#include "MantidMDEvents/MDHistoWorkspace.h"
+#include <vector>
+
+namespace Mantid
+{
+namespace MDEvents
+{
+
+  /** ImportMDHistoWorkspaceBase : Base class for algorithms Importing data as MDHistoWorkspaces.
+    
+    @date 2012-06-21
+
+    Copyright &copy; 2012 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
+
+    This file is part of Mantid.
+
+    Mantid is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 3 of the License, or
+    (at your option) any later version.
+
+    Mantid is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+    File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>
+    Code Documentation is available at: <http://doxygen.mantidproject.org>
+  */
+  class DLLExport ImportMDHistoWorkspaceBase  : public API::Algorithm
+  {
+  public:
+
+    ImportMDHistoWorkspaceBase();
+    virtual ~ImportMDHistoWorkspaceBase();
+    
+  protected:
+    /// Vector containing the number of bins in each dimension.
+    std::vector<int> nbins;
+    /// Creates an empty md histo workspace (with dimensions)
+    MDHistoWorkspace_sptr ImportMDHistoWorkspaceBase::createEmptyOutputWorkspace();
+    /// Initialise the properties associated with the generic import (those to do with dimensionality).
+    void initGenericImportProps();
+    /// Getter for the number of bins (product accross all dimensions)
+    size_t getBinProduct() const
+    {
+      return m_bin_product;
+    }
+
+  private:
+    // Product of the bins across all dimensions.
+    size_t m_bin_product;
+  };
+
+
+} // namespace MDEvents
+} // namespace Mantid
+
+#endif  /* MANTID_MDEVENTS_IMPORTMDHISTOWORKSPACEBASE_H_ */
\ No newline at end of file
diff --git a/Code/Mantid/Framework/MDEvents/src/ImportMDHistoWorkspace.cpp b/Code/Mantid/Framework/MDEvents/src/ImportMDHistoWorkspace.cpp
index 8fb943575cb..b5071b40992 100644
--- a/Code/Mantid/Framework/MDEvents/src/ImportMDHistoWorkspace.cpp
+++ b/Code/Mantid/Framework/MDEvents/src/ImportMDHistoWorkspace.cpp
@@ -14,20 +14,19 @@ Signal and Error inputs are read in such that, the first entries in the file wil
 *WIKI*/
 
 #include "MantidMDEvents/ImportMDHistoWorkspace.h"
-#include "MantidMDEvents/MDHistoWorkspace.h"
 #include "MantidAPI/FileProperty.h"
-#include "MantidAPI/Progress.h"
+
+#include "MantidMDEvents/MDHistoWorkspace.h"
 #include "MantidKernel/CompositeValidator.h"
 #include "MantidKernel/BoundedValidator.h"
 #include "MantidKernel/MandatoryValidator.h"
 #include "MantidKernel/ArrayProperty.h"
-#include "MantidKernel/System.h"
 #include <vector>
+
 #include <deque>
 #include <iostream>
 #include <fstream>
 #include <iterator>
-#include <algorithm>
 
 
 using namespace Mantid::Kernel;
@@ -41,22 +40,11 @@ namespace MDEvents
 
   // Register the algorithm into the AlgorithmFactory
   DECLARE_ALGORITHM(ImportMDHistoWorkspace)
-  
-  /**
-  Functor to compute the product of the set.
-  */
-  struct Product : public std::unary_function<size_t, void>
-  {
-    Product() : result(1) {}
-    size_t result;
-    void operator()(size_t x) { result *= x; }
-  };
-
 
   //----------------------------------------------------------------------------------------------
   /** Constructor
    */
-  ImportMDHistoWorkspace::ImportMDHistoWorkspace()
+  ImportMDHistoWorkspace::ImportMDHistoWorkspace() : ImportMDHistoWorkspaceBase()
   {
   }
     
@@ -96,29 +84,8 @@ namespace MDEvents
     declareProperty(new API::FileProperty("Filename","", API::FileProperty::Load,fileExtensions), "File of type txt");
     declareProperty(new WorkspaceProperty<IMDHistoWorkspace>("OutputWorkspace","",Direction::Output), "MDHistoWorkspace reflecting the input text file.");
 
-    auto validator = boost::make_shared<CompositeValidator>();
-    validator->add(boost::make_shared<BoundedValidator<int> >(1,9));
-    validator->add(boost::make_shared<MandatoryValidator<int> >());
-
-    declareProperty(new PropertyWithValue<int>("Dimensionality", -1, validator, Direction::Input), "Dimensionality of the data in the file.");
-
-    declareProperty(
-      new ArrayProperty<double>("Extents"),
-      "A comma separated list of min, max for each dimension,\n"
-      "specifying the extents of each dimension.");
-    
-    declareProperty(
-      new ArrayProperty<int>("NumberOfBins"), 
-      "Number of bin in each dimension.");
-
-    declareProperty(
-      new ArrayProperty<std::string>("Names"), 
-      "A comma separated list of the name of each dimension.");
-
-    declareProperty(
-      new ArrayProperty<std::string>("Units"), 
-      "A comma separated list of the units of each dimension.");
-
+    // Initialize generic dimension properties on the base class.
+    this->initGenericImportProps();
   }
 
   //----------------------------------------------------------------------------------------------
@@ -126,40 +93,13 @@ namespace MDEvents
    */
   void ImportMDHistoWorkspace::exec()
   {
-    // Fetch input properties
-    size_t ndims;
-    {
-      int ndims_int = getProperty("Dimensionality");
-      ndims = ndims_int;
-    }
-    std::vector<double> extents = getProperty("Extents");
-    std::vector<int> nbins = getProperty("NumberOfBins");
-    std::vector<std::string> names = getProperty("Names");
-    std::vector<std::string> units = getProperty("Units");
-    std::string filename = getProperty("Filename");
     
-    // Perform all validation on inputs
-    if (extents.size() != ndims*2)
-      throw std::invalid_argument("You must specify twice as many extents (min,max) as there are dimensions.");    
-    if (nbins.size() != ndims)
-      throw std::invalid_argument("You must specify as number of bins as there are dimensions.");
-    if (names.size() != ndims)
-      throw std::invalid_argument("You must specify as many names as there are dimensions.");
-    if (units.size() != ndims)
-      throw std::invalid_argument("You must specify as many units as there are dimensions.");
+    std::string filename = getProperty("Filename");
     
-    // Fabricate new dimensions from inputs
-    std::vector<MDHistoDimension_sptr> dimensions;
-    for(size_t k = 0; k < ndims; ++k)
-    {
-      dimensions.push_back(MDHistoDimension_sptr(new MDHistoDimension(names[k], names[k], units[k], static_cast<coord_t>(extents[k*2]), static_cast<coord_t>(extents[(k*2) + 1]), nbins[k])));
-    }
-   
     /*
-    Create a new output workspace. Note that the MDHistoWorkspace will take care of memory allocation associated with
-    the internal arrays for signal and error values. So no need to provide any clean-up in this algorithm if allocation is not possible.
+     Base class creates an empty output workspace, with the correct dimensionality according to the algorithm inputs (see base class).
     */
-    MDHistoWorkspace_sptr ws (new MDHistoWorkspace(dimensions));
+    MDHistoWorkspace_sptr ws = this->createEmptyOutputWorkspace();
 
     // Open the file
     std::ifstream file;
@@ -185,9 +125,7 @@ namespace MDEvents
     // Release the resource.
     file.close();
 
-    // Calculated the expected number of elements.
-    Product answer = std::for_each(nbins.begin(), nbins.end(), Product());
-    const size_t nElements = answer.result * 2;
+    const size_t nElements = this->getBinProduct() * 2;
 
     // Handle the case that the number of elements is wrong.
     if(box_elements.size() % nElements != 0)
diff --git a/Code/Mantid/Framework/MDEvents/src/ImportMDHistoWorkspaceBase.cpp b/Code/Mantid/Framework/MDEvents/src/ImportMDHistoWorkspaceBase.cpp
new file mode 100644
index 00000000000..1f3dd87871f
--- /dev/null
+++ b/Code/Mantid/Framework/MDEvents/src/ImportMDHistoWorkspaceBase.cpp
@@ -0,0 +1,121 @@
+/*WIKI*
+TODO: Enter a full wiki-markup description of your algorithm here. You can then use the Build/wiki_maker.py script to generate your full wiki page.
+*WIKI*/
+
+#include "MantidMDEvents/ImportMDHistoWorkspaceBase.h"
+#include "MantidMDEvents/MDHistoWorkspace.h"
+#include "MantidKernel/CompositeValidator.h"
+#include "MantidKernel/BoundedValidator.h"
+#include "MantidKernel/MandatoryValidator.h"
+#include "MantidKernel/ArrayProperty.h"
+#include <algorithm>
+
+using namespace Mantid::Kernel;
+using namespace Mantid::API;
+using namespace Mantid::Geometry;
+
+namespace Mantid
+{
+namespace MDEvents
+{ 
+
+    
+  /**
+  Functor to compute the product of the set.
+  */
+  struct Product : public std::unary_function<size_t, void>
+  {
+    Product() : result(1) {}
+    size_t result;
+    void operator()(size_t x) { result *= x; }
+  };
+
+
+  //----------------------------------------------------------------------------------------------
+  /** Constructor
+   */
+  ImportMDHistoWorkspaceBase::ImportMDHistoWorkspaceBase() : m_bin_product(0)
+  {
+  }
+
+  //----------------------------------------------------------------------------------------------
+  /** Destructor
+   */
+  ImportMDHistoWorkspaceBase::~ImportMDHistoWorkspaceBase()
+  {
+  }
+
+  //----------------------------------------------------------------------------------------------
+  /** Initalise generic importing properties.
+   */
+  void ImportMDHistoWorkspaceBase::initGenericImportProps()
+  {
+    auto validator = boost::make_shared<CompositeValidator>();
+    validator->add(boost::make_shared<BoundedValidator<int> >(1,9));
+    validator->add(boost::make_shared<MandatoryValidator<int> >());
+
+    declareProperty(new PropertyWithValue<int>("Dimensionality", -1, validator, Direction::Input), "Dimensionality of the data in the file.");
+
+    declareProperty(
+      new ArrayProperty<double>("Extents"),
+      "A comma separated list of min, max for each dimension,\n"
+      "specifying the extents of each dimension.");
+    
+    declareProperty(
+      new ArrayProperty<int>("NumberOfBins"), 
+      "Number of bin in each dimension.");
+
+    declareProperty(
+      new ArrayProperty<std::string>("Names"), 
+      "A comma separated list of the name of each dimension.");
+
+    declareProperty(
+      new ArrayProperty<std::string>("Units"), 
+      "A comma separated list of the units of each dimension.");
+  }
+
+  //----------------------------------------------------------------------------------------------
+  /** Create an empty output workspace from the generic properies. This gives a new workspace with dimensions provided, but signal and 
+  error arrays will not yet be set.
+  */
+  MDHistoWorkspace_sptr ImportMDHistoWorkspaceBase::createEmptyOutputWorkspace()
+  {
+        // Fetch input properties
+    size_t ndims;
+    {
+      int ndims_int = getProperty("Dimensionality");
+      ndims = ndims_int;
+    }
+    std::vector<double> extents = getProperty("Extents");
+    std::vector<int> nbins = getProperty("NumberOfBins");
+    std::vector<std::string> names = getProperty("Names");
+    std::vector<std::string> units = getProperty("Units");
+    
+    // Perform all validation on inputs
+    if (extents.size() != ndims*2)
+      throw std::invalid_argument("You must specify twice as many extents (min,max) as there are dimensions.");    
+    if (nbins.size() != ndims)
+      throw std::invalid_argument("You must specify as number of bins as there are dimensions.");
+    if (names.size() != ndims)
+      throw std::invalid_argument("You must specify as many names as there are dimensions.");
+    if (units.size() != ndims)
+      throw std::invalid_argument("You must specify as many units as there are dimensions.");
+    
+    // Fabricate new dimensions from inputs
+    std::vector<MDHistoDimension_sptr> dimensions;
+    for(size_t k = 0; k < ndims; ++k)
+    {
+      dimensions.push_back(MDHistoDimension_sptr(new MDHistoDimension(names[k], names[k], units[k], static_cast<coord_t>(extents[k*2]), static_cast<coord_t>(extents[(k*2) + 1]), nbins[k])));
+    }
+
+    // Calculate the total number of bins by multiplying across each dimension.
+    Product answer = std::for_each(nbins.begin(), nbins.end(), Product());
+    m_bin_product = answer.result;
+
+    MDHistoWorkspace_sptr ws (new MDHistoWorkspace(dimensions));
+    return ws;
+  }
+
+
+} // namespace Mantid
+} // namespace MDEvents
\ No newline at end of file
-- 
GitLab