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 {
359 Geometry::SymmetryOperationFactory::Instance().createSymOps(symOps);
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 (const 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
813void MDNorm::validateBinningForTemporaryDataWorkspace(const std::map<std::string, std::string> &parameters,
814 const Mantid::API::IMDHistoWorkspace_sptr &tempDataWS) {
815
816 // parse the paramters map and get extents from tempDataWS
817 const std::string numBinsStr = parameters.at("OutputBins");
818 const std::string extentsStr = parameters.at("OutputExtents");
819 const std::vector<size_t> numBins = VectorHelper::splitStringIntoVector<size_t>(numBinsStr);
820 const std::vector<double> extents = VectorHelper::splitStringIntoVector<double>(extentsStr);
821
822 // make sure the number of dimensions is the same for both workspaces
823 size_t numDimsTemp = tempDataWS->getNumDims();
824 if ((numBins.size() != numDimsTemp) || (extents.size() != numDimsTemp * 2)) {
825 std::stringstream errorMessage;
826 errorMessage << "The number of dimensions in the output and ";
827 errorMessage << "TemporaryDataWorkspace are not the same.";
828 throw(std::invalid_argument(errorMessage.str()));
829 }
830
831 // compare the extents and number of bins
832 for (size_t i = 0; i < numDimsTemp; i++) {
833 auto ax = tempDataWS->getDimension(i);
834 if (numBins[i] != ax->getNBins()) {
835 std::stringstream errorMessage;
836 errorMessage << "The number of bins output and number of bins in ";
837 errorMessage << "TemporaryDataWorkspace are not the same along ";
838 errorMessage << "dimension " << i;
839 throw(std::invalid_argument(errorMessage.str()));
840 }
841 if (std::abs(extents[2 * i] - ax->getMinimum()) > 1.e-5) {
842 std::stringstream errorMessage;
843 errorMessage << "The minimum binning value for the output and ";
844 errorMessage << "TemporaryDataWorkspace are not the same along ";
845 errorMessage << "dimension " << i;
846 throw(std::invalid_argument(errorMessage.str()));
847 }
848 if (std::abs(extents[2 * i + 1] - ax->getMaximum()) > 1.e-5) {
849 std::stringstream errorMessage;
850 errorMessage << "The maximum binning value for the output and ";
851 errorMessage << "TemporaryDataWorkspace are not the same along ";
852 errorMessage << "dimension " << i;
853 throw(std::invalid_argument(errorMessage.str()));
854 }
855 }
856
857 // sort out which axes are dimensional and check names
858 size_t parametersIndex = 0;
859 std::vector<size_t> dimensionIndex(numDimsTemp + 1, 3); // stores h, k, l or Qx, Qy, Qz dimensions
860 for (const auto &p : parameters) {
861 auto key = p.first;
862 auto value = p.second;
863 // value starts with QDimension0, then other stuff
864 // do not use ==
865 if (value.find("QDimension0") != std::string::npos) {
866 dimensionIndex[0] = parametersIndex;
867 const std::string dimXName = tempDataWS->getDimension(parametersIndex)->getName();
868 if (m_isRLU) { // hkl
869 if (dimXName != QDimensionName(m_Q0Basis)) {
870 std::stringstream errorMessage;
871 std::stringstream debugMessage;
872 errorMessage << "TemporaryDataWorkspace does not have the ";
873 errorMessage << "correct name for dimension " << parametersIndex;
874 debugMessage << "QDimension0 Names: Output will be: " << QDimensionName(m_Q0Basis);
875 debugMessage << " TemporaryDataWorkspace: " << dimXName;
876 g_log.warning(debugMessage.str());
877 throw(std::invalid_argument(errorMessage.str()));
878 }
879 } else {
880 if (dimXName != QDimensionNameQSample(0)) {
881 std::stringstream errorMessage;
882 std::stringstream debugMessage;
883 errorMessage << "TemporaryDataWorkspace does not have the ";
884 errorMessage << "correct name for dimension " << parametersIndex;
885 debugMessage << "QDimension0 Names: Output will be: " << QDimensionNameQSample(0);
886 debugMessage << " TemporaryDataWorkspace: " << dimXName;
887 g_log.warning(debugMessage.str());
888 throw(std::invalid_argument(errorMessage.str()));
889 }
890 }
891 } else if (value.find("QDimension1") != std::string::npos) {
892 dimensionIndex[1] = parametersIndex;
893 const std::string dimYName = tempDataWS->getDimension(parametersIndex)->getName();
894 if (m_isRLU) { // hkl
895 if (dimYName != QDimensionName(m_Q1Basis)) {
896 std::stringstream errorMessage;
897 std::stringstream debugMessage;
898 errorMessage << "TemporaryDataWorkspace does not have the ";
899 errorMessage << "correct name for dimension " << parametersIndex;
900 debugMessage << "QDimension1 Names: Output will be: " << QDimensionName(m_Q1Basis);
901 debugMessage << " TemporaryDataWorkspace: " << dimYName;
902 g_log.warning(debugMessage.str());
903 throw(std::invalid_argument(errorMessage.str()));
904 }
905 } else {
906 if (dimYName != QDimensionNameQSample(1)) {
907 std::stringstream errorMessage;
908 std::stringstream debugMessage;
909 errorMessage << "TemporaryDataWorkspace does not have the ";
910 errorMessage << "correct name for dimension " << parametersIndex;
911 debugMessage << "QDimension1 Names: Output will be: " << QDimensionNameQSample(1);
912 debugMessage << " TemporaryDataWorkspace: " << dimYName;
913 g_log.warning(debugMessage.str());
914 throw(std::invalid_argument(errorMessage.str()));
915 }
916 }
917 } else if (value.find("QDimension2") != std::string::npos) {
918 dimensionIndex[2] = parametersIndex;
919 const std::string dimZName = tempDataWS->getDimension(parametersIndex)->getName();
920 if (m_isRLU) { // hkl
921 if (dimZName != QDimensionName(m_Q2Basis)) {
922 std::stringstream errorMessage;
923 std::stringstream debugMessage;
924 errorMessage << "TemporaryDataWorkspace does not have the ";
925 errorMessage << "correct name for dimension " << parametersIndex;
926 debugMessage << "QDimension2 Names: Output will be: " << QDimensionName(m_Q2Basis);
927 debugMessage << " TemporaryDataWorkspace: " << dimZName;
928 g_log.warning(debugMessage.str());
929 throw(std::invalid_argument(errorMessage.str()));
930 }
931 } else {
932 if (dimZName != QDimensionNameQSample(2)) {
933 std::stringstream errorMessage;
934 std::stringstream debugMessage;
935 errorMessage << "TemporaryDataWorkspace does not have the ";
936 errorMessage << "correct name for dimension " << parametersIndex;
937 debugMessage << "QDimension2 Names: Output will be: " << QDimensionNameQSample(2);
938 debugMessage << " TemporaryDataWorkspace: " << dimZName;
939 g_log.warning(debugMessage.str());
940 throw(std::invalid_argument(errorMessage.str()));
941 }
942 }
943
944 } else if ((key != "OutputBins") && (key != "OutputExtents")) {
945 // make sure the names of non-directional dimensions are the same
946 const std::string nameData = tempDataWS->getDimension(parametersIndex)->getName();
947 if (value.find(nameData) != 0) {
948 g_log.error() << "Dimension " << nameData
949 << " from the temporary workspace"
950 " is not one of the binning dimensions, "
951 " or dimensions are in the wrong order."
952 << std::endl;
953 throw(std::invalid_argument("Beside the Q dimensions, "
954 "TemporaryDataWorkspace does not have the "
955 "same dimension names as OutputWorkspace."));
956 }
957 }
958 parametersIndex++;
959 }
960 const auto it = std::find_if(dimensionIndex.cbegin(), dimensionIndex.cend(),
961 [numDimsTemp](const auto &idx) { return idx > numDimsTemp; });
962 if (it != dimensionIndex.cend())
963 throw(std::invalid_argument("Cannot find at least one of QDimension0, "
964 "QDimension1, or QDimension2"));
965}
966
973 // calculate dimensions for binning
974 DblMatrix soMatrix(3, 3);
975 auto v = so.transformHKL(V3D(1, 0, 0));
976 soMatrix.setColumn(0, v);
977 v = so.transformHKL(V3D(0, 1, 0));
978 soMatrix.setColumn(1, v);
979 v = so.transformHKL(V3D(0, 0, 1));
980 soMatrix.setColumn(2, v);
981
982 return soMatrix;
983}
984
985// projection: input/output
986// requiring: m_hIdx, m_kIndex, m_lIdx, meidx, m_dEintegrated, m_Q0Basis,
987// mQ1Basis,
997inline void MDNorm::determineBasisVector(const size_t &qindex, const std::string &value,
998 const Mantid::Kernel::DblMatrix &Qtransform, std::vector<double> &projection,
999 std::stringstream &basisVector, std::vector<size_t> &qDimensionIndices) {
1000 if (value.find("QDimension0") != std::string::npos) {
1001 m_hIdx = qindex;
1002 if (!m_isRLU) {
1003 projection[0] = 1.;
1004 basisVector << QDimensionNameQSample(0) << ",A^{-1}";
1005 } else {
1006 qDimensionIndices.emplace_back(qindex);
1007 projection[0] = Qtransform[0][0];
1008 projection[1] = Qtransform[1][0];
1009 projection[2] = Qtransform[2][0];
1010 basisVector << QDimensionName(m_Q0Basis) << ", r.l.u.";
1011 }
1012 } else if (value.find("QDimension1") != std::string::npos) {
1013 m_kIdx = qindex;
1014 if (!m_isRLU) {
1015 projection[1] = 1.;
1016 basisVector << QDimensionNameQSample(1) << ",A^{-1}";
1017 } else {
1018 qDimensionIndices.emplace_back(qindex);
1019 projection[0] = Qtransform[0][1];
1020 projection[1] = Qtransform[1][1];
1021 projection[2] = Qtransform[2][1];
1022 basisVector << QDimensionName(m_Q1Basis) << ", r.l.u.";
1023 }
1024 } else if (value.find("QDimension2") != std::string::npos) {
1025 m_lIdx = qindex;
1026 if (!m_isRLU) {
1027 projection[2] = 1.;
1028 basisVector << QDimensionNameQSample(2) << ",A^{-1}";
1029 } else {
1030 qDimensionIndices.emplace_back(qindex);
1031 projection[0] = Qtransform[0][2];
1032 projection[1] = Qtransform[1][2];
1033 projection[2] = Qtransform[2][2];
1034 basisVector << QDimensionName(m_Q2Basis) << ", r.l.u.";
1035 }
1036 } else if (value.find("DeltaE") != std::string::npos) {
1037 m_eIdx = qindex;
1038 m_dEIntegrated = false;
1039 }
1040}
1041
1047inline void MDNorm::setQUnit(const std::vector<size_t> &qDimensionIndices,
1048 const Mantid::DataObjects::MDHistoWorkspace_sptr &outputMDHWS) {
1050 auto mdFrameFactory = Mantid::Geometry::makeMDFrameFactoryChain();
1051 Mantid::Geometry::MDFrame_uptr hklFrame = mdFrameFactory->create(argument);
1052 for (size_t i : qDimensionIndices) {
1053 auto mdHistoDimension = std::const_pointer_cast<Mantid::Geometry::MDHistoDimension>(
1054 std::dynamic_pointer_cast<const Mantid::Geometry::MDHistoDimension>(outputMDHWS->getDimension(i)));
1055 mdHistoDimension->setMDFrame(*hklFrame);
1056 }
1057 // add W_matrix
1058 auto ei = outputMDHWS->getExperimentInfo(0);
1059 ei->mutableRun().addProperty("W_MATRIX", m_W.getVector(), true);
1060}
1061
1068MDNorm::binBackgroundWS(const std::vector<Geometry::SymmetryOperation> &symmetryOps) {
1069 // Create output background data histogram MD workspace
1070 // Either from TemporaryBackgroundDataWorkspace
1071 // Or from scratch
1072 Mantid::API::IMDHistoWorkspace_sptr tempBkgdDataWS = this->getProperty("TemporaryBackgroundDataWorkspace");
1074
1075 // check that our input matches the temporary workspaces
1076 std::map<std::string, std::string> parameters = getBinParameters();
1077 if (tempBkgdDataWS) {
1078 validateBinningForTemporaryDataWorkspace(parameters, tempBkgdDataWS);
1079 }
1080
1081 // For each symmetry operation, do binning MD once
1082 std::vector<size_t> qDimensionIndices;
1083 uint16_t numexpinfo = static_cast<uint16_t>(m_inputWS->getNumExperimentInfo());
1084 if (m_numSymmOps != symmetryOps.size())
1085 throw std::runtime_error("Symmetry operation number m_umSymops is wrong!");
1086
1087 for (uint16_t i_expinfo = 0; i_expinfo < numexpinfo; ++i_expinfo) {
1088
1089 auto rotMatrix = m_inputWS->getExperimentInfo(i_expinfo)->run().getGoniometerMatrix();
1090
1091 // Reset symmetry operation index
1092 double soIndex = 0;
1093
1094 for (const auto &so : symmetryOps) {
1095 // Q transformation matrix: From Q_lab to HKL or Q_sample
1096 // Building symmetric operation matrix
1097 DblMatrix soMatrix = buildSymmetryMatrix(so);
1098 // Calculate Q transform matrix
1099 DblMatrix Qtransform;
1100 if (m_isRLU) {
1101 Qtransform = rotMatrix * m_UB * soMatrix * m_W;
1102 } else {
1103 Qtransform = rotMatrix * soMatrix * m_W;
1104 }
1105
1106 // Set up BinMD for this symmetry opeation
1107 double progress_fraction = 1. / static_cast<double>(symmetryOps.size() * numexpinfo);
1108 auto binMD =
1109 createChildAlgorithm("BinMD", soIndex * 0.3 * progress_fraction, (soIndex + 1) * 0.3 * progress_fraction);
1110
1111 binMD->setPropertyValue("AxisAligned", "0");
1112 binMD->setProperty("InputWorkspace", m_backgroundWS);
1113 binMD->setProperty("TemporaryDataWorkspace", tempBkgdDataWS);
1114 binMD->setPropertyValue("NormalizeBasisVectors", "0");
1115 // Set the output Workspace directly to Algorithm's
1116 // OutputBackgroundDataWorkspace
1117 binMD->setPropertyValue("OutputWorkspace", getPropertyValue("OutputBackgroundDataWorkspace"));
1118 // set binning properties
1119 size_t qindex = 0;
1120 for (const auto &p : parameters) {
1121 auto key = p.first;
1122 auto value = p.second;
1123 std::stringstream basisVector;
1124 std::vector<double> projection(m_inputWS->getNumDims(), 0.);
1125 // value is a string that can start with QDimension0, etc, but contain
1126 // other stuff. Do not use ==
1127 determineBasisVector(qindex, value, Qtransform, projection, basisVector, qDimensionIndices);
1128
1129 if (!basisVector.str().empty()) {
1130 // reconstruct from calculated basis vector
1131 for (auto proji : projection) {
1132 proji = std::abs(proji) > 1e-10 ? proji : 0.0;
1133 basisVector << "," << proji;
1134 }
1135 value = basisVector.str();
1136 }
1137
1138 binMD->setPropertyValue(key, value);
1139 qindex++;
1140 }
1141 // execute algorithm
1142 binMD->executeAsChildAlg();
1143
1144 // set the temporary workspace to be the output workspace, so it keeps
1145 // adding different symmetries AND
1146 // FIXME in future another ExpInfo
1147 outputWS = binMD->getProperty("OutputWorkspace");
1148 tempBkgdDataWS = std::dynamic_pointer_cast<MDHistoWorkspace>(outputWS);
1149 tempBkgdDataWS->clearOriginalWorkspaces();
1150 tempBkgdDataWS->clearTransforms();
1151 }
1152 }
1153 auto outputMDHWS = std::dynamic_pointer_cast<MDHistoWorkspace>(outputWS);
1154 // set MDUnits for Q dimensions
1155 if (m_isRLU) {
1156 setQUnit(qDimensionIndices, outputMDHWS);
1157 }
1158
1159 outputMDHWS->setDisplayNormalization(Mantid::API::NoNormalization);
1160 return outputMDHWS;
1161}
1162
1168DataObjects::MDHistoWorkspace_sptr MDNorm::binInputWS(const std::vector<Geometry::SymmetryOperation> &symmetryOps) {
1169 Mantid::API::IMDHistoWorkspace_sptr tempDataWS = this->getProperty("TemporaryDataWorkspace");
1171 std::map<std::string, std::string> parameters = getBinParameters();
1172
1173 // check that our input matches the temporary workspaces
1174 if (tempDataWS)
1175 validateBinningForTemporaryDataWorkspace(parameters, tempDataWS);
1176
1177 double soIndex = 0;
1178 std::vector<size_t> qDimensionIndices;
1179 for (const auto &so : symmetryOps) {
1180 // calculate dimensions for binning
1181 DblMatrix soMatrix = buildSymmetryMatrix(so);
1182 DblMatrix Qtransform;
1183
1184 if (m_isRLU) {
1185 Qtransform = m_UB * soMatrix * m_W;
1186 } else {
1187 Qtransform = soMatrix * m_W;
1188 }
1189
1190 // bin the data
1191 double fraction = 1. / static_cast<double>(symmetryOps.size());
1192 auto binMD = createChildAlgorithm("BinMD", soIndex * 0.3 * fraction, (soIndex + 1) * 0.3 * fraction);
1193 binMD->setPropertyValue("AxisAligned", "0");
1194 binMD->setProperty("InputWorkspace", m_inputWS);
1195 binMD->setProperty("TemporaryDataWorkspace", tempDataWS);
1196 binMD->setPropertyValue("NormalizeBasisVectors", "0");
1197 binMD->setPropertyValue("OutputWorkspace", getPropertyValue("OutputDataWorkspace"));
1198 // set binning properties
1199 size_t qindex = 0;
1200 for (const auto &p : parameters) {
1201 auto key = p.first;
1202 auto value = p.second;
1203 std::stringstream basisVector;
1204 std::vector<double> projection(m_inputWS->getNumDims(), 0.);
1205 // value is a string that can start with QDimension0, etc, but contain
1206 // other stuff. Do not use ==
1207 determineBasisVector(qindex, value, Qtransform, projection, basisVector, qDimensionIndices);
1208
1209 if (!basisVector.str().empty()) {
1210 // reconstruct from calculated basis vector
1211 for (auto proji : projection) {
1212 proji = std::abs(proji) > 1e-10 ? proji : 0.0;
1213 basisVector << "," << proji;
1214 }
1215 value = basisVector.str();
1216 }
1217
1218 binMD->setPropertyValue(key, value);
1219 qindex++;
1220 }
1221 // execute algorithm
1222 binMD->executeAsChildAlg();
1223 outputWS = binMD->getProperty("OutputWorkspace");
1224
1225 // set the temporary workspace to be the output workspace, so it keeps
1226 // adding different symmetries
1227 tempDataWS = std::dynamic_pointer_cast<MDHistoWorkspace>(outputWS);
1228 tempDataWS->clearOriginalWorkspaces();
1229 tempDataWS->clearTransforms();
1230 soIndex += 1;
1231 }
1232
1233 auto outputMDHWS = std::dynamic_pointer_cast<MDHistoWorkspace>(outputWS);
1234 // set MDUnits for Q dimensions
1235 if (m_isRLU) {
1236 setQUnit(qDimensionIndices, outputMDHWS);
1237 }
1238
1239 outputMDHWS->setDisplayNormalization(Mantid::API::NoNormalization);
1240 return outputMDHWS;
1241}
1242
1251std::vector<coord_t> MDNorm::getValuesFromOtherDimensions(bool &skipNormalization, uint16_t expInfoIndex) const {
1252 const auto &currentRun = m_inputWS->getExperimentInfo(expInfoIndex)->run();
1253
1254 std::vector<coord_t> otherDimValues;
1255 for (size_t i = 3; i < m_inputWS->getNumDims(); i++) {
1256 const auto dimension = m_inputWS->getDimension(i);
1257 auto inputDimMin = static_cast<float>(dimension->getMinimum());
1258 auto inputDimMax = static_cast<float>(dimension->getMaximum());
1259 coord_t outputDimMin(0), outputDimMax(0);
1260 bool isIntegrated = true;
1261
1262 for (size_t j = 0; j < m_transformation.numRows(); j++) {
1263 if (m_transformation[j][i] == 1) {
1264 isIntegrated = false;
1265 outputDimMin = m_normWS->getDimension(j)->getMinimum();
1266 outputDimMax = m_normWS->getDimension(j)->getMaximum();
1267 }
1268 }
1269 if (dimension->getName() == "DeltaE") {
1270 if ((inputDimMax < outputDimMin) || (inputDimMin > outputDimMax)) {
1271 skipNormalization = true;
1272 }
1273 } else {
1274 coord_t value = static_cast<coord_t>(
1275 currentRun.getLogAsSingleValue(dimension->getName(), Mantid::Kernel::Math::TimeAveragedMean));
1276 otherDimValues.emplace_back(value);
1277 if (value < inputDimMin || value > inputDimMax) {
1278 skipNormalization = true;
1279 }
1280 if ((!isIntegrated) && (value < outputDimMin || value > outputDimMax)) {
1281 skipNormalization = true;
1282 }
1283 }
1284 }
1285 return otherDimValues;
1286}
1287
1293 auto &hDim = *m_normWS->getDimension(m_hIdx);
1294 m_hX.resize(hDim.getNBoundaries());
1295 for (size_t i = 0; i < m_hX.size(); ++i) {
1296 m_hX[i] = hDim.getX(i);
1297 }
1298 auto &kDim = *m_normWS->getDimension(m_kIdx);
1299 m_kX.resize(kDim.getNBoundaries());
1300 for (size_t i = 0; i < m_kX.size(); ++i) {
1301 m_kX[i] = kDim.getX(i);
1302 }
1303
1304 auto &lDim = *m_normWS->getDimension(m_lIdx);
1305 m_lX.resize(lDim.getNBoundaries());
1306 for (size_t i = 0; i < m_lX.size(); ++i) {
1307 m_lX[i] = lDim.getX(i);
1308 }
1309
1310 if ((!m_diffraction) && (!m_dEIntegrated)) {
1311 // NOTE: store k final instead
1312 auto &eDim = *m_normWS->getDimension(m_eIdx);
1313 m_eX.resize(eDim.getNBoundaries());
1314 for (size_t i = 0; i < m_eX.size(); ++i) {
1315 double temp = m_Ei - eDim.getX(i);
1316 temp = std::max(temp, 0.);
1317 m_eX[i] = std::sqrt(energyToK * temp);
1318 }
1319 }
1320}
1321
1329 const Geometry::SymmetryOperation &so) {
1330 // Make it to a method!
1331 DblMatrix R = currentExpInfo.run().getGoniometerMatrix();
1332 DblMatrix soMatrix(3, 3);
1333 auto v = so.transformHKL(V3D(1, 0, 0));
1334 soMatrix.setColumn(0, v);
1335 v = so.transformHKL(V3D(0, 1, 0));
1336 soMatrix.setColumn(1, v);
1337 v = so.transformHKL(V3D(0, 0, 1));
1338 soMatrix.setColumn(2, v);
1339 soMatrix.Invert();
1340 DblMatrix Qtransform = R * m_UB * soMatrix * m_W;
1341 Qtransform.Invert();
1342
1343 return Qtransform;
1344}
1345
1355inline void MDNorm::calcDiffractionIntersectionIntegral(std::vector<std::array<double, 4>> &intersections,
1356 std::vector<double> &xValues, std::vector<double> &yValues,
1357 const API::MatrixWorkspace &integrFlux, const size_t &wsIdx) {
1358 // -- calculate integrals for the intersection --
1359 // momentum values at intersections
1360 auto intersectionsBegin = intersections.begin();
1361 // copy momenta to xValues
1362 xValues.resize(intersections.size());
1363 yValues.resize(intersections.size());
1364 auto x = xValues.begin();
1365 for (auto it = intersectionsBegin; it != intersections.end(); ++it, ++x) {
1366 *x = (*it)[3];
1367 }
1368 // calculate integrals at momenta from xValues by interpolating between
1369 // points in spectrum sp
1370 // of workspace integrFlux. The result is stored in yValues
1371 calcIntegralsForIntersections(xValues, integrFlux, wsIdx, yValues);
1372}
1373
1387inline void MDNorm::calcSingleDetectorNorm(const std::vector<std::array<double, 4>> &intersections, const double &solid,
1388 std::vector<double> &yValues, const size_t &vmdDims,
1389 std::vector<coord_t> &pos, std::vector<coord_t> &posNew,
1390 std::vector<std::atomic<signal_t>> &signalArray, const double &solidBkgd,
1391 std::vector<std::atomic<signal_t>> &bkgdSignalArray) {
1392
1393 auto intersectionsBegin = intersections.begin();
1394 for (auto it = intersectionsBegin + 1; it != intersections.end(); ++it) {
1395
1396 const auto &curIntSec = *it;
1397 const auto &prevIntSec = *(it - 1);
1398
1399 // The full vector isn't used so compute only what is necessary
1400 // If the difference between 2 adjacent intersection is trivial, no
1401 // intersection normalization is to be calculated
1402 double delta, eps;
1403 if (m_diffraction) {
1404 // diffraction
1405 delta = curIntSec[3] - prevIntSec[3];
1406 eps = 1e-7;
1407 } else {
1408 // inelastic
1409 delta = (curIntSec[3] * curIntSec[3] - prevIntSec[3] * prevIntSec[3]) / energyToK;
1410 eps = 1e-10;
1411 }
1412 if (delta < eps)
1413 continue; // Assume zero contribution if difference is small
1414
1415 // Average between two intersections for final position
1416 // [Task 89] Sample and background have same 'pos[]'
1417 std::transform(curIntSec.data(), curIntSec.data() + vmdDims, prevIntSec.data(), pos.begin(),
1418 [](const double rhs, const double lhs) { return static_cast<coord_t>(0.5 * (rhs + lhs)); });
1419 signal_t signal;
1420 signal_t bkgdSignal(0.);
1421 if (m_diffraction) {
1422 // Diffraction
1423 // index of the current intersection
1424 auto k = static_cast<size_t>(std::distance(intersectionsBegin, it));
1425 // signal = integral between two consecutive intersections
1426 signal = (yValues[k] - yValues[k - 1]) * solid;
1427 if (m_backgroundWS)
1428 bkgdSignal = (yValues[k] - yValues[k - 1]) * solidBkgd;
1429
1430 } else {
1431 // Inelastic
1432 // transform kf to energy transfer
1433 pos[3] = static_cast<coord_t>(m_Ei - pos[3] * pos[3] / energyToK);
1434 // signal = energy distance between two consecutive intersections *solid
1435 // angle *PC
1436 signal = solid * delta;
1437 if (m_backgroundWS)
1438 bkgdSignal = solidBkgd * delta;
1439 }
1440
1441 // Find the coordiate of the new position after transformation
1442 m_transformation.multiplyPoint(pos, posNew);
1443 // [Task 89] Is linIndex common to both sample and background?
1444 size_t linIndex = m_normWS->getLinearIndexAtCoord(posNew.data());
1445 if (linIndex == size_t(-1))
1446 continue; // not found
1447
1448 // Set to output
1449 // set the calculated signal to
1450 Mantid::Kernel::AtomicOp(signalArray[linIndex], signal, std::plus<signal_t>());
1451 // [Task 89]
1452 if (m_backgroundWS)
1453 Mantid::Kernel::AtomicOp(bkgdSignalArray[linIndex], bkgdSignal, std::plus<signal_t>());
1454 }
1455 return;
1456}
1457
1466void MDNorm::calculateNormalization(const std::vector<coord_t> &otherValues, const Geometry::SymmetryOperation &so,
1467 uint16_t expInfoIndex, size_t soIndex) {
1468 const auto &currentExptInfo = *(m_inputWS->getExperimentInfo(expInfoIndex));
1469 std::vector<double> lowValues, highValues;
1470 auto *lowValuesLog = dynamic_cast<VectorDoubleProperty *>(currentExptInfo.getLog("MDNorm_low"));
1471 lowValues = (*lowValuesLog)();
1472 auto *highValuesLog = dynamic_cast<VectorDoubleProperty *>(currentExptInfo.getLog("MDNorm_high"));
1473 highValues = (*highValuesLog)();
1474
1475 // calculate Q transformation matrix (R * UB * SymmetryOperation * m_W)^-1
1476 // in order to calculate intersections
1477 DblMatrix Qtransform = calQTransform(currentExptInfo, so);
1478
1479 // get proton charges
1480 const double protonCharge = currentExptInfo.run().getProtonCharge();
1481 // [Task 89]
1482 const double protonChargeBkgd =
1483 (m_backgroundWS != nullptr) ? m_backgroundWS->getExperimentInfo(0)->run().getProtonCharge() : 0;
1484
1485 const auto &spectrumInfo = currentExptInfo.spectrumInfo();
1486
1487 // Mappings: solid angle and flux workspaces' detector to ws_index map
1488 const auto ndets = static_cast<int64_t>(spectrumInfo.size());
1489 bool haveSA = false;
1490 API::MatrixWorkspace_const_sptr solidAngleWS = getProperty("SolidAngleWorkspace");
1491 if (solidAngleWS != nullptr) {
1492 haveSA = true;
1493 }
1494 API::MatrixWorkspace_const_sptr integrFlux = getProperty("FluxWorkspace");
1495 const detid2index_map solidAngDetToIdx =
1496 (haveSA) ? solidAngleWS->getDetectorIDToWorkspaceIndexMap() : detid2index_map();
1497 const detid2index_map fluxDetToIdx =
1498 (m_diffraction) ? integrFlux->getDetectorIDToWorkspaceIndexMap() : detid2index_map();
1499
1500 // Define dimension, signal array
1501 const size_t vmdDims = (m_diffraction) ? 3 : 4;
1502 std::vector<std::atomic<signal_t>> signalArray(m_normWS->getNPoints());
1503
1504 size_t numNPoints = (m_backgroundWS) ? m_bkgdNormWS->getNPoints() : 0;
1505 if (m_backgroundWS && numNPoints != m_normWS->getNPoints()) {
1506 throw std::runtime_error("N points are different");
1507 }
1508 std::vector<std::atomic<signal_t>> bkgdSignalArray(numNPoints);
1509
1510 std::vector<std::array<double, 4>> intersections;
1511 std::vector<double> xValues, yValues;
1512 std::vector<coord_t> pos, posNew;
1513
1514 // Progress report
1515 double progStep = 0.7 / static_cast<double>(m_numExptInfos * m_numSymmOps);
1516 auto progIndex = static_cast<double>(soIndex + expInfoIndex * m_numSymmOps);
1517 auto prog =
1518 std::make_unique<API::Progress>(this, 0.3 + progStep * progIndex, 0.3 + progStep * (1. + progIndex), ndets);
1519 // muliple threading
1520 bool safe = m_diffraction ? Kernel::threadSafe(*integrFlux) : true;
1521
1522PRAGMA_OMP(parallel for private(intersections, xValues, yValues, pos, posNew) if (safe))
1523for (int64_t i = 0; i < ndets; i++) {
1525
1526 // Skip: non-existing detector, monitor and masked detector
1527 if (!spectrumInfo.hasDetectors(i) || spectrumInfo.isMonitor(i) || spectrumInfo.isMasked(i)) {
1528 continue;
1529 }
1530
1531 const auto &detector = spectrumInfo.detector(i);
1532 double theta = detector.getTwoTheta(m_samplePos, m_beamDir);
1533 double phi = detector.getPhi();
1534 // If the dtefctor is a group, this should be the ID of the first detector
1535 const auto detID = detector.getID();
1536
1537 // get the flux spectrum number: this is for diffraction only!
1538 size_t wsIdx = 0;
1539 if (m_diffraction) {
1540 auto index = fluxDetToIdx.find(detID);
1541 if (index != fluxDetToIdx.end()) {
1542 wsIdx = index->second;
1543 } else { // masked detector in flux, but not in input workspace
1544 continue;
1545 }
1546 }
1547
1548 // Intersections for sample and background if present
1549 this->calculateIntersections(intersections, theta, phi, Qtransform, lowValues[i], highValues[i]);
1550
1551 // No need to do normalization calculation if there is no intersection
1552 if (intersections.empty())
1553 continue;
1554
1555 // Get solid angle for this contribution
1556 double solid = protonCharge;
1557 // [Task 89]
1558 double bkgdSolid = protonChargeBkgd;
1559 if (haveSA) {
1560 double solid_angle_factor = solidAngleWS->y(solidAngDetToIdx.find(detID)->second)[0];
1561 // solidAngleWS->y(solidAngDetToIdx.find(detID)->second)[0]
1562 solid = solid_angle_factor * protonCharge;
1563 // [Task 89]
1564 bkgdSolid = solid_angle_factor * protonChargeBkgd;
1565 }
1566
1567 if (m_diffraction) {
1568 // -- calculate integrals for the intersection --
1569 calcDiffractionIntersectionIntegral(intersections, xValues, yValues, *integrFlux, wsIdx);
1570 }
1571
1572 // Compute final position in HKL
1573 // pre-allocate for efficiency and copy non-hkl dim values into place
1574 pos.resize(vmdDims + otherValues.size());
1575 std::copy(otherValues.begin(), otherValues.end(), pos.begin() + vmdDims);
1576
1577 calcSingleDetectorNorm(intersections, solid, yValues, vmdDims, pos, posNew, signalArray, bkgdSolid,
1578 bkgdSignalArray); // [Task 89] ADD solidBkgd, bkgdYValues, bkgdSignalArray
1579
1580 prog->report();
1581
1583}
1585if (m_accumulate) {
1586 std::transform(signalArray.cbegin(), signalArray.cend(), m_normWS->getSignalArray(), m_normWS->mutableSignalArray(),
1587 [](const std::atomic<signal_t> &a, const signal_t &b) { return a + b; });
1588 // [Task 89] Process background
1589 if (m_backgroundWS)
1590 std::transform(bkgdSignalArray.cbegin(), bkgdSignalArray.cend(), m_bkgdNormWS->getSignalArray(),
1591 m_bkgdNormWS->mutableSignalArray(),
1592 [](const std::atomic<signal_t> &a, const signal_t &b) { return a + b; });
1593
1594} else {
1595 // First time, init
1596 std::copy(signalArray.cbegin(), signalArray.cend(), m_normWS->mutableSignalArray());
1597 // [Task 89]
1598 if (m_backgroundWS)
1599 std::copy(bkgdSignalArray.cbegin(), bkgdSignalArray.cend(), m_bkgdNormWS->mutableSignalArray());
1600}
1601m_accumulate = true;
1602}
1603
1614void MDNorm::calculateIntersections(std::vector<std::array<double, 4>> &intersections, const double theta,
1615 const double phi, const Kernel::DblMatrix &transform, double lowvalue,
1616 double highvalue) {
1617 V3D qout(sin(theta) * cos(phi), sin(theta) * sin(phi), cos(theta)), qin(0., 0., 1);
1618
1619 qout = transform * qout;
1620 qin = transform * qin;
1621 if (convention == "Crystallography") {
1622 qout *= -1;
1623 qin *= -1;
1624 }
1625 double kfmin, kfmax, kimin, kimax;
1626 if (m_diffraction) {
1627 kimin = lowvalue;
1628 kimax = highvalue;
1629 kfmin = kimin;
1630 kfmax = kimax;
1631 } else {
1632 kimin = std::sqrt(energyToK * m_Ei);
1633 kimax = kimin;
1634 kfmin = std::sqrt(energyToK * (m_Ei - highvalue));
1635 kfmax = std::sqrt(energyToK * (m_Ei - lowvalue));
1636 }
1637
1638 double hStart = qin.X() * kimin - qout.X() * kfmin, hEnd = qin.X() * kimax - qout.X() * kfmax;
1639 double kStart = qin.Y() * kimin - qout.Y() * kfmin, kEnd = qin.Y() * kimax - qout.Y() * kfmax;
1640 double lStart = qin.Z() * kimin - qout.Z() * kfmin, lEnd = qin.Z() * kimax - qout.Z() * kfmax;
1641
1642 double eps = 1e-10;
1643 auto hNBins = m_hX.size();
1644 auto kNBins = m_kX.size();
1645 auto lNBins = m_lX.size();
1646 auto eNBins = m_eX.size();
1647 intersections.clear();
1648 intersections.reserve(hNBins + kNBins + lNBins + eNBins + 2);
1649
1650 // calculate intersections with planes perpendicular to h
1651 if (fabs(hStart - hEnd) > eps) {
1652 double fmom = (kfmax - kfmin) / (hEnd - hStart);
1653 double fk = (kEnd - kStart) / (hEnd - hStart);
1654 double fl = (lEnd - lStart) / (hEnd - hStart);
1655 for (size_t i = 0; i < hNBins; i++) {
1656 double hi = m_hX[i];
1657 if (((hStart - hi) * (hEnd - hi) < 0)) {
1658 // if hi is between hStart and hEnd, then ki and li will be between
1659 // kStart, kEnd and lStart, lEnd and momi will be between kfmin and
1660 // kfmax
1661 double ki = fk * (hi - hStart) + kStart;
1662 double li = fl * (hi - hStart) + lStart;
1663 if ((ki >= m_kX[0]) && (ki <= m_kX[kNBins - 1]) && (li >= m_lX[0]) && (li <= m_lX[lNBins - 1])) {
1664 double momi = fmom * (hi - hStart) + kfmin;
1665 intersections.push_back({{hi, ki, li, momi}});
1666 }
1667 }
1668 }
1669 }
1670 // calculate intersections with planes perpendicular to k
1671 if (fabs(kStart - kEnd) > eps) {
1672 double fmom = (kfmax - kfmin) / (kEnd - kStart);
1673 double fh = (hEnd - hStart) / (kEnd - kStart);
1674 double fl = (lEnd - lStart) / (kEnd - kStart);
1675 for (size_t i = 0; i < kNBins; i++) {
1676 double ki = m_kX[i];
1677 if (((kStart - ki) * (kEnd - ki) < 0)) {
1678 // if ki is between kStart and kEnd, then hi and li will be between
1679 // hStart, hEnd and lStart, lEnd and momi will be between kfmin and
1680 // kfmax
1681 double hi = fh * (ki - kStart) + hStart;
1682 double li = fl * (ki - kStart) + lStart;
1683 if ((hi >= m_hX[0]) && (hi <= m_hX[hNBins - 1]) && (li >= m_lX[0]) && (li <= m_lX[lNBins - 1])) {
1684 double momi = fmom * (ki - kStart) + kfmin;
1685 intersections.push_back({{hi, ki, li, momi}});
1686 }
1687 }
1688 }
1689 }
1690
1691 // calculate intersections with planes perpendicular to l
1692 if (fabs(lStart - lEnd) > eps) {
1693 double fmom = (kfmax - kfmin) / (lEnd - lStart);
1694 double fh = (hEnd - hStart) / (lEnd - lStart);
1695 double fk = (kEnd - kStart) / (lEnd - lStart);
1696
1697 for (size_t i = 0; i < lNBins; i++) {
1698 double li = m_lX[i];
1699 if (((lStart - li) * (lEnd - li) < 0)) {
1700 double hi = fh * (li - lStart) + hStart;
1701 double ki = fk * (li - lStart) + kStart;
1702 if ((hi >= m_hX[0]) && (hi <= m_hX[hNBins - 1]) && (ki >= m_kX[0]) && (ki <= m_kX[kNBins - 1])) {
1703 double momi = fmom * (li - lStart) + kfmin;
1704 intersections.push_back({{hi, ki, li, momi}});
1705 }
1706 }
1707 }
1708 }
1709 // intersections with dE
1710 if (!m_dEIntegrated) {
1711 for (size_t i = 0; i < eNBins; i++) {
1712 double kfi = m_eX[i];
1713 if ((kfi - kfmin) * (kfi - kfmax) <= 0) {
1714 double h = qin.X() * kimin - qout.X() * kfi;
1715 double k = qin.Y() * kimin - qout.Y() * kfi;
1716 double l = qin.Z() * kimin - qout.Z() * kfi;
1717 if ((h >= m_hX[0]) && (h <= m_hX[hNBins - 1]) && (k >= m_kX[0]) && (k <= m_kX[kNBins - 1]) && (l >= m_lX[0]) &&
1718 (l <= m_lX[lNBins - 1])) {
1719 intersections.push_back({{h, k, l, kfi}});
1720 }
1721 }
1722 }
1723 }
1724
1725 // endpoints
1726 if ((hStart >= m_hX[0]) && (hStart <= m_hX[hNBins - 1]) && (kStart >= m_kX[0]) && (kStart <= m_kX[kNBins - 1]) &&
1727 (lStart >= m_lX[0]) && (lStart <= m_lX[lNBins - 1])) {
1728 intersections.push_back({{hStart, kStart, lStart, kfmin}});
1729 }
1730 if ((hEnd >= m_hX[0]) && (hEnd <= m_hX[hNBins - 1]) && (kEnd >= m_kX[0]) && (kEnd <= m_kX[kNBins - 1]) &&
1731 (lEnd >= m_lX[0]) && (lEnd <= m_lX[lNBins - 1])) {
1732 intersections.push_back({{hEnd, kEnd, lEnd, kfmax}});
1733 }
1734
1735 // sort intersections by final momentum
1736 std::stable_sort(intersections.begin(), intersections.end(), compareMomentum);
1737}
1738
1747void MDNorm::calcIntegralsForIntersections(const std::vector<double> &xValues, const API::MatrixWorkspace &integrFlux,
1748 size_t sp, std::vector<double> &yValues) {
1749 assert(xValues.size() == yValues.size());
1750
1751 // the x-data from the workspace
1752 const auto &xData = integrFlux.x(sp);
1753 const double xStart = xData.front();
1754 const double xEnd = xData.back();
1755
1756 // the values in integrFlux are expected to be integrals of a non-negative
1757 // function
1758 // ie they must make a non-decreasing function
1759 const auto &yData = integrFlux.y(sp);
1760 size_t spSize = yData.size();
1761
1762 const double yMin = 0.0;
1763 const double yMax = yData.back();
1764
1765 size_t nData = xValues.size();
1766 // all integrals below xStart must be 0
1767 if (xValues[nData - 1] < xStart) {
1768 std::fill(yValues.begin(), yValues.end(), yMin);
1769 return;
1770 }
1771
1772 // all integrals above xEnd must be equal tp yMax
1773 if (xValues[0] > xEnd) {
1774 std::fill(yValues.begin(), yValues.end(), yMax);
1775 return;
1776 }
1777
1778 size_t i = 0;
1779 // integrals below xStart must be 0
1780 while (i < nData - 1 && xValues[i] < xStart) {
1781 yValues[i] = yMin;
1782 i++;
1783 }
1784 size_t j = 0;
1785 for (; i < nData; i++) {
1786 // integrals above xEnd must be equal tp yMax
1787 if (j >= spSize - 1) {
1788 yValues[i] = yMax;
1789 } else {
1790 double xi = xValues[i];
1791 while (j < spSize - 1 && xi > xData[j])
1792 j++;
1793 // if x falls onto an interpolation point return the corresponding y
1794 if (xi == xData[j]) {
1795 yValues[i] = yData[j];
1796 } else if (j == spSize - 1) {
1797 // if we get above xEnd it's yMax
1798 yValues[i] = yMax;
1799 } else if (j > 0) {
1800 // interpolate between the consecutive points
1801 double x0 = xData[j - 1];
1802 double x1 = xData[j];
1803 double y0 = yData[j - 1];
1804 double y1 = yData[j];
1805 yValues[i] = y0 + (y1 - y0) * (xi - x0) / (x1 - x0);
1806 } else // j == 0
1807 {
1808 yValues[i] = yMin;
1809 }
1810 }
1811 }
1812}
1813
1814} // namespace Mantid::MDAlgorithms
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
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
#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...
#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.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
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.
Kernel::Logger & g_log
Definition Algorithm.h:422
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:570
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:26
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:34
static const std::string QSampleName
Definition QSample.h:22
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.
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:145
void error(const std::string &msg)
Logs at error level.
Definition Logger.cpp:108
void warning(const std::string &msg)
Logs at warning level.
Definition Logger.cpp:117
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 const UnitLabel RLU
Reciprocal lattice units.
Class for 3D vectors.
Definition V3D.h:34
constexpr double X() const noexcept
Get x.
Definition V3D.h:238
constexpr double Y() const noexcept
Get y.
Definition V3D.h:239
constexpr double Z() const noexcept
Get z.
Definition V3D.h:240
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:1387
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:1068
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:1747
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:1251
void setQUnit(const std::vector< size_t > &qDimensionIndices, const Mantid::DataObjects::MDHistoWorkspace_sptr &outputMDHWS)
Set the output Frame to HKL.
Definition MDNorm.cpp:1047
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:1614
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:813
Mantid::Kernel::DblMatrix buildSymmetryMatrix(const Geometry::SymmetryOperation &so)
build symmetry matrix
Definition MDNorm.cpp:972
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:1355
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:1292
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:1168
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:1466
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:1328
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:997
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
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.
@ 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:734
template DLLExport std::vector< size_t > splitStringIntoVector< size_t >(std::string listString, const std::string &separator)
template DLLExport std::vector< double > splitStringIntoVector< double >(std::string listString, const std::string &separator)
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.
MANTID_KERNEL_DLL V3D normalize(V3D v)
Normalizes a V3D.
Definition V3D.h:352
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...
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