Mantid
Loading...
Searching...
No Matches
DiffPeaksWorkspaces.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 +
8#include "MantidAPI/Sample.h"
11
12namespace Mantid::Crystal {
13// Register the algorithm into the AlgorithmFactory
14DECLARE_ALGORITHM(DiffPeaksWorkspaces)
15
16using namespace Kernel;
17using namespace API;
18using DataObjects::PeaksWorkspace;
21
23const std::string DiffPeaksWorkspaces::name() const { return "DiffPeaksWorkspaces"; }
25int DiffPeaksWorkspaces::version() const { return 1; }
27const std::string DiffPeaksWorkspaces::category() const { return "Crystal\\Peaks"; }
28
32 declareProperty(std::make_unique<WorkspaceProperty<PeaksWorkspace>>("LHSWorkspace", "", Direction::Input),
33 "The first of peaks.");
34 declareProperty(std::make_unique<WorkspaceProperty<PeaksWorkspace>>("RHSWorkspace", "", Direction::Input),
35 "The second set of peaks.");
36 declareProperty(std::make_unique<WorkspaceProperty<PeaksWorkspace>>("OutputWorkspace", "", Direction::Output),
37 "The set of peaks that are in the first, but not the second, workspace.");
38
39 auto mustBePositive = std::make_shared<BoundedValidator<double>>();
40 mustBePositive->setLower(0.0);
41 // N.B. Andrei reckons it should be delta_q/q
42 declareProperty("Tolerance", 0.0, mustBePositive,
43 "Maximum difference in each component of Q for which peaks "
44 "are considered identical");
45}
46
50 PeaksWorkspace_const_sptr LHSWorkspace = getProperty("LHSWorkspace");
51 PeaksWorkspace_const_sptr RHSWorkspace = getProperty("RHSWorkspace");
52 const double Tolerance = getProperty("Tolerance");
53
54 // Warn if not the same instrument, sample
55 if (LHSWorkspace->getInstrument()->getName() != RHSWorkspace->getInstrument()->getName()) {
56 g_log.warning("The two input workspaces do not appear to come from data "
57 "take on the same instrument");
58 }
59 if (LHSWorkspace->sample().getName() != RHSWorkspace->sample().getName()) {
60 g_log.warning("The two input workspaces do not appear to relate to the same sample");
61 }
62
63 // Copy the first workspace to our output workspace
64 PeaksWorkspace_sptr output(LHSWorkspace->clone());
65 // Get hold of the peaks in the second workspace
66 auto &rhsPeaks = RHSWorkspace->getPeaks();
67 // Get hold of the peaks in the first workspace as we'll need to examine them
68 auto &lhsPeaks = output->getPeaks();
69
70 Progress progress(this, 0.0, 1.0, rhsPeaks.size());
71
72 std::vector<int> badPeaks;
73 // Loop over the peaks in the second workspace, searching for a match in the
74 // first
75 for (const auto &currentPeak : rhsPeaks) {
76 // Now have to go through the first workspace checking for matches
77 // Not doing anything clever as peaks workspace are typically not large -
78 // just a linear search
79 for (int j = 0; j < output->getNumberPeaks(); ++j) {
80 const V3D deltaQ = currentPeak.getQSampleFrame() - lhsPeaks[j].getQSampleFrame();
81 if (deltaQ.nullVector(Tolerance)) // Using a V3D method that does the job
82 {
83 // As soon as we find a match, remove it from the output and move onto
84 // the next rhs peak
85 badPeaks.emplace_back(j);
86 break;
87 }
88 }
89
90 progress.report();
91 }
92 output->removePeaks(std::move(badPeaks));
93 setProperty("OutputWorkspace", output);
94}
95
96} // namespace Mantid::Crystal
#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
Kernel::Logger & g_log
Definition: Algorithm.h:451
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
Definition: Algorithm.cpp:231
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
A property class for workspaces.
void exec() override
Executes the algorithm.
const std::string category() const override
Algorithm's category for identification.
const std::string name() const override
Algorithm's name for identification.
int version() const override
Algorithm's version for identification.
void init() override
Initialises the algorithm's properties.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void warning(const std::string &msg)
Logs at warning level.
Definition: Logger.cpp:86
Class for 3D vectors.
Definition: V3D.h:34
bool nullVector(const double tolerance=1e-3) const noexcept
Determine if the point is null.
Definition: V3D.cpp:241
std::shared_ptr< const PeaksWorkspace > PeaksWorkspace_const_sptr
Typedef for a shared pointer to a const peaks workspace.
std::shared_ptr< PeaksWorkspace > PeaksWorkspace_sptr
Typedef for a shared pointer to a peaks workspace.
constexpr double Tolerance
Standard tolerance value.
Definition: Tolerance.h:12
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54