Mantid
Loading...
Searching...
No Matches
SumRowColumn.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 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
11#include "MantidAPI/Axis.h"
16#include "MantidKernel/Unit.h"
17
18namespace Mantid::Algorithms {
19
20// Register the algorithm into the AlgorithmFactory
21DECLARE_ALGORITHM(SumRowColumn)
22
23using namespace Mantid::Kernel;
24using namespace Mantid::API;
25
27 // Assume input workspace has correct spectra in it - no more and no less
28 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input),
29 "The input workspace, which must contain all the spectra from the bank "
30 "of interest - no more and no less (so 128x128 or 192x192).");
31 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
32 "The name of the workspace in which to store the result.");
33
34 // Need to select whether to sum rows or columns
35 std::vector<std::string> orientation{"D_H", "D_V"};
36 declareProperty("Orientation", "", std::make_shared<StringListValidator>(orientation),
37 "Whether to sum rows (D_H) or columns (D_V).");
38
39 // This is the range to select - the whole lot by default
40 declareProperty("XMin", EMPTY_DBL(), "The starting X value for each spectrum to include in the summation.");
41 declareProperty("XMax", EMPTY_DBL(), "The ending X value for each spectrum to include in the summation.");
42
43 // For selecting a column range - the whole lot by default
44 auto mustBePositive = std::make_shared<BoundedValidator<int>>();
45 mustBePositive->setLower(0);
46 declareProperty("HOverVMin", EMPTY_INT(), mustBePositive,
47 "The first row to include in the summation when summing by "
48 "columns, or vice versa.");
49 declareProperty("HOverVMax", EMPTY_INT(), mustBePositive,
50 "The last row to include in the summation when summing by "
51 "columns, or vice versa.");
52}
53
55 // First task is to integrate the input workspace
57
58 const size_t numSpec = integratedWS->getNumberHistograms();
59 // Check number of spectra is 128*128 or 192*192. Print warning if not.
60 if (numSpec != 16384 && numSpec != 36864) {
61 g_log.warning() << "The input workspace has " << numSpec << " spectra."
62 << "This is not 128*128 or 192*192 - did you make a mistake?\n";
63 }
64
65 // This is the dimension if all rows/columns are included
66 const auto dim = static_cast<int>(std::sqrt(static_cast<double>(numSpec)));
67
68 // Check the column range properties
69 int start = getProperty("HOverVMin");
70 int end = getProperty("HOverVMax");
71 if (isEmpty(start))
72 start = 0;
73 if (isEmpty(end) || end > dim - 1)
74 end = dim - 1;
75 if (start > end) {
76 g_log.error("H/V_Min must be less than H/V_Max");
77 throw std::out_of_range("H/V_Min must be less than H/V_Max");
78 }
79
80 MatrixWorkspace_sptr outputWS = WorkspaceFactory::Instance().create(integratedWS, 1, dim, dim);
81 // Remove the unit
82 outputWS->getAxis(0)->unit().reset(new Mantid::Kernel::Units::Empty);
83
84 // Get references to the vectors for the results
85 auto &X = outputWS->mutableX(0);
86 auto &Y = outputWS->mutableY(0);
87
88 // Get the orientation
89 const std::string orientation = getProperty("Orientation");
90 const bool horizontal = (orientation == "D_H" ? 1 : 0);
91
92 Progress progress(this, 0.0, 1.0, dim);
93 for (int i = 0; i < dim; ++i) {
94 // Copy X values
95 X[i] = i;
96
97 // Now loop over calculating Y's
98 for (int j = start; j <= end; ++j) {
99 const int index = (horizontal ? (i + j * dim) : (i * dim + j));
100 Y[i] += integratedWS->y(index)[0];
101 }
102 }
103
104 setProperty("OutputWorkspace", outputWS);
105}
106
111 g_log.debug() << "Integrating input workspace\n";
112
113 auto childAlg = createChildAlgorithm("Integration");
114 // pass inputed values straight to this Child Algorithm, checking must be done
115 // there
116 childAlg->setProperty<MatrixWorkspace_sptr>("InputWorkspace", getProperty("InputWorkspace"));
117 childAlg->setProperty<double>("RangeLower", getProperty("XMin"));
118 childAlg->setProperty<double>("RangeUpper", getProperty("XMax"));
119 childAlg->executeAsChildAlg();
120 return childAlg->getProperty("OutputWorkspace");
121}
122
123} // namespace Mantid::Algorithms
#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
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
static bool isEmpty(const NumT toCheck)
checks that the value was not set by users, uses the value in empty double/int.
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
A property class for workspaces.
API::MatrixWorkspace_sptr integrateWorkspace()
Call Integration as a Child Algorithm.
void init() override
Initialisation code.
void exec() override
Execution code.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void debug(const std::string &msg)
Logs at debug level.
Definition: Logger.cpp:114
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
void warning(const std::string &msg)
Logs at warning level.
Definition: Logger.cpp:86
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
MANTID_GEOMETRY_DLL PolygonEdge::Orientation orientation(const PolygonEdge &focusEdge, const PolygonEdge &refEdge, double &t)
Calculate the orientation type of one edge wrt to another.
Definition: PolygonEdge.cpp:81
constexpr int EMPTY_INT() noexcept
Returns what we consider an "empty" integer within a property.
Definition: EmptyValues.h:25
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
Definition: EmptyValues.h:43
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54