-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First go at ApplyCalibration untested re #5414
Signed-off-by: Karl Palmen <karl.palmen@stfc.ac.uk>
- Loading branch information
1 parent
19a02ac
commit ce80a42
Showing
3 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
Code/Mantid/Framework/Algorithms/inc/MantidAlgorithms/ApplyCalibration.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 © 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
136
Code/Mantid/Framework/Algorithms/src/ApplyCalibration.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |