From ff4b43d801ef1e671cae6f734d6a4010889edbc2 Mon Sep 17 00:00:00 2001
From: Vickie Lynch <lynchve@ornl.gov>
Date: Mon, 6 Oct 2014 16:27:41 -0400
Subject: [PATCH] Refs #10254 tube gap in instrument parameter files

---
 .../Mantid/Framework/DataObjects/src/Peak.cpp | 129 +++++++++---------
 .../MDAlgorithms/src/IntegratePeaksMD2.cpp    |   3 +-
 .../plugins/algorithms/MaskBTP.py             |   8 +-
 .../src/AlignAndFocusPowder.cpp               |   3 +-
 Code/Mantid/instrument/CORELLI_Parameters.xml |  14 ++
 Code/Mantid/instrument/WISH_Parameters.xml    |   5 +
 6 files changed, 93 insertions(+), 69 deletions(-)
 create mode 100644 Code/Mantid/instrument/CORELLI_Parameters.xml

diff --git a/Code/Mantid/Framework/DataObjects/src/Peak.cpp b/Code/Mantid/Framework/DataObjects/src/Peak.cpp
index cbafd4067c4..5cf0ed7394e 100644
--- a/Code/Mantid/Framework/DataObjects/src/Peak.cpp
+++ b/Code/Mantid/Framework/DataObjects/src/Peak.cpp
@@ -528,69 +528,72 @@ namespace DataObjects
    */
   bool Peak::findDetector()
   {
-    // Scattered beam direction
-    V3D oldDetPos = detPos;
-    V3D beam = detPos - samplePos;
-    beam.normalize();
-
-    // Create a ray tracer
-    InstrumentRayTracer tracker( m_inst );
-    tracker.traceFromSample(beam);
-    IDetector_const_sptr det = tracker.getDetectorResult();
-    if (det)
-    {
-      // Set the detector ID, the row, col, etc.
-      this->setDetectorID(det->getID());
-      // The old detector position is not more precise if it comes from FindPeaksMD
-      detPos = det->getPos();
-      return true;
-    }
-    else  //fix for gaps between tubes
-    {
-    	double gap = 0.00065;
-    	V3D beam1 = beam + V3D(gap,0.,0.);
-        tracker.traceFromSample(beam1);
-        IDetector_const_sptr det1 = tracker.getDetectorResult();
-    	V3D beam2 = beam + V3D(-gap,0.,0.);
-        tracker.traceFromSample(beam2);
-        IDetector_const_sptr det2 = tracker.getDetectorResult();
-        if (det1 && det2)
-         {
-           // Set the detector ID, the row, col, etc.
-           this->setDetectorID(static_cast<int>((det1->getID()+det2->getID())*0.5));;
-           detPos = (det1->getPos() + det2->getPos())*0.5;
-           std::cout <<static_cast<int>((det1->getID()+det2->getID())*0.5) << " x gap\n";
-           return true;
-         }
-    	beam1 = beam + V3D(0.,gap,0.);
-        tracker.traceFromSample(beam1);
-        det1 = tracker.getDetectorResult();
-    	beam2 = beam + V3D(0.,-gap,0.);
-        tracker.traceFromSample(beam2);
-        det2 = tracker.getDetectorResult();
-        if (det1 && det2)
-         {
-           // Set the detector ID, the row, col, etc.
-           this->setDetectorID(static_cast<int>((det1->getID()+det2->getID())*0.5));;
-           detPos = (det1->getPos() + det2->getPos())*0.5;
-           std::cout <<static_cast<int>((det1->getID()+det2->getID())*0.5) << " y gap\n";
-           return true;
-         }
-    	beam1 = beam + V3D(0.,0.,gap);
-        tracker.traceFromSample(beam1);
-        det1 = tracker.getDetectorResult();
-    	beam2 = beam + V3D(0.,0.,-gap);
-        tracker.traceFromSample(beam2);
-        det2 = tracker.getDetectorResult();
-        if (det1 && det2)
-         {
-           // Set the detector ID, the row, col, etc.
-           this->setDetectorID(static_cast<int>((det1->getID()+det2->getID())*0.5));;
-           detPos = (det1->getPos() + det2->getPos())*0.5;
-           std::cout <<static_cast<int>((det1->getID()+det2->getID())*0.5) << " z gap\n";
-           return true;
-         }
-    }
+	// Scattered beam direction
+	V3D oldDetPos = detPos;
+	V3D beam = detPos - samplePos;
+	beam.normalize();
+
+	// Create a ray tracer
+	InstrumentRayTracer tracker( m_inst );
+	tracker.traceFromSample(beam);
+	IDetector_const_sptr det = tracker.getDetectorResult();
+	if (det)
+	{
+		// Set the detector ID, the row, col, etc.
+		this->setDetectorID(det->getID());
+		// The old detector position is not more precise if it comes from FindPeaksMD
+		detPos = det->getPos();
+		return true;
+	}
+	//fix for gaps between tubes
+	else if (m_inst->hasParameter("tube-gap"))
+	{
+		std::vector<double> gaps = m_inst->getNumberParameter("tube-gap", true);
+		if (gaps.empty()) return false;
+		const double gap = static_cast<double>(gaps.front());
+		V3D beam1 = beam + V3D(0.,0.,gap);
+		tracker.traceFromSample(beam1);
+		IDetector_const_sptr det1 = tracker.getDetectorResult();
+		V3D beam2 = beam + V3D(0.,0.,-gap);
+		tracker.traceFromSample(beam2);
+		IDetector_const_sptr det2 = tracker.getDetectorResult();
+		if (det1 && det2)
+		{
+			// Set the detector ID, the row, col, etc.
+			this->setDetectorID(static_cast<int>((det1->getID()+det2->getID())*0.5));;
+			detPos = (det1->getPos() + det2->getPos())*0.5;
+			std::cout <<gap<<"  "<<static_cast<int>((det1->getID()+det2->getID())*0.5) << " z gap\n";
+			return true;
+		}
+		beam1 = beam + V3D(gap,0.,0.);
+		tracker.traceFromSample(beam1);
+		det1 = tracker.getDetectorResult();
+		beam2 = beam + V3D(-gap,0.,0.);
+		tracker.traceFromSample(beam2);
+		det2 = tracker.getDetectorResult();
+		if (det1 && det2)
+		{
+			// Set the detector ID, the row, col, etc.
+			this->setDetectorID(static_cast<int>((det1->getID()+det2->getID())*0.5));;
+			detPos = (det1->getPos() + det2->getPos())*0.5;
+			std::cout <<gap<<"  "<<static_cast<int>((det1->getID()+det2->getID())*0.5) << " x gap\n";
+			return true;
+		}
+		beam1 = beam + V3D(0.,gap,0.);
+		tracker.traceFromSample(beam1);
+		det1 = tracker.getDetectorResult();
+		beam2 = beam + V3D(0.,-gap,0.);
+		tracker.traceFromSample(beam2);
+		det2 = tracker.getDetectorResult();
+		if (det1 && det2)
+		{
+			// Set the detector ID, the row, col, etc.
+			this->setDetectorID(static_cast<int>((det1->getID()+det2->getID())*0.5));;
+			detPos = (det1->getPos() + det2->getPos())*0.5;
+			std::cout <<gap<<"  "<<static_cast<int>((det1->getID()+det2->getID())*0.5) << " y gap\n";
+			return true;
+		}
+	}
     return false;
   }
 
diff --git a/Code/Mantid/Framework/MDAlgorithms/src/IntegratePeaksMD2.cpp b/Code/Mantid/Framework/MDAlgorithms/src/IntegratePeaksMD2.cpp
index 11bee7adaf1..e778e8ab44d 100644
--- a/Code/Mantid/Framework/MDAlgorithms/src/IntegratePeaksMD2.cpp
+++ b/Code/Mantid/Framework/MDAlgorithms/src/IntegratePeaksMD2.cpp
@@ -587,7 +587,6 @@ namespace MDAlgorithms
    */
   bool IntegratePeaksMD2::detectorQ(Mantid::Kernel::V3D QLabFrame, double r)
   {
-		double kmax=100.;
 		std::vector<detid_t> detectorIDs = inst->getDetectorIDs();
 
 		for (auto detID = detectorIDs.begin(); detID != detectorIDs.end(); ++detID)
@@ -597,7 +596,7 @@ namespace MDAlgorithms
 				if( !det->isMasked() ) continue;// edge is masked so don't check if not masked
 				double tt1=det->getTwoTheta(V3D(0,0,0),V3D(0,0,1)); //two theta
 				double ph1=det->getPhi(); //phi
-				V3D E1=V3D(-kmax*std::sin(tt1)*std::cos(ph1),-kmax*std::sin(tt1)*std::sin(ph1),kmax-kmax*std::cos(tt1)); //end of trajectory
+				V3D E1=V3D(-std::sin(tt1)*std::cos(ph1),-std::sin(tt1)*std::sin(ph1),1.-std::cos(tt1)); //end of trajectory
 				E1=E1*(1./E1.norm()); //normalize
 				V3D distv=QLabFrame-E1*(QLabFrame.scalar_prod(E1)); //distance to the trajectory as a vector
 				if(distv.norm()<r)
diff --git a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/MaskBTP.py b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/MaskBTP.py
index 8dea47ab8c6..17921a34243 100644
--- a/Code/Mantid/Framework/PythonInterface/plugins/algorithms/MaskBTP.py
+++ b/Code/Mantid/Framework/PythonInterface/plugins/algorithms/MaskBTP.py
@@ -49,7 +49,7 @@ class MaskBTP(mantid.api.PythonAlgorithm):
 
         instrumentList=["ARCS","CNCS","CORELLI","HYSPEC","NOMAD","POWGEN","SEQUOIA","SNAP","TOPAZ","WISH"]
         self.bankmin={"ARCS":1,"CNCS":1,"CORELLI":1,"HYSPEC":1,"NOMAD":1,"POWGEN":1,"SEQUOIA":38,"SNAP":1,"TOPAZ":10,"WISH":1}
-        self.bankmax={"ARCS":115,"CNCS":50,"CORELLI":91,"HYSPEC":20,"NOMAD":99,"POWGEN":300,"SEQUOIA":150,"SNAP":18,"TOPAZ":59,"WISH":5}
+        self.bankmax={"ARCS":115,"CNCS":50,"CORELLI":91,"HYSPEC":20,"NOMAD":99,"POWGEN":300,"SEQUOIA":150,"SNAP":18,"TOPAZ":59,"WISH":10}
         tubemin={"ARCS":1,"CNCS":1,"CORELLI":1,"HYSPEC":1,"NOMAD":1,"POWGEN":0,"SEQUOIA":1,"SNAP":0,"TOPAZ":0,"WISH":1}
         tubemax={"ARCS":8,"CNCS":8,"CORELLI":16,"HYSPEC":8,"NOMAD":8,"POWGEN":153,"SEQUOIA":8,"SNAP":255,"TOPAZ":255,"WISH":152}
         pixmin={"ARCS":1,"CNCS":1,"CORELLI":1,"HYSPEC":1,"NOMAD":1,"POWGEN":0,"SEQUOIA":1,"SNAP":0,"TOPAZ":0,"WISH":1}
@@ -175,8 +175,10 @@ class MaskBTP(mantid.api.PythonAlgorithm):
                 raise ValueError("Out of range index for "+str(self.instname)+" instrument bank numbers")
         elif self.instname=="WISH":
             if (self.bankmin[self.instname]<=banknum<= self.bankmax[self.instname]):
-                print "panel"+"%02d" % banknum
-                return self.instrument.getComponentByName("panel"+"%02d" % banknum)[0]
+                try:
+                	return self.instrument.getComponentByName("panel"+"%02d" % banknum)[0]
+                except:
+                        return None
             else:
                 raise ValueError("Out of range index for "+str(self.instname)+" instrument bank numbers")
         else:
diff --git a/Code/Mantid/Framework/WorkflowAlgorithms/src/AlignAndFocusPowder.cpp b/Code/Mantid/Framework/WorkflowAlgorithms/src/AlignAndFocusPowder.cpp
index 3c89ed9415b..e648af6e75d 100644
--- a/Code/Mantid/Framework/WorkflowAlgorithms/src/AlignAndFocusPowder.cpp
+++ b/Code/Mantid/Framework/WorkflowAlgorithms/src/AlignAndFocusPowder.cpp
@@ -481,7 +481,8 @@ namespace WorkflowAlgorithms
       maskAlg->setProperty("Workspace", m_outputW);
       maskAlg->setProperty("MaskedWorkspace", m_maskWS);
       maskAlg->executeAsChildAlg();
-      m_outputW = maskAlg->getProperty("Workspace");
+      Workspace_sptr tmpW = maskAlg->getProperty("Workspace");
+      m_outputW =boost::dynamic_pointer_cast<MatrixWorkspace>(tmpW);
     }
     m_progress->report();
 
diff --git a/Code/Mantid/instrument/CORELLI_Parameters.xml b/Code/Mantid/instrument/CORELLI_Parameters.xml
new file mode 100644
index 00000000000..ddf9701b1f4
--- /dev/null
+++ b/Code/Mantid/instrument/CORELLI_Parameters.xml
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<parameter-file instrument = "CORELLI" valid-from = "2014-02-25 00:00:00">
+
+  <component-link name = "CORELLI">
+
+    <!-- Specify the gap between the tubes for Peak::findDetector -->
+    <parameter name="tube-gap">
+      <value val="0.00065"/>
+    </parameter>
+
+  </component-link>
+
+</parameter-file>
+
diff --git a/Code/Mantid/instrument/WISH_Parameters.xml b/Code/Mantid/instrument/WISH_Parameters.xml
index 09c45dbaae2..b14a5057845 100644
--- a/Code/Mantid/instrument/WISH_Parameters.xml
+++ b/Code/Mantid/instrument/WISH_Parameters.xml
@@ -23,5 +23,10 @@
           <value val="Always" />
          </parameter>
 		
+         <!-- Specify the gap between the tubes for Peak::findDetector -->
+         <parameter name="tube-gap">
+           <value val="0.0003"/>
+         </parameter>
+
     </component-link>
 </parameter-file>    
-- 
GitLab