Skip to content

Commit

Permalink
Re #7494. Small tidy-ups in the algorithm.
Browse files Browse the repository at this point in the history
Mostly the addition of const in several places.
  • Loading branch information
RussellTaylor committed Sep 4, 2013
1 parent 850a8bf commit 3d1306f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ namespace Algorithms
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport ModeratorTzeroLinear : public Mantid::API::Algorithm
class DLLExport ModeratorTzeroLinear : public API::Algorithm
{
public:
/// (Empty) Constructor
ModeratorTzeroLinear() : Mantid::API::Algorithm() {}
ModeratorTzeroLinear() : API::Algorithm() {}
/// Virtual destructor
virtual ~ModeratorTzeroLinear() {}
/// Algorithm's name
Expand All @@ -81,7 +81,7 @@ class DLLExport ModeratorTzeroLinear : public Mantid::API::Algorithm
//conversion constants applicable to histogram and event workspaces
double m_gradient;
double m_intercept;
Mantid::Geometry::Instrument_const_sptr m_instrument;
Geometry::Instrument_const_sptr m_instrument;
// Sets documentation strings for this algorithm
virtual void initDocs();
// Initialisation code
Expand All @@ -91,7 +91,7 @@ class DLLExport ModeratorTzeroLinear : public Mantid::API::Algorithm
// Execution code for event workspace
void execEvent();
// Calculate time from sample to detector and initial flight path
void CalculateTfLi(Mantid::API::MatrixWorkspace_sptr inputWS, size_t i, double &t_f, double &L_i);
void calculateTfLi(API::MatrixWorkspace_const_sptr inputWS, size_t i, double &t_f, double &L_i);

};

Expand Down
48 changes: 21 additions & 27 deletions Code/Mantid/Framework/Algorithms/src/ModeratorTzeroLinear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ using namespace Mantid::DataObjects;

void ModeratorTzeroLinear::init()
{

auto wsValidator = boost::make_shared<CompositeValidator>();
wsValidator->add<WorkspaceUnitValidator>("TOF");
declareProperty(new WorkspaceProperty<MatrixWorkspace>("InputWorkspace","",Direction::Input,wsValidator),
declareProperty(new WorkspaceProperty<MatrixWorkspace>("InputWorkspace","",Direction::Input,boost::make_shared<WorkspaceUnitValidator>("TOF")),
"The name of the input workspace, containing events and/or histogram data, in units of time-of-flight");
//declare the output workspace
declareProperty(new WorkspaceProperty<MatrixWorkspace>("OutputWorkspace","",Direction::Output),
Expand All @@ -89,15 +86,15 @@ void ModeratorTzeroLinear::init()
void ModeratorTzeroLinear::exec()
{
//retrieve the input workspace.
const MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace");
const MatrixWorkspace_const_sptr inputWS = getProperty("InputWorkspace");

//Get a pointer to the instrument contained in the workspace
m_instrument = inputWS->getInstrument();

//deltaE-mode (should be "indirect")
try
{
std::vector<std::string> Emode=m_instrument->getStringParameter("deltaE-mode");
const std::vector<std::string> Emode = m_instrument->getStringParameter("deltaE-mode");
if(Emode.empty())
throw Exception::InstrumentDefinitionError("Unable to retrieve instrument geometry (direct or indirect) parameter", inputWS->getTitle());
if(Emode[0]!= "indirect")
Expand All @@ -117,7 +114,7 @@ void ModeratorTzeroLinear::exec()
throw Exception::InstrumentDefinitionError("Unable to retrieve Moderator Time Zero parameters (gradient)", inputWS->getTitle());
m_gradient = gradientParam[0]; //[gradient]=microsecond/Angstrom
//conversion factor for gradient from microsecond/Angstrom to meters
double convfactor = 1e+4*PhysicalConstants::h/PhysicalConstants::NeutronMass;
double convfactor = 1.0e4*PhysicalConstants::h/PhysicalConstants::NeutronMass;
m_gradient *= convfactor; //[gradient] = meter
const std::vector<double> interceptParam = m_instrument->getNumberParameter("Moderator.TimeZero.intercept");
if ( interceptParam.empty() )
Expand Down Expand Up @@ -148,21 +145,21 @@ void ModeratorTzeroLinear::exec()
}

// do the shift in X
const size_t numHists = static_cast<size_t>(inputWS->getNumberHistograms());
const size_t numHists = inputWS->getNumberHistograms();
Progress prog(this,0.0,1.0,numHists); //report progress of algorithm
PARALLEL_FOR2(inputWS, outputWS)
for (int i=0; i < static_cast<int>(numHists); ++i)
{
PARALLEL_START_INTERUPT_REGION
double t_f, L_i;
size_t wsIndex = static_cast<size_t>(i);
CalculateTfLi(inputWS, wsIndex ,t_f, L_i);
calculateTfLi(inputWS, wsIndex, t_f, L_i);
// shift the time of flights
if(t_f >= 0) //t_f < 0 when no detector info is available
{
double scaling = L_i/(L_i+m_gradient);
double offset = (1-scaling)*t_f - scaling*m_intercept;
MantidVec &inbins = inputWS->dataX(i);
const double scaling = L_i/(L_i+m_gradient);
const double offset = (1-scaling)*t_f - scaling*m_intercept;
const MantidVec & inbins = inputWS->readX(i);
MantidVec &outbins = outputWS->dataX(i);
for(unsigned int j=0; j < inbins.size(); j++)
{
Expand All @@ -171,24 +168,24 @@ void ModeratorTzeroLinear::exec()
}
else
{
outputWS->dataX(i) = inputWS->dataX(i);
outputWS->dataX(i) = inputWS->readX(i);
}
//Copy y and e data
outputWS->dataY(i) = inputWS->dataY(i);
outputWS->dataE(i) = inputWS->dataE(i);
outputWS->dataY(i) = inputWS->readY(i);
outputWS->dataE(i) = inputWS->readE(i);
prog.report();
PARALLEL_END_INTERUPT_REGION
}
PARALLEL_CHECK_INTERUPT_REGION

// Copy units
if (inputWS->getAxis(0)->unit().get())
if ( inputWS->getAxis(0)->unit() )
{
outputWS->getAxis(0)->unit() = inputWS->getAxis(0)->unit();
outputWS->getAxis(0)->unit() = inputWS->getAxis(0)->unit();
}
try
{
if(inputWS->getAxis(1)->unit().get())
if( inputWS->getAxis(1)->unit() )
{
outputWS->getAxis(1)->unit() = inputWS->getAxis(1)->unit();
}
Expand All @@ -209,8 +206,8 @@ void ModeratorTzeroLinear::execEvent()
EventWorkspace_const_sptr inputWS= boost::dynamic_pointer_cast<const EventWorkspace>(matrixInputWS);

// generate the output workspace pointer
const size_t numHists = static_cast<size_t>(inputWS->getNumberHistograms());
Mantid::API::MatrixWorkspace_sptr matrixOutputWS = getProperty("OutputWorkspace");
const size_t numHists = inputWS->getNumberHistograms();
MatrixWorkspace_sptr matrixOutputWS = getProperty("OutputWorkspace");
EventWorkspace_sptr outputWS;
if (matrixOutputWS == matrixInputWS)
{
Expand All @@ -229,9 +226,6 @@ void ModeratorTzeroLinear::execEvent()
setProperty("OutputWorkspace", matrixOutputWS);
}

//Get a pointer to the sample
IObjComponent_const_sptr sample = outputWS->getInstrument()->getSample();

// Loop over the spectra
Progress prog(this,0.0,1.0,numHists); //report progress of algorithm
PARALLEL_FOR1(outputWS)
Expand All @@ -244,10 +238,10 @@ void ModeratorTzeroLinear::execEvent()
{
// Calculate the time from sample to detector 'i'
double t_f, L_i;
CalculateTfLi(matrixOutputWS, wsIndex, t_f, L_i);
calculateTfLi(matrixOutputWS, wsIndex, t_f, L_i);
if(t_f >= 0)
{
double scaling = L_i/(L_i+m_gradient);
const double scaling = L_i/(L_i+m_gradient);
//Calculate new time of flight, TOF'=scaling*(TOF-t_f-intercept)+t_f = scaling*TOF + (1-scaling)*t_f - scaling*intercept
evlist.convertTof(scaling, (1-scaling)*t_f - scaling*m_intercept);
}
Expand All @@ -256,11 +250,11 @@ void ModeratorTzeroLinear::execEvent()
PARALLEL_END_INTERUPT_REGION
}
PARALLEL_CHECK_INTERUPT_REGION
outputWS->clearMRU(); // Clears the Most Recent Used lists */
outputWS->clearMRU(); // Clears the Most Recent Used lists */
} // end of void ModeratorTzeroLinear::execEvent()

//calculate time from sample to detector
void ModeratorTzeroLinear::CalculateTfLi(MatrixWorkspace_sptr inputWS, size_t i, double &t_f, double &L_i)
void ModeratorTzeroLinear::calculateTfLi(MatrixWorkspace_const_sptr inputWS, size_t i, double &t_f, double &L_i)
{
static const double convFact = 1.0e-6*sqrt(2*PhysicalConstants::meV/PhysicalConstants::NeutronMass);
static const double TfError = -1.0; //signal error when calculating final time
Expand Down

0 comments on commit 3d1306f

Please sign in to comment.