Mantid
Loading...
Searching...
No Matches
CompositeCluster.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8
9#include <numeric>
10
11namespace {
15class Comparitor {
16private:
17 size_t m_label;
18
19public:
20 explicit Comparitor(const size_t &label) : m_label(label) {}
21 bool operator()(const std::shared_ptr<Mantid::Crystal::ICluster> &pCluster) const {
22 return pCluster->containsLabel(m_label);
23 }
24};
25} // namespace
26
27namespace Mantid::Crystal {
28
35CompositeCluster::integrate(std::shared_ptr<const Mantid::API::IMDHistoWorkspace> ws) const {
36
37 double errorIntSQ = 0;
38 double sigInt = 0;
39 // Integrate owned clusters and add those results too.
40 for (const auto &ownedCluster : m_ownedClusters) {
41 auto integratedValues = ownedCluster->integrate(ws);
42 sigInt += integratedValues.get<0>();
43 errorIntSQ += integratedValues.get<1>();
44 }
45 return ClusterIntegratedValues(sigInt, errorIntSQ);
46}
47
52void CompositeCluster::writeTo(std::shared_ptr<Mantid::API::IMDHistoWorkspace> ws) const {
53 for (const auto &ownedCluster : m_ownedClusters) {
54 ownedCluster->writeTo(ws);
55 }
56}
57
64 if (!m_label.is_initialized()) {
65 throw std::runtime_error("No child IClusters. CompositeCluster::getLabel() is not supported.");
66 } else {
67 return m_label.get(); // Assumes all are uniform.
68 }
69}
70
76size_t CompositeCluster::getOriginalLabel() const { return getLabel(); }
77
82size_t CompositeCluster::size() const {
83 return std::accumulate(m_ownedClusters.cbegin(), m_ownedClusters.cend(), static_cast<size_t>(0),
84 [](auto sum, const auto &cluster) { return sum + cluster->size(); });
85}
86
88void CompositeCluster::addIndex(const size_t & /*index*/) {
89 throw std::runtime_error("addIndex not implemented on CompositeCluster");
90}
91
96 if (!m_ownedClusters.empty()) {
97 ICluster *minCluster = m_ownedClusters.front().get();
98 size_t minLabel = minCluster->getLabel();
99 for (size_t i = 1; i < m_ownedClusters.size(); ++i) {
100 size_t temp = m_ownedClusters[i]->getLabel();
101 if (temp < minLabel) {
102 minLabel = temp;
103 }
104 }
105 m_label = minLabel;
106 }
107}
108
113void CompositeCluster::toUniformMinimum(std::vector<DisjointElement> &disjointSet) {
114 if (!m_ownedClusters.empty()) {
115 ICluster *minCluster = m_ownedClusters.front().get();
116 size_t minLabel = minCluster->getLabel();
117 for (size_t i = 1; i < m_ownedClusters.size(); ++i) {
118 size_t temp = m_ownedClusters[i]->getLabel();
119 if (temp < minLabel) {
120 minLabel = temp;
121 minCluster = m_ownedClusters[i].get();
122 }
123 }
124 m_label = minLabel;
125
126 for (auto &ownedCluster : m_ownedClusters) {
127 ownedCluster->setRootCluster(minCluster);
128 ownedCluster->toUniformMinimum(disjointSet);
129 }
130 }
131}
132
138 return this->m_ownedClusters.front()->getRepresentitiveIndex();
139}
140
146 for (auto &ownedCluster : m_ownedClusters) {
147 ownedCluster->setRootCluster(root);
148 }
149}
150
155void CompositeCluster::add(std::shared_ptr<ICluster> &toOwn) {
156 if (toOwn->size() > 0) // Do not add empty clusters.
157 {
158 m_ownedClusters.emplace_back(toOwn);
159 }
160}
161
167bool CompositeCluster::containsLabel(const size_t &label) const {
168 Comparitor comparitor(label);
169 return m_ownedClusters.end() != std::find_if(m_ownedClusters.begin(), m_ownedClusters.end(), comparitor);
170}
171
172} // namespace Mantid::Crystal
ICluster::ClusterIntegratedValues integrate(std::shared_ptr< const Mantid::API::IMDHistoWorkspace > ws) const override
integrate the cluster
size_t getRepresentitiveIndex() const override
Get a representative index of the cluster.
void setRootCluster(ICluster const *root) override
Set the root cluster.
size_t size() const override
Number of indexes tracked.
void toUniformMinimum(std::vector< DisjointElement > &disjointSet) override
Resolve the proper label for this cluster.
size_t getLabel() const override
Get the cluster label.
void add(std::shared_ptr< ICluster > &toOwn)
Own.
bool containsLabel(const size_t &label) const override
Is a given label part of this cluster.
size_t getOriginalLabel() const override
Original label.
void addIndex(const size_t &index) override
Track a linear IMDHistoWorkspace index that belongs to the cluster.
boost::optional< size_t > m_label
Label used by cluster.
void findMinimum() const
Helper method to find the minimum label.
std::vector< std::shared_ptr< ICluster > > m_ownedClusters
Attached clusters.
void writeTo(std::shared_ptr< Mantid::API::IMDHistoWorkspace > ws) const override
Apply labels to the workspace.
ICluster : Abstract cluster.
Definition: ICluster.h:25
virtual size_t getLabel() const =0
Get the cluster label.
boost::tuple< double, double > ClusterIntegratedValues
Definition: ICluster.h:27