/
DiffPeaksWorkspaces.cpp
96 lines (84 loc) · 4.02 KB
/
DiffPeaksWorkspaces.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
// 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 "MantidCrystal/DiffPeaksWorkspaces.h"
#include "MantidAPI/Sample.h"
#include "MantidDataObjects/PeaksWorkspace.h"
#include "MantidKernel/BoundedValidator.h"
namespace Mantid::Crystal {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(DiffPeaksWorkspaces)
using namespace Kernel;
using namespace API;
using DataObjects::PeaksWorkspace;
using DataObjects::PeaksWorkspace_const_sptr;
using DataObjects::PeaksWorkspace_sptr;
/// Algorithm's name for identification. @see Algorithm::name
const std::string DiffPeaksWorkspaces::name() const { return "DiffPeaksWorkspaces"; }
/// Algorithm's version for identification. @see Algorithm::version
int DiffPeaksWorkspaces::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string DiffPeaksWorkspaces::category() const { return "Crystal\\Peaks"; }
/** Initialises the algorithm's properties.
*/
void DiffPeaksWorkspaces::init() {
declareProperty(std::make_unique<WorkspaceProperty<PeaksWorkspace>>("LHSWorkspace", "", Direction::Input),
"The first of peaks.");
declareProperty(std::make_unique<WorkspaceProperty<PeaksWorkspace>>("RHSWorkspace", "", Direction::Input),
"The second set of peaks.");
declareProperty(std::make_unique<WorkspaceProperty<PeaksWorkspace>>("OutputWorkspace", "", Direction::Output),
"The set of peaks that are in the first, but not the second, workspace.");
auto mustBePositive = std::make_shared<BoundedValidator<double>>();
mustBePositive->setLower(0.0);
// N.B. Andrei reckons it should be delta_q/q
declareProperty("Tolerance", 0.0, mustBePositive,
"Maximum difference in each component of Q for which peaks "
"are considered identical");
}
/** Executes the algorithm.
*/
void DiffPeaksWorkspaces::exec() {
PeaksWorkspace_const_sptr LHSWorkspace = getProperty("LHSWorkspace");
PeaksWorkspace_const_sptr RHSWorkspace = getProperty("RHSWorkspace");
const double Tolerance = getProperty("Tolerance");
// Warn if not the same instrument, sample
if (LHSWorkspace->getInstrument()->getName() != RHSWorkspace->getInstrument()->getName()) {
g_log.warning("The two input workspaces do not appear to come from data "
"take on the same instrument");
}
if (LHSWorkspace->sample().getName() != RHSWorkspace->sample().getName()) {
g_log.warning("The two input workspaces do not appear to relate to the same sample");
}
// Copy the first workspace to our output workspace
PeaksWorkspace_sptr output(LHSWorkspace->clone());
// Get hold of the peaks in the second workspace
auto &rhsPeaks = RHSWorkspace->getPeaks();
// Get hold of the peaks in the first workspace as we'll need to examine them
auto &lhsPeaks = output->getPeaks();
Progress progress(this, 0.0, 1.0, rhsPeaks.size());
std::vector<int> badPeaks;
// Loop over the peaks in the second workspace, searching for a match in the
// first
for (const auto ¤tPeak : rhsPeaks) {
// Now have to go through the first workspace checking for matches
// Not doing anything clever as peaks workspace are typically not large -
// just a linear search
for (int j = 0; j < output->getNumberPeaks(); ++j) {
const V3D deltaQ = currentPeak.getQSampleFrame() - lhsPeaks[j].getQSampleFrame();
if (deltaQ.nullVector(Tolerance)) // Using a V3D method that does the job
{
// As soon as we find a match, remove it from the output and move onto
// the next rhs peak
badPeaks.emplace_back(j);
break;
}
}
progress.report();
}
output->removePeaks(std::move(badPeaks));
setProperty("OutputWorkspace", output);
}
} // namespace Mantid::Crystal