Mantid
Loading...
Searching...
No Matches
MDNormSCD.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 +
8
14
15namespace Mantid::MDAlgorithms {
16
19using namespace Mantid::DataObjects;
20using namespace Mantid::API;
21using namespace Mantid::Kernel;
22
23// Register the algorithm into the AlgorithmFactory
24DECLARE_ALGORITHM(MDNormSCD)
25
26
27int MDNormSCD::version() const { return 1; }
28
30const std::string MDNormSCD::category() const { return "MDAlgorithms\\Normalisation"; }
31
33const std::string MDNormSCD::summary() const {
34 return "Calculate normalization for an MDEvent workspace for single crystal "
35 "diffraction.";
36}
37
39const std::string MDNormSCD::name() const { return "MDNormSCD"; }
40
45 declareProperty(std::make_unique<WorkspaceProperty<IMDEventWorkspace>>("InputWorkspace", "", Direction::Input),
46 "An input MDWorkspace.");
47
48 std::string dimChars = getDimensionChars();
49 // --------------- Axis-aligned properties
50 // ---------------------------------------
51 for (size_t i = 0; i < dimChars.size(); i++) {
52 std::string dim(" ");
53 dim[0] = dimChars[i];
54 std::string propName = "AlignedDim" + dim;
56 "Binning parameters for the " + Strings::toString(i) +
57 "th dimension.\n"
58 "Enter it as a comma-separated list of values with the format: "
59 "'name,minimum,maximum,number_of_bins'. Leave blank for NONE.");
60 }
61
62 auto fluxValidator = std::make_shared<CompositeValidator>();
63 fluxValidator->add<WorkspaceUnitValidator>("Momentum");
64 fluxValidator->add<InstrumentValidator>();
65 fluxValidator->add<CommonBinsValidator>();
66 auto solidAngleValidator = fluxValidator->clone();
67
68 declareProperty(std::make_unique<WorkspaceProperty<>>("FluxWorkspace", "", Direction::Input, fluxValidator),
69 "An input workspace containing momentum dependent flux.");
71 std::make_unique<WorkspaceProperty<>>("SolidAngleWorkspace", "", Direction::Input, solidAngleValidator),
72 "An input workspace containing momentum integrated vanadium "
73 "(a measure of the solid angle).");
74
75 declareProperty(std::make_unique<PropertyWithValue<bool>>("SkipSafetyCheck", false, Direction::Input),
76 "If set to true, the algorithm does "
77 "not check history if the workspace was modified since the"
78 "ConvertToMD algorithm was run, and assume that the elastic "
79 "mode is used.");
80
81 declareProperty(std::make_unique<WorkspaceProperty<IMDHistoWorkspace>>("TemporaryNormalizationWorkspace", "",
83 "An input MDHistoWorkspace used to accumulate normalization "
84 "from multiple MDEventWorkspaces. "
85 "If unspecified a blank MDHistoWorkspace will be created.");
86
87 declareProperty(std::make_unique<WorkspaceProperty<IMDHistoWorkspace>>("TemporaryDataWorkspace", "", Direction::Input,
89 "An input MDHistoWorkspace used to accumulate data from "
90 "multiple MDEventWorkspaces. If "
91 "unspecified a blank MDHistoWorkspace will be created.");
92
93 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("OutputWorkspace", "", Direction::Output),
94 "A name for the output data MDHistoWorkspace.");
95 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("OutputNormalizationWorkspace", "", Direction::Output),
96 "A name for the output normalization MDHistoWorkspace.");
97}
98
103 cacheInputs();
104 auto outputWS = binInputWS();
105 m_convention = Kernel::ConfigService::Instance().getString("Q.convention");
106 outputWS->setDisplayNormalization(Mantid::API::NoNormalization);
107 setProperty<Workspace_sptr>("OutputWorkspace", outputWS);
108 createNormalizationWS(*outputWS);
109 m_normWS->setDisplayNormalization(Mantid::API::NoNormalization);
110 setProperty("OutputNormalizationWorkspace", m_normWS);
111 m_diffraction = true;
112
113 m_numExptInfos = outputWS->getNumExperimentInfo();
114 m_signalArray = std::vector<std::atomic<signal_t>>(m_normWS->getNPoints());
115 // loop over all experiment infos
116 for (uint16_t expInfoIndex = 0; expInfoIndex < m_numExptInfos; expInfoIndex++) {
117 // Check for other dimensions if we could measure anything in the original
118 // data
119 bool skipNormalization = false;
120 const std::vector<coord_t> otherValues = getValuesFromOtherDimensions(skipNormalization, expInfoIndex);
121 findIntegratedDimensions(otherValues, skipNormalization);
123
124 if (!skipNormalization) {
125 calculateNormalization(otherValues, expInfoIndex);
126 } else {
127 g_log.warning("Binning limits are outside the limits of the MDWorkspace. "
128 "Not applying normalization.");
129 }
130 }
131 if (m_accumulate) {
132 std::transform(m_signalArray.cbegin(), m_signalArray.cend(), m_normWS->getSignalArray(),
133 m_normWS->mutableSignalArray(),
134 [](const std::atomic<signal_t> &a, const signal_t &b) { return a + b; });
135 } else {
136 std::copy(m_signalArray.cbegin(), m_signalArray.cend(), m_normWS->mutableSignalArray());
137 }
138}
139
144 m_inputWS = getProperty("InputWorkspace");
145 bool skipCheck = getProperty("SkipSafetyCheck");
146 if (!skipCheck && inputEnergyMode() != "Elastic") {
147 throw std::invalid_argument("Invalid energy transfer mode. Algorithm "
148 "currently only supports elastic data.");
149 }
150 // Min/max dimension values
151 const auto hdim(m_inputWS->getDimension(0)), kdim(m_inputWS->getDimension(1)), ldim(m_inputWS->getDimension(2));
152 m_hmin = hdim->getMinimum();
153 m_kmin = kdim->getMinimum();
154 m_lmin = ldim->getMinimum();
155 m_hmax = hdim->getMaximum();
156 m_kmax = kdim->getMaximum();
157 m_lmax = ldim->getMaximum();
158
159 const auto &exptInfoZero = *(m_inputWS->getExperimentInfo(0));
160 auto source = exptInfoZero.getInstrument()->getSource();
161 auto sample = exptInfoZero.getInstrument()->getSample();
162 if (source == nullptr || sample == nullptr) {
164 "Instrument not sufficiently defined: failed to get source and/or "
165 "sample");
166 }
167 m_samplePos = sample->getPos();
168 m_beamDir = normalize(m_samplePos - source->getPos());
169}
170
171} // namespace Mantid::MDAlgorithms
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:542
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Kernel::Logger & g_log
Definition Algorithm.h:423
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.
A validator which checks that a workspace has a valid instrument.
A property class for workspaces.
A validator which checks that the unit of the workspace referred to by a WorkspaceProperty is the exp...
Exception for errors associated with the instrument definition.
Definition Exception.h:220
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void warning(const std::string &msg)
Logs at warning level.
Definition Logger.cpp:117
The concrete, templated class for properties.
API::IMDEventWorkspace_sptr m_inputWS
Input workspace.
Definition MDNormBase.h:69
void createNormalizationWS(const DataObjects::MDHistoWorkspace &dataWS)
Create & cached the normalization workspace.
bool m_diffraction
Flag indicating if the input workspace is from diffraction.
Definition MDNormBase.h:100
static std::string getDimensionChars()
Definition MDNormBase.h:36
void cacheDimensionXValues()
Stores the X values from each H,K,L,E dimension as member variables Energy dimension is transformed t...
void findIntegratedDimensions(const std::vector< coord_t > &otherDimValues, bool &skipNormalization)
Checks the normalization workspace against the indices of the original dimensions.
void calculateNormalization(const std::vector< coord_t > &otherValues, uint16_t expInfoIndex)
Computed the normalization for the input workspace (for MDNormSCD/MDNormDirectSC).
DataObjects::MDHistoWorkspace_sptr m_normWS
Normalization workspace.
Definition MDNormBase.h:71
Kernel::V3D m_samplePos
Sample position.
Definition MDNormBase.h:92
std::vector< coord_t > getValuesFromOtherDimensions(bool &skipNormalization, uint16_t expInfoIndex=0) const
Retrieve logged values from non-HKL dimensions.
std::vector< std::atomic< signal_t > > m_signalArray
internal array to accumulate signals to avoid copying (serial) each loop
Definition MDNormBase.h:106
bool m_accumulate
Flag to accumulate normalization.
Definition MDNormBase.h:102
DataObjects::MDHistoWorkspace_sptr binInputWS()
Runs the BinMD algorithm on the input to provide the output workspace All slicing algorithm propertie...
uint16_t m_numExptInfos
number of experiment infos
Definition MDNormBase.h:98
Kernel::V3D m_beamDir
Beam direction.
Definition MDNormBase.h:94
coord_t m_hmin
limits for h,k,l, dE dimensions
Definition MDNormBase.h:76
std::string inputEnergyMode() const
Currently looks for the ConvertToMD algorithm in the history.
std::string m_convention
ki-kf for Inelastic convention; kf-ki for Crystallography convention
Definition MDNormBase.h:96
MDNormSCD : Generate MD normalization for single crystal diffraction.
Definition MDNormSCD.h:15
const std::string category() const override
Algorithm's category for identification.
Definition MDNormSCD.cpp:30
void exec() override
Execute the algorithm.
const std::string name() const override
Algorithm's name for use in the GUI and help.
Definition MDNormSCD.cpp:39
void init() override
Initialize the algorithm's properties.
Definition MDNormSCD.cpp:44
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
Definition MDNormSCD.cpp:33
void cacheInputs()
Set up starting values for cached variables.
@ NoNormalization
Don't normalize = return raw counts.
Definition IMDIterator.h:27
std::string toString(const T &value)
Convert a number to a string.
Definition Strings.cpp:734
MANTID_KERNEL_DLL V3D normalize(V3D v)
Normalizes a V3D.
Definition V3D.h:352
double signal_t
Typedef for the signal recorded in a MDBox, etc.
Definition MDTypes.h:36
Describes the direction (within an algorithm) of a Property.
Definition Property.h:50
@ Input
An input workspace.
Definition Property.h:53
@ Output
An output workspace.
Definition Property.h:54