Skip to content

Commit

Permalink
Refs #5285 MatrixWorkspace-level generateHistogram() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Janik Zikovsky committed May 7, 2012
1 parent 292947a commit 4c801e4
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Code/Mantid/Framework/API/inc/MantidAPI/MatrixWorkspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ namespace Mantid
virtual void setData(const std::size_t index, const MantidVecPtr::ptr_type& Y, const MantidVecPtr::ptr_type& E)
{ getSpectrum(index)->setData(Y,E); }


/// Generate the histogram or rebin the existing histogram.
virtual void generateHistogram(const std::size_t index, const MantidVec& X, MantidVec& Y, MantidVec& E, bool skipError = false) const = 0;


/// Return a vector with the integrated counts for all spectra withing the given range
Expand Down
2 changes: 2 additions & 0 deletions Code/Mantid/Framework/API/test/CompositeFunctionTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class CompositeFunctionTest_MocMatrixWorkspace : public MatrixWorkspace
virtual const ISpectrum * getSpectrum(const size_t index) const {return &m_spectra[index];}
const std::string id(void) const {return "";}
void init(const size_t &,const size_t &,const size_t &) { }
void generateHistogram(const std::size_t , const MantidVec& , MantidVec& , MantidVec& , bool ) const { }

private:
std::vector<CompositeFunctionTest_MocSpectrum> m_spectra;
size_t m_blocksize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ class DLLExport EventWorkspace : public API::IEventWorkspace
// Get a pointer to the x data at the given workspace index
Kernel::cow_ptr<MantidVec> refX(const std::size_t) const;

void generateHistogram(const std::size_t index, const MantidVec& X, MantidVec& Y, MantidVec& E, bool skipError = false) const;

//------------------------------------------------------------
// Set the x-axis data (histogram bins) for all pixels
void setAllX(Kernel::cow_ptr<MantidVec> &x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class DLLExport Workspace2D : public API::MatrixWorkspace
/// Return the underlying ISpectrum ptr (const version) at the given workspace index.
virtual const Mantid::API::ISpectrum * getSpectrum(const size_t index) const;

void generateHistogram(const std::size_t index, const MantidVec& X, MantidVec& Y, MantidVec& E, bool skipError = false) const;

/** sets the monitorWorkspace indexlist
@param mList :: a vector holding the monitor workspace indexes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class DLLExport WorkspaceSingleValue : public API::MatrixWorkspace
// Return the underlying ISpectrum ptr (const version) at the given workspace index.
virtual const Mantid::API::ISpectrum * getSpectrum(const size_t index) const;

void generateHistogram(const std::size_t index, const MantidVec& X, MantidVec& Y, MantidVec& E, bool skipError = false) const;

private:
/// Private copy constructor. NO COPY ALLOWED
Expand Down
16 changes: 16 additions & 0 deletions Code/Mantid/Framework/DataObjects/src/EventWorkspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,22 @@ namespace DataObjects
return this->data[index]->ptrX();
}

//---------------------------------------------------------------------------
/** Using the event data in the event list, generate a histogram of it.
*
* @param index :: workspace index to generate
* @param X :: input X vector of the bin boundaries.
* @param Y :: output vector to be filled with the Y data.
* @param E :: output vector to be filled with the Error data (optionally)
* @param skipError :: if true, the error vector is NOT calculated.
* This may save some processing time.
*/
void EventWorkspace::generateHistogram(const std::size_t index, const MantidVec& X, MantidVec& Y, MantidVec& E, bool skipError) const
{
if (index >= this->m_noVectors)
throw std::range_error("EventWorkspace::generateHistogram, histogram number out of range");
this->data[index]->generateHistogram(X, Y, E, skipError);
}


//-----------------------------------------------------------------------------
Expand Down
31 changes: 31 additions & 0 deletions Code/Mantid/Framework/DataObjects/src/Workspace2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "MantidAPI/WorkspaceProperty.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidAPI/ISpectrum.h"
#include "MantidKernel/VectorHelper.h"

using Mantid::API::ISpectrum;

Expand Down Expand Up @@ -121,6 +122,36 @@ namespace Mantid
return data.size();
}


//---------------------------------------------------------------------------
/** Rebin a particulare spectrum to a new histogram bin boundaries.
*
* @param index :: workspace index to generate
* @param X :: input X vector of the bin boundaries.
* @param Y :: output vector to be filled with the Y data.
* @param E :: output vector to be filled with the Error data (optionally)
* @param skipError :: if true, the error vector is NOT calculated.
* CURRENTLY IGNORED, the Error is always calculated.
*/
void Workspace2D::generateHistogram(const std::size_t index, const MantidVec& X, MantidVec& Y, MantidVec& E, bool skipError) const
{
UNUSED_ARG(skipError);
if (index >= this->m_noVectors)
throw std::range_error("Workspace2D::generateHistogram, histogram number out of range");
// output data arrays are implicitly filled by function
const ISpectrum * spec = this->getSpectrum(index);
const MantidVec & currentX = spec->readX();
const MantidVec & currentY = spec->readY();
const MantidVec & currentE = spec->readE();
if (X.size() <= 1) throw std::runtime_error("Workspace2D::generateHistogram(): X vector must be at least length 2");
Y.resize(X.size()-1, 0);
E.resize(X.size()-1, 0);
// Perform the rebin from the current bins to the new ones
Mantid::Kernel::VectorHelper::rebin(currentX,currentY,currentE, X, Y, E,
this->isDistribution());
}


} // namespace DataObjects
} //NamespaceMantid

Expand Down
11 changes: 11 additions & 0 deletions Code/Mantid/Framework/DataObjects/src/WorkspaceSingleValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ namespace Mantid
return &data;
}

/// Rebin the workspace. Not implemented for this workspace.
void WorkspaceSingleValue::generateHistogram(const std::size_t index, const MantidVec& X, MantidVec& Y, MantidVec& E, bool skipError) const
{
UNUSED_ARG(index);
UNUSED_ARG(X);
UNUSED_ARG(Y);
UNUSED_ARG(E);
UNUSED_ARG(skipError);
throw std::runtime_error("generateHistogram() not implemented for WorkspaceSingleValue.");
}


} // namespace DataObjects
} // namespace Mantid
Expand Down
17 changes: 17 additions & 0 deletions Code/Mantid/Framework/DataObjects/test/Workspace2DTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,23 @@ class Workspace2DTest : public CxxTest::TestSuite
}
}

void test_generateHistogram()
{
Workspace2D_sptr ws = Create2DWorkspaceBinned(2, 5);
MantidVec X, Y, E;
X.push_back(0.0);
X.push_back(0.5);
X.push_back(1.0);
TS_ASSERT_THROWS_ANYTHING( ws->generateHistogram(2, X, Y, E); );
TS_ASSERT_THROWS_NOTHING( ws->generateHistogram(0, X, Y, E); );
TS_ASSERT_EQUALS( Y.size(), 2);
TS_ASSERT_EQUALS( E.size(), 2);
TS_ASSERT_DELTA( Y[0], 1.0, 1e-5);
TS_ASSERT_DELTA( Y[1], 1.0, 1e-5);
TS_ASSERT_DELTA( E[0], 1.0, 1e-5);
TS_ASSERT_DELTA( E[1], 1.0, 1e-5);
}

void testDataDx()
{
TS_ASSERT_EQUALS( ws->readDx(0).size(), 6 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class WorkspaceTester : public MatrixWorkspace
size_t blocksize() const {return vec[0].dataY().size();}
ISpectrum * getSpectrum(const size_t index) { return &vec[index]; }
const ISpectrum * getSpectrum(const size_t index) const { return &vec[index];; }
void generateHistogram(const std::size_t , const MantidVec& , MantidVec& , MantidVec& , bool ) const { }

private:
std::vector<SpectrumTester> vec;
Expand Down

0 comments on commit 4c801e4

Please sign in to comment.