Skip to content

Commit

Permalink
refs #11056. Create test data.
Browse files Browse the repository at this point in the history
This is extremely hard to do, which is presumably why this algorithm never had any unit tests in the first place.
  • Loading branch information
OwenArnold committed Feb 24, 2015
1 parent f42f48d commit 47734c5
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 5 deletions.
1 change: 1 addition & 0 deletions Code/Mantid/Framework/MDEvents/CMakeLists.txt
Expand Up @@ -135,6 +135,7 @@ set ( TEST_FILES
CoordTransformDistanceParserTest.h
CoordTransformDistanceTest.h
FitMDTest.h
IntegrateEllipsoidsTest.h
ImportMDEventWorkspaceTest.h
ImportMDHistoWorkspaceTest.h
MDBinTest.h
Expand Down
Expand Up @@ -62,7 +62,7 @@ class BoxControllerSettingsAlgorithmTest : public CxxTest::TestSuite
{
auto ws = boost::make_shared<Mantid::DataObjects::Workspace2D>();
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.
Expand Down
124 changes: 124 additions & 0 deletions Code/Mantid/Framework/MDEvents/test/IntegrateEllipsoidsTest.h
@@ -0,0 +1,124 @@
#include <cxxtest/TestSuite.h>
#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 <boost/make_shared.hpp>
#include <boost/tuple/tuple.hpp>

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<specid_t, std::set<detid_t>> 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<specid_t, Mantid::Kernel::V3D> neighbourMap = nn.neighbours(detectorId);

typedef std::map<specid_t, Mantid::Kernel::V3D> 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<EventWorkspace_sptr, PeaksWorkspace_sptr> 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<PeaksWorkspace>();
// 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<EventWorkspace>();
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<EventWorkspace_sptr, PeaksWorkspace_sptr>(eventWS, peaksWS);
}

void test_init()
{
Mantid::MDEvents::IntegrateEllipsoids alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize());
}

void test_execution()
{
auto out = createDiffractionData();
}







};
Expand Up @@ -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,
Expand Down
Expand Up @@ -439,9 +439,10 @@ createCylInstrumentWithDetInGivenPosisions(const std::vector<double> &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<Instrument> testInst(new Instrument("basic_rect"));

const double cylRadius(pixelSpacing / 2);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 47734c5

Please sign in to comment.