-
Notifications
You must be signed in to change notification settings - Fork 121
/
BinaryOperation.h
273 lines (214 loc) · 11.4 KB
/
BinaryOperation.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#ifndef MANTID_ALGORITHMS_BINARYOPERATION_H_
#define MANTID_ALGORITHMS_BINARYOPERATION_H_
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/Run.h"
#include "MantidAPI/Workspace.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidDataObjects/EventList.h"
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidKernel/System.h"
namespace Mantid
{
namespace Algorithms
{
enum OperandType
{
eEventList = 0,
eHistogram = 1,
eNumber = 2
};
/**
BinaryOperation supports the implementation of a binary operation on two input workspaces.
It inherits from the Algorithm class, and overrides the init() & exec() methods.
Required Properties:
<UL>
<LI> InputWorkspace1 - The name of the workspace forming the left hand operand</LI>
<LI> InputWorkspace2 - The name of the workspace forming the right hand operand </LI>
<LI> OutputWorkspace - The name of the workspace in which to store the result </LI>
</UL>
@author Nick Draper
@date 14/12/2007
Copyright © 2007-2010 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>
*/
class DLLExport BinaryOperation : public API::Algorithm
{
public:
/// Default constructor
BinaryOperation();
/// Destructor
virtual ~BinaryOperation();
/// Algorithm's category for identification overriding a virtual method
virtual const std::string category() const { return "Arithmetic";}
/** BinaryOperationTable: a list of ints.
* Index into vector: workspace index in the lhs;
* Value at that index: workspace index of the rhs to apply to the WI in the lhs. -1 if not found.
*/
typedef std::vector< int64_t> BinaryOperationTable;
typedef boost::shared_ptr<BinaryOperationTable> BinaryOperationTable_sptr;
static BinaryOperationTable_sptr
buildBinaryOperationTable(const API::MatrixWorkspace_const_sptr & lhs, const API::MatrixWorkspace_const_sptr & rhs);
protected:
// Overridden Algorithm methods
virtual void exec();
virtual void init();
bool handleSpecialDivideMinus();
/// Execution method for event workspaces, to be overridden as needed.
virtual void execEvent( DataObjects::EventWorkspace_const_sptr lhs, DataObjects::EventWorkspace_const_sptr rhs );
/// The name of the first input workspace property
virtual std::string inputPropName1() const { return "LHSWorkspace";}
/// The name of the second input workspace property
virtual std::string inputPropName2() const { return "RHSWorkspace";}
/// The name of the output workspace property
virtual std::string outputPropName() const { return "OutputWorkspace";}
/// Checks the compatibility of the two workspaces
virtual bool checkCompatibility(const API::MatrixWorkspace_const_sptr lhs,const API::MatrixWorkspace_const_sptr rhs) const;
/// Checks the compatibility of event-based processing of the two workspaces
virtual bool checkEventCompatibility(const API::MatrixWorkspace_const_sptr lhs,const API::MatrixWorkspace_const_sptr rhs);
/// Checks the overall size compatibility of two workspaces
virtual std::string checkSizeCompatibility(const API::MatrixWorkspace_const_sptr lhs,const API::MatrixWorkspace_const_sptr rhs) const;
/// Checks if the spectra at the given index of either input workspace is masked. If so then the output spectra has zeroed data
/// and is also masked. The function returns true if further processing is not required on the spectra.
virtual bool propagateSpectraMask(const API::MatrixWorkspace_const_sptr lhs,const API::MatrixWorkspace_const_sptr rhs,
const int64_t index, API::MatrixWorkspace_sptr out);
/** Carries out the binary operation on a single spectrum, with another spectrum as the right-hand operand.
*
* @param lhsX :: The X values, made available if required.
* @param lhsY :: The vector of lhs data values
* @param lhsE :: The vector of lhs error values
* @param rhsY :: The vector of rhs data values
* @param rhsE :: The vector of rhs error values
* @param YOut :: The vector to hold the data values resulting from the operation
* @param EOut :: The vector to hold the error values resulting from the operation
*/
virtual void performBinaryOperation(const MantidVec& lhsX, const MantidVec& lhsY, const MantidVec& lhsE,
const MantidVec& rhsY, const MantidVec& rhsE, MantidVec& YOut, MantidVec& EOut) = 0;
/** Carries out the binary operation when the right hand operand is a single number.
*
* @param lhsX :: The X values, made available if required.
* @param lhsY :: The vector of lhs data values
* @param lhsE :: The vector of lhs error values
* @param rhsY :: The rhs data value
* @param rhsE :: The rhs error value
* @param YOut :: The vector to hold the data values resulting from the operation
* @param EOut :: The vector to hold the error values resulting from the operation
*/
virtual void performBinaryOperation(const MantidVec& lhsX, const MantidVec& lhsY, const MantidVec& lhsE,
const double rhsY, const double rhsE, MantidVec& YOut, MantidVec& EOut) = 0;
// ===================================== EVENT LIST BINARY OPERATIONS ==========================================
/** Carries out the binary operation IN-PLACE on a single EventList,
* with another EventList as the right-hand operand.
* The event lists simply get appended.
*
* @param lhs :: Reference to the EventList that will be modified in place.
* @param rhs :: Const reference to the EventList on the right hand side.
*/
virtual void performEventBinaryOperation(DataObjects::EventList & lhs,
const DataObjects::EventList & rhs);
/** Carries out the binary operation IN-PLACE on a single EventList,
* with another (histogrammed) spectrum as the right-hand operand.
*
* @param lhs :: Reference to the EventList that will be modified in place.
* @param rhsX :: The vector of rhs X bin boundaries
* @param rhsY :: The vector of rhs data values
* @param rhsE :: The vector of rhs error values
*/
virtual void performEventBinaryOperation(DataObjects::EventList & lhs,
const MantidVec& rhsX, const MantidVec& rhsY, const MantidVec& rhsE);
/** Carries out the binary operation IN-PLACE on a single EventList,
* with a single (double) value as the right-hand operand
*
* @param lhs :: Reference to the EventList that will be modified in place.
* @param rhsY :: The rhs data value
* @param rhsE :: The rhs error value
*/
virtual void performEventBinaryOperation(DataObjects::EventList & lhs,
const double& rhsY, const double& rhsE);
/** Should be overridden by operations that need to manipulate the units of the output workspace.
* Does nothing by default.
* @param lhs :: The first input workspace
* @param rhs :: The second input workspace
* @param out :: The output workspace
*/
virtual void setOutputUnits(const API::MatrixWorkspace_const_sptr lhs,const API::MatrixWorkspace_const_sptr rhs,API::MatrixWorkspace_sptr out)
{
(void) lhs; //Avoid compiler warning
(void) rhs;
(void) out;
}
/** Only overridden by operations that affect the properties of the run (e.g. Plus
* where the proton currents (charges) are added). Otherwise it does nothing.
* @param lhs :: one of the workspaces to operate on
* @param rhs :: the other workspace
* @param ans :: the output workspace
*/
virtual void operateOnRun(const API::Run& lhs, const API::Run& rhs, API::Run& ans) const
{
(void) lhs; //Avoid compiler warning
(void) rhs;
(void) ans;
};
OperandType getOperandType(const API::MatrixWorkspace_const_sptr ws);
virtual void checkRequirements();
// ------- Workspaces being worked on --------
/// Left-hand side workspace
API::MatrixWorkspace_const_sptr m_lhs;
/// Left-hand side EventWorkspace
DataObjects::EventWorkspace_const_sptr m_elhs;
/// Right-hand side workspace
API::MatrixWorkspace_const_sptr m_rhs;
/// Right-hand side EventWorkspace
DataObjects::EventWorkspace_const_sptr m_erhs;
/// Output workspace
API::MatrixWorkspace_sptr m_out;
/// Output EventWorkspace
DataObjects::EventWorkspace_sptr m_eout;
/// The property value
bool m_AllowDifferentNumberSpectra;
/// Flag to clear RHS workspace in binary operation
bool m_ClearRHSWorkspace;
//------ Requirements -----------
/// matchXSize set to true if the X sizes of histograms must match.
bool m_matchXSize;
/// flipSides set to true if the rhs and lhs operands should be flipped - for commutative binary operations, normally.
bool m_flipSides;
/// Variable set to true if the operation allows the output to stay as an EventWorkspace. If this returns false, any EventWorkspace will be converted to Workspace2D. This is ignored if the lhs operand is not an EventWorkspace.
bool m_keepEventWorkspace;
/** Are we going to use the histogram representation of the RHS event list when performing the operation?
* e.g. divide and multiply? Plus and Minus will set this to false (default).
*/
bool m_useHistogramForRhsEventWorkspace;
/** Special case for plus/minus: if there is only one bin on the RHS, use the 2D method (appending event lists)
* so that the single bin is not treated as a scalar
*/
bool m_do2D_even_for_SingleColumn_on_rhs;
private:
void doSingleValue();
void doSingleSpectrum();
void doSingleColumn();
void do2D(bool mismatchedSpectra);
void propagateBinMasks(const API::MatrixWorkspace_const_sptr rhs, API::MatrixWorkspace_sptr out);
/// Apply masking requested by propagateSpectraMasks.
void applyMaskingToOutput(API::MatrixWorkspace_sptr out);
/// A store for accumulated spectra that should be masked in the output workspace
std::vector<int64_t> m_indicesToMask;
/// Progress reporting
API::Progress* m_progress;
};
} // namespace Algorithms
} // namespace Mantid
#endif /*MANTID_ALGORITHM_BINARYOPERATION_H_*/