Stitch1DMany.cpp 9.66 KB
Newer Older
1
2
#include "MantidAlgorithms/Stitch1DMany.h"
#include "MantidAPI/MatrixWorkspace.h"
3
#include "MantidAPI/WorkspaceProperty.h"
4
5
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/BoundedValidator.h"
6
#include "MantidKernel/RebinParamsValidator.h"
7
8
9
10
11
12

#include <boost/make_shared.hpp>

using namespace Mantid::Kernel;
using namespace Mantid::API;

13
14
15
16
17
18
19
20
21
namespace Mantid {
namespace Algorithms {
DECLARE_ALGORITHM(Stitch1DMany)

/** Initialize the algorithm's properties.
 */
void Stitch1DMany::init() {

  declareProperty(
22
23
      make_unique<ArrayProperty<std::string>>("InputWorkspaces",
                                              Direction::Input),
24
25
      "Input Workspaces. List of histogram workspaces to stitch together.");

26
27
  declareProperty(make_unique<WorkspaceProperty<Workspace>>(
                      "OutputWorkspace", "", Direction::Output),
28
29
                  "Output stitched workspace.");

30
  declareProperty(make_unique<ArrayProperty<double>>(
31
32
33
34
                      "Params", boost::make_shared<RebinParamsValidator>(true),
                      Direction::Input),
                  "Rebinning Parameters. See Rebin for format.");

35
36
37
  declareProperty(
      make_unique<ArrayProperty<double>>("StartOverlaps", Direction::Input),
      "Start overlaps for stitched workspaces.");
38
39

  declareProperty(
40
41
      make_unique<ArrayProperty<double>>("EndOverlaps", Direction::Input),
      "End overlaps for stitched workspaces.");
42

43
44
45
46
47
48
  declareProperty(make_unique<PropertyWithValue<bool>>("ScaleRHSWorkspace",
                                                       true, Direction::Input),
                  "Scaling either with respect to workspace 1 or workspace 2");

  declareProperty(make_unique<PropertyWithValue<bool>>("UseManualScaleFactor",
                                                       false, Direction::Input),
49
50
51
52
53
54
                  "True to use a provided value for the scale factor.");

  auto manualScaleFactorValidator =
      boost::make_shared<BoundedValidator<double>>();
  manualScaleFactorValidator->setLower(0);
  manualScaleFactorValidator->setExclusive(true);
55
56
57
  declareProperty(make_unique<PropertyWithValue<double>>(
                      "ManualScaleFactor", 1.0, manualScaleFactorValidator,
                      Direction::Input),
58
59
60
                  "Provided value for the scale factor.");

  declareProperty(
61
62
      make_unique<ArrayProperty<double>>("OutScaleFactors", Direction::Output),
      "The actual used values for the scaling factors at each stitch step.");
63
64
65
66
67
68
69
70
71
72
73
74
75
76
}

/** Load and validate the algorithm's properties.
 */
std::map<std::string, std::string> Stitch1DMany::validateInputs() {
  std::map<std::string, std::string> errors;

  m_inputWorkspaces.clear();

  const std::vector<std::string> inputWorkspacesStr =
      this->getProperty("InputWorkspaces");
  if (inputWorkspacesStr.size() < 2)
    errors["InputWorkspaces"] = "At least 2 input workspaces required.";

77
78
  for (const auto &ws : inputWorkspacesStr) {
    if (AnalysisDataService::Instance().doesExist(ws)) {
79
      m_inputWorkspaces.push_back(
80
          AnalysisDataService::Instance().retrieveWS<Workspace>(ws));
81
    } else {
82
      errors["InputWorkspaces"] = ws + " is not a valid workspace.";
83
      break;
84
    }
85
86
87
  }

  // Check that all the workspaces are of the same type
88
  if (!m_inputWorkspaces.empty()) {
89
    const std::string id = m_inputWorkspaces[0]->id();
Hahn, Steven's avatar
Hahn, Steven committed
90
91
    for (auto &inputWorkspace : m_inputWorkspaces) {
      if (inputWorkspace->id() != id) {
92
93
        errors["InputWorkspaces"] = "All workspaces must be the same type.";
        break;
94
      }
95
    }
96

97
98
99
100
101
    // If our inputs are all group workspaces, check they're the same size
    WorkspaceGroup_sptr firstGroup =
        boost::dynamic_pointer_cast<WorkspaceGroup>(m_inputWorkspaces[0]);
    if (firstGroup) {
      size_t groupSize = firstGroup->size();
Hahn, Steven's avatar
Hahn, Steven committed
102
      for (auto &inputWorkspace : m_inputWorkspaces) {
103
        WorkspaceGroup_sptr group =
Hahn, Steven's avatar
Hahn, Steven committed
104
            boost::dynamic_pointer_cast<WorkspaceGroup>(inputWorkspace);
105
106
107
108
        if (group->size() != groupSize) {
          errors["InputWorkspaces"] =
              "All group workspaces must be the same size.";
          break;
109
110
        }
      }
111
112
113
114
115
116
117
118
119
120
    }
  } else {
    errors["InputWorkspaces"] = "Input workspaces must be given";
  }

  m_numWorkspaces = m_inputWorkspaces.size();

  m_startOverlaps = this->getProperty("StartOverlaps");
  m_endOverlaps = this->getProperty("EndOverlaps");

121
  if (!m_startOverlaps.empty() && m_startOverlaps.size() != m_numWorkspaces - 1)
122
123
124
125
126
127
128
129
130
131
132
133
    errors["StartOverlaps"] = "If given, StartOverlaps must have one fewer "
                              "entries than the number of input workspaces.";

  if (m_startOverlaps.size() != m_endOverlaps.size())
    errors["EndOverlaps"] =
        "EndOverlaps must have the same number of entries as StartOverlaps.";

  m_scaleRHSWorkspace = this->getProperty("ScaleRHSWorkspace");
  m_useManualScaleFactor = this->getProperty("UseManualScaleFactor");
  m_manualScaleFactor = this->getProperty("ManualScaleFactor");
  m_params = this->getProperty("Params");

134
  if (m_params.empty())
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
    errors["Params"] = "At least one parameter must be given.";

  if (!m_scaleRHSWorkspace) {
    // Flip these around for processing
    std::reverse(m_inputWorkspaces.begin(), m_inputWorkspaces.end());
    std::reverse(m_startOverlaps.begin(), m_startOverlaps.end());
    std::reverse(m_endOverlaps.begin(), m_endOverlaps.end());
  }

  m_scaleFactors.clear();
  m_outputWorkspace.reset();

  return errors;
}

/** Execute the algorithm.
 */
void Stitch1DMany::exec() {
  // Check we're not dealing with group workspaces
  if (!boost::dynamic_pointer_cast<WorkspaceGroup>(m_inputWorkspaces[0])) {
    MatrixWorkspace_sptr lhsWS =
        boost::dynamic_pointer_cast<MatrixWorkspace>(m_inputWorkspaces[0]);

    for (size_t i = 1; i < m_numWorkspaces; ++i) {
      MatrixWorkspace_sptr rhsWS =
          boost::dynamic_pointer_cast<MatrixWorkspace>(m_inputWorkspaces[i]);

      IAlgorithm_sptr stitchAlg = createChildAlgorithm("Stitch1D");
      stitchAlg->initialize();

      stitchAlg->setProperty("LHSWorkspace", lhsWS);
      stitchAlg->setProperty("RHSWorkspace", rhsWS);
      if (m_startOverlaps.size() > i - 1) {
        stitchAlg->setProperty("StartOverlap", m_startOverlaps[i - 1]);
        stitchAlg->setProperty("EndOverlap", m_endOverlaps[i - 1]);
170
      }
171
172
173
174
175
      stitchAlg->setProperty("Params", m_params);
      stitchAlg->setProperty("ScaleRHSWorkspace", m_scaleRHSWorkspace);
      stitchAlg->setProperty("UseManualScaleFactor", m_useManualScaleFactor);
      if (m_useManualScaleFactor)
        stitchAlg->setProperty("ManualScaleFactor", m_manualScaleFactor);
176

177
      stitchAlg->execute();
178

179
180
181
      lhsWS = stitchAlg->getProperty("OutputWorkspace");
      m_scaleFactors.push_back(stitchAlg->getProperty("OutScaleFactor"));
    }
182

183
184
    if (!isChild()) {
      // Copy each input workspace's history into our output workspace's history
Hahn, Steven's avatar
Hahn, Steven committed
185
186
      for (auto &inputWorkspace : m_inputWorkspaces)
        lhsWS->history().addHistory(inputWorkspace->getHistory());
187
188
189
190
191
    }
    // We're a child algorithm, but we're recording history anyway
    else if (isRecordingHistoryForChild() && m_parentHistory) {
      m_parentHistory->addChildHistory(m_history);
    }
192

193
194
195
196
197
    m_outputWorkspace = lhsWS;
  }
  // We're dealing with group workspaces
  else {
    std::vector<WorkspaceGroup_sptr> groupWorkspaces;
Hahn, Steven's avatar
Hahn, Steven committed
198
199
    groupWorkspaces.reserve(m_inputWorkspaces.size());
    for (auto &inputWorkspace : m_inputWorkspaces)
200
      groupWorkspaces.push_back(
Hahn, Steven's avatar
Hahn, Steven committed
201
          boost::dynamic_pointer_cast<WorkspaceGroup>(inputWorkspace));
202
203
204
205

    // List of workspaces to be grouped
    std::vector<std::string> toGroup;

206
207
    const std::string groupName = this->getProperty("OutputWorkspace");

208
209
210
211
212
213
    size_t numWSPerGroup = groupWorkspaces[0]->size();

    for (size_t i = 0; i < numWSPerGroup; ++i) {
      // List of workspaces to stitch
      std::vector<std::string> toProcess;
      // The name of the resulting workspace
214
      std::string outName = groupName;
215

216
217
      for (auto &groupWorkspace : groupWorkspaces) {
        const std::string wsName = groupWorkspace->getItem(i)->name();
218
        toProcess.push_back(wsName);
219
        outName += "_" + wsName;
220
221
      }

222
      IAlgorithm_sptr stitchAlg = createChildAlgorithm("Stitch1DMany");
223
      stitchAlg->initialize();
224
      stitchAlg->setAlwaysStoreInADS(true);
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
      stitchAlg->setProperty("InputWorkspaces", toProcess);
      stitchAlg->setProperty("OutputWorkspace", outName);
      stitchAlg->setProperty("StartOverlaps", m_startOverlaps);
      stitchAlg->setProperty("EndOverlaps", m_endOverlaps);
      stitchAlg->setProperty("Params", m_params);
      stitchAlg->setProperty("ScaleRHSWorkspace", m_scaleRHSWorkspace);
      stitchAlg->setProperty("UseManualScaleFactor", m_useManualScaleFactor);
      if (m_useManualScaleFactor)
        stitchAlg->setProperty("ManualScaleFactor", m_manualScaleFactor);
      stitchAlg->execute();

      // Add the resulting workspace to the list to be grouped together
      toGroup.push_back(outName);

      // Add the scalefactors to the list so far
      const std::vector<double> scaleFactors =
          stitchAlg->getProperty("OutScaleFactors");
      m_scaleFactors.insert(m_scaleFactors.end(), scaleFactors.begin(),
                            scaleFactors.end());
244
245
    }

246
    IAlgorithm_sptr groupAlg = createChildAlgorithm("GroupWorkspaces");
247
    groupAlg->initialize();
248
    groupAlg->setAlwaysStoreInADS(true);
249
250
251
    groupAlg->setProperty("InputWorkspaces", toGroup);
    groupAlg->setProperty("OutputWorkspace", groupName);
    groupAlg->execute();
252

253
254
255
    m_outputWorkspace =
        AnalysisDataService::Instance().retrieveWS<Workspace>(groupName);
  }
256

257
258
259
260
  // Save output
  this->setProperty("OutputWorkspace", m_outputWorkspace);
  this->setProperty("OutScaleFactors", m_scaleFactors);
}
261

262
} // namespace Algorithms
263
} // namespace Mantid