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 {
22struct Square {
23 void operator()(double &i) { i *= i; }
24};
25
26// Register the algorithm into the AlgorithmFactory
27DECLARE_ALGORITHM(CreateMDHistoWorkspace)
28
29//----------------------------------------------------------------------------------------------
31const std::string CreateMDHistoWorkspace::name() const { return "CreateMDHistoWorkspace"; }
32
34int CreateMDHistoWorkspace::version() const { return 1; }
35
37const std::string CreateMDHistoWorkspace::category() const { return "MDAlgorithms\\Creation"; }
38
39//----------------------------------------------------------------------------------------------
40
41//----------------------------------------------------------------------------------------------
45 auto validator = std::make_shared<CompositeValidator>();
46 validator->add(std::make_shared<BoundedValidator<int>>(1, 9));
47 validator->add(std::make_shared<MandatoryValidator<int>>());
48 auto mandatoryIntArrayValidator = std::make_shared<MandatoryValidator<std::vector<int>>>();
49 auto mandatoryDoubleArrayValidator = std::make_shared<MandatoryValidator<std::vector<double>>>();
50 auto mandatoryStrArrayValidator = std::make_shared<MandatoryValidator<std::vector<std::string>>>();
51
52 declareProperty(std::make_unique<ArrayProperty<double>>("SignalInput", mandatoryDoubleArrayValidator),
53 "Signal array for n-dimensional workspace");
54
55 declareProperty(std::make_unique<ArrayProperty<double>>("ErrorInput", mandatoryDoubleArrayValidator),
56 "Error array for n-dimensional workspace");
57
58 declareProperty(std::make_unique<ArrayProperty<double>>("NumberOfEvents", std::vector<double>(0)),
59 "Number of pixels array for n-dimensional workspace. Optional, defaults "
60 "to 1 per bin.");
61 declareProperty(std::make_unique<PropertyWithValue<int>>("Dimensionality", -1, validator, Direction::Input),
62 "Dimensionality of the data in the file.");
63
64 declareProperty(std::make_unique<ArrayProperty<double>>("Extents", mandatoryDoubleArrayValidator),
65 "A comma separated list of min, max for each dimension,\n"
66 "specifying the extents of each dimension.");
67
68 declareProperty(std::make_unique<ArrayProperty<int>>("NumberOfBins", mandatoryIntArrayValidator),
69 "Number of bin in each dimension.");
70
71 declareProperty(std::make_unique<ArrayProperty<std::string>>("Names", mandatoryStrArrayValidator),
72 "A comma separated list of the name of each dimension. "
73 "e.g. ('[H,0,0]','[0,K,0]','[0,0,L]') ");
74
75 declareProperty(std::make_unique<ArrayProperty<std::string>>("Units", mandatoryStrArrayValidator),
76 "A comma separated list of the units of each dimension.");
77
78 declareProperty(std::make_unique<WorkspaceProperty<IMDHistoWorkspace>>("OutputWorkspace", "", Direction::Output),
79 "MDHistoWorkspace reflecting the input text file.");
80 declareProperty(std::make_unique<ArrayProperty<std::string>>("Frames"),
81 " A comma separated list of the frames of each dimension. "
82 " The frames can be"
83 " **General Frame**: Any frame which is not a Q-based frame."
84 " **QLab**: Wave-vector converted into the lab frame."
85 " **QSample**: Wave-vector converted into the frame of the sample."
86 " **HKL**: Wave-vector converted into the crystal's HKL indices."
87 " Note if nothing is specified then the **General Frame** is being "
88 "selected. Also note that if you select a frame then this might override "
89 "your unit selection if it is not compatible with the frame.");
90}
91
92//----------------------------------------------------------------------------------------------
97 auto signals = ws->mutableSignalArray();
98 auto errors = ws->mutableErrorSquaredArray();
99 auto nEvents = ws->mutableNumEventsArray();
100
101 std::vector<double> signalValues = getProperty("SignalInput");
102 std::vector<double> errorValues = getProperty("ErrorInput");
103 std::vector<double> numberOfEvents = getProperty("NumberOfEvents");
104
105 size_t binProduct = this->getBinProduct();
106 std::stringstream stream;
107 stream << binProduct;
108 if (binProduct != signalValues.size()) {
109 throw std::invalid_argument("Expected size of the SignalInput is: " + stream.str());
110 }
111 if (binProduct != errorValues.size()) {
112 throw std::invalid_argument("Expected size of the ErrorInput is: " + stream.str());
113 }
114 if (!numberOfEvents.empty() && binProduct != numberOfEvents.size()) {
115 throw std::invalid_argument("Expected size of the NumberOfEvents is: " + stream.str() +
116 ". Leave empty to auto fill with 1.0");
117 }
118
119 // Auto fill number of events.
120 if (numberOfEvents.empty()) {
121 numberOfEvents = std::vector<double>(binProduct, 1.0);
122 }
123
124 // Copy from property
125 std::copy(signalValues.begin(), signalValues.end(), signals);
126 std::vector<double> empty;
127 // Clean up.
128 signalValues.swap(empty);
129 // Copy from property
130 std::for_each(errorValues.begin(), errorValues.end(), Square());
131 std::copy(errorValues.begin(), errorValues.end(), errors);
132 // Clean up
133 errorValues.swap(empty);
134 // Copy from property
135 std::copy(numberOfEvents.begin(), numberOfEvents.end(), nEvents);
136 // Clean up
137 numberOfEvents.swap(empty);
138
139 setProperty("OutputWorkspace", ws);
140}
141
142} // namespace Mantid::MDAlgorithms
#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
A property class for workspaces.
Support for a property that holds an array of values.
Definition: ArrayProperty.h:28
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.
void exec() override
Execute 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.
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54
Helper type to compute the square in-place.