Skip to content

Commit

Permalink
Added unit test. Refs #9326.
Browse files Browse the repository at this point in the history
Init unit test file;
Modified CMake;
Reformat .h and .cpp files
  • Loading branch information
wdzhou committed Apr 17, 2014
1 parent 200adde commit 579071a
Show file tree
Hide file tree
Showing 4 changed files with 302 additions and 140 deletions.
1 change: 1 addition & 0 deletions Code/Mantid/Framework/Algorithms/CMakeLists.txt
Expand Up @@ -672,6 +672,7 @@ set ( TEST_FILES
WeightingStrategyTest.h
WorkspaceCreationHelperTest.h
WorkspaceGroupTest.h
ChopEventFiltersTest.h
)

set ( TEST_PY_FILES NormaliseToUnityTest.py )
Expand Down
Expand Up @@ -7,11 +7,11 @@

namespace Mantid
{
namespace Algorithms
{
/**
* Add a peak to a PeaksWorkspace.
namespace Algorithms
{

/**
* Add a peak to a PeaksWorkspace.
@date 2012-10-16
Expand All @@ -35,53 +35,52 @@ namespace Mantid
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport ChopEventFilters : public API::Algorithm
{
public:
ChopEventFilters();
~ChopEventFilters();

/// Algorithm's name for identification
virtual const std::string name() const { return "ChopEventFilters";};
/// Algorithm's version for identification
virtual int version() const { return 1;};
/// Algorithm's category for identification
virtual const std::string category() const { return "Events\\EventFiltering";}

private:
/// Sets documentation strings for this algorithm
virtual void initDocs();
/// Initialise the properties
void init();
/// Run the algorithm
void exec();

void processAlgProperties();
void chopEventFilterByTime();


/// Input (source) time splitters workspace in MatrixWorkspace format
API::MatrixWorkspace_const_sptr m_inputWS;

/// Output time splitters workspace in MatrixWorkspace format
API::MatrixWorkspace_sptr m_outputWS;

/// Number of total time slots for chopping
int m_numSlots;

/// Index of the time slot for output
int m_slotIndex;

/// Target worskpace group to be chopped
int m_wsGroup;

std::vector<double> m_outX;
std::vector<double> m_outY;

};


} // namespace Mantid
class DLLExport ChopEventFilters : public API::Algorithm
{
public:
ChopEventFilters();
~ChopEventFilters();

/// Algorithm's name for identification
virtual const std::string name() const { return "ChopEventFilters";};
/// Algorithm's version for identification
virtual int version() const { return 1;};
/// Algorithm's category for identification
virtual const std::string category() const { return "Events\\EventFiltering";}

private:
/// Sets documentation strings for this algorithm
virtual void initDocs();
/// Initialise the properties
void init();
/// Run the algorithm
void exec();

void processAlgProperties();
void chopEventFilterByTime();


/// Input (source) time splitters workspace in MatrixWorkspace format
API::MatrixWorkspace_const_sptr m_inputWS;

/// Output time splitters workspace in MatrixWorkspace format
API::MatrixWorkspace_sptr m_outputWS;

/// Number of total time slots for chopping
int m_numSlots;

/// Index of the time slot for output
int m_slotIndex;

/// Target worskpace group to be chopped
int m_wsGroup;

std::vector<double> m_outX;
std::vector<double> m_outY;

};

} // namespace Mantid
} // namespace Algorithms


Expand Down
176 changes: 88 additions & 88 deletions Code/Mantid/Framework/Algorithms/src/ChopEventFilters.cpp
Expand Up @@ -30,40 +30,40 @@ namespace Mantid
namespace Algorithms
{

using namespace Mantid::API;
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::Kernel;

using namespace std;
using namespace std;

const double TOL = 1.0E-5;
const double TOL = 1.0E-5;

DECLARE_ALGORITHM(ChopEventFilters)
DECLARE_ALGORITHM(ChopEventFilters)

ChopEventFilters::ChopEventFilters()
{
}
ChopEventFilters::ChopEventFilters()
{
}

ChopEventFilters::~ChopEventFilters()
{
}
ChopEventFilters::~ChopEventFilters()
{
}

/** Wiki documentation
*/
void ChopEventFilters::initDocs()
{
/** Wiki documentation
*/
void ChopEventFilters::initDocs()
{
setWikiSummary("Chop the event filters for a specified workspace group to even time slots "
"and output one slot to a new workspace. ");
}

/** Declare properties
*/
void ChopEventFilters::init()
{
declareProperty(new WorkspaceProperty<MatrixWorkspace>("InputWorkspace", "", Direction::Input),
"Name of input event filter workspace to be processed.");
}

/** Declare properties
*/
void ChopEventFilters::init()
{
declareProperty(new WorkspaceProperty<MatrixWorkspace>("InputWorkspace", "", Direction::Input),
"Name of input event filter workspace to be processed.");

declareProperty(new WorkspaceProperty<MatrixWorkspace>("OutputWorkspace", "", Direction::Output),
"Name of the output event filter workspace as a result of processing.");
"Name of the output event filter workspace as a result of processing.");

declareProperty("WorkspaceGroup", EMPTY_INT(), "Workspace group of the evnet filers to be processed.");

Expand All @@ -73,55 +73,55 @@ void ChopEventFilters::init()
// FIXME - Need a validator for slot index
declareProperty("IndexOfSlot", EMPTY_INT(), "Index of the time slot to be kept.");

}
}

/** Main execution
*/
void ChopEventFilters::exec()
{
/** Main execution
*/
void ChopEventFilters::exec()
{
// Process inputs
processAlgProperties();

// Calculating splitted
// Calculating splitted
chopEventFilterByTime();

// Finale
if (m_outputWS) setProperty("OutputWorkspace", m_outputWS);
else throw std::runtime_error("Output workspace is a null pointer.");

return;
}
}

/** Process input property
*/
void ChopEventFilters::processAlgProperties()
{
/** Process input property
*/
void ChopEventFilters::processAlgProperties()
{
m_inputWS = getProperty("InputWorkspace");

m_wsGroup = getProperty("WorkspaceGroup");
if (isEmpty(m_wsGroup))
throw std::runtime_error("Workspace must be given.");
throw std::runtime_error("Workspace must be given.");

m_numSlots = getProperty("NumberOfSlots");

m_slotIndex = getProperty("IndexOfSlot");
if (m_slotIndex >= m_numSlots)
throw std::runtime_error("Slot index is out of boundary as number of slots.");
throw std::runtime_error("Slot index is out of boundary as number of slots.");

return;
}
}

/** Chop the selected splitters by time
*/
void ChopEventFilters::chopEventFilterByTime()
{
/** Chop the selected splitters by time
*/
void ChopEventFilters::chopEventFilterByTime()
{
double targetWSgroup = static_cast<double> (m_wsGroup);

const MantidVec& vecX = m_inputWS->readX(0);
const MantidVec& vecY = m_inputWS->readY(0);
if (vecY.size() == vecX.size())
{
throw runtime_error("Input workspace is wrong at x and y's sizes.");
throw runtime_error("Input workspace is wrong at x and y's sizes.");
}

// Find out splitters with matched workspace group
Expand All @@ -130,56 +130,56 @@ void ChopEventFilters::chopEventFilterByTime()
size_t numfilters = vecY.size();
for (size_t i = 0; i < numfilters; ++i)
{
double tmpgroup = vecY[i];
if (fabs(tmpgroup - targetWSgroup) < TOL)
double tmpgroup = vecY[i];
if (fabs(tmpgroup - targetWSgroup) < TOL)
{
// Found a group
double abs_start = vecX[i];
double abs_end = vecX[i+1];
double deltaT = (abs_end - abs_start)*invertN;

// Set up new start time and end time
double newstart = abs_start + slotindex_d * deltaT;
double newend = newstart + deltaT;

// Append to output vector
if (m_outX.size() == 0)
{
// Found a group
double abs_start = vecX[i];
double abs_end = vecX[i+1];
double deltaT = (abs_end - abs_start)*invertN;

// Set up new start time and end time
double newstart = abs_start + slotindex_d * deltaT;
double newend = newstart + deltaT;

// Append to output vector
if (m_outX.size() == 0)
{
// First entry
m_outX.push_back(newstart);
m_outX.push_back(newend);
m_outY.push_back(targetWSgroup);
}
else
{
// Appending to previous
if (newstart - m_outX.back() > TOL)
{
// This splitter is not adjacent
m_outX.push_back(newstart);
m_outX.push_back(newend);
m_outY.push_back(-1);
m_outY.push_back(targetWSgroup);
}
else
{
// New splitter is same and adjacent to previous one.
m_outX.back() = newend;
g_log.warning("It is not likely to happen that new splitter is continous to previous one.");
}
}

// First entry
m_outX.push_back(newstart);
m_outX.push_back(newend);
m_outY.push_back(targetWSgroup);
}

else
{
// Appending to previous
if (newstart - m_outX.back() > TOL)
{
// This splitter is not adjacent
m_outX.push_back(newstart);
m_outX.push_back(newend);
m_outY.push_back(-1);
m_outY.push_back(targetWSgroup);
}
else
{
// New splitter is same and adjacent to previous one.
m_outX.back() = newend;
g_log.warning("It is not likely to happen that new splitter is continous to previous one.");
}
}

}

} /// END(for i)

// Check
if (m_outX.size() == 0)
{
std::stringstream errss;
errss << "Input workspace does not have any splitters with target workspace group "
std::stringstream errss;
errss << "Input workspace does not have any splitters with target workspace group "
<< m_wsGroup << ", as user specified. ";
throw runtime_error(errss.str());
throw runtime_error(errss.str());
}

// Build output workspace group
Expand All @@ -190,13 +190,13 @@ void ChopEventFilters::chopEventFilterByTime()
MantidVec& dataY = m_outputWS->dataY(0);
for (size_t i = 0; i < sizey; ++i)
{
dataX[i] = m_outX[i];
dataY[i] = m_outY[i];
dataX[i] = m_outX[i];
dataY[i] = m_outY[i];
}
dataX[sizey] = m_outX[sizey];

return;
}
}

}
}

0 comments on commit 579071a

Please sign in to comment.