14#include "MantidTypes/SpectrumDefinition.h"
16#include <gsl/gsl_statistics.h>
17#include <unordered_map>
44 std::make_shared<HistogramValidator>()),
45 "The input workspace.");
47 "The output workspace.");
48 auto mustBePosInt = std::make_shared<BoundedValidator<int>>();
49 mustBePosInt->setLower(0);
51 "Levels above pixel that will be used to compute the average.\n"
52 "If no level is specified, the median is over the whole instrument.\n If "
53 "0, it will just return the integrated values in each pixel");
64 childAlg->setProperty(
"InputWorkspace", inputWS);
65 childAlg->setProperty(
"StartWorkspaceIndex", 0);
66 childAlg->setProperty(
"EndWorkspaceIndex",
EMPTY_INT());
67 childAlg->setProperty(
"RangeLower", inputWS->getXMin());
68 childAlg->setProperty(
"RangeUpper", inputWS->getXMax());
69 childAlg->setPropertyValue(
"IncludePartialBins",
"1");
70 childAlg->executeAsChildAlg();
74 std::vector<std::vector<size_t>> specmap =
makeMap(integratedWS, parents);
77 const auto &spectrumInfo = integratedWS->spectrumInfo();
78 for (
auto hists : specmap) {
80 std::vector<double> averageYInput, averageEInput;
83 for (
int i = 0; i < static_cast<int>(hists.size()); ++i) {
86 if (spectrumInfo.isMonitor(hists[i]))
88 if (spectrumInfo.isMasked(hists[i]))
91 const double yValue = integratedWS->y(hists[i])[0];
92 const double eValue = integratedWS->e(hists[i])[0];
94 if (!std::isfinite(yValue) || !std::isfinite(eValue))
99 averageYInput.emplace_back(yValue);
100 averageEInput.emplace_back(eValue * eValue);
107 double averageY, averageE;
108 if (averageYInput.empty()) {
109 g_log.
information(
"some group has no valid histograms. Will use 0 for average.");
113 averageY = gsl_stats_mean(&averageYInput[0], 1, averageEInput.size());
114 averageE = std::sqrt(gsl_stats_mean(&averageEInput[0], 1, averageYInput.size()));
118 for (
int i = 0; i < static_cast<int>(hists.size()); ++i) {
120 if (spectrumInfo.isMonitor(hists[i]))
122 if (spectrumInfo.isMasked(hists[i]))
125 const double yValue = integratedWS->y(hists[i])[0];
126 const double eValue = integratedWS->e(hists[i])[0];
127 if (!std::isfinite(yValue) || !std::isfinite(eValue))
132 integratedWS->mutableY(hists[i])[0] = averageY;
133 integratedWS->mutableE(hists[i])[0] = averageE;
153 std::vector<std::vector<size_t>> mymap;
154 std::vector<size_t> single;
156 for (
size_t i = 0; i < countsWS->getNumberHistograms(); i++) {
157 single.emplace_back(i);
159 mymap.emplace_back(single);
172 std::unordered_multimap<size_t, size_t> mymap;
181 const auto spectrumInfo = countsWS->spectrumInfo();
182 const auto &componentInfo = countsWS->componentInfo();
183 for (
size_t i = 0; i < countsWS->getNumberHistograms(); i++) {
184 if (!spectrumInfo.hasDetectors(i)) {
185 g_log.
debug(
"Spectrum has no detector, skipping");
190 size_t ancestor = spectrumInfo.firstDetectorIndex(i);
191 for (
int level = 0; level < parents; ++level) {
192 if (!componentInfo.hasParent(ancestor)) {
193 g_log.
warning(
"Too many levels up. Will ignore LevelsUp");
196 ancestor = componentInfo.parent(ancestor);
198 mymap.emplace(ancestor, i);
201 std::vector<std::vector<size_t>> speclist;
202 std::vector<size_t> speclistsingle;
204 auto s_it = mymap.begin();
205 for (
auto m_it = mymap.begin(); m_it != mymap.end(); m_it = s_it) {
206 const size_t theKey = (*m_it).first;
207 auto keyRange = mymap.equal_range(theKey);
210 speclistsingle.clear();
211 for (s_it = keyRange.first; s_it != keyRange.second; ++s_it) {
212 speclistsingle.emplace_back((*s_it).second);
214 speclist.emplace_back(speclistsingle);
#define DECLARE_ALGORITHM(classname)
#define PARALLEL_START_INTERRUPT_REGION
Begins a block to skip processing is the algorithm has been interupted Note the end of the block if n...
#define PARALLEL_CRITICAL(name)
#define PARALLEL_END_INTERRUPT_REGION
Ends a block to skip processing is the algorithm has been interupted Note the start of the block if n...
#define PARALLEL_FOR_IF(condition)
Empty definitions - to enable set your complier to enable openMP.
#define PARALLEL_CHECK_INTERRUPT_REGION
Adds a check after a Parallel region to see if it was interupted.
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.
Helper class for reporting progress from algorithms.
A property class for workspaces.
void init() override
Initialize the algorithm's properties.
std::vector< std::vector< size_t > > makeMap(const API::MatrixWorkspace_sptr &countsWS, int parents)
method to check which spectra should be averaged
std::vector< std::vector< size_t > > makeInstrumentMap(const API::MatrixWorkspace_sptr &countsWS)
method to create the map with all spectra
int version() const override
Algorithm's version for identification.
const std::string name() const override
Algorithm's name for identification.
const std::string category() const override
Algorithm's category for identification.
void exec() override
Execute the algorithm.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void debug(const std::string &msg)
Logs at debug level.
void warning(const std::string &msg)
Logs at warning level.
void information(const std::string &msg)
Logs at information level.
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::enable_if< std::is_pointer< Arg >::value, bool >::type threadSafe(Arg workspace)
Thread-safety check Checks the workspace to ensure it is suitable for multithreaded access.
constexpr int EMPTY_INT() noexcept
Returns what we consider an "empty" integer within a property.
@ Input
An input workspace.
@ Output
An output workspace.