Mantid
Loading...
Searching...
No Matches
UnaryOperation.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 +
12
13using namespace Mantid::API;
14using namespace Mantid::Kernel;
15using namespace Mantid::DataObjects;
16
17namespace Mantid::Algorithms {
23 "The name of the input workspace");
25 "The name to use for the output workspace (can be the same "
26 "as the input one).");
27
28 // Call the virtual defineProperties functions to declare any properties
29 // defined in concrete algorithm
31}
32
35 // get the input workspaces
37
38 // Check if it is an event workspace
39 EventWorkspace_const_sptr eventW = std::dynamic_pointer_cast<const EventWorkspace>(in_work);
40 if ((eventW != nullptr) && !(this->useHistogram)) {
41 this->execEvent();
42 return;
43 }
44
46 // Only create output workspace if different to input one
47 if (out_work != in_work) {
48 if (in_work->id() == "EventWorkspace") {
49 // Handles case of EventList which needs to be converted to Workspace2D
50 out_work = WorkspaceFactory::Instance().create(in_work);
51 } else {
52 out_work = in_work->clone();
53 }
54 setProperty(outputPropName(), out_work);
55 }
56
57 // Now fetch any properties defined by concrete algorithm
59
60 const size_t numSpec = in_work->getNumberHistograms();
61 const size_t specSize = in_work->blocksize();
62
63 // Initialise the progress reporting object
64 Progress progress(this, 0.0, 1.0, numSpec);
65
66 // Loop over every cell in the workspace, calling the abstract correction
67 // function
68 PARALLEL_FOR_IF(Kernel::threadSafe(*in_work, *out_work))
69 for (int64_t i = 0; i < int64_t(numSpec); ++i) {
71 // Copy the X values over
72 out_work->setSharedX(i, in_work->sharedX(i));
73 // Get references to the data
74 // Output (non-const) ones first because they may copy the vector
75 // if it's shared, which isn't thread-safe.
76 auto &YOut = out_work->mutableY(i);
77 auto &EOut = out_work->mutableE(i);
78 const auto X = in_work->points(i);
79 const auto &Y = in_work->y(i);
80 const auto &E = in_work->e(i);
81
82 for (size_t j = 0; j < specSize; ++j) {
83 // Call the abstract function, passing in the current values
84 performUnaryOperation(X[j], Y[j], E[j], YOut[j], EOut[j]);
85 }
86
87 progress.report();
89 }
91}
92
95 g_log.information("Processing event workspace");
96
98
99 // generate the output workspace pointer
101 if (matrixOutputWS != matrixInputWS) {
102 matrixOutputWS = matrixInputWS->clone();
103 setProperty(outputPropName(), matrixOutputWS);
104 }
105 auto outputWS = std::dynamic_pointer_cast<EventWorkspace>(matrixOutputWS);
106
107 // Now fetch any properties defined by concrete algorithm
109
110 auto numHistograms = static_cast<int64_t>(outputWS->getNumberHistograms());
111 API::Progress prog = API::Progress(this, 0.0, 1.0, numHistograms);
113 for (int64_t i = 0; i < numHistograms; ++i) {
115 // switch to weighted events if needed, and use the appropriate helper
116 // function
117 auto &evlist = outputWS->getSpectrum(i);
118 switch (evlist.getEventType()) {
119 case TOF:
120 // Switch to weights if needed.
121 evlist.switchTo(WEIGHTED);
122 /* no break */
123 // Fall through
124
125 case WEIGHTED:
126 unaryOperationEventHelper(evlist.getWeightedEvents());
127 break;
128
129 case WEIGHTED_NOTIME:
130 unaryOperationEventHelper(evlist.getWeightedEventsNoTime());
131 break;
132 }
133
134 prog.report();
136 }
138
139 outputWS->clearMRU();
140 auto inputWS = std::dynamic_pointer_cast<EventWorkspace>(matrixOutputWS);
141 if (inputWS->getNumberEvents() != outputWS->getNumberEvents()) {
142 g_log.information() << "Number of events has changed!!!\n";
143 }
144}
145
147template <class T> void UnaryOperation::unaryOperationEventHelper(std::vector<T> &wevector) {
148
149 typename std::vector<T>::iterator it;
150 for (it = wevector.begin(); it < wevector.end(); ++it) {
151 double yout, eout;
152 // Call the abstract function, passing in the current values
153 performUnaryOperation(it->tof(), it->weight(), std::sqrt(it->errorSquared()), yout, eout);
154 it->m_weight = static_cast<float>(yout);
155 it->m_errorSquared = static_cast<float>(eout * eout);
156 }
157}
158} // namespace Mantid::Algorithms
#define PARALLEL_START_INTERRUPT_REGION
Begins a block to skip processing is the algorithm has been interupted Note the end of the block if n...
Definition: MultiThreaded.h:94
#define PARALLEL_END_INTERRUPT_REGION
Ends a block to skip processing is the algorithm has been interupted Note the start of the block if n...
#define PARALLEL_FOR_IF(condition)
Empty definitions - to enable set your complier to enable openMP.
#define PARALLEL_CHECK_INTERRUPT_REGION
Adds a check after a Parallel region to see if it was interupted.
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
Kernel::Logger & g_log
Definition: Algorithm.h:451
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
Definition: Algorithm.cpp:231
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
A property class for workspaces.
bool useHistogram
flag to use histogram representation instead of events for certain algorithms
virtual void defineProperties()
A virtual function in which additional properties of an algorithm should be declared.
virtual const std::string outputPropName() const
The name of the output workspace property.
void unaryOperationEventHelper(std::vector< T > &wevector)
Helper for events, for use with different types of weighted events.
virtual void performUnaryOperation(const double XIn, const double YIn, const double EIn, double &YOut, double &EOut)=0
Carries out the Unary operation on the current 'cell'.
void exec() override
Executes the algorithm.
void init() override
Initialisation method.
virtual const std::string inputPropName() const
The name of the input workspace property.
virtual void retrieveProperties()
A virtual function in which additional properties should be retrieved into member variables.
virtual void execEvent()
Executes the algorithm for events.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void information(const std::string &msg)
Logs at information level.
Definition: Logger.cpp:105
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
Definition: ProgressBase.h:51
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
@ WEIGHTED_NOTIME
Definition: IEventList.h:18
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< const EventWorkspace > EventWorkspace_const_sptr
shared pointer to a const Workspace2D
std::enable_if< std::is_pointer< Arg >::value, bool >::type threadSafe(Arg workspace)
Thread-safety check Checks the workspace to ensure it is suitable for multithreaded access.
Definition: MultiThreaded.h:22
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54