Mantid
Loading...
Searching...
No Matches
CentroidPeaksMD2.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 +
15#include "MantidKernel/System.h"
17
18namespace Mantid::MDAlgorithms {
19
20// Register the algorithm into the AlgorithmFactory
21DECLARE_ALGORITHM(CentroidPeaksMD2)
22
23using namespace Mantid::API;
24using namespace Mantid::DataObjects;
25using namespace Mantid::Geometry;
26using namespace Mantid::Kernel;
27using namespace Mantid::DataObjects;
28
29//----------------------------------------------------------------------------------------------
33 declareProperty(std::make_unique<WorkspaceProperty<IMDEventWorkspace>>("InputWorkspace", "", Direction::Input),
34 "An input MDEventWorkspace.");
35
36 declareProperty(std::make_unique<PropertyWithValue<double>>("PeakRadius", 1.0, Direction::Input),
37 "Fixed radius around each peak position in which to calculate the "
38 "centroid.");
39
40 declareProperty(std::make_unique<WorkspaceProperty<IPeaksWorkspace>>("PeaksWorkspace", "", Direction::Input),
41 "A PeaksWorkspace containing the peaks to centroid.");
42
43 declareProperty(std::make_unique<WorkspaceProperty<IPeaksWorkspace>>("OutputWorkspace", "", Direction::Output),
44 "The output PeaksWorkspace will be a copy of the input PeaksWorkspace "
45 "with the peaks' positions modified by the new found centroids.");
46}
47
48//----------------------------------------------------------------------------------------------
53template <typename MDE, size_t nd> void CentroidPeaksMD2::integrate(typename MDEventWorkspace<MDE, nd>::sptr ws) {
54 if (nd != 3)
55 throw std::invalid_argument("For now, we expect the input MDEventWorkspace "
56 "to have 3 dimensions only.");
57
59 IPeaksWorkspace_sptr inPeakWS = getProperty("PeaksWorkspace");
60
62 IPeaksWorkspace_sptr peakWS = getProperty("OutputWorkspace");
63
64 if (peakWS != inPeakWS)
65 peakWS = inPeakWS->clone();
66
67 int CoordinatesToUse = ws->getSpecialCoordinateSystem();
68
70 double PeakRadius = getProperty("PeakRadius");
71
72 PRAGMA_OMP(parallel for schedule(dynamic, 10) )
73 for (int i = 0; i < int(peakWS->getNumberPeaks()); ++i) {
74 // Get a direct ref to that peak.
75 IPeak &p = peakWS->getPeak(i);
76 Peak *peak = dynamic_cast<Peak *>(&p);
77 double detectorDistance = 0.;
78 if (peak)
79 detectorDistance = p.getL2();
80
81 // Get the peak center as a position in the dimensions of the workspace
82 V3D pos;
83 if (CoordinatesToUse == 1) //"Q (lab frame)"
84 pos = p.getQLabFrame();
85 else if (CoordinatesToUse == 2) //"Q (sample frame)"
86 pos = p.getQSampleFrame();
87 else if (CoordinatesToUse == 3) //"HKL"
88 pos = p.getHKL();
89
90 // Build the sphere transformation
91 bool dimensionsUsed[nd];
92 coord_t center[nd];
93 for (size_t d = 0; d < nd; ++d) {
94 dimensionsUsed[d] = true; // Use all dimensions
95 center[d] = static_cast<coord_t>(pos[d]);
96 }
97 CoordTransformDistance sphere(nd, center, dimensionsUsed);
98
99 // Initialize the centroid to 0.0
100 signal_t signal = 0;
101 coord_t centroid[nd];
102 for (size_t d = 0; d < nd; d++)
103 centroid[d] = 0.0;
104
105 // Perform centroid
106 ws->getBox()->centroidSphere(sphere, static_cast<coord_t>(PeakRadius * PeakRadius), centroid, signal);
107
108 // Normalize by signal
109 if (signal != 0.0) {
110 for (size_t d = 0; d < nd; d++)
111 centroid[d] /= static_cast<coord_t>(signal);
112
113 V3D vecCentroid(centroid[0], centroid[1], centroid[2]);
114 p.setBinCount(static_cast<double>(signal));
115
116 // Save it back in the peak object, in the dimension specified.
117 try {
118 if (CoordinatesToUse == 1) //"Q (lab frame)"
119 {
120 p.setQLabFrame(vecCentroid, detectorDistance);
121 if (peak)
122 peak->findDetector();
123 } else if (CoordinatesToUse == 2) //"Q (sample frame)"
124 {
125 p.setQSampleFrame(vecCentroid, detectorDistance);
126 if (peak)
127 peak->findDetector();
128 } else if (CoordinatesToUse == 3) //"HKL"
129 {
130 p.setHKL(vecCentroid);
131 }
132 } catch (std::exception &e) {
133 g_log.warning() << "Error setting Q or HKL\n";
134 g_log.warning() << e.what() << '\n';
135 }
136
137 g_log.information() << "Peak " << i << " at " << pos << ": signal " << signal << ", centroid " << vecCentroid
138 << " in " << CoordinatesToUse << '\n';
139 } else {
140 g_log.information() << "Peak " << i << " at " << pos << " had no signal, and could not be centroided.\n";
141 }
142 }
143
144 // Save the output
145 setProperty("OutputWorkspace", peakWS);
146}
147
148//----------------------------------------------------------------------------------------------
152 inWS = getProperty("InputWorkspace");
153
155}
156
157} // namespace Mantid::MDAlgorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
#define CALL_MDEVENT_FUNCTION3(funcname, workspace)
Macro that makes it possible to call a templated method for a MDEventWorkspace using a IMDEventWorksp...
#define PRAGMA_OMP(expression)
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
A property class for workspaces.
Unique CoordCenterVectorParam type declaration for ndimensional coordinate centers.
void centroidSphere(Mantid::API::CoordTransform &radiusTransform, const coord_t radiusSquared, coord_t *centroid, signal_t &signal) const override=0
Find the centroid around a sphere.
std::shared_ptr< MDEventWorkspace< MDE, nd > > sptr
Typedef for a shared pointer of this kind of event workspace.
Kernel::SpecialCoordinateSystem getSpecialCoordinateSystem() const override
Get the coordinate system.
Structure describing a single-crystal peak.
Definition: Peak.h:34
bool findDetector()
After creating a peak using the Q in the lab frame, the detPos is set to the direction of the detecto...
Definition: Peak.cpp:613
Structure describing a single-crystal peak.
Definition: IPeak.h:26
virtual void setHKL(double H, double K, double L)=0
virtual void setBinCount(double m_BinCount)=0
virtual void setQLabFrame(const Mantid::Kernel::V3D &QLabFrame, boost::optional< double > detectorDistance)=0
virtual double getL2() const =0
virtual Mantid::Kernel::V3D getQSampleFrame() const =0
virtual Mantid::Kernel::V3D getQLabFrame() const =0
virtual void setQSampleFrame(const Mantid::Kernel::V3D &QSampleFrame, boost::optional< double > detectorDistance)=0
virtual Mantid::Kernel::V3D getHKL() const =0
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
void information(const std::string &msg)
Logs at information level.
Definition: Logger.cpp:105
The concrete, templated class for properties.
Class for 3D vectors.
Definition: V3D.h:34
void exec() override
Run the algorithm.
void init() override
Initialise the properties.
Mantid::API::IMDEventWorkspace_sptr inWS
Input MDEventWorkspace.
void integrate(typename DataObjects::MDEventWorkspace< MDE, nd >::sptr ws)
Integrate the peaks of the workspace using parameters saved in the algorithm class.
std::shared_ptr< IPeaksWorkspace > IPeaksWorkspace_sptr
shared pointer to Mantid::API::IPeaksWorkspace
float coord_t
Typedef for the data type to use for coordinate axes in MD objects such as MDBox, MDEventWorkspace,...
Definition: MDTypes.h:27
double signal_t
Typedef for the signal recorded in a MDBox, etc.
Definition: MDTypes.h:36
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54