Skip to content

Commit

Permalink
refs #7386. Safe detector position method.
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenArnold committed Jul 3, 2013
1 parent 399f9b7 commit bf7c1cd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/IPeak.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ namespace API
virtual void setHKL(double H, double K, double L) = 0;
virtual void setHKL(Mantid::Kernel::V3D HKL) = 0;
virtual Mantid::Kernel::V3D getDetectorPosition() const = 0;
virtual Mantid::Kernel::V3D getDetectorPositionSafe() const = 0;

virtual Mantid::Kernel::V3D getQLabFrame() const = 0;
virtual Mantid::Kernel::V3D getQSampleFrame() const = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ namespace DataObjects
Mantid::Kernel::V3D getQLabFrame() const;
Mantid::Kernel::V3D getQSampleFrame() const;
Mantid::Kernel::V3D getDetectorPosition() const;
Mantid::Kernel::V3D getDetectorPositionSafe() const;

void setQSampleFrame(Mantid::Kernel::V3D QSampleFrame, double detectorDistance=1.0);
void setQLabFrame(Mantid::Kernel::V3D QLabFrame, double detectorDistance=1.0);
Expand Down
16 changes: 16 additions & 0 deletions Code/Mantid/Framework/DataObjects/src/Peak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,22 @@ namespace DataObjects
return getDetector()->getPos();
}

/**
Forwarding function. Exposes the detector position directly, but checks that the detector is not null before
accessing its position. Throws if null.
*/
Mantid::Kernel::V3D Peak::getDetectorPositionSafe() const
{
auto det = getDetector();
if(det == NULL)
{
std::stringstream stream;
stream << "Peak at row " << this->m_Row;
throw Mantid::Kernel::Exception::NotFoundError("Detector cannot be found.", stream.str());
}
return getDetector()->getPos();
}



} // namespace Mantid
Expand Down
22 changes: 22 additions & 0 deletions Code/Mantid/Framework/DataObjects/test/PeakTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,28 @@ class PeakTest : public CxxTest::TestSuite
TS_ASSERT_EQUALS( p2.getDetectorID(), 19999);
}

void test_getDetectorPositionSafe()
{
const int detectorId = 19999;
const double wavelength = 2;
Peak p(inst, detectorId, wavelength);

V3D a = p.getDetectorPosition();
V3D b = p.getDetectorPositionSafe();

TSM_ASSERT_EQUALS("Results should be the same", a, b);
}

void test_getDetectorPositionThrows()
{
const int detectorId = 19999;
const double wavelength = 2;
Peak p(inst, detectorId, wavelength);
TSM_ASSERT_THROWS_NOTHING("Nothing wrong here, detector is valid", p.getDetectorPositionSafe());
p.setQLabFrame(V3D(1,1,1), 1); // This sets the detector pointer to null and detector id to -1;
TSM_ASSERT_THROWS("Detector is not valid", p.getDetectorPositionSafe(), Mantid::Kernel::Exception::NotFoundError&);
}

private:
void check_Contributing_Detectors(const Peak & peak, const std::vector<int> & expected)
{
Expand Down

0 comments on commit bf7c1cd

Please sign in to comment.