Skip to content

Commit

Permalink
refs #9401. Create and Use base algorithm class
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenArnold committed May 13, 2014
1 parent 39aaa4f commit 90f91be
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 133 deletions.
2 changes: 2 additions & 0 deletions Code/Mantid/Framework/Algorithms/CMakeLists.txt
Expand Up @@ -204,6 +204,7 @@ set ( SRC_FILES
src/SolidAngle.cpp
src/SortEvents.cpp
src/SpatialGrouping.cpp
src/SpecularReflectionAlgorithm.cpp
src/SpecularReflectionPositionCorrect.cpp
src/SphericalAbsorption.cpp
src/StripPeaks.cpp
Expand Down Expand Up @@ -434,6 +435,7 @@ set ( INC_FILES
inc/MantidAlgorithms/SolidAngle.h
inc/MantidAlgorithms/SortEvents.h
inc/MantidAlgorithms/SpatialGrouping.h
inc/MantidAlgorithms/SpecularReflectionAlgorithm.h
inc/MantidAlgorithms/SpecularReflectionPositionCorrect.h
inc/MantidAlgorithms/SphericalAbsorption.h
inc/MantidAlgorithms/StripPeaks.h
Expand Down
@@ -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_ */
Expand Up @@ -2,11 +2,8 @@
#define MANTID_ALGORITHMS_SPECULARREFLECTIONPOSITIONCORRECT_H_

#include "MantidKernel/System.h"
#include "MantidAPI/Algorithm.h"
#include "MantidAlgorithms/SpecularReflectionAlgorithm.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidGeometry/Instrument.h"
#include "MantidGeometry/IComponent.h"
#include "MantidGeometry/IDetector.h"

namespace Mantid
{
Expand Down Expand Up @@ -35,7 +32,7 @@ namespace Mantid
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport SpecularReflectionPositionCorrect: public API::Algorithm
class DLLExport SpecularReflectionPositionCorrect: public SpecularReflectionAlgorithm
{
public:
SpecularReflectionPositionCorrect();
Expand All @@ -50,21 +47,10 @@ namespace Mantid
void init();
void exec();

/// 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);

/// Correct detector positions.
void correctPosition(API::MatrixWorkspace_sptr toCorrect, const double& twoThetaInDeg,
Geometry::IComponent_const_sptr sample, Geometry::IComponent_const_sptr detector);

/// Does the property have a default value.
bool isPropertyDefault(const std::string& propertyName) const;

/// Move detectors.
void moveDetectors(API::MatrixWorkspace_sptr toCorrect, Geometry::IComponent_const_sptr detector, Geometry::IComponent_const_sptr sample, const double& upOffset, const double& acrossOffset, const Mantid::Kernel::V3D& detectorPosition);
};
Expand Down
154 changes: 154 additions & 0 deletions Code/Mantid/Framework/Algorithms/src/SpecularReflectionAlgorithm.cpp
@@ -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

0 comments on commit 90f91be

Please sign in to comment.