Skip to content

Commit

Permalink
Re: 5285 Add convertToBinBoundary() to VectorHelper
Browse files Browse the repository at this point in the history
Since VectorHelper::rebin() requires current X vector with
one more entry than the current Y vector, even if the
Workspace2D is a distribution, we need to adjust the current
X vector before calling rebin.  This is a convenience method
that essentially reverses the convertToBinCentre() method, so
it seems to belong in VectorHelper.
refs #5285
  • Loading branch information
DennisMikkelson committed May 12, 2012
1 parent cae894e commit bc2a6e3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
18 changes: 15 additions & 3 deletions Code/Mantid/Framework/DataObjects/src/Workspace2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,21 @@ namespace Mantid
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());

// Perform the rebin from the current bins to the new ones
if ( currentX.size() == currentY.size() ) // First, convert to bin boundaries if needed. The
{ // VectorHelper::rebin, assumes bin boundaries, even if
std::vector<double> histX; // it is a distribution!
histX.resize( currentX.size() + 1 );
Mantid::Kernel::VectorHelper::convertToBinBoundary( currentX, histX );
Mantid::Kernel::VectorHelper::rebin( histX, currentY, currentE, X, Y, E,
this->isDistribution() );
}
else // assume x_size = y_size + 1
{
Mantid::Kernel::VectorHelper::rebin( currentX, currentY, currentE, X, Y, E,
this->isDistribution() );
}
}


Expand Down
4 changes: 4 additions & 0 deletions Code/Mantid/Framework/Kernel/inc/MantidKernel/VectorHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ namespace VectorHelper

/// Convert an array of bin boundaries to bin centre values.
void MANTID_KERNEL_DLL convertToBinCentre(const std::vector<double> & bin_edges, std::vector<double> & bin_centres);

/// Convert an array of bin centers to bin boundary values.
void MANTID_KERNEL_DLL convertToBinBoundary(const std::vector<double> & bin_centers, std::vector<double> & bin_edges);

bool MANTID_KERNEL_DLL isConstantValue(const MantidVec &arra);

template <typename NumT>
Expand Down
35 changes: 35 additions & 0 deletions Code/Mantid/Framework/Kernel/src/VectorHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,41 @@ void convertToBinCentre(const std::vector<double> & bin_edges, std::vector<doubl
bin_centres.erase(bin_centres.begin());
}


//-------------------------------------------------------------------------------------------------
/**
* Convert the given set of bin centers into bin boundary values.
* NOTE: the first and last bin boundaries are calculated so
* that the first and last bin centers are in the center of the
* first and last bins, respectively. For a particular set of
* bin centers, this may not be correct, but it is the best that
* can be done, lacking any other information.
*
* @param bin_centres :: A vector of values specifying bin centers.
* @param bin_edges :: An output vector of values specifying bin
* boundaries
*
*/
void convertToBinBoundary(const std::vector<double> & bin_centers, std::vector<double> & bin_edges)
{
const std::vector<double>::size_type n = bin_centers.size();
if( bin_edges.size() != ( n + 1 ) )
{
bin_edges.resize( n + 1 );
}

for( size_t i = 0; i < n - 1; ++i )
{
bin_edges[i+1] = 0.5*(bin_centers[i] + bin_centers[i+1]);
}

bin_edges[0] = bin_centers[0] - (bin_edges[1] - bin_centers[0]);

bin_edges[ n ] = bin_centers[ n - 1 ] +
( bin_centers[ n - 1 ] - bin_edges[ n - 1 ] );
}


//-------------------------------------------------------------------------------------------------
/** Assess if all the values in the vector are equal or if there are some different values
* @param[in] arra the vector to examine
Expand Down

0 comments on commit bc2a6e3

Please sign in to comment.