-
Notifications
You must be signed in to change notification settings - Fork 121
/
PredictPeaks.cpp
448 lines (374 loc) · 16.9 KB
/
PredictPeaks.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include "MantidCrystal/PredictPeaks.h"
#include "MantidAPI/IMDEventWorkspace.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidGeometry/Crystal/BasicHKLFilters.h"
#include "MantidGeometry/Crystal/HKLFilterWavelength.h"
#include "MantidGeometry/Crystal/HKLGenerator.h"
#include "MantidGeometry/Crystal/StructureFactorCalculatorSummation.h"
#include "MantidGeometry/Objects/InstrumentRayTracer.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/EnabledWhenProperty.h"
using Mantid::Kernel::EnabledWhenProperty;
namespace Mantid {
namespace Crystal {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(PredictPeaks)
using namespace Mantid::API;
using namespace Mantid::DataObjects;
using namespace Mantid::Geometry;
using namespace Mantid::Kernel;
namespace {
/// Small helper function that return -1 if convention
/// is "Crystallography" and 1 otherwise.
double get_factor_for_q_convention(const std::string &convention) {
if (convention == "Crystallography") {
return -1.0;
}
return 1.0;
}
}
//----------------------------------------------------------------------------------------------
/** Constructor
*/
PredictPeaks::PredictPeaks()
: m_runNumber(-1), m_inst(), m_pw(), m_sfCalculator(),
m_qConventionFactor(get_factor_for_q_convention(
ConfigService::Instance().getString("Q.convention"))) {
m_refConds = getAllReflectionConditions();
}
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void PredictPeaks::init() {
declareProperty(make_unique<WorkspaceProperty<Workspace>>(
"InputWorkspace", "", Direction::Input),
"An input workspace (MatrixWorkspace, MDEventWorkspace, or "
"PeaksWorkspace) containing:\n"
" - The relevant Instrument (calibrated as needed).\n"
" - A sample with a UB matrix.\n"
" - The goniometer rotation matrix.");
declareProperty(
make_unique<PropertyWithValue<double>>("WavelengthMin", 0.1,
Direction::Input),
"Minimum wavelength limit at which to start looking for single-crystal "
"peaks.");
declareProperty(
make_unique<PropertyWithValue<double>>("WavelengthMax", 100.0,
Direction::Input),
"Maximum wavelength limit at which to stop looking for single-crystal "
"peaks.");
declareProperty(make_unique<PropertyWithValue<double>>("MinDSpacing", 1.0,
Direction::Input),
"Minimum d-spacing of peaks to consider. Default = 1.0");
declareProperty(make_unique<PropertyWithValue<double>>("MaxDSpacing", 100.0,
Direction::Input),
"Maximum d-spacing of peaks to consider.");
// Build up a list of reflection conditions to use
std::vector<std::string> propOptions;
for (auto &refCond : m_refConds)
propOptions.push_back(refCond->getName());
declareProperty("ReflectionCondition", "Primitive",
boost::make_shared<StringListValidator>(propOptions),
"Which reflection condition applies to this crystal, "
"reducing the number of expected HKL peaks?");
declareProperty("CalculateStructureFactors", false,
"Calculate structure factors for the predicted peaks. This "
"option only works if the sample of the input workspace has "
"a crystal structure assigned.");
declareProperty(
make_unique<WorkspaceProperty<PeaksWorkspace>>(
"HKLPeaksWorkspace", "", Direction::Input, PropertyMode::Optional),
"Optional: An input PeaksWorkspace with the HKL of the peaks "
"that we should predict. \n"
"The WavelengthMin/Max and Min/MaxDSpacing parameters are "
"unused if this is specified.");
declareProperty("RoundHKL", true,
"When using HKLPeaksWorkspace, this will round the HKL "
"values in the HKLPeaksWorkspace to the nearest integers if "
"checked.\n"
"Keep unchecked to use the original values");
setPropertySettings("RoundHKL", make_unique<EnabledWhenProperty>(
"HKLPeaksWorkspace", IS_NOT_DEFAULT));
// Disable some props when using HKLPeaksWorkspace
auto makeSet = [] {
std::unique_ptr<IPropertySettings> set =
make_unique<EnabledWhenProperty>("HKLPeaksWorkspace", IS_DEFAULT);
return set;
};
setPropertySettings("WavelengthMin", makeSet());
setPropertySettings("WavelengthMax", makeSet());
setPropertySettings("MinDSpacing", makeSet());
setPropertySettings("MaxDSpacing", makeSet());
setPropertySettings("ReflectionCondition", makeSet());
declareProperty(make_unique<WorkspaceProperty<PeaksWorkspace>>(
"OutputWorkspace", "", Direction::Output),
"An output PeaksWorkspace.");
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void PredictPeaks::exec() {
// Get the input properties
Workspace_sptr rawInputWorkspace = getProperty("InputWorkspace");
ExperimentInfo_sptr inputExperimentInfo =
boost::dynamic_pointer_cast<ExperimentInfo>(rawInputWorkspace);
MatrixWorkspace_sptr matrixWS =
boost::dynamic_pointer_cast<MatrixWorkspace>(rawInputWorkspace);
PeaksWorkspace_sptr peaksWS =
boost::dynamic_pointer_cast<PeaksWorkspace>(rawInputWorkspace);
IMDEventWorkspace_sptr mdWS =
boost::dynamic_pointer_cast<IMDEventWorkspace>(rawInputWorkspace);
std::vector<DblMatrix> gonioVec;
if (matrixWS) {
// Retrieve the goniometer rotation matrix
try {
DblMatrix goniometerMatrix = matrixWS->run().getGoniometerMatrix();
gonioVec.push_back(goniometerMatrix);
} catch (std::runtime_error &e) {
// If there is no goniometer matrix, use identity matrix instead.
g_log.error() << "Error getting the goniometer rotation matrix from the "
"InputWorkspace.\n" << e.what() << '\n';
g_log.warning() << "Using identity goniometer rotation matrix instead.\n";
}
} else if (peaksWS) {
// Sort peaks by run number so that peaks with equal goniometer matrices are
// adjacent
std::vector<std::pair<std::string, bool>> criteria;
criteria.push_back(std::pair<std::string, bool>("RunNumber", true));
peaksWS->sort(criteria);
// Get all goniometer matrices
DblMatrix lastGoniometerMatrix = Matrix<double>(3, 3, false);
for (int i = 0; i < static_cast<int>(peaksWS->getNumberPeaks()); ++i) {
IPeak &p = peaksWS->getPeak(i);
DblMatrix currentGoniometerMatrix = p.getGoniometerMatrix();
if (!(currentGoniometerMatrix == lastGoniometerMatrix)) {
gonioVec.push_back(currentGoniometerMatrix);
lastGoniometerMatrix = currentGoniometerMatrix;
}
}
} else if (mdWS) {
if (mdWS->getNumExperimentInfo() <= 0)
throw std::invalid_argument(
"Specified a MDEventWorkspace as InputWorkspace but it does not have "
"any ExperimentInfo associated. Please choose a workspace with a "
"full instrument and sample.");
inputExperimentInfo = mdWS->getExperimentInfo(0);
// Retrieve the goniometer rotation matrices for each experiment info
for (uint16_t i = 0; i < mdWS->getNumExperimentInfo(); ++i) {
try {
DblMatrix goniometerMatrix =
mdWS->getExperimentInfo(i)->mutableRun().getGoniometerMatrix();
gonioVec.push_back(goniometerMatrix);
} catch (std::runtime_error &e) {
// If there is no goniometer matrix, use identity matrix instead.
gonioVec.push_back(DblMatrix(3, 3, true));
g_log.error()
<< "Error getting the goniometer rotation matrix from the "
"InputWorkspace.\n" << e.what() << '\n';
g_log.warning()
<< "Using identity goniometer rotation matrix instead.\n";
}
}
}
// If there's no goniometer matrix at this point, push back an identity
// matrix.
if (gonioVec.empty()) {
gonioVec.push_back(DblMatrix(3, 3, true));
}
setInstrumentFromInputWorkspace(inputExperimentInfo);
setRunNumberFromInputWorkspace(inputExperimentInfo);
checkBeamDirection();
// Create the output
m_pw = boost::make_shared<PeaksWorkspace>();
// Copy instrument, sample, etc.
m_pw->copyExperimentInfoFrom(inputExperimentInfo.get());
const Sample &sample = inputExperimentInfo->sample();
// Retrieve the OrientedLattice (UnitCell) from the workspace
OrientedLattice orientedLattice = sample.getOrientedLattice();
// Get the UB matrix from it
Matrix<double> ub(3, 3, true);
ub = orientedLattice.getUB();
std::vector<V3D> possibleHKLs;
PeaksWorkspace_sptr possibleHKLWorkspace = getProperty("HKLPeaksWorkspace");
if (!possibleHKLWorkspace) {
fillPossibleHKLsUsingGenerator(orientedLattice, possibleHKLs);
} else {
fillPossibleHKLsUsingPeaksWorkspace(possibleHKLWorkspace, possibleHKLs);
}
setStructureFactorCalculatorFromSample(sample);
/* The wavelength filtering can not be done before because it depends
* on q being correctly oriented, so an additional filtering step is required.
*/
double lambdaMin = getProperty("WavelengthMin");
double lambdaMax = getProperty("WavelengthMax");
Progress prog(this, 0.0, 1.0, possibleHKLs.size() * gonioVec.size());
prog.setNotifyStep(0.01);
for (auto &goniometerMatrix : gonioVec) {
// Final transformation matrix (HKL to Q in lab frame)
DblMatrix orientedUB = goniometerMatrix * ub;
/* Because of the additional filtering step it's better to keep track of the
* allowed peaks with a counter. */
HKLFilterWavelength lambdaFilter(orientedUB, lambdaMin, lambdaMax);
size_t allowedPeakCount = 0;
for (auto &possibleHKL : possibleHKLs) {
if (lambdaFilter.isAllowed(possibleHKL)) {
++allowedPeakCount;
calculateQAndAddToOutput(possibleHKL, orientedUB, goniometerMatrix);
}
prog.report();
}
g_log.notice() << "Out of " << allowedPeakCount
<< " allowed peaks within parameters, "
<< m_pw->getNumberPeaks()
<< " were found to hit a detector.\n";
}
setProperty<PeaksWorkspace_sptr>("OutputWorkspace", m_pw);
}
/// Tries to set the internally stored instrument from an ExperimentInfo-object.
void PredictPeaks::setInstrumentFromInputWorkspace(
const ExperimentInfo_sptr &inWS) {
// Check that there is an input workspace that has a sample.
if (!inWS || !inWS->getInstrument())
throw std::invalid_argument("Did not specify a valid InputWorkspace with a "
"full instrument.");
m_inst = inWS->getInstrument();
}
/// Sets the run number from the supplied ExperimentInfo or throws an exception.
void PredictPeaks::setRunNumberFromInputWorkspace(
const ExperimentInfo_sptr &inWS) {
if (!inWS) {
throw std::runtime_error("Failed to get run number");
}
m_runNumber = inWS->getRunNumber();
}
/// Checks that the beam direction is +Z, throws exception otherwise.
void PredictPeaks::checkBeamDirection() const {
V3D samplePos = m_inst->getSample()->getPos();
// L1 path and direction
V3D beamDir = m_inst->getSource()->getPos() - samplePos;
if ((fabs(beamDir.X()) > 1e-2) ||
(fabs(beamDir.Y()) > 1e-2)) // || (beamDir.Z() < 0))
throw std::invalid_argument("Instrument must have a beam direction that "
"is only in the +Z direction for this "
"algorithm to be valid..");
}
/// Fills possibleHKLs with all HKLs that are allowed within d- and
/// lambda-limits.
void PredictPeaks::fillPossibleHKLsUsingGenerator(
const OrientedLattice &orientedLattice,
std::vector<V3D> &possibleHKLs) const {
double dMin = getProperty("MinDSpacing");
double dMax = getProperty("MaxDSpacing");
// --- Reflection condition ----
// Use the primitive by default
ReflectionCondition_sptr refCond =
boost::make_shared<ReflectionConditionPrimitive>();
// Get it from the property
std::string refCondName = getPropertyValue("ReflectionCondition");
for (const auto &m_refCond : m_refConds)
if (m_refCond->getName() == refCondName)
refCond = m_refCond;
HKLGenerator gen(orientedLattice, dMin);
auto filter =
boost::make_shared<HKLFilterCentering>(refCond) &
boost::make_shared<HKLFilterDRange>(orientedLattice, dMin, dMax);
V3D hklMin = *(gen.begin());
g_log.information() << "HKL range for d_min of " << dMin << " to d_max of "
<< dMax << " is from " << hklMin << " to "
<< hklMin * -1.0 << ", a total of " << gen.size()
<< " possible HKL's\n";
if (gen.size() > 10000000000)
throw std::invalid_argument("More than 10 billion HKLs to search. Is "
"your d_min value too small?");
possibleHKLs.clear();
possibleHKLs.reserve(gen.size());
std::remove_copy_if(gen.begin(), gen.end(), std::back_inserter(possibleHKLs),
(~filter)->fn());
}
/// Fills possibleHKLs with all HKLs from the supplied PeaksWorkspace.
void PredictPeaks::fillPossibleHKLsUsingPeaksWorkspace(
const PeaksWorkspace_sptr &peaksWorkspace,
std::vector<V3D> &possibleHKLs) const {
possibleHKLs.clear();
possibleHKLs.reserve(peaksWorkspace->getNumberPeaks());
bool roundHKL = getProperty("RoundHKL");
/* Q is at the end multiplied with the factor determined in the
* constructor (-1 for crystallography, 1 otherwise). So to avoid
* "flippling HKLs" when it's not required, the HKLs of the input
* workspace are also multiplied by the factor that is appropriate
* for the convention stored in the workspace.
*/
double peaks_q_convention_factor =
get_factor_for_q_convention(peaksWorkspace->getConvention());
for (int i = 0; i < static_cast<int>(peaksWorkspace->getNumberPeaks()); ++i) {
IPeak &p = peaksWorkspace->getPeak(i);
// Get HKL from that peak
V3D hkl = p.getHKL() * peaks_q_convention_factor;
if (roundHKL)
hkl.round();
possibleHKLs.push_back(hkl);
} // for each hkl in the workspace
}
/**
* @brief Assigns a StructureFactorCalculator if available in sample.
*
* This method constructs a StructureFactorCalculator using the CrystalStructure
* stored in sample if available. For consistency it sets the OrientedLattice
* in the sample as the unit cell of the crystal structure.
*
* Additionally, the property CalculateStructureFactors is taken into account.
* If it's disabled, the calculator will not be assigned, disabling structure
* factor calculation.
*
* @param sample :: Sample, potentially with crystal structure
*/
void PredictPeaks::setStructureFactorCalculatorFromSample(
const Sample &sample) {
bool calculateStructureFactors = getProperty("CalculateStructureFactors");
if (calculateStructureFactors && sample.hasCrystalStructure()) {
CrystalStructure crystalStructure = sample.getCrystalStructure();
crystalStructure.setCell(sample.getOrientedLattice());
m_sfCalculator = StructureFactorCalculatorFactory::create<
StructureFactorCalculatorSummation>(crystalStructure);
}
}
/**
* @brief Calculates Q from HKL and adds a peak to the output workspace
*
* This method takes HKL and uses the oriented UB matrix (UB multiplied by the
* goniometer matrix) to calculate Q. It then creates a Peak-object using
* that Q-vector and the internally stored instrument. If the corresponding
* diffracted beam intersects with a detector, the peak is added to the output-
* workspace.
*
* @param hkl
* @param orientedUB
* @param goniometerMatrix
*/
void PredictPeaks::calculateQAndAddToOutput(const V3D &hkl,
const DblMatrix &orientedUB,
const DblMatrix &goniometerMatrix) {
// The q-vector direction of the peak is = goniometer * ub * hkl_vector
// This is in inelastic convention: momentum transfer of the LATTICE!
// Also, q does have a 2pi factor = it is equal to 2pi/wavelength.
V3D q = orientedUB * hkl * (2.0 * M_PI * m_qConventionFactor);
// Create the peak using the Q in the lab framewith all its info:
Peak p(m_inst, q);
/* The constructor calls setQLabFrame, which already calls findDetector, which
is expensive. It's not necessary to call it again, instead it's enough to
check whether a detector has already been set. */
if (p.getDetector()) {
// Only add peaks that hit the detector
p.setGoniometerMatrix(goniometerMatrix);
// Save the run number found before.
p.setRunNumber(m_runNumber);
p.setHKL(hkl);
if (m_sfCalculator) {
p.setIntensity(m_sfCalculator->getFSquared(hkl));
}
// Add it to the workspace
m_pw->addPeak(p);
} // Detector was found
}
} // namespace Mantid
} // namespace Crystal