Mantid
Loading...
Searching...
No Matches
CompactMD.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 +
9
10#include <boost/lexical_cast.hpp>
11
12using namespace Mantid::API;
13using namespace Mantid::Geometry;
14using namespace Mantid::Kernel;
15
16namespace {
30std::vector<std::string> createPBinStringVector(std::vector<Mantid::coord_t> minVector,
31 std::vector<Mantid::coord_t> maxVector,
32 const IMDHistoWorkspace_sptr &inputWs) {
33 size_t numDims = inputWs->getNumDims();
34 std::vector<std::string> pBinStrVector;
35 for (size_t iter = 0; iter < numDims; iter++) {
36 // creating pbin string using Min and Max Centre positions
37 auto pBinStr =
38 boost::lexical_cast<std::string>(minVector[iter] - (inputWs->getDimension(iter)->getBinWidth() * 0.5)) + ",0," +
39 boost::lexical_cast<std::string>(maxVector[iter] + (inputWs->getDimension(iter)->getBinWidth() * 0.5));
40 pBinStrVector.emplace_back(pBinStr);
41 }
42 return pBinStrVector;
43}
44} // namespace
45
46namespace Mantid::MDAlgorithms {
47
48DECLARE_ALGORITHM(CompactMD)
49
50
61void CompactMD::findFirstNonZeroMinMaxExtents(const IMDHistoWorkspace_sptr &inputWs,
62 std::vector<Mantid::coord_t> &minVec,
63 std::vector<Mantid::coord_t> &maxVec) {
64 auto ws_iter = inputWs->createIterator();
65 do {
66 if (ws_iter->getSignal() == 0) {
67 // if signal is 0 then go to next index
68 continue;
69 } else {
70 // we have found a non-zero signal we need to compare
71 // the position of the bin with our Min and Max values
72 auto current_index = ws_iter->getLinearIndex();
73 auto current_center = inputWs->getCenter(current_index);
74 for (size_t index = 0; index < inputWs->getNumDims(); index++) {
75 if (current_center[index] > maxVec[index]) {
76 // set new maximum
77 maxVec[index] = current_center[index];
78 }
79 if (current_center[index] < minVec[index]) {
80 // set new minimum
81 minVec[index] = current_center[index];
82 }
83 }
84 }
85 } while (ws_iter->next());
86 // check that min/max vector have changed, if not then we set them to original
87 // extents of the workspace
88 for (size_t index = 0; index < inputWs->getNumDims(); index++) {
89 // if min/max for a dimension haven't changed then there were
90 // no signals found in that dimension. We must set the min and max
91 // for that dimension to the actual min and max extents respectively
92 // which will stop that dimension being cropped or causing errors
93 // when passed to IntegrateMDHistoWorkspace
94 if (minVec[index] == inputWs->getDimension(index)->getMaximum()) {
95 minVec[index] = inputWs->getDimension(index)->getMinimum();
96 }
97 if (maxVec[index] == inputWs->getDimension(index)->getMinimum()) {
98 maxVec[index] = inputWs->getDimension(index)->getMaximum();
99 }
100 }
101}
102
107 // input workspace to compact
108 declareProperty(std::make_unique<WorkspaceProperty<IMDHistoWorkspace>>("InputWorkspace", "", Direction::Input),
109 "MDHistoWorkspace to compact");
110 // output workspace that will have been compacted
111 declareProperty(std::make_unique<WorkspaceProperty<IMDHistoWorkspace>>("OutputWorkspace", "", Direction::Output),
112 "Output compacted workspace");
113}
118 const IMDHistoWorkspace_sptr input_ws = this->getProperty("InputWorkspace");
119 IMDWorkspace_sptr out_ws;
120
121 const size_t nDimensions = input_ws->getNumDims();
122 std::vector<Mantid::coord_t> minVector;
123 std::vector<Mantid::coord_t> maxVector;
124
125 // fill the min/max vectors with values per dimension.
126 for (size_t index = 0; index < nDimensions; index++) {
127 minVector.emplace_back(input_ws->getDimension(index)->getMaximum());
128 maxVector.emplace_back(input_ws->getDimension(index)->getMinimum());
129 }
130 // start our search for the first non-zero signal index.
131 findFirstNonZeroMinMaxExtents(input_ws, minVector, maxVector);
132 auto pBinStrings = createPBinStringVector(minVector, maxVector, input_ws);
133 // creating IntegrateMDHistoWorkspace algorithm to crop our workspace.
134 auto cut_alg = this->createChildAlgorithm("IntegrateMDHistoWorkspace");
135 cut_alg->setProperty("InputWorkspace", input_ws);
136 cut_alg->setProperty("OutputWorkspace", "temp");
137 // setting property PxBin depending on the number of dimensions the
138 // input workspace has.
139 for (size_t iter = 0; iter < input_ws->getNumDims(); iter++) {
140 std::string propertyString = "P" + std::to_string(iter + 1) + "Bin";
141 cut_alg->setProperty(propertyString, pBinStrings[iter]);
142 }
143 cut_alg->execute();
144
145 // retrieve the output workspace from IntegrateMDHistoWorkspace
146 IMDHistoWorkspace_sptr temp = cut_alg->getProperty("OutputWorkspace");
147 out_ws = temp;
148 // set output workspace of CompactMD to output of IntegrateMDHistoWorkspace
149 this->setProperty("OutputWorkspace", out_ws);
150}
151} // namespace Mantid::MDAlgorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
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
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
Definition: Algorithm.cpp:842
A property class for workspaces.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void init() override
Initiliase the algorithm's properties.
Definition: CompactMD.cpp:106
void findFirstNonZeroMinMaxExtents(const Mantid::API::IMDHistoWorkspace_sptr &inputWs, std::vector< Mantid::coord_t > &minVec, std::vector< Mantid::coord_t > &maxVec)
Finding the extents of the first non-zero signals.
Definition: CompactMD.cpp:61
void exec() override
Execute the algorithm.
Definition: CompactMD.cpp:117
std::shared_ptr< IMDHistoWorkspace > IMDHistoWorkspace_sptr
shared pointer to Mantid::API::IMDHistoWorkspace
std::shared_ptr< IMDWorkspace > IMDWorkspace_sptr
Shared pointer to the IMDWorkspace base class.
Definition: IMDWorkspace.h:146
Helper class which provides the Collimation Length for SANS instruments.
float coord_t
Typedef for the data type to use for coordinate axes in MD objects such as MDBox, MDEventWorkspace,...
Definition: MDTypes.h:27
STL namespace.
std::string to_string(const wide_integer< Bits, Signed > &n)
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54