Mantid
Loading...
Searching...
No Matches
CreateMDHistoWorkspace.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 +
12#include <algorithm>
13
14using namespace Mantid::Kernel;
15using namespace Mantid::API;
16using namespace Mantid::DataObjects;
17
18namespace Mantid::MDAlgorithms {
19
20// Register the algorithm into the AlgorithmFactory
21DECLARE_ALGORITHM(CreateMDHistoWorkspace)
22
23//----------------------------------------------------------------------------------------------
25const std::string CreateMDHistoWorkspace::name() const { return "CreateMDHistoWorkspace"; }
26
28int CreateMDHistoWorkspace::version() const { return 1; }
29
31const std::string CreateMDHistoWorkspace::category() const { return "MDAlgorithms\\Creation"; }
32
33//----------------------------------------------------------------------------------------------
34
35//----------------------------------------------------------------------------------------------
39 auto validator = std::make_shared<CompositeValidator>();
40 validator->add(std::make_shared<BoundedValidator<int>>(1, 9));
41 validator->add(std::make_shared<MandatoryValidator<int>>());
42 auto mandatoryIntArrayValidator = std::make_shared<MandatoryValidator<std::vector<int>>>();
43 auto mandatoryDoubleArrayValidator = std::make_shared<MandatoryValidator<std::vector<double>>>();
44 auto mandatoryStrArrayValidator = std::make_shared<MandatoryValidator<std::vector<std::string>>>();
45
46 declareProperty(std::make_unique<ArrayProperty<double>>("SignalInput", mandatoryDoubleArrayValidator),
47 "Signal array for n-dimensional workspace");
48
49 declareProperty(std::make_unique<ArrayProperty<double>>("ErrorInput", mandatoryDoubleArrayValidator),
50 "Error array for n-dimensional workspace");
51
52 declareProperty(std::make_unique<ArrayProperty<double>>("NumberOfEvents", std::vector<double>(0)),
53 "Number of pixels array for n-dimensional workspace. Optional, defaults "
54 "to 1 per bin.");
55 declareProperty(std::make_unique<PropertyWithValue<int>>("Dimensionality", -1, validator, Direction::Input),
56 "Dimensionality of the data in the file.");
57
58 declareProperty(std::make_unique<ArrayProperty<double>>("Extents", mandatoryDoubleArrayValidator),
59 "A comma separated list of min, max for each dimension,\n"
60 "specifying the extents of each dimension.");
61
62 declareProperty(std::make_unique<ArrayProperty<int>>("NumberOfBins", mandatoryIntArrayValidator),
63 "Number of bin in each dimension.");
64
65 declareProperty(std::make_unique<ArrayProperty<std::string>>("Names", mandatoryStrArrayValidator),
66 "A comma separated list of the name of each dimension. "
67 "e.g. ('[H,0,0]','[0,K,0]','[0,0,L]') ");
68
69 declareProperty(std::make_unique<ArrayProperty<std::string>>("Units", mandatoryStrArrayValidator),
70 "A comma separated list of the units of each dimension.");
71
72 declareProperty(std::make_unique<WorkspaceProperty<IMDHistoWorkspace>>("OutputWorkspace", "", Direction::Output),
73 "MDHistoWorkspace reflecting the input text file.");
74 declareProperty(std::make_unique<ArrayProperty<std::string>>("Frames"),
75 " A comma separated list of the frames of each dimension. "
76 " The frames can be"
77 " **General Frame**: Any frame which is not a Q-based frame."
78 " **QLab**: Wave-vector converted into the lab frame."
79 " **QSample**: Wave-vector converted into the frame of the sample."
80 " **HKL**: Wave-vector converted into the crystal's HKL indices."
81 " Note if nothing is specified then the **General Frame** is being "
82 "selected. Also note that if you select a frame then this might override "
83 "your unit selection if it is not compatible with the frame.");
84}
85
86std::map<std::string, std::string> CreateMDHistoWorkspace::validateInputs() {
87 std::map<std::string, std::string> errors;
88
89 const std::vector<double> &signalValues = getProperty("SignalInput");
90 const std::vector<double> &errorValues = getProperty("ErrorInput");
91 const std::vector<double> &numberOfEvents = getProperty("NumberOfEvents");
92
93 const std::string msg("All inputs must match size: " + std::to_string(signalValues.size()));
94
95 if (signalValues.size() != errorValues.size()) {
96 errors["SignalInput"] = msg;
97 errors["ErrorInput"] = msg;
98 }
99 // don't need to add message to empty array
100 if ((!numberOfEvents.empty()) && (numberOfEvents.size() != signalValues.size()))
101 errors["NumberOfEvents"] = msg;
102
103 return errors;
104}
105
106//----------------------------------------------------------------------------------------------
111 auto signals = ws->mutableSignalArray();
112 auto errors = ws->mutableErrorSquaredArray();
113 auto nEvents = ws->mutableNumEventsArray();
114 const size_t binProduct = this->getBinProduct();
115
116 const std::vector<double> &signalValues = getProperty("SignalInput");
117 const std::vector<double> &errorValues = getProperty("ErrorInput");
118 const std::vector<double> &numberOfEvents = getProperty("NumberOfEvents");
119
120 // this->createEmptyOutputWorkspace() initializes the value returned by this->getBinProduct()
121 if (signalValues.size() != binProduct) {
122 const std::string msg("All inputs must match size: " + std::to_string(binProduct));
123 throw std::invalid_argument(msg);
124 }
125
126 // Fast memory copies and squaring
127 std::memcpy(signals, signalValues.data(), binProduct * sizeof(double));
128 std::transform(errorValues.cbegin(), errorValues.cend(), errors, // first element
129 [](const auto &value) { return value * value; });
130
131 if (numberOfEvents.empty()) {
132 std::fill(nEvents, nEvents + binProduct, 1.0);
133 } else {
134 std::memcpy(nEvents, numberOfEvents.data(), binProduct * sizeof(double));
135 }
136
137 setProperty("OutputWorkspace", ws);
138}
139
140} // namespace Mantid::MDAlgorithms
std::string name
Definition Run.cpp:60
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
double value
The value of the point.
Definition FitMW.cpp:51
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
A property class for workspaces.
Support for a property that holds an array of values.
BoundedValidator is a validator that requires the values to be between upper or lower bounds,...
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
Validator to check that a property is not left empty.
The concrete, templated class for properties.
void init() override
Initialize the algorithm's properties.
int version() const override
Algorithm's version for identification.
const std::string category() const override
Algorithm's category for identification.
std::map< std::string, std::string > validateInputs() override
Perform validation of ALL the input properties of the algorithm.
DataObjects::MDHistoWorkspace_sptr createEmptyOutputWorkspace()
Creates an empty md histo workspace (with dimensions)
size_t getBinProduct() const
Getter for the number of bins (product accross all dimensions)
std::shared_ptr< MDHistoWorkspace > MDHistoWorkspace_sptr
A shared pointer to a MDHistoWorkspace.
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