Skip to content

Commit

Permalink
refs #8589. Move common toLam to base class.
Browse files Browse the repository at this point in the history
Move and make adjustments to ensure that everything is still working and that tests still pass.
  • Loading branch information
OwenArnold committed Dec 13, 2013
1 parent 8843ff2 commit fbb8721
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 204 deletions.
Expand Up @@ -7,9 +7,6 @@
#include "MantidGeometry/IComponent.h"
#include "MantidGeometry/IDetector.h"
#include "MantidAlgorithms/ReflectometryWorkflowBase.h"
#include <boost/optional.hpp>
#include <boost/tuple/tuple.hpp>
#include <vector>

namespace Mantid
{
Expand Down Expand Up @@ -42,14 +39,6 @@ namespace Mantid
{
public:

// Class typedefs
typedef boost::tuple<double, double> MinMax;
typedef boost::optional<double> OptionalDouble;
typedef boost::optional<Mantid::API::MatrixWorkspace_sptr> OptionalMatrixWorkspace_sptr;
typedef std::vector<int> WorkspaceIndexList;
typedef boost::optional<std::vector<int> > OptionalWorkspaceIndexes;
typedef boost::tuple<Mantid::API::MatrixWorkspace_sptr, Mantid::API::MatrixWorkspace_sptr> DetectorMonitorWorkspacePair;

/// Constructor
ReflectometryReductionOne();
/// Destructor
Expand All @@ -59,11 +48,6 @@ namespace Mantid
virtual int version() const;
virtual const std::string category() const;

/// Convert the input workspace to wavelength, splitting according to the properties provided.
DetectorMonitorWorkspacePair toLam(Mantid::API::MatrixWorkspace_sptr toConvert,
const WorkspaceIndexList& detectorIndexRange, const int monitorIndex,
const MinMax& wavelengthMinMax, const MinMax& backgroundMinMax, const double& wavelengthStep);

/// Convert to an IvsQ workspace. Performs detector positional corrections based on the component name and the theta value.
Mantid::API::MatrixWorkspace_sptr toIvsQ(API::MatrixWorkspace_sptr toConvert, const bool correctPosition,
OptionalDouble& thetaInDeg, Geometry::IComponent_const_sptr sample, Geometry::IComponent_const_sptr detector);
Expand Down Expand Up @@ -101,14 +85,6 @@ namespace Mantid

/** Algorithm running methods **/

/// Convert the monitor parts of the input workspace to wavelength
API::MatrixWorkspace_sptr toLamMonitor(const API::MatrixWorkspace_sptr& toConvert,
const int monitorIndex, const MinMax& backgroundMinMax);

/// Convert the detector spectrum of the input workspace to wavelength
API::MatrixWorkspace_sptr toLamDetector(const WorkspaceIndexList& detectorIndexRange,
const API::MatrixWorkspace_sptr& toConvert, const MinMax& wavelengthMinMax, const double& wavelengthStep);

/// Perform a transmission correction on the input IvsLam workspace
API::MatrixWorkspace_sptr transmissonCorrection(API::MatrixWorkspace_sptr IvsLam,
const MinMax& wavelengthInterval, const MinMax& wavelengthMonitorBackgroundInterval,
Expand Down
Expand Up @@ -4,48 +4,72 @@
#include "MantidKernel/System.h"
#include "MantidAPI/DataProcessorAlgorithm.h"

#include <boost/optional.hpp>
#include <boost/tuple/tuple.hpp>
#include <vector>

namespace Mantid
{
namespace Algorithms
{
namespace Algorithms
{

/** ReflectometryWorkflowBase : Abstract workflow algortithm base class containing common implementation functionality usable
* by concrete reflectometry workflow algorithms.
Copyright &copy; 2013 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
/** ReflectometryWorkflowBase : Abstract workflow algortithm base class containing common implementation functionality usable
* by concrete reflectometry workflow algorithms.
This file is part of Mantid.
Copyright &copy; 2013 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
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.
This file is part of Mantid.
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.
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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
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.
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport ReflectometryWorkflowBase : public API::DataProcessorAlgorithm
{
public:
ReflectometryWorkflowBase();
virtual ~ReflectometryWorkflowBase();

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 ReflectometryWorkflowBase: public API::DataProcessorAlgorithm
{
public:

// Class typedefs
typedef boost::tuple<double, double> MinMax;
typedef boost::optional<double> OptionalDouble;
typedef boost::optional<Mantid::API::MatrixWorkspace_sptr> OptionalMatrixWorkspace_sptr;
typedef std::vector<int> WorkspaceIndexList;
typedef boost::optional<std::vector<int> > OptionalWorkspaceIndexes;
typedef boost::tuple<Mantid::API::MatrixWorkspace_sptr, Mantid::API::MatrixWorkspace_sptr> DetectorMonitorWorkspacePair;

ReflectometryWorkflowBase();
virtual ~ReflectometryWorkflowBase();

/// Convert the input workspace to wavelength, splitting according to the properties provided.
DetectorMonitorWorkspacePair toLam(Mantid::API::MatrixWorkspace_sptr toConvert,
const WorkspaceIndexList& detectorIndexRange, const int monitorIndex,
const MinMax& wavelengthMinMax, const MinMax& backgroundMinMax, const double& wavelengthStep);

//protected:
private:

/// Convert the monitor parts of the input workspace to wavelength
API::MatrixWorkspace_sptr toLamMonitor(const API::MatrixWorkspace_sptr& toConvert,
const int monitorIndex, const MinMax& backgroundMinMax);

};
/// Convert the detector spectrum of the input workspace to wavelength
API::MatrixWorkspace_sptr toLamDetector(const WorkspaceIndexList& detectorIndexRange,
const API::MatrixWorkspace_sptr& toConvert, const MinMax& wavelengthMinMax,
const double& wavelengthStep);

};

} // namespace Algorithms
} // namespace Algorithms
} // namespace Mantid

#endif /* MANTID_ALGORITHMS_REFLECTOMETRYWORKFLOWBASE_H_ */
151 changes: 5 additions & 146 deletions Code/Mantid/Framework/Algorithms/src/ReflectometryReductionOne.cpp
Expand Up @@ -58,11 +58,11 @@ namespace Mantid
* @param hostWS : Workspace onto which the resulting workspace indexes will be hosted
* @return Remapped wokspace indexes applicable for the host workspace.
*/
ReflectometryReductionOne::WorkspaceIndexList createWorkspaceIndexListFromDetectorWorkspace(
ReflectometryWorkflowBase::WorkspaceIndexList createWorkspaceIndexListFromDetectorWorkspace(
MatrixWorkspace_const_sptr originWS, MatrixWorkspace_const_sptr hostWS)
{
auto spectrumMap = originWS->getSpectrumToWorkspaceIndexMap();
ReflectometryReductionOne::WorkspaceIndexList translatedIndexList;
ReflectometryWorkflowBase::WorkspaceIndexList translatedIndexList;
for (auto it = spectrumMap.begin(); it != spectrumMap.end(); ++it)
{
specid_t specId = (*it).first;
Expand All @@ -81,7 +81,7 @@ namespace Mantid
* @param originIndexes : Indexes in terms of the origin workspace
* @return WorkspaceIndexes in terms of the host workspace
*/
ReflectometryReductionOne::WorkspaceIndexList getIndexesInTermsOf(
ReflectometryWorkflowBase::WorkspaceIndexList getIndexesInTermsOf(
MatrixWorkspace_const_sptr hostWS, MatrixWorkspace_sptr originWS,
const ReflectometryReductionOne::WorkspaceIndexList& originIndexes)
{
Expand Down Expand Up @@ -306,7 +306,7 @@ namespace Mantid
* Get the workspace index list
* @return Workspace index list.
*/
ReflectometryReductionOne::WorkspaceIndexList ReflectometryReductionOne::getWorkspaceIndexList() const
ReflectometryWorkflowBase::WorkspaceIndexList ReflectometryReductionOne::getWorkspaceIndexList() const
{
WorkspaceIndexList indexList = getProperty("WorkspaceIndexList");
if (indexList.size() % 2 != 0 || indexList.size() == 0)
Expand Down Expand Up @@ -372,7 +372,7 @@ namespace Mantid
* @param maxProperty : Property name for the max property
* @return A tuple consisting of min, max
*/
ReflectometryReductionOne::MinMax ReflectometryReductionOne::getMinMax(
ReflectometryWorkflowBase::MinMax ReflectometryReductionOne::getMinMax(
const std::string& minProperty, const std::string& maxProperty) const
{
const double min = getProperty(minProperty);
Expand Down Expand Up @@ -476,147 +476,6 @@ namespace Mantid

}


/**
* Convert the TOF workspace into a monitor workspace. Crops to the monitorIndex and applying flat background correction as part of the process.
* @param toConvert : TOF wavlength to convert.
* @param monitorIndex : Monitor index to crop to
* @param backgroundMinMax : Min and Max Lambda range for Flat background correction.
* @return The cropped and corrected monitor workspace.
*/
MatrixWorkspace_sptr ReflectometryReductionOne::toLamMonitor(const MatrixWorkspace_sptr& toConvert, const int monitorIndex, const MinMax& backgroundMinMax)
{
// Convert Units.
auto convertUnitsAlg = this->createChildAlgorithm("ConvertUnits");
convertUnitsAlg->initialize();
convertUnitsAlg->setProperty("InputWorkspace", toConvert);
convertUnitsAlg->setProperty("Target", "Wavelength");
convertUnitsAlg->setProperty("AlignBins", true);
convertUnitsAlg->execute();

// Crop the to the monitor index.
MatrixWorkspace_sptr monitorWS = convertUnitsAlg->getProperty("OutputWorkspace");
auto cropWorkspaceAlg = this->createChildAlgorithm("CropWorkspace");
cropWorkspaceAlg->initialize();
cropWorkspaceAlg->setProperty("InputWorkspace", monitorWS);
cropWorkspaceAlg->setProperty("StartWorkspaceIndex", monitorIndex);
cropWorkspaceAlg->setProperty("EndWorkspaceIndex", monitorIndex);
cropWorkspaceAlg->execute();
monitorWS = cropWorkspaceAlg->getProperty("OutputWorkspace");

// Flat background correction
auto correctMonitorsAlg = this->createChildAlgorithm("CalculateFlatBackground");
correctMonitorsAlg->initialize();
correctMonitorsAlg->setProperty("InputWorkspace", monitorWS);
correctMonitorsAlg->setProperty("WorkspaceIndexList",
boost::assign::list_of(0).convert_to_container<std::vector<int> >());
correctMonitorsAlg->setProperty("StartX", backgroundMinMax.get<0>());
correctMonitorsAlg->setProperty("EndX", backgroundMinMax.get<1>());
correctMonitorsAlg->execute();
monitorWS = correctMonitorsAlg->getProperty("OutputWorkspace");

return monitorWS;
}

/**
* Convert to a detector workspace in lambda.
* @param detectorIndexRange : Workspace index ranges to keep
* @param toConvert : TOF wavelength to convert.
* @param wavelengthMinMax : Wavelength minmax to keep. Crop out the rest.
* @param wavelengthStep : Wavelength step for rebinning
* @return Detector workspace in wavelength
*/
MatrixWorkspace_sptr ReflectometryReductionOne::toLamDetector(
const WorkspaceIndexList& detectorIndexRange, const MatrixWorkspace_sptr& toConvert,
const MinMax& wavelengthMinMax, const double& wavelengthStep)
{
// Detector Workspace Processing
MatrixWorkspace_sptr detectorWS;

// Loop over pairs of detector index ranges. Peform the cropping and then conjoin the results into a single workspace.
for (size_t i = 0; i < detectorIndexRange.size(); i += 2)
{
auto cropWorkspaceAlg = this->createChildAlgorithm("CropWorkspace");
cropWorkspaceAlg->initialize();
cropWorkspaceAlg->setProperty("InputWorkspace", toConvert);
cropWorkspaceAlg->setProperty("StartWorkspaceIndex", detectorIndexRange[i]);
cropWorkspaceAlg->setProperty("EndWorkspaceIndex", detectorIndexRange[i + 1]);
cropWorkspaceAlg->execute();
MatrixWorkspace_sptr subRange = cropWorkspaceAlg->getProperty("OutputWorkspace");
if (i == 0)
{
detectorWS = subRange;
}
else
{
auto conjoinWorkspaceAlg = this->createChildAlgorithm("ConjoinWorkspaces");
conjoinWorkspaceAlg->initialize();
conjoinWorkspaceAlg->setProperty("InputWorkspace1", detectorWS);
conjoinWorkspaceAlg->setProperty("InputWorkspace2", subRange);
conjoinWorkspaceAlg->execute();
detectorWS = conjoinWorkspaceAlg->getProperty("InputWorkspace1");
}
}
// Now convert units. Do this after the conjoining step otherwise the x bins will not match up.
auto convertUnitsAlg = this->createChildAlgorithm("ConvertUnits");
convertUnitsAlg->initialize();
convertUnitsAlg->setProperty("InputWorkspace", detectorWS);
convertUnitsAlg->setProperty("Target", "Wavelength");
convertUnitsAlg->setProperty("AlignBins", true);
convertUnitsAlg->execute();
detectorWS = convertUnitsAlg->getProperty("OutputWorkspace");

// Crop out the lambda x-ranges now that the workspace is in wavelength.
auto cropWorkspaceAlg = this->createChildAlgorithm("CropWorkspace");
cropWorkspaceAlg->initialize();
cropWorkspaceAlg->setProperty("InputWorkspace", detectorWS);
cropWorkspaceAlg->setProperty("XMin", wavelengthMinMax.get<0>());
cropWorkspaceAlg->setProperty("XMax", wavelengthMinMax.get<1>());
cropWorkspaceAlg->execute();
detectorWS = cropWorkspaceAlg->getProperty("OutputWorkspace");

auto rebinWorkspaceAlg = this->createChildAlgorithm("Rebin");
rebinWorkspaceAlg->initialize();
std::vector<double> params = boost::assign::list_of(wavelengthStep);
rebinWorkspaceAlg->setProperty("Params", params);
rebinWorkspaceAlg->setProperty("InputWorkspace", detectorWS);
rebinWorkspaceAlg->execute();
detectorWS = rebinWorkspaceAlg->getProperty("OutputWorkspace");

return detectorWS;
}

/**
* Convert From a TOF workspace into a detector and monitor workspace both in Lambda.
* @param toConvert: TOF workspace to convert
* @param detectorIndexRange : Detector index ranges
* @param monitorIndex : Monitor index
* @param wavelengthMinMax : Wavelength min max for detector workspace
* @param backgroundMinMax : Wavelength min max for flat background correction of monitor workspace
* @param wavelengthStep : Wavlength step size for rebinning.
* @return Tuple of detector and monitor workspaces
*/
ReflectometryReductionOne::DetectorMonitorWorkspacePair ReflectometryReductionOne::toLam(MatrixWorkspace_sptr toConvert,
const WorkspaceIndexList& detectorIndexRange, const int monitorIndex,
const MinMax& wavelengthMinMax, const MinMax& backgroundMinMax, const double& wavelengthStep)
{
// Detector Workspace Processing
MatrixWorkspace_sptr detectorWS = toLamDetector(detectorIndexRange, toConvert, wavelengthMinMax, wavelengthStep);

// Monitor Workspace Processing
MatrixWorkspace_sptr monitorWS = toLamMonitor(toConvert, monitorIndex, backgroundMinMax);

// Rebin the Monitor Workspace to match the Detector Workspace.
auto rebinToWorkspaceAlg = this->createChildAlgorithm("RebinToWorkspace");
rebinToWorkspaceAlg->initialize();
rebinToWorkspaceAlg->setProperty("WorkspaceToRebin", monitorWS);
rebinToWorkspaceAlg->setProperty("WorkspaceToMatch", detectorWS);
rebinToWorkspaceAlg->execute();
monitorWS = rebinToWorkspaceAlg->getProperty("OutputWorkspace");

return DetectorMonitorWorkspacePair( detectorWS, monitorWS );
}

/**
* Perform Transmission Corrections.
* @param IvsLam : Run workspace which is to be normalized by the results of the transmission corrections.
Expand Down

0 comments on commit fbb8721

Please sign in to comment.