12#include "MantidHistogramData/Histogram.h"
13#include "MantidIndexing/IndexInfo.h"
29 this->useAlgorithm(
"DiffractionFocussing", 2);
32using namespace Kernel;
33using namespace HistogramData;
45 "The input workspace");
47 "The result of diffraction focussing of InputWorkspace");
49 "The name of the CalFile with grouping data");
61 std::string groupingFileName =
getProperty(
"GroupingFileName");
66 bool dist = inputW->isDistribution();
77 std::set<int64_t> groupNumbers;
78 std::transform(detectorGroups.cbegin(), detectorGroups.cend(), std::inserter(groupNumbers, groupNumbers.begin()),
79 [](
const auto &e) { return e.first; });
82 auto iprogress_count =
static_cast<int>(groupNumbers.size());
83 int iprogress_step = iprogress_count / 100;
84 if (iprogress_step == 0)
86 std::vector<int64_t> resultIndeces;
87 for (
auto groupNumber : groupNumbers) {
88 if (iprogress++ % iprogress_step == 0) {
89 progress(0.68 +
double(iprogress) / iprogress_count / 3);
91 auto from = detectorGroups.lower_bound(groupNumber);
92 auto to = detectorGroups.upper_bound(groupNumber);
94 for (
auto d = from;
d != to; ++
d)
98 childAlg->setProperty(
"Workspace", tmpW);
99 childAlg->setProperty<std::vector<detid_t>>(
"DetectorList",
detectorList);
100 childAlg->executeAsChildAlg();
103 int ri = childAlg->getProperty(
"ResultIndex");
105 resultIndeces.emplace_back(ri);
108 throw std::runtime_error(
"Unable to get Properties from GroupDetectors Child Algorithm");
115 const int64_t oldHistNumber = tmpW->getNumberHistograms();
116 API::Axis *spectraAxis = tmpW->getAxis(1);
117 for (int64_t i = 0; i < oldHistNumber; i++)
118 if (spectraAxis->
spectraNo(i) >= 0 && find(resultIndeces.begin(), resultIndeces.end(), i) == resultIndeces.end()) {
121 g_log.
warning() <<
"Discarded " << discarded <<
" spectra that were not assigned to any group\n";
126 int64_t newSize = tmpW->blocksize();
128 DataObjects::create<API::MatrixWorkspace>(*tmpW, resultIndeces.size(), BinEdges(newSize + 1));
130 std::vector<Indexing::SpectrumNumber> specNums;
131 const auto &tmpIndices = tmpW->indexInfo();
132 for (int64_t hist = 0; hist < static_cast<int64_t>(resultIndeces.size()); hist++) {
133 int64_t i = resultIndeces[hist];
134 outputW->setHistogram(hist, tmpW->histogram(i));
135 specNums.emplace_back(tmpIndices.spectrumNumber(i));
137 auto outputIndices = outputW->indexInfo();
138 outputIndices.setSpectrumNumbers(std::move(specNums));
139 outputW->setIndexInfo(outputIndices);
143 outputW->setDistribution(dist);
151 const std::string CONVERSION_UNIT =
"dSpacing";
155 g_log.
information() <<
"Converting units from " << xUnit->label().ascii() <<
" to " << CONVERSION_UNIT <<
".\n";
158 childAlg->setProperty(
"InputWorkspace",
workspace);
159 childAlg->setPropertyValue(
"Target", CONVERSION_UNIT);
160 childAlg->executeAsChildAlg();
162 return childAlg->getProperty(
"OutputWorkspace");
173 std::vector<double> paramArray{min, -step, max};
175 g_log.
information() <<
"Rebinning from " << min <<
" to " << max <<
" in " << step <<
" logaritmic steps.\n";
179 childAlg->setProperty<std::vector<double>>(
"Params", paramArray);
180 childAlg->executeAsChildAlg();
181 workspace = childAlg->getProperty(
"OutputWorkspace");
193 double &max,
double &step) {
195 min = std::numeric_limits<double>::max();
198 int64_t length =
workspace->getNumberHistograms();
199 for (int64_t i = 0; i < length; i++) {
201 const double &localMin = xVec.front();
202 const double &localMax = xVec.back();
203 if (std::isfinite(localMin) && std::isfinite(localMax)) {
204 min = std::min(min, localMin);
205 max = std::max(max, localMax);
213 auto n =
static_cast<double>(
workspace->blocksize());
214 step = (log(max) - log(min)) /
n;
224 std::ifstream grFile(groupingFileName.c_str());
226 g_log.
error() <<
"Unable to open grouping file " << groupingFileName <<
'\n';
230 std::multimap<int64_t, int64_t> detectorGroups;
232 while (getline(grFile, str)) {
233 if (str.empty() || str[0] ==
'#')
235 std::istringstream istr(str);
236 int n, udet, sel, group;
238 istr >>
n >> udet >> offset >> sel >> group;
242 if ((sel) && (group > 0)) {
243 detectorGroups.emplace(group, udet);
246 return detectorGroups;
#define DECLARE_ALGORITHM(classname)
IPeaksWorkspace_sptr workspace
Base class from which all concrete algorithm classes should be derived.
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
Class to represent the axis of a workspace.
virtual specnum_t spectraNo(const std::size_t &index) const
Get the spectrum number.
Class for marking algorithms as deprecated.
A specialized class for dealing with file properties.
@ Load
allowed here which will be passed to the algorithm
Base MatrixWorkspace Abstract Class.
A property class for workspaces.
This is a parent algorithm that uses several different child algorithms to perform it's task.
void init() override
Initialisation method.
void RebinWorkspace(API::MatrixWorkspace_sptr &workspace)
Run Rebin as a Child Algorithm to harmonise the bin boundaries.
void exec() override
Executes the algorithm.
void calculateRebinParams(const API::MatrixWorkspace_const_sptr &workspace, double &min, double &max, double &step)
Calculates rebin parameters: the min and max bin boundaries and the logarithmic step.
std::multimap< int64_t, int64_t > readGroupingFile(const std::string &groupingFileName)
Reads in the file with the grouping information.
API::MatrixWorkspace_sptr convertUnitsToDSpacing(const API::MatrixWorkspace_sptr &workspace)
Run ConvertUnits as a Child Algorithm to convert to dSpacing.
Records the filename and the description of failure.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void error(const std::string &msg)
Logs at error level.
void warning(const std::string &msg)
Logs at warning level.
void information(const std::string &msg)
Logs at information level.
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< const Unit > Unit_const_sptr
Shared pointer to the Unit base class (const version)
int32_t detid_t
Typedef for a detector ID.
@ Input
An input workspace.
@ Output
An output workspace.