Mantid
Loading...
Searching...
No Matches
MDNorm.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 +
11#include "MantidAPI/Run.h"
12#include "MantidAPI/Sample.h"
32
33#include <algorithm>
34#include <boost/lexical_cast.hpp>
35
36namespace Mantid::MDAlgorithms {
37
38using namespace Mantid::Kernel;
39using namespace Mantid::API;
40using namespace Mantid::Geometry;
41using namespace Mantid::DataObjects;
42
43namespace {
45// function to compare two intersections (h,k,l,Momentum) by Momentum
46bool compareMomentum(const std::array<double, 4> &v1, const std::array<double, 4> &v2) { return (v1[3] < v2[3]); }
47
48// k=sqrt(energyToK * E)
49constexpr double energyToK = 8.0 * M_PI * M_PI * PhysicalConstants::NeutronMass * PhysicalConstants::meV * 1e-20 /
51
52// compare absolute values of doubles
53static bool abs_compare(double a, double b) { return (std::fabs(a) < std::fabs(b)); }
54} // namespace
55
56// Register the algorithm into the AlgorithmFactory
58
59//----------------------------------------------------------------------------------------------
64 : m_normWS(), m_inputWS(), m_isRLU(false), m_UB(3, 3, true), m_W(3, 3, true), m_transformation(), m_hX(), m_kX(),
65 m_lX(), m_eX(), m_hIdx(-1), m_kIdx(-1), m_lIdx(-1), m_eIdx(-1), m_numExptInfos(0), m_Ei(0.0), m_diffraction(true),
66 m_accumulate(false), m_dEIntegrated(true), m_samplePos(), m_beamDir(), convention("") {}
67
69const std::string MDNorm::name() const { return "MDNorm"; }
70
72int MDNorm::version() const { return 1; }
73
75const std::string MDNorm::category() const { return "MDAlgorithms\\Normalisation"; }
76
78const std::string MDNorm::summary() const {
79 return "Bins multidimensional data and calculate the normalization on the "
80 "same grid";
81}
82
83//----------------------------------------------------------------------------------------------
88 std::make_unique<WorkspaceProperty<API::IMDEventWorkspace>>("InputWorkspace", "", Kernel::Direction::Input),
89 "An input MDEventWorkspace. Must be in Q_sample frame.");
90
92 "BackgroundWorkspace", "", Kernel::Direction::Input, PropertyMode::Optional),
93 "An (optional) input MDEventWorkspace for background. Must be in Q_lab frame.");
94
95 // RLU and settings
96 declareProperty("RLU", true, "Use reciprocal lattice units. If false, use Q_sample");
97 setPropertyGroup("RLU", "Q projections RLU");
98
99 auto mustBe3D = std::make_shared<Kernel::ArrayLengthValidator<double>>(3);
100 std::vector<double> Q0(3, 0.), Q1(3, 0), Q2(3, 0);
101 Q0[0] = 1.;
102 Q1[1] = 1.;
103 Q2[2] = 1.;
104
105 declareProperty(std::make_unique<ArrayProperty<double>>("QDimension0", Q0, mustBe3D),
106 "The first Q projection axis - Default is (1,0,0)");
107 setPropertySettings("QDimension0", std::make_unique<Kernel::VisibleWhenProperty>("RLU", IS_EQUAL_TO, "1"));
108 setPropertyGroup("QDimension0", "Q projections RLU");
109
110 declareProperty(std::make_unique<ArrayProperty<double>>("QDimension1", Q1, mustBe3D),
111 "The second Q projection axis - Default is (0,1,0)");
112 setPropertySettings("QDimension1", std::make_unique<Kernel::VisibleWhenProperty>("RLU", IS_EQUAL_TO, "1"));
113 setPropertyGroup("QDimension1", "Q projections RLU");
114
115 declareProperty(std::make_unique<ArrayProperty<double>>("QDimension2", Q2, mustBe3D),
116 "The thirdtCalculateCover Q projection axis - Default is (0,0,1)");
117 setPropertySettings("QDimension2", std::make_unique<Kernel::VisibleWhenProperty>("RLU", IS_EQUAL_TO, "1"));
118 setPropertyGroup("QDimension2", "Q projections RLU");
119
120 // vanadium
121 auto fluxValidator = std::make_shared<CompositeValidator>();
122 fluxValidator->add<InstrumentValidator>();
123 fluxValidator->add<CommonBinsValidator>();
124 auto solidAngleValidator = fluxValidator->clone();
125 declareProperty(std::make_unique<WorkspaceProperty<>>("SolidAngleWorkspace", "", Direction::Input,
126 API::PropertyMode::Optional, solidAngleValidator),
127 "An input workspace containing integrated vanadium "
128 "(a measure of the solid angle).\n"
129 "Mandatory for diffraction, optional for direct geometry inelastic");
130 declareProperty(std::make_unique<WorkspaceProperty<>>("FluxWorkspace", "", Direction::Input,
131 API::PropertyMode::Optional, fluxValidator),
132 "An input workspace containing momentum dependent flux.\n"
133 "Mandatory for diffraction. No effect on direct geometry inelastic");
134 setPropertyGroup("SolidAngleWorkspace", "Vanadium normalization");
135 setPropertyGroup("FluxWorkspace", "Vanadium normalization");
136
137 // Define slicing
138 for (std::size_t i = 0; i < 6; i++) {
139 std::string propName = "Dimension" + Strings::toString(i) + "Name";
140 std::string propBinning = "Dimension" + Strings::toString(i) + "Binning";
141 std::string defaultName = "";
142 if (i < 3) {
143 defaultName = "QDimension" + Strings::toString(i);
144 }
145 declareProperty(std::make_unique<PropertyWithValue<std::string>>(propName, defaultName, Direction::Input),
146 "Name for the " + Strings::toString(i) + "th dimension. Leave blank for NONE.");
147 auto atMost3 = std::make_shared<ArrayLengthValidator<double>>(0, 3);
148 std::vector<double> temp;
149 declareProperty(std::make_unique<ArrayProperty<double>>(propBinning, temp, atMost3),
150 "Binning for the " + Strings::toString(i) + "th dimension.\n" +
151 "- Leave blank for complete integration\n" +
152 "- One value is interpreted as step\n"
153 "- Two values are interpreted integration interval\n" +
154 "- Three values are interpreted as min, step, max");
155 setPropertyGroup(propName, "Binning");
156 setPropertyGroup(propBinning, "Binning");
157 }
158
159 // symmetry operations
160 declareProperty(std::make_unique<PropertyWithValue<std::string>>("SymmetryOperations", "", Direction::Input),
161 "If specified the symmetry will be applied, "
162 "can be space group name, point group name, or list "
163 "individual symmetries.");
164
165 // temporary workspaces
166 declareProperty(std::make_unique<WorkspaceProperty<IMDHistoWorkspace>>("TemporaryDataWorkspace", "", Direction::Input,
168 "An (optional) input MDHistoWorkspace used to accumulate data from "
169 "multiple MDEventWorkspaces. If unspecified a blank "
170 "MDHistoWorkspace will be created.");
171 declareProperty(std::make_unique<WorkspaceProperty<IMDHistoWorkspace>>("TemporaryNormalizationWorkspace", "",
173 "An (optional) input MDHistoWorkspace used to accumulate normalization "
174 "from multiple MDEventWorkspaces. If unspecified a blank "
175 "MDHistoWorkspace will be created.");
176
177 // temporary background workspace
178 declareProperty(std::make_unique<WorkspaceProperty<IMDHistoWorkspace>>("TemporaryBackgroundDataWorkspace", "",
180 "An (optional) input MDHistoWorkspace used to accumulate background from "
181 "multiple background MDEventWorkspaces. If unspecified but "
182 "BackgroundWorkspace is specified, a blank "
183 "MDHistoWorkspace will be created.");
184 declareProperty(std::make_unique<WorkspaceProperty<IMDHistoWorkspace>>("TemporaryBackgroundNormalizationWorkspace",
186 "An (optional) input MDHistoWorkspace used to accumulate background normalization "
187 "from multiple background MDEventWorkspaces. If unspecified but "
188 "BackgroundWorkspace is specified, a blank "
189 "MDHistoWorkspace will be created.");
190
191 setPropertyGroup("TemporaryDataWorkspace", "Temporary workspaces");
192 setPropertyGroup("TemporaryNormalizationWorkspace", "Temporary workspaces");
193 setPropertyGroup("TemporaryBackgroundDataWorkspace", "Temporary workspaces");
194 setPropertyGroup("TemporaryBackgroundNormalizationWorkspace", "Temporary workspaces");
195
196 declareProperty(std::make_unique<WorkspaceProperty<API::Workspace>>("OutputWorkspace", "", Kernel::Direction::Output),
197 "A name for the normalized output MDHistoWorkspace.");
199 std::make_unique<WorkspaceProperty<API::Workspace>>("OutputDataWorkspace", "", Kernel::Direction::Output),
200 "A name for the output data MDHistoWorkspace.");
201 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("OutputNormalizationWorkspace", "", Direction::Output),
202 "A name for the output normalization MDHistoWorkspace.");
204 "OutputBackgroundDataWorkspace", "", Kernel::Direction::Output, PropertyMode::Optional),
205 "A name for the optional output background data MDHistoWorkspace.");
206 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("OutputBackgroundNormalizationWorkspace", "",
208 "A name for the optional output background normalization MDHistoWorkspace.");
209}
210
211//----------------------------------------------------------------------------------------------
213std::map<std::string, std::string> MDNorm::validateInputs() {
214 std::map<std::string, std::string> errorMessage;
215
216 // Check for input workspace frame
217 Mantid::API::IMDEventWorkspace_sptr inputWS = this->getProperty("InputWorkspace");
218 if (inputWS->getNumDims() < 3) {
219 errorMessage.emplace("InputWorkspace", "The input workspace must be at least 3D");
220 } else {
221 for (size_t i = 0; i < 3; i++) {
222 if (inputWS->getDimension(i)->getMDFrame().name() != Mantid::Geometry::QSample::QSampleName) {
223 errorMessage.emplace("InputWorkspace", "The input workspace must be in Q_sample");
224 }
225 }
226 }
227
228 // Optional background input IMDE
229 Mantid::API::IMDEventWorkspace_sptr bkgdWS = this->getProperty("BackgroundWorkspace");
230 if (bkgdWS) {
231 if (bkgdWS->getNumDims() < 3) {
232 // must have at least 3 dimensions
233 errorMessage.emplace("BackgroundWorkspace", "The input background workspace must be at least 3D");
234 } else {
235 // Check first 3 dimension for Q lab,
236 for (size_t i = 0; i < 3; i++) {
237 if (bkgdWS->getDimension(i)->getMDFrame().name() != Mantid::Geometry::QLab::QLabName) {
238 errorMessage.emplace("BackgroundWorkspace", "The input backgound workspace must be in Q_lab");
239 }
240 }
241
242 // Check 4th dimension if input workspace is elastic
243 if (inputWS->getNumDims() > 3) {
244 if (bkgdWS->getNumDims() <= 3) {
245 errorMessage.emplace("BackgroundWorkspace", "The input background workspace must have at 4 dimensions when "
246 "input workspace has more than 4 dimensions (inelastic case).");
247 } else if (bkgdWS->getDimension(3)->getName() != inputWS->getDimension(3)->getName()) {
248 errorMessage.emplace("BackgroundWorkspace", "The input background workspace 4th dimension must be DeltaE "
249 "for inelastic case.");
250 }
251 }
252 }
253 }
254
255 // Check if the vanadium is available for diffraction
256 bool diffraction = true;
257 if ((inputWS->getNumDims() > 3) && (inputWS->getDimension(3)->getName() == "DeltaE")) {
258 diffraction = false;
259 }
260 if (diffraction) {
261 API::MatrixWorkspace_const_sptr solidAngleWS = getProperty("SolidAngleWorkspace");
262 API::MatrixWorkspace_const_sptr fluxWS = getProperty("FluxWorkspace");
263 if (solidAngleWS == nullptr) {
264 errorMessage.emplace("SolidAngleWorkspace", "SolidAngleWorkspace is required for diffraction");
265 }
266 if (fluxWS == nullptr) {
267 errorMessage.emplace("FluxWorkspace", "FluxWorkspace is required for diffraction");
268 }
269 }
270 // Check for property MDNorm_low and MDNorm_high
271 size_t nExperimentInfos = inputWS->getNumExperimentInfo();
272 if (nExperimentInfos == 0) {
273 errorMessage.emplace("InputWorkspace", "There must be at least one experiment info");
274 } else {
275 for (size_t iExpInfo = 0; iExpInfo < nExperimentInfos; iExpInfo++) {
276 auto &currentExptInfo = *(inputWS->getExperimentInfo(static_cast<uint16_t>(iExpInfo)));
277 if (!currentExptInfo.run().hasProperty("MDNorm_low")) {
278 errorMessage.emplace("InputWorkspace", "Missing MDNorm_low log. Please "
279 "use CropWorkspaceForMDNorm "
280 "before converting to MD");
281 }
282 if (!currentExptInfo.run().hasProperty("MDNorm_high")) {
283 errorMessage.emplace("InputWorkspace", "Missing MDNorm_high log. Please use "
284 "CropWorkspaceForMDNorm before converting to MD");
285 }
286 }
287 }
288 // check projections and UB
289 if (getProperty("RLU")) {
290 DblMatrix W = DblMatrix(3, 3);
291 std::vector<double> Q0Basis = getProperty("QDimension0");
292 std::vector<double> Q1Basis = getProperty("QDimension1");
293 std::vector<double> Q2Basis = getProperty("QDimension2");
294 W.setColumn(0, Q0Basis);
295 W.setColumn(1, Q1Basis);
296 W.setColumn(2, Q2Basis);
297 if (fabs(W.determinant()) < 1e-5) {
298 errorMessage.emplace("QDimension0", "The projection dimensions are coplanar or zero");
299 errorMessage.emplace("QDimension1", "The projection dimensions are coplanar or zero");
300 errorMessage.emplace("QDimension2", "The projection dimensions are coplanar or zero");
301 }
302 if (!inputWS->getExperimentInfo(0)->sample().hasOrientedLattice()) {
303 errorMessage.emplace("InputWorkspace", "There is no oriented lattice "
304 "associated with the input workspace. "
305 "Use SetUB algorithm");
306 }
307 }
308 // check dimension names
309 std::vector<std::string> originalDimensionNames;
310 for (size_t i = 3; i < inputWS->getNumDims(); i++) {
311 originalDimensionNames.emplace_back(inputWS->getDimension(i)->getName());
312 }
313 originalDimensionNames.emplace_back("QDimension0");
314 originalDimensionNames.emplace_back("QDimension1");
315 originalDimensionNames.emplace_back("QDimension2");
316 std::vector<std::string> selectedDimensions;
317 for (std::size_t i = 0; i < 6; i++) {
318 std::string propName = "Dimension" + Strings::toString(i) + "Name";
319 std::string dimName = getProperty(propName);
320 std::string binningName = "Dimension" + Strings::toString(i) + "Binning";
321 std::vector<double> binning = getProperty(binningName);
322 if (!dimName.empty()) {
323 auto it = std::find(originalDimensionNames.begin(), originalDimensionNames.end(), dimName);
324 if (it == originalDimensionNames.end()) {
325 errorMessage.emplace(propName, "Name '" + dimName +
326 "' is not one of the "
327 "original workspace names or a directional dimension");
328 } else {
329 // make sure dimension is unique
330 auto itSel = std::find(selectedDimensions.begin(), selectedDimensions.end(), dimName);
331 if (itSel == selectedDimensions.end()) {
332 selectedDimensions.emplace_back(dimName);
333 } else {
334 errorMessage.emplace(propName, "Name '" + dimName + "' was already selected");
335 }
336 }
337 } else {
338 if (!binning.empty()) {
339 errorMessage.emplace(binningName, "There should be no binning if the dimension name is empty");
340 }
341 }
342 }
343 // since Q dimensions can be non - orthogonal, all must be present
344 if ((std::find(selectedDimensions.begin(), selectedDimensions.end(), "QDimension0") == selectedDimensions.end()) ||
345 (std::find(selectedDimensions.begin(), selectedDimensions.end(), "QDimension1") == selectedDimensions.end()) ||
346 (std::find(selectedDimensions.begin(), selectedDimensions.end(), "QDimension2") == selectedDimensions.end())) {
347 for (std::size_t i = 0; i < 6; i++) {
348 std::string propName = "Dimension" + Strings::toString(i) + "Name";
349 errorMessage.emplace(propName, "All of QDimension0, QDimension1, QDimension2 must be present");
350 }
351 }
352 // symmetry operations
353 std::string symOps = this->getProperty("SymmetryOperations");
354 if (!symOps.empty()) {
355 bool isSpaceGroup = Geometry::SpaceGroupFactory::Instance().isSubscribed(symOps);
356 bool isPointGroup = Geometry::PointGroupFactory::Instance().isSubscribed(symOps);
357 if (!isSpaceGroup && !isPointGroup) {
358 try {
360 } catch (const Mantid::Kernel::Exception::ParseError &) {
361 errorMessage.emplace("SymmetryOperations", "The input is not a space group, a point group, "
362 "or a list of symmetry operations");
363 }
364 }
365 }
366 // validate accumulation workspaces, if provided
367 std::shared_ptr<IMDHistoWorkspace> tempNormWS = this->getProperty("TemporaryNormalizationWorkspace");
368 Mantid::API::IMDHistoWorkspace_sptr tempDataWS = this->getProperty("TemporaryDataWorkspace");
369
370 // check that either both or neuther accumulation workspaces are provied
371 if ((tempNormWS && !tempDataWS) || (!tempNormWS && tempDataWS)) {
372 errorMessage.emplace("TemporaryDataWorkspace", "Must provide either no accumulation workspaces or,"
373 "both TemporaryNormalizationWorkspaces and TemporaryDataWorkspace");
374 }
375 // check that both accumulation workspaces are on the same grid
376 if (tempNormWS && tempDataWS) {
377 size_t numNormDims = tempNormWS->getNumDims();
378 size_t numDataDims = tempDataWS->getNumDims();
379 if (numNormDims == numDataDims) {
380 for (size_t i = 0; i < numNormDims; i++) {
381 const auto dim1 = tempNormWS->getDimension(i);
382 const auto dim2 = tempDataWS->getDimension(i);
383 if ((dim1->getMinimum() != dim2->getMinimum()) || (dim1->getMaximum() != dim2->getMaximum()) ||
384 (dim1->getNBins() != dim2->getNBins()) || (dim1->getName() != dim2->getName())) {
385 errorMessage.emplace("TemporaryDataWorkspace", "Binning for TemporaryNormalizationWorkspaces "
386 "and TemporaryDataWorkspace must be the same.");
387 break;
388 }
389 }
390 } else { // accumulation workspaces have different number of dimensions
391 errorMessage.emplace("TemporaryDataWorkspace", "TemporaryNormalizationWorkspace and TemporaryDataWorkspace "
392 "do not have the same number of dimensions");
393 }
394 }
395
396 // validate accumulated background workspaces
397 Mantid::API::IMDHistoWorkspace_sptr tempBkgdDataWS = this->getProperty("TemporaryBackgroundDataWorkspace");
398 Mantid::API::IMDHistoWorkspace_sptr tempBkgdNormWS = this->getProperty("TemporaryBackgroundNormalizationWorkspace");
399 // check existing criteria: Background, TempBackgroundData and
400 // TempBackgroundNormalization must be specified
401 if (tempBkgdDataWS && (!bkgdWS || !tempDataWS || !tempBkgdNormWS)) {
402 errorMessage.emplace("TemporaryBackgroundDataWorkspace", "TemporaryBackgroundDataWorkspace is specified but at "
403 "least one of these is not.");
404 } else if (tempBkgdNormWS && (!bkgdWS || !tempNormWS || !tempBkgdDataWS)) {
405 errorMessage.emplace("TemporaryBackgroundNormalizationWorkspace", "TemporaryBackgroundNormalizationWorkspace is "
406 "specified but at least one of these is not.");
407 } else if (bkgdWS && tempDataWS && !tempBkgdDataWS) {
408 errorMessage.emplace("TemporaryDataWorkspace",
409 "With Background is specifed and TemporaryDataWorkspace is specifed, "
410 "TemporaryBackgroundDataWorkspace must be specified.");
411 } else if (tempBkgdDataWS && tempNormWS) {
412 // check when they both exist
413 size_t numBkgdDataDims = tempBkgdDataWS->getNumDims();
414 size_t numBkgdNormDims = tempBkgdNormWS->getNumDims();
415 size_t numDataDims = tempDataWS->getNumDims();
416 if (numBkgdDataDims == numBkgdNormDims && numBkgdDataDims == numDataDims) {
417 // On each dimension, compare min, max, NBins and name
418 for (size_t idim = 0; idim < numBkgdDataDims; ++idim) {
419 const auto dimB = tempBkgdDataWS->getDimension(idim);
420 const auto dimN = tempBkgdNormWS->getDimension(idim);
421 const auto dimD = tempDataWS->getDimension(idim);
422 if ((dimB->getMinimum() != dimN->getMinimum()) || (dimB->getMinimum() != dimD->getMinimum()) ||
423 (dimB->getMaximum() != dimN->getMaximum()) || (dimB->getMaximum() != dimD->getMaximum()) ||
424 (dimB->getNBins() != dimN->getNBins()) || (dimB->getNBins() != dimD->getNBins()) ||
425 (dimB->getName() != dimN->getName()) || (dimB->getName() != dimD->getName())) {
426 errorMessage.emplace("TemporaryBackgroundDataWorkspace",
427 "TemporaryBackgroundDataWorkspace, "
428 "TemporaryBackgroundNormalizationWorkspace and "
429 "TemporaryDataWorkspace "
430 "must have same minimum, maximum, number of bins and name.");
431 break;
432 }
433 }
434 } else {
435 errorMessage.emplace("TemporaryBackgroundDataWorkspace", "TemporaryBackgroundDataWorkspace, "
436 "TemporaryBackgroundNormalizationWorkspace and "
437 "TemporaryDataWorkspace must have same dimensions");
438 }
439 }
440
441 return errorMessage;
442}
443
444//----------------------------------------------------------------------------------------------
448 convention = Kernel::ConfigService::Instance().getString("Q.convention");
449 // symmetry operations
450 std::string symOps = this->getProperty("SymmetryOperations");
451 std::vector<Geometry::SymmetryOperation> symmetryOps;
452 if (symOps.empty()) {
453 symOps = "x,y,z";
454 }
455 if (Geometry::SpaceGroupFactory::Instance().isSubscribed(symOps)) {
456 auto spaceGroup = Geometry::SpaceGroupFactory::Instance().createSpaceGroup(symOps);
457 auto pointGroup = spaceGroup->getPointGroup();
458 symmetryOps = pointGroup->getSymmetryOperations();
459 } else if (Geometry::PointGroupFactory::Instance().isSubscribed(symOps)) {
460 auto pointGroup = Geometry::PointGroupFactory::Instance().createPointGroup(symOps);
461 symmetryOps = pointGroup->getSymmetryOperations();
462 } else {
463 symmetryOps = Geometry::SymmetryOperationFactory::Instance().createSymOps(symOps);
464 }
465 g_log.debug() << "Symmetry operations\n";
466 for (auto so : symmetryOps) {
467 g_log.debug() << so.identifier() << "\n";
468 }
469 m_numSymmOps = symmetryOps.size();
470
471 m_isRLU = getProperty("RLU");
472 // get the workspaces
473 m_inputWS = this->getProperty("InputWorkspace");
474 const auto &exptInfoZero = *(m_inputWS->getExperimentInfo(0));
475 auto source = exptInfoZero.getInstrument()->getSource();
476 auto sample = exptInfoZero.getInstrument()->getSample();
477 if (source == nullptr || sample == nullptr) {
479 "Instrument not sufficiently defined: failed to get source and/or "
480 "sample");
481 }
482 m_samplePos = sample->getPos();
483 m_beamDir = normalize(m_samplePos - source->getPos());
484 if ((m_inputWS->getNumDims() > 3) && (m_inputWS->getDimension(3)->getName() == "DeltaE")) {
485 // DeltaE in input MDE: it cannot be diffraction!
486 m_diffraction = false;
487 if (exptInfoZero.run().hasProperty("Ei")) {
488 Kernel::Property *eiprop = exptInfoZero.run().getProperty("Ei");
489 m_Ei = boost::lexical_cast<double>(eiprop->value());
490 if (m_Ei <= 0) {
491 throw std::invalid_argument("Ei stored in the workspace is not positive");
492 }
493 } else {
494 throw std::invalid_argument("Could not find Ei value in the workspace.");
495 }
496 }
497
498 // Calculate (BinMD) input sample MDE to MDH and create noramlization MDH from
499 // it
500 auto outputDataWS = binInputWS(symmetryOps);
501 createNormalizationWS(*outputDataWS);
502 this->setProperty("OutputNormalizationWorkspace", m_normWS);
503 this->setProperty("OutputDataWorkspace", outputDataWS);
504
505 // Background
506 m_backgroundWS = this->getProperty("BackgroundWorkspace");
507 DataObjects::MDHistoWorkspace_sptr outputBackgroundDataWS(nullptr);
508 // Outputs for background related
509 if (m_backgroundWS) {
510 outputBackgroundDataWS = binBackgroundWS(symmetryOps);
511 createBackgroundNormalizationWS(*outputBackgroundDataWS);
512 this->setProperty("OutputBackgroundNormalizationWorkspace", m_bkgdNormWS);
513 this->setProperty("OutputBackgroundDataWorkspace", outputBackgroundDataWS);
514 }
515
516 m_numExptInfos = outputDataWS->getNumExperimentInfo();
517 // loop over all experiment infos
518 for (uint16_t expInfoIndex = 0; expInfoIndex < m_numExptInfos; expInfoIndex++) {
519 // Check for other dimensions if we could measure anything in the original
520 // data
521 bool skipNormalization = false;
522 const std::vector<coord_t> otherValues = getValuesFromOtherDimensions(skipNormalization, expInfoIndex);
523
525
526 if (!skipNormalization) {
527 size_t symmOpsIndex = 0;
528 for (const auto &so : symmetryOps) {
529 calculateNormalization(otherValues, so, expInfoIndex, symmOpsIndex);
530 symmOpsIndex++;
531 }
532
533 } else {
534 g_log.warning("Binning limits are outside the limits of the MDWorkspace. "
535 "Not applying normalization.");
536 }
537 // if more than one experiment info, keep accumulating
538 m_accumulate = true;
539 }
540
541 API::IMDWorkspace_sptr out(nullptr);
542
543 if (m_backgroundWS) {
544 // Normalize binned (BinMD) sample workspace with background
545 out = divideMD(outputDataWS, m_normWS, getPropertyValue("OutputWorkspace"), 0.97, 0.98);
546
547 // Normalize background
548 const std::string normedBkgdWSName("_normedBkgd");
549 API::IMDWorkspace_sptr outbkgd = divideMD(outputBackgroundDataWS, m_bkgdNormWS, normedBkgdWSName, 0.98, 0.99);
550
551 // Clean workspace
552 auto minusMD = createChildAlgorithm("MinusMD", 0.99, 1.00);
553 // set up
554 minusMD->setProperty("LHSWorkspace", out);
555 minusMD->setProperty("RHSWorkspace", outbkgd);
556 minusMD->setPropertyValue("OutputWorkspace", getPropertyValue("OutputWorkspace"));
557 // run and return
558 minusMD->executeAsChildAlg();
559 out = minusMD->getProperty("OutputWorkspace");
560
561 } else {
562 // Normalize binned (BinMD) sample workspace without background
563 out = divideMD(outputDataWS, m_normWS, getPropertyValue("OutputWorkspace"), 0.97, 1.);
564 }
565
566 // Set output workspace
567 this->setProperty("OutputWorkspace", out);
568}
569
571 const API::IMDHistoWorkspace_sptr &rhs, const std::string &outputwsname,
572 const double &startProgress, const double &endProgress) {
573 auto divideMD = createChildAlgorithm("DivideMD", startProgress, endProgress);
574 divideMD->setProperty("LHSWorkspace", lhs);
575 divideMD->setProperty("RHSWorkspace", rhs);
576 divideMD->setPropertyValue("OutputWorkspace", outputwsname);
577 divideMD->executeAsChildAlg();
578 // API::IMDWorkspace_sptr
579 API::IMDWorkspace_sptr out = divideMD->getProperty("OutputWorkspace");
580
581 return out;
582}
583
590 if (i == 0)
591 return std::string("Q_sample_x");
592 else if (i == 1)
593 return std::string("Q_sample_y");
594 else if (i == 2)
595 return std::string("Q_sample_z");
596 else
597 throw std::invalid_argument("Index must be 0, 1, or 2 for QDimensionNameQSample");
598}
605std::string MDNorm::QDimensionName(std::vector<double> projection) {
606 std::vector<double>::iterator result;
607 result = std::max_element(projection.begin(), projection.end(), abs_compare);
608 std::vector<char> symbol{'H', 'K', 'L'};
609 char character = symbol[std::distance(projection.begin(), result)];
610 std::stringstream name;
611 name << "[";
612 for (size_t i = 0; i < 3; i++) {
613 if (projection[i] == 0) {
614 name << "0";
615 } else if (projection[i] == 1) {
616 name << character;
617 } else if (projection[i] == -1) {
618 name << "-" << character;
619 } else {
620 name << std::defaultfloat << std::setprecision(3) << projection[i] << character;
621 }
622 if (i != 2) {
623 name << ",";
624 }
625 }
626 name << "]";
627 return name.str();
628}
629
634std::map<std::string, std::string> MDNorm::getBinParameters() {
635 std::map<std::string, std::string> parameters;
636 std::stringstream extents;
637 std::stringstream bins;
638 std::vector<std::string> originalDimensionNames;
639 originalDimensionNames.emplace_back("QDimension0");
640 originalDimensionNames.emplace_back("QDimension1");
641 originalDimensionNames.emplace_back("QDimension2");
642 for (size_t i = 3; i < m_inputWS->getNumDims(); i++) {
643 originalDimensionNames.emplace_back(m_inputWS->getDimension(i)->getName());
644 }
645
646 if (m_isRLU) {
647 m_Q0Basis = getProperty("QDimension0");
648 m_Q1Basis = getProperty("QDimension1");
649 m_Q2Basis = getProperty("QDimension2");
650 m_UB = m_inputWS->getExperimentInfo(0)->sample().getOrientedLattice().getUB() * 2 * M_PI;
651 }
652
653 std::vector<double> W(m_Q0Basis);
654 W.insert(W.end(), m_Q1Basis.begin(), m_Q1Basis.end());
655 W.insert(W.end(), m_Q2Basis.begin(), m_Q2Basis.end());
656 m_W = DblMatrix(W);
657 m_W.Transpose();
658
659 // Find maximum Q
660 auto &exptInfo0 = *(m_inputWS->getExperimentInfo(static_cast<uint16_t>(0)));
661 auto upperLimitsVector =
662 (*(dynamic_cast<Kernel::PropertyWithValue<std::vector<double>> *>(exptInfo0.getLog("MDNorm_high"))))();
663 double maxQ;
664 if (m_diffraction) {
665 maxQ = 2. * (*std::max_element(upperLimitsVector.begin(), upperLimitsVector.end()));
666 } else {
667 double Ei;
668 double maxDE = *std::max_element(upperLimitsVector.begin(), upperLimitsVector.end());
669 auto loweLimitsVector =
670 (*(dynamic_cast<Kernel::PropertyWithValue<std::vector<double>> *>(exptInfo0.getLog("MDNorm_low"))))();
671 double minDE = *std::min_element(loweLimitsVector.begin(), loweLimitsVector.end());
672 if (exptInfo0.run().hasProperty("Ei")) {
673 Kernel::Property *eiprop = exptInfo0.run().getProperty("Ei");
674 Ei = boost::lexical_cast<double>(eiprop->value());
675 if (Ei <= 0) {
676 throw std::invalid_argument("Ei stored in the workspace is not positive");
677 }
678 } else {
679 throw std::invalid_argument("Could not find Ei value in the workspace.");
680 }
681 const double energyToK = 8.0 * M_PI * M_PI * PhysicalConstants::NeutronMass * PhysicalConstants::meV * 1e-20 /
683 double ki = std::sqrt(energyToK * Ei);
684 double kfmin = std::sqrt(energyToK * (Ei - minDE));
685 double kfmax = std::sqrt(energyToK * (Ei - maxDE));
686
687 maxQ = ki + std::max(kfmin, kfmax);
688 }
689 size_t basisVectorIndex = 0;
690 std::vector<coord_t> transformation;
691 for (std::size_t i = 0; i < 6; i++) {
692 std::string propName = "Dimension" + Strings::toString(i) + "Name";
693 std::string binningName = "Dimension" + Strings::toString(i) + "Binning";
694 std::string dimName = getProperty(propName);
695 std::vector<double> binning = getProperty(binningName);
696 std::string bv = "BasisVector";
697 if (!dimName.empty()) {
698 std::string property = bv + Strings::toString(basisVectorIndex);
699 std::stringstream propertyValue;
700 propertyValue << dimName;
701 // get the index in the original workspace
702 auto dimIndex = std::distance(originalDimensionNames.begin(),
703 std::find(originalDimensionNames.begin(), originalDimensionNames.end(), dimName));
704 auto dimension = m_inputWS->getDimension(dimIndex);
705 propertyValue << "," << dimension->getMDUnits().getUnitLabel().ascii();
706 for (size_t j = 0; j < originalDimensionNames.size(); j++) {
707 if (j == static_cast<size_t>(dimIndex)) {
708 propertyValue << ",1";
709 transformation.emplace_back(1.f);
710 } else {
711 propertyValue << ",0";
712 transformation.emplace_back(0.f);
713 }
714 }
715 parameters.emplace(property, propertyValue.str());
716 // get the extents an number of bins
717 coord_t dimMax = dimension->getMaximum();
718 coord_t dimMin = dimension->getMinimum();
719 if (m_isRLU) {
721 ol.setUB(m_UB * m_W); // note that this is already multiplied by 2Pi
722 if (dimIndex == 0) {
723 dimMax = static_cast<coord_t>(ol.a() * maxQ);
724 dimMin = -dimMax;
725 } else if (dimIndex == 1) {
726 dimMax = static_cast<coord_t>(ol.b() * maxQ);
727 dimMin = -dimMax;
728 } else if (dimIndex == 2) {
729 dimMax = static_cast<coord_t>(ol.c() * maxQ);
730 dimMin = -dimMax;
731 }
732 }
733 if (binning.size() == 0) {
734 // only one bin, integrating from min to max
735 extents << dimMin << "," << dimMax << ",";
736 bins << 1 << ",";
737 } else if (binning.size() == 2) {
738 // only one bin, integrating from min to max
739 extents << binning[0] << "," << binning[1] << ",";
740 bins << 1 << ",";
741 } else if (binning.size() == 1) {
742 auto step = binning[0];
743 double nsteps = (dimMax - dimMin) / step;
744 if (nsteps + 1 - std::ceil(nsteps) >= 1e-4) {
745 nsteps = std::ceil(nsteps);
746 } else {
747 nsteps = std::floor(nsteps);
748 }
749 bins << static_cast<int>(nsteps) << ",";
750 extents << dimMin << "," << dimMin + nsteps * step << ",";
751 } else if (binning.size() == 3) {
752 dimMin = static_cast<coord_t>(binning[0]);
753 auto step = binning[1];
754 dimMax = static_cast<coord_t>(binning[2]);
755 double nsteps = (dimMax - dimMin) / step;
756 if (nsteps + 1 - std::ceil(nsteps) >= 1e-4) {
757 nsteps = std::ceil(nsteps);
758 } else {
759 nsteps = std::floor(nsteps);
760 }
761 bins << static_cast<int>(nsteps) << ",";
762 extents << dimMin << "," << dimMin + nsteps * step << ",";
763 }
764 basisVectorIndex++;
765 }
766 }
767 parameters.emplace("OutputExtents", extents.str());
768 parameters.emplace("OutputBins", bins.str());
770 transformation, static_cast<size_t>((transformation.size()) / m_inputWS->getNumDims()), m_inputWS->getNumDims());
771 return parameters;
772}
773
779 // Copy the MDHisto workspace, and change signals and errors to 0.
780 std::shared_ptr<IMDHistoWorkspace> tmp = this->getProperty("TemporaryNormalizationWorkspace");
781 m_normWS = std::dynamic_pointer_cast<MDHistoWorkspace>(tmp);
782 if (!m_normWS) {
783 m_normWS = dataWS.clone();
784 m_normWS->setTo(0., 0., 0.);
785 } else {
786 // Temp is given. Accumulation mode is on
787 m_accumulate = true;
788 }
789}
790
792
793 // requiring background workspace is specified
794 if (!m_backgroundWS) {
795 return;
796 }
797
798 // Copy the MDHisto workspace, and change signals and errors to 0.
799 std::shared_ptr<IMDHistoWorkspace> tmp = this->getProperty("TemporaryBackgroundNormalizationWorkspace");
800 m_bkgdNormWS = std::dynamic_pointer_cast<MDHistoWorkspace>(tmp);
801 if (!m_bkgdNormWS) {
802 m_bkgdNormWS = bkgdDataWS.clone();
803 m_bkgdNormWS->setTo(0., 0., 0.);
804 }
805}
806
815void MDNorm::validateBinningForTemporaryDataWorkspace(const std::map<std::string, std::string> &parameters,
816 const Mantid::API::IMDHistoWorkspace_sptr &tempDataWS) {
817
818 // parse the paramters map and get extents from tempDataWS
819 const std::string numBinsStr = parameters.at("OutputBins");
820 const std::string extentsStr = parameters.at("OutputExtents");
821 const std::vector<size_t> numBins = VectorHelper::splitStringIntoVector<size_t>(numBinsStr);
822 const std::vector<double> extents = VectorHelper::splitStringIntoVector<double>(extentsStr);
823
824 // make sure the number of dimensions is the same for both workspaces
825 size_t numDimsTemp = tempDataWS->getNumDims();
826 if ((numBins.size() != numDimsTemp) || (extents.size() != numDimsTemp * 2)) {
827 std::stringstream errorMessage;
828 errorMessage << "The number of dimensions in the output and ";
829 errorMessage << "TemporaryDataWorkspace are not the same.";
830 throw(std::invalid_argument(errorMessage.str()));
831 }
832
833 // compare the extents and number of bins
834 for (size_t i = 0; i < numDimsTemp; i++) {
835 auto ax = tempDataWS->getDimension(i);
836 if (numBins[i] != ax->getNBins()) {
837 std::stringstream errorMessage;
838 errorMessage << "The number of bins output and number of bins in ";
839 errorMessage << "TemporaryDataWorkspace are not the same along ";
840 errorMessage << "dimension " << i;
841 throw(std::invalid_argument(errorMessage.str()));
842 }
843 if (std::abs(extents[2 * i] - ax->getMinimum()) > 1.e-5) {
844 std::stringstream errorMessage;
845 errorMessage << "The minimum binning value for the output and ";
846 errorMessage << "TemporaryDataWorkspace are not the same along ";
847 errorMessage << "dimension " << i;
848 throw(std::invalid_argument(errorMessage.str()));
849 }
850 if (std::abs(extents[2 * i + 1] - ax->getMaximum()) > 1.e-5) {
851 std::stringstream errorMessage;
852 errorMessage << "The maximum binning value for the output and ";
853 errorMessage << "TemporaryDataWorkspace are not the same along ";
854 errorMessage << "dimension " << i;
855 throw(std::invalid_argument(errorMessage.str()));
856 }
857 }
858
859 // sort out which axes are dimensional and check names
860 size_t parametersIndex = 0;
861 std::vector<size_t> dimensionIndex(numDimsTemp + 1, 3); // stores h, k, l or Qx, Qy, Qz dimensions
862 for (auto const &p : parameters) {
863 auto key = p.first;
864 auto value = p.second;
865 // value starts with QDimension0, then other stuff
866 // do not use ==
867 if (value.find("QDimension0") != std::string::npos) {
868 dimensionIndex[0] = parametersIndex;
869 const std::string dimXName = tempDataWS->getDimension(parametersIndex)->getName();
870 if (m_isRLU) { // hkl
871 if (dimXName != QDimensionName(m_Q0Basis)) {
872 std::stringstream errorMessage;
873 std::stringstream debugMessage;
874 errorMessage << "TemporaryDataWorkspace does not have the ";
875 errorMessage << "correct name for dimension " << parametersIndex;
876 debugMessage << "QDimension0 Names: Output will be: " << QDimensionName(m_Q0Basis);
877 debugMessage << " TemporaryDataWorkspace: " << dimXName;
878 g_log.warning(debugMessage.str());
879 throw(std::invalid_argument(errorMessage.str()));
880 }
881 } else {
882 if (dimXName != QDimensionNameQSample(0)) {
883 std::stringstream errorMessage;
884 std::stringstream debugMessage;
885 errorMessage << "TemporaryDataWorkspace does not have the ";
886 errorMessage << "correct name for dimension " << parametersIndex;
887 debugMessage << "QDimension0 Names: Output will be: " << QDimensionNameQSample(0);
888 debugMessage << " TemporaryDataWorkspace: " << dimXName;
889 g_log.warning(debugMessage.str());
890 throw(std::invalid_argument(errorMessage.str()));
891 }
892 }
893 } else if (value.find("QDimension1") != std::string::npos) {
894 dimensionIndex[1] = parametersIndex;
895 const std::string dimYName = tempDataWS->getDimension(parametersIndex)->getName();
896 if (m_isRLU) { // hkl
897 if (dimYName != QDimensionName(m_Q1Basis)) {
898 std::stringstream errorMessage;
899 std::stringstream debugMessage;
900 errorMessage << "TemporaryDataWorkspace does not have the ";
901 errorMessage << "correct name for dimension " << parametersIndex;
902 debugMessage << "QDimension1 Names: Output will be: " << QDimensionName(m_Q1Basis);
903 debugMessage << " TemporaryDataWorkspace: " << dimYName;
904 g_log.warning(debugMessage.str());
905 throw(std::invalid_argument(errorMessage.str()));
906 }
907 } else {
908 if (dimYName != QDimensionNameQSample(1)) {
909 std::stringstream errorMessage;
910 std::stringstream debugMessage;
911 errorMessage << "TemporaryDataWorkspace does not have the ";
912 errorMessage << "correct name for dimension " << parametersIndex;
913 debugMessage << "QDimension1 Names: Output will be: " << QDimensionNameQSample(1);
914 debugMessage << " TemporaryDataWorkspace: " << dimYName;
915 g_log.warning(debugMessage.str());
916 throw(std::invalid_argument(errorMessage.str()));
917 }
918 }
919 } else if (value.find("QDimension2") != std::string::npos) {
920 dimensionIndex[2] = parametersIndex;
921 const std::string dimZName = tempDataWS->getDimension(parametersIndex)->getName();
922 if (m_isRLU) { // hkl
923 if (dimZName != QDimensionName(m_Q2Basis)) {
924 std::stringstream errorMessage;
925 std::stringstream debugMessage;
926 errorMessage << "TemporaryDataWorkspace does not have the ";
927 errorMessage << "correct name for dimension " << parametersIndex;
928 debugMessage << "QDimension2 Names: Output will be: " << QDimensionName(m_Q2Basis);
929 debugMessage << " TemporaryDataWorkspace: " << dimZName;
930 g_log.warning(debugMessage.str());
931 throw(std::invalid_argument(errorMessage.str()));
932 }
933 } else {
934 if (dimZName != QDimensionNameQSample(2)) {
935 std::stringstream errorMessage;
936 std::stringstream debugMessage;
937 errorMessage << "TemporaryDataWorkspace does not have the ";
938 errorMessage << "correct name for dimension " << parametersIndex;
939 debugMessage << "QDimension2 Names: Output will be: " << QDimensionNameQSample(2);
940 debugMessage << " TemporaryDataWorkspace: " << dimZName;
941 g_log.warning(debugMessage.str());
942 throw(std::invalid_argument(errorMessage.str()));
943 }
944 }
945
946 } else if ((key != "OutputBins") && (key != "OutputExtents")) {
947 // make sure the names of non-directional dimensions are the same
948 const std::string nameData = tempDataWS->getDimension(parametersIndex)->getName();
949 if (value.find(nameData) != 0) {
950 g_log.error() << "Dimension " << nameData
951 << " from the temporary workspace"
952 " is not one of the binning dimensions, "
953 " or dimensions are in the wrong order."
954 << std::endl;
955 throw(std::invalid_argument("Beside the Q dimensions, "
956 "TemporaryDataWorkspace does not have the "
957 "same dimension names as OutputWorkspace."));
958 }
959 }
960 parametersIndex++;
961 }
962 const auto it = std::find_if(dimensionIndex.cbegin(), dimensionIndex.cend(),
963 [numDimsTemp](const auto &idx) { return idx > numDimsTemp; });
964 if (it != dimensionIndex.cend())
965 throw(std::invalid_argument("Cannot find at least one of QDimension0, "
966 "QDimension1, or QDimension2"));
967}
968
975 // calculate dimensions for binning
976 DblMatrix soMatrix(3, 3);
977 auto v = so.transformHKL(V3D(1, 0, 0));
978 soMatrix.setColumn(0, v);
979 v = so.transformHKL(V3D(0, 1, 0));
980 soMatrix.setColumn(1, v);
981 v = so.transformHKL(V3D(0, 0, 1));
982 soMatrix.setColumn(2, v);
983
984 return soMatrix;
985}
986
987// projection: input/output
988// requiring: m_hIdx, m_kIndex, m_lIdx, meidx, m_dEintegrated, m_Q0Basis,
989// mQ1Basis,
999inline void MDNorm::determineBasisVector(const size_t &qindex, const std::string &value,
1000 const Mantid::Kernel::DblMatrix &Qtransform, std::vector<double> &projection,
1001 std::stringstream &basisVector, std::vector<size_t> &qDimensionIndices) {
1002 if (value.find("QDimension0") != std::string::npos) {
1003 m_hIdx = qindex;
1004 if (!m_isRLU) {
1005 projection[0] = 1.;
1006 basisVector << QDimensionNameQSample(0) << ",A^{-1}";
1007 } else {
1008 qDimensionIndices.emplace_back(qindex);
1009 projection[0] = Qtransform[0][0];
1010 projection[1] = Qtransform[1][0];
1011 projection[2] = Qtransform[2][0];
1012 basisVector << QDimensionName(m_Q0Basis) << ", r.l.u.";
1013 }
1014 } else if (value.find("QDimension1") != std::string::npos) {
1015 m_kIdx = qindex;
1016 if (!m_isRLU) {
1017 projection[1] = 1.;
1018 basisVector << QDimensionNameQSample(1) << ",A^{-1}";
1019 } else {
1020 qDimensionIndices.emplace_back(qindex);
1021 projection[0] = Qtransform[0][1];
1022 projection[1] = Qtransform[1][1];
1023 projection[2] = Qtransform[2][1];
1024 basisVector << QDimensionName(m_Q1Basis) << ", r.l.u.";
1025 }
1026 } else if (value.find("QDimension2") != std::string::npos) {
1027 m_lIdx = qindex;
1028 if (!m_isRLU) {
1029 projection[2] = 1.;
1030 basisVector << QDimensionNameQSample(2) << ",A^{-1}";
1031 } else {
1032 qDimensionIndices.emplace_back(qindex);
1033 projection[0] = Qtransform[0][2];
1034 projection[1] = Qtransform[1][2];
1035 projection[2] = Qtransform[2][2];
1036 basisVector << QDimensionName(m_Q2Basis) << ", r.l.u.";
1037 }
1038 } else if (value.find("DeltaE") != std::string::npos) {
1039 m_eIdx = qindex;
1040 m_dEIntegrated = false;
1041 }
1042}
1043
1049inline void MDNorm::setQUnit(const std::vector<size_t> &qDimensionIndices,
1050 const Mantid::DataObjects::MDHistoWorkspace_sptr &outputMDHWS) {
1052 auto mdFrameFactory = Mantid::Geometry::makeMDFrameFactoryChain();
1053 Mantid::Geometry::MDFrame_uptr hklFrame = mdFrameFactory->create(argument);
1054 for (size_t i : qDimensionIndices) {
1055 auto mdHistoDimension = std::const_pointer_cast<Mantid::Geometry::MDHistoDimension>(
1056 std::dynamic_pointer_cast<const Mantid::Geometry::MDHistoDimension>(outputMDHWS->getDimension(i)));
1057 mdHistoDimension->setMDFrame(*hklFrame);
1058 }
1059 // add W_matrix
1060 auto ei = outputMDHWS->getExperimentInfo(0);
1061 ei->mutableRun().addProperty("W_MATRIX", m_W.getVector(), true);
1062}
1063
1070MDNorm::binBackgroundWS(const std::vector<Geometry::SymmetryOperation> &symmetryOps) {
1071 // Create output background data histogram MD workspace
1072 // Either from TemporaryBackgroundDataWorkspace
1073 // Or from scratch
1074 Mantid::API::IMDHistoWorkspace_sptr tempBkgdDataWS = this->getProperty("TemporaryBackgroundDataWorkspace");
1076
1077 // check that our input matches the temporary workspaces
1078 std::map<std::string, std::string> parameters = getBinParameters();
1079 if (tempBkgdDataWS) {
1080 validateBinningForTemporaryDataWorkspace(parameters, tempBkgdDataWS);
1081 }
1082
1083 // For each symmetry operation, do binning MD once
1084 std::vector<size_t> qDimensionIndices;
1085 uint16_t numexpinfo = static_cast<uint16_t>(m_inputWS->getNumExperimentInfo());
1086 if (m_numSymmOps != symmetryOps.size())
1087 throw std::runtime_error("Symmetry operation number m_umSymops is wrong!");
1088
1089 for (uint16_t i_expinfo = 0; i_expinfo < numexpinfo; ++i_expinfo) {
1090
1091 auto rotMatrix = m_inputWS->getExperimentInfo(i_expinfo)->run().getGoniometerMatrix();
1092
1093 // Reset symmetry operation index
1094 double soIndex = 0;
1095
1096 for (auto so : symmetryOps) {
1097 // Q transformation matrix: From Q_lab to HKL or Q_sample
1098 // Building symmetric operation matrix
1099 DblMatrix soMatrix = buildSymmetryMatrix(so);
1100 // Calculate Q transform matrix
1101 DblMatrix Qtransform;
1102 if (m_isRLU) {
1103 Qtransform = rotMatrix * m_UB * soMatrix * m_W;
1104 } else {
1105 Qtransform = rotMatrix * soMatrix * m_W;
1106 }
1107
1108 // Set up BinMD for this symmetry opeation
1109 double progress_fraction = 1. / static_cast<double>(symmetryOps.size() * numexpinfo);
1110 auto binMD =
1111 createChildAlgorithm("BinMD", soIndex * 0.3 * progress_fraction, (soIndex + 1) * 0.3 * progress_fraction);
1112
1113 binMD->setPropertyValue("AxisAligned", "0");
1114 binMD->setProperty("InputWorkspace", m_backgroundWS);
1115 binMD->setProperty("TemporaryDataWorkspace", tempBkgdDataWS);
1116 binMD->setPropertyValue("NormalizeBasisVectors", "0");
1117 // Set the output Workspace directly to Algorithm's
1118 // OutputBackgroundDataWorkspace
1119 binMD->setPropertyValue("OutputWorkspace", getPropertyValue("OutputBackgroundDataWorkspace"));
1120 // set binning properties
1121 size_t qindex = 0;
1122 for (auto const &p : parameters) {
1123 auto key = p.first;
1124 auto value = p.second;
1125 std::stringstream basisVector;
1126 std::vector<double> projection(m_inputWS->getNumDims(), 0.);
1127 // value is a string that can start with QDimension0, etc, but contain
1128 // other stuff. Do not use ==
1129 determineBasisVector(qindex, value, Qtransform, projection, basisVector, qDimensionIndices);
1130
1131 if (!basisVector.str().empty()) {
1132 // reconstruct from calculated basis vector
1133 for (auto proji : projection) {
1134 proji = std::abs(proji) > 1e-10 ? proji : 0.0;
1135 basisVector << "," << proji;
1136 }
1137 value = basisVector.str();
1138 }
1139
1140 binMD->setPropertyValue(key, value);
1141 qindex++;
1142 }
1143 // execute algorithm
1144 binMD->executeAsChildAlg();
1145
1146 // set the temporary workspace to be the output workspace, so it keeps
1147 // adding different symmetries AND
1148 // FIXME in future another ExpInfo
1149 outputWS = binMD->getProperty("OutputWorkspace");
1150 tempBkgdDataWS = std::dynamic_pointer_cast<MDHistoWorkspace>(outputWS);
1151 tempBkgdDataWS->clearOriginalWorkspaces();
1152 tempBkgdDataWS->clearTransforms();
1153 }
1154 }
1155 auto outputMDHWS = std::dynamic_pointer_cast<MDHistoWorkspace>(outputWS);
1156 // set MDUnits for Q dimensions
1157 if (m_isRLU) {
1158 setQUnit(qDimensionIndices, outputMDHWS);
1159 }
1160
1161 outputMDHWS->setDisplayNormalization(Mantid::API::NoNormalization);
1162 return outputMDHWS;
1163}
1164
1170DataObjects::MDHistoWorkspace_sptr MDNorm::binInputWS(const std::vector<Geometry::SymmetryOperation> &symmetryOps) {
1171 Mantid::API::IMDHistoWorkspace_sptr tempDataWS = this->getProperty("TemporaryDataWorkspace");
1173 std::map<std::string, std::string> parameters = getBinParameters();
1174
1175 // check that our input matches the temporary workspaces
1176 if (tempDataWS)
1177 validateBinningForTemporaryDataWorkspace(parameters, tempDataWS);
1178
1179 double soIndex = 0;
1180 std::vector<size_t> qDimensionIndices;
1181 for (auto so : symmetryOps) {
1182 // calculate dimensions for binning
1183 DblMatrix soMatrix = buildSymmetryMatrix(so);
1184 DblMatrix Qtransform;
1185
1186 if (m_isRLU) {
1187 Qtransform = m_UB * soMatrix * m_W;
1188 } else {
1189 Qtransform = soMatrix * m_W;
1190 }
1191
1192 // bin the data
1193 double fraction = 1. / static_cast<double>(symmetryOps.size());
1194 auto binMD = createChildAlgorithm("BinMD", soIndex * 0.3 * fraction, (soIndex + 1) * 0.3 * fraction);
1195 binMD->setPropertyValue("AxisAligned", "0");
1196 binMD->setProperty("InputWorkspace", m_inputWS);
1197 binMD->setProperty("TemporaryDataWorkspace", tempDataWS);
1198 binMD->setPropertyValue("NormalizeBasisVectors", "0");
1199 binMD->setPropertyValue("OutputWorkspace", getPropertyValue("OutputDataWorkspace"));
1200 // set binning properties
1201 size_t qindex = 0;
1202 for (auto const &p : parameters) {
1203 auto key = p.first;
1204 auto value = p.second;
1205 std::stringstream basisVector;
1206 std::vector<double> projection(m_inputWS->getNumDims(), 0.);
1207 // value is a string that can start with QDimension0, etc, but contain
1208 // other stuff. Do not use ==
1209 determineBasisVector(qindex, value, Qtransform, projection, basisVector, qDimensionIndices);
1210
1211 if (!basisVector.str().empty()) {
1212 // reconstruct from calculated basis vector
1213 for (auto proji : projection) {
1214 proji = std::abs(proji) > 1e-10 ? proji : 0.0;
1215 basisVector << "," << proji;
1216 }
1217 value = basisVector.str();
1218 }
1219
1220 binMD->setPropertyValue(key, value);
1221 qindex++;
1222 }
1223 // execute algorithm
1224 binMD->executeAsChildAlg();
1225 outputWS = binMD->getProperty("OutputWorkspace");
1226
1227 // set the temporary workspace to be the output workspace, so it keeps
1228 // adding different symmetries
1229 tempDataWS = std::dynamic_pointer_cast<MDHistoWorkspace>(outputWS);
1230 tempDataWS->clearOriginalWorkspaces();
1231 tempDataWS->clearTransforms();
1232 soIndex += 1;
1233 }
1234
1235 auto outputMDHWS = std::dynamic_pointer_cast<MDHistoWorkspace>(outputWS);
1236 // set MDUnits for Q dimensions
1237 if (m_isRLU) {
1238 setQUnit(qDimensionIndices, outputMDHWS);
1239 }
1240
1241 outputMDHWS->setDisplayNormalization(Mantid::API::NoNormalization);
1242 return outputMDHWS;
1243}
1244
1253std::vector<coord_t> MDNorm::getValuesFromOtherDimensions(bool &skipNormalization, uint16_t expInfoIndex) const {
1254 const auto &currentRun = m_inputWS->getExperimentInfo(expInfoIndex)->run();
1255
1256 std::vector<coord_t> otherDimValues;
1257 for (size_t i = 3; i < m_inputWS->getNumDims(); i++) {
1258 const auto dimension = m_inputWS->getDimension(i);
1259 auto inputDimMin = static_cast<float>(dimension->getMinimum());
1260 auto inputDimMax = static_cast<float>(dimension->getMaximum());
1261 coord_t outputDimMin(0), outputDimMax(0);
1262 bool isIntegrated = true;
1263
1264 for (size_t j = 0; j < m_transformation.numRows(); j++) {
1265 if (m_transformation[j][i] == 1) {
1266 isIntegrated = false;
1267 outputDimMin = m_normWS->getDimension(j)->getMinimum();
1268 outputDimMax = m_normWS->getDimension(j)->getMaximum();
1269 }
1270 }
1271 if (dimension->getName() == "DeltaE") {
1272 if ((inputDimMax < outputDimMin) || (inputDimMin > outputDimMax)) {
1273 skipNormalization = true;
1274 }
1275 } else {
1276 coord_t value = static_cast<coord_t>(
1277 currentRun.getLogAsSingleValue(dimension->getName(), Mantid::Kernel::Math::TimeAveragedMean));
1278 otherDimValues.emplace_back(value);
1279 if (value < inputDimMin || value > inputDimMax) {
1280 skipNormalization = true;
1281 }
1282 if ((!isIntegrated) && (value < outputDimMin || value > outputDimMax)) {
1283 skipNormalization = true;
1284 }
1285 }
1286 }
1287 return otherDimValues;
1288}
1289
1295 auto &hDim = *m_normWS->getDimension(m_hIdx);
1296 m_hX.resize(hDim.getNBoundaries());
1297 for (size_t i = 0; i < m_hX.size(); ++i) {
1298 m_hX[i] = hDim.getX(i);
1299 }
1300 auto &kDim = *m_normWS->getDimension(m_kIdx);
1301 m_kX.resize(kDim.getNBoundaries());
1302 for (size_t i = 0; i < m_kX.size(); ++i) {
1303 m_kX[i] = kDim.getX(i);
1304 }
1305
1306 auto &lDim = *m_normWS->getDimension(m_lIdx);
1307 m_lX.resize(lDim.getNBoundaries());
1308 for (size_t i = 0; i < m_lX.size(); ++i) {
1309 m_lX[i] = lDim.getX(i);
1310 }
1311
1312 if ((!m_diffraction) && (!m_dEIntegrated)) {
1313 // NOTE: store k final instead
1314 auto &eDim = *m_normWS->getDimension(m_eIdx);
1315 m_eX.resize(eDim.getNBoundaries());
1316 for (size_t i = 0; i < m_eX.size(); ++i) {
1317 double temp = m_Ei - eDim.getX(i);
1318 temp = std::max(temp, 0.);
1319 m_eX[i] = std::sqrt(energyToK * temp);
1320 }
1321 }
1322}
1323
1331 const Geometry::SymmetryOperation &so) {
1332 // Make it to a method!
1333 DblMatrix R = currentExpInfo.run().getGoniometerMatrix();
1334 DblMatrix soMatrix(3, 3);
1335 auto v = so.transformHKL(V3D(1, 0, 0));
1336 soMatrix.setColumn(0, v);
1337 v = so.transformHKL(V3D(0, 1, 0));
1338 soMatrix.setColumn(1, v);
1339 v = so.transformHKL(V3D(0, 0, 1));
1340 soMatrix.setColumn(2, v);
1341 soMatrix.Invert();
1342 DblMatrix Qtransform = R * m_UB * soMatrix * m_W;
1343 Qtransform.Invert();
1344
1345 return Qtransform;
1346}
1347
1357inline void MDNorm::calcDiffractionIntersectionIntegral(std::vector<std::array<double, 4>> &intersections,
1358 std::vector<double> &xValues, std::vector<double> &yValues,
1359 const API::MatrixWorkspace &integrFlux, const size_t &wsIdx) {
1360 // -- calculate integrals for the intersection --
1361 // momentum values at intersections
1362 auto intersectionsBegin = intersections.begin();
1363 // copy momenta to xValues
1364 xValues.resize(intersections.size());
1365 yValues.resize(intersections.size());
1366 auto x = xValues.begin();
1367 for (auto it = intersectionsBegin; it != intersections.end(); ++it, ++x) {
1368 *x = (*it)[3];
1369 }
1370 // calculate integrals at momenta from xValues by interpolating between
1371 // points in spectrum sp
1372 // of workspace integrFlux. The result is stored in yValues
1373 calcIntegralsForIntersections(xValues, integrFlux, wsIdx, yValues);
1374}
1375
1389inline void MDNorm::calcSingleDetectorNorm(const std::vector<std::array<double, 4>> &intersections, const double &solid,
1390 std::vector<double> &yValues, const size_t &vmdDims,
1391 std::vector<coord_t> &pos, std::vector<coord_t> &posNew,
1392 std::vector<std::atomic<signal_t>> &signalArray, const double &solidBkgd,
1393 std::vector<std::atomic<signal_t>> &bkgdSignalArray) {
1394
1395 auto intersectionsBegin = intersections.begin();
1396 for (auto it = intersectionsBegin + 1; it != intersections.end(); ++it) {
1397
1398 const auto &curIntSec = *it;
1399 const auto &prevIntSec = *(it - 1);
1400
1401 // The full vector isn't used so compute only what is necessary
1402 // If the difference between 2 adjacent intersection is trivial, no
1403 // intersection normalization is to be calculated
1404 double delta, eps;
1405 if (m_diffraction) {
1406 // diffraction
1407 delta = curIntSec[3] - prevIntSec[3];
1408 eps = 1e-7;
1409 } else {
1410 // inelastic
1411 delta = (curIntSec[3] * curIntSec[3] - prevIntSec[3] * prevIntSec[3]) / energyToK;
1412 eps = 1e-10;
1413 }
1414 if (delta < eps)
1415 continue; // Assume zero contribution if difference is small
1416
1417 // Average between two intersections for final position
1418 // [Task 89] Sample and background have same 'pos[]'
1419 std::transform(curIntSec.data(), curIntSec.data() + vmdDims, prevIntSec.data(), pos.begin(),
1420 [](const double rhs, const double lhs) { return static_cast<coord_t>(0.5 * (rhs + lhs)); });
1421 signal_t signal;
1422 signal_t bkgdSignal(0.);
1423 if (m_diffraction) {
1424 // Diffraction
1425 // index of the current intersection
1426 auto k = static_cast<size_t>(std::distance(intersectionsBegin, it));
1427 // signal = integral between two consecutive intersections
1428 signal = (yValues[k] - yValues[k - 1]) * solid;
1429 if (m_backgroundWS)
1430 bkgdSignal = (yValues[k] - yValues[k - 1]) * solidBkgd;
1431
1432 } else {
1433 // Inelastic
1434 // transform kf to energy transfer
1435 pos[3] = static_cast<coord_t>(m_Ei - pos[3] * pos[3] / energyToK);
1436 // signal = energy distance between two consecutive intersections *solid
1437 // angle *PC
1438 signal = solid * delta;
1439 if (m_backgroundWS)
1440 bkgdSignal = solidBkgd * delta;
1441 }
1442
1443 // Find the coordiate of the new position after transformation
1444 m_transformation.multiplyPoint(pos, posNew);
1445 // [Task 89] Is linIndex common to both sample and background?
1446 size_t linIndex = m_normWS->getLinearIndexAtCoord(posNew.data());
1447 if (linIndex == size_t(-1))
1448 continue; // not found
1449
1450 // Set to output
1451 // set the calculated signal to
1452 Mantid::Kernel::AtomicOp(signalArray[linIndex], signal, std::plus<signal_t>());
1453 // [Task 89]
1454 if (m_backgroundWS)
1455 Mantid::Kernel::AtomicOp(bkgdSignalArray[linIndex], bkgdSignal, std::plus<signal_t>());
1456 }
1457 return;
1458}
1459
1468void MDNorm::calculateNormalization(const std::vector<coord_t> &otherValues, const Geometry::SymmetryOperation &so,
1469 uint16_t expInfoIndex, size_t soIndex) {
1470 const auto &currentExptInfo = *(m_inputWS->getExperimentInfo(expInfoIndex));
1471 std::vector<double> lowValues, highValues;
1472 auto *lowValuesLog = dynamic_cast<VectorDoubleProperty *>(currentExptInfo.getLog("MDNorm_low"));
1473 lowValues = (*lowValuesLog)();
1474 auto *highValuesLog = dynamic_cast<VectorDoubleProperty *>(currentExptInfo.getLog("MDNorm_high"));
1475 highValues = (*highValuesLog)();
1476
1477 // calculate Q transformation matrix (R * UB * SymmetryOperation * m_W)^-1
1478 // in order to calculate intersections
1479 DblMatrix Qtransform = calQTransform(currentExptInfo, so);
1480
1481 // get proton charges
1482 const double protonCharge = currentExptInfo.run().getProtonCharge();
1483 // [Task 89]
1484 const double protonChargeBkgd =
1485 (m_backgroundWS != nullptr) ? m_backgroundWS->getExperimentInfo(0)->run().getProtonCharge() : 0;
1486
1487 const auto &spectrumInfo = currentExptInfo.spectrumInfo();
1488
1489 // Mappings: solid angle and flux workspaces' detector to ws_index map
1490 const auto ndets = static_cast<int64_t>(spectrumInfo.size());
1491 bool haveSA = false;
1492 API::MatrixWorkspace_const_sptr solidAngleWS = getProperty("SolidAngleWorkspace");
1493 if (solidAngleWS != nullptr) {
1494 haveSA = true;
1495 }
1496 API::MatrixWorkspace_const_sptr integrFlux = getProperty("FluxWorkspace");
1497 const detid2index_map solidAngDetToIdx =
1498 (haveSA) ? solidAngleWS->getDetectorIDToWorkspaceIndexMap() : detid2index_map();
1499 const detid2index_map fluxDetToIdx =
1500 (m_diffraction) ? integrFlux->getDetectorIDToWorkspaceIndexMap() : detid2index_map();
1501
1502 // Define dimension, signal array
1503 const size_t vmdDims = (m_diffraction) ? 3 : 4;
1504 std::vector<std::atomic<signal_t>> signalArray(m_normWS->getNPoints());
1505
1506 size_t numNPoints = (m_backgroundWS) ? m_bkgdNormWS->getNPoints() : 0;
1507 if (m_backgroundWS && numNPoints != m_normWS->getNPoints()) {
1508 throw std::runtime_error("N points are different");
1509 }
1510 std::vector<std::atomic<signal_t>> bkgdSignalArray(numNPoints);
1511
1512 std::vector<std::array<double, 4>> intersections;
1513 std::vector<double> xValues, yValues;
1514 std::vector<coord_t> pos, posNew;
1515
1516 // Progress report
1517 double progStep = 0.7 / static_cast<double>(m_numExptInfos * m_numSymmOps);
1518 auto progIndex = static_cast<double>(soIndex + expInfoIndex * m_numSymmOps);
1519 auto prog =
1520 std::make_unique<API::Progress>(this, 0.3 + progStep * progIndex, 0.3 + progStep * (1. + progIndex), ndets);
1521 // muliple threading
1522 bool safe = m_diffraction ? Kernel::threadSafe(*integrFlux) : true;
1523
1524PRAGMA_OMP(parallel for private(intersections, xValues, yValues, pos, posNew) if (safe))
1525for (int64_t i = 0; i < ndets; i++) {
1527
1528 // Skip: non-existing detector, monitor and masked detector
1529 if (!spectrumInfo.hasDetectors(i) || spectrumInfo.isMonitor(i) || spectrumInfo.isMasked(i)) {
1530 continue;
1531 }
1532
1533 const auto &detector = spectrumInfo.detector(i);
1534 double theta = detector.getTwoTheta(m_samplePos, m_beamDir);
1535 double phi = detector.getPhi();
1536 // If the dtefctor is a group, this should be the ID of the first detector
1537 const auto detID = detector.getID();
1538
1539 // get the flux spectrum number: this is for diffraction only!
1540 size_t wsIdx = 0;
1541 if (m_diffraction) {
1542 auto index = fluxDetToIdx.find(detID);
1543 if (index != fluxDetToIdx.end()) {
1544 wsIdx = index->second;
1545 } else { // masked detector in flux, but not in input workspace
1546 continue;
1547 }
1548 }
1549
1550 // Intersections for sample and background if present
1551 this->calculateIntersections(intersections, theta, phi, Qtransform, lowValues[i], highValues[i]);
1552
1553 // No need to do normalization calculation if there is no intersection
1554 if (intersections.empty())
1555 continue;
1556
1557 // Get solid angle for this contribution
1558 double solid = protonCharge;
1559 // [Task 89]
1560 double bkgdSolid = protonChargeBkgd;
1561 if (haveSA) {
1562 double solid_angle_factor = solidAngleWS->y(solidAngDetToIdx.find(detID)->second)[0];
1563 // solidAngleWS->y(solidAngDetToIdx.find(detID)->second)[0]
1564 solid = solid_angle_factor * protonCharge;
1565 // [Task 89]
1566 bkgdSolid = solid_angle_factor * protonChargeBkgd;
1567 }
1568
1569 if (m_diffraction) {
1570 // -- calculate integrals for the intersection --
1571 calcDiffractionIntersectionIntegral(intersections, xValues, yValues, *integrFlux, wsIdx);
1572 }
1573
1574 // Compute final position in HKL
1575 // pre-allocate for efficiency and copy non-hkl dim values into place
1576 pos.resize(vmdDims + otherValues.size());
1577 std::copy(otherValues.begin(), otherValues.end(), pos.begin() + vmdDims);
1578
1579 calcSingleDetectorNorm(intersections, solid, yValues, vmdDims, pos, posNew, signalArray, bkgdSolid,
1580 bkgdSignalArray); // [Task 89] ADD solidBkgd, bkgdYValues, bkgdSignalArray
1581
1582 prog->report();
1583
1585}
1587if (m_accumulate) {
1588 std::transform(signalArray.cbegin(), signalArray.cend(), m_normWS->getSignalArray(), m_normWS->mutableSignalArray(),
1589 [](const std::atomic<signal_t> &a, const signal_t &b) { return a + b; });
1590 // [Task 89] Process background
1591 if (m_backgroundWS)
1592 std::transform(bkgdSignalArray.cbegin(), bkgdSignalArray.cend(), m_bkgdNormWS->getSignalArray(),
1593 m_bkgdNormWS->mutableSignalArray(),
1594 [](const std::atomic<signal_t> &a, const signal_t &b) { return a + b; });
1595
1596} else {
1597 // First time, init
1598 std::copy(signalArray.cbegin(), signalArray.cend(), m_normWS->mutableSignalArray());
1599 // [Task 89]
1600 if (m_backgroundWS)
1601 std::copy(bkgdSignalArray.cbegin(), bkgdSignalArray.cend(), m_bkgdNormWS->mutableSignalArray());
1602}
1603m_accumulate = true;
1604}
1605
1616void MDNorm::calculateIntersections(std::vector<std::array<double, 4>> &intersections, const double theta,
1617 const double phi, const Kernel::DblMatrix &transform, double lowvalue,
1618 double highvalue) {
1619 V3D qout(sin(theta) * cos(phi), sin(theta) * sin(phi), cos(theta)), qin(0., 0., 1);
1620
1621 qout = transform * qout;
1622 qin = transform * qin;
1623 if (convention == "Crystallography") {
1624 qout *= -1;
1625 qin *= -1;
1626 }
1627 double kfmin, kfmax, kimin, kimax;
1628 if (m_diffraction) {
1629 kimin = lowvalue;
1630 kimax = highvalue;
1631 kfmin = kimin;
1632 kfmax = kimax;
1633 } else {
1634 kimin = std::sqrt(energyToK * m_Ei);
1635 kimax = kimin;
1636 kfmin = std::sqrt(energyToK * (m_Ei - highvalue));
1637 kfmax = std::sqrt(energyToK * (m_Ei - lowvalue));
1638 }
1639
1640 double hStart = qin.X() * kimin - qout.X() * kfmin, hEnd = qin.X() * kimax - qout.X() * kfmax;
1641 double kStart = qin.Y() * kimin - qout.Y() * kfmin, kEnd = qin.Y() * kimax - qout.Y() * kfmax;
1642 double lStart = qin.Z() * kimin - qout.Z() * kfmin, lEnd = qin.Z() * kimax - qout.Z() * kfmax;
1643
1644 double eps = 1e-10;
1645 auto hNBins = m_hX.size();
1646 auto kNBins = m_kX.size();
1647 auto lNBins = m_lX.size();
1648 auto eNBins = m_eX.size();
1649 intersections.clear();
1650 intersections.reserve(hNBins + kNBins + lNBins + eNBins + 2);
1651
1652 // calculate intersections with planes perpendicular to h
1653 if (fabs(hStart - hEnd) > eps) {
1654 double fmom = (kfmax - kfmin) / (hEnd - hStart);
1655 double fk = (kEnd - kStart) / (hEnd - hStart);
1656 double fl = (lEnd - lStart) / (hEnd - hStart);
1657 for (size_t i = 0; i < hNBins; i++) {
1658 double hi = m_hX[i];
1659 if (((hStart - hi) * (hEnd - hi) < 0)) {
1660 // if hi is between hStart and hEnd, then ki and li will be between
1661 // kStart, kEnd and lStart, lEnd and momi will be between kfmin and
1662 // kfmax
1663 double ki = fk * (hi - hStart) + kStart;
1664 double li = fl * (hi - hStart) + lStart;
1665 if ((ki >= m_kX[0]) && (ki <= m_kX[kNBins - 1]) && (li >= m_lX[0]) && (li <= m_lX[lNBins - 1])) {
1666 double momi = fmom * (hi - hStart) + kfmin;
1667 intersections.push_back({{hi, ki, li, momi}});
1668 }
1669 }
1670 }
1671 }
1672 // calculate intersections with planes perpendicular to k
1673 if (fabs(kStart - kEnd) > eps) {
1674 double fmom = (kfmax - kfmin) / (kEnd - kStart);
1675 double fh = (hEnd - hStart) / (kEnd - kStart);
1676 double fl = (lEnd - lStart) / (kEnd - kStart);
1677 for (size_t i = 0; i < kNBins; i++) {
1678 double ki = m_kX[i];
1679 if (((kStart - ki) * (kEnd - ki) < 0)) {
1680 // if ki is between kStart and kEnd, then hi and li will be between
1681 // hStart, hEnd and lStart, lEnd and momi will be between kfmin and
1682 // kfmax
1683 double hi = fh * (ki - kStart) + hStart;
1684 double li = fl * (ki - kStart) + lStart;
1685 if ((hi >= m_hX[0]) && (hi <= m_hX[hNBins - 1]) && (li >= m_lX[0]) && (li <= m_lX[lNBins - 1])) {
1686 double momi = fmom * (ki - kStart) + kfmin;
1687 intersections.push_back({{hi, ki, li, momi}});
1688 }
1689 }
1690 }
1691 }
1692
1693 // calculate intersections with planes perpendicular to l
1694 if (fabs(lStart - lEnd) > eps) {
1695 double fmom = (kfmax - kfmin) / (lEnd - lStart);
1696 double fh = (hEnd - hStart) / (lEnd - lStart);
1697 double fk = (kEnd - kStart) / (lEnd - lStart);
1698
1699 for (size_t i = 0; i < lNBins; i++) {
1700 double li = m_lX[i];
1701 if (((lStart - li) * (lEnd - li) < 0)) {
1702 double hi = fh * (li - lStart) + hStart;
1703 double ki = fk * (li - lStart) + kStart;
1704 if ((hi >= m_hX[0]) && (hi <= m_hX[hNBins - 1]) && (ki >= m_kX[0]) && (ki <= m_kX[kNBins - 1])) {
1705 double momi = fmom * (li - lStart) + kfmin;
1706 intersections.push_back({{hi, ki, li, momi}});
1707 }
1708 }
1709 }
1710 }
1711 // intersections with dE
1712 if (!m_dEIntegrated) {
1713 for (size_t i = 0; i < eNBins; i++) {
1714 double kfi = m_eX[i];
1715 if ((kfi - kfmin) * (kfi - kfmax) <= 0) {
1716 double h = qin.X() * kimin - qout.X() * kfi;
1717 double k = qin.Y() * kimin - qout.Y() * kfi;
1718 double l = qin.Z() * kimin - qout.Z() * kfi;
1719 if ((h >= m_hX[0]) && (h <= m_hX[hNBins - 1]) && (k >= m_kX[0]) && (k <= m_kX[kNBins - 1]) && (l >= m_lX[0]) &&
1720 (l <= m_lX[lNBins - 1])) {
1721 intersections.push_back({{h, k, l, kfi}});
1722 }
1723 }
1724 }
1725 }
1726
1727 // endpoints
1728 if ((hStart >= m_hX[0]) && (hStart <= m_hX[hNBins - 1]) && (kStart >= m_kX[0]) && (kStart <= m_kX[kNBins - 1]) &&
1729 (lStart >= m_lX[0]) && (lStart <= m_lX[lNBins - 1])) {
1730 intersections.push_back({{hStart, kStart, lStart, kfmin}});
1731 }
1732 if ((hEnd >= m_hX[0]) && (hEnd <= m_hX[hNBins - 1]) && (kEnd >= m_kX[0]) && (kEnd <= m_kX[kNBins - 1]) &&
1733 (lEnd >= m_lX[0]) && (lEnd <= m_lX[lNBins - 1])) {
1734 intersections.push_back({{hEnd, kEnd, lEnd, kfmax}});
1735 }
1736
1737 // sort intersections by final momentum
1738 std::stable_sort(intersections.begin(), intersections.end(), compareMomentum);
1739}
1740
1749void MDNorm::calcIntegralsForIntersections(const std::vector<double> &xValues, const API::MatrixWorkspace &integrFlux,
1750 size_t sp, std::vector<double> &yValues) {
1751 assert(xValues.size() == yValues.size());
1752
1753 // the x-data from the workspace
1754 const auto &xData = integrFlux.x(sp);
1755 const double xStart = xData.front();
1756 const double xEnd = xData.back();
1757
1758 // the values in integrFlux are expected to be integrals of a non-negative
1759 // function
1760 // ie they must make a non-decreasing function
1761 const auto &yData = integrFlux.y(sp);
1762 size_t spSize = yData.size();
1763
1764 const double yMin = 0.0;
1765 const double yMax = yData.back();
1766
1767 size_t nData = xValues.size();
1768 // all integrals below xStart must be 0
1769 if (xValues[nData - 1] < xStart) {
1770 std::fill(yValues.begin(), yValues.end(), yMin);
1771 return;
1772 }
1773
1774 // all integrals above xEnd must be equal tp yMax
1775 if (xValues[0] > xEnd) {
1776 std::fill(yValues.begin(), yValues.end(), yMax);
1777 return;
1778 }
1779
1780 size_t i = 0;
1781 // integrals below xStart must be 0
1782 while (i < nData - 1 && xValues[i] < xStart) {
1783 yValues[i] = yMin;
1784 i++;
1785 }
1786 size_t j = 0;
1787 for (; i < nData; i++) {
1788 // integrals above xEnd must be equal tp yMax
1789 if (j >= spSize - 1) {
1790 yValues[i] = yMax;
1791 } else {
1792 double xi = xValues[i];
1793 while (j < spSize - 1 && xi > xData[j])
1794 j++;
1795 // if x falls onto an interpolation point return the corresponding y
1796 if (xi == xData[j]) {
1797 yValues[i] = yData[j];
1798 } else if (j == spSize - 1) {
1799 // if we get above xEnd it's yMax
1800 yValues[i] = yMax;
1801 } else if (j > 0) {
1802 // interpolate between the consecutive points
1803 double x0 = xData[j - 1];
1804 double x1 = xData[j];
1805 double y0 = yData[j - 1];
1806 double y1 = yData[j];
1807 yValues[i] = y0 + (y1 - y0) * (xi - x0) / (x1 - x0);
1808 } else // j == 0
1809 {
1810 yValues[i] = yMin;
1811 }
1812 }
1813 }
1814}
1815
1816} // namespace Mantid::MDAlgorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
gsl_vector * tmp
const std::vector< double > & rhs
double value
The value of the point.
Definition: FitMW.cpp:51
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
#define fabs(x)
Definition: Matrix.cpp:22
#define PARALLEL_START_INTERRUPT_REGION
Begins a block to skip processing is the algorithm has been interupted Note the end of the block if n...
Definition: MultiThreaded.h:94
#define PARALLEL_END_INTERRUPT_REGION
Ends a block to skip processing is the algorithm has been interupted Note the start of the block if n...
#define PRAGMA_OMP(expression)
#define PARALLEL_CHECK_INTERRUPT_REGION
Adds a check after a Parallel region to see if it was interupted.
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
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
Definition: Algorithm.cpp:2026
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
A validator which provides a TENTATIVE check that a workspace contains common bins in each spectrum.
Kernel::IValidator_sptr clone() const override
Clone the current state.
This class is shared by a few Workspace types and holds information related to a particular experimen...
const Run & run() const
Run details object access.
A validator which checks that a workspace has a valid instrument.
Base MatrixWorkspace Abstract Class.
const HistogramData::HistogramX & x(const size_t index) const
const HistogramData::HistogramY & y(const size_t index) const
const Kernel::Matrix< double > & getGoniometerMatrix() const
Retrieve the first goniometer rotation matrix.
Definition: Run.cpp:369
A property class for workspaces.
std::unique_ptr< MDHistoWorkspace > clone() const
Returns a clone of the workspace.
static const std::string HKLName
Definition: HKL.h:27
Input argument type for MDFrameFactory chainable factory.
Class to implement UB matrix.
void setUB(const Kernel::DblMatrix &newUB)
Sets the UB matrix and recalculates lattice parameters.
static const std::string QLabName
Definition: QLab.h:35
static const std::string QSampleName
Definition: QSample.h:23
Crystallographic symmetry operations are composed of a rotational component, which is represented by ...
Kernel::V3D transformHKL(const Kernel::V3D &hkl) const
Transforms an index triplet hkl.
double a(int nd) const
Get lattice parameter a1-a3 as function of index (0-2)
Definition: UnitCell.cpp:94
double c() const
Get lattice parameter.
Definition: UnitCell.cpp:128
double b() const
Get lattice parameter.
Definition: UnitCell.cpp:123
Support for a property that holds an array of values.
Definition: ArrayProperty.h:28
Exception for errors associated with the instrument definition.
Definition: Exception.h:220
Records the filename, the description of failure and the line on which it happened.
Definition: Exception.h:115
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void setPropertySettings(const std::string &name, std::unique_ptr< IPropertySettings > settings)
void setPropertyGroup(const std::string &name, const std::string &group)
Set the group for a given property.
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
T determinant() const
Calculate the determinant.
Definition: Matrix.cpp:1048
T Invert()
LU inversion routine.
Definition: Matrix.cpp:924
void multiplyPoint(const std::vector< T > &in, std::vector< T > &out) const
Multiply M*Vec.
Definition: Matrix.cpp:375
std::vector< T > getVector() const
Definition: Matrix.cpp:77
size_t numRows() const
Return the number of rows in the matrix.
Definition: Matrix.h:144
void setColumn(const size_t nCol, const std::vector< T > &newCol)
Definition: Matrix.cpp:675
Matrix< T > & Transpose()
Transpose the matrix.
Definition: Matrix.cpp:793
The concrete, templated class for properties.
Base class for properties.
Definition: Property.h:94
virtual std::string value() const =0
Returns the value of the property as a string.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
static const UnitLabel RLU
Reciprocal lattice units.
Class for 3D vectors.
Definition: V3D.h:34
constexpr double X() const noexcept
Get x.
Definition: V3D.h:232
constexpr double Y() const noexcept
Get y.
Definition: V3D.h:233
constexpr double Z() const noexcept
Get z.
Definition: V3D.h:234
MDNormalization : Bin single crystal diffraction or direct geometry inelastic data and calculate the ...
Definition: MDNorm.h:21
size_t m_numSymmOps
number of symmetry operations
Definition: MDNorm.h:113
Mantid::Kernel::DblMatrix m_W
W matrix.
Definition: MDNorm.h:102
void calcSingleDetectorNorm(const std::vector< std::array< double, 4 > > &intersections, const double &solid, std::vector< double > &yValues, const size_t &vmdDims, std::vector< coord_t > &pos, std::vector< coord_t > &posNew, std::vector< std::atomic< signal_t > > &signalArray, const double &solidBkgd, std::vector< std::atomic< signal_t > > &bkgdSignalArray)
Calculate the normalization among intersections on a single detector in 1 specific SpectrumInfo/Exper...
Definition: MDNorm.cpp:1389
Mantid::Kernel::Matrix< coord_t > m_transformation
matrix for transforming from intersections to positions in the normalization workspace
Definition: MDNorm.h:105
bool m_dEIntegrated
Flag to indicate that the energy dimension is integrated.
Definition: MDNorm.h:121
DataObjects::MDHistoWorkspace_sptr binBackgroundWS(const std::vector< Geometry::SymmetryOperation > &symmetryOps)
Bin(MD) input Background workspace.
Definition: MDNorm.cpp:1070
std::string QDimensionName(std::vector< double > projection)
Get the dimension name when using reciprocal lattice units.
Definition: MDNorm.cpp:605
bool m_diffraction
Flag indicating if the input workspace is from diffraction.
Definition: MDNorm.h:117
void createNormalizationWS(const DataObjects::MDHistoWorkspace &dataWS)
Create & cached the normalization workspace.
Definition: MDNorm.cpp:778
void exec() override
Execute the algorithm.
Definition: MDNorm.cpp:447
bool m_accumulate
Flag to accumulate normalization.
Definition: MDNorm.h:119
void calcIntegralsForIntersections(const std::vector< double > &xValues, const API::MatrixWorkspace &integrFlux, size_t sp, std::vector< double > &yValues)
Linearly interpolate between the points in integrFlux at xValues and save the results in yValues.
Definition: MDNorm.cpp:1749
std::vector< double > m_Q0Basis
The projection vectors.
Definition: MDNorm.h:98
DataObjects::MDHistoWorkspace_sptr m_normWS
Normalization workspace.
Definition: MDNorm.h:88
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
Definition: MDNorm.cpp:78
std::map< std::string, std::string > getBinParameters()
Calculate binning parameters.
Definition: MDNorm.cpp:634
API::IMDWorkspace_sptr divideMD(const API::IMDHistoWorkspace_sptr &lhs, const API::IMDHistoWorkspace_sptr &rhs, const std::string &outputwsname, const double &startProgress, const double &endProgress)
Definition: MDNorm.cpp:570
const std::string category() const override
Algorithm's category for identification.
Definition: MDNorm.cpp:75
std::vector< double > m_lX
Definition: MDNorm.h:107
std::vector< double > m_kX
Definition: MDNorm.h:107
std::vector< coord_t > getValuesFromOtherDimensions(bool &skipNormalization, uint16_t expInfoIndex=0) const
Retrieve logged values from non-HKL dimensions.
Definition: MDNorm.cpp:1253
void setQUnit(const std::vector< size_t > &qDimensionIndices, const Mantid::DataObjects::MDHistoWorkspace_sptr &outputMDHWS)
Set the output Frame to HKL.
Definition: MDNorm.cpp:1049
DataObjects::MDHistoWorkspace_sptr m_bkgdNormWS
Definition: MDNorm.h:89
void calculateIntersections(std::vector< std::array< double, 4 > > &intersections, const double theta, const double phi, const Kernel::DblMatrix &transform, double lowvalue, double highvalue)
Calculate the points of intersection for the given detector with cuboid surrounding the detector posi...
Definition: MDNorm.cpp:1616
API::IMDEventWorkspace_sptr m_inputWS
Input workspace.
Definition: MDNorm.h:91
double m_Ei
Cached value of incident energy dor direct geometry.
Definition: MDNorm.h:115
size_t m_hIdx
index of h,k,l, dE dimensions in the output workspaces
Definition: MDNorm.h:109
Mantid::Kernel::DblMatrix m_UB
UB matrix.
Definition: MDNorm.h:100
std::vector< double > m_Q1Basis
Definition: MDNorm.h:98
void validateBinningForTemporaryDataWorkspace(const std::map< std::string, std::string > &, const Mantid::API::IMDHistoWorkspace_sptr &)
Validates the TemporaryDataWorkspace has the same binning as the input binning parameters.
Definition: MDNorm.cpp:815
Mantid::Kernel::DblMatrix buildSymmetryMatrix(const Geometry::SymmetryOperation &so)
build symmetry matrix
Definition: MDNorm.cpp:974
Kernel::V3D m_beamDir
Beam direction.
Definition: MDNorm.h:125
size_t m_numExptInfos
number of experimentInfo objects
Definition: MDNorm.h:111
void calcDiffractionIntersectionIntegral(std::vector< std::array< double, 4 > > &intersections, std::vector< double > &xValues, std::vector< double > &yValues, const API::MatrixWorkspace &integrFlux, const size_t &wsIdx)
Calculate the diffraction MDE's intersection integral of a certain detector/spectru.
Definition: MDNorm.cpp:1357
std::string QDimensionNameQSample(int i)
Get the dimension name when not using reciprocal lattice units.
Definition: MDNorm.cpp:589
void cacheDimensionXValues()
Stores the X values from each H,K,L, and optionally DeltaE dimension as member variables.
Definition: MDNorm.cpp:1294
std::vector< double > m_hX
cached X values along dimensions h,k,l. dE
Definition: MDNorm.h:107
const std::string name() const override
Algorithms name for identification.
Definition: MDNorm.cpp:69
std::vector< double > m_Q2Basis
Definition: MDNorm.h:98
bool m_isRLU
flag for reciprocal lattice units
Definition: MDNorm.h:96
DataObjects::MDHistoWorkspace_sptr binInputWS(const std::vector< Geometry::SymmetryOperation > &symmetryOps)
Bin(MD) input MDE workspace.
Definition: MDNorm.cpp:1170
void calculateNormalization(const std::vector< coord_t > &otherValues, const Geometry::SymmetryOperation &so, uint16_t expInfoIndex, size_t soIndex)
Computed the normalization for the input workspace.
Definition: MDNorm.cpp:1468
std::string convention
ki-kf for Inelastic convention; kf-ki for Crystallography convention
Definition: MDNorm.h:127
Mantid::Kernel::DblMatrix calQTransform(const Mantid::API::ExperimentInfo &currentExpInfo, const Geometry::SymmetryOperation &so)
Calculate QTransform = (R * UB * SymmetryOperation * m_W)^-1.
Definition: MDNorm.cpp:1330
std::map< std::string, std::string > validateInputs() override final
Validate the input workspace.
Definition: MDNorm.cpp:213
void determineBasisVector(const size_t &qindex, const std::string &value, const Kernel::DblMatrix &Qtransform, std::vector< double > &projection, std::stringstream &basisVector, std::vector< size_t > &qDimensionIndices)
MDNorm::determineBasisVector.
Definition: MDNorm.cpp:999
int version() const override
Algorithm's version for identification.
Definition: MDNorm.cpp:72
void init() override
Initialize the algorithm's properties.
Definition: MDNorm.cpp:86
API::IMDEventWorkspace_sptr m_backgroundWS
Input background workspace.
Definition: MDNorm.h:93
std::vector< double > m_eX
Definition: MDNorm.h:107
void createBackgroundNormalizationWS(const DataObjects::MDHistoWorkspace &dataWS)
Definition: MDNorm.cpp:791
Kernel::V3D m_samplePos
Sample position.
Definition: MDNorm.h:123
std::shared_ptr< IMDEventWorkspace > IMDEventWorkspace_sptr
Shared pointer to Mantid::API::IMDEventWorkspace.
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Definition: Workspace_fwd.h:20
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
std::shared_ptr< IMDHistoWorkspace > IMDHistoWorkspace_sptr
shared pointer to Mantid::API::IMDHistoWorkspace
std::shared_ptr< IMDWorkspace > IMDWorkspace_sptr
Shared pointer to the IMDWorkspace base class.
Definition: IMDWorkspace.h:146
@ NoNormalization
Don't normalize = return raw counts.
Definition: IMDIterator.h:27
std::shared_ptr< MDHistoWorkspace > MDHistoWorkspace_sptr
A shared pointer to a MDHistoWorkspace.
std::unique_ptr< MDFrame > MDFrame_uptr
Definition: MDFrame.h:36
MDFrameFactory_uptr MANTID_GEOMETRY_DLL makeMDFrameFactoryChain()
Make a complete factory chain.
std::string toString(const T &value)
Convert a number to a string.
Definition: Strings.cpp:703
template DLLExport std::vector< double > splitStringIntoVector< double >(std::string listString)
template DLLExport std::vector< size_t > splitStringIntoVector< size_t >(std::string listString)
std::enable_if< std::is_pointer< Arg >::value, bool >::type threadSafe(Arg workspace)
Thread-safety check Checks the workspace to ensure it is suitable for multithreaded access.
Definition: MultiThreaded.h:22
MANTID_KERNEL_DLL V3D normalize(V3D v)
Normalizes a V3D.
Definition: V3D.h:341
void AtomicOp(std::atomic< T > &f, T d, BinaryOp op)
Uses std::compare_exchange_weak to update the atomic value f = op(f, d) Used to improve parallel scal...
Definition: MultiThreaded.h:67
Mantid::Kernel::Matrix< double > DblMatrix
Definition: Matrix.h:206
Kernel::PropertyWithValue< std::vector< double > > VectorDoubleProperty
static constexpr double NeutronMass
Mass of the neutron in kg.
static constexpr double h
Planck constant in J*s.
static constexpr double meV
1 meV in Joules.
float coord_t
Typedef for the data type to use for coordinate axes in MD objects such as MDBox, MDEventWorkspace,...
Definition: MDTypes.h:27
std::unordered_map< detid_t, size_t > detid2index_map
Map with key = detector ID, value = workspace index.
double signal_t
Typedef for the signal recorded in a MDBox, etc.
Definition: MDTypes.h:36
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54