From 1b60e1dc631c3c4d6b4788f9d4df5d4f93a27f5d Mon Sep 17 00:00:00 2001
From: Janik Zikovsky <zikovskyjl@ornl.gov>
Date: Mon, 1 Nov 2010 17:04:46 +0000
Subject: [PATCH] Refs #1716: SumNeighbours handles one level of grouping for
 RectangularDetectors (for PG3 instrument and others, maybe). Am having issues
 in the instrument view, but I think it is unrelated.

---
 Code/Mantid/Algorithms/src/SumNeighbours.cpp  | 50 +++++++++++++++----
 .../Algorithms/test/SumNeighboursTest.h       | 46 ++++++++++-------
 2 files changed, 67 insertions(+), 29 deletions(-)

diff --git a/Code/Mantid/Algorithms/src/SumNeighbours.cpp b/Code/Mantid/Algorithms/src/SumNeighbours.cpp
index 14ade0d1b34..0a4d61f86f1 100644
--- a/Code/Mantid/Algorithms/src/SumNeighbours.cpp
+++ b/Code/Mantid/Algorithms/src/SumNeighbours.cpp
@@ -5,6 +5,7 @@
 #include "MantidDataObjects/EventWorkspace.h"
 #include "MantidDataObjects/EventList.h"
 #include "MantidGeometry/IComponent.h"
+#include "MantidGeometry/ICompAssembly.h"
 #include "MantidGeometry/Instrument/RectangularDetector.h"
 #include "MantidAPI/WorkspaceValidators.h"
 #include "MantidAPI/SpectraDetectorMap.h"
@@ -46,7 +47,7 @@ void SumNeighbours::init()
   declareProperty("SumY", 4, mustBePositive->clone(),
     "The number of Y (vertical) pixels to sum together. This must evenly divide the number of Y pixels in a detector" );
 
-  declareProperty("DetectorNames", "", "Comma-separated list of the names of the detectors in the Mantid geometry file." );
+  //declareProperty("DetectorNames", "", "Comma-separated list of the names of the detectors in the Mantid geometry file." );
 
 
 }
@@ -87,10 +88,10 @@ void SumNeighbours::exec()
   matrixOutputWS = boost::dynamic_pointer_cast<MatrixWorkspace>(outWS);
   this->setProperty("OutputWorkspace", matrixOutputWS);
 
-  //Split the detector names string.
-  std::vector<std::string> det_names;
-  std::string det_name_list = getPropertyValue("DetectorNames");
-  boost::split(det_names, det_name_list, boost::is_any_of(", "));
+//  //Split the detector names string.
+//  std::vector<std::string> det_names;
+//  std::string det_name_list = getPropertyValue("DetectorNames");
+//  boost::split(det_names, det_name_list, boost::is_any_of(", "));
 
   //To get the workspace index from the detector ID
   IndexToIndexMap * pixel_to_wi = inWS->getDetectorIDToWorkspaceIndexMap(true);
@@ -99,16 +100,45 @@ void SumNeighbours::exec()
   //std::cout << " inst->nelements() " << inst->nelements() << "\n";
   Progress prog(this,0.0,1.0,inst->nelements());
 
-  //Loop through all the elements in the instrument, looking for RectangularDetector's
+  //Build a list of Rectangular Detectors
+  std::vector<boost::shared_ptr<IRectangularDetector> > detList;
   for (int i=0; i < inst->nelements(); i++)
   {
-    std::string det_name("");
-    boost::shared_ptr<RectangularDetector> det;
-    int x, y;
+    boost::shared_ptr<IRectangularDetector> det;
+    boost::shared_ptr<ICompAssembly> assem;
+
+    det = boost::dynamic_pointer_cast<IRectangularDetector>( (*inst)[i] );
+    if (det)
+      detList.push_back(det);
+    else
+    {
+      //Also, look in the first sub-level for RectangularDetectors (e.g. PG3).
+      // We are not doing a full recursive search since that will be very long for lots of pixels.
+      assem = boost::dynamic_pointer_cast<ICompAssembly>( (*inst)[i] );
+      if (assem)
+      {
+        for (int j=0; j < assem->nelements(); j++)
+        {
+          det = boost::dynamic_pointer_cast<IRectangularDetector>( (*assem)[j] );
+          if (det) detList.push_back(det);
+        }
+      }
 
-    det = boost::dynamic_pointer_cast<RectangularDetector>( (*inst)[i] );
+    }
+  }
+
+  if (detList.size() == 0)
+    throw std::runtime_error("This instrument does not have any RectangularDetector's. SumNeighbors cannot operate on this instrument at this time.");
+
+  //Loop through the RectangularDetector's we listed before.
+  for (int i=0; i < static_cast<int>(detList.size()); i++)
+  {
+    std::string det_name("");
+    boost::shared_ptr<IRectangularDetector> det;
+    det = detList[i];
     if (det)
     {
+      int x, y;
       det_name = det->getName();
       //TODO: Check validity of the parameters
 
diff --git a/Code/Mantid/Algorithms/test/SumNeighboursTest.h b/Code/Mantid/Algorithms/test/SumNeighboursTest.h
index 68da7e0c042..83eca8c4cb9 100644
--- a/Code/Mantid/Algorithms/test/SumNeighboursTest.h
+++ b/Code/Mantid/Algorithms/test/SumNeighboursTest.h
@@ -20,8 +20,8 @@ class SumNeighboursTest : public CxxTest::TestSuite
 {
 public:
 
-//  SumNeighboursTest()
-//  {
+  SumNeighboursTest()
+  {
 //    outputSpace1 = "SNAP_sum";
 //    inputSpace = "SNAP";
 //
@@ -31,23 +31,31 @@ public:
 //    loader.setPropertyValue("Filename","/home/janik/data/SNAP_4105_event.nxs");
 //    loader.setPropertyValue("OutputWorkspace",inputSpace);
 //    loader.execute();
-//
-//  }
-//
-//  ~SumNeighboursTest()
-//  {}
-//
-//  void xtestBadInputs()
-//  {
-//    alg.initialize();
-//    TS_ASSERT( alg.isInitialized() );
-//    alg.setPropertyValue("InputWorkspace",inputSpace) ;
-//    alg.setPropertyValue("OutputWorkspace",outputSpace1) ;
-//    alg.setPropertyValue("SumX","12"); //! This is not valid :)
-//    alg.setPropertyValue("SumY","16");
-//    alg.execute();
-//    TS_ASSERT( !alg.isExecuted());
-//  }
+
+  }
+
+  ~SumNeighboursTest()
+  {}
+
+
+
+  void testTheBasics()
+  {
+    TS_ASSERT_EQUALS( alg.name(), "SumNeighbours" );
+    TS_ASSERT_EQUALS( alg.version(), 1 );
+    TS_ASSERT_EQUALS( alg.category(), "General" );
+  }
+
+  void testInit()
+  {
+    TS_ASSERT_THROWS_NOTHING( alg.initialize() );
+    TS_ASSERT( alg.isInitialized() );
+
+    std::vector<Property*> props = alg.getProperties();
+    TS_ASSERT_EQUALS( static_cast<int>(props.size()), 4 );
+  }
+
+
 //
 //
 //  void testExec_SNAP()
-- 
GitLab