From 25c42c00adb1e8fc446cdb2b071ed1727992c6a8 Mon Sep 17 00:00:00 2001 From: "Ricardo M. Ferraz Leal" <ricleal@gmail.com> Date: Mon, 9 Nov 2015 13:02:56 -0500 Subject: [PATCH] Re #14318 After clang-format patch --- .../inc/MantidDataHandling/LoadSpice2D.h | 146 ++-- .../inc/MantidDataHandling/XmlHandler.h | 18 +- Framework/DataHandling/src/LoadSpice2D.cpp | 715 +++++++++--------- Framework/DataHandling/src/XmlHandler.cpp | 105 ++- Framework/DataHandling/test/LoadSpice2dTest.h | 36 +- 5 files changed, 521 insertions(+), 499 deletions(-) diff --git a/Framework/DataHandling/inc/MantidDataHandling/LoadSpice2D.h b/Framework/DataHandling/inc/MantidDataHandling/LoadSpice2D.h index c24ea1346c5..8ffde7606cb 100644 --- a/Framework/DataHandling/inc/MantidDataHandling/LoadSpice2D.h +++ b/Framework/DataHandling/inc/MantidDataHandling/LoadSpice2D.h @@ -47,84 +47,80 @@ namespace DataHandling { File change history is stored at: <https://github.com/mantidproject/mantid>. Code Documentation is available at: <http://doxygen.mantidproject.org> */ -class DLLExport LoadSpice2D: public API::IFileLoader<Kernel::FileDescriptor> { +class DLLExport LoadSpice2D : public API::IFileLoader<Kernel::FileDescriptor> { public: - /// default constructor - LoadSpice2D(); - /// destructor - ~LoadSpice2D(); - /// Algorithm's name for identification overriding a virtual method - virtual const std::string name() const { - return "LoadSpice2D"; - } - /// Summary of algorithms purpose - virtual const std::string summary() const { - return "Loads a SANS data file produce by the HFIR instruments at ORNL. " - "The instrument geometry is also loaded. The center of the detector " - "is placed at (0,0,D), where D is the sample-to-detector distance."; - } - - /// Algorithm's version for identification overriding a virtual method - virtual int version() const { - return 1; - } - /// Algorithm's category for identification overriding a virtual method - virtual const std::string category() const { - return "DataHandling\\Text;SANS"; - } - /// Number of monitors - static const int nMonitors = 2; - - /// Returns a confidence value that this algorithm can load a file - virtual int confidence(Kernel::FileDescriptor &descriptor) const; + /// default constructor + LoadSpice2D(); + /// destructor + ~LoadSpice2D(); + /// Algorithm's name for identification overriding a virtual method + virtual const std::string name() const { return "LoadSpice2D"; } + /// Summary of algorithms purpose + virtual const std::string summary() const { + return "Loads a SANS data file produce by the HFIR instruments at ORNL. " + "The instrument geometry is also loaded. The center of the detector " + "is placed at (0,0,D), where D is the sample-to-detector distance."; + } + + /// Algorithm's version for identification overriding a virtual method + virtual int version() const { return 1; } + /// Algorithm's category for identification overriding a virtual method + virtual const std::string category() const { + return "DataHandling\\Text;SANS"; + } + /// Number of monitors + static const int nMonitors = 2; + + /// Returns a confidence value that this algorithm can load a file + virtual int confidence(Kernel::FileDescriptor &descriptor) const; private: - /// Overwrites Algorithm method. - void init(); - /// Overwrites Algorithm method - void exec(); - - /// This method throws not found error if a element is not found in the xml - /// file - void throwException(Poco::XML::Element *elem, const std::string &name, - const std::string &fileName); - /// Run LoadInstrument Child Algorithm - void runLoadInstrument(const std::string &inst_name, - DataObjects::Workspace2D_sptr localWorkspace); - /// Run the LoadMappingTable Child Algorithm to fill the SpectraToDetectorMap - void runLoadMappingTable(DataObjects::Workspace2D_sptr localWorkspace, - int nxbins, int nybins); - - void setInputPropertiesAsMemberProperties(); - - void addMetadataAsRunProperties(const std::map<std::string, std::string> &); - void parseDetectorDimensions(const std::string&); - void createWorkspace(); - std::vector<int> getData(const std::string &); - void createWorkspace(const std::vector<int> &data, const std::string &title, - double monitor1_counts, double monitor2_counts); - void setWavelength(std::map<std::string, std::string> &metadata); - template<class T> - T addRunProperty(std::map<std::string, std::string> &metadata, - const std::string &oldName, const std::string &newName, - const std::string &units=""); - template<class T> - void addRunProperty(const std::string &name, const T &value, - const std::string &units = ""); - void setBeamTrapRunProperty(std::map<std::string, std::string> &metadata); - void moveDetector(double sample_detector_distance); - double totalDetectorDistance(std::map<std::string, std::string> &metadata); - void setMetadataAsRunProperties(std::map<std::string, std::string> &metadata); - - // Member variables: - DataObjects::Workspace2D_sptr m_workspace; - double m_wavelength_input; - double m_wavelength_spread_input; - Mantid::DataHandling::XmlHandler m_xmlHandler; - int m_numberXPixels; - int m_numberYPixels; - double m_wavelength; - double m_dwavelength; + /// Overwrites Algorithm method. + void init(); + /// Overwrites Algorithm method + void exec(); + + /// This method throws not found error if a element is not found in the xml + /// file + void throwException(Poco::XML::Element *elem, const std::string &name, + const std::string &fileName); + /// Run LoadInstrument Child Algorithm + void runLoadInstrument(const std::string &inst_name, + DataObjects::Workspace2D_sptr localWorkspace); + /// Run the LoadMappingTable Child Algorithm to fill the SpectraToDetectorMap + void runLoadMappingTable(DataObjects::Workspace2D_sptr localWorkspace, + int nxbins, int nybins); + + void setInputPropertiesAsMemberProperties(); + + void addMetadataAsRunProperties(const std::map<std::string, std::string> &); + void parseDetectorDimensions(const std::string &); + void createWorkspace(); + std::vector<int> getData(const std::string &); + void createWorkspace(const std::vector<int> &data, const std::string &title, + double monitor1_counts, double monitor2_counts); + void setWavelength(std::map<std::string, std::string> &metadata); + template <class T> + T addRunProperty(std::map<std::string, std::string> &metadata, + const std::string &oldName, const std::string &newName, + const std::string &units = ""); + template <class T> + void addRunProperty(const std::string &name, const T &value, + const std::string &units = ""); + void setBeamTrapRunProperty(std::map<std::string, std::string> &metadata); + void moveDetector(double sample_detector_distance); + double totalDetectorDistance(std::map<std::string, std::string> &metadata); + void setMetadataAsRunProperties(std::map<std::string, std::string> &metadata); + + // Member variables: + DataObjects::Workspace2D_sptr m_workspace; + double m_wavelength_input; + double m_wavelength_spread_input; + Mantid::DataHandling::XmlHandler m_xmlHandler; + int m_numberXPixels; + int m_numberYPixels; + double m_wavelength; + double m_dwavelength; }; } } diff --git a/Framework/DataHandling/inc/MantidDataHandling/XmlHandler.h b/Framework/DataHandling/inc/MantidDataHandling/XmlHandler.h index 99f03f338cb..2ffac86eef1 100644 --- a/Framework/DataHandling/inc/MantidDataHandling/XmlHandler.h +++ b/Framework/DataHandling/inc/MantidDataHandling/XmlHandler.h @@ -30,19 +30,19 @@ namespace DataHandling { class XmlHandler { public: - XmlHandler(); - XmlHandler(std::string); - virtual ~XmlHandler(); + XmlHandler(); + XmlHandler(std::string); + virtual ~XmlHandler(); - std::map<std::string, std::string> get_metadata(const std::string &tag_to_ignore="Detector"); - std::string get_text_from_tag(const std::string &); - std::map<std::string, std::string> get_attributes_from_tag(const std::string &); + std::map<std::string, std::string> + get_metadata(const std::string &tag_to_ignore = "Detector"); + std::string get_text_from_tag(const std::string &); + std::map<std::string, std::string> + get_attributes_from_tag(const std::string &); private: - Poco::AutoPtr<Poco::XML::Document> pDoc; - + Poco::AutoPtr<Poco::XML::Document> pDoc; }; - } /* namespace DataHandling */ } /* namespace Mantid */ diff --git a/Framework/DataHandling/src/LoadSpice2D.cpp b/Framework/DataHandling/src/LoadSpice2D.cpp index ac2db85994e..09b352e43c8 100644 --- a/Framework/DataHandling/src/LoadSpice2D.cpp +++ b/Framework/DataHandling/src/LoadSpice2D.cpp @@ -45,14 +45,13 @@ namespace DataHandling { DECLARE_FILELOADER_ALGORITHM(LoadSpice2D) // Parse string and convert to numeric type -template<class T> +template <class T> bool from_string(T &t, const std::string &s, - std::ios_base &(*f)(std::ios_base &)) { - std::istringstream iss(s); - return !(iss >> f >> t).fail(); + std::ios_base &(*f)(std::ios_base &)) { + std::istringstream iss(s); + return !(iss >> f >> t).fail(); } - /** * Convenience function to store a detector value into a given spectrum. * Note that this type of data doesn't use TOD, so that we use a single dummy @@ -65,17 +64,17 @@ bool from_string(T &t, const std::string &s, * @param dwavelength: error on the wavelength [Angstrom] */ void store_value(DataObjects::Workspace2D_sptr ws, int specID, double value, - double error, double wavelength, double dwavelength) { - MantidVec &X = ws->dataX(specID); - MantidVec &Y = ws->dataY(specID); - MantidVec &E = ws->dataE(specID); - // The following is mostly to make Mantid happy by defining a histogram with - // a single bin around the neutron wavelength - X[0] = wavelength - dwavelength / 2.0; - X[1] = wavelength + dwavelength / 2.0; - Y[0] = value; - E[0] = error; - ws->getSpectrum(specID)->setSpectrumNo(specID); + double error, double wavelength, double dwavelength) { + MantidVec &X = ws->dataX(specID); + MantidVec &Y = ws->dataY(specID); + MantidVec &E = ws->dataE(specID); + // The following is mostly to make Mantid happy by defining a histogram with + // a single bin around the neutron wavelength + X[0] = wavelength - dwavelength / 2.0; + X[1] = wavelength + dwavelength / 2.0; + Y[0] = value; + E[0] = error; + ws->getSpectrum(specID)->setSpectrumNo(specID); } /** @@ -85,73 +84,67 @@ void store_value(DataObjects::Workspace2D_sptr ws, int specID, double value, * be used */ int LoadSpice2D::confidence(Kernel::FileDescriptor &descriptor) const { - if (descriptor.extension().compare(".xml") != 0) - return 0; - - std::istream &is = descriptor.data(); - int confidence(0); - - { // start of inner scope - Poco::XML::InputSource src(is); - // Set up the DOM parser and parse xml file - DOMParser pParser; - Poco::AutoPtr<Document> pDoc; - try { - pDoc = pParser.parse(&src); - } catch (...) { - throw Kernel::Exception::FileError("Unable to parse File:", - descriptor.filename()); - } - // Get pointer to root element - Element *pRootElem = pDoc->documentElement(); - if (pRootElem) { - if (pRootElem->tagName().compare("SPICErack") == 0) { - confidence = 80; - } - } - } // end of inner scope - - return confidence; + if (descriptor.extension().compare(".xml") != 0) + return 0; + + std::istream &is = descriptor.data(); + int confidence(0); + + { // start of inner scope + Poco::XML::InputSource src(is); + // Set up the DOM parser and parse xml file + DOMParser pParser; + Poco::AutoPtr<Document> pDoc; + try { + pDoc = pParser.parse(&src); + } catch (...) { + throw Kernel::Exception::FileError("Unable to parse File:", + descriptor.filename()); + } + // Get pointer to root element + Element *pRootElem = pDoc->documentElement(); + if (pRootElem) { + if (pRootElem->tagName().compare("SPICErack") == 0) { + confidence = 80; + } + } + } // end of inner scope + + return confidence; } /// Constructor -LoadSpice2D::LoadSpice2D() : - m_wavelength_input(0), m_wavelength_spread_input(0), - m_numberXPixels(0), m_numberYPixels(0), - m_wavelength(0), m_dwavelength(0){ -} +LoadSpice2D::LoadSpice2D() + : m_wavelength_input(0), m_wavelength_spread_input(0), m_numberXPixels(0), + m_numberYPixels(0), m_wavelength(0), m_dwavelength(0) {} /// Destructor -LoadSpice2D::~LoadSpice2D() { -} +LoadSpice2D::~LoadSpice2D() {} /// Overwrites Algorithm Init method. void LoadSpice2D::init() { - declareProperty( - new API::FileProperty("Filename", "", API::FileProperty::Load, - ".xml"), "The name of the input xml file to load"); - declareProperty( - new API::WorkspaceProperty<API::Workspace>("OutputWorkspace", "", - Kernel::Direction::Output), - "The name of the Output workspace"); - - // Optionally, we can specify the wavelength and wavelength spread and - // overwrite - // the value in the data file (used when the data file is not populated) - auto mustBePositive = - boost::make_shared<Kernel::BoundedValidator<double>>(); - mustBePositive->setLower(0.0); - declareProperty("Wavelength", EMPTY_DBL(), mustBePositive, - "Optional wavelength value to use when loading the data file " - "(Angstrom). This value will be used instead of the value " - "found in the data file."); - declareProperty("WavelengthSpread", 0.1, mustBePositive, - "Optional wavelength spread value to use when loading the " - "data file (Angstrom). This value will be used instead of " - "the value found in the data file."); + declareProperty( + new API::FileProperty("Filename", "", API::FileProperty::Load, ".xml"), + "The name of the input xml file to load"); + declareProperty(new API::WorkspaceProperty<API::Workspace>( + "OutputWorkspace", "", Kernel::Direction::Output), + "The name of the Output workspace"); + + // Optionally, we can specify the wavelength and wavelength spread and + // overwrite + // the value in the data file (used when the data file is not populated) + auto mustBePositive = boost::make_shared<Kernel::BoundedValidator<double>>(); + mustBePositive->setLower(0.0); + declareProperty("Wavelength", EMPTY_DBL(), mustBePositive, + "Optional wavelength value to use when loading the data file " + "(Angstrom). This value will be used instead of the value " + "found in the data file."); + declareProperty("WavelengthSpread", 0.1, mustBePositive, + "Optional wavelength spread value to use when loading the " + "data file (Angstrom). This value will be used instead of " + "the value found in the data file."); } - /* * Main method. * Creates an XML handler. All tag values will be a map. @@ -160,32 +153,32 @@ void LoadSpice2D::init() { */ void LoadSpice2D::exec() { - setInputPropertiesAsMemberProperties(); - std::map<std::string, std::string> metadata = m_xmlHandler.get_metadata("Detector"); - setWavelength(metadata); + setInputPropertiesAsMemberProperties(); + std::map<std::string, std::string> metadata = + m_xmlHandler.get_metadata("Detector"); + setWavelength(metadata); - std::vector<int> data = getData("//Data/Detector" ); - double monitorCounts = 0; - from_string<double>(monitorCounts, metadata["Counters/monitor"], std::dec); - double countingTime = 0; - from_string<double>(countingTime, metadata["Counters/time"], std::dec); - createWorkspace(data, metadata["Header/Scan_Title"], monitorCounts, countingTime ); + std::vector<int> data = getData("//Data/Detector"); + double monitorCounts = 0; + from_string<double>(monitorCounts, metadata["Counters/monitor"], std::dec); + double countingTime = 0; + from_string<double>(countingTime, metadata["Counters/time"], std::dec); + createWorkspace(data, metadata["Header/Scan_Title"], monitorCounts, + countingTime); - // Add all metadata to the WS - addMetadataAsRunProperties(metadata); + // Add all metadata to the WS + addMetadataAsRunProperties(metadata); - setMetadataAsRunProperties(metadata); - - // run load instrument - std::string instrument = metadata["Header/Instrument"]; - runLoadInstrument(instrument, m_workspace); - runLoadMappingTable(m_workspace, m_numberXPixels, m_numberYPixels); - - // sample_detector_distances - double detector_distance = totalDetectorDistance(metadata); - moveDetector(detector_distance); + setMetadataAsRunProperties(metadata); + // run load instrument + std::string instrument = metadata["Header/Instrument"]; + runLoadInstrument(instrument, m_workspace); + runLoadMappingTable(m_workspace, m_numberXPixels, m_numberYPixels); + // sample_detector_distances + double detector_distance = totalDetectorDistance(metadata); + moveDetector(detector_distance); } /** @@ -194,17 +187,16 @@ void LoadSpice2D::exec() { */ void LoadSpice2D::parseDetectorDimensions(const std::string &dims_str) { - // Read in the detector dimensions from the Detector tag - - boost::regex b_re_sig("INT\\d+\\[(\\d+),(\\d+)\\]"); - if (boost::regex_match(dims_str, b_re_sig)) { - boost::match_results<std::string::const_iterator> match; - boost::regex_search(dims_str, match, b_re_sig); - // match[0] is the full string - Kernel::Strings::convert(match[1], m_numberXPixels); - Kernel::Strings::convert(match[2], m_numberYPixels); - } + // Read in the detector dimensions from the Detector tag + boost::regex b_re_sig("INT\\d+\\[(\\d+),(\\d+)\\]"); + if (boost::regex_match(dims_str, b_re_sig)) { + boost::match_results<std::string::const_iterator> match; + boost::regex_search(dims_str, match, b_re_sig); + // match[0] is the full string + Kernel::Strings::convert(match[1], m_numberXPixels); + Kernel::Strings::convert(match[2], m_numberYPixels); + } } /** @@ -212,14 +204,13 @@ void LoadSpice2D::parseDetectorDimensions(const std::string &dims_str) { * as Workspace run properties */ void LoadSpice2D::addMetadataAsRunProperties( - const std::map<std::string, std::string> &metadata) { - - for (auto it = metadata.begin(); it != metadata.end(); it++) { - std::string key = it->first; - std::replace( key.begin(), key.end(), '/', '_'); - m_workspace->mutableRun().addProperty(key, it->second, true); - } + const std::map<std::string, std::string> &metadata) { + for (auto it = metadata.begin(); it != metadata.end(); it++) { + std::string key = it->first; + std::replace(key.begin(), key.end(), '/', '_'); + m_workspace->mutableRun().addProperty(key, it->second, true); + } } /** @@ -227,255 +218,284 @@ void LoadSpice2D::addMetadataAsRunProperties( */ void LoadSpice2D::setInputPropertiesAsMemberProperties() { - m_wavelength_input = getProperty("Wavelength"); - m_wavelength_spread_input = getProperty("WavelengthSpread"); + m_wavelength_input = getProperty("Wavelength"); + m_wavelength_spread_input = getProperty("WavelengthSpread"); - g_log.debug() << "setInputPropertiesAsMemberProperties: "<< m_wavelength_input << " , " << m_wavelength_input << std::endl; + g_log.debug() << "setInputPropertiesAsMemberProperties: " + << m_wavelength_input << " , " << m_wavelength_input + << std::endl; - std::string fileName = getPropertyValue("Filename"); - // Set up the XmlHandler handler and parse xml file - try { - m_xmlHandler = XmlHandler(fileName); - } catch (...) { - throw Kernel::Exception::FileError("Unable to parse File:", fileName); - } + std::string fileName = getPropertyValue("Filename"); + // Set up the XmlHandler handler and parse xml file + try { + m_xmlHandler = XmlHandler(fileName); + } catch (...) { + throw Kernel::Exception::FileError("Unable to parse File:", fileName); + } } /** * Gets the wavelenght and wavelength spread from the metadata * and sets them as class attributes */ -void LoadSpice2D::setWavelength(std::map<std::string, std::string> &metadata){ - // Read in wavelength and wavelength spread +void LoadSpice2D::setWavelength(std::map<std::string, std::string> &metadata) { + // Read in wavelength and wavelength spread - g_log.debug() << "setWavelength: "<< m_wavelength_input << " , " << m_wavelength_input << std::endl; + g_log.debug() << "setWavelength: " << m_wavelength_input << " , " + << m_wavelength_input << std::endl; - if (isEmpty(m_wavelength_input)) { - std::string s = metadata["Header/wavelength"]; - from_string<double>(m_wavelength, s , std::dec); - s = metadata["Header/wavelength_spread"]; - from_string<double>(m_dwavelength,s, std::dec); + if (isEmpty(m_wavelength_input)) { + std::string s = metadata["Header/wavelength"]; + from_string<double>(m_wavelength, s, std::dec); + s = metadata["Header/wavelength_spread"]; + from_string<double>(m_dwavelength, s, std::dec); - g_log.debug() << "setWavelength: "<< m_wavelength << " , " << m_dwavelength << std::endl; + g_log.debug() << "setWavelength: " << m_wavelength << " , " << m_dwavelength + << std::endl; - } else { - m_wavelength = m_wavelength_input; - m_dwavelength = m_wavelength_spread_input; - } + } else { + m_wavelength = m_wavelength_input; + m_dwavelength = m_wavelength_spread_input; + } } /** * Parses the data dimensions and stores them as member variables * Reads the data and returns a vector */ -std::vector<int> LoadSpice2D::getData(const std::string &dataXpath = "//Data/Detector" ) { - //type : INT32[192,256] - std::map<std::string, std::string> attributes = - m_xmlHandler.get_attributes_from_tag(dataXpath); - parseDetectorDimensions(attributes["type"]); - if (m_numberXPixels == 0 || m_numberYPixels == 0) - g_log.notice() << "Could not read in the number of pixels!" - << std::endl; - - std::string data_str = m_xmlHandler.get_text_from_tag(dataXpath); - //convert string data into a vector<int> - std::vector<int> data; - std::stringstream iss( data_str ); - int number; - while ( iss >> number ){ - data.push_back( number ); - } - - if (data.size() != static_cast<size_t>(m_numberXPixels * m_numberYPixels)) - throw Kernel::Exception::NotImplementedError("Inconsistent data set: There were more data pixels found than declared in the Spice XML meta-data."); - return data; +std::vector<int> +LoadSpice2D::getData(const std::string &dataXpath = "//Data/Detector") { + // type : INT32[192,256] + std::map<std::string, std::string> attributes = + m_xmlHandler.get_attributes_from_tag(dataXpath); + parseDetectorDimensions(attributes["type"]); + if (m_numberXPixels == 0 || m_numberYPixels == 0) + g_log.notice() << "Could not read in the number of pixels!" << std::endl; + + std::string data_str = m_xmlHandler.get_text_from_tag(dataXpath); + // convert string data into a vector<int> + std::vector<int> data; + std::stringstream iss(data_str); + int number; + while (iss >> number) { + data.push_back(number); + } + + if (data.size() != static_cast<size_t>(m_numberXPixels * m_numberYPixels)) + throw Kernel::Exception::NotImplementedError( + "Inconsistent data set: There were more data pixels found than " + "declared in the Spice XML meta-data."); + return data; } /** * Creates workspace and loads the data along with 2 monitors! */ -void LoadSpice2D::createWorkspace(const std::vector<int> &data, const std::string &title, - double monitor1_counts, double monitor2_counts) { - int nBins = 1; - int numSpectra = m_numberXPixels * m_numberYPixels + LoadSpice2D::nMonitors; - - m_workspace = boost::dynamic_pointer_cast< - DataObjects::Workspace2D>( - API::WorkspaceFactory::Instance().create("Workspace2D", numSpectra, - nBins + 1, nBins)); - m_workspace->setTitle(title); - m_workspace->getAxis(0)->unit() = Kernel::UnitFactory::Instance().create( - "Wavelength"); - m_workspace->setYUnit(""); - API::Workspace_sptr workspace = boost::static_pointer_cast<API::Workspace>( - m_workspace); - setProperty("OutputWorkspace", workspace); - - int specID = 0; - // Store monitor counts in the beggining - store_value(m_workspace, specID++, monitor1_counts, monitor1_counts > 0 ? sqrt(monitor1_counts) : 0.0, m_wavelength, m_dwavelength); - store_value(m_workspace, specID++, monitor2_counts, 0.0, m_wavelength, m_dwavelength); - - // Store detector pixels - for(auto it = data.begin(); it != data.end(); ++it) { - double count = static_cast<double>(*it); - // Data uncertainties, computed according to the HFIR/IGOR reduction code - // The following is what I would suggest instead... - // error = count > 0 ? sqrt((double)count) : 0.0; - double error = sqrt(0.5 + fabs(count - 0.5)); - store_value(m_workspace, specID++, count, error, m_wavelength, m_dwavelength); - } +void LoadSpice2D::createWorkspace(const std::vector<int> &data, + const std::string &title, + double monitor1_counts, + double monitor2_counts) { + int nBins = 1; + int numSpectra = m_numberXPixels * m_numberYPixels + LoadSpice2D::nMonitors; + + m_workspace = boost::dynamic_pointer_cast<DataObjects::Workspace2D>( + API::WorkspaceFactory::Instance().create("Workspace2D", numSpectra, + nBins + 1, nBins)); + m_workspace->setTitle(title); + m_workspace->getAxis(0)->unit() = + Kernel::UnitFactory::Instance().create("Wavelength"); + m_workspace->setYUnit(""); + API::Workspace_sptr workspace = + boost::static_pointer_cast<API::Workspace>(m_workspace); + setProperty("OutputWorkspace", workspace); + + int specID = 0; + // Store monitor counts in the beggining + store_value(m_workspace, specID++, monitor1_counts, + monitor1_counts > 0 ? sqrt(monitor1_counts) : 0.0, m_wavelength, + m_dwavelength); + store_value(m_workspace, specID++, monitor2_counts, 0.0, m_wavelength, + m_dwavelength); + + // Store detector pixels + for (auto it = data.begin(); it != data.end(); ++it) { + double count = static_cast<double>(*it); + // Data uncertainties, computed according to the HFIR/IGOR reduction code + // The following is what I would suggest instead... + // error = count > 0 ? sqrt((double)count) : 0.0; + double error = sqrt(0.5 + fabs(count - 0.5)); + store_value(m_workspace, specID++, count, error, m_wavelength, + m_dwavelength); + } } /** * Inserts a property in the run with a new name! */ -template<class T> -T LoadSpice2D::addRunProperty( - std::map<std::string, std::string> &metadata, - const std::string &oldName, const std::string &newName, const std::string &units) { - - std::string value_str = metadata[oldName]; - T value; - from_string<T>(value, value_str, std::dec); - m_workspace->mutableRun().addProperty(newName, value, units, true); - return value; +template <class T> +T LoadSpice2D::addRunProperty(std::map<std::string, std::string> &metadata, + const std::string &oldName, + const std::string &newName, + const std::string &units) { + + std::string value_str = metadata[oldName]; + T value; + from_string<T>(value, value_str, std::dec); + m_workspace->mutableRun().addProperty(newName, value, units, true); + return value; } -template<class T> -void LoadSpice2D::addRunProperty(const std::string &name, const T &value, const std::string &units) { - m_workspace->mutableRun().addProperty(name, value, units, true); +template <class T> +void LoadSpice2D::addRunProperty(const std::string &name, const T &value, + const std::string &units) { + m_workspace->mutableRun().addProperty(name, value, units, true); } - /** * Sets the beam trap as Run Property */ -void LoadSpice2D::setBeamTrapRunProperty(std::map<std::string, std::string> &metadata){ - - // Read in beam trap positions - double trap_pos = 0; - from_string<double>(trap_pos, metadata["Motor_Positions/trap_y_25mm"], std::dec); - - double beam_trap_diam = 25.4; - - double highest_trap = 0; - from_string<double>(trap_pos, metadata["Motor_Positions/trap_y_101mm"], std::dec); - - if (trap_pos > highest_trap) { - highest_trap = trap_pos; - beam_trap_diam = 101.6; - } - - from_string<double>(trap_pos, metadata["Motor_Positions/trap_y_50mm"], std::dec); - if (trap_pos > highest_trap) { - highest_trap = trap_pos; - beam_trap_diam = 50.8; - } - - from_string<double>(trap_pos, metadata["Motor_Positions/trap_y_76mm"], std::dec); - if (trap_pos > highest_trap) { - beam_trap_diam = 76.2; - } - - addRunProperty<double>("beam-trap-diameter",beam_trap_diam,"mm"); +void LoadSpice2D::setBeamTrapRunProperty( + std::map<std::string, std::string> &metadata) { + + // Read in beam trap positions + double trap_pos = 0; + from_string<double>(trap_pos, metadata["Motor_Positions/trap_y_25mm"], + std::dec); + + double beam_trap_diam = 25.4; + + double highest_trap = 0; + from_string<double>(trap_pos, metadata["Motor_Positions/trap_y_101mm"], + std::dec); + + if (trap_pos > highest_trap) { + highest_trap = trap_pos; + beam_trap_diam = 101.6; + } + + from_string<double>(trap_pos, metadata["Motor_Positions/trap_y_50mm"], + std::dec); + if (trap_pos > highest_trap) { + highest_trap = trap_pos; + beam_trap_diam = 50.8; + } + + from_string<double>(trap_pos, metadata["Motor_Positions/trap_y_76mm"], + std::dec); + if (trap_pos > highest_trap) { + beam_trap_diam = 76.2; + } + + addRunProperty<double>("beam-trap-diameter", beam_trap_diam, "mm"); } -void LoadSpice2D::setMetadataAsRunProperties(std::map<std::string, std::string> &metadata){ - setBeamTrapRunProperty(metadata); - - // start_time - std::map<std::string, std::string> attributes = m_xmlHandler.get_attributes_from_tag("/"); - addRunProperty<std::string>(attributes,"start_time", "start_time",""); - addRunProperty<std::string>(attributes,"start_time", "run_start",""); - - - //sample thickness - addRunProperty<double>(metadata,"Header/Sample_Thickness", "sample-thickness","mm"); - - addRunProperty<double>(metadata,"Header/source_aperture_size", "source-aperture-diameter","mm"); - addRunProperty<double>(metadata,"Header/sample_aperture_size", "sample-aperture-diameter","mm"); - addRunProperty<double>(metadata,"Header/source_distance", "source-sample-distance","mm"); - addRunProperty<int>(metadata,"Motor_Positions/nguides", "number-of-guides",""); - - addRunProperty<double>("wavelength",m_wavelength,"Angstrom"); - addRunProperty<double>("wavelength-spread",m_dwavelength,"Angstrom"); - - addRunProperty<double>(metadata,"Counters/monitor", "monitor",""); - addRunProperty<double>(metadata,"Counters/time", "timer","sec"); - - +void LoadSpice2D::setMetadataAsRunProperties( + std::map<std::string, std::string> &metadata) { + setBeamTrapRunProperty(metadata); + + // start_time + std::map<std::string, std::string> attributes = + m_xmlHandler.get_attributes_from_tag("/"); + addRunProperty<std::string>(attributes, "start_time", "start_time", ""); + addRunProperty<std::string>(attributes, "start_time", "run_start", ""); + + // sample thickness + addRunProperty<double>(metadata, "Header/Sample_Thickness", + "sample-thickness", "mm"); + + addRunProperty<double>(metadata, "Header/source_aperture_size", + "source-aperture-diameter", "mm"); + addRunProperty<double>(metadata, "Header/sample_aperture_size", + "sample-aperture-diameter", "mm"); + addRunProperty<double>(metadata, "Header/source_distance", + "source-sample-distance", "mm"); + addRunProperty<int>(metadata, "Motor_Positions/nguides", "number-of-guides", + ""); + + addRunProperty<double>("wavelength", m_wavelength, "Angstrom"); + addRunProperty<double>("wavelength-spread", m_dwavelength, "Angstrom"); + + addRunProperty<double>(metadata, "Counters/monitor", "monitor", ""); + addRunProperty<double>(metadata, "Counters/time", "timer", "sec"); } - /** * Calculates all the detector distances and sets them as Run properties * @return : total_sample_detector_distance */ -double LoadSpice2D::totalDetectorDistance(std::map<std::string, std::string> &metadata){ - - // sample_detector_distances - double sample_detector_distance = 0; - from_string<double>(sample_detector_distance, metadata["Motor_Positions/sample_det_dist"], std::dec); - sample_detector_distance *= 1000.0; - addRunProperty<double>("sample_detector_distance",sample_detector_distance,"mm"); - addRunProperty<double>("sample-detector-distance",sample_detector_distance,"mm"); - double sample_detector_distance_offset = addRunProperty<double>(metadata,"Header/tank_internal_offset", "sample-detector-distance-offset","mm"); - double sample_si_window_distance = addRunProperty<double>(metadata,"Header/sample_to_flange", "sample-si-window-distance","mm"); - double total_sample_detector_distance = sample_detector_distance + sample_detector_distance_offset + sample_si_window_distance; - addRunProperty<double>("total-sample-detector-distance",total_sample_detector_distance,"mm"); - return total_sample_detector_distance; +double LoadSpice2D::totalDetectorDistance( + std::map<std::string, std::string> &metadata) { + + // sample_detector_distances + double sample_detector_distance = 0; + from_string<double>(sample_detector_distance, + metadata["Motor_Positions/sample_det_dist"], std::dec); + sample_detector_distance *= 1000.0; + addRunProperty<double>("sample_detector_distance", sample_detector_distance, + "mm"); + addRunProperty<double>("sample-detector-distance", sample_detector_distance, + "mm"); + double sample_detector_distance_offset = + addRunProperty<double>(metadata, "Header/tank_internal_offset", + "sample-detector-distance-offset", "mm"); + double sample_si_window_distance = addRunProperty<double>( + metadata, "Header/sample_to_flange", "sample-si-window-distance", "mm"); + double total_sample_detector_distance = sample_detector_distance + + sample_detector_distance_offset + + sample_si_window_distance; + addRunProperty<double>("total-sample-detector-distance", + total_sample_detector_distance, "mm"); + return total_sample_detector_distance; } /** * Places the detector at the right sample_detector_distance */ void LoadSpice2D::moveDetector(double sample_detector_distance) { - // Move the detector to the right position - API::IAlgorithm_sptr mover = createChildAlgorithm( - "MoveInstrumentComponent"); - - // Finding the name of the detector object. - std::string detID = - m_workspace->getInstrument()->getStringParameter("detector-name")[0]; - - g_log.information("Moving " + detID); - try { - mover->setProperty<API::MatrixWorkspace_sptr>("Workspace", m_workspace); - mover->setProperty("ComponentName", detID); - mover->setProperty("Z", sample_detector_distance / 1000.0); - mover->execute(); - } catch (std::invalid_argument &e) { - g_log.error( - "Invalid argument to MoveInstrumentComponent Child Algorithm"); - g_log.error(e.what()); - } catch (std::runtime_error &e) { - g_log.error( - "Unable to successfully run MoveInstrumentComponent Child Algorithm"); - g_log.error(e.what()); - } + // Move the detector to the right position + API::IAlgorithm_sptr mover = createChildAlgorithm("MoveInstrumentComponent"); + + // Finding the name of the detector object. + std::string detID = + m_workspace->getInstrument()->getStringParameter("detector-name")[0]; + + g_log.information("Moving " + detID); + try { + mover->setProperty<API::MatrixWorkspace_sptr>("Workspace", m_workspace); + mover->setProperty("ComponentName", detID); + mover->setProperty("Z", sample_detector_distance / 1000.0); + mover->execute(); + } catch (std::invalid_argument &e) { + g_log.error("Invalid argument to MoveInstrumentComponent Child Algorithm"); + g_log.error(e.what()); + } catch (std::runtime_error &e) { + g_log.error( + "Unable to successfully run MoveInstrumentComponent Child Algorithm"); + g_log.error(e.what()); + } } /** Run the Child Algorithm LoadInstrument (as for LoadRaw) * @param inst_name :: The name written in the Nexus file * @param localWorkspace :: The workspace to insert the instrument into */ -void LoadSpice2D::runLoadInstrument(const std::string &inst_name, - DataObjects::Workspace2D_sptr localWorkspace) { - - API::IAlgorithm_sptr loadInst = createChildAlgorithm("LoadInstrument"); - - // Now execute the Child Algorithm. Catch and log any error, but don't stop. - try { - loadInst->setPropertyValue("InstrumentName", inst_name); - loadInst->setProperty<API::MatrixWorkspace_sptr>("Workspace", - localWorkspace); - loadInst->setProperty("RewriteSpectraMap", false); - loadInst->execute(); - } catch (std::invalid_argument &) { - g_log.information("Invalid argument to LoadInstrument Child Algorithm"); - } catch (std::runtime_error &) { - g_log.information( - "Unable to successfully run LoadInstrument Child Algorithm"); - } +void LoadSpice2D::runLoadInstrument( + const std::string &inst_name, + DataObjects::Workspace2D_sptr localWorkspace) { + + API::IAlgorithm_sptr loadInst = createChildAlgorithm("LoadInstrument"); + + // Now execute the Child Algorithm. Catch and log any error, but don't stop. + try { + loadInst->setPropertyValue("InstrumentName", inst_name); + loadInst->setProperty<API::MatrixWorkspace_sptr>("Workspace", + localWorkspace); + loadInst->setProperty("RewriteSpectraMap", false); + loadInst->execute(); + } catch (std::invalid_argument &) { + g_log.information("Invalid argument to LoadInstrument Child Algorithm"); + } catch (std::runtime_error &) { + g_log.information( + "Unable to successfully run LoadInstrument Child Algorithm"); + } } /** @@ -488,41 +508,41 @@ void LoadSpice2D::runLoadInstrument(const std::string &inst_name, * @param nybins: number of bins in Y */ void LoadSpice2D::runLoadMappingTable( - DataObjects::Workspace2D_sptr localWorkspace, int nxbins, int nybins) { - // Get the number of monitor channels - boost::shared_ptr<const Geometry::Instrument> instrument = - localWorkspace->getInstrument(); - std::vector<detid_t> monitors = instrument->getMonitors(); - const int nMonitors = static_cast<int>(monitors.size()); - - // Number of monitors should be consistent with data file format - if (nMonitors != LoadSpice2D::nMonitors) { - std::stringstream error; - error << "Geometry error for " << instrument->getName() - << ": Spice data format defines " << LoadSpice2D::nMonitors - << " monitors, " << nMonitors << " were/was found"; - throw std::runtime_error(error.str()); - } - - // Generate mapping of detector/channel IDs to spectrum ID - - // Detector/channel counter - int icount = 0; - - // Monitor: IDs start at 1 and increment by 1 - for (int i = 0; i < nMonitors; i++) { - localWorkspace->getSpectrum(icount)->setDetectorID(icount + 1); - icount++; - } - - // Detector pixels - for (int ix = 0; ix < nxbins; ix++) { - for (int iy = 0; iy < nybins; iy++) { - localWorkspace->getSpectrum(icount)->setDetectorID( - 1000000 + iy * 1000 + ix); - icount++; - } - } + DataObjects::Workspace2D_sptr localWorkspace, int nxbins, int nybins) { + // Get the number of monitor channels + boost::shared_ptr<const Geometry::Instrument> instrument = + localWorkspace->getInstrument(); + std::vector<detid_t> monitors = instrument->getMonitors(); + const int nMonitors = static_cast<int>(monitors.size()); + + // Number of monitors should be consistent with data file format + if (nMonitors != LoadSpice2D::nMonitors) { + std::stringstream error; + error << "Geometry error for " << instrument->getName() + << ": Spice data format defines " << LoadSpice2D::nMonitors + << " monitors, " << nMonitors << " were/was found"; + throw std::runtime_error(error.str()); + } + + // Generate mapping of detector/channel IDs to spectrum ID + + // Detector/channel counter + int icount = 0; + + // Monitor: IDs start at 1 and increment by 1 + for (int i = 0; i < nMonitors; i++) { + localWorkspace->getSpectrum(icount)->setDetectorID(icount + 1); + icount++; + } + + // Detector pixels + for (int ix = 0; ix < nxbins; ix++) { + for (int iy = 0; iy < nybins; iy++) { + localWorkspace->getSpectrum(icount) + ->setDetectorID(1000000 + iy * 1000 + ix); + icount++; + } + } } /* This method throws not found error if a element is not found in the xml file @@ -531,11 +551,12 @@ void LoadSpice2D::runLoadMappingTable( * @param fileName :: xml file name */ void LoadSpice2D::throwException(Poco::XML::Element *elem, - const std::string &name, const std::string &fileName) { - if (!elem) { - throw Kernel::Exception::NotFoundError( - name + " element not found in Spice XML file", fileName); - } + const std::string &name, + const std::string &fileName) { + if (!elem) { + throw Kernel::Exception::NotFoundError( + name + " element not found in Spice XML file", fileName); + } } } } diff --git a/Framework/DataHandling/src/XmlHandler.cpp b/Framework/DataHandling/src/XmlHandler.cpp index 8a316a25f71..e38932776a7 100644 --- a/Framework/DataHandling/src/XmlHandler.cpp +++ b/Framework/DataHandling/src/XmlHandler.cpp @@ -11,75 +11,72 @@ namespace Mantid { namespace DataHandling { XmlHandler::XmlHandler() { - // TODO Auto-generated constructor stub - + // TODO Auto-generated constructor stub } XmlHandler::XmlHandler(std::string filename) { - std::ifstream in(filename); - Poco::XML::InputSource src(in); - Poco::XML::DOMParser parser; - pDoc = parser.parse(&src); - + std::ifstream in(filename); + Poco::XML::InputSource src(in); + Poco::XML::DOMParser parser; + pDoc = parser.parse(&src); } XmlHandler::~XmlHandler() { - // TODO Auto-generated destructor stub - // pDoc->release(); + // TODO Auto-generated destructor stub + // pDoc->release(); } - -std::map<std::string, std::string> XmlHandler::get_metadata(const std::string &tag_to_ignore) { - std::map<std::string, std::string> metadata; - - Poco::XML::NodeIterator it(pDoc, Poco::XML::NodeFilter::SHOW_ELEMENT); - Poco::XML::Node* pNode = it.nextNode(); - - while (pNode) { - if (pNode->childNodes()->length() == 1 - && pNode->nodeName() != tag_to_ignore) { - std::string key = pNode->parentNode()->nodeName() + "/" - + pNode->nodeName(); - std::string value = pNode->innerText(); - auto to_insert = std::pair<std::string, std::string>(key, value); - metadata.insert(to_insert); - } - pNode = it.nextNode(); - } - return metadata; +std::map<std::string, std::string> +XmlHandler::get_metadata(const std::string &tag_to_ignore) { + std::map<std::string, std::string> metadata; + + Poco::XML::NodeIterator it(pDoc, Poco::XML::NodeFilter::SHOW_ELEMENT); + Poco::XML::Node *pNode = it.nextNode(); + + while (pNode) { + if (pNode->childNodes()->length() == 1 && + pNode->nodeName() != tag_to_ignore) { + std::string key = + pNode->parentNode()->nodeName() + "/" + pNode->nodeName(); + std::string value = pNode->innerText(); + auto to_insert = std::pair<std::string, std::string>(key, value); + metadata.insert(to_insert); + } + pNode = it.nextNode(); + } + return metadata; } std::string XmlHandler::get_text_from_tag(const std::string &xpath) { - Poco::XML::NodeIterator it(pDoc, Poco::XML::NodeFilter::SHOW_ELEMENT); - Poco::XML::Node* pNode = it.nextNode(); - Poco::XML::Node* detectorNode = pNode->getNodeByPath(xpath); - std::string value(""); - if (detectorNode) - value = detectorNode->innerText(); - return value; - + Poco::XML::NodeIterator it(pDoc, Poco::XML::NodeFilter::SHOW_ELEMENT); + Poco::XML::Node *pNode = it.nextNode(); + Poco::XML::Node *detectorNode = pNode->getNodeByPath(xpath); + std::string value(""); + if (detectorNode) + value = detectorNode->innerText(); + return value; } -std::map<std::string, std::string> XmlHandler::get_attributes_from_tag(const std::string &xpath) { - std::map<std::string, std::string> attributes_map; - Poco::XML::NodeIterator it(pDoc, Poco::XML::NodeFilter::SHOW_ELEMENT); - Poco::XML::Node* pNode = it.nextNode(); - Poco::XML::Node* detectorNode = pNode->getNodeByPath(xpath); - std::string value(""); - if (detectorNode) { - Poco::XML::NamedNodeMap* attributes = detectorNode->attributes(); - for (unsigned int i = 0; i < attributes->length(); i++) { - Poco::XML::Node* attribute = attributes->item(i); - std::string key = attribute->nodeName(); - std::string value = attribute->nodeValue(); - auto to_insert = std::pair<std::string, std::string>(key, value); - attributes_map.insert(to_insert); - } - } - return attributes_map; +std::map<std::string, std::string> +XmlHandler::get_attributes_from_tag(const std::string &xpath) { + std::map<std::string, std::string> attributes_map; + Poco::XML::NodeIterator it(pDoc, Poco::XML::NodeFilter::SHOW_ELEMENT); + Poco::XML::Node *pNode = it.nextNode(); + Poco::XML::Node *detectorNode = pNode->getNodeByPath(xpath); + std::string value(""); + if (detectorNode) { + Poco::XML::NamedNodeMap *attributes = detectorNode->attributes(); + for (unsigned int i = 0; i < attributes->length(); i++) { + Poco::XML::Node *attribute = attributes->item(i); + std::string key = attribute->nodeName(); + std::string value = attribute->nodeValue(); + auto to_insert = std::pair<std::string, std::string>(key, value); + attributes_map.insert(to_insert); + } + } + return attributes_map; } - } /* namespace DataHandling */ } /* namespace Mantid */ diff --git a/Framework/DataHandling/test/LoadSpice2dTest.h b/Framework/DataHandling/test/LoadSpice2dTest.h index 3168228bd14..c57ad1ebb8b 100644 --- a/Framework/DataHandling/test/LoadSpice2dTest.h +++ b/Framework/DataHandling/test/LoadSpice2dTest.h @@ -169,8 +169,10 @@ public: // Check detector position prop = ws2d->run().getProperty("total-sample-detector-distance"); - Mantid::Kernel::PropertyWithValue<double> *tsdd = dynamic_cast<Mantid::Kernel::PropertyWithValue<double> *>(prop); - TS_ASSERT_EQUALS(i->getComponentByName("detector1")->getPos().Z(), *tsdd * 1e-3); + Mantid::Kernel::PropertyWithValue<double> *tsdd = + dynamic_cast<Mantid::Kernel::PropertyWithValue<double> *>(prop); + TS_ASSERT_EQUALS(i->getComponentByName("detector1")->getPos().Z(), + *tsdd * 1e-3); assertDetectorDistances(ws2d); } @@ -216,18 +218,24 @@ public: TS_ASSERT_DELTA(ws2d->dataX(192 + nmon)[0], 4.5, tolerance); } - void assertDetectorDistances(Mantid::DataObjects::Workspace2D_sptr ws2d){ - Mantid::Kernel::Property *prop = ws2d->run().getProperty("sample-detector-distance"); - Mantid::Kernel::PropertyWithValue<double> *sdd = dynamic_cast<Mantid::Kernel::PropertyWithValue<double> *>(prop); - prop = ws2d->run().getProperty("sample-detector-distance-offset"); - Mantid::Kernel::PropertyWithValue<double> *sddo = dynamic_cast<Mantid::Kernel::PropertyWithValue<double> *>(prop); - prop = ws2d->run().getProperty("sample-si-window-distance"); - Mantid::Kernel::PropertyWithValue<double> *siwo = dynamic_cast<Mantid::Kernel::PropertyWithValue<double> *>(prop); - prop = ws2d->run().getProperty("total-sample-detector-distance"); - Mantid::Kernel::PropertyWithValue<double> *tsdd = dynamic_cast<Mantid::Kernel::PropertyWithValue<double> *>(prop); - double total_sample_detector_distance = *tsdd; - TS_ASSERT_EQUALS ((*sdd) + (*sddo) + (*siwo), total_sample_detector_distance); - TS_ASSERT_EQUALS (6811.4, total_sample_detector_distance); + void assertDetectorDistances(Mantid::DataObjects::Workspace2D_sptr ws2d) { + Mantid::Kernel::Property *prop = + ws2d->run().getProperty("sample-detector-distance"); + Mantid::Kernel::PropertyWithValue<double> *sdd = + dynamic_cast<Mantid::Kernel::PropertyWithValue<double> *>(prop); + prop = ws2d->run().getProperty("sample-detector-distance-offset"); + Mantid::Kernel::PropertyWithValue<double> *sddo = + dynamic_cast<Mantid::Kernel::PropertyWithValue<double> *>(prop); + prop = ws2d->run().getProperty("sample-si-window-distance"); + Mantid::Kernel::PropertyWithValue<double> *siwo = + dynamic_cast<Mantid::Kernel::PropertyWithValue<double> *>(prop); + prop = ws2d->run().getProperty("total-sample-detector-distance"); + Mantid::Kernel::PropertyWithValue<double> *tsdd = + dynamic_cast<Mantid::Kernel::PropertyWithValue<double> *>(prop); + double total_sample_detector_distance = *tsdd; + TS_ASSERT_EQUALS((*sdd) + (*sddo) + (*siwo), + total_sample_detector_distance); + TS_ASSERT_EQUALS(6811.4, total_sample_detector_distance); } private: -- GitLab