Commit 7db86202 authored by Owen Arnold's avatar Owen Arnold
Browse files

refs #11056. Peak looks up detector after setQLab.

Refactor to use the ray tracing tools to look up the detector. Distance is optional, but I'd rather remove it altogether.
parent b0d5c230
......@@ -7,6 +7,7 @@
#include "MantidKernel/Matrix.h"
#include "MantidKernel/V3D.h"
#include "MantidKernel/PhysicalConstants.h"
#include <boost/optional.hpp>
namespace Mantid {
namespace API {
......@@ -52,7 +53,7 @@ public:
virtual void setQSampleFrame(Mantid::Kernel::V3D QSampleFrame,
double detectorDistance = 1.0) = 0;
virtual void setQLabFrame(Mantid::Kernel::V3D QLabFrame,
double detectorDistance = 1.0) = 0;
boost::optional<double> detectorDistance = boost::optional<double>()) = 0;
virtual void setWavelength(double wavelength) = 0;
virtual double getWavelength() const = 0;
......
......@@ -3,12 +3,15 @@
#include "MantidAPI/IPeak.h"
#include "MantidGeometry/Instrument.h"
#include "MantidKernel/Logger.h"
#include "MantidKernel/Matrix.h"
#include "MantidKernel/V3D.h"
#include "MantidKernel/PhysicalConstants.h"
#include "MantidKernel/System.h"
#include "MantidGeometry/Crystal/PeakShape.h"
#include <boost/shared_ptr.hpp>
#include <boost/optional.hpp>
namespace Mantid {
namespace DataObjects {
......@@ -85,7 +88,7 @@ public:
void setQSampleFrame(Mantid::Kernel::V3D QSampleFrame,
double detectorDistance = 1.0);
void setQLabFrame(Mantid::Kernel::V3D QLabFrame,
double detectorDistance = 1.0);
boost::optional<double> detectorDistance = boost::optional<double>());
void setWavelength(double wavelength);
double getWavelength() const;
......@@ -134,7 +137,7 @@ public:
private:
bool findDetector(const Mantid::Kernel::V3D &beam);
/// Shared pointer to the instrument (for calculating some values )
Geometry::Instrument_const_sptr m_inst;
......@@ -208,6 +211,9 @@ private:
/// Peak shape
Mantid::Geometry::PeakShape_const_sptr m_peakShape;
/// Static logger
static Mantid::Kernel::Logger g_log;
};
} // namespace Mantid
......
......@@ -484,11 +484,11 @@ void Peak::setQSampleFrame(Mantid::Kernel::V3D QSampleFrame,
* This is in inelastic convention: momentum transfer of the LATTICE!
* Also, q does have a 2pi factor = it is equal to 2pi/wavelength (in
*Angstroms).
* @param detectorDistance :: distance between the sample and the detector.
* Used to give a valid TOF. Default 1.0 meters.
* @param detectorDistance :: distance between the sample and the detector. If this is provided. Then we do not
* ray trace to find the intersecing detector.
*/
void Peak::setQLabFrame(Mantid::Kernel::V3D QLabFrame,
double detectorDistance) {
boost::optional<double> detectorDistance) {
// Clear out the detector = we can't know them
m_DetectorID = -1;
m_det = IDetector_sptr();
......@@ -535,11 +535,25 @@ void Peak::setQLabFrame(Mantid::Kernel::V3D QLabFrame,
this->setWavelength(wl);
V3D detectorDir = q * -1.0;
detectorDir[refFrame->pointingAlongBeam()] = one_over_wl - q.Z();
detectorDir[refFrame->pointingAlongBeam()] = one_over_wl - qBeam;
detectorDir.normalize();
// Use the given detector distance to find the detector position.
detPos = samplePos + detectorDir * detectorDistance;
if(detectorDistance.is_initialized())
{
detPos = samplePos + detectorDir * detectorDistance.get();
// We do not-update the detector as by manually setting the distance the client seems to know better.
}
else
{
// Find the detector
const bool found = findDetector(detectorDir);
if (!found)
{
// This is important, so we ought to log when this fails to happen.
g_log.debug("Could not find detector after setting qLab via setQLab with QLab : " + q.toString());
}
}
}
/** After creating a peak using the Q in the lab frame,
......@@ -552,12 +566,22 @@ void Peak::setQLabFrame(Mantid::Kernel::V3D QLabFrame,
* @return true if the detector ID was found.
*/
bool Peak::findDetector() {
bool found = false;
// Scattered beam direction
V3D oldDetPos = detPos;
V3D beam = detPos - samplePos;
beam.normalize();
return findDetector(beam);
}
/**
* @brief Peak::findDetector : Find the detector along the beam location. sets the detector, and detector position if found
* @param beam : detector direction from the sample as V3D
* @return True if a detector has been found
*/
bool Peak::findDetector(const Mantid::Kernel::V3D &beam) {
bool found = false;
// Create a ray tracer
InstrumentRayTracer tracker(m_inst);
tracker.traceFromSample(beam);
......@@ -904,5 +928,7 @@ Mantid::Kernel::V3D Peak::getDetectorPosition() const {
return getDetector()->getPos();
}
Mantid::Kernel::Logger Peak::g_log("PeakLogger");
} // namespace Mantid
} // namespace DataObjects
......@@ -329,7 +329,7 @@ public:
const double l1 = beam1.norm();
const double l2 = beam2.norm();
const double scatteringAngle2 = beam2.angle(beam1);
const V3D qLabDir = beam1 - beam2;
const V3D qLabDir = (beam1/l1) - (beam2/l2);
// Derive the wavelength
std::vector<double> x;
......@@ -427,7 +427,7 @@ public:
const double wavelength = 2;
Peak p(inst, detectorId, wavelength);
TSM_ASSERT_THROWS_NOTHING("Nothing wrong here, detector is valid", p.getDetectorPosition());
p.setQLabFrame(V3D(1,1,1), 1); // This sets the detector pointer to null and detector id to -1;
p.setQLabFrame(V3D(1,1,1), 1.0); // This sets the detector pointer to null and detector id to -1;
TSM_ASSERT_THROWS("Detector is not valid", p.getDetectorPosition(), Mantid::Kernel::Exception::NullPointerException&);
}
......
......@@ -582,18 +582,21 @@ Instrument_sptr createMinimalInstrument(const V3D& sourcePos, const V3D& sampleP
// A source
ObjComponent *source = new ObjComponent("source");
source->setPos(sourcePos);
source->setShape(createSphere(0.01 /*1cm*/, V3D(0,0,0), "1"));
instrument->add(source);
instrument->markAsSource(source);
// A sample
ObjComponent *sample = new ObjComponent("some-surface-holder");
source->setPos(samplePos);
sample->setPos(samplePos);
sample->setShape(createSphere(0.01 /*1cm*/, V3D(0,0,0), "1"));
instrument->add(sample);
instrument->markAsSamplePos(sample);
// A detector
Detector *det = new Detector("point-detector", 1 /*detector id*/, NULL);
det->setPos(detectorPos);
det->setShape(createSphere(0.01 /*1cm*/, V3D(0,0,0), "1"));
instrument->add(det);
instrument->markAsDetector(det);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment