Mantid
Loading...
Searching...
No Matches
CalculateDIFC.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
11
12namespace Mantid {
13
14namespace {
15
16// calculate DIFC using the geometry and
17void calculateFromOffset(API::Progress &progress, DataObjects::SpecialWorkspace2D &outputWs,
18 const DataObjects::OffsetsWorkspace *const offsetsWS,
19 const Geometry::DetectorInfo &detectorInfo) {
20 const auto &detectorIDs = detectorInfo.detectorIDs();
21 const bool haveOffset = (offsetsWS != nullptr);
22 const double l1 = detectorInfo.l1();
23
24 for (size_t i = 0; i < detectorInfo.size(); ++i) {
25 if ((!detectorInfo.isMasked(i)) && (!detectorInfo.isMonitor(i))) {
26 // offset=0 means that geometry is correct
27 const double offset = (haveOffset) ? offsetsWS->getValue(detectorIDs[i], 0.) : 0.;
28
29 // tofToDSpacingFactor gives 1/DIFC
30 double difc =
31 1. / Geometry::Conversion::tofToDSpacingFactor(l1, detectorInfo.l2(i), detectorInfo.twoTheta(i), offset);
32 outputWs.setValue(detectorIDs[i], difc);
33 }
34
35 progress.report("Calculate DIFC");
36 }
37}
38
39// look through the columns of detid and difc and copy theminto the
40// SpecialWorkspace2D
41void calculateFromTable(API::Progress &progress, DataObjects::SpecialWorkspace2D &outputWs,
42 const API::ITableWorkspace &calibWs) {
43 API::ConstColumnVector<double> difcCol = calibWs.getVector("difc");
44 API::ConstColumnVector<int> detIDs = calibWs.getVector("detid");
45
46 const size_t numRows = detIDs.size();
47 for (size_t i = 0; i < numRows; ++i) {
48 outputWs.setValue(detIDs[i], difcCol[i]);
49 progress.report("Calculate DIFC");
50 }
51}
52} // namespace
53
54namespace Algorithms {
55
64
65// Register the algorithm into the AlgorithmFactory
66DECLARE_ALGORITHM(CalculateDIFC)
67
68//----------------------------------------------------------------------------------------------
69
70
71const std::string CalculateDIFC::name() const { return "CalculateDIFC"; }
72
74int CalculateDIFC::version() const { return 1; }
75
77const std::string CalculateDIFC::category() const { return "Diffraction\\Utility"; }
78
80const std::string CalculateDIFC::summary() const { return "Calculate the DIFC for every pixel"; }
81
82//----------------------------------------------------------------------------------------------
86 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("InputWorkspace", "", Direction::Input),
87 "Name of the workspace to have DIFC calculated from");
88 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("OutputWorkspace", "", Direction::Output),
89 "Workspace containing DIFC for each pixel");
91 "CalibrationWorkspace", "", Direction::Input, Mantid::API::PropertyMode::Optional),
92 "Optional: A TableWorkspace containing the DIFC values, "
93 "which will be copied. This property cannot be set in "
94 "conjunction with property OffsetsWorkspace.");
95 declareProperty(std::make_unique<WorkspaceProperty<OffsetsWorkspace>>("OffsetsWorkspace", "", Direction::Input,
97 "Optional: A OffsetsWorkspace containing the calibration "
98 "offsets. This property cannot be set in conjunction with "
99 "property CalibrationWorkspace.");
100}
101
102std::map<std::string, std::string> CalculateDIFC::validateInputs() {
103 std::map<std::string, std::string> result;
104
105 OffsetsWorkspace_const_sptr offsetsWS = getProperty("OffsetsWorkspace");
106 API::ITableWorkspace_const_sptr calibrationWS = getProperty("CalibrationWorkspace");
107
108 if ((bool(offsetsWS)) && (bool(calibrationWS))) {
109 std::string msg = "Only specify calibration one way";
110 result["OffsetsWorkspace"] = msg;
111 result["CalibrationWorkspace"] = msg;
112 }
113
114 return result;
115}
116
117//----------------------------------------------------------------------------------------------
121
122 DataObjects::OffsetsWorkspace_const_sptr offsetsWs = getProperty("OffsetsWorkspace");
123 API::ITableWorkspace_const_sptr calibWs = getProperty("CalibrationWorkspace");
124 API::MatrixWorkspace_sptr inputWs = getProperty("InputWorkspace");
125 API::MatrixWorkspace_sptr outputWs = getProperty("OutputWorkspace");
126
127 if ((!bool(inputWs == outputWs)) ||
128 // SpecialWorkspace2D is a Workspace2D where each spectrum
129 // has one detector pixel, one X-value, and one Y-value.
130 (!bool(std::dynamic_pointer_cast<SpecialWorkspace2D>(outputWs)))) {
131 outputWs =
132 std::dynamic_pointer_cast<MatrixWorkspace>(std::make_shared<SpecialWorkspace2D>(inputWs->getInstrument()));
133 outputWs->setTitle("DIFC workspace");
134 }
135
136 // convert to actual type being used
138 std::dynamic_pointer_cast<DataObjects::SpecialWorkspace2D>(outputWs);
139
140 API::Progress progress(this, 0.0, 1.0, inputWs->getNumberHistograms());
141 if (bool(calibWs)) {
142 calculateFromTable(progress, *outputSpecialWs, *calibWs);
143 } else {
144 // this method handles calculating from instrument geometry as well,
145 // and even when OffsetsWorkspace hasn't been set
146 const auto &detectorInfo = inputWs->detectorInfo();
147 calculateFromOffset(progress, *outputSpecialWs, offsetsWs.get(), detectorInfo);
148 }
149
150 setProperty("OutputWorkspace", outputWs);
151}
152
153} // namespace Algorithms
154} // namespace Mantid
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
Definition: Algorithm.cpp:231
Base MatrixWorkspace Abstract Class.
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
A property class for workspaces.
CalculateDIFC : Calculate the DIFC for every pixel.
Definition: CalculateDIFC.h:19
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
const std::string category() const override
Algorithm's category for identification.
int version() const override
Algorithm's version for identification.
std::map< std::string, std::string > validateInputs() override
Cross-check properties with each other.
void init() override
Initialize the algorithm's properties.
void exec() override
Execute the algorithm.
An OffsetsWorkspace is a specialized Workspace2D where the Y value at each pixel is the offset to be ...
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
std::shared_ptr< const ITableWorkspace > ITableWorkspace_const_sptr
shared pointer to Mantid::API::ITableWorkspace (const version)
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< const OffsetsWorkspace > OffsetsWorkspace_const_sptr
shared pointer to a const OffsetsWorkspace
std::shared_ptr< SpecialWorkspace2D > SpecialWorkspace2D_sptr
shared pointer to the SpecialWorkspace2D class
MANTID_GEOMETRY_DLL double tofToDSpacingFactor(const double l1, const double l2, const double twoTheta, const double offset)
Calculate and return conversion factor from tof to d-spacing.
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
Helper class which provides the Collimation Length for SANS instruments.
STL namespace.
Describes the direction (within an algorithm) of a Property.
Definition: Property.h:50
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54