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