29#include <Poco/DOM/DOMParser.h>
30#include <Poco/DOM/Element.h>
31#include <Poco/DOM/NodeFilter.h>
32#include <Poco/DOM/NodeIterator.h>
33#include <Poco/DOM/NodeList.h>
34#include <Poco/Exception.h>
36#include <boost/algorithm/string.hpp>
38using Poco::XML::DOMParser;
40using Poco::XML::NodeFilter;
41using Poco::XML::NodeIterator;
42using Poco::XML::NodeList;
63void convertToVector(
const std::vector<T> &singles,
const std::vector<T> &ranges, std::vector<T> &tot_singles) {
66 size_t n_total(singles.size() + tot_singles.size());
67 for (
size_t i = 0; i < ranges.size(); i += 2) {
68 n_total += ranges[i + 1] - ranges[i] + 1;
72 tot_singles.reserve(n_total);
74 tot_singles.insert(tot_singles.end(), singles.begin(), singles.end());
76 for (
size_t i = 0; i < ranges.size(); i += 2) {
77 for (T obj_id = ranges[i]; obj_id < ranges[i + 1] + 1; ++obj_id) {
78 tot_singles.emplace_back(obj_id);
91template <
typename T>
void parseRangeText(
const std::string &inputstr, std::vector<T> &singles, std::vector<T> &pairs) {
93 std::vector<std::string> rawstrings;
94 boost::split(rawstrings, inputstr, boost::is_any_of(
","), boost::token_compress_on);
96 for (
auto &rawstring : rawstrings) {
98 boost::trim(rawstring);
99 bool containDash(
true);
100 if (rawstring.find_first_of(
'-') == std::string::npos) {
106 std::vector<std::string> ptemp;
107 boost::split(ptemp, rawstring, boost::is_any_of(
"-"), boost::token_compress_on);
108 if (ptemp.size() != 2) {
109 std::string
error =
"Range string " + rawstring +
" has a wrong format!";
110 throw std::invalid_argument(
error);
113 auto intstart = boost::lexical_cast<T>(ptemp[0]);
114 auto intend = boost::lexical_cast<T>(ptemp[1]);
115 if (intstart >= intend) {
116 std::string
error =
"Range string " + rawstring +
" has wrong order of detectors ID!";
117 throw std::invalid_argument(
error);
119 pairs.emplace_back(intstart);
120 pairs.emplace_back(intend);
123 auto itemp = boost::lexical_cast<T>(rawstring);
124 singles.emplace_back(itemp);
137void parseISISStringToVector(
const std::string &ins, std::vector<Mantid::specnum_t> &ranges) {
139 std::vector<string> splitstrings;
140 boost::split(splitstrings, ins, boost::is_any_of(
" "), boost::token_compress_on);
143 bool tocontinue =
true;
147 if (
index == splitstrings.size() - 1) {
152 vector<string> temps;
153 boost::split(temps, splitstrings[
index], boost::is_any_of(
"-"), boost::token_compress_on);
154 if (splitstrings[
index] ==
"-" || temps.size() == 1) {
157 }
else if (temps.size() == 2) {
159 temps.insert(temps.begin() + 1,
"-");
160 splitstrings.erase(splitstrings.begin() +
index);
161 for (
size_t ic = 0; ic < 3; ic++) {
162 if (!temps[ic].empty()) {
163 splitstrings.insert(splitstrings.begin() +
index, temps[ic]);
169 std::string err =
"String " + splitstrings[
index] +
" has too many '-'";
170 throw std::invalid_argument(err);
173 if (
index >= splitstrings.size())
183 ranges.emplace_back(boost::lexical_cast<Mantid::specnum_t>(splitstrings[
index]));
186 if (
index == splitstrings.size() - 1 || splitstrings[
index + 1] !=
"-") {
188 ranges.emplace_back(boost::lexical_cast<Mantid::specnum_t>(splitstrings[
index]));
192 ranges.emplace_back(boost::lexical_cast<Mantid::specnum_t>(splitstrings[
index + 2]));
196 if (
index >= splitstrings.size())
205void loadISISMaskFile(
const std::string &isisfilename, std::vector<Mantid::specnum_t> &spectraMasks) {
207 std::vector<Mantid::specnum_t> ranges;
210 ifs.open(isisfilename, std::ios::in);
211 if (!ifs.is_open()) {
212 throw std::invalid_argument(
"Cannot open ISIS mask file" + isisfilename);
215 std::string isisline;
216 while (getline(ifs, isisline)) {
217 boost::trim(isisline);
220 if (isisline.empty())
224 if (isisline.c_str()[0] <
'0' || isisline.c_str()[0] >
'9')
228 parseISISStringToVector(isisline, ranges);
232 std::vector<Mantid::specnum_t> dummy;
233 convertToVector(dummy, ranges, spectraMasks);
247 "The name of the instrument to apply the mask.");
249 const std::vector<std::string> maskExts{
".xml",
".msk"};
250 declareProperty(std::make_unique<FileProperty>(
"InputFile",
"",
FileProperty::Load, maskExts),
251 "Masking file for masking. Supported file format is XML and "
255 "The name of the workspace wich defines instrument and spectra, "
256 "used as the source of the spectra-detector map for the mask to load. "
257 "The instrument, attached to this workspace has to be the same as the "
258 "one specified by 'Instrument' property");
262 "Output Masking Workspace");
272 const std::string instrumentname =
getProperty(
"Instrument");
281 auto t_inst_name =
m_maskWS->getInstrument()->getName();
282 auto r_inst_name =
m_sourceMapWS->getInstrument()->getName();
283 if (t_inst_name != r_inst_name) {
284 throw std::invalid_argument(
"If reference workspace is provided, it has "
285 "to have instrument with the same name as "
286 "specified by 'Instrument' property");
297 if (filename.ends_with(
"l") || filename.ends_with(
"L")) {
301 }
else if (filename.ends_with(
"k") || filename.ends_with(
"K")) {
306 g_log.
error() <<
"File " << filename <<
" is not in supported format. \n";
336 size_t numHist =
m_maskWS->getNumberHistograms();
337 for (
size_t wkspIndex = 0; wkspIndex < numHist; wkspIndex++) {
338 m_maskWS->setMaskedIndex(wkspIndex);
351 const std::vector<detid_t> &singledetids) {
354 g_log.
debug() <<
"Mask = " << tomask <<
" Final Single IDs Size = " << singledetids.size() <<
'\n';
356 for (
auto detid : singledetids) {
357 detid2index_map::const_iterator it;
358 it = indexmap.find(detid);
359 if (it != indexmap.end()) {
360 size_t index = it->second;
363 g_log.
warning() <<
"Pixel w/ ID = " << detid <<
" Cannot Be Located\n";
377 const auto &componentInfo =
m_maskWS->componentInfo();
378 const auto &detectorInfo =
m_maskWS->detectorInfo();
380 for (
auto &componentname : componentnames) {
381 g_log.
debug() <<
"Component name = " << componentname <<
'\n';
384 size_t componentIndex;
386 componentIndex = componentInfo.indexOfAny(componentname);
387 g_log.
debug() <<
"Component ID = " << componentInfo.componentID(componentIndex) <<
'\n';
388 }
catch (
const std::exception &) {
390 g_log.
warning() <<
"Component " << componentname <<
" does not exist!\n";
395 const auto detectorIndices = componentInfo.detectorsInSubtree(componentIndex);
397 g_log.
debug() <<
"Number of Children = " << detectorIndices.size() <<
'\n';
400 detid_t id_min(std::numeric_limits<Mantid::detid_t>::max());
403 for (
const auto &detIndex : detectorIndices) {
404 detid_t detid = detectorInfo.detectorIDs()[detIndex];
405 detectors.emplace_back(detid);
413 g_log.
debug() <<
"Number of Detectors in Children = " << numdets <<
" Range = " << id_min <<
", " << id_max
425 std::stringstream infoss;
426 infoss <<
"Bank IDs to be converted to detectors: \n";
427 for (
const auto &singlebank : singlebanks) {
428 infoss <<
"Bank: " << singlebank <<
'\n';
434 for (
auto &singlebank : singlebanks) {
435 std::vector<Geometry::IDetector_const_sptr> idetectors;
437 instrument->getDetectorsInBank(idetectors, singlebank);
438 g_log.
debug() <<
"Bank: " << singlebank <<
" has " << idetectors.size() <<
" detectors\n";
441 size_t numdets = idetectors.size();
442 detid_t detid_first = idetectors.front()->getID();
443 detid_t detid_last = idetectors.back()->getID();
447 for (
const auto &det : idetectors) {
449 detectors.emplace_back(detid);
451 g_log.
debug() <<
"Number of Detectors in Bank " << singlebank <<
" is: " << numdets
452 <<
"\nRange From: " << detid_first <<
" To: " << detid_last <<
'\n';
466 std::vector<int32_t> &singleDetIds) {
468 if (maskedSpecID.empty())
474 maskedSpecID.clear();
480 spec2index_map::const_iterator s2iter;
483 auto spec0 = maskedSpecID[0];
484 auto prev_masks = spec0;
485 for (
int spec2mask : maskedSpecID) {
487 s2iter = s2imap.find(spec2mask);
488 if (s2iter == s2imap.end()) {
490 g_log.
error() <<
"Spectrum " << spec2mask <<
" does not have an entry in GroupWorkspace's spec2index map\n";
491 throw std::runtime_error(
"Logic error");
493 size_t wsindex = s2iter->second;
494 if (wsindex >=
m_maskWS->getNumberHistograms()) {
496 g_log.
error() <<
"Group workspace's spec2index map is set wrong: "
497 <<
" Found workspace index = " << wsindex <<
" for spectrum No " << spec2mask
498 <<
" with workspace size = " <<
m_maskWS->getNumberHistograms() <<
'\n';
501 m_maskWS->mutableY(wsindex)[0] = (mask) ? 1.0 : 0.0;
505 if (spec2mask > prev_masks + 1) {
506 g_log.
debug() <<
"Masked Spectrum " << spec0 <<
" To " << prev_masks <<
'\n';
525 m_pDoc = pParser.parseString(xmlText);
526 }
catch (Poco::Exception &exc) {
534 g_log.
error(
"XML file: " + filename +
"contains no root element.");
564 throw std::runtime_error(
"Call LoadMask::initialize() before parseXML.");
567 Poco::AutoPtr<NodeList> pNL_type =
m_pRootElem->getElementsByTagName(
"type");
570 Poco::XML::NodeIterator it(
m_pDoc, Poco::XML::NodeFilter::SHOW_ELEMENT);
571 Poco::XML::Node *pNode = it.nextNode();
573 std::vector<specnum_t> singleSp, pairSp;
574 std::vector<detid_t> maskSingleDet, maskPairDet;
576 bool ingroup =
false;
578 const Poco::XML::XMLString
value = pNode->innerText();
580 if (pNode->nodeName() ==
"group") {
584 }
else if (pNode->nodeName() ==
"component") {
589 g_log.
error() <<
"XML File hierarchical (component) error!\n";
592 }
else if (pNode->nodeName() ==
"ids") {
595 parseRangeText(
value, singleSp, pairSp);
597 g_log.
error() <<
"XML File (ids) hierarchical error!"
598 <<
" Inner Text = " << pNode->innerText() <<
'\n';
601 }
else if (pNode->nodeName() ==
"detids") {
604 parseRangeText(
value, maskSingleDet, maskPairDet);
606 g_log.
error() <<
"XML File (detids) hierarchical error!\n";
609 }
else if (pNode->nodeName() ==
"detector-masking") {
614 pNode = it.nextNode();
618 convertToVector(maskSingleDet, maskPairDet,
m_maskDetID);
633 std::vector<int32_t> &singleDetIds) {
638 std::multimap<size_t, Mantid::detid_t> spectr2index_map;
639 for (
auto &it : sourceDetMap) {
640 spectr2index_map.insert(std::pair<size_t, Mantid::detid_t>(it.second, it.first));
642 for (
int i : maskedSpecID) {
644 const auto itSpec = s2imap.find(i);
645 if (itSpec == s2imap.end()) {
646 throw std::runtime_error(
"Can not find spectra with ID: " + boost::lexical_cast<std::string>(i) +
647 " in the workspace" + sourceWS.
getName());
649 size_t specN = itSpec->second;
652 const auto source_range = spectr2index_map.equal_range(specN);
653 if (source_range.first == spectr2index_map.end()) {
654 throw std::runtime_error(
"Can not find spectra N: " + boost::lexical_cast<std::string>(specN) +
655 " in the workspace" + sourceWS.
getName());
658 for (
auto it = source_range.first; it != source_range.second; ++it) {
659 singleDetIds.emplace_back(it->second);
673 const bool ignoreDirs(
true);
685 loadInst->executeAsChildAlg();
687 if (!loadInst->isExecuted()) {
689 throw std::invalid_argument(
"Incorrect instrument name or invalid IDF given.");
703 std::map<std::string, std::string> result;
708 boost::trim(InstrName);
709 boost::algorithm::to_lower(InstrName);
710 size_t len = InstrName.size();
713 bool IDF_provided{
false};
717 if (InstrName.compare(len - 4, len,
".xml") == 0) {
720 IDF_provided =
false;
723 IDF_provided =
false;
726 auto inst = inputWS->getInstrument();
727 std::string Name = inst->getName();
728 boost::algorithm::to_lower(Name);
729 if (Name != InstrName && !IDF_provided) {
730 result[
"RefWorkspace"] =
"If both reference workspace and instrument name are defined, "
731 "workspace has to have the instrument with the same name\n"
732 "'Instrument' value: " +
733 InstrName +
" Workspace Instrument name: " + Name;
736 result[
"RefWorkspace"] =
"If reference workspace is defined, it mast have an instrument";
#define DECLARE_ALGORITHM(classname)
double value
The value of the point.
std::map< DeltaEMode::Type, std::string > index
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.
@ Load
allowed here which will be passed to the algorithm
Base MatrixWorkspace Abstract Class.
detid2index_map getDetectorIDToWorkspaceIndexMap(bool throwIfMultipleDets=false, bool ignoreIfNoValidDets=false) const
Return a map where: KEY is the DetectorID (pixel ID) VALUE is the Workspace Index.
spec2index_map getSpectrumToWorkspaceIndexMap() const
Return a map where: KEY is the Spectrum # VALUE is the Workspace Index.
A property class for workspaces.
const std::string & getName() const override
Get the workspace name.
LoadMask : Load masking file to generate a SpecialWorkspace2D object (masking workspace).
DataObjects::MaskWorkspace_sptr m_maskWS
Mask Workspace.
void processMaskOnWorkspaceIndex(bool mask, std::vector< specnum_t > &maskedSpecID, std::vector< detid_t > &singleDetIds)
Convert spectrum to detector.
void processMaskOnDetectors(const detid2index_map &indexmap, bool tomask, const std::vector< detid_t > &singledetids)
Mask detectors or Unmask detectors.
void convertSpMasksToDetIDs(const API::MatrixWorkspace &sourceWS, const std::vector< specnum_t > &maskedSpecID, std::vector< detid_t > &singleDetIds)
std::vector< std::string > m_uMaskCompIdSingle
Poco::XML::Element * m_pRootElem
Root element of the parsed XML.
void componentToDetectors(const std::vector< std::string > &componentnames, std::vector< detid_t > &detectors)
Convert component to detectors.
std::map< std::string, std::string > validateInputs() override
Validates if either input workspace or instrument name is defined.
std::string m_instrumentPropValue
Instrument name.
std::vector< std::string > m_maskCompIdSingle
std::vector< detid_t > m_maskDetID
bool m_defaultToUse
Default setup. If true, not masking, but use the pixel.
void exec() override
Run the algorithm.
Poco::AutoPtr< Poco::XML::Document > m_pDoc
XML document loaded.
std::vector< specnum_t > m_maskSpecID
void intializeMaskWorkspace()
Initialize a Mask Workspace.
void initializeXMLParser(const std::string &filename)
Initialize XML parser.
void parseXML()
Parse XML.
void bankToDetectors(const std::vector< std::string > &singlebanks, std::vector< detid_t > &detectors)
Convert bank to detector.
std::vector< detid_t > m_unMaskDetID
API::MatrixWorkspace_sptr m_sourceMapWS
optional source workspace, containing spectra-detector mapping
Concrete workspace implementation.
Records the filename and the description of failure.
Exception for errors associated with the instrument definition.
Exception for when an item is not found in a collection.
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.
void information(const std::string &msg)
Logs at information level.
Validator to check that a property is not left empty.
OptionalBool : Tri-state bool.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< MaskWorkspace > MaskWorkspace_sptr
shared pointer to the MaskWorkspace class
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
MANTID_KERNEL_DLL std::string loadFile(const std::string &filename)
Loads the entire contents of a text file into a string.
std::unordered_map< specnum_t, size_t > spec2index_map
Map with key = spectrum number, value = workspace index.
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.