17#include <boost/lexical_cast.hpp>
20#include <Poco/DOM/AutoPtr.h>
21#include <Poco/DOM/DOMParser.h>
22#include <Poco/DOM/Document.h>
23#include <Poco/DOM/Element.h>
24#include <Poco/DOM/NodeFilter.h>
25#include <Poco/DOM/NodeIterator.h>
26#include <Poco/DOM/NodeList.h>
28#include <Poco/SAX/InputSource.h>
35using Mantid::HistogramData::BinEdges;
36using Mantid::HistogramData::Counts;
59 "The filename of the runinfo file for a particular run. "
60 "Allowed Values are: _runinfo.xml");
64 "The workspace to load the monitors into.");
75 std::string instrumentName;
78 std::vector<std::string> monitorFilenames;
79 std::vector<int> monitorIDs;
86 Poco::Path runinfoPath(runinfo_filename, Poco::Path::PATH_GUESS);
88 Poco::Path dirPath(runinfoPath.parent());
93 std::ifstream in(runinfo_filename.c_str());
94 Poco::XML::InputSource src(in);
96 Poco::XML::DOMParser parser;
97 Poco::AutoPtr<Poco::XML::Document> pDoc = parser.parse(&src);
99 Poco::XML::NodeIterator it(pDoc, Poco::XML::NodeFilter::SHOW_ELEMENT);
100 Poco::XML::Node *pNode = it.nextNode();
104 if (pNode->nodeName() ==
"RunInfo") {
105 auto *pRunInfoElement =
static_cast<Poco::XML::Element *
>(pNode);
106 instrumentName = pRunInfoElement->getAttribute(
"instrument");
113 if (pNode->nodeName() ==
"BeamMonitorInfo") {
117 auto *pE =
static_cast<Poco::XML::Element *
>(pNode);
118 g_log.
debug() <<
"Beam Monitor " << pE->getAttribute(
"id") <<
'\n';
119 g_log.
debug() <<
"\tname: " << pE->getAttribute(
"name") <<
'\n';
120 g_log.
debug() <<
"\tdescription: " << pE->getAttribute(
"description") <<
'\n';
123 Poco::XML::Element *pTimeChannels = pE->getChildElement(
"NumTimeChannels");
124 tmin = boost::lexical_cast<double>(pTimeChannels->getAttribute(
"startbin"));
125 tstep = boost::lexical_cast<double>(pTimeChannels->getAttribute(
"width"));
130 if (pNode->nodeName() ==
"DataList") {
132 Poco::AutoPtr<Poco::XML::NodeList> pDataListChildren = pNode->childNodes();
133 for (
unsigned long i = 0; i < pDataListChildren->length(); ++i) {
135 if (pDataListChildren->item(i)->nodeName() ==
"monitor") {
136 auto *element =
static_cast<Poco::XML::Element *
>(pDataListChildren->item(i));
137 monitorIDs.emplace_back(boost::lexical_cast<int>(element->getAttribute(
"id")));
138 monitorFilenames.emplace_back(element->getAttribute(
"name"));
144 if (pNode->nodeName() ==
"FileFormats") {
146 Poco::AutoPtr<Poco::XML::NodeList> pDataListChildren = pNode->childNodes();
147 for (
unsigned long i = 0; i < pDataListChildren->length(); ++i) {
149 if (pDataListChildren->item(i)->nodeName() ==
"monitor") {
150 std::string dims =
static_cast<Poco::XML::Element *
>(pDataListChildren->item(i))->getAttribute(
"dims");
151 tchannels = boost::lexical_cast<int>(dims);
156 pNode = it.nextNode();
164 const int numberTimeBins = tchannels + 1;
166 for (
int i = 0; i < numberTimeBins; ++i) {
167 time_bins[i] = tmin + (i)*tstep;
174 BinEdges edges(time_bins);
177 Poco::Path pMonitorFilename(dirPath, monitorFilenames[i]);
179 g_log.
debug() <<
"Loading monitor file :" << pMonitorFilename.toString() <<
'\n';
185 localWorkspace->setHistogram(i, edges, Counts(buffer.begin(), buffer.end()));
188 auto &spectrum = localWorkspace->getSpectrum(i);
189 spectrum.setSpectrumNo(monitorIDs[i]);
190 spectrum.setDetectorID(-monitorIDs[i]);
193 g_log.
debug() <<
"Setting axis zero to TOF\n";
197 localWorkspace->setYUnit(
"Counts");
219 bool executionSuccessful(
true);
221 loadInst->setPropertyValue(
"InstrumentName", instrument);
223 loadInst->setProperty(
"RewriteSpectraMap",
229 localWorkspace->populateInstrumentParameters();
230 }
catch (std::invalid_argument &e) {
231 g_log.
information() <<
"Invalid argument to LoadInstrument Child Algorithm : " << e.what() <<
'\n';
232 executionSuccessful =
false;
233 }
catch (std::runtime_error &e) {
234 g_log.
information() <<
"Unable to successfully run LoadInstrument Child Algorithm : " << e.what() <<
'\n';
235 executionSuccessful =
false;
239 if (!executionSuccessful) {
240 g_log.
error() <<
"Error loading Instrument definition file\n";
#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.
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
A property class for workspaces.
bool instrument_loaded_correctly
Set to true when instrument geometry was loaded.
void init() override
Initialisation code.
void exec() override
Execution code.
int nMonitors
Number of monitors.
void runLoadInstrument(const std::string &instrument, const API::MatrixWorkspace_sptr &localWorkspace)
Load the instrument geometry File.
LoadPreNexusMonitors()
(Empty) Constructor
The BinaryFile template is a helper function for loading simple binary files.
std::vector< T > loadAllIntoVector()
Loads the entire contents of the file into a std::vector.
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 information(const std::string &msg)
Logs at information level.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
static const std::string RUNINFO_FILENAME("RunInfoFilename")
static const std::string WORKSPACE_OUT("OutputWorkspace")
Helper class which provides the Collimation Length for SANS instruments.
std::vector< double > MantidVec
typedef for the data storage used in Mantid matrix workspaces
@ Output
An output workspace.