Mantid
Loading...
Searching...
No Matches
MaskBinsFromTable.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 +
10#include "MantidAPI/TableRow.h"
14#include "MantidKernel/System.h"
15
16#include <boost/algorithm/string/predicate.hpp>
17
18using namespace Mantid::Kernel;
19using namespace Mantid::API;
20using namespace Mantid::DataObjects;
21using namespace Mantid::Geometry;
22
23using namespace std;
24
25namespace Mantid::Algorithms {
26
27DECLARE_ALGORITHM(MaskBinsFromTable)
28
29//----------------------------------------------------------------------------------------------
33 : API::Algorithm(), id_xmin(-1), id_xmax(-1), id_spec(-1), id_dets(-1), m_useDetectorID(false),
34 m_useSpectrumID(false) {}
35
36//----------------------------------------------------------------------------------------------
38 this->declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input,
39 std::make_shared<HistogramValidator>()),
40 "Input Workspace to mask bins. ");
41 this->declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
42 "Output Workspace with bins masked.");
43 this->declareProperty(
44 std::make_unique<WorkspaceProperty<DataObjects::TableWorkspace>>("MaskingInformation", "", Direction::Input),
45 "Input TableWorkspace containing parameters, XMin and "
46 "XMax and either SpectraList or DetectorIDsList");
47}
48
49//----------------------------------------------------------------------------------------------
53 // Process input properties
54 MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace");
55 TableWorkspace_sptr paramWS = getProperty("MaskingInformation");
56
57 processMaskBinWorkspace(paramWS, inputWS);
58
59 // Mask bins for all
60 maskBins(inputWS);
61}
62
63//----------------------------------------------------------------------------------------------
68 bool firstloop = true;
70
71 size_t numcalls = m_xminVec.size();
72
73 g_log.debug() << "There will be " << numcalls << " calls to MaskBins"
74 << "\n";
75 for (size_t ib = 0; ib < numcalls; ++ib) {
76 // Construct algorithm
77 auto maskbins = createChildAlgorithm("MaskBins", 0, 0.3, true);
78 maskbins->initialize();
79
80 // Set properties
81 g_log.debug() << "Input to MaskBins: SpetraList = '" << m_spectraVec[ib] << "'; Xmin = " << m_xminVec[ib]
82 << ", Xmax = " << m_xmaxVec[ib] << ".\n";
83
84 if (firstloop) {
85 maskbins->setProperty("InputWorkspace", dataws);
86 firstloop = false;
87 } else {
88 if (!outputws)
89 throw runtime_error("Programming logic error.");
90 maskbins->setProperty("InputWorkspace", outputws);
91 }
92 maskbins->setProperty("OutputWorkspace", this->getPropertyValue("OutputWorkspace"));
93 maskbins->setPropertyValue("SpectraList", m_spectraVec[ib]);
94 maskbins->setProperty("XMin", m_xminVec[ib]);
95 maskbins->setProperty("XMax", m_xmaxVec[ib]);
96
97 bool isexec = maskbins->execute();
98 if (!isexec) {
99 stringstream errmsg;
100 errmsg << "MaskBins() is not executed for row " << ib << "\n";
101 g_log.error(errmsg.str());
102 throw std::runtime_error(errmsg.str());
103 } else {
104 g_log.debug("MaskBins() is executed successfully.");
105 }
106
107 outputws = maskbins->getProperty("OutputWorkspace");
108 if (!outputws) {
109 stringstream errmsg;
110 errmsg << "OutputWorkspace cannot be obtained from algorithm row " << ib << ". ";
111 g_log.error(errmsg.str());
112 throw std::runtime_error(errmsg.str());
113 }
114 }
115
116 //
117 g_log.debug() << "About to set to output."
118 << "\n";
119 setProperty("OutputWorkspace", outputws);
120}
121
122//----------------------------------------------------------------------------------------------
129 const API::MatrixWorkspace_sptr &dataws) {
130 // Check input
131 if (!masktblws)
132 throw std::invalid_argument("Input workspace is not a table workspace.");
133 g_log.debug() << "Lines of parameters workspace = " << masktblws->rowCount() << '\n';
134
135 // Check column names type and sequence
136 vector<std::string> colnames = masktblws->getColumnNames();
137
138 // check colum name order
139 id_xmin = -1;
140 id_xmax = -1;
141 id_spec = -1;
142 id_dets = -1;
143 m_useDetectorID = false;
144 m_useSpectrumID = false;
145
146 for (int i = 0; i < static_cast<int>(colnames.size()); ++i) {
147 string colname = colnames[i];
148 transform(colname.begin(), colname.end(), colname.begin(), ::tolower);
149 if (colname == "xmin")
150 id_xmin = i;
151 else if (colname == "xmax")
152 id_xmax = i;
153 else if (boost::algorithm::starts_with(colname, "spec")) {
154 id_spec = i;
155 } else if (boost::algorithm::starts_with(colname, "detectorid")) {
156 id_dets = i;
157 } else {
158 g_log.warning() << "In TableWorkspace " << masktblws->getName() << ", column " << i << " with name " << colname
159 << " is not used by MaskBinsFromTable.";
160 }
161 }
162
163 if (id_xmin < 0 || id_xmax < 0 || id_xmin == id_xmax)
164 throw runtime_error("Either Xmin nor Xmax is not given. ");
165 if (id_spec == id_dets)
166 throw runtime_error("Neither SpectraList nor DetectorIDList is given.");
167 else if (id_dets >= 0)
168 m_useDetectorID = true;
169 else
170 m_useSpectrumID = true;
171
172 // Construct vectors for xmin, xmax and spectra-list
173 size_t numrows = masktblws->rowCount();
174 for (size_t i = 0; i < numrows; ++i) {
175 double xmin = masktblws->cell<double>(i, static_cast<size_t>(id_xmin));
176 double xmax = masktblws->cell<double>(i, static_cast<size_t>(id_xmax));
177
178 string spectralist;
179 if (m_useSpectrumID) {
180 spectralist = masktblws->cell<string>(i, static_cast<size_t>(id_spec));
181 } else {
182 // Convert detectors list to spectra list
183 string detidslist = masktblws->cell<string>(i, static_cast<size_t>(id_dets));
184 spectralist = convertToSpectraList(dataws, detidslist);
185 }
186
187 g_log.debug() << "Row " << i << " XMin = " << xmin << " XMax = " << xmax << " SpectraList = " << spectralist
188 << ".\n";
189
190 // Store to class variables
191 m_xminVec.emplace_back(xmin);
192 m_xmaxVec.emplace_back(xmax);
193 m_spectraVec.emplace_back(spectralist);
194 }
195}
196
197//----------------------------------------------------------------------------------------------
205 const std::string &detidliststr) {
206 // Use array property to get a list of detectors
207 vector<int> detidvec;
208 ArrayProperty<int> parser("detids", detidliststr);
209
210 size_t numitems = parser.size();
211 for (size_t i = 0; i < numitems; ++i) {
212 detidvec.emplace_back(parser.operator()()[i]);
213 g_log.debug() << "[DB] DetetorID = " << detidvec.back() << " to mask.";
214 }
215
216 // Get workspace index from
217 vector<size_t> wsindexvec;
218
219 detid2index_map refermap = dataws->getDetectorIDToWorkspaceIndexMap(false);
220 for (size_t i = 0; i < numitems; ++i) {
221 detid_t detid = detidvec[i];
222 detid2index_map::const_iterator fiter = refermap.find(detid);
223 if (fiter != refermap.end()) {
224 size_t wsindex = fiter->second;
225 wsindexvec.emplace_back(wsindex);
226 } else {
227 g_log.warning() << "Detector ID " << detid << " cannot be mapped to any workspace index/spectrum."
228 << ".\n";
229 }
230 }
231
232 // Sort the vector
233 if (wsindexvec.empty())
234 throw runtime_error("There is no spectrum found for input detectors list.");
235
236 sort(wsindexvec.begin(), wsindexvec.end());
237
238 // Convert the spectra to a string
239 stringstream rss;
240 size_t headid = wsindexvec[0];
241 size_t previd = wsindexvec[0];
242
243 for (size_t i = 1; i < wsindexvec.size(); ++i) {
244 size_t currid = wsindexvec[i];
245 if (currid > previd + 1) {
246 // skipped. previous region should be written, and a new region start
247 if (headid == previd)
248 rss << headid << ", ";
249 else
250 rss << headid << "-" << previd << ", ";
251 headid = currid;
252 } else {
253 // Equal to continuous
254 ;
255 }
256
257 // Update
258 previd = currid;
259 }
260
261 // Finalize
262 if (headid == previd)
263 rss << headid;
264 else
265 rss << headid << "-" << previd;
266
267 string spectraliststr(rss.str());
268
269 return spectraliststr;
270}
271
272} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:85
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
Definition: Algorithm.cpp:2026
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
Definition: Algorithm.cpp:842
Kernel::Logger & g_log
Definition: Algorithm.h:451
A property class for workspaces.
MaskBinsFromTable : TODO: DESCRIPTION.
void init() override
Virtual method - must be overridden by concrete algorithm.
int id_xmin
Column indexes of XMin, XMax, SpectraList, DetectorIDsList.
void exec() override
Main execution body.
std::string convertToSpectraList(const API::MatrixWorkspace_sptr &dataws, const std::string &detidliststr)
Convert a list of detector IDs list (string) to a list of spectra/workspace indexes list.
std::vector< std::string > m_spectraVec
void maskBins(const API::MatrixWorkspace_sptr &dataws)
Call MaskBins.
std::vector< double > m_xminVec
Vector to store XMin, XMax and SpectraList.
void processMaskBinWorkspace(const DataObjects::TableWorkspace_sptr &masktblws, const API::MatrixWorkspace_sptr &dataws)
Process input Mask bin TableWorkspace.
Support for a property that holds an array of values.
Definition: ArrayProperty.h:28
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void debug(const std::string &msg)
Logs at debug level.
Definition: Logger.cpp:114
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
void warning(const std::string &msg)
Logs at warning level.
Definition: Logger.cpp:86
int size() const override
Return the size of this property.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< TableWorkspace > TableWorkspace_sptr
shared pointer to Mantid::DataObjects::TableWorkspace
int32_t detid_t
Typedef for a detector ID.
Definition: SpectrumInfo.h:21
std::unordered_map< detid_t, size_t > detid2index_map
Map with key = detector ID, value = workspace index.
STL namespace.
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54