Mantid
Loading...
Searching...
No Matches
RunCombinationHelper.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 +
8
10#include "MantidAPI/Axis.h"
15#include "MantidKernel/Logger.h"
16#include "MantidKernel/Unit.h"
17
18namespace Mantid::Algorithms {
19
20using namespace API;
21using namespace Geometry;
22using namespace Kernel;
23
24//----------------------------------------------------------------------------------------------
32std::vector<std::string> RunCombinationHelper::unWrapGroups(const std::vector<std::string> &inputs) {
33 std::vector<std::string> outputs;
34 for (const auto &input : inputs) {
35 WorkspaceGroup_sptr wsgroup = AnalysisDataService::Instance().retrieveWS<WorkspaceGroup>(input);
36 if (wsgroup) {
37 // workspace group
38 std::vector<std::string> group = wsgroup->getNames();
39 outputs.insert(outputs.end(), group.begin(), group.end());
40 } else {
41 // MatrixWorkspace
42 MatrixWorkspace_sptr matrixws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(input);
43 if (matrixws)
44 outputs.emplace_back(matrixws->getName());
45 else
46 throw(std::runtime_error("The input " + input + " is neither a WorkspaceGroup nor a MatrixWorkspace"));
47 }
48 }
49 return outputs;
50}
51
52//----------------------------------------------------------------------------------------------
59 m_numberDetectors = ref->detectorInfo().size();
60 m_xUnit = ref->getAxis(0)->unit()->unitID();
61 m_spectrumAxisUnit = ref->getAxis(1)->unit()->unitID();
62 m_yUnit = ref->YUnit();
63 m_isHistogramData = ref->isHistogramData();
64 m_isDistribution = ref->isDistribution();
65 m_isScanning = ref->detectorInfo().isScanning();
66 m_instrumentName = ref->getInstrumentName();
67 if (m_numberSpectra) {
68 m_hasDx.reserve(m_numberSpectra);
69 for (unsigned int i = 0; i < m_numberSpectra; ++i)
70 m_hasDx.emplace_back(ref->hasDx(i));
71 }
72}
73
74//----------------------------------------------------------------------------------------------
80std::string RunCombinationHelper::checkCompatibility(const MatrixWorkspace_sptr &ws, bool checkNumberHistograms) {
81 std::string errors;
82 if (ws->getNumberHistograms() != m_numberSpectra && checkNumberHistograms)
83 errors += "different number of histograms; ";
84 if (ws->getAxis(0)->unit()->unitID() != m_xUnit)
85 errors += "different X units; ";
86 if (ws->getAxis(1)->unit()->unitID() != m_spectrumAxisUnit)
87 errors += "different spectrum axis units; ";
88 if (ws->YUnit() != m_yUnit)
89 errors += "different Y units; ";
90 if (ws->isHistogramData() != m_isHistogramData)
91 errors += "different data type (Histogram Data vs Point Data); ";
92 if (ws->isDistribution() != m_isDistribution)
93 errors += "different distribution flag (Histogrm vs Distribution); ";
94 if (ws->detectorInfo().isScanning() != m_isScanning)
95 errors += "a mix of workspaces with and without detector scans; ";
96 if (m_isScanning && ws->detectorInfo().size() != m_numberDetectors)
97 errors += "workspaces with detectors scans have different number of "
98 "detectors; ";
99 if (ws->getInstrumentName() != m_instrumentName)
100 errors += "different instrument names; ";
101 if (ws->getNumberHistograms() == m_numberSpectra) {
102 if (!m_hasDx.empty()) {
103 for (unsigned int i = 0; i < m_numberSpectra; ++i) {
104 if (m_hasDx[i] != ws->hasDx(i)) {
105 errors += "spectra must have either Dx values or not; ";
106 break;
107 }
108 }
109 }
110 }
111 return errors;
112}
113
124std::list<API::MatrixWorkspace_sptr>
125RunCombinationHelper::validateInputWorkspaces(const std::vector<std::string> &inputWorkspaces, Logger &g_log) {
126 std::list<MatrixWorkspace_sptr> inWS;
127
128 for (size_t i = 0; i < inputWorkspaces.size(); ++i) {
130 // Fetch the next input workspace - throw an error if it's not there
131 ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(inputWorkspaces[i]);
132 if (!ws) {
133 throw std::runtime_error("Could not find a MatrixWorkspace with the name " + inputWorkspaces[i]);
134 }
135 inWS.emplace_back(ws);
136 // Check that it has common binning
137 if (!inWS.back()->isCommonBins()) {
138 g_log.error("Input workspaces must have common binning for all spectra");
139 throw std::invalid_argument("Input workspaces must have common binning for all spectra");
140 }
141 // Check a few things are the same for all input workspaces
142 if (i == 0) {
144 } else {
145 std::string compatibility = checkCompatibility(ws);
146 if (!compatibility.empty()) {
147 g_log.error("Input workspaces are not compatible: " + compatibility);
148 throw std::invalid_argument("Input workspaces are not compatible: " + compatibility);
149 }
150 }
151 }
152 return inWS;
153}
154
155} // namespace Mantid::Algorithms
Base MatrixWorkspace Abstract Class.
virtual std::size_t getNumberHistograms() const =0
Returns the number of histograms in the workspace.
Class to hold a set of workspaces.
std::vector< std::string > getNames() const
Returns the names of workspaces that make up this group.
static std::vector< std::string > unWrapGroups(const std::vector< std::string > &)
Flattens the list of group workspaces (if any) into list of workspaces.
std::list< API::MatrixWorkspace_sptr > validateInputWorkspaces(const std::vector< std::string > &inputWorkspaces, Kernel::Logger &g_log)
Checks that the input workspace all exist, that they are the same size, have the same units and the s...
void setReferenceProperties(const API::MatrixWorkspace_sptr &)
Sets the properties of the reference (usually first) workspace, to later check the compatibility of t...
std::string checkCompatibility(const API::MatrixWorkspace_sptr &, bool checkNumberHistograms=false)
Compares the properties of the input workspace with the reference.
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition Logger.h:51
void error(const std::string &msg)
Logs at error level.
Definition Logger.cpp:108
std::shared_ptr< WorkspaceGroup > WorkspaceGroup_sptr
shared pointer to Mantid::API::WorkspaceGroup
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
Kernel::Logger g_log("DetermineSpinStateOrder")