Skip to content

Commit

Permalink
Refs #4703, Refs #4511 BinMD records the intermediate WS
Browse files Browse the repository at this point in the history
and the coordinate transformations to it when binning a MDHistoWorkspace
  • Loading branch information
Janik Zikovsky committed Feb 2, 2012
1 parent c45fdde commit d20ad65
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 11 deletions.
10 changes: 7 additions & 3 deletions Code/Mantid/Framework/API/inc/MantidAPI/CoordTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@ namespace API
/// @return the number of output dimensions
size_t getOutD() const
{ return outD; };
//
// /// Compound the transformation by appending another one after this one.
// virtual void compound(CoordTransform * other) = 0;

/// @return the affine matrix equivalent to this transformation, if possible
/// @throw std::runtime_error if there is no possible affine matrix
virtual Mantid::Kernel::Matrix<coord_t> makeAffineMatrix() const
{
throw std::runtime_error("This coordinate transformation does not have an equivalent affine matrix.");
}

protected:
/// Input number of dimensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace MDEvents
virtual std::string toXMLString() const;
void addTranslation(const coord_t * translationVector);
const Mantid::Kernel::Matrix<coord_t> & getMatrix() const;
Mantid::Kernel::Matrix<coord_t> makeAffineMatrix() const;
void setMatrix(const Mantid::Kernel::Matrix<coord_t> & newMatrix);
void buildOrthogonal(const Mantid::Kernel::VMD & origin, const std::vector< Mantid::Kernel::VMD> & axes,
const Mantid::Kernel::VMD & scaling);
Expand All @@ -46,6 +47,7 @@ namespace MDEvents

static CoordTransformAffine * combineTransformations(CoordTransform * first, CoordTransform * second);


protected:
/** Affine Matrix to perform the transformation. The matrix has inD+1 columns, outD+1 rows.
* By using an affine, translations and rotations (or other linear transforms) can be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "MantidMDEvents/MDEventWorkspace.h"
#include "MantidMDEvents/MDHistoWorkspace.h"
#include "MantidMDEvents/SlicingAlgorithm.h"
#include "MantidMDEvents/CoordTransformAffine.h"

namespace Mantid
{
Expand Down Expand Up @@ -97,6 +98,13 @@ namespace MDEvents
/// Coordinate transformation to save in the output workspace (binned->original)
Mantid::API::CoordTransform * m_transformToOriginal;

/// Intermediate original workspace. Output -> intermediate (MDHisto) -> original (MDEvent)
Mantid::API::IMDWorkspace_sptr m_intermediateWS;
/// Coordinate transformation to save in the output WS, from the intermediate WS
Mantid::MDEvents::CoordTransformAffine * m_transformFromIntermediate;
/// Coordinate transformation to save in the intermediate WS
Mantid::MDEvents::CoordTransformAffine * m_transformToIntermediate;

/// Set to true if the cut is aligned with the axes
bool m_axisAligned;

Expand Down
14 changes: 11 additions & 3 deletions Code/Mantid/Framework/MDEvents/src/BinMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,12 +544,20 @@ namespace MDEvents
outWS = MDHistoWorkspace_sptr(new MDHistoWorkspace(m_binDimensions));

// Saves the geometry transformation from original to binned in the workspace
outWS->setTransformFromOriginal( this->m_transformFromOriginal );
outWS->setTransformToOriginal( this->m_transformToOriginal );
outWS->setTransformFromOriginal( this->m_transformFromOriginal, 0 );
outWS->setTransformToOriginal( this->m_transformToOriginal, 0 );
for (size_t i=0; i<m_bases.size(); i++)
outWS->setBasisVector(i, m_bases[i]);
outWS->setOrigin( this->m_origin );
outWS->setOriginalWorkspace(m_inWS);
outWS->setOriginalWorkspace(m_inWS, 0);

// And the intermediate WS one too, if any
if (m_intermediateWS)
{
outWS->setOriginalWorkspace(m_intermediateWS, 1);
outWS->setTransformFromOriginal(m_transformFromIntermediate, 1);
outWS->setTransformToOriginal(m_transformToIntermediate, 1);
}

// Wrapper to cast to MDEventWorkspace then call the function
bool IterateEvents = getProperty("IterateEvents");
Expand Down
8 changes: 8 additions & 0 deletions Code/Mantid/Framework/MDEvents/src/CoordTransformAffine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ namespace MDEvents
return affineMatrix;
}

/** @return the affine matrix */
Mantid::Kernel::Matrix<coord_t> CoordTransformAffine::makeAffineMatrix() const
{
return affineMatrix;
}



//----------------------------------------------------------------------------------------------
/** Add a translation (in the output coordinates) to the transform.
*
Expand Down
39 changes: 37 additions & 2 deletions Code/Mantid/Framework/MDEvents/src/SlicingAlgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ namespace MDEvents
/** Constructor
*/
SlicingAlgorithm::SlicingAlgorithm()
: m_transform(),
m_transformFromOriginal(), m_transformToOriginal(),
m_transformFromIntermediate(), m_transformToIntermediate()
{
}

Expand Down Expand Up @@ -90,6 +93,9 @@ namespace MDEvents

//----------------------------------------------------------------------------------------------
/** Generate the MDHistoDimension and basis vector for a given string from BasisVectorX etc.
*
* If the workspace being binned has an original workspace, then the vector
* is transformed to THOSE coordinates.
*
* "Format: 'name, units, x,y,z,.., length, number_of_bins'.\n"
* Adds values to m_bases, m_binDimensions and m_scaling
Expand Down Expand Up @@ -483,7 +489,8 @@ namespace MDEvents
m_axisAligned = getProperty("AxisAligned");

// Refer to the original workspace. Make sure that is possible
m_originalWS = boost::dynamic_pointer_cast<IMDWorkspace>(m_inWS->getOriginalWorkspace());
if (m_inWS->numOriginalWorkspaces() > 0)
m_originalWS = boost::dynamic_pointer_cast<IMDWorkspace>(m_inWS->getOriginalWorkspace());
if (m_originalWS)
{
if (m_axisAligned)
Expand All @@ -506,7 +513,35 @@ namespace MDEvents
// Finalize, for binnign MDHistoWorkspace
if (m_originalWS)
{
// Replace the input workspace
// The intermediate workspace is the MDHistoWorkspace being BINNED
m_intermediateWS = m_inWS;
CoordTransform * originalToIntermediate = m_inWS->getTransformFromOriginal();
if (originalToIntermediate && (m_originalWS->getNumDims() == m_intermediateWS->getNumDims()))
{
try
{
// The transform from the INPUT to the INTERMEDIATE ws
// = transformToOriginal * originalToIntermediate^-1
Matrix<coord_t> matToOriginal = m_transformToOriginal->makeAffineMatrix();
Matrix<coord_t> matOriginalToIntermediate = originalToIntermediate->makeAffineMatrix();
matOriginalToIntermediate.Invert();
Matrix<coord_t> matToIntermediate = matToOriginal * matOriginalToIntermediate;

m_transformToIntermediate = new CoordTransformAffine(m_originalWS->getNumDims(), m_intermediateWS->getNumDims());
m_transformToIntermediate->setMatrix(matToIntermediate);
// And now the reverse
matToIntermediate.Invert();
m_transformFromIntermediate = new CoordTransformAffine(m_intermediateWS->getNumDims(), m_originalWS->getNumDims());
m_transformFromIntermediate->setMatrix(matToIntermediate);
}
catch (std::runtime_error & )
{
// Ignore error. Have no transform.
}
}

// Replace the input workspace with the original MDEventWorkspace
// for future binning
m_inWS = m_originalWS;
}

Expand Down
23 changes: 20 additions & 3 deletions Code/Mantid/Framework/MDEvents/test/BinMDTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,9 @@ class BinMDTest : public CxxTest::TestSuite
* @param binned1Name :: original, binned direct from MDEW
* @param binned2Name :: binned from a MDHisto
* @param origWS :: both should have this as its originalWorkspace
* @return binned2 shared pointer
*/
void do_compare_histo(std::string binned1Name, std::string binned2Name, std::string origWS)
MDHistoWorkspace_sptr do_compare_histo(std::string binned1Name, std::string binned2Name, std::string origWS)
{
MDHistoWorkspace_sptr binned1 = boost::dynamic_pointer_cast<MDHistoWorkspace>(AnalysisDataService::Instance().retrieve(binned1Name));
MDHistoWorkspace_sptr binned2 = boost::dynamic_pointer_cast<MDHistoWorkspace>(AnalysisDataService::Instance().retrieve(binned2Name));
Expand All @@ -452,6 +453,7 @@ class BinMDTest : public CxxTest::TestSuite
TS_ASSERT_DELTA( binned1->getSignalAt(i), binned2->getSignalAt(i), 1e-5);
}
TS_ASSERT_EQUALS( numErrors, 0);
return binned2;
}

/** Common setup for double-binning tests */
Expand Down Expand Up @@ -504,7 +506,14 @@ class BinMDTest : public CxxTest::TestSuite
"ForceOrthogonal", "1",
"Origin", "-10, -10");

do_compare_histo("binned0", "binned1", "mdew");
MDHistoWorkspace_sptr binned1 = do_compare_histo("binned0", "binned1", "mdew");

// Intermediate workspace (the MDHisto)
TS_ASSERT_EQUALS( binned1->numOriginalWorkspaces(), 2);
TS_ASSERT_EQUALS( binned1->getOriginalWorkspace(1)->name(), "binned0");
// Transforms to/from the INTERMEDIATE workspace exist
TS_ASSERT( binned1->getTransformToOriginal(1) );
TS_ASSERT( binned1->getTransformFromOriginal(1) );
}


Expand Down Expand Up @@ -543,7 +552,15 @@ class BinMDTest : public CxxTest::TestSuite
"ForceOrthogonal", "1",
"Origin", "0, 0");
// Check they are the same
do_compare_histo("binned0", "binned2", "mdew");
MDHistoWorkspace_sptr binned2 = do_compare_histo("binned0", "binned2", "mdew");

// Intermediate workspace (the MDHisto)
TS_ASSERT_EQUALS( binned2->numOriginalWorkspaces(), 2);
TS_ASSERT_EQUALS( binned2->getOriginalWorkspace(1)->name(), "binned1");
// Transforms to/from the INTERMEDIATE workspace exist
TS_ASSERT( binned2->getTransformToOriginal(1) );
TS_ASSERT( binned2->getTransformFromOriginal(1) );

}


Expand Down

0 comments on commit d20ad65

Please sign in to comment.