-
Notifications
You must be signed in to change notification settings - Fork 122
/
AppendSpectra.cpp
128 lines (107 loc) · 4.29 KB
/
AppendSpectra.cpp
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
#include "MantidAlgorithms/AppendSpectra.h"
#include "MantidKernel/System.h"
#include "MantidAPI/WorkspaceValidators.h"
#include "MantidAPI/WorkspaceOpOverloads.h"
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidKernel/SingletonHolder.h"
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::DataObjects;
namespace Mantid
{
namespace Algorithms
{
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(AppendSpectra)
/** Constructor
*/
AppendSpectra::AppendSpectra() : WorkspaceJoiners()
{}
/** Destructor
*/
AppendSpectra::~AppendSpectra()
{
}
/// Algorithm's name for identification. @see Algorithm::name
const std::string AppendSpectra::name() const { return "AppendSpectra";};
/// Algorithm's version for identification. @see Algorithm::version
int AppendSpectra::version() const { return 1;};
/** Initialize the algorithm's properties.
*/
void AppendSpectra::init()
{
declareProperty(new WorkspaceProperty<>("InputWorkspace1",
"", Direction::Input, boost::make_shared<CommonBinsValidator>()),
"The name of the first input workspace");
declareProperty(new WorkspaceProperty<>("InputWorkspace2",
"", Direction::Input, boost::make_shared<CommonBinsValidator>()),
"The name of the second input workspace");
declareProperty("ValidateInputs", true,
"Perform a set of checks that the two input workspaces are compatible.");
declareProperty(new WorkspaceProperty<>("OutputWorkspace", "", Direction::Output),
"The name of the output workspace");
}
/** Execute the algorithm.
*/
void AppendSpectra::exec()
{
// Retrieve the input workspaces
MatrixWorkspace_const_sptr ws1 = getProperty("InputWorkspace1");
MatrixWorkspace_const_sptr ws2 = getProperty("InputWorkspace2");
event_ws1 = boost::dynamic_pointer_cast<const EventWorkspace>(ws1);
event_ws2 = boost::dynamic_pointer_cast<const EventWorkspace>(ws2);
//Make sure that we are not mis-matching EventWorkspaces and other types of workspaces
if (((event_ws1) && (!event_ws2)) || ((!event_ws1) && (event_ws2)))
{
const std::string message("Only one of the input workspaces are of type EventWorkspace; please use matching workspace types (both EventWorkspace's or both Workspace2D's).");
g_log.error(message);
throw std::invalid_argument(message);
}
bool ValidateInputs = this->getProperty("ValidateInputs");
if (ValidateInputs)
{
// Check that the input workspaces meet the requirements for this algorithm
this->validateInputs(ws1,ws2);
}
if (event_ws1 && event_ws2)
{
//Both are event workspaces. Use the special method
MatrixWorkspace_sptr output = this->execEvent();
// Set the output workspace
setProperty("OutputWorkspace", output );
return;
}
// So it is a workspace 2D.
// The only restriction, even with ValidateInputs=false
if (ws1->blocksize() != ws2->blocksize())
throw std::runtime_error("Workspace2D's must have the same number of bins.");
MatrixWorkspace_sptr output = execWS2D(ws1, ws2);
// Set the output workspace
setProperty("OutputWorkspace", boost::dynamic_pointer_cast<MatrixWorkspace>(output) );
}
/** If there is an overlap in spectrum numbers between ws1 and ws2,
* then the spectrum numbers are reset as a simple 1-1 correspondence
* with the workspace index.
*
* @param ws1 The first workspace supplied to the algorithm.
* @param ws2 The second workspace supplied to the algorithm.
* @param output The workspace that is going to be returned by the algorithm.
*/
void AppendSpectra::fixSpectrumNumbers(API::MatrixWorkspace_const_sptr ws1, API::MatrixWorkspace_const_sptr ws2,
API::MatrixWorkspace_sptr output)
{
specid_t ws1min;
specid_t ws1max;
getMinMax(ws1, ws1min, ws1max);
specid_t ws2min;
specid_t ws2max;
getMinMax(ws2, ws2min, ws2max);
// is everything possibly ok?
if (ws2min > ws1max)
return;
// change the axis by adding the maximum existing spectrum number to the current value
for (size_t i = 0; i < output->getNumberHistograms(); i++)
output->getSpectrum(i)->setSpectrumNo( specid_t(i) );
}
} // namespace Mantid
} // namespace Algorithms