Skip to content

Commit

Permalink
Refs #10774. Added PoldiSpectrumConstantBackground
Browse files Browse the repository at this point in the history
This function inherits from FlatBackground and implements IPoldiFunction1D.
  • Loading branch information
Michael Wedel committed Jan 8, 2015
1 parent 3e26913 commit 2050932
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 2 deletions.
7 changes: 5 additions & 2 deletions Code/Mantid/Framework/SINQ/CMakeLists.txt
Expand Up @@ -35,6 +35,7 @@ set ( SRC_FILES
src/PoldiUtilities/PoldiResidualCorrelationCore.cpp
src/PoldiUtilities/PoldiSpectrumDomainFunction.cpp
src/PoldiUtilities/PoldiSourceSpectrum.cpp
src/PoldiUtilities/PoldiSpectrumConstantBackground.cpp
src/PoldiUtilities/PoldiSpectrumLinearBackground.cpp
src/PoldiUtilities/PoldiTimeTransformer.cpp
src/PoldiUtilities/UncertainValue.cpp
Expand Down Expand Up @@ -84,6 +85,7 @@ set ( INC_FILES
inc/MantidSINQ/PoldiUtilities/PoldiPeak.h
inc/MantidSINQ/PoldiUtilities/PoldiPeakCollection.h
inc/MantidSINQ/PoldiUtilities/PoldiResidualCorrelationCore.h
inc/MantidSINQ/PoldiUtilities/PoldiSpectrumConstantBackground.h
inc/MantidSINQ/PoldiUtilities/PoldiSpectrumDomainFunction.h
inc/MantidSINQ/PoldiUtilities/PoldiSpectrumLinearBackground.h
inc/MantidSINQ/PoldiUtilities/PoldiTimeTransformer.h
Expand Down Expand Up @@ -124,6 +126,7 @@ set ( TEST_FILES
PoldiResidualCorrelationCoreTest.h
PoldiSourceSpectrumTest.h
PoldiSpectrumDomainFunctionTest.h
PoldiSpectrumConstantBackgroundTest.h
PoldiSpectrumLinearBackgroundTest.h
PoldiTimeTransformerTest.h
PoldiTruncateDataTest.h
Expand All @@ -144,9 +147,9 @@ set_target_properties ( SINQ PROPERTIES OUTPUT_NAME MantidSINQ
# Add to the 'Framework' group in VS
set_property ( TARGET SINQ PROPERTY FOLDER "MantidFramework" )

include_directories ( inc ../MDEvents/inc )
include_directories ( inc ../MDEvents/inc ../CurveFitting/inc )

target_link_libraries ( SINQ ${MANTIDLIBS} MDEvents )
target_link_libraries ( SINQ ${MANTIDLIBS} MDEvents CurveFitting )

# Add the unit tests directory
add_subdirectory ( test )
Expand Down
@@ -0,0 +1,62 @@
#ifndef MANTID_SINQ_POLDISPECTRUMCONSTANTBACKGROUND_H_
#define MANTID_SINQ_POLDISPECTRUMCONSTANTBACKGROUND_H_

#include "MantidSINQ/DllConfig.h"
#include "MantidCurveFitting/FlatBackground.h"
#include "MantidSINQ/PoldiUtilities/IPoldiFunction1D.h"

namespace Mantid {
namespace Poldi {

/** PoldiSpectrumConstantBackground
The function inherits from FlatBackground and also implements the
IPoldiFunction1D interface.
@author Michael Wedel, Paul Scherrer Institut - SINQ
@date 07/01/2015
Copyright © 2015 PSI-MSS
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class MANTID_SINQ_DLL PoldiSpectrumConstantBackground
: public CurveFitting::FlatBackground,
public IPoldiFunction1D {
public:
PoldiSpectrumConstantBackground();
virtual ~PoldiSpectrumConstantBackground();

virtual std::string name() const { return "PoldiSpectrumConstantBackground"; }

virtual void setWorkspace(boost::shared_ptr<const API::Workspace> ws);
size_t getTimeBinCount() const;

virtual void poldiFunction1D(const std::vector<int> &indices,
const API::FunctionDomain1D &domain,
API::FunctionValues &values) const;

protected:
size_t m_timeBinCount;
};

} // namespace Poldi
} // namespace Mantid

#endif /* MANTID_SINQ_POLDISPECTRUMCONSTANTBACKGROUND_H_ */
@@ -0,0 +1,49 @@
#include "MantidSINQ/PoldiUtilities/PoldiSpectrumConstantBackground.h"
#include "MantidAPI/FunctionFactory.h"
#include "MantidAPI/MatrixWorkspace.h"

namespace Mantid {
namespace Poldi {

using namespace CurveFitting;
using namespace API;

DECLARE_FUNCTION(PoldiSpectrumConstantBackground)

/// Default constructor
PoldiSpectrumConstantBackground::PoldiSpectrumConstantBackground()
: FlatBackground(), IPoldiFunction1D(), m_timeBinCount(0) {}

/// Destructor
PoldiSpectrumConstantBackground::~PoldiSpectrumConstantBackground() {}

void PoldiSpectrumConstantBackground::setWorkspace(
boost::shared_ptr<const API::Workspace> ws) {
MatrixWorkspace_const_sptr matrixWs =
boost::dynamic_pointer_cast<const MatrixWorkspace>(ws);

if (matrixWs && matrixWs->getNumberHistograms() > 0) {
m_timeBinCount = matrixWs->readX(0).size();
}
}

size_t PoldiSpectrumConstantBackground::getTimeBinCount() const {
return m_timeBinCount;
}

void PoldiSpectrumConstantBackground::poldiFunction1D(
const std::vector<int> &indices, const API::FunctionDomain1D &domain,
API::FunctionValues &values) const {
double backgroundDetector = getParameter(0);
double wireCount = static_cast<double>(indices.size());
double distributionFactor = wireCount * static_cast<double>(m_timeBinCount) /
static_cast<double>(domain.size());
double backgroundD = backgroundDetector * distributionFactor;

for (size_t i = 0; i < values.size(); ++i) {
values.addToCalculated(i, backgroundD);
}
}

} // namespace SINQ
} // namespace Mantid
@@ -0,0 +1,79 @@
#ifndef MANTID_SINQ_POLDISPECTRUMCONSTANTBACKGROUNDTEST_H_
#define MANTID_SINQ_POLDISPECTRUMCONSTANTBACKGROUNDTEST_H_

#include <cxxtest/TestSuite.h>

#include "MantidSINQ/PoldiUtilities/PoldiSpectrumConstantBackground.h"

#include "MantidAPI/FrameworkManager.h"
#include "MantidAPI/FunctionFactory.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"

using namespace Mantid::API;
using namespace Mantid::Poldi;

class PoldiSpectrumConstantBackgroundTest : public CxxTest::TestSuite
{
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static PoldiSpectrumConstantBackgroundTest *createSuite() { return new PoldiSpectrumConstantBackgroundTest(); }
static void destroySuite( PoldiSpectrumConstantBackgroundTest *suite ) { delete suite; }

PoldiSpectrumConstantBackgroundTest()
{
FrameworkManager::Instance();
}

void testParameterCount()
{
PoldiSpectrumConstantBackground function;
function.initialize();

TS_ASSERT_EQUALS(function.nParams(), 1);
}

void testFunction()
{
IFunction_sptr function = FunctionFactory::Instance().createFunction("PoldiSpectrumConstantBackground");
MatrixWorkspace_sptr ws = WorkspaceCreationHelper::Create2DWorkspaceWhereYIsWorkspaceIndex(20, 2);

TS_ASSERT_THROWS_NOTHING(function->setWorkspace(ws));
function->setParameter(0, 10.0);

FunctionDomain1DVector domain(ws->readX(0));
FunctionValues values(domain);

function->function(domain, values);

TS_ASSERT_EQUALS(values[0], 10.0);
TS_ASSERT_EQUALS(values[1], 10.0);
}

void testPoldiFunction1D()
{
IFunction_sptr function = FunctionFactory::Instance().createFunction("PoldiSpectrumConstantBackground");
MatrixWorkspace_sptr ws = WorkspaceCreationHelper::Create2DWorkspace123(20, 2);

TS_ASSERT_THROWS_NOTHING(function->setWorkspace(ws));
function->setParameter(0, 10.0);

FunctionDomain1DVector domain(0.0, 10.0, 100);
FunctionValues values(domain);

// workspace has 20 spectra, value does not matter for function
std::vector<int> indices(20, 1);

boost::shared_ptr<IPoldiFunction1D> poldiFunction = boost::dynamic_pointer_cast<IPoldiFunction1D>(function);
TS_ASSERT(poldiFunction);
poldiFunction->poldiFunction1D(indices, domain, values);

for(size_t i = 0; i < values.size(); ++i) {
TS_ASSERT_EQUALS(values[i], 4.0)
}
}

};


#endif /* MANTID_SINQ_POLDISPECTRUMCONSTANTBACKGROUNDTEST_H_ */

0 comments on commit 2050932

Please sign in to comment.