-
Notifications
You must be signed in to change notification settings - Fork 121
/
CalculateDIFC.cpp
154 lines (126 loc) · 6.52 KB
/
CalculateDIFC.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidAlgorithms/CalculateDIFC.h"
#include "MantidAPI/ITableWorkspace.h"
#include "MantidDataObjects/SpecialWorkspace2D.h"
#include "MantidGeometry/IDetector.h"
namespace Mantid {
namespace {
// calculate DIFC using the geometry and
void calculateFromOffset(API::Progress &progress, DataObjects::SpecialWorkspace2D &outputWs,
const DataObjects::OffsetsWorkspace *const offsetsWS,
const Geometry::DetectorInfo &detectorInfo) {
const auto &detectorIDs = detectorInfo.detectorIDs();
const bool haveOffset = (offsetsWS != nullptr);
const double l1 = detectorInfo.l1();
for (size_t i = 0; i < detectorInfo.size(); ++i) {
if ((!detectorInfo.isMasked(i)) && (!detectorInfo.isMonitor(i))) {
// offset=0 means that geometry is correct
const double offset = (haveOffset) ? offsetsWS->getValue(detectorIDs[i], 0.) : 0.;
// tofToDSpacingFactor gives 1/DIFC
double difc =
1. / Geometry::Conversion::tofToDSpacingFactor(l1, detectorInfo.l2(i), detectorInfo.twoTheta(i), offset);
outputWs.setValue(detectorIDs[i], difc);
}
progress.report("Calculate DIFC");
}
}
// look through the columns of detid and difc and copy theminto the
// SpecialWorkspace2D
void calculateFromTable(API::Progress &progress, DataObjects::SpecialWorkspace2D &outputWs,
const API::ITableWorkspace &calibWs) {
API::ConstColumnVector<double> difcCol = calibWs.getVector("difc");
API::ConstColumnVector<int> detIDs = calibWs.getVector("detid");
const size_t numRows = detIDs.size();
for (size_t i = 0; i < numRows; ++i) {
outputWs.setValue(detIDs[i], difcCol[i]);
progress.report("Calculate DIFC");
}
}
} // namespace
namespace Algorithms {
using Mantid::API::MatrixWorkspace;
using Mantid::API::WorkspaceProperty;
using Mantid::DataObjects::OffsetsWorkspace;
using Mantid::DataObjects::OffsetsWorkspace_const_sptr;
using Mantid::DataObjects::SpecialWorkspace2D;
using Mantid::DataObjects::SpecialWorkspace2D_sptr;
using Mantid::Geometry::Instrument_const_sptr;
using Mantid::Kernel::Direction;
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(CalculateDIFC)
//----------------------------------------------------------------------------------------------
/// Algorithms name for identification. @see Algorithm::name
const std::string CalculateDIFC::name() const { return "CalculateDIFC"; }
/// Algorithm's version for identification. @see Algorithm::version
int CalculateDIFC::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string CalculateDIFC::category() const { return "Diffraction\\Utility"; }
/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary
const std::string CalculateDIFC::summary() const { return "Calculate the DIFC for every pixel"; }
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void CalculateDIFC::init() {
declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("InputWorkspace", "", Direction::Input),
"Name of the workspace to have DIFC calculated from");
declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("OutputWorkspace", "", Direction::Output),
"Workspace containing DIFC for each pixel");
declareProperty(std::make_unique<WorkspaceProperty<API::ITableWorkspace>>(
"CalibrationWorkspace", "", Direction::Input, Mantid::API::PropertyMode::Optional),
"Optional: A TableWorkspace containing the DIFC values, "
"which will be copied. This property cannot be set in "
"conjunction with property OffsetsWorkspace.");
declareProperty(std::make_unique<WorkspaceProperty<OffsetsWorkspace>>("OffsetsWorkspace", "", Direction::Input,
Mantid::API::PropertyMode::Optional),
"Optional: A OffsetsWorkspace containing the calibration "
"offsets. This property cannot be set in conjunction with "
"property CalibrationWorkspace.");
}
std::map<std::string, std::string> CalculateDIFC::validateInputs() {
std::map<std::string, std::string> result;
OffsetsWorkspace_const_sptr offsetsWS = getProperty("OffsetsWorkspace");
API::ITableWorkspace_const_sptr calibrationWS = getProperty("CalibrationWorkspace");
if ((bool(offsetsWS)) && (bool(calibrationWS))) {
std::string msg = "Only specify calibration one way";
result["OffsetsWorkspace"] = msg;
result["CalibrationWorkspace"] = msg;
}
return result;
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void CalculateDIFC::exec() {
DataObjects::OffsetsWorkspace_const_sptr offsetsWs = getProperty("OffsetsWorkspace");
API::ITableWorkspace_const_sptr calibWs = getProperty("CalibrationWorkspace");
API::MatrixWorkspace_sptr inputWs = getProperty("InputWorkspace");
API::MatrixWorkspace_sptr outputWs = getProperty("OutputWorkspace");
if ((!bool(inputWs == outputWs)) ||
// SpecialWorkspace2D is a Workspace2D where each spectrum
// has one detector pixel, one X-value, and one Y-value.
(!bool(std::dynamic_pointer_cast<SpecialWorkspace2D>(outputWs)))) {
outputWs =
std::dynamic_pointer_cast<MatrixWorkspace>(std::make_shared<SpecialWorkspace2D>(inputWs->getInstrument()));
outputWs->setTitle("DIFC workspace");
}
// convert to actual type being used
DataObjects::SpecialWorkspace2D_sptr outputSpecialWs =
std::dynamic_pointer_cast<DataObjects::SpecialWorkspace2D>(outputWs);
API::Progress progress(this, 0.0, 1.0, inputWs->getNumberHistograms());
if (bool(calibWs)) {
calculateFromTable(progress, *outputSpecialWs, *calibWs);
} else {
// this method handles calculating from instrument geometry as well,
// and even when OffsetsWorkspace hasn't been set
const auto &detectorInfo = inputWs->detectorInfo();
calculateFromOffset(progress, *outputSpecialWs, offsetsWs.get(), detectorInfo);
}
setProperty("OutputWorkspace", outputWs);
}
} // namespace Algorithms
} // namespace Mantid