16#include <boost/algorithm/string/predicate.hpp>
33 :
API::
Algorithm(), id_xmin(-1), id_xmax(-1), id_spec(-1), id_dets(-1), m_useDetectorID(false),
34 m_useSpectrumID(false) {}
39 std::make_shared<HistogramValidator>()),
40 "Input Workspace to mask bins. ");
42 "Output Workspace with bins masked.");
45 "Input TableWorkspace containing parameters, XMin and "
46 "XMax and either SpectraList or DetectorIDsList");
68 bool firstloop =
true;
73 g_log.
debug() <<
"There will be " << numcalls <<
" calls to MaskBins"
75 for (
size_t ib = 0; ib < numcalls; ++ib) {
78 maskbins->initialize();
85 maskbins->setProperty(
"InputWorkspace", dataws);
89 throw runtime_error(
"Programming logic error.");
90 maskbins->setProperty(
"InputWorkspace", outputws);
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]);
97 bool isexec = maskbins->execute();
100 errmsg <<
"MaskBins() is not executed for row " << ib <<
"\n";
102 throw std::runtime_error(errmsg.str());
104 g_log.
debug(
"MaskBins() is executed successfully.");
107 outputws = maskbins->getProperty(
"OutputWorkspace");
110 errmsg <<
"OutputWorkspace cannot be obtained from algorithm row " << ib <<
". ";
112 throw std::runtime_error(errmsg.str());
132 throw std::invalid_argument(
"Input workspace is not a table workspace.");
133 g_log.
debug() <<
"Lines of parameters workspace = " << masktblws->rowCount() <<
'\n';
136 vector<std::string> colnames = masktblws->getColumnNames();
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")
151 else if (colname ==
"xmax")
153 else if (boost::algorithm::starts_with(colname,
"spec")) {
155 }
else if (boost::algorithm::starts_with(colname,
"detectorid")) {
158 g_log.
warning() <<
"In TableWorkspace " << masktblws->getName() <<
", column " << i <<
" with name " << colname
159 <<
" is not used by MaskBinsFromTable.";
164 throw runtime_error(
"Either Xmin nor Xmax is not given. ");
166 throw runtime_error(
"Neither SpectraList nor DetectorIDList is given.");
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));
180 spectralist = masktblws->cell<
string>(i,
static_cast<size_t>(
id_spec));
183 string detidslist = masktblws->cell<
string>(i,
static_cast<size_t>(
id_dets));
187 g_log.
debug() <<
"Row " << i <<
" XMin = " << xmin <<
" XMax = " << xmax <<
" SpectraList = " << spectralist
205 const std::string &detidliststr) {
207 vector<int> detidvec;
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.";
217 vector<size_t> wsindexvec;
219 detid2index_map refermap = dataws->getDetectorIDToWorkspaceIndexMap(
false);
220 for (
size_t i = 0; i < numitems; ++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);
227 g_log.
warning() <<
"Detector ID " << detid <<
" cannot be mapped to any workspace index/spectrum."
233 if (wsindexvec.empty())
234 throw runtime_error(
"There is no spectrum found for input detectors list.");
236 sort(wsindexvec.begin(), wsindexvec.end());
240 size_t headid = wsindexvec[0];
241 size_t previd = wsindexvec[0];
243 for (
size_t i = 1; i < wsindexvec.size(); ++i) {
244 size_t currid = wsindexvec[i];
245 if (currid > previd + 1) {
247 if (headid == previd)
248 rss << headid <<
", ";
250 rss << headid <<
"-" << previd <<
", ";
262 if (headid == previd)
265 rss << headid <<
"-" << previd;
267 string spectraliststr(rss.str());
269 return spectraliststr;
#define DECLARE_ALGORITHM(classname)
Base class from which all concrete algorithm classes should be derived.
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.
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.
std::vector< double > m_xmaxVec
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.
void error(const std::string &msg)
Logs at error level.
void warning(const std::string &msg)
Logs at warning level.
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.
std::unordered_map< detid_t, size_t > detid2index_map
Map with key = detector ID, value = workspace index.
@ Input
An input workspace.
@ Output
An output workspace.