40 std::vector<std::string> unitOptions = UnitFactory::Instance().getKeys();
41 unitOptions.emplace_back(
"SpectraNumber");
42 unitOptions.emplace_back(
"Text");
45 "Name to be given to the created workspace.");
47 auto required = std::make_shared<MandatoryValidator<std::vector<double>>>();
48 declareProperty(std::make_unique<
ArrayProperty<double>>(
"DataX", required),
"X-axis data values for workspace.");
50 "Y-axis data values for workspace (measures).");
52 declareProperty(std::make_unique<
PropertyWithValue<int>>(
"NSpec", 1),
"Number of spectra to divide data into.");
53 declareProperty(
"UnitX",
"",
"The unit to assign to the XAxis");
55 declareProperty(
"VerticalAxisUnit",
"SpectraNumber", std::make_shared<StringListValidator>(unitOptions),
56 "The unit to assign to the second Axis (leave blank for "
57 "default Spectra number)");
61 "Whether OutputWorkspace should be marked as a distribution.");
62 declareProperty(
"YUnitLabel",
"",
"Label for Y Axis");
64 declareProperty(
"WorkspaceTitle",
"",
"Title for Workspace");
68 "Name of a parent workspace.");
69 declareProperty(std::make_unique<
ArrayProperty<double>>(
"Dx"),
"X error values for workspace (optional).");
70 declareProperty(
"ParallelStorageMode",
"",
71 "The parallel storage mode of the output workspace for MPI builds. This paramter has been deprecated "
72 "and will be ignored because MPI support has been removed from Mantid. ");
98 throw std::invalid_argument(
"DataX cannot be casted to a double vector");
99 const std::vector<double> &dataX = *xCheck;
103 throw std::invalid_argument(
"DataY cannot be casted to a double vector");
104 const std::vector<double> &dataY = *yCheck;
108 throw std::invalid_argument(
"DataE cannot be casted to a double vector");
109 const std::vector<double> &dataE = *eCheck;
113 throw std::invalid_argument(
"Dx cannot be casted to a double vector");
114 const std::vector<double> &dX = *dxCheck;
118 const std::string vUnit =
getProperty(
"VerticalAxisUnit");
119 const std::vector<std::string> vAxis =
getProperty(
"VerticalAxisValues");
122 const auto vAxisSize =
static_cast<int>(vAxis.size());
123 if (vUnit !=
"SpectraNumber") {
126 if ((vUnit ==
"Text" && vAxisSize != nSpec) || (vAxisSize != nSpec && vAxisSize != nSpec + 1)) {
127 throw std::invalid_argument(
"The number of vertical axis values doesn't "
128 "match the number of histograms.");
133 if ((dataY.size() % nSpec) != 0) {
134 throw std::invalid_argument(
"Length of DataY must be divisible by NSpec");
136 const std::size_t ySize = dataY.size() / nSpec;
140 const bool commonX(dataX.size() == ySize || dataX.size() == ySize + 1);
142 std::size_t xSize{dataX.size()};
143 HistogramBuilder histogramBuilder;
145 histogramBuilder.setX(dataX);
147 if (xSize % nSpec != 0) {
148 throw std::invalid_argument(
"Length of DataX must be divisible by NSpec");
151 histogramBuilder.setX(xSize);
153 histogramBuilder.setY(ySize);
156 if (dX.size() != dataY.size())
157 throw std::runtime_error(
"Dx must have the same size as DataY");
158 histogramBuilder.setDx(ySize);
161 histogramBuilder.setDistribution(
getProperty(
"Distribution"));
162 auto histogram = histogramBuilder.build();
164 const bool dataE_provided = !dataE.empty();
165 if (dataE_provided && dataY.size() != dataE.size()) {
166 throw std::runtime_error(
"DataE (if provided) must be the same size as DataY");
173 outputWS = create<HistoWorkspace>(*parentWS, nSpec, histogram);
176 <<
"MPI support has been removed from Mantid. ParallelStorageMode has been deprecated and will be ignored.";
177 IndexInfo indexInfo(nSpec);
178 outputWS = create<Workspace2D>(indexInfo, histogram);
182 const auto &indexInfo = outputWS->indexInfo();
185 for (
int i = 0; i < nSpec; i++) {
188 const auto localIndices = indexInfo.makeIndexSet({
static_cast<GlobalSpectrumIndex
>(i)});
189 if (localIndices.empty())
192 const std::vector<double>::difference_type xStart = i * xSize;
193 const std::vector<double>::difference_type xEnd = xStart + xSize;
194 const std::vector<double>::difference_type yStart = i * ySize;
195 const std::vector<double>::difference_type yEnd = yStart + ySize;
196 auto local_i = localIndices[0];
201 outputWS->mutableX(local_i).assign(dataX.begin() + xStart, dataX.begin() + xEnd);
203 outputWS->mutableY(local_i).assign(dataY.begin() + yStart, dataY.begin() + yEnd);
206 outputWS->mutableE(local_i).assign(dataE.begin() + yStart, dataE.begin() + yEnd);
209 outputWS->mutableDx(local_i).assign(dX.begin() + yStart, dX.begin() + yEnd);
218 outputWS->getAxis(0)->unit() = UnitFactory::Instance().create(xUnit);
220 outputWS->getAxis(0)->unit() = UnitFactory::Instance().create(
"Label");
221 Unit_sptr unit = outputWS->getAxis(0)->unit();
222 std::shared_ptr<Units::Label> label = std::dynamic_pointer_cast<Units::Label>(unit);
223 label->setLabel(xUnit, xUnit);
228 if (vUnit !=
"SpectraNumber") {
229 if (vUnit ==
"Text") {
230 auto newAxis = std::make_unique<TextAxis>(vAxis.size());
231 auto newAxisRaw = newAxis.get();
232 outputWS->replaceAxis(1, std::move(newAxis));
233 for (
size_t i = 0; i < vAxis.size(); i++) {
234 newAxisRaw->setLabel(i, vAxis[i]);
237 std::unique_ptr<NumericAxis> newAxis(
nullptr);
238 if (vAxisSize == nSpec)
239 newAxis = std::make_unique<NumericAxis>(vAxisSize);
240 else if (vAxisSize == nSpec + 1)
241 newAxis = std::make_unique<BinEdgeAxis>(vAxisSize);
243 throw std::range_error(
"Invalid vertical axis length. It must be the "
244 "same length as NSpec or 1 longer.");
245 auto newAxisRaw = newAxis.get();
246 newAxis->unit() = UnitFactory::Instance().create(vUnit);
247 outputWS->replaceAxis(1, std::move(newAxis));
248 for (
size_t i = 0; i < vAxis.size(); i++) {
250 newAxisRaw->setValue(i, boost::lexical_cast<double, std::string>(vAxis[i]));
251 }
catch (boost::bad_lexical_cast &) {
252 throw std::invalid_argument(
"CreateWorkspace - YAxisValues property "
253 "could not be converted to a double.");
261 outputWS->setYUnitLabel(
getProperty(
"YUnitLabel"));