Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
306 additions
and
3 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
70 changes: 70 additions & 0 deletions
70
Code/Mantid/Framework/CurveFitting/inc/MantidCurveFitting/CalculateGammaBackground.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,70 @@ | ||
#ifndef MANTID_CURVEFITTING_CALCULATEGAMMABACKGROUND_H_ | ||
#define MANTID_CURVEFITTING_CALCULATEGAMMABACKGROUND_H_ | ||
|
||
#include "MantidAPI/Algorithm.h" | ||
|
||
namespace Mantid | ||
{ | ||
namespace CurveFitting | ||
{ | ||
|
||
/** | ||
Copyright © 2013 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory | ||
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 DLLExport CalculateGammaBackground : public API::Algorithm | ||
{ | ||
public: | ||
CalculateGammaBackground(); | ||
|
||
private: | ||
|
||
const std::string name() const; | ||
int version() const; | ||
const std::string category() const; | ||
|
||
void initDocs(); | ||
void init(); | ||
void exec(); | ||
|
||
/// Check and store appropriate input data | ||
void retrieveInputs(); | ||
/// Create the output workspaces | ||
void createOutputWorkspaces(); | ||
/// Calculate & correct the given index of the input workspace | ||
void applyCorrection(const size_t index); | ||
|
||
API::MatrixWorkspace_const_sptr m_inputWS; | ||
size_t m_npeaks; | ||
std::vector<double> m_masses; | ||
std::vector<double> m_amplitudes; | ||
std::vector<double> m_widths; | ||
|
||
API::MatrixWorkspace_sptr m_backgroundWS; | ||
API::MatrixWorkspace_sptr m_correctedWS; | ||
|
||
}; | ||
|
||
|
||
} // namespace CurveFitting | ||
} // namespace Mantid | ||
|
||
#endif /* MANTID_CURVEFITTING_CALCULATEGAMMABACKGROUND_H_ */ |
134 changes: 134 additions & 0 deletions
134
Code/Mantid/Framework/CurveFitting/src/CalculateGammaBackground.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,134 @@ | ||
/*WIKI* | ||
*WIKI*/ | ||
#include "MantidCurveFitting/CalculateGammaBackground.h" | ||
#include "MantidAPI/WorkspaceValidators.h" | ||
#include "MantidKernel/ArrayProperty.h" | ||
#include "MantidKernel/MandatoryValidator.h" | ||
|
||
|
||
namespace Mantid | ||
{ | ||
namespace CurveFitting | ||
{ | ||
//-------------------------------------------------------------------------------------------------------- | ||
// Public members | ||
//-------------------------------------------------------------------------------------------------------- | ||
|
||
/// Default constructor | ||
CalculateGammaBackground::CalculateGammaBackground() | ||
: Algorithm(), m_inputWS(), m_npeaks(0), m_masses(), m_amplitudes(), m_widths(), | ||
m_backgroundWS(), m_correctedWS() | ||
{ | ||
} | ||
|
||
//-------------------------------------------------------------------------------------------------------- | ||
// Private members | ||
//-------------------------------------------------------------------------------------------------------- | ||
|
||
const std::string CalculateGammaBackground::name() const | ||
{ | ||
return "CalculateGammaBackground"; | ||
} | ||
|
||
int CalculateGammaBackground::version() const | ||
{ | ||
return 1; | ||
} | ||
|
||
const std::string CalculateGammaBackground::category() const | ||
{ | ||
return "CorrectionFunctions"; | ||
} | ||
|
||
void CalculateGammaBackground::initDocs() | ||
{ | ||
this->setWikiSummary("Calculates and the background due to gamma rays produced when neutrons are absorbed by shielding"); | ||
this->setOptionalMessage("Calculates the background due to gamma rays produced when neutrons are absorbed by shielding."); | ||
} | ||
|
||
void CalculateGammaBackground::init() | ||
{ | ||
using namespace API; | ||
using namespace Kernel; | ||
|
||
declareProperty(new WorkspaceProperty<>("InputWorkspace", "", Direction::Input, | ||
boost::make_shared<WorkspaceUnitValidator>("TOF")), | ||
"An input workspace containing TOF data"); | ||
|
||
// Peak information | ||
const std::string grpName = "Peaks"; | ||
typedef MandatoryValidator<std::vector<double>> NonEmptyDoubleArray; | ||
declareProperty(new ArrayProperty<double>("Masses",boost::make_shared<NonEmptyDoubleArray>()), | ||
"List of mass values for each peak in the input spectrum in atomic mass units"); | ||
setPropertyGroup("Masses", grpName); | ||
declareProperty(new ArrayProperty<double>("PeakAmplitudes",boost::make_shared<NonEmptyDoubleArray>()), | ||
"List of amplitudes of the peaks in the same order as the Masses property"); | ||
setPropertyGroup("PeakAmplitudes", grpName); | ||
declareProperty(new ArrayProperty<double>("PeakWidths",boost::make_shared<NonEmptyDoubleArray>()), | ||
"List of std deviations of peak widths in the same order as the Masses property"); | ||
setPropertyGroup("PeakWidths", grpName); | ||
|
||
declareProperty(new WorkspaceProperty<>("BackgroundWorkspace", "", Direction::Output)); | ||
declareProperty(new WorkspaceProperty<>("CorrectedWorkspace", "", Direction::Output)); | ||
} | ||
|
||
void CalculateGammaBackground::exec() | ||
{ | ||
retrieveInputs(); | ||
createOutputWorkspaces(); | ||
|
||
const size_t nhist = m_inputWS->getNumberHistograms(); | ||
for(size_t i = 0; i < nhist; ++i) | ||
{ | ||
applyCorrection(i); | ||
} | ||
|
||
setProperty("BackgroundWorkspace",m_backgroundWS); | ||
setProperty("CorrectedWorkspace",m_correctedWS); | ||
} | ||
|
||
/** | ||
* Caches input details for the peak information | ||
*/ | ||
void CalculateGammaBackground::retrieveInputs() | ||
{ | ||
m_inputWS = getProperty("InputWorkspace"); | ||
m_masses = getProperty("Masses"); | ||
m_amplitudes = getProperty("PeakAmplitudes"); | ||
m_widths = getProperty("PeakWidths"); | ||
m_npeaks = m_masses.size(); | ||
|
||
if(m_masses.size() != m_amplitudes.size() || m_masses.size() != m_widths.size()) | ||
{ | ||
std::ostringstream os; | ||
os << "Invalid peak inputs: No. of masses=" << m_masses.size() | ||
<< ", No. of amplitudes=" << m_amplitudes.size() << ", No. of peaks=" << m_widths.size(); | ||
throw std::invalid_argument(os.str()); | ||
} | ||
} | ||
|
||
/** | ||
* Create & cache output workspaces | ||
*/ | ||
void CalculateGammaBackground::createOutputWorkspaces() | ||
{ | ||
using namespace API; | ||
m_backgroundWS = WorkspaceFactory::Instance().create(m_inputWS); | ||
m_correctedWS = WorkspaceFactory::Instance().create(m_inputWS); | ||
} | ||
|
||
/** | ||
* Calculate & apply gamma correction for the given index of the | ||
* input workspace | ||
* @param index A workspace index that defines the spectrum to correct | ||
*/ | ||
void CalculateGammaBackground::applyCorrection(const size_t index) | ||
{ | ||
|
||
} | ||
|
||
} // namespace CurveFitting | ||
} // namespace Mantid |
95 changes: 95 additions & 0 deletions
95
Code/Mantid/Framework/CurveFitting/test/CalculateGammaBackgroundTest.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,95 @@ | ||
#ifndef MANTID_CURVEFITTING_CALCULATEGAMMABACKGROUNDTEST_H_ | ||
#define MANTID_CURVEFITTING_CALCULATEGAMMABACKGROUNDTEST_H_ | ||
|
||
#include <cxxtest/TestSuite.h> | ||
|
||
#include "MantidCurveFitting/CalculateGammaBackground.h" | ||
#include "ComptonProfileTestHelpers.h" | ||
|
||
using Mantid::CurveFitting::CalculateGammaBackground; | ||
|
||
class CalculateGammaBackgroundTest : 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 CalculateGammaBackgroundTest *createSuite() { return new CalculateGammaBackgroundTest(); } | ||
static void destroySuite( CalculateGammaBackgroundTest *suite ) { delete suite; } | ||
|
||
//------------------------------------ Success cases --------------------------------------- | ||
void test_Input_With_Single_Mass_Gives_Expected_Output_Workspaces() | ||
{ | ||
auto alg = createAlgorithm(); | ||
alg->setRethrows(true); | ||
|
||
double x0(165.0),x1(166.0),dx(0.5); | ||
alg->setProperty("InputWorkspace",ComptonProfileTestHelpers::createSingleSpectrumTestWorkspace(x0,x1,dx)); | ||
|
||
alg->setPropertyValue("Masses", "1.0079"); | ||
alg->setPropertyValue("PeakAmplitudes", "2.9e-2"); | ||
alg->setPropertyValue("PeakWidths", "4.29"); | ||
|
||
TS_ASSERT_THROWS_NOTHING(alg->execute()); | ||
TS_ASSERT(alg->isExecuted()); | ||
|
||
using namespace Mantid::API; | ||
MatrixWorkspace_sptr backgroundWS = alg->getProperty("BackgroundWorkspace"); | ||
MatrixWorkspace_sptr correctedWS = alg->getProperty("CorrectedWorkspace"); | ||
TS_ASSERT(backgroundWS); | ||
TS_ASSERT(correctedWS); | ||
TS_ASSERT(backgroundWS != correctedWS); | ||
} | ||
|
||
|
||
//------------------------------------ Error cases --------------------------------------- | ||
|
||
void test_Peak_Information_Lists_Of_Zero_Length_Throw_An_Error() | ||
{ | ||
auto alg = createAlgorithm(); | ||
alg->setRethrows(true); | ||
|
||
double x0(165.0),x1(166.0),dx(0.5); | ||
alg->setProperty("InputWorkspace",ComptonProfileTestHelpers::createSingleSpectrumTestWorkspace(x0,x1,dx)); | ||
|
||
// None set=all empty | ||
TS_ASSERT_THROWS(alg->execute(), std::runtime_error); | ||
// Just set masses | ||
alg->setPropertyValue("Masses", "1,2,3"); | ||
TS_ASSERT_THROWS(alg->execute(), std::runtime_error); | ||
// now amplitudes | ||
alg->setPropertyValue("PeakAmplitudes", "1,2,3"); | ||
TS_ASSERT_THROWS(alg->execute(), std::runtime_error); | ||
// now widths | ||
alg->setPropertyValue("PeakWidths", "1,2,3"); | ||
TS_ASSERT_THROWS_NOTHING(alg->execute()); | ||
} | ||
|
||
void test_Peak_Information_Lists_Of_Different_Lengths_Throws_Error() | ||
{ | ||
auto alg = createAlgorithm(); | ||
alg->setRethrows(true); | ||
double x0(165.0),x1(166.0),dx(0.5); | ||
alg->setProperty("InputWorkspace",ComptonProfileTestHelpers::createSingleSpectrumTestWorkspace(x0,x1,dx)); | ||
|
||
alg->setProperty("Masses", "1,2,3"); | ||
alg->setProperty("PeakAmplitudes", "1,2"); | ||
alg->setProperty("PeakWidths", "1,2,3,4"); | ||
TS_ASSERT_THROWS(alg->execute(), std::invalid_argument); | ||
} | ||
|
||
private: | ||
|
||
Mantid::API::IAlgorithm_sptr createAlgorithm() | ||
{ | ||
Mantid::API::IAlgorithm_sptr alg = boost::make_shared<CalculateGammaBackground>(); | ||
alg->initialize(); | ||
alg->setChild(true); | ||
alg->setPropertyValue("CorrectedWorkspace", "__UNUSED__"); | ||
alg->setPropertyValue("BackgroundWorkspace", "__UNUSED__"); | ||
return alg; | ||
} | ||
|
||
}; | ||
|
||
|
||
#endif /* MANTID_ALGORITHMS_CalculateGammaBackgroundTEST_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