Skip to content

Commit

Permalink
First go at ApplyCalibration untested re #5414
Browse files Browse the repository at this point in the history
Signed-off-by: Karl Palmen <karl.palmen@stfc.ac.uk>
  • Loading branch information
KarlPalmen committed May 29, 2012
1 parent 19a02ac commit ce80a42
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Code/Mantid/Framework/Algorithms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set ( SRC_FILES
src/AlphaCalc.cpp
src/AnyShapeAbsorption.cpp
src/AppendSpectra.cpp
src/ApplyCalibration.cpp
src/ApplyDeadTimeCorr.cpp
src/ApplyDetailedBalance.cpp
src/ApplyTransmissionCorrection.cpp
Expand Down Expand Up @@ -204,6 +205,7 @@ set ( INC_FILES
inc/MantidAlgorithms/AlphaCalc.h
inc/MantidAlgorithms/AnyShapeAbsorption.h
inc/MantidAlgorithms/AppendSpectra.h
inc/MantidAlgorithms/ApplyCalibration.h
inc/MantidAlgorithms/ApplyDeadTimeCorr.h
inc/MantidAlgorithms/ApplyDetailedBalance.h
inc/MantidAlgorithms/ApplyTransmissionCorrection.h
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#ifndef MANTID_DATAHANDLING_APPLYCALIBRATION_H_
#define MANTID_DATAHANDLING_APPLYCALIBRATION_H_

//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/Algorithm.h"

namespace Mantid
{
//----------------------------------------------------------------------
// Forward declarations
//----------------------------------------------------------------------
namespace Geometry
{
class Instrument;
}

namespace Algorithms
{
/**
Update detector positions from input table workspace. The positions are updated as absolute positions and so this update can be repeated.
@author Karl Palmen
Copyright &copy; 2012 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://svn.mantidproject.org/mantid/trunk/Code/Mantid>
*/
class DLLExport ApplyCalibration : public API::Algorithm
{
public:
/// Default constructor
ApplyCalibration();

/// Destructor
~ApplyCalibration() {}

/// Algorithm's name for identification overriding a virtual method
virtual const std::string name() const { return "ApplyCalibration"; }

/// Algorithm's version for identification overriding a virtual method
virtual int version() const { return 1;};

/// Algorithm's category for identification overriding a virtual method
virtual const std::string category() const { return "DataHandling\\Instrument";} // Needs to change

private:
/// Sets documentation strings for this algorithm
virtual void initDocs();
/// Overwrites Algorithm method. Does nothing at present
void init();
/// Overwrites Algorithm method
void exec();

/// Assumes the file is a raw file
//void updateFromRaw(boost::shared_ptr<Geometry::Instrument> instrument,
// const std::string & filename);
/// Assumes the file is an ISIS NeXus file
//void updateFromNeXus(boost::shared_ptr<Geometry::Instrument> instrument,
// const std::string & filename);
/// Set the new detector positions
void setDetectorPosition(API::MatrixWorkspace_sptr Ws, boost::shared_ptr<Geometry::Instrument> instrument,
int detID, Mantid::Kernel::V3D pos, bool sameParent );
};

} // namespace Algorithms
} // namespace Mantid

#endif /*MANTID_DATAHANDLING_APPLYCALIBRATION_H_*/

136 changes: 136 additions & 0 deletions Code/Mantid/Framework/Algorithms/src/ApplyCalibration.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*WIKI*
To be written
*WIKI*/
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAlgorithms/ApplyCalibration.h"
#include "MantidAPI/ITableWorkspace.h"
#include "MantidGeometry/Instrument.h"
#include "MantidGeometry/Instrument/Component.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/SpectraDetectorMap.h"
#include "MantidNexusCPP/NeXusFile.hpp"
#include "MantidNexusCPP/NeXusException.hpp"
#include <boost/scoped_ptr.hpp>

namespace Mantid
{
namespace Algorithms
{

DECLARE_ALGORITHM(ApplyCalibration)

/// Sets documentation strings for this algorithm
void ApplyCalibration::initDocs()
{
this->setWikiSummary(" TBA ");
this->setOptionalMessage(" TBA ");
}

using namespace Kernel;
using namespace API;
using Geometry::Instrument;
using Geometry::Instrument_sptr;
using Geometry::IDetector_sptr;
using Kernel::V3D;

/// Empty default constructor
ApplyCalibration::ApplyCalibration()
{}

/// Initialisation method.
void ApplyCalibration::init()
{

declareProperty(new API::WorkspaceProperty<API::MatrixWorkspace>("Workspace", "",
Direction::InOut), "The name of the input workspace to apply the calibration to");

declareProperty(new API::WorkspaceProperty<API::ITableWorkspace>("PositionTable", "",
Direction::Input), "The name of the table workspace containing the new positions of detectors");

}

/** Executes the algorithm. Moving detectors of input workspace to positions indicated in table workspace
*
* @throw FileError Thrown if unable to get instrument from workspace,
* table workspace is incompatible with instrument
*/
void ApplyCalibration::exec()
{
// Get pointers to the workspace and table
API::MatrixWorkspace_sptr Ws = getProperty("Workspace");
API::ITableWorkspace_sptr PosTable = getProperty("PositionTable");

// Have to resort to a cast here as in UpdayeInstrumentFromFile.cpp
// Actually we are changing position via the instrument parameters, so may be OK with const instrument here
Instrument_sptr instrument = boost::const_pointer_cast<Instrument>(Ws->getInstrument()->baseInstrument());
if (instrument.get() == 0)
{
throw std::runtime_error("Workspace to apply calibration to has no defined instrument");
}

int numDetector = PosTable->rowCount();
//API::Column_const_sptr detID = PosTable->getColumn( 0 );
ColumnVector<int> detID = PosTable->getVector("Detector ID");
ColumnVector<V3D> detPos = PosTable->getVector("Detector Position");
// numDetector needs to be got as the number of rows in the table and the detID got from the (i)th row of table.
for (int i = 0; i < numDetector; ++i)
{
Geometry::IDetector_sptr det = boost::const_pointer_cast<Geometry::IDetector>(instrument->getDetector(detID[i]));

det->setPos(detPos[i]); // We need to make this an absolute position, but first we leave it relative to get a build
}

}

/**
* Set the absolute detector position of a detector
* @param Ws: The workspace containing detectors to be moved
* @param instrument :: A shared pointer to the base instrument
* @param detID :: Detector ID
* @param pos :: new position of Dectector
* @param sameParent :: true if detector has same parent as previous detector set here.
*/
void ApplyCalibration::setDetectorPosition(API::MatrixWorkspace_sptr Ws, boost::shared_ptr<Geometry::Instrument> instrument, int detID, V3D pos, bool sameParent )
{
Geometry::IDetector_sptr det = boost::const_pointer_cast<Geometry::IDetector>(instrument->getDetector(detID));

if (det == 0)
{
std::ostringstream mess;
mess<<"Detector with ID "<<detID<<" was not found.";
g_log.error(mess.str());
throw std::runtime_error(mess.str());
}

// Then find the corresponding relative position
boost::shared_ptr<const Geometry::IComponent> parent = det->getParent();
if (parent)
{
pos -= parent->getPos();
Quat rot = parent->getRelativeRot();
rot.inverse();
rot.rotate(pos);
}
boost::shared_ptr<const Geometry::IComponent>grandparent = parent->getParent();
if (grandparent)
{
Quat rot = grandparent->getRelativeRot();
rot.inverse();
rot.rotate(pos);
}

Geometry::ParameterMap& pmap = Ws->instrumentParameters();
// Add a parameter for the new position
pmap.addV3D(det.get(), "pos", pos);


}

} // namespace Algorithms
} // namespace Mantid

0 comments on commit ce80a42

Please sign in to comment.