Skip to content

Commit

Permalink
Refactoring to avoid code duplication.
Browse files Browse the repository at this point in the history
* getComponentParameter is now only defined in ConvertToYSpace
* DetectorParams struct has moved to ConvertToYSpace
* Y calculation has moved to ConvertToYSpace

Refs #8598
  • Loading branch information
martyngigg committed Dec 16, 2013
1 parent f7b330b commit 06606cc
Show file tree
Hide file tree
Showing 12 changed files with 227 additions and 158 deletions.
Expand Up @@ -3,14 +3,17 @@

#include "MantidAPI/Algorithm.h"

#include "MantidCurveFitting/ComptonProfile.h"

#include <boost/unordered_map.hpp>

namespace Mantid
{
namespace CurveFitting
{
//---------------------------------------------------------------------------
// Forward declarations
//---------------------------------------------------------------------------
struct DetectorParams;
struct ResolutionParams;

/**
Expand Down
Expand Up @@ -10,17 +10,12 @@ namespace Mantid
{
namespace CurveFitting
{
/// Simple data structure to store nominal detector values
/// It avoids some functions taking a huge number of arguments
struct DetectorParams
{
double l1; ///< source-sample distance in metres
double l2; ///< sample-detector distance in metres
double theta; ///< scattering angle in radians
double t0; ///< time delay in microseconds
double efixed; ///< final energy
};
//---------------------------------------------------------------------------
// Forward declarations
//---------------------------------------------------------------------------
struct DetectorParams;

//---------------------------------------------------------------------------
/// Simple data structure to store resolution parameter values
/// It avoids some functions taking a huge number of arguments
struct ResolutionParams
Expand Down Expand Up @@ -58,10 +53,6 @@ namespace CurveFitting
class MANTID_CURVEFITTING_DLL ComptonProfile : public virtual API::ParamFunction, public virtual API::IFunction1D
{
public:
/// Retrieve a component parameter
static double getComponentParameter(const Geometry::IComponent_const_sptr & comp, const Geometry::ParameterMap &pmap,
const std::string &name);

/// Default constructor required for factory
ComptonProfile();

Expand Down Expand Up @@ -125,28 +116,6 @@ namespace CurveFitting
/// Store the mass values
double m_mass;

/// Source to sample distance
double m_l1;
/// Std dev of l1 distance
double m_sigmaL1;
/// Sample to detector distance
double m_l2;
/// Std dev of l1 distance
double m_sigmaL2;
/// Theta value for this spectrum
double m_theta;
/// Std Dev theta value for this spectrum
double m_sigmaTheta;

/// Final energy
double m_e1;
/// T0 value for this spectrum in seconds
double m_t0;
/// Lorentzian HWHM of the foil analyser energy
double m_hwhmGaussE;
/// Gaussian HWHM of the foil analyser energy
double m_hwhmLorentzE;

/// Voigt function
boost::shared_ptr<API::IPeakFunction> m_voigt;

Expand Down
Expand Up @@ -67,9 +67,9 @@ namespace CurveFitting
void cacheBackground(const API::IFunction1D_sptr & profile,
const size_t paramsOffset);
/// Set up the constraint matrices
void createConstraintMatrices(const MantidVec & xValues);
void createConstraintMatrices(const std::vector<double> & xValues);
/// Set up positivity constraint matrix
void createPositivityCM(const MantidVec & xValues);
void createPositivityCM(const std::vector<double> & xValues);
/// Set up equality constraint matrix
void createEqualityCM(const size_t nmasses);

Expand Down
Expand Up @@ -7,10 +7,17 @@ namespace Mantid
{
namespace CurveFitting
{
//---------------------------------------------------------------------------
// Forward declarations
//---------------------------------------------------------------------------
struct DetectorParams;

/// Simple data structure to store nominal detector values
/// It avoids some functions taking a huge number of arguments
struct DetectorParams
{
double l1; ///< source-sample distance in metres
double l2; ///< sample-detector distance in metres
double theta; ///< scattering angle in radians
double t0; ///< time delay in microseconds
double efixed; ///< final energy
};

/**
Takes a workspace with X axis in TOF and converts it to Y-space where the transformation is defined
Expand Down Expand Up @@ -46,6 +53,10 @@ namespace CurveFitting
int version() const;
const std::string category() const;

/// Retrieve a component parameter
static double getComponentParameter(const Geometry::IComponent_const_sptr & comp,
const Geometry::ParameterMap &pmap,
const std::string &name);
/// Convert single time value to Y,Q & Ei values
static void calculateY(double & yspace, double & qspace, double &ei,
const double mass, const double tmicro,
Expand All @@ -57,6 +68,8 @@ namespace CurveFitting
void init();
void exec();

/// Perform the conversion to Y-space
void convert(const size_t i);
/// Check and store appropriate input data
void retrieveInputs();
/// Create the output workspace
Expand All @@ -66,6 +79,8 @@ namespace CurveFitting

/// Input workspace
API::MatrixWorkspace_sptr m_inputWS;
/// The input mass in AMU
double m_mass;
/// Source-sample distance
double m_l1;
/// Sample position
Expand Down
38 changes: 20 additions & 18 deletions Code/Mantid/Framework/CurveFitting/src/CalculateGammaBackground.cpp
Expand Up @@ -4,6 +4,8 @@
*WIKI*/
#include "MantidCurveFitting/CalculateGammaBackground.h"
#include "MantidCurveFitting/ComptonProfile.h"
#include "MantidCurveFitting/ConvertToYSpace.h"

#include "MantidAPI/CompositeFunction.h"
#include "MantidAPI/FunctionProperty.h"
Expand Down Expand Up @@ -226,15 +228,15 @@ namespace Mantid
detPar.l1 = m_l1;
detPar.l2 = m_samplePos.distance(detPos);
detPar.theta = m_inputWS->detectorTwoTheta(det); //radians
detPar.t0 = ComptonProfile::getComponentParameter(det, pmap, "t0")*1e-6; // seconds
detPar.efixed = ComptonProfile::getComponentParameter(det, pmap,"efixed");
detPar.t0 = ConvertToYSpace::getComponentParameter(det, pmap, "t0")*1e-6; // seconds
detPar.efixed = ConvertToYSpace::getComponentParameter(det, pmap,"efixed");

ResolutionParams detRes;
detRes.dl1 = ComptonProfile::getComponentParameter(det, pmap, "sigma_l1"); // DL0
detRes.dl2 = ComptonProfile::getComponentParameter(det, pmap, "sigma_l2"); // DL1
detRes.dthe = ComptonProfile::getComponentParameter(det, pmap, "sigma_theta"); //DTH in radians
detRes.dEnGauss = ComptonProfile::getComponentParameter(det, pmap, "sigma_gauss");
detRes.dEnLorentz = ComptonProfile::getComponentParameter(det, pmap, "hwhm_lorentz");
detRes.dl1 = ConvertToYSpace::getComponentParameter(det, pmap, "sigma_l1"); // DL0
detRes.dl2 = ConvertToYSpace::getComponentParameter(det, pmap, "sigma_l2"); // DL1
detRes.dthe = ConvertToYSpace::getComponentParameter(det, pmap, "sigma_theta"); //DTH in radians
detRes.dEnGauss = ConvertToYSpace::getComponentParameter(det, pmap, "sigma_gauss");
detRes.dEnLorentz = ConvertToYSpace::getComponentParameter(det, pmap, "hwhm_lorentz");

// Compute a time of flight spectrum convolved with a Voigt resolution function for each mass
// at the detector point & sum to a single spectrum
Expand Down Expand Up @@ -264,15 +266,15 @@ namespace Mantid
detPar.l1 = m_l1;
detPar.l2 = m_samplePos.distance(detPos);
detPar.theta = m_inputWS->detectorTwoTheta(det); //radians
detPar.t0 = ComptonProfile::getComponentParameter(det, pmap, "t0")*1e-6; // seconds
detPar.efixed = ComptonProfile::getComponentParameter(det, pmap,"efixed");
detPar.t0 = ConvertToYSpace::getComponentParameter(det, pmap, "t0")*1e-6; // seconds
detPar.efixed = ConvertToYSpace::getComponentParameter(det, pmap,"efixed");

ResolutionParams detRes;
detRes.dl1 = ComptonProfile::getComponentParameter(det, pmap, "sigma_l1"); // DL0
detRes.dl2 = ComptonProfile::getComponentParameter(det, pmap, "sigma_l2"); // DL1
detRes.dthe = ComptonProfile::getComponentParameter(det, pmap, "sigma_theta"); //DTH in radians
detRes.dEnGauss = ComptonProfile::getComponentParameter(det, pmap, "sigma_gauss");
detRes.dEnLorentz = ComptonProfile::getComponentParameter(det, pmap, "hwhm_lorentz");
detRes.dl1 = ConvertToYSpace::getComponentParameter(det, pmap, "sigma_l1"); // DL0
detRes.dl2 = ConvertToYSpace::getComponentParameter(det, pmap, "sigma_l2"); // DL1
detRes.dthe = ConvertToYSpace::getComponentParameter(det, pmap, "sigma_theta"); //DTH in radians
detRes.dEnGauss = ConvertToYSpace::getComponentParameter(det, pmap, "sigma_gauss");
detRes.dEnLorentz = ConvertToYSpace::getComponentParameter(det, pmap, "hwhm_lorentz");

const size_t nxvalues = m_backgroundWS->blocksize();
std::vector<double> foilSpectrum(nxvalues);
Expand Down Expand Up @@ -537,16 +539,16 @@ namespace Mantid
FoilInfo descr;
descr.thetaMin = thetaRng0.first;
descr.thetaMax = thetaRng0.second;
descr.lorentzWidth = ComptonProfile::getComponentParameter(foil0, pmap, "hwhm_lorentz");
descr.gaussWidth = ComptonProfile::getComponentParameter(foil0, pmap, "sigma_gauss");
descr.lorentzWidth = ConvertToYSpace::getComponentParameter(foil0, pmap, "hwhm_lorentz");
descr.gaussWidth = ConvertToYSpace::getComponentParameter(foil0, pmap, "sigma_gauss");
m_foils0[i] = descr; //copy

const auto & foil1 = foils1[i];
auto thetaRng1 = calculateThetaRange(foil1, m_foilRadius,refFrame->pointingHorizontal());
descr.thetaMin = thetaRng1.first;
descr.thetaMax = thetaRng1.second;
descr.lorentzWidth = ComptonProfile::getComponentParameter(foil1, pmap, "hwhm_lorentz");
descr.gaussWidth = ComptonProfile::getComponentParameter(foil1, pmap, "sigma_gauss");
descr.lorentzWidth = ConvertToYSpace::getComponentParameter(foil1, pmap, "hwhm_lorentz");
descr.gaussWidth = ConvertToYSpace::getComponentParameter(foil1, pmap, "sigma_gauss");
m_foils1[i] = descr; //copy
}

Expand Down

0 comments on commit 06606cc

Please sign in to comment.