From 47734c5a7d23b57f6e63008c934de802d61e3fd3 Mon Sep 17 00:00:00 2001 From: Owen Arnold Date: Tue, 24 Feb 2015 17:27:22 +0000 Subject: [PATCH] refs #11056. Create test data. This is extremely hard to do, which is presumably why this algorithm never had any unit tests in the first place. --- Code/Mantid/Framework/MDEvents/CMakeLists.txt | 1 + .../test/BoxControllerSettingsAlgorithmTest.h | 2 +- .../MDEvents/test/IntegrateEllipsoidsTest.h | 124 ++++++++++++++++++ .../ComponentCreationHelper.h | 2 +- .../src/ComponentCreationHelper.cpp | 7 +- 5 files changed, 131 insertions(+), 5 deletions(-) create mode 100644 Code/Mantid/Framework/MDEvents/test/IntegrateEllipsoidsTest.h diff --git a/Code/Mantid/Framework/MDEvents/CMakeLists.txt b/Code/Mantid/Framework/MDEvents/CMakeLists.txt index 14f65d1e4e18..7ef89d372bb7 100644 --- a/Code/Mantid/Framework/MDEvents/CMakeLists.txt +++ b/Code/Mantid/Framework/MDEvents/CMakeLists.txt @@ -135,6 +135,7 @@ set ( TEST_FILES CoordTransformDistanceParserTest.h CoordTransformDistanceTest.h FitMDTest.h + IntegrateEllipsoidsTest.h ImportMDEventWorkspaceTest.h ImportMDHistoWorkspaceTest.h MDBinTest.h diff --git a/Code/Mantid/Framework/MDEvents/test/BoxControllerSettingsAlgorithmTest.h b/Code/Mantid/Framework/MDEvents/test/BoxControllerSettingsAlgorithmTest.h index b54466c963a5..3c2ab44f9f93 100644 --- a/Code/Mantid/Framework/MDEvents/test/BoxControllerSettingsAlgorithmTest.h +++ b/Code/Mantid/Framework/MDEvents/test/BoxControllerSettingsAlgorithmTest.h @@ -62,7 +62,7 @@ class BoxControllerSettingsAlgorithmTest : public CxxTest::TestSuite { auto ws = boost::make_shared(); ws->initialize(1, 2, 1); - ws->setInstrument(ComponentCreationHelper::createTestInstrumentRectangular(6, 1, 0)); + ws->setInstrument(ComponentCreationHelper::createTestInstrumentRectangular(6, 1, 0.0)); const std::string instrumentName = ws->getInstrument()->getName(); // Create a parameter file, with a root equation that will apply to all detectors. diff --git a/Code/Mantid/Framework/MDEvents/test/IntegrateEllipsoidsTest.h b/Code/Mantid/Framework/MDEvents/test/IntegrateEllipsoidsTest.h new file mode 100644 index 000000000000..0c4fb341d5ff --- /dev/null +++ b/Code/Mantid/Framework/MDEvents/test/IntegrateEllipsoidsTest.h @@ -0,0 +1,124 @@ +#include +#include "MantidMDEvents/IntegrateEllipsoids.h" +#include "MantidTestHelpers/ComponentCreationHelper.h" +#include "MantidDataObjects/PeaksWorkspace.h" +#include "MantidDataObjects/EventWorkspace.h" +#include "MantidGeometry/Crystal/OrientedLattice.h" +#include "MantidGeometry/Instrument/NearestNeighbours.h" +#include +#include + +using namespace Mantid; +using namespace Mantid::MDEvents; +using namespace Mantid::Kernel; +using namespace Mantid::Geometry; +using namespace Mantid::DataObjects; + +class IntegrateEllipsoidsTest : public CxxTest::TestSuite { +public: + // This pair of boilerplate methods prevent the suite being created statically + // This means the constructor isn't called when running other tests + static IntegrateEllipsoidsTest *createSuite() { + return new IntegrateEllipsoidsTest(); + } + static void destroySuite(IntegrateEllipsoidsTest *suite) { delete suite; } + + static ISpectrumDetectorMapping buildSpectrumDetectorMapping(const specid_t start, const specid_t end) + { + boost::unordered_map> map; + for ( specid_t i = start; i <= end; ++i ) + { + map[i].insert(i); + } + return map; + } + + void addFakeEllipsoid(const int& detectorId, const int& totalNPixels, const double tofExact, const int& nEvents, const NearestNeighbours& nn, EventWorkspace_sptr& eventWS) + { + EventList& el = eventWS->getOrAddEventList(detectorId - totalNPixels); + el.setDetectorID(detectorId); + el.addEventQuickly(TofEvent(tofExact)); + + // Find some neighbours, + std::map neighbourMap = nn.neighbours(detectorId); + + typedef std::map NeighbourMap; + typedef NeighbourMap::iterator NeighbourMapIterator; + for(NeighbourMapIterator it = neighbourMap.begin(); it != neighbourMap.end(); ++it) + { + const specid_t neighbourDet = (*it).first; + const double distanceFromCentre = (*it).second.norm2(); // gives TOF delta + EventList neighbourEventList = eventWS->getOrAddEventList(neighbourDet - totalNPixels); + neighbourEventList.setDetectorID(neighbourDet); + for(int i = 0; i < nEvents; ++i) { + + const double tof = (tofExact - (distanceFromCentre/2) ) + ( distanceFromCentre * double(i)/double(nEvents) ); + neighbourEventList.addEventQuickly(TofEvent(tof)); + } + } + + + } + + boost::tuple createDiffractionData() + { + const int nPixels = 100; + Mantid::Geometry::Instrument_sptr inst = + ComponentCreationHelper::createTestInstrumentRectangular(1 /*num_banks*/, nPixels /*pixels in each direction yields n by n*/,0.01, 1.0); + + // Create a peaks workspace + auto peaksWS = boost::make_shared(); + // Set the instrument to be the fake rectangular bank above. + peaksWS->setInstrument(inst); + // Set the oriented lattice for a cubic crystal + OrientedLattice ol(6,6,6,90,90,90); + ol.setUFromVectors(V3D(6, 0, 0), V3D(0, 6, 0)); + peaksWS->mutableSample().setOrientedLattice(&ol); + // Add some peaks which should correspond to real reflections (could calculate these) + Peak* a = peaksWS->createPeakHKL(V3D(1, -5, -3)); + Peak* b = peaksWS->createPeakHKL(V3D(1, -4, -4)); + Peak* c = peaksWS->createPeakHKL(V3D(1, -3, -5)); + peaksWS->addPeak(*c); + peaksWS->addPeak(*a); + peaksWS->addPeak(*b); + // Make an event workspace and add fake peak data + auto eventWS = boost::make_shared(); + eventWS->setInstrument(inst); + eventWS->initialize(nPixels * nPixels /*n spectra*/, 3 /* x-size */, 3 /* y-size */); + + // Make a nn map, so that we can add counts in the vicinity of the actual peak centre. + const int nPixelsTotal = nPixels*nPixels; + NearestNeighbours nn(inst, buildSpectrumDetectorMapping(nPixelsTotal, nPixelsTotal+inst->getNumberDetectors() - 1)); + + // Add a fake ellipsoid + addFakeEllipsoid(c->getDetectorID(), nPixelsTotal, c->getTOF(), 10, nn, eventWS); + addFakeEllipsoid(a->getDetectorID(), nPixelsTotal, a->getTOF(), 10, nn, eventWS); + addFakeEllipsoid(b->getDetectorID(), nPixelsTotal, b->getTOF(), 10, nn, eventWS); + + // Clean up + delete a; + delete b; + delete c; + + // Return test data. + return boost::tuple(eventWS, peaksWS); + } + + void test_init() + { + Mantid::MDEvents::IntegrateEllipsoids alg; + TS_ASSERT_THROWS_NOTHING(alg.initialize()); + } + + void test_execution() + { + auto out = createDiffractionData(); + } + + + + + + + +}; diff --git a/Code/Mantid/Framework/TestHelpers/inc/MantidTestHelpers/ComponentCreationHelper.h b/Code/Mantid/Framework/TestHelpers/inc/MantidTestHelpers/ComponentCreationHelper.h index 073fe49bd53c..304a1ff5b6b6 100644 --- a/Code/Mantid/Framework/TestHelpers/inc/MantidTestHelpers/ComponentCreationHelper.h +++ b/Code/Mantid/Framework/TestHelpers/inc/MantidTestHelpers/ComponentCreationHelper.h @@ -145,7 +145,7 @@ createTestInstrumentCylindrical(int num_banks, bool verbose = false, /// pixels*pixels in size, a source and spherical sample shape. Mantid::Geometry::Instrument_sptr createTestInstrumentRectangular(int num_banks, int pixels, - double pixelSpacing = 0.008); + double pixelSpacing = 0.008, double bankDistanceFromSample= 5.0); Mantid::Geometry::Instrument_sptr createTestInstrumentRectangular2(int num_banks, int pixels, diff --git a/Code/Mantid/Framework/TestHelpers/src/ComponentCreationHelper.cpp b/Code/Mantid/Framework/TestHelpers/src/ComponentCreationHelper.cpp index 0618e788d5aa..c802d5585022 100644 --- a/Code/Mantid/Framework/TestHelpers/src/ComponentCreationHelper.cpp +++ b/Code/Mantid/Framework/TestHelpers/src/ComponentCreationHelper.cpp @@ -439,9 +439,10 @@ createCylInstrumentWithDetInGivenPosisions(const std::vector &L2, * @param num_banks :: number of rectangular banks to create * @param pixels :: number of pixels in each direction. * @param pixelSpacing :: padding between pixels + * @param bankDistanceFromSample :: How far the bank is from the sample */ Instrument_sptr createTestInstrumentRectangular(int num_banks, int pixels, - double pixelSpacing) { + double pixelSpacing, double bankDistanceFromSample) { boost::shared_ptr testInst(new Instrument("basic_rect")); const double cylRadius(pixelSpacing / 2); @@ -470,12 +471,12 @@ Instrument_sptr createTestInstrumentRectangular(int num_banks, int pixels, } testInst->add(bank); - bank->setPos(V3D(0.0, 0.0, 5.0 * banknum)); + bank->setPos(V3D(0.0, 0.0, bankDistanceFromSample * banknum)); } // Define a source component ObjComponent *source = - new ObjComponent("moderator", Object_sptr(new Object), testInst.get()); + new ObjComponent("source", createSphere(0.01 /*1cm*/, V3D(0,0,0), "1"), testInst.get()); source->setPos(V3D(0.0, 0.0, -10.)); testInst->add(source); testInst->markAsSource(source);