From d46b6edd2ada8d8b112e97cbb43141cecefe5a5f Mon Sep 17 00:00:00 2001 From: Marina Ganeva <m.ganeva@fz-juelich.de> Date: Wed, 6 Sep 2017 09:27:38 +0200 Subject: [PATCH] Refs #20206. clang-format --- .../MantidAlgorithms/Bin2DPowderDiffraction.h | 36 +- .../Algorithms/src/Bin2DPowderDiffraction.cpp | 547 +++++++++--------- .../test/Bin2DPowderDiffractionTest.h | 469 +++++++-------- 3 files changed, 532 insertions(+), 520 deletions(-) diff --git a/Framework/Algorithms/inc/MantidAlgorithms/Bin2DPowderDiffraction.h b/Framework/Algorithms/inc/MantidAlgorithms/Bin2DPowderDiffraction.h index 8cc2f78482b..6e60d127429 100644 --- a/Framework/Algorithms/inc/MantidAlgorithms/Bin2DPowderDiffraction.h +++ b/Framework/Algorithms/inc/MantidAlgorithms/Bin2DPowderDiffraction.h @@ -39,27 +39,29 @@ namespace Algorithms { */ class MANTID_ALGORITHMS_DLL Bin2DPowderDiffraction : public API::Algorithm { public: - const std::string name() const override; - int version() const override; - const std::string category() const override; - const std::string summary() const override; - /// Cross-check properties with each other @see IAlgorithm::validateInputs - std::map<std::string, std::string> validateInputs() override; + const std::string name() const override; + int version() const override; + const std::string category() const override; + const std::string summary() const override; + /// Cross-check properties with each other @see IAlgorithm::validateInputs + std::map<std::string, std::string> validateInputs() override; protected: - boost::shared_ptr<API::Progress> m_progress; + boost::shared_ptr<API::Progress> m_progress; private: - void init() override; - void exec() override; - /// Setup the output workspace - API::MatrixWorkspace_sptr createOutputWorkspace(); - void ReadBinsFromFile(std::vector<double> &Ybins, std::vector<std::vector<double>>&Xbins) const; - size_t UnifyXBins(std::vector<std::vector<double> > &Xbins) const; - - DataObjects::EventWorkspace_sptr m_inputWS; ///< Pointer to the input event workspace - int m_numberOfSpectra; ///< The number of spectra in the workspace - void normalizeToBinArea (API::MatrixWorkspace_sptr outWS); + void init() override; + void exec() override; + /// Setup the output workspace + API::MatrixWorkspace_sptr createOutputWorkspace(); + void ReadBinsFromFile(std::vector<double> &Ybins, + std::vector<std::vector<double>> &Xbins) const; + size_t UnifyXBins(std::vector<std::vector<double>> &Xbins) const; + + DataObjects::EventWorkspace_sptr + m_inputWS; ///< Pointer to the input event workspace + int m_numberOfSpectra; ///< The number of spectra in the workspace + void normalizeToBinArea(API::MatrixWorkspace_sptr outWS); }; void convertToDSpacing(double wavelength, double theta, double *d, double *dp); diff --git a/Framework/Algorithms/src/Bin2DPowderDiffraction.cpp b/Framework/Algorithms/src/Bin2DPowderDiffraction.cpp index 6e89ef5d495..e321221e172 100644 --- a/Framework/Algorithms/src/Bin2DPowderDiffraction.cpp +++ b/Framework/Algorithms/src/Bin2DPowderDiffraction.cpp @@ -35,90 +35,91 @@ using namespace Geometry; using namespace DataObjects; using namespace Mantid::HistogramData; - // Register the algorithm into the AlgorithmFactory DECLARE_ALGORITHM(Bin2DPowderDiffraction) //---------------------------------------------------------------------------------------------- /// Algorithms name for identification. @see Algorithm::name -const std::string Bin2DPowderDiffraction::name() const { return "Bin2DPowderDiffraction"; } +const std::string Bin2DPowderDiffraction::name() const { + return "Bin2DPowderDiffraction"; +} /// Algorithm's version for identification. @see Algorithm::version int Bin2DPowderDiffraction::version() const { return 1; } /// Algorithm's category for identification. @see Algorithm::category const std::string Bin2DPowderDiffraction::category() const { - return "Diffraction\\Focussing"; + return "Diffraction\\Focussing"; } /// Algorithm's summary for use in the GUI and help. @see Algorithm::summary const std::string Bin2DPowderDiffraction::summary() const { - return "Bins TOF powder diffraction event data in 2D space."; + return "Bins TOF powder diffraction event data in 2D space."; } //---------------------------------------------------------------------------------------------- /** Initialize the algorithm's properties. */ void Bin2DPowderDiffraction::init() { - auto wsValidator = boost::make_shared<CompositeValidator>(); - wsValidator->add<WorkspaceUnitValidator>("Wavelength"); - wsValidator->add<SpectraAxisValidator>(); - wsValidator->add<InstrumentValidator>(); - wsValidator->add<HistogramValidator>(); - - declareProperty(make_unique<WorkspaceProperty<EventWorkspace>>( - "InputWorkspace", "", Direction::Input, - wsValidator), - "An input EventWorkspace must be a Histogram workspace, not Point data. " - "X-axis units must be wavelength."); - - declareProperty( - make_unique<WorkspaceProperty<API::Workspace>>("OutputWorkspace", "", - Direction::Output), - "An output workspace."); - - const std::string docString = - "A comma separated list of first bin boundary, width, last bin boundary. " - "Optionally " - "this can be followed by a comma and more widths and last boundary " - "pairs. " - "Negative width values indicate logarithmic binning."; - auto rebinValidator = boost::make_shared<RebinParamsValidator>(true); - declareProperty(make_unique<ArrayProperty<double>>("Axis1Binning", - rebinValidator), - docString); - declareProperty(make_unique<ArrayProperty<double>>("Axis2Binning", - rebinValidator), - docString); - - const std::vector<std::string> exts{".txt", ".dat"}; - declareProperty(make_unique<FileProperty>("BinEdgesFile", "", - FileProperty::OptionalLoad, exts), - "Optional: The ascii file containing the list of bin edges. " - "Either this or Axis1- and Axis2Binning need to be specified."); - - declareProperty( - Kernel::make_unique<PropertyWithValue<bool>>("NormalizeByBinArea", true), - "Normalize the binned workspace by the bin area."); + auto wsValidator = boost::make_shared<CompositeValidator>(); + wsValidator->add<WorkspaceUnitValidator>("Wavelength"); + wsValidator->add<SpectraAxisValidator>(); + wsValidator->add<InstrumentValidator>(); + wsValidator->add<HistogramValidator>(); + + declareProperty( + make_unique<WorkspaceProperty<EventWorkspace>>( + "InputWorkspace", "", Direction::Input, wsValidator), + "An input EventWorkspace must be a Histogram workspace, not Point data. " + "X-axis units must be wavelength."); + + declareProperty(make_unique<WorkspaceProperty<API::Workspace>>( + "OutputWorkspace", "", Direction::Output), + "An output workspace."); + + const std::string docString = + "A comma separated list of first bin boundary, width, last bin boundary. " + "Optionally " + "this can be followed by a comma and more widths and last boundary " + "pairs. " + "Negative width values indicate logarithmic binning."; + auto rebinValidator = boost::make_shared<RebinParamsValidator>(true); + declareProperty( + make_unique<ArrayProperty<double>>("Axis1Binning", rebinValidator), + docString); + declareProperty( + make_unique<ArrayProperty<double>>("Axis2Binning", rebinValidator), + docString); + + const std::vector<std::string> exts{".txt", ".dat"}; + declareProperty( + make_unique<FileProperty>("BinEdgesFile", "", FileProperty::OptionalLoad, + exts), + "Optional: The ascii file containing the list of bin edges. " + "Either this or Axis1- and Axis2Binning need to be specified."); + + declareProperty( + Kernel::make_unique<PropertyWithValue<bool>>("NormalizeByBinArea", true), + "Normalize the binned workspace by the bin area."); } //---------------------------------------------------------------------------------------------- /** Execute the algorithm. */ void Bin2DPowderDiffraction::exec() { - m_inputWS = this->getProperty("InputWorkspace"); - m_numberOfSpectra = static_cast<int>(m_inputWS->getNumberHistograms()); - g_log.debug() << "Number of spectra in input workspace: " << m_numberOfSpectra - << "\n"; + m_inputWS = this->getProperty("InputWorkspace"); + m_numberOfSpectra = static_cast<int>(m_inputWS->getNumberHistograms()); + g_log.debug() << "Number of spectra in input workspace: " << m_numberOfSpectra + << "\n"; - MatrixWorkspace_sptr outputWS = createOutputWorkspace(); + MatrixWorkspace_sptr outputWS = createOutputWorkspace(); - const bool normalizeByBinArea = this->getProperty("NormalizeByBinArea"); - if (normalizeByBinArea) - normalizeToBinArea(outputWS); + const bool normalizeByBinArea = this->getProperty("NormalizeByBinArea"); + if (normalizeByBinArea) + normalizeToBinArea(outputWS); - setProperty("OutputWorkspace", outputWS); + setProperty("OutputWorkspace", outputWS); } //---------------------------------------------------------------------------------------------- /** @@ -126,22 +127,24 @@ void Bin2DPowderDiffraction::exec() { * @return */ std::map<std::string, std::string> Bin2DPowderDiffraction::validateInputs() { - std::map<std::string, std::string> result; - - const auto useBinFile = !getPointerToProperty("BinEdgesFile")->isDefault(); - const auto useBinning1 = !getPointerToProperty("Axis1Binning")->isDefault(); - const auto useBinning2 = !getPointerToProperty("Axis2Binning")->isDefault(); - if (!useBinFile && !useBinning1 && !useBinning2) { - const std::string msg = "You must specify either Axis1Binning and Axis2Binning, or a BinEdgesFile."; - result["Axis1Binning"] = msg; - result["Axis2Binning"] = msg; - result["BinEdgesFile"] = msg; - } else if (useBinFile && (useBinning1 || useBinning2)) { - const std::string msg = "You must specify either Axis1Binning and Axis2Binning, or a BinEdgesFile, but not both."; - result["BinEdgesFile"] = msg; - } - - return result; + std::map<std::string, std::string> result; + + const auto useBinFile = !getPointerToProperty("BinEdgesFile")->isDefault(); + const auto useBinning1 = !getPointerToProperty("Axis1Binning")->isDefault(); + const auto useBinning2 = !getPointerToProperty("Axis2Binning")->isDefault(); + if (!useBinFile && !useBinning1 && !useBinning2) { + const std::string msg = "You must specify either Axis1Binning and " + "Axis2Binning, or a BinEdgesFile."; + result["Axis1Binning"] = msg; + result["Axis2Binning"] = msg; + result["BinEdgesFile"] = msg; + } else if (useBinFile && (useBinning1 || useBinning2)) { + const std::string msg = "You must specify either Axis1Binning and " + "Axis2Binning, or a BinEdgesFile, but not both."; + result["BinEdgesFile"] = msg; + } + + return result; } //---------------------------------------------------------------------------------------------- /** @@ -152,233 +155,237 @@ std::map<std::string, std::string> Bin2DPowderDiffraction::validateInputs() { MatrixWorkspace_sptr Bin2DPowderDiffraction::createOutputWorkspace() { - using VectorHelper::createAxisFromRebinParams; - bool binsFromFile(false); - size_t newYSize = 0; - size_t newXSize = 0; - MatrixWorkspace_sptr outputWS; - const auto &spectrumInfo = m_inputWS->spectrumInfo(); - - const std::string beFileName = getProperty("BinEdgesFile"); - if (!beFileName.empty()) - binsFromFile=true; - - const auto &oldXEdges = m_inputWS->x(0); - BinEdges newXBins(oldXEdges.size()); - BinEdges newYBins(oldXEdges.size()); - - auto &newY = newYBins.mutableRawData(); - std::vector<std::vector<double>> fileXbins; - - // First create the output Workspace filled with zeros - if (binsFromFile) { - newY.clear(); - ReadBinsFromFile(newY, fileXbins); - newYSize = newY.size(); - // unify xbins - newXSize = UnifyXBins(fileXbins); - g_log.debug() << "Maximal size of Xbins = " << newXSize; - outputWS = WorkspaceFactory::Instance().create(m_inputWS, newYSize-1, newXSize, newXSize-1); - g_log.debug() << "Outws has " << outputWS->getNumberHistograms() << " histograms and " << outputWS->blocksize() << " bins." << std::endl; - - size_t idx = 0; - for (const auto Xbins : fileXbins) { - g_log.debug() << "Xbins size: " << Xbins.size() << std::endl; - BinEdges binEdges (Xbins); - outputWS->setBinEdges(idx, binEdges); - idx++; - } - - } else { - static_cast<void>(createAxisFromRebinParams(getProperty("Axis1Binning"), - newXBins.mutableRawData())); - HistogramData::BinEdges binEdges(newXBins); - newYSize = - createAxisFromRebinParams(getProperty("Axis2Binning"), newY); - newXSize = binEdges.size(); - outputWS = WorkspaceFactory::Instance().create(m_inputWS, newYSize - 1, newXSize, newXSize-1); - for (size_t idx=0; idx<newYSize-1; idx++) - outputWS->setBinEdges(idx, binEdges); - NumericAxis *const abscissa = new BinEdgeAxis(newXBins.mutableRawData()); - outputWS->replaceAxis(0, abscissa); + using VectorHelper::createAxisFromRebinParams; + bool binsFromFile(false); + size_t newYSize = 0; + size_t newXSize = 0; + MatrixWorkspace_sptr outputWS; + const auto &spectrumInfo = m_inputWS->spectrumInfo(); + + const std::string beFileName = getProperty("BinEdgesFile"); + if (!beFileName.empty()) + binsFromFile = true; + + const auto &oldXEdges = m_inputWS->x(0); + BinEdges newXBins(oldXEdges.size()); + BinEdges newYBins(oldXEdges.size()); + + auto &newY = newYBins.mutableRawData(); + std::vector<std::vector<double>> fileXbins; + + // First create the output Workspace filled with zeros + if (binsFromFile) { + newY.clear(); + ReadBinsFromFile(newY, fileXbins); + newYSize = newY.size(); + // unify xbins + newXSize = UnifyXBins(fileXbins); + g_log.debug() << "Maximal size of Xbins = " << newXSize; + outputWS = WorkspaceFactory::Instance().create(m_inputWS, newYSize - 1, + newXSize, newXSize - 1); + g_log.debug() << "Outws has " << outputWS->getNumberHistograms() + << " histograms and " << outputWS->blocksize() << " bins." + << std::endl; + + size_t idx = 0; + for (const auto Xbins : fileXbins) { + g_log.debug() << "Xbins size: " << Xbins.size() << std::endl; + BinEdges binEdges(Xbins); + outputWS->setBinEdges(idx, binEdges); + idx++; } - outputWS->getAxis(0)->unit() = UnitFactory::Instance().create("dSpacing"); - - NumericAxis *const verticalAxis = new BinEdgeAxis(newY); - // Meta data - verticalAxis->unit() = UnitFactory::Instance().create("dSpacingPerpendicular"); - verticalAxis->title() = "d_p"; - outputWS->replaceAxis(1, verticalAxis); - - Progress prog(this, 0.0, 1.0, m_numberOfSpectra); - int64_t numSpectra = static_cast<int64_t>(m_numberOfSpectra); - std::vector<std::vector<double>> newYValues(newYSize-1, std::vector<double>(newXSize-1, 0.0)); - std::vector<std::vector<double>> newEValues(newYSize-1, std::vector<double>(newXSize-1, 0.0)); - - // fill the workspace with data - g_log.debug() << "newYSize = " << newYSize << std::endl; - g_log.debug() << "newXSize = " << newXSize << std::endl; - std::vector<double> dp_vec (verticalAxis->getValues()); - - PARALLEL_FOR_IF(Kernel::threadSafe(*m_inputWS, *outputWS)) - for (int64_t snum=0; snum<numSpectra; ++snum) { - PARALLEL_START_INTERUPT_REGION - if (!spectrumInfo.isMasked(snum)) { - double theta = 0.5*spectrumInfo.twoTheta(snum); - if (theta==0){ - throw std::runtime_error("Spectrum " + std::to_string(snum) + " has theta=0. Cannot calculate d-Spacing!"); - } - if (cos(theta)<=0){ - throw std::runtime_error("Spectrum " + std::to_string(snum) + " has cos(theta) <= 0. Cannot calculate d-SpacingPerpendicular!"); - } - EventList &evList = m_inputWS->getSpectrum(snum); - - // Switch to weighted if needed. - if (evList.getEventType() == TOF) - evList.switchTo(WEIGHTED); - - std::vector<WeightedEvent> events = evList.getWeightedEvents(); - - for(const auto &ev:events){ - double d, dp; - convertToDSpacing(ev.tof(), theta, &d, &dp); - std::vector<double>::iterator upy = std::lower_bound(dp_vec.begin(), dp_vec.end(), dp); - long int h_index = std::distance(dp_vec.begin(), upy) -1; - if ((h_index < static_cast<int>(newYSize) -1) && h_index > -1) { - if (h_index == static_cast<int>(newYSize)-1) - g_log.error("h_index is equal to the size of the Y axis!"); - auto xs = binsFromFile ? fileXbins[h_index] : newXBins.rawData(); - std::vector<double>::iterator lowx = std::lower_bound(xs.begin(), xs.end(), d); - long int index = std::distance(xs.begin(), lowx) -1; - if ((index < static_cast<int>(newXSize-1)) && (index > -1)) { - // writing to the same vectors is not threat-safe - PARALLEL_CRITICAL(newValues) { - newYValues[h_index][index] += ev.weight(); - newEValues[h_index][index] += ev.errorSquared(); - } - } - + } else { + static_cast<void>(createAxisFromRebinParams(getProperty("Axis1Binning"), + newXBins.mutableRawData())); + HistogramData::BinEdges binEdges(newXBins); + newYSize = createAxisFromRebinParams(getProperty("Axis2Binning"), newY); + newXSize = binEdges.size(); + outputWS = WorkspaceFactory::Instance().create(m_inputWS, newYSize - 1, + newXSize, newXSize - 1); + for (size_t idx = 0; idx < newYSize - 1; idx++) + outputWS->setBinEdges(idx, binEdges); + NumericAxis *const abscissa = new BinEdgeAxis(newXBins.mutableRawData()); + outputWS->replaceAxis(0, abscissa); + } + + outputWS->getAxis(0)->unit() = UnitFactory::Instance().create("dSpacing"); + + NumericAxis *const verticalAxis = new BinEdgeAxis(newY); + // Meta data + verticalAxis->unit() = + UnitFactory::Instance().create("dSpacingPerpendicular"); + verticalAxis->title() = "d_p"; + outputWS->replaceAxis(1, verticalAxis); + + Progress prog(this, 0.0, 1.0, m_numberOfSpectra); + int64_t numSpectra = static_cast<int64_t>(m_numberOfSpectra); + std::vector<std::vector<double>> newYValues( + newYSize - 1, std::vector<double>(newXSize - 1, 0.0)); + std::vector<std::vector<double>> newEValues( + newYSize - 1, std::vector<double>(newXSize - 1, 0.0)); + + // fill the workspace with data + g_log.debug() << "newYSize = " << newYSize << std::endl; + g_log.debug() << "newXSize = " << newXSize << std::endl; + std::vector<double> dp_vec(verticalAxis->getValues()); + + PARALLEL_FOR_IF(Kernel::threadSafe(*m_inputWS, *outputWS)) + for (int64_t snum = 0; snum < numSpectra; ++snum) { + PARALLEL_START_INTERUPT_REGION + if (!spectrumInfo.isMasked(snum)) { + double theta = 0.5 * spectrumInfo.twoTheta(snum); + if (theta == 0) { + throw std::runtime_error("Spectrum " + std::to_string(snum) + + " has theta=0. Cannot calculate d-Spacing!"); + } + if (cos(theta) <= 0) { + throw std::runtime_error( + "Spectrum " + std::to_string(snum) + + " has cos(theta) <= 0. Cannot calculate d-SpacingPerpendicular!"); + } + EventList &evList = m_inputWS->getSpectrum(snum); + + // Switch to weighted if needed. + if (evList.getEventType() == TOF) + evList.switchTo(WEIGHTED); + + std::vector<WeightedEvent> events = evList.getWeightedEvents(); + + for (const auto &ev : events) { + double d, dp; + convertToDSpacing(ev.tof(), theta, &d, &dp); + std::vector<double>::iterator upy = + std::lower_bound(dp_vec.begin(), dp_vec.end(), dp); + long int h_index = std::distance(dp_vec.begin(), upy) - 1; + if ((h_index < static_cast<int>(newYSize) - 1) && h_index > -1) { + if (h_index == static_cast<int>(newYSize) - 1) + g_log.error("h_index is equal to the size of the Y axis!"); + auto xs = binsFromFile ? fileXbins[h_index] : newXBins.rawData(); + std::vector<double>::iterator lowx = + std::lower_bound(xs.begin(), xs.end(), d); + long int index = std::distance(xs.begin(), lowx) - 1; + if ((index < static_cast<int>(newXSize - 1)) && (index > -1)) { + // writing to the same vectors is not threat-safe + PARALLEL_CRITICAL(newValues) { + newYValues[h_index][index] += ev.weight(); + newEValues[h_index][index] += ev.errorSquared(); } - + } } - } - prog.report("Binning event data..."); - PARALLEL_END_INTERUPT_REGION - - } - PARALLEL_CHECK_INTERUPT_REGION - size_t idx=0; - for (const auto &yVec: newYValues) { - outputWS->setCounts(idx, yVec); - idx++; + } } - idx=0; - for (auto &eVec: newEValues) { - std::transform(eVec.begin(), eVec.end(), eVec.begin(), - static_cast<double (*)(double)>(sqrt)); - outputWS->setCountStandardDeviations(idx, eVec); - idx++; - } - return outputWS; + prog.report("Binning event data..."); + PARALLEL_END_INTERUPT_REGION + } + PARALLEL_CHECK_INTERUPT_REGION + size_t idx = 0; + for (const auto &yVec : newYValues) { + outputWS->setCounts(idx, yVec); + idx++; + } + idx = 0; + for (auto &eVec : newEValues) { + std::transform(eVec.begin(), eVec.end(), eVec.begin(), + static_cast<double (*)(double)>(sqrt)); + outputWS->setCountStandardDeviations(idx, eVec); + idx++; + } + return outputWS; } //---------------------------------------------------------------------------------------------- /** * @brief Bin2DPowderDiffraction::ReadBinsFromFile - * @param[out] Ybins vector of doubles to save the dSpacingPerpendicular bin edges + * @param[out] Ybins vector of doubles to save the dSpacingPerpendicular bin + * edges * @param[out] Xbins vector of vectors of doubles to save the dSpacing bin edges */ -void Bin2DPowderDiffraction::ReadBinsFromFile(std::vector<double> &Ybins, std::vector<std::vector<double> > &Xbins) const -{ - const std::string beFileName = getProperty("BinEdgesFile"); - std::ifstream file (beFileName); - std::string line; - std::string::size_type n; - std::string::size_type sz; - std::vector<double> tmp; - int dpno = 0; - while(getline(file, line)){ - n = line.find("dp ="); - if (n != std::string::npos) { - if (!tmp.empty()){ - Xbins.push_back(tmp); - tmp.clear(); - } - double dp1 = std::stod (line.substr(4),&sz); // 4 is needed to crop 'dp=' - double dp2 = std::stod (line.substr(sz + 4)); - if (dpno < 1){ - Ybins.push_back(dp1); - Ybins.push_back(dp2); - } else { - Ybins.push_back(dp2); - } - dpno++; - } else if(line.find("#")==std::string::npos) { - std::stringstream ss(line); - double d; - while (ss >> d) - { - tmp.push_back(d); - } - - } +void Bin2DPowderDiffraction::ReadBinsFromFile( + std::vector<double> &Ybins, std::vector<std::vector<double>> &Xbins) const { + const std::string beFileName = getProperty("BinEdgesFile"); + std::ifstream file(beFileName); + std::string line; + std::string::size_type n; + std::string::size_type sz; + std::vector<double> tmp; + int dpno = 0; + while (getline(file, line)) { + n = line.find("dp ="); + if (n != std::string::npos) { + if (!tmp.empty()) { + Xbins.push_back(tmp); + tmp.clear(); + } + double dp1 = std::stod(line.substr(4), &sz); // 4 is needed to crop 'dp=' + double dp2 = std::stod(line.substr(sz + 4)); + if (dpno < 1) { + Ybins.push_back(dp1); + Ybins.push_back(dp2); + } else { + Ybins.push_back(dp2); + } + dpno++; + } else if (line.find("#") == std::string::npos) { + std::stringstream ss(line); + double d; + while (ss >> d) { + tmp.push_back(d); + } } - Xbins.push_back(tmp); - g_log.information() << "Number of Ybins: " << Ybins.size() << std::endl; - g_log.information() << "Number of Xbins sets: " << Xbins.size() << std::endl; - + } + Xbins.push_back(tmp); + g_log.information() << "Number of Ybins: " << Ybins.size() << std::endl; + g_log.information() << "Number of Xbins sets: " << Xbins.size() << std::endl; } //---------------------------------------------------------------------------------------------- /** - * @brief Bin2DPowderDiffraction::UnifyXBins unifies size of the vectors in Xbins. + * @brief Bin2DPowderDiffraction::UnifyXBins unifies size of the vectors in + *Xbins. * Just fills std::nans at the end of the shorter bins. * Required to avoid garbage values in the X values after ws->setHistogram. * returns the maximal size * * @param Xbins[in] --- bins to unify. Will be overwritten. */ -size_t Bin2DPowderDiffraction::UnifyXBins(std::vector<std::vector<double>> &Xbins) const -{ - // get maximal vector size - size_t max_size = 0; - for (const auto& v : Xbins) { - max_size = std::max(v.size(), max_size); - } - // resize all vectors to maximum size, fill last vector element at the end - for (auto &v: Xbins) { - if (v.size() < max_size) - v.resize(max_size, v.back()); - } - return max_size; - +size_t Bin2DPowderDiffraction::UnifyXBins( + std::vector<std::vector<double>> &Xbins) const { + // get maximal vector size + size_t max_size = 0; + for (const auto &v : Xbins) { + max_size = std::max(v.size(), max_size); + } + // resize all vectors to maximum size, fill last vector element at the end + for (auto &v : Xbins) { + if (v.size() < max_size) + v.resize(max_size, v.back()); + } + return max_size; } -void Bin2DPowderDiffraction::normalizeToBinArea(MatrixWorkspace_sptr outWS) -{ - NumericAxis *verticalAxis = dynamic_cast<NumericAxis *>(outWS->getAxis(1)); - const std::vector<double> yValues = verticalAxis->getValues(); - auto nhist = outWS->getNumberHistograms(); - g_log.debug() << "Number of hists: " << nhist << " Length of YAxis: " << verticalAxis->length() << std::endl; - - for (size_t idx=0; idx<nhist; ++idx){ - double factor = 1.0/(yValues[idx+1] - yValues[idx]); - // divide by the xBinWidth - outWS->convertToFrequencies(idx); - auto &freqs = outWS->mutableY(idx); - std::transform(freqs.begin(), freqs.end(), freqs.begin(), - std::bind1st(std::multiplies<double>(), factor)); - auto &errors = outWS->mutableE(idx); - std::transform(errors.begin(), errors.end(), errors.begin(), - std::bind1st(std::multiplies<double>(), factor)); - - } +void Bin2DPowderDiffraction::normalizeToBinArea(MatrixWorkspace_sptr outWS) { + NumericAxis *verticalAxis = dynamic_cast<NumericAxis *>(outWS->getAxis(1)); + const std::vector<double> yValues = verticalAxis->getValues(); + auto nhist = outWS->getNumberHistograms(); + g_log.debug() << "Number of hists: " << nhist + << " Length of YAxis: " << verticalAxis->length() << std::endl; + + for (size_t idx = 0; idx < nhist; ++idx) { + double factor = 1.0 / (yValues[idx + 1] - yValues[idx]); + // divide by the xBinWidth + outWS->convertToFrequencies(idx); + auto &freqs = outWS->mutableY(idx); + std::transform(freqs.begin(), freqs.end(), freqs.begin(), + std::bind1st(std::multiplies<double>(), factor)); + auto &errors = outWS->mutableE(idx); + std::transform(errors.begin(), errors.end(), errors.begin(), + std::bind1st(std::multiplies<double>(), factor)); + } } -void convertToDSpacing(double wavelength, double theta, double *d, double *dp) -{ - *d = wavelength*0.5/sin(theta); - *dp = sqrt(wavelength*wavelength - 2.0*log(cos(theta))); +void convertToDSpacing(double wavelength, double theta, double *d, double *dp) { + *d = wavelength * 0.5 / sin(theta); + *dp = sqrt(wavelength * wavelength - 2.0 * log(cos(theta))); } } // namespace Algorithms diff --git a/Framework/Algorithms/test/Bin2DPowderDiffractionTest.h b/Framework/Algorithms/test/Bin2DPowderDiffractionTest.h index 519ed850bf7..56287f1b256 100644 --- a/Framework/Algorithms/test/Bin2DPowderDiffractionTest.h +++ b/Framework/Algorithms/test/Bin2DPowderDiffractionTest.h @@ -32,260 +32,263 @@ using namespace Mantid::Geometry; using namespace Mantid::DataObjects; using Mantid::Algorithms::Bin2DPowderDiffraction; - class Bin2DPowderDiffractionTest : public CxxTest::TestSuite { public: // This pair of boilerplate methods prevent the suite being created statically // This means the constructor isn't called when running other tests - static Bin2DPowderDiffractionTest *createSuite() { return new Bin2DPowderDiffractionTest(); } - static void destroySuite( Bin2DPowderDiffractionTest *suite ) { delete suite; } + static Bin2DPowderDiffractionTest *createSuite() { + return new Bin2DPowderDiffractionTest(); + } + static void destroySuite(Bin2DPowderDiffractionTest *suite) { delete suite; } -//-------------------- Test success -------------------------------------- - void test_Init() - { + //-------------------- Test success -------------------------------------- + void test_Init() { Bin2DPowderDiffraction alg; - TS_ASSERT_THROWS_NOTHING( alg.initialize() ); - TS_ASSERT( alg.isInitialized() ); + TS_ASSERT_THROWS_NOTHING(alg.initialize()); + TS_ASSERT(alg.isInitialized()); } - void test_Binning() - { - using Mantid::API::IAlgorithm; - using Mantid::DataHandling::MoveInstrumentComponent; - using Mantid::Kernel::Unit; - - std::size_t numbins = 1; - EventWorkspace_sptr eventWS = this->createInputWorkspace(numbins); - - int numSpectra = static_cast<int>(eventWS->getNumberHistograms()); - - Bin2DPowderDiffraction alg; - TS_ASSERT_THROWS_NOTHING( alg.initialize() ); - TS_ASSERT( alg.isInitialized() ); - TS_ASSERT_THROWS_NOTHING( alg.setProperty("InputWorkspace", eventWS)); - TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("OutputWorkspace", "_bin2d_test1") ); - TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Axis1Binning", "2,2,6") ); - TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Axis2Binning", "1,2,5") ); - TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("NormalizeByBinArea", "0") ); - TS_ASSERT_THROWS_NOTHING( alg.execute(); ); - TS_ASSERT( alg.isExecuted() ); - - // get and check output workspace - boost::shared_ptr<MatrixWorkspace> outputWS = - AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>("_bin2d_test1"); - TS_ASSERT(outputWS); - - TS_ASSERT_EQUALS(outputWS->getAxis(0)->length(), 3); - TS_ASSERT_EQUALS(outputWS->getAxis(0)->unit()->unitID(), "dSpacing"); - TS_ASSERT_EQUALS(outputWS->getAxis(1)->length(), 3); - TS_ASSERT_EQUALS(outputWS->getAxis(1)->unit()->unitID(), "dSpacingPerpendicular"); - - TS_ASSERT_DELTA((*(outputWS->getAxis(0)))(0), 2.0, 0.0001); - TS_ASSERT_DELTA((*(outputWS->getAxis(0)))(1), 4.0, 0.0001); - TS_ASSERT_DELTA((*(outputWS->getAxis(0)))(2), 6.0, 0.0001); - - TS_ASSERT_DELTA((*(outputWS->getAxis(1)))(0), 1.0, 0.0001); - TS_ASSERT_DELTA((*(outputWS->getAxis(1)))(1), 3.0, 0.0001); - TS_ASSERT_DELTA((*(outputWS->getAxis(1)))(2), 5.0, 0.0001); - - // all events must come into one bin - TS_ASSERT_EQUALS(outputWS->y(0)[0], 0); - TS_ASSERT_EQUALS(outputWS->y(0)[1], 0); - TS_ASSERT_EQUALS(outputWS->y(1)[0], 0); - TS_ASSERT_EQUALS(outputWS->y(1)[1], numbins*numSpectra); - + void test_Binning() { + using Mantid::API::IAlgorithm; + using Mantid::DataHandling::MoveInstrumentComponent; + using Mantid::Kernel::Unit; + + std::size_t numbins = 1; + EventWorkspace_sptr eventWS = this->createInputWorkspace(numbins); + + int numSpectra = static_cast<int>(eventWS->getNumberHistograms()); + + Bin2DPowderDiffraction alg; + TS_ASSERT_THROWS_NOTHING(alg.initialize()); + TS_ASSERT(alg.isInitialized()); + TS_ASSERT_THROWS_NOTHING(alg.setProperty("InputWorkspace", eventWS)); + TS_ASSERT_THROWS_NOTHING( + alg.setPropertyValue("OutputWorkspace", "_bin2d_test1")); + TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("Axis1Binning", "2,2,6")); + TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("Axis2Binning", "1,2,5")); + TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("NormalizeByBinArea", "0")); + TS_ASSERT_THROWS_NOTHING(alg.execute();); + TS_ASSERT(alg.isExecuted()); + + // get and check output workspace + boost::shared_ptr<MatrixWorkspace> outputWS = + AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>( + "_bin2d_test1"); + TS_ASSERT(outputWS); + + TS_ASSERT_EQUALS(outputWS->getAxis(0)->length(), 3); + TS_ASSERT_EQUALS(outputWS->getAxis(0)->unit()->unitID(), "dSpacing"); + TS_ASSERT_EQUALS(outputWS->getAxis(1)->length(), 3); + TS_ASSERT_EQUALS(outputWS->getAxis(1)->unit()->unitID(), + "dSpacingPerpendicular"); + + TS_ASSERT_DELTA((*(outputWS->getAxis(0)))(0), 2.0, 0.0001); + TS_ASSERT_DELTA((*(outputWS->getAxis(0)))(1), 4.0, 0.0001); + TS_ASSERT_DELTA((*(outputWS->getAxis(0)))(2), 6.0, 0.0001); + + TS_ASSERT_DELTA((*(outputWS->getAxis(1)))(0), 1.0, 0.0001); + TS_ASSERT_DELTA((*(outputWS->getAxis(1)))(1), 3.0, 0.0001); + TS_ASSERT_DELTA((*(outputWS->getAxis(1)))(2), 5.0, 0.0001); + + // all events must come into one bin + TS_ASSERT_EQUALS(outputWS->y(0)[0], 0); + TS_ASSERT_EQUALS(outputWS->y(0)[1], 0); + TS_ASSERT_EQUALS(outputWS->y(1)[0], 0); + TS_ASSERT_EQUALS(outputWS->y(1)[1], numbins * numSpectra); } - void test_NormBinArea() - { - using Mantid::API::IAlgorithm; - using Mantid::DataHandling::MoveInstrumentComponent; - using Mantid::Kernel::Unit; - - std::size_t numbins = 1; - EventWorkspace_sptr eventWS = this->createInputWorkspace(numbins); - - Bin2DPowderDiffraction alg; - TS_ASSERT_THROWS_NOTHING( alg.initialize() ); - TS_ASSERT( alg.isInitialized() ); - TS_ASSERT_THROWS_NOTHING( alg.setProperty("InputWorkspace", eventWS)); - TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("OutputWorkspace", "_bin2d_test1") ); - TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Axis1Binning", "2,2,6") ); - TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Axis2Binning", "1,2,5") ); - TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("NormalizeByBinArea", "1") ); - TS_ASSERT_THROWS_NOTHING( alg.execute(); ); - TS_ASSERT( alg.isExecuted() ); - - // get and check output workspace - boost::shared_ptr<MatrixWorkspace> outputWS = - AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>("_bin2d_test1"); - TS_ASSERT(outputWS); - - TS_ASSERT_EQUALS(outputWS->getAxis(0)->length(), 3); - TS_ASSERT_EQUALS(outputWS->getAxis(0)->unit()->unitID(), "dSpacing"); - TS_ASSERT_EQUALS(outputWS->getAxis(1)->length(), 3); - TS_ASSERT_EQUALS(outputWS->getAxis(1)->unit()->unitID(), "dSpacingPerpendicular"); - - TS_ASSERT_DELTA((*(outputWS->getAxis(0)))(0), 2.0, 0.0001); - TS_ASSERT_DELTA((*(outputWS->getAxis(0)))(1), 4.0, 0.0001); - TS_ASSERT_DELTA((*(outputWS->getAxis(0)))(2), 6.0, 0.0001); - - TS_ASSERT_DELTA((*(outputWS->getAxis(1)))(0), 1.0, 0.0001); - TS_ASSERT_DELTA((*(outputWS->getAxis(1)))(1), 3.0, 0.0001); - TS_ASSERT_DELTA((*(outputWS->getAxis(1)))(2), 5.0, 0.0001); - - // all events must come into one bin - TS_ASSERT_EQUALS(outputWS->y(0)[0], 0); - TS_ASSERT_EQUALS(outputWS->y(0)[1], 0); - TS_ASSERT_EQUALS(outputWS->y(1)[0], 0); - TS_ASSERT_DELTA(outputWS->y(1)[1], 6.25, 0.0001); - } + void test_NormBinArea() { + using Mantid::API::IAlgorithm; + using Mantid::DataHandling::MoveInstrumentComponent; + using Mantid::Kernel::Unit; - void test_BinningFromFile() - { - using Mantid::API::IAlgorithm; - using Mantid::DataHandling::MoveInstrumentComponent; - using Mantid::Kernel::Unit; - - std::size_t numbins = 1; - EventWorkspace_sptr eventWS = this->createInputWorkspace(numbins); - std::string binFileName = "bin2dpd_test.txt"; - - this->createBinFile(binFileName); - - Bin2DPowderDiffraction alg; - TS_ASSERT_THROWS_NOTHING( alg.initialize() ); - TS_ASSERT( alg.isInitialized() ); - TS_ASSERT_THROWS_NOTHING( alg.setProperty("InputWorkspace", eventWS)); - TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("OutputWorkspace", "_bin2d_test1") ); - TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("BinEdgesFile", binFileName) ); - TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("NormalizeByBinArea", "0") ); - TS_ASSERT_THROWS_NOTHING( alg.execute(); ); - TS_ASSERT( alg.isExecuted() ); - - // get and check output workspace - boost::shared_ptr<MatrixWorkspace> outputWS = - AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>("_bin2d_test1"); - TS_ASSERT(outputWS); - - TS_ASSERT_EQUALS(outputWS->getAxis(0)->unit()->unitID(), "dSpacing"); - TS_ASSERT_EQUALS(outputWS->getAxis(1)->length(), 3); - TS_ASSERT_EQUALS(outputWS->getAxis(1)->unit()->unitID(), "dSpacingPerpendicular"); - - // bins vary, test x values - TS_ASSERT_DELTA(outputWS->x(0)[0], 1.0, 0.0001); - TS_ASSERT_DELTA(outputWS->x(0)[1], 3.0, 0.0001); - TS_ASSERT_DELTA(outputWS->x(0)[2], 6.0, 0.0001); - TS_ASSERT_DELTA(outputWS->x(0)[3], 6.0, 0.0001); // unify bins - TS_ASSERT_DELTA(outputWS->x(1)[0], 2.0, 0.0001); - TS_ASSERT_DELTA(outputWS->x(1)[1], 4.0, 0.0001); - TS_ASSERT_DELTA(outputWS->x(1)[2], 5.15, 0.0001); - TS_ASSERT_DELTA(outputWS->x(1)[3], 6.0, 0.0001); - - TS_ASSERT_DELTA((*(outputWS->getAxis(1)))(0), 3.0, 0.0001); - TS_ASSERT_DELTA((*(outputWS->getAxis(1)))(1), 4.0, 0.0001); - TS_ASSERT_DELTA((*(outputWS->getAxis(1)))(2), 4.5, 0.0001); - - // 25 events: 5 in one bin and 20 in the other one - TS_ASSERT_EQUALS(outputWS->y(0)[0], 0); - TS_ASSERT_EQUALS(outputWS->y(0)[1], 0); - TS_ASSERT_EQUALS(outputWS->y(1)[0], 0); - TS_ASSERT_EQUALS(outputWS->y(1)[1], 5); - TS_ASSERT_EQUALS(outputWS->y(1)[2], 20); - - std::remove(binFileName.c_str()); // delete file - } + std::size_t numbins = 1; + EventWorkspace_sptr eventWS = this->createInputWorkspace(numbins); -//-------------------- Test failure -------------------------------------- - - void test_Zero2theta() - { - using Mantid::API::IAlgorithm; - using Mantid::DataHandling::MoveInstrumentComponent; - using Mantid::Kernel::Unit; - - EventWorkspace_sptr eventWS = - WorkspaceCreationHelper::createEventWorkspaceWithFullInstrument(1, 5, true); - eventWS->getAxis(0)->setUnit("Wavelength"); - - Bin2DPowderDiffraction alg; - alg.setRethrows(true); - TS_ASSERT_THROWS_NOTHING( alg.initialize() ); - TS_ASSERT( alg.isInitialized() ); - TS_ASSERT_THROWS_NOTHING( alg.setProperty("InputWorkspace", eventWS)); - TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("OutputWorkspace", "_bin2d_test3") ); - TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Axis1Binning", "2,2,6") ); - TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Axis2Binning", "1,2,5") ); - TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("NormalizeByBinArea", "0") ); - TS_ASSERT_THROWS(alg.execute(), std::runtime_error); - TS_ASSERT(!alg.isExecuted()); + Bin2DPowderDiffraction alg; + TS_ASSERT_THROWS_NOTHING(alg.initialize()); + TS_ASSERT(alg.isInitialized()); + TS_ASSERT_THROWS_NOTHING(alg.setProperty("InputWorkspace", eventWS)); + TS_ASSERT_THROWS_NOTHING( + alg.setPropertyValue("OutputWorkspace", "_bin2d_test1")); + TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("Axis1Binning", "2,2,6")); + TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("Axis2Binning", "1,2,5")); + TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("NormalizeByBinArea", "1")); + TS_ASSERT_THROWS_NOTHING(alg.execute();); + TS_ASSERT(alg.isExecuted()); + + // get and check output workspace + boost::shared_ptr<MatrixWorkspace> outputWS = + AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>( + "_bin2d_test1"); + TS_ASSERT(outputWS); + + TS_ASSERT_EQUALS(outputWS->getAxis(0)->length(), 3); + TS_ASSERT_EQUALS(outputWS->getAxis(0)->unit()->unitID(), "dSpacing"); + TS_ASSERT_EQUALS(outputWS->getAxis(1)->length(), 3); + TS_ASSERT_EQUALS(outputWS->getAxis(1)->unit()->unitID(), + "dSpacingPerpendicular"); + + TS_ASSERT_DELTA((*(outputWS->getAxis(0)))(0), 2.0, 0.0001); + TS_ASSERT_DELTA((*(outputWS->getAxis(0)))(1), 4.0, 0.0001); + TS_ASSERT_DELTA((*(outputWS->getAxis(0)))(2), 6.0, 0.0001); + + TS_ASSERT_DELTA((*(outputWS->getAxis(1)))(0), 1.0, 0.0001); + TS_ASSERT_DELTA((*(outputWS->getAxis(1)))(1), 3.0, 0.0001); + TS_ASSERT_DELTA((*(outputWS->getAxis(1)))(2), 5.0, 0.0001); + + // all events must come into one bin + TS_ASSERT_EQUALS(outputWS->y(0)[0], 0); + TS_ASSERT_EQUALS(outputWS->y(0)[1], 0); + TS_ASSERT_EQUALS(outputWS->y(1)[0], 0); + TS_ASSERT_DELTA(outputWS->y(1)[1], 6.25, 0.0001); } -//-------------------- Helpers -------------------------------------- + void test_BinningFromFile() { + using Mantid::API::IAlgorithm; + using Mantid::DataHandling::MoveInstrumentComponent; + using Mantid::Kernel::Unit; + + std::size_t numbins = 1; + EventWorkspace_sptr eventWS = this->createInputWorkspace(numbins); + std::string binFileName = "bin2dpd_test.txt"; + + this->createBinFile(binFileName); + + Bin2DPowderDiffraction alg; + TS_ASSERT_THROWS_NOTHING(alg.initialize()); + TS_ASSERT(alg.isInitialized()); + TS_ASSERT_THROWS_NOTHING(alg.setProperty("InputWorkspace", eventWS)); + TS_ASSERT_THROWS_NOTHING( + alg.setPropertyValue("OutputWorkspace", "_bin2d_test1")); + TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("BinEdgesFile", binFileName)); + TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("NormalizeByBinArea", "0")); + TS_ASSERT_THROWS_NOTHING(alg.execute();); + TS_ASSERT(alg.isExecuted()); + + // get and check output workspace + boost::shared_ptr<MatrixWorkspace> outputWS = + AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>( + "_bin2d_test1"); + TS_ASSERT(outputWS); + + TS_ASSERT_EQUALS(outputWS->getAxis(0)->unit()->unitID(), "dSpacing"); + TS_ASSERT_EQUALS(outputWS->getAxis(1)->length(), 3); + TS_ASSERT_EQUALS(outputWS->getAxis(1)->unit()->unitID(), + "dSpacingPerpendicular"); + + // bins vary, test x values + TS_ASSERT_DELTA(outputWS->x(0)[0], 1.0, 0.0001); + TS_ASSERT_DELTA(outputWS->x(0)[1], 3.0, 0.0001); + TS_ASSERT_DELTA(outputWS->x(0)[2], 6.0, 0.0001); + TS_ASSERT_DELTA(outputWS->x(0)[3], 6.0, 0.0001); // unify bins + TS_ASSERT_DELTA(outputWS->x(1)[0], 2.0, 0.0001); + TS_ASSERT_DELTA(outputWS->x(1)[1], 4.0, 0.0001); + TS_ASSERT_DELTA(outputWS->x(1)[2], 5.15, 0.0001); + TS_ASSERT_DELTA(outputWS->x(1)[3], 6.0, 0.0001); + + TS_ASSERT_DELTA((*(outputWS->getAxis(1)))(0), 3.0, 0.0001); + TS_ASSERT_DELTA((*(outputWS->getAxis(1)))(1), 4.0, 0.0001); + TS_ASSERT_DELTA((*(outputWS->getAxis(1)))(2), 4.5, 0.0001); + + // 25 events: 5 in one bin and 20 in the other one + TS_ASSERT_EQUALS(outputWS->y(0)[0], 0); + TS_ASSERT_EQUALS(outputWS->y(0)[1], 0); + TS_ASSERT_EQUALS(outputWS->y(1)[0], 0); + TS_ASSERT_EQUALS(outputWS->y(1)[1], 5); + TS_ASSERT_EQUALS(outputWS->y(1)[2], 20); + + std::remove(binFileName.c_str()); // delete file + } + + //-------------------- Test failure -------------------------------------- + + void test_Zero2theta() { + using Mantid::API::IAlgorithm; + using Mantid::DataHandling::MoveInstrumentComponent; + using Mantid::Kernel::Unit; + + EventWorkspace_sptr eventWS = + WorkspaceCreationHelper::createEventWorkspaceWithFullInstrument(1, 5, + true); + eventWS->getAxis(0)->setUnit("Wavelength"); + + Bin2DPowderDiffraction alg; + alg.setRethrows(true); + TS_ASSERT_THROWS_NOTHING(alg.initialize()); + TS_ASSERT(alg.isInitialized()); + TS_ASSERT_THROWS_NOTHING(alg.setProperty("InputWorkspace", eventWS)); + TS_ASSERT_THROWS_NOTHING( + alg.setPropertyValue("OutputWorkspace", "_bin2d_test3")); + TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("Axis1Binning", "2,2,6")); + TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("Axis2Binning", "1,2,5")); + TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("NormalizeByBinArea", "0")); + TS_ASSERT_THROWS(alg.execute(), std::runtime_error); + TS_ASSERT(!alg.isExecuted()); + } + + //-------------------- Helpers -------------------------------------- private: EventWorkspace_sptr createInputWorkspace(std::size_t numbins) { - using Mantid::API::IAlgorithm; - using Mantid::DataHandling::MoveInstrumentComponent; - using Mantid::Kernel::Unit; - - EventWorkspace_sptr eventWS = - WorkspaceCreationHelper::createEventWorkspaceWithFullInstrument(1, 5, - true); - // Set the X axes - const auto &xVals = eventWS->x(0); - const size_t xSize = xVals.size(); - auto ax0 = new NumericAxis(xSize); - std::cout << "xSize = " << xSize << std::endl; - // X-axis is 1 <= wavelength <= 6 Angstrom with step of 0.05 - ax0->setUnit("Wavelength"); - for (size_t i = 0; i < xSize; i++) { - ax0->setValue(i, 1.0 + 0.05*xVals[i]); - } - eventWS->replaceAxis(0, ax0); - // detector angles - auto algc = boost::make_shared<MoveInstrumentComponent>(); - algc->initialize(); - algc->setProperty<EventWorkspace_sptr>("Workspace", eventWS); - algc->setPropertyValue("ComponentName", "bank1"); - algc->setProperty("X", 1.0); - algc->setProperty("Y", 0.0); - algc->setProperty("Z", 1.0); - algc->setPropertyValue("RelativePosition", "0"); - algc->execute(); - - int numSpectra = static_cast<int>(eventWS->getNumberHistograms()); - - // add events - // Make up some data for each pixels - for (int i = 0; i < numSpectra; i++) { - // Create one event for each bin - EventList &events = eventWS->getSpectrum(i); - for (std::size_t ie = 0; ie < numbins; ie++) { - // Create a list of events, randomize - events += TofEvent(4.0); - } - events.addDetectorID(Mantid::detid_t(i)); - } - std::cout << "Number of events: " << numbins*numSpectra << std::endl; - - return eventWS; + using Mantid::API::IAlgorithm; + using Mantid::DataHandling::MoveInstrumentComponent; + using Mantid::Kernel::Unit; + + EventWorkspace_sptr eventWS = + WorkspaceCreationHelper::createEventWorkspaceWithFullInstrument(1, 5, + true); + // Set the X axes + const auto &xVals = eventWS->x(0); + const size_t xSize = xVals.size(); + auto ax0 = new NumericAxis(xSize); + std::cout << "xSize = " << xSize << std::endl; + // X-axis is 1 <= wavelength <= 6 Angstrom with step of 0.05 + ax0->setUnit("Wavelength"); + for (size_t i = 0; i < xSize; i++) { + ax0->setValue(i, 1.0 + 0.05 * xVals[i]); + } + eventWS->replaceAxis(0, ax0); + // detector angles + auto algc = boost::make_shared<MoveInstrumentComponent>(); + algc->initialize(); + algc->setProperty<EventWorkspace_sptr>("Workspace", eventWS); + algc->setPropertyValue("ComponentName", "bank1"); + algc->setProperty("X", 1.0); + algc->setProperty("Y", 0.0); + algc->setProperty("Z", 1.0); + algc->setPropertyValue("RelativePosition", "0"); + algc->execute(); + + int numSpectra = static_cast<int>(eventWS->getNumberHistograms()); + + // add events + // Make up some data for each pixels + for (int i = 0; i < numSpectra; i++) { + // Create one event for each bin + EventList &events = eventWS->getSpectrum(i); + for (std::size_t ie = 0; ie < numbins; ie++) { + // Create a list of events, randomize + events += TofEvent(4.0); + } + events.addDetectorID(Mantid::detid_t(i)); + } + std::cout << "Number of events: " << numbins *numSpectra << std::endl; + + return eventWS; } // creates test file with bins description void createBinFile(std::string fname) { - std::ofstream binfile; - binfile.open (fname); - binfile << "#dp_min #dp_max\n"; - binfile << "#d_bins\n"; - binfile << "dp = 3.0 4.0\n"; - binfile << " 1.0 3.0 6.0\n\n"; - binfile << "dp = 4.0 4.5\n"; - binfile << " 2.0 4.0 5.15 6.0\n\n"; - binfile.close(); + std::ofstream binfile; + binfile.open(fname); + binfile << "#dp_min #dp_max\n"; + binfile << "#d_bins\n"; + binfile << "dp = 3.0 4.0\n"; + binfile << " 1.0 3.0 6.0\n\n"; + binfile << "dp = 4.0 4.5\n"; + binfile << " 2.0 4.0 5.15 6.0\n\n"; + binfile.close(); } - - }; - #endif /* MANTID_ALGORITHMS_BIN2DPOWDERDIFFRACTIONTEST_H_ */ -- GitLab