Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs #10774. Added PoldiSpectrumConstantBackground
This function inherits from FlatBackground and implements IPoldiFunction1D.
- Loading branch information
Michael Wedel
committed
Jan 8, 2015
1 parent
3e26913
commit 2050932
Showing
4 changed files
with
195 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
Code/Mantid/Framework/SINQ/inc/MantidSINQ/PoldiUtilities/PoldiSpectrumConstantBackground.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_ */ |
49 changes: 49 additions & 0 deletions
49
Code/Mantid/Framework/SINQ/src/PoldiUtilities/PoldiSpectrumConstantBackground.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
79 changes: 79 additions & 0 deletions
79
Code/Mantid/Framework/SINQ/test/PoldiSpectrumConstantBackgroundTest.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_ */ |