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