Mantid
Loading...
Searching...
No Matches
SpectralMomentMD.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2025 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 +
7
12#include <cmath>
13
14namespace Mantid {
15namespace MDAlgorithms {
16using namespace Mantid::Kernel;
17using namespace Mantid::API;
18using namespace Mantid::DataObjects;
19// using Mantid::API::WorkspaceProperty;
20// using Mantid::Kernel::Direction;
21
22// Register the algorithm into the AlgorithmFactory
23DECLARE_ALGORITHM(SpectralMomentMD)
24
25//----------------------------------------------------------------------------------------------
26
27
28const std::string SpectralMomentMD::name() const { return "SpectralMomentMD"; }
29
31int SpectralMomentMD::version() const { return 1; }
32
34const std::string SpectralMomentMD::category() const { return "MDAlgorithms"; }
35
37const std::string SpectralMomentMD::summary() const { return "Multiply MD events by DeltaE^n"; }
38
39//----------------------------------------------------------------------------------------------
44 std::make_unique<WorkspaceProperty<API::IMDEventWorkspace>>("InputWorkspace", "", Kernel::Direction::Input),
45 "An input MDEventWorkspace. Must have an axis as DeltaE");
46
47 declareProperty(std::make_unique<PropertyWithValue<int>>("Moment", 1),
48 "The integer exponent of energy transfer (default: 1)");
50 std::make_unique<WorkspaceProperty<API::IMDEventWorkspace>>("OutputWorkspace", "", Kernel::Direction::Output),
51 "The output MDEventWorkspace with events scaled by DeltaE^Moment");
52}
53
54//---------------------------------------------------------------------------------------------------------
59std::map<std::string, std::string> SpectralMomentMD::validateInputs() {
60 std::map<std::string, std::string> output;
61
62 // Get input workspace
63 API::IMDEventWorkspace_sptr input_ws = getProperty("InputWorkspace");
64
65 // initialize error string
66 std::string error_str("No dimension containing energy transfer was found.");
67
68 // loop over dimensions
69 for (std::size_t dim_num = 0; dim_num < input_ws->getNumDims(); dim_num++) {
70 if (input_ws->getDimension(dim_num)->getName() == "DeltaE") {
71 error_str = "";
72 // cache DeltaE index
73 mDeltaEIndex = dim_num;
74 }
75 }
76
77 if (error_str != "") {
78 output["InputWorkspace"] = error_str;
79 }
80
81 return output;
82}
83
84//----------------------------------------------------------------------------------------------
88 // Get input workspace
89 API::IMDEventWorkspace_sptr input_ws = getProperty("InputWorkspace");
90
91 // Process input workspace and create output workspace
92 std::string output_ws_name = getPropertyValue("OutputWorkspace");
93
94 API::IMDEventWorkspace_sptr output_ws(0);
95 if (input_ws->getName() == output_ws_name) {
96 // Calcualte in-place
97 output_ws = input_ws;
98 } else {
99 // Clone input workace to output workspace
100 output_ws = input_ws->clone();
101 }
102
103 // Cache moment
104 mExponent = getProperty("Moment");
105
106 g_log.debug() << "Moment: " << mExponent << " DeltaE index: " << mDeltaEIndex << "\n";
107
108 // Apply scaling to MDEvents
110
111 // refresh cache for MDBoxes: set correct Box signal
112 output_ws->refreshCache();
113
114 // Clear masking (box flags) from the output workspace
115 output_ws->clearMDMasking();
116
117 // Set output
118 setProperty("OutputWorkspace", output_ws);
119}
120
121//---------------------------------------------------------------------------------------------
125template <typename MDE, size_t nd>
127 // Get Box from MDEventWorkspace
128 MDBoxBase<MDE, nd> *box1 = ws->getBox();
129 std::vector<API::IMDNode *> boxes;
130 box1->getBoxes(boxes, 1000, true);
131 auto numBoxes = int(boxes.size());
132
133 PRAGMA_OMP( parallel for if (!ws->isFileBacked()))
134 for (int i = 0; i < numBoxes; ++i) {
136 auto *box = dynamic_cast<MDBox<MDE, nd> *>(boxes[i]);
137 if (box && !box->getIsMasked()) {
138 // get the MEEvents from box
139 std::vector<MDE> &events = box->getEvents();
140 // Add events, with bounds checking
141 for (auto it = events.begin(); it != events.end(); ++it) {
142 // Create the event
143 // do calculattion
144 float factor = static_cast<float>(std::pow(it->getCenter(mDeltaEIndex), mExponent));
145
146 // calcalate and set intesity
147 auto intensity = it->getSignal() * factor;
148 it->setSignal(intensity);
149
150 // calculate and set error
151 auto error2 = it->getErrorSquared() * factor * factor;
152 // error2 *= factor * factor;
153 it->setErrorSquared(error2);
154 }
155 }
156 if (box) {
157 box->releaseEvents();
158 }
160 }
162
163 return;
164}
165
166} // namespace MDAlgorithms
167} // namespace Mantid
std::string name
Definition Run.cpp:60
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
#define CALL_MDEVENT_FUNCTION(funcname, workspace)
Macro that makes it possible to call a templated method for a MDEventWorkspace using a IMDEventWorksp...
#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.
Kernel::Logger & g_log
Definition Algorithm.h:422
virtual void getBoxes(std::vector< IMDNode * > &boxes, size_t maxDepth, bool leafOnly)=0
Fill a vector with all the boxes who are the childred of this one up to a certain depth.
A property class for workspaces.
Templated super-class of a multi-dimensional event "box".
Definition MDBoxBase.h:50
Templated class for a multi-dimensional event "box".
Definition MDBox.h:45
std::vector< MDE > & getEvents()
Get vector of events to change.
Definition MDBox.hxx:243
std::shared_ptr< MDEventWorkspace< MDE, nd > > sptr
Typedef for a shared pointer of this kind of event workspace.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void debug(const std::string &msg)
Logs at debug level.
Definition Logger.cpp:145
The concrete, templated class for properties.
SpectralMomentMD : Multiply MD events by DeltaE^n.
const std::string category() const override
Algorithm's category for identification.
int version() const override
Algorithm's version for identification.
void exec() override
Execute the algorithm.
void applyScaling(typename Mantid::DataObjects::MDEventWorkspace< MDE, nd >::sptr ws)
Apply weight to each MDEvent.
std::map< std::string, std::string > validateInputs() override
Validate inputs.
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
void init() override
Initialize the algorithm's properties.
size_t mDeltaEIndex
index of the MD dimension index for DeltaE
std::shared_ptr< IMDEventWorkspace > IMDEventWorkspace_sptr
Shared pointer to Mantid::API::IMDEventWorkspace.
Helper class which provides the Collimation Length for SANS instruments.
STL namespace.
@ Input
An input workspace.
Definition Property.h:53
@ Output
An output workspace.
Definition Property.h:54