Skip to content

Commit

Permalink
refs #5016. Move it location and some other improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenArnold committed Mar 23, 2012
1 parent a2076a4 commit b885aff
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 36 deletions.
9 changes: 3 additions & 6 deletions Code/Mantid/Framework/MDAlgorithms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ set ( SRC_FILES
src/ConvertToMDEvents.cpp
src/ConvertToMDEventsParams.cpp
src/ConvertToMDEventsSubalgFactory.cpp
src/ConvertToReflectometryQ.cpp
src/DivideMD.cpp
src/EqualToMD.cpp
src/ExponentialMD.cpp
Expand Down Expand Up @@ -74,7 +73,6 @@ set ( INC_FILES
inc/MantidMDAlgorithms/ConvertToMDEventsTransfNoQ.h
inc/MantidMDAlgorithms/ConvertToMDEventsTransfQ3D.h
inc/MantidMDAlgorithms/ConvertToMDEventsUnitsConv.h
inc/MantidMDAlgorithms/ConvertToReflectometryQ.h
inc/MantidMDAlgorithms/DivideMD.h
inc/MantidMDAlgorithms/EqualToMD.h
inc/MantidMDAlgorithms/ExponentialMD.h
Expand Down Expand Up @@ -127,7 +125,6 @@ set ( TEST_FILES
test/ConvertToMDEventsTest.h
test/ConvertToMDEventsUnitsConvTest.h
test/ConvertToQ3DdETest.h
test/ConvertToReflectometryQTest.h
test/DivideMDTest.h
test/EqualToMDTest.h
test/ExponentialMDTest.h
Expand Down Expand Up @@ -186,7 +183,7 @@ target_link_libraries ( MDAlgorithms ${MANTIDLIBS} MDEvents)

# Create test file projects
if ( CXXTEST_FOUND )
include_directories( ../TestHelpers/inc ../DataHandling/inc ../CurveFitting/inc ../Algorithms/inc)
include_directories( ../TestHelpers/inc ../DataHandling/inc ../CurveFitting/inc)
# This variable is used within the cxxtest_add_test macro to build these helper classes into the test executable.
# It will go out of scope at the end of this file so doesn't need un-setting
set ( TESTHELPER_SRCS ../TestHelpers/src/ComponentCreationHelper.cpp
Expand All @@ -195,10 +192,10 @@ if ( CXXTEST_FOUND )
../TestHelpers/src/BinaryOperationMDTestHelper.cpp )
if ( GMOCK_FOUND AND GTEST_FOUND )
cxxtest_add_test ( MDAlgorithmsTest ${TEST_FILES} ${GMOCK_TEST_FILES})
target_link_libraries( MDAlgorithmsTest MDAlgorithms DataHandling CurveFitting Nexus NexusCPP Algorithms ${GMOCK_LIBRARIES} ${GTEST_LIBRARIES} )
target_link_libraries( MDAlgorithmsTest MDAlgorithms DataHandling CurveFitting Nexus NexusCPP ${GMOCK_LIBRARIES} ${GTEST_LIBRARIES} )
else ()
cxxtest_add_test ( MDAlgorithmsTest ${TEST_FILES} )
target_link_libraries( MDAlgorithmsTest MDAlgorithms DataHandling CurveFitting Nexus NexusCPP Algorithms)
target_link_libraries( MDAlgorithmsTest MDAlgorithms DataHandling CurveFitting Nexus NexusCPP)
endif ()
add_dependencies ( FrameworkTests MDAlgorithmsTest )
# Add to the 'FrameworkTests' group in VS
Expand Down
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/MDEvents/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set ( SRC_FILES
src/CentroidPeaksMD.cpp
src/CloneMDWorkspace.cpp
src/ConvertToDiffractionMDWorkspace.cpp
src/ConvertToReflectometryQ.cpp
src/CoordTransformAffine.cpp
src/CoordTransformAffineParser.cpp
src/CoordTransformAligned.cpp
Expand Down Expand Up @@ -58,6 +59,7 @@ set ( INC_FILES
inc/MantidMDEvents/CentroidPeaksMD.h
inc/MantidMDEvents/CloneMDWorkspace.h
inc/MantidMDEvents/ConvertToDiffractionMDWorkspace.h
inc/MantidMDEvents/ConvertToReflectometryQ.h
inc/MantidMDEvents/CoordTransformAffine.h
inc/MantidMDEvents/CoordTransformAffineParser.h
inc/MantidMDEvents/CoordTransformAligned.h
Expand Down Expand Up @@ -101,6 +103,7 @@ set ( TEST_FILES
test/CentroidPeaksMDTest.h
test/CloneMDWorkspaceTest.h
test/ConvertToDiffractionMDWorkspaceTest.h
test/ConvertToReflectometryQTest.h
test/CoordTransformAffineParserTest.h
test/CoordTransformAffineTest.h
test/CoordTransformAlignedTest.h
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#ifndef MANTID_MDALGORITHMS_CONVERTTOREFLECTOMETRYQ_H_
#define MANTID_MDALGORITHMS_CONVERTTOREFLECTOMETRYQ_H_
#ifndef MANTID_MDEVENTS_CONVERTTOREFLECTOMETRYQ_H_
#define MANTID_MDEVENTS_CONVERTTOREFLECTOMETRYQ_H_

#include "MantidKernel/System.h"
#include "MantidAPI/Algorithm.h"

namespace Mantid
{
namespace MDAlgorithms
namespace MDEvents
{

/** ConvertToReflectometryQ : Creates a 2D MD Histogram workspace with two axis qz and qx.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ TODO: Enter a full wiki-markup description of your algorithm here. You can then
*WIKI*/

#include "MantidAPI/WorkspaceValidators.h"
#include "MantidMDAlgorithms/ConvertToReflectometryQ.h"
#include "MantidMDEvents/ConvertToReflectometryQ.h"
#include "MantidKernel/System.h"
#include "MantidGeometry/Instrument/ReferenceFrame.h"
#include "MantidGeometry/MDGeometry/MDHistoDimension.h"
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidAPI/IMDHistoWorkspace.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidMDEvents/MDEventWorkspace.h"
#include "MantidMDEvents/MDHistoWorkspace.h"

#include <boost/shared_ptr.hpp>

Expand All @@ -21,7 +22,7 @@ using namespace Mantid::DataObjects;

namespace Mantid
{
namespace MDAlgorithms
namespace MDEvents
{

// Register the algorithm into the AlgorithmFactory
Expand Down Expand Up @@ -71,32 +72,38 @@ namespace MDAlgorithms
declareProperty(new WorkspaceProperty<MatrixWorkspace>("InputWorkspace","",Direction::Input, validator),
"An input workspace in time-of-flight");

declareProperty(new WorkspaceProperty<IMDHistoWorkspace>("OutputWorkspace","",Direction::Output), "Output 2D Workspace.");
declareProperty(new WorkspaceProperty<IMDWorkspace>("OutputWorkspace","",Direction::Output), "Output 2D Workspace.");
}

//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void ConvertToReflectometryQ::exec()
{
// -------- Input workspace -> convert to Event ------------------------------------
MatrixWorkspace_sptr in_ws = getProperty("InputWorkspace");

Mantid::API::WorkspaceFactory().Instance().create("");

//outWs->addDimension(new MDHistoDimension("", "", "A^(-1)")
//TODO rebin input?

MatrixWorkspace_sptr in_ws = getProperty("InputWorkspace");

//TODO, how to calculate max, min + number of bins? Jon Taylor thinks that there is a way to calculate these upfront.
const double qzmin = -50;
const double qxmin = -50;
const double qxmax = 50;
const double qzmax = 50;
const size_t nbinsx = 10;
const size_t nbinsz = 10;

// Fixed dimensionality
MDHistoDimension_sptr qxDim = MDHistoDimension_sptr(new MDHistoDimension("Qx","qx","(Ang^-1)", qxmin, qxmax, nbinsx));
MDHistoDimension_sptr qzDim = MDHistoDimension_sptr(new MDHistoDimension("Qz","qz","(Ang^-1)", qzmin, qzmax, nbinsz));
MDHistoWorkspace_sptr ws = MDHistoWorkspace_sptr(new MDHistoWorkspace(qxDim, qzDim));

// check the input units
if (in_ws->getAxis(0)->unit()->unitID() != "TOF")
throw std::invalid_argument("Input event workspace's X axis must be in TOF units.");

// Try to get the output workspace
//IMDEventWorkspace_sptr i_out = getProperty("OutputWorkspace");
//ws = boost::dynamic_pointer_cast<MDEventWorkspace<MDLeanEvent<3>,3> >( i_out );

// Calculate theta incident
Instrument_const_sptr instrument = in_ws->getInstrument();

V3D sample = instrument->getSample()->getPos();
V3D source = instrument->getSource()->getPos();
V3D beamDir = (sample - source);
Expand All @@ -105,26 +112,26 @@ namespace MDAlgorithms
double thetaIn = beamDir.angle(along);
double thetaFinal = 0;


//Calculate Theta_f and Theta_i

const size_t nHistograms = in_ws->getNumberHistograms();
//Loop through all spectra
for(size_t index = 0; index < nHistograms; ++index)
{
V3D sink = in_ws->getDetector(index)->getPos();
V3D detectorDir = (sink - sample);
double distance = detectorDir.norm() + beamDir.norm();
V3D normaDetectorDir = detectorDir / detectorDir.norm();
V3D normalDetectorDir = detectorDir / detectorDir.norm();
thetaFinal = detectorDir.angle(along);

auto counts = in_ws->readY(index);
auto tofs = in_ws->readX(index);
auto errors = in_ws->readE(index);

size_t nTofs = in_ws->isHistogramData() ? (tofs.size() - 1) : tofs.size();

const double wavenumber_in_angstrom_times_tof_in_microsec =
(PhysicalConstants::NeutronMass * distance * 1e-10) / (1e-6 * PhysicalConstants::h_bar);

//Loop through all TOF
for(size_t tof = 0; tof < nTofs; ++tof)
{
//Calculate wave number
Expand All @@ -134,17 +141,38 @@ namespace MDAlgorithms
t = (t + tofs[tof+1])/2;
}
double wavenumber = wavenumber_in_angstrom_times_tof_in_microsec / t;
double qx = wavenumber *(sin(thetaFinal) + sin(thetaIn));
double qz = wavenumber *(cos(thetaFinal) - cos(thetaIn));
//double _qx = wavenumber *(sin(thetaFinal) + sin(thetaIn));
//double _qz = wavenumber *(cos(thetaFinal) - cos(thetaIn));

double signal = counts[tof];
double error = errors[tof];
double _qx = wavenumber *(sin(thetaFinal) + sin(thetaIn));
double _qz = wavenumber *(cos(thetaFinal) - cos(thetaIn));


/// If q-max and min are known a-prori, these boundrary case truncations are not required. See top of method for comment.
_qx = _qx < qxmin ? qxmin : _qx;
_qz = _qz < qzmin ? qzmin : _qz;
_qx = _qx > qxmin ? qxmin : _qx;
_qz = _qz > qzmin ? qzmin : _qz;

//Set up for linear transformation qi -> dimension index.
double mx = (nbinsx / (qxmax - qxmin));
double mz = (nbinsz / (qzmax - qzmin));
double cx = (nbinsx - mx * (qxmin + qxmax))/2;
double cz = (nbinsz - mz * (qzmin + qzmax))/2;

size_t posIndexX = mx*_qx + cx;
size_t posIndexZ = mz*_qz + cz;

size_t linearindex = (posIndexX * nbinsx) + posIndexZ;

double error = errors[tof];

//Do we want to accumulate signal values, i.e will there be any overlap in Q?
ws->setSignalAt(linearindex, counts[tof]);
ws->setErrorSquaredAt(linearindex, error*error);
}
}

setProperty("OutputWorkspace", ws);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef MANTID_MDALGORITHMS_CONVERTTOREFLECTOMETRYQTEST_H_
#define MANTID_MDALGORITHMS_CONVERTTOREFLECTOMETRYQTEST_H_
#ifndef MANTID_MDEVENTS_CONVERTTOREFLECTOMETRYQTEST_H_
#define MANTID_MDEVENTS_CONVERTTOREFLECTOMETRYQTEST_H_

#include <cxxtest/TestSuite.h>
#include "MantidKernel/Timer.h"
Expand All @@ -8,10 +8,10 @@
#include <iomanip>
#include "MantidGeometry/Instrument/ReferenceFrame.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
#include "MantidMDAlgorithms/ConvertToReflectometryQ.h"
#include "MantidMDEvents/ConvertToReflectometryQ.h"

using namespace Mantid;
using namespace Mantid::MDAlgorithms;
using namespace Mantid::MDEvents;
using namespace Mantid::API;
using namespace Mantid::Geometry;

Expand Down

0 comments on commit b885aff

Please sign in to comment.