Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refs #9401. Create and Use base algorithm class
- Loading branch information
1 parent
39aaa4f
commit 90f91be
Showing
5 changed files
with
223 additions
and
133 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
65 changes: 65 additions & 0 deletions
65
Code/Mantid/Framework/Algorithms/inc/MantidAlgorithms/SpecularReflectionAlgorithm.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,65 @@ | ||
#ifndef MANTID_ALGORITHMS_SPECULARREFLECTIONALGORITHM_H_ | ||
#define MANTID_ALGORITHMS_SPECULARREFLECTIONALGORITHM_H_ | ||
|
||
#include "MantidKernel/System.h" | ||
#include "MantidAPI/Algorithm.h" | ||
#include "MantidGeometry/Instrument.h" | ||
#include "MantidGeometry/IComponent.h" | ||
|
||
namespace Mantid | ||
{ | ||
namespace Algorithms | ||
{ | ||
|
||
/** SpecularReflectionAlgorithm : Algorithm base class implementing generic methods required for specular reflection calculations. | ||
* | ||
Copyright © 2014 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 SpecularReflectionAlgorithm: public Mantid::API::Algorithm | ||
{ | ||
protected: | ||
|
||
/// Constructor | ||
SpecularReflectionAlgorithm(); | ||
|
||
/// Get the surface sample component | ||
Mantid::Geometry::IComponent_const_sptr getSurfaceSampleComponent( | ||
Mantid::Geometry::Instrument_const_sptr inst); | ||
|
||
/// Get the detector component | ||
Mantid::Geometry::IComponent_const_sptr getDetectorComponent( | ||
Mantid::API::MatrixWorkspace_sptr workspace, const bool isPointDetector); | ||
|
||
/// Does the property have a default value. | ||
bool isPropertyDefault(const std::string& propertyName) const; | ||
|
||
public: | ||
|
||
/// Destructor | ||
virtual ~SpecularReflectionAlgorithm()=0; | ||
|
||
}; | ||
|
||
} // namespace Algorithms | ||
} // namespace Mantid | ||
|
||
#endif /* MANTID_ALGORITHMS_SPECULARREFLECTIONALGORITHM_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
154 changes: 154 additions & 0 deletions
154
Code/Mantid/Framework/Algorithms/src/SpecularReflectionAlgorithm.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,154 @@ | ||
#include "MantidAlgorithms/SpecularReflectionAlgorithm.h" | ||
|
||
#include "MantidGeometry/Instrument/DetectorGroup.h" | ||
#include "MantidGeometry/Instrument/ReferenceFrame.h" | ||
#include "MantidGeometry/Instrument/ComponentHelper.h" | ||
|
||
using namespace Mantid::Kernel; | ||
using namespace Mantid::Geometry; | ||
using namespace Mantid::API; | ||
|
||
namespace | ||
{ | ||
void checkSpectrumNumbers(const std::vector<int>& spectrumNumbers, bool strictSpectrumChecking, | ||
Logger& logger) | ||
{ | ||
std::set<int> uniqueSpectrumNumbers(spectrumNumbers.begin(), spectrumNumbers.end()); | ||
if (uniqueSpectrumNumbers.size() != spectrumNumbers.size()) | ||
{ | ||
throw std::invalid_argument("Spectrum numbers are not unique."); | ||
} | ||
auto maxIt = std::max_element(uniqueSpectrumNumbers.begin(), uniqueSpectrumNumbers.end()); | ||
auto minIt = std::min_element(uniqueSpectrumNumbers.begin(), uniqueSpectrumNumbers.end()); | ||
const int max = *maxIt; | ||
const int min = *minIt; | ||
for (int i = min; i <= max; ++i) | ||
{ | ||
if (uniqueSpectrumNumbers.find(i) == uniqueSpectrumNumbers.end()) | ||
{ | ||
const std::string message = "Spectrum numbers are not a contiguous linear sequence."; | ||
if (strictSpectrumChecking) | ||
{ | ||
throw std::invalid_argument(message); | ||
} | ||
else | ||
{ | ||
logger.warning(message); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
namespace Mantid | ||
{ | ||
namespace Algorithms | ||
{ | ||
|
||
//---------------------------------------------------------------------------------------------- | ||
/** Constructor | ||
*/ | ||
SpecularReflectionAlgorithm::SpecularReflectionAlgorithm() | ||
{ | ||
} | ||
|
||
//---------------------------------------------------------------------------------------------- | ||
/** Destructor | ||
*/ | ||
SpecularReflectionAlgorithm::~SpecularReflectionAlgorithm() | ||
{ | ||
} | ||
|
||
/** | ||
* Get the sample component. Use the name provided as a property as the basis for the lookup as a priority. | ||
* | ||
* Throws if the name is invalid. | ||
* @param inst : Instrument to search through | ||
* @return : The component : The component object found. | ||
*/ | ||
Mantid::Geometry::IComponent_const_sptr SpecularReflectionAlgorithm::getSurfaceSampleComponent( | ||
Mantid::Geometry::Instrument_const_sptr inst) | ||
{ | ||
std::string sampleComponent = "some-surface-holder"; | ||
if (!isPropertyDefault("SampleComponentName")) | ||
{ | ||
sampleComponent = this->getPropertyValue("SampleComponentName"); | ||
} | ||
auto searchResult = inst->getComponentByName(sampleComponent); | ||
if (searchResult == NULL) | ||
{ | ||
throw std::invalid_argument(sampleComponent + " does not exist. Check input properties."); | ||
} | ||
return searchResult; | ||
} | ||
|
||
/** | ||
* Get the detector component. Use the name provided as a property as the basis for the lookup as a priority. | ||
* | ||
* Throws if the name is invalid. | ||
* @param workspace : Workspace from instrument with detectors | ||
* @param isPointDetector : True if this is a point detector. Used to guess a name. | ||
* @return The component : The component object found. | ||
*/ | ||
boost::shared_ptr<const Mantid::Geometry::IComponent> SpecularReflectionAlgorithm::getDetectorComponent( | ||
MatrixWorkspace_sptr workspace, const bool isPointDetector) | ||
{ | ||
boost::shared_ptr<const IComponent> searchResult; | ||
if (!isPropertyDefault("SpectrumNumbersOfDetectors")) | ||
{ | ||
const std::vector<int> spectrumNumbers = this->getProperty("SpectrumNumbersOfDetectors"); | ||
const bool strictSpectrumChecking = this->getProperty("StrictSpectrumChecking"); | ||
checkSpectrumNumbers(spectrumNumbers, strictSpectrumChecking, g_log); | ||
auto specToWorkspaceIndex = workspace->getSpectrumToWorkspaceIndexMap(); | ||
DetectorGroup_sptr allDetectors = boost::make_shared<DetectorGroup>(); | ||
bool warnIfMasked = true; | ||
for (size_t i = 0; i < spectrumNumbers.size(); ++i) | ||
{ | ||
const size_t& spectrumNumber = spectrumNumbers[i]; | ||
auto it = specToWorkspaceIndex.find(spectrumNumbers[i]); | ||
if (it == specToWorkspaceIndex.end()) | ||
{ | ||
std::stringstream message; | ||
message << "Spectrum number " << spectrumNumber << " does not exist in the InputWorkspace"; | ||
throw std::invalid_argument(message.str()); | ||
} | ||
const size_t workspaceIndex = it->second; | ||
auto detector = workspace->getDetector(workspaceIndex); | ||
allDetectors->addDetector(detector, warnIfMasked); | ||
} | ||
searchResult = allDetectors; | ||
} | ||
else | ||
{ | ||
Mantid::Geometry::Instrument_const_sptr inst = workspace->getInstrument(); | ||
std::string componentToCorrect = isPointDetector ? "point-detector" : "linedetector"; | ||
|
||
if (!isPropertyDefault("DetectorComponentName")) | ||
{ | ||
componentToCorrect = this->getPropertyValue("DetectorComponentName"); | ||
|
||
} | ||
searchResult = inst->getComponentByName(componentToCorrect); | ||
if (searchResult == NULL) | ||
{ | ||
throw std::invalid_argument(componentToCorrect + " does not exist. Check input properties."); | ||
} | ||
} | ||
|
||
return searchResult; | ||
} | ||
|
||
/** | ||
* Determine if the property value is the same as the default value. | ||
* This can be used to determine if the property has not been set. | ||
* @param propertyName : Name of property to query | ||
* @return: True only if the property has it's default value. | ||
*/ | ||
bool SpecularReflectionAlgorithm::isPropertyDefault(const std::string& propertyName) const | ||
{ | ||
Property* property = this->getProperty(propertyName); | ||
return property->isDefault(); | ||
} | ||
|
||
} // namespace Algorithms | ||
} // namespace Mantid |
Oops, something went wrong.