Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs #9531. Added SeqDomainSpectrumCreator with test.
- Loading branch information
Michael Wedel
committed
May 30, 2014
1 parent
ea35aea
commit 9a354d7
Showing
4 changed files
with
381 additions
and
0 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
78 changes: 78 additions & 0 deletions
78
Code/Mantid/Framework/CurveFitting/inc/MantidCurveFitting/SeqDomainSpectrumCreator.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,78 @@ | ||
#ifndef MANTID_CURVEFITTING_SEQDOMAINSPECTRUMCREATOR_H_ | ||
#define MANTID_CURVEFITTING_SEQDOMAINSPECTRUMCREATOR_H_ | ||
|
||
#include "MantidKernel/System.h" | ||
#include "MantidAPI/IDomainCreator.h" | ||
#include "MantidAPI/FunctionDomain1D.h" | ||
#include "MantidAPI/MatrixWorkspace.h" | ||
|
||
namespace Mantid | ||
{ | ||
namespace CurveFitting | ||
{ | ||
|
||
/** SeqDomainSpectrumCreator : | ||
SeqDomainSpectrumCreator creates a special type of SeqDomain, which contains | ||
one FunctionDomain1DSpectrum for each histogram of the Workspace2D this | ||
domain refers to. It can be used for functions that involve several histograms | ||
at once. | ||
@author Michael Wedel, Paul Scherrer Institut - SINQ | ||
@date 28/05/2014 | ||
Copyright © 2014 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> | ||
*/ | ||
|
||
using namespace API; | ||
|
||
class DLLExport SeqDomainSpectrumCreator : public IDomainCreator | ||
{ | ||
public: | ||
SeqDomainSpectrumCreator(Kernel::IPropertyManager* manager, | ||
const std::string& workspacePropertyName); | ||
|
||
virtual ~SeqDomainSpectrumCreator() { } | ||
|
||
virtual void createDomain(boost::shared_ptr<FunctionDomain> &domain, | ||
boost::shared_ptr<FunctionValues> &values, | ||
size_t i0 = 0); | ||
|
||
virtual Workspace_sptr createOutputWorkspace(const std::string &baseName, | ||
IFunction_sptr function, | ||
boost::shared_ptr<FunctionDomain> domain, | ||
boost::shared_ptr<FunctionValues> values, | ||
const std::string &outputWorkspacePropertyName = "OutputWorkspace"); | ||
virtual size_t getDomainSize() const; | ||
|
||
protected: | ||
void setParametersFromPropertyManager(); | ||
void setMatrixWorkspace(MatrixWorkspace_sptr matrixWorkspace); | ||
|
||
std::string m_workspacePropertyName; | ||
MatrixWorkspace_sptr m_matrixWorkspace; | ||
}; | ||
|
||
|
||
} // namespace CurveFitting | ||
} // namespace Mantid | ||
|
||
#endif /* MANTID_CURVEFITTING_SEQDOMAINSPECTRUMCREATOR_H_ */ |
128 changes: 128 additions & 0 deletions
128
Code/Mantid/Framework/CurveFitting/src/SeqDomainSpectrumCreator.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,128 @@ | ||
#include "MantidCurveFitting/SeqDomainSpectrumCreator.h" | ||
#include "MantidAPI/Workspace.h" | ||
#include "MantidCurveFitting/SeqDomain.h" | ||
#include "MantidCurveFitting/FunctionDomain1DSpectrumCreator.h" | ||
#include "MantidAPI/WorkspaceFactory.h" | ||
#include "MantidCurveFitting/Jacobian.h" | ||
#include "MantidKernel/Matrix.h" | ||
#include "MantidAPI/WorkspaceProperty.h" | ||
|
||
namespace Mantid | ||
{ | ||
namespace CurveFitting | ||
{ | ||
|
||
using namespace API; | ||
|
||
SeqDomainSpectrumCreator::SeqDomainSpectrumCreator(Kernel::IPropertyManager *manager, const std::string &workspacePropertyName) : | ||
IDomainCreator(manager, std::vector<std::string>(1, workspacePropertyName), SeqDomainSpectrumCreator::Sequential), | ||
m_matrixWorkspace() | ||
{ | ||
m_workspacePropertyName = m_workspacePropertyNames.front(); | ||
} | ||
|
||
void SeqDomainSpectrumCreator::createDomain(boost::shared_ptr<FunctionDomain> &domain, boost::shared_ptr<FunctionValues> &values, size_t i0) | ||
{ | ||
setParametersFromPropertyManager(); | ||
|
||
if(!m_matrixWorkspace) { | ||
throw std::invalid_argument("No matrix workspace assigned - can not create domain."); | ||
} | ||
|
||
SeqDomain *seqDomain = SeqDomain::create(m_domainType); | ||
|
||
size_t numberOfHistograms = m_matrixWorkspace->getNumberHistograms(); | ||
for(size_t i = 0; i < numberOfHistograms; ++i) { | ||
FunctionDomain1DSpectrumCreator *spectrumDomain = new FunctionDomain1DSpectrumCreator; | ||
spectrumDomain->setMatrixWorkspace(m_matrixWorkspace); | ||
spectrumDomain->setWorkspaceIndex(i); | ||
|
||
seqDomain->addCreator(IDomainCreator_sptr(spectrumDomain)); | ||
} | ||
|
||
domain.reset(seqDomain); | ||
|
||
if(!values) { | ||
values.reset(new FunctionValues(*domain)); | ||
} else { | ||
values->expand(i0 + domain->size()); | ||
} | ||
|
||
} | ||
|
||
Workspace_sptr SeqDomainSpectrumCreator::createOutputWorkspace(const std::string &baseName, IFunction_sptr function, boost::shared_ptr<FunctionDomain> domain, boost::shared_ptr<FunctionValues> values, const std::string &outputWorkspacePropertyName) | ||
{ | ||
boost::shared_ptr<SeqDomain> seqDomain = boost::dynamic_pointer_cast<SeqDomain>(domain); | ||
|
||
if(!seqDomain) { | ||
throw std::invalid_argument("CreateOutputWorkspace requires SeqDomain."); | ||
} | ||
|
||
MatrixWorkspace_sptr outputWs = boost::dynamic_pointer_cast<MatrixWorkspace>(WorkspaceFactory::Instance().create(m_matrixWorkspace)); | ||
|
||
for(size_t i = 0; i < seqDomain->getNDomains(); ++i) { | ||
FunctionDomain_sptr localDomain; | ||
FunctionValues_sptr localValues; | ||
|
||
seqDomain->getDomainAndValues(i, localDomain, localValues); | ||
function->function(*localDomain, *localValues); | ||
|
||
const MantidVec& originalXValue = m_matrixWorkspace->readX(i); | ||
MantidVec& xValues = outputWs->dataX(i); | ||
MantidVec& yValues = outputWs->dataY(i); | ||
|
||
for(size_t j = 0; j < yValues.size(); ++j) { | ||
xValues[j] = originalXValue[j]; | ||
yValues[j] = localValues->getCalculated(j); | ||
} | ||
} | ||
|
||
if(m_manager && !outputWorkspacePropertyName.empty()) { | ||
declareProperty(new WorkspaceProperty<MatrixWorkspace>(outputWorkspacePropertyName, "", Kernel::Direction::Output), | ||
"Result workspace"); | ||
|
||
m_manager->setPropertyValue(outputWorkspacePropertyName, baseName + "Workspace"); | ||
m_manager->setProperty(outputWorkspacePropertyName, outputWs); | ||
} | ||
|
||
return outputWs; | ||
} | ||
|
||
size_t SeqDomainSpectrumCreator::getDomainSize() const | ||
{ | ||
if(!m_matrixWorkspace) { | ||
throw std::invalid_argument("No matrix workspace assigned."); | ||
} | ||
|
||
size_t nHist = m_matrixWorkspace->getNumberHistograms(); | ||
size_t totalSize = 0; | ||
|
||
for(size_t i = 0; i < nHist; ++i) { | ||
totalSize += m_matrixWorkspace->dataY(i).size(); | ||
} | ||
|
||
return totalSize; | ||
} | ||
|
||
void SeqDomainSpectrumCreator::setParametersFromPropertyManager() | ||
{ | ||
if(m_manager) { | ||
Workspace_sptr workspace = m_manager->getProperty(m_workspacePropertyName); | ||
|
||
setMatrixWorkspace(boost::dynamic_pointer_cast<MatrixWorkspace>(workspace)); | ||
} | ||
} | ||
|
||
void SeqDomainSpectrumCreator::setMatrixWorkspace(MatrixWorkspace_sptr matrixWorkspace) | ||
{ | ||
if(!matrixWorkspace) { | ||
throw std::invalid_argument("InputWorkspace must be a valid MatrixWorkspace."); | ||
} | ||
|
||
m_matrixWorkspace = matrixWorkspace; | ||
} | ||
|
||
|
||
|
||
} // namespace CurveFitting | ||
} // namespace Mantid |
Oops, something went wrong.