-
Notifications
You must be signed in to change notification settings - Fork 122
/
IntegratePeaksMDHKL.cpp
314 lines (291 loc) · 13.2 KB
/
IntegratePeaksMDHKL.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include "MantidMDAlgorithms/IntegratePeaksMDHKL.h"
#include "MantidDataObjects/MDFramesToSpecialCoordinateSystem.h"
#include "MantidAPI/CommonBinsValidator.h"
#include "MantidAPI/InstrumentValidator.h"
#include "MantidAPI/WorkspaceUnitValidator.h"
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidDataObjects/MDEventWorkspace.h"
#include "MantidDataObjects/MDHistoWorkspace.h"
#include "MantidGeometry/Instrument.h"
#include "MantidKernel/CompositeValidator.h"
#include "MantidGeometry/Crystal/IPeak.h"
#include <boost/math/special_functions/round.hpp>
#include <algorithm>
#include <limits>
namespace Mantid {
namespace MDAlgorithms {
using Mantid::Kernel::Direction;
// using Mantid::API::WorkspaceProperty;
using namespace Mantid::DataObjects;
using namespace Mantid::API;
using namespace Mantid::Kernel;
using namespace Mantid::Geometry;
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(IntegratePeaksMDHKL)
//----------------------------------------------------------------------------------------------
/**
* Initialize the algorithm's properties.
*/
void IntegratePeaksMDHKL::init() {
declareProperty(
make_unique<WorkspaceProperty<IMDWorkspace>>("InputWorkspace", "",
Direction::Input),
"An input Sample MDHistoWorkspace or MDEventWorkspace in HKL.");
declareProperty("DeltaHKL", 0.5,
"Distance from integer HKL to integrate peak.");
declareProperty("GridPoints", 201,
"Number of grid points for each dimension of HKL box.");
declareProperty("NeighborPoints", 10, "Number of points in 5^3 surrounding "
"points above intensity threshold for "
"point to be part of peak.");
auto fluxValidator = boost::make_shared<CompositeValidator>();
fluxValidator->add<WorkspaceUnitValidator>("Momentum");
fluxValidator->add<InstrumentValidator>();
fluxValidator->add<CommonBinsValidator>();
auto solidAngleValidator = fluxValidator->clone();
declareProperty(
make_unique<WorkspaceProperty<>>("FluxWorkspace", "", Direction::Input,
PropertyMode::Optional, fluxValidator),
"An optional input workspace containing momentum dependent flux for "
"normalization.");
declareProperty(make_unique<WorkspaceProperty<>>(
"SolidAngleWorkspace", "", Direction::Input,
PropertyMode::Optional, solidAngleValidator),
"An optional input workspace containing momentum integrated "
"vanadium for normalization "
"(a measure of the solid angle).");
declareProperty(make_unique<WorkspaceProperty<PeaksWorkspace>>(
"PeaksWorkspace", "", Direction::Input),
"A PeaksWorkspace containing the peaks to integrate.");
declareProperty(
make_unique<WorkspaceProperty<PeaksWorkspace>>("OutputWorkspace", "",
Direction::Output),
"The output PeaksWorkspace will be a copy of the input PeaksWorkspace "
"with the peaks' integrated intensities.");
}
//----------------------------------------------------------------------------------------------
/**
* Execute the algorithm.
*/
void IntegratePeaksMDHKL::exec() {
IMDWorkspace_sptr m_inputWS = getProperty("InputWorkspace");
Mantid::DataObjects::MDFramesToSpecialCoordinateSystem converter;
boost::optional<Mantid::Kernel::SpecialCoordinateSystem> coordinateSystem =
converter(m_inputWS.get());
if (*coordinateSystem != Mantid::Kernel::SpecialCoordinateSystem::HKL) {
std::stringstream errmsg;
errmsg << "Input MDWorkspace's coordinate system is not HKL.";
throw std::invalid_argument(errmsg.str());
}
/// Peak workspace to integrate
PeaksWorkspace_sptr inPeakWS = getProperty("PeaksWorkspace");
const double box = getProperty("DeltaHKL");
const int gridPts = getProperty("GridPoints");
const int neighborPts = getProperty("NeighborPoints");
/// Output peaks workspace, create if needed
PeaksWorkspace_sptr peakWS = getProperty("OutputWorkspace");
if (peakWS != inPeakWS)
peakWS = inPeakWS->clone();
MatrixWorkspace_sptr flux = getProperty("FluxWorkspace");
MatrixWorkspace_sptr sa = getProperty("SolidAngleWorkspace");
IMDEventWorkspace_sptr m_eventWS =
boost::dynamic_pointer_cast<IMDEventWorkspace>(m_inputWS);
IMDHistoWorkspace_sptr m_histoWS =
boost::dynamic_pointer_cast<IMDHistoWorkspace>(m_inputWS);
int npeaks = peakWS->getNumberPeaks();
auto prog = make_unique<Progress>(this, 0.3, 1.0, npeaks);
PARALLEL_FOR_IF(Kernel::threadSafe(*peakWS))
for (int i = 0; i < npeaks; i++) {
PARALLEL_START_INTERUPT_REGION
IPeak &p = peakWS->getPeak(i);
// round to integer
int h = static_cast<int>(boost::math::iround(p.getH()));
int k = static_cast<int>(boost::math::iround(p.getK()));
int l = static_cast<int>(boost::math::iround(p.getL()));
MDHistoWorkspace_sptr histoBox;
if (m_histoWS) {
histoBox = cropHisto(h, k, l, box, m_histoWS);
} else if (sa && flux) {
histoBox = normalize(h, k, l, box, gridPts, flux, sa, m_eventWS);
} else {
histoBox = binEvent(h, k, l, box, gridPts, m_eventWS);
}
double intensity = 0.0;
double errorSquared = 0.0;
integratePeak(neighborPts, histoBox, intensity, errorSquared);
p.setIntensity(intensity);
p.setSigmaIntensity(sqrt(errorSquared));
prog->report();
PARALLEL_END_INTERUPT_REGION
}
PARALLEL_CHECK_INTERUPT_REGION
// Save the output
setProperty("OutputWorkspace", peakWS);
}
MDHistoWorkspace_sptr
IntegratePeaksMDHKL::normalize(int h, int k, int l, double box, int gridPts,
const MatrixWorkspace_sptr &flux,
const MatrixWorkspace_sptr &sa,
const IMDEventWorkspace_sptr &ws) {
IAlgorithm_sptr normAlg = createChildAlgorithm("MDNormSCD");
normAlg->setProperty("InputWorkspace", ws);
normAlg->setProperty("AlignedDim0",
"[H,0,0]," + boost::lexical_cast<std::string>(h - box) +
"," + boost::lexical_cast<std::string>(h + box) +
"," + std::to_string(gridPts));
normAlg->setProperty("AlignedDim1",
"[0,K,0]," + boost::lexical_cast<std::string>(k - box) +
"," + boost::lexical_cast<std::string>(k + box) +
"," + std::to_string(gridPts));
normAlg->setProperty("AlignedDim2",
"[0,0,L]," + boost::lexical_cast<std::string>(l - box) +
"," + boost::lexical_cast<std::string>(l + box) +
"," + std::to_string(gridPts));
normAlg->setProperty("FluxWorkspace", flux);
normAlg->setProperty("SolidAngleWorkspace", sa);
normAlg->setProperty("OutputWorkspace", "mdout");
normAlg->setProperty("OutputNormalizationWorkspace", "mdnorm");
normAlg->executeAsChildAlg();
Workspace_sptr mdout = normAlg->getProperty("OutputWorkspace");
Workspace_sptr mdnorm = normAlg->getProperty("OutputNormalizationWorkspace");
IAlgorithm_sptr alg = createChildAlgorithm("DivideMD");
alg->setProperty("LHSWorkspace", mdout);
alg->setProperty("RHSWorkspace", mdnorm);
alg->setPropertyValue("OutputWorkspace", "out");
alg->execute();
IMDWorkspace_sptr out = alg->getProperty("OutputWorkspace");
return boost::dynamic_pointer_cast<MDHistoWorkspace>(out);
}
void IntegratePeaksMDHKL::integratePeak(const int neighborPts,
MDHistoWorkspace_sptr out,
double &intensity,
double &errorSquared) {
std::vector<int> gridPts;
const size_t dimensionality = out->getNumDims();
for (size_t i = 0; i < dimensionality; ++i) {
gridPts.push_back(static_cast<int>(out->getDimension(i)->getNBins()));
}
double *F = out->getSignalArray();
double Fmax = 0.;
double Fmin = std::numeric_limits<double>::max();
double sum = 0.0;
for (int i = 0; i < gridPts[0] * gridPts[1] * gridPts[2]; i++) {
if (std::isnormal(F[i])) {
sum += F[i];
Fmin = std::min(Fmin, F[i]);
Fmax = std::max(Fmax, F[i]);
}
}
double *SqError = out->getErrorSquaredArray();
double minIntensity = Fmin + 0.01 * (Fmax - Fmin);
int measuredPoints = 0;
int peakPoints = 0;
double peakSum = 0.0;
double measuredSum = 0.0;
double errSqSum = 0.0;
double measuredErrSqSum = 0.0;
for (int Hindex = 0; Hindex < gridPts[0]; Hindex++) {
for (int Kindex = 0; Kindex < gridPts[1]; Kindex++) {
for (int Lindex = 0; Lindex < gridPts[2]; Lindex++) {
int iHKL = Hindex + gridPts[0] * (Kindex + gridPts[1] * Lindex);
if (std::isfinite(F[iHKL])) {
measuredPoints = measuredPoints + 1;
measuredSum = measuredSum + F[iHKL];
measuredErrSqSum = measuredErrSqSum + SqError[iHKL];
if (F[iHKL] > minIntensity) {
int neighborPoints = 0;
for (int Hj = -2; Hj < 3; Hj++) {
for (int Kj = -2; Kj < 3; Kj++) {
for (int Lj = -2; Lj < 3; Lj++) {
int jHKL =
Hindex + Hj +
gridPts[0] * (Kindex + Kj + gridPts[1] * (Lindex + Lj));
if (Lindex + Lj >= 0 && Lindex + Lj < gridPts[2] &&
Kindex + Kj >= 0 && Kindex + Kj < gridPts[1] &&
Hindex + Hj >= 0 && Hindex + Hj < gridPts[0] &&
F[jHKL] > minIntensity) {
neighborPoints = neighborPoints + 1;
}
}
}
}
if (neighborPoints >= neighborPts) {
peakPoints = peakPoints + 1;
peakSum = peakSum + F[iHKL];
errSqSum = errSqSum + SqError[iHKL];
}
}
} else {
double minR =
sqrt(std::pow(float(Hindex) / float(gridPts[0]) - 0.5, 2) +
std::pow(float(Kindex) / float(gridPts[1]) - 0.5, 2) +
std::pow(float(Lindex) / float(gridPts[0]) - 0.5, 2));
if (minR < 0.05) {
intensity = 0.0;
errorSquared = 0.0;
return;
}
}
}
}
}
double ratio = float(peakPoints) / float(measuredPoints - peakPoints);
intensity = peakSum - ratio * (measuredSum - peakSum);
errorSquared = errSqSum + ratio * ratio * (measuredErrSqSum - errSqSum);
return;
}
/**
* Runs the BinMD algorithm on the input to provide the output workspace
* All slicing algorithm properties are passed along
* @return MDHistoWorkspace as a result of the binning
*/
MDHistoWorkspace_sptr
IntegratePeaksMDHKL::binEvent(int h, int k, int l, double box, int gridPts,
const IMDWorkspace_sptr &ws) {
IAlgorithm_sptr binMD = createChildAlgorithm("BinMD", 0.0, 0.3);
binMD->setProperty("InputWorkspace", ws);
binMD->setProperty("AlignedDim0",
"[H,0,0]," + boost::lexical_cast<std::string>(h - box) +
"," + boost::lexical_cast<std::string>(h + box) + "," +
std::to_string(gridPts));
binMD->setProperty("AlignedDim1",
"[0,K,0]," + boost::lexical_cast<std::string>(k - box) +
"," + boost::lexical_cast<std::string>(k + box) + "," +
std::to_string(gridPts));
binMD->setProperty("AlignedDim2",
"[0,0,L]," + boost::lexical_cast<std::string>(l - box) +
"," + boost::lexical_cast<std::string>(l + box) + "," +
std::to_string(gridPts));
binMD->setPropertyValue("AxisAligned", "1");
binMD->setPropertyValue("OutputWorkspace", "out");
binMD->executeAsChildAlg();
Workspace_sptr outputWS = binMD->getProperty("OutputWorkspace");
return boost::dynamic_pointer_cast<MDHistoWorkspace>(outputWS);
}
/**
* Runs the BinMD algorithm on the input to provide the output workspace
* All slicing algorithm properties are passed along
* @return MDHistoWorkspace as a result of the binning
*/
MDHistoWorkspace_sptr
IntegratePeaksMDHKL::cropHisto(int h, int k, int l, double box,
const IMDWorkspace_sptr &ws) {
IAlgorithm_sptr cropMD =
createChildAlgorithm("IntegrateMDHistoWorkspace", 0.0, 0.3);
cropMD->setProperty("InputWorkspace", ws);
cropMD->setProperty("P1Bin", boost::lexical_cast<std::string>(h - box) +
",0," +
boost::lexical_cast<std::string>(h + box));
cropMD->setProperty("P2Bin", boost::lexical_cast<std::string>(k - box) +
",0," +
boost::lexical_cast<std::string>(k + box));
cropMD->setProperty("P3Bin", boost::lexical_cast<std::string>(l - box) +
",0," +
boost::lexical_cast<std::string>(l + box));
cropMD->setPropertyValue("OutputWorkspace", "out");
cropMD->executeAsChildAlg();
IMDHistoWorkspace_sptr outputWS = cropMD->getProperty("OutputWorkspace");
return boost::dynamic_pointer_cast<MDHistoWorkspace>(outputWS);
}
} // namespace MDAlgorithms
} // namespace Mantid