-
Notifications
You must be signed in to change notification settings - Fork 124
/
CreateMDHistoWorkspace.cpp
158 lines (135 loc) · 6.42 KB
/
CreateMDHistoWorkspace.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "MantidMDAlgorithms/CreateMDHistoWorkspace.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/MandatoryValidator.h"
#include "MantidKernel/CompositeValidator.h"
#include "MantidKernel/BoundedValidator.h"
#include <algorithm>
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::DataObjects;
namespace Mantid {
namespace MDAlgorithms {
/**
Helper type to compute the square in-place.
*/
struct Square : public std::unary_function<double, void> {
void operator()(double &i) { i *= i; }
};
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(CreateMDHistoWorkspace)
//----------------------------------------------------------------------------------------------
/// Algorithm's name for identification. @see Algorithm::name
const std::string CreateMDHistoWorkspace::name() const {
return "CreateMDHistoWorkspace";
}
/// Algorithm's version for identification. @see Algorithm::version
int CreateMDHistoWorkspace::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string CreateMDHistoWorkspace::category() const {
return "MDAlgorithms\\Creation";
}
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void CreateMDHistoWorkspace::init() {
auto validator = boost::make_shared<CompositeValidator>();
validator->add(boost::make_shared<BoundedValidator<int>>(1, 9));
validator->add(boost::make_shared<MandatoryValidator<int>>());
auto mandatoryIntArrayValidator =
boost::make_shared<MandatoryValidator<std::vector<int>>>();
auto mandatoryDoubleArrayValidator =
boost::make_shared<MandatoryValidator<std::vector<double>>>();
auto mandatoryStrArrayValidator =
boost::make_shared<MandatoryValidator<std::vector<std::string>>>();
declareProperty(Kernel::make_unique<ArrayProperty<double>>(
"SignalInput", mandatoryDoubleArrayValidator),
"Signal array for n-dimensional workspace");
declareProperty(Kernel::make_unique<ArrayProperty<double>>(
"ErrorInput", mandatoryDoubleArrayValidator),
"Error array for n-dimensional workspace");
declareProperty(
Kernel::make_unique<ArrayProperty<double>>("NumberOfEvents",
std::vector<double>(0)),
"Number of pixels array for n-dimensional workspace. Optional, defaults "
"to 1 per bin.");
declareProperty(make_unique<PropertyWithValue<int>>(
"Dimensionality", -1, validator, Direction::Input),
"Dimensionality of the data in the file.");
declareProperty(Kernel::make_unique<ArrayProperty<double>>(
"Extents", mandatoryDoubleArrayValidator),
"A comma separated list of min, max for each dimension,\n"
"specifying the extents of each dimension.");
declareProperty(Kernel::make_unique<ArrayProperty<int>>(
"NumberOfBins", mandatoryIntArrayValidator),
"Number of bin in each dimension.");
declareProperty(Kernel::make_unique<ArrayProperty<std::string>>(
"Names", mandatoryStrArrayValidator),
"A comma separated list of the name of each dimension.");
declareProperty(Kernel::make_unique<ArrayProperty<std::string>>(
"Units", mandatoryStrArrayValidator),
"A comma separated list of the units of each dimension.");
declareProperty(make_unique<WorkspaceProperty<IMDHistoWorkspace>>(
"OutputWorkspace", "", Direction::Output),
"MDHistoWorkspace reflecting the input text file.");
declareProperty(
make_unique<ArrayProperty<std::string>>("Frames"),
" A comma separated list of the frames of each dimension. "
" The frames can be"
" **General Frame**: Any frame which is not a Q-based frame."
" **QLab**: Wave-vector converted into the lab frame."
" **QSample**: Wave-vector converted into the frame of the sample."
" **HKL**: Wave-vector converted into the crystal's HKL indices."
" Note if nothing is specified then the **General Frame** is being "
"selected. Also note that if you select a frame then this might override "
"your unit selection if it is not compatible with the frame.");
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void CreateMDHistoWorkspace::exec() {
MDHistoWorkspace_sptr ws = this->createEmptyOutputWorkspace();
Mantid::signal_t *signals = ws->getSignalArray();
Mantid::signal_t *errors = ws->getErrorSquaredArray();
Mantid::signal_t *nEvents = ws->getNumEventsArray();
std::vector<double> signalValues = getProperty("SignalInput");
std::vector<double> errorValues = getProperty("ErrorInput");
std::vector<double> numberOfEvents = getProperty("NumberOfEvents");
size_t binProduct = this->getBinProduct();
std::stringstream stream;
stream << binProduct;
if (binProduct != signalValues.size()) {
throw std::invalid_argument("Expected size of the SignalInput is: " +
stream.str());
}
if (binProduct != errorValues.size()) {
throw std::invalid_argument("Expected size of the ErrorInput is: " +
stream.str());
}
if (!numberOfEvents.empty() && binProduct != numberOfEvents.size()) {
throw std::invalid_argument("Expected size of the NumberOfEvents is: " +
stream.str() +
". Leave empty to auto fill with 1.0");
}
// Auto fill number of events.
if (numberOfEvents.empty()) {
numberOfEvents = std::vector<double>(binProduct, 1.0);
}
// Copy from property
std::copy(signalValues.begin(), signalValues.end(), signals);
std::vector<double> empty;
// Clean up.
signalValues.swap(empty);
// Copy from property
std::for_each(errorValues.begin(), errorValues.end(), Square());
std::copy(errorValues.begin(), errorValues.end(), errors);
// Clean up
errorValues.swap(empty);
// Copy from property
std::copy(numberOfEvents.begin(), numberOfEvents.end(), nEvents);
// Clean up
numberOfEvents.swap(empty);
setProperty("OutputWorkspace", ws);
}
} // namespace Mantid
} // namespace DataObjects