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 <algorithm>
10#include <numeric>
11#include <stdexcept>
12
13namespace {
17class Comparitor {
18private:
19 size_t m_label;
20
21public:
22 explicit Comparitor(const size_t &label) : m_label(label) {}
23 bool operator()(const std::shared_ptr<Mantid::Crystal::ICluster> &pCluster) const {
24 return pCluster->containsLabel(m_label);
25 }
26};
27} // namespace
28
29namespace Mantid::Crystal {
30
37CompositeCluster::integrate(std::shared_ptr<const Mantid::API::IMDHistoWorkspace> ws) const {
38
39 double errorIntSQ = 0;
40 double sigInt = 0;
41 // Integrate owned clusters and add those results too.
42 for (const auto &ownedCluster : m_ownedClusters) {
43 auto integratedValues = ownedCluster->integrate(ws);
44 sigInt += integratedValues.get<0>();
45 errorIntSQ += integratedValues.get<1>();
46 }
47 return ClusterIntegratedValues(sigInt, errorIntSQ);
48}
49
54void CompositeCluster::writeTo(std::shared_ptr<Mantid::API::IMDHistoWorkspace> ws) const {
55 for (const auto &ownedCluster : m_ownedClusters) {
56 ownedCluster->writeTo(ws);
57 }
58}
59
66 if (!m_label.has_value()) {
67 throw std::runtime_error("No child IClusters. CompositeCluster::getLabel() is not supported.");
68 } else {
69 return m_label.value(); // Assumes all are uniform.
70 }
71}
72
78size_t CompositeCluster::getOriginalLabel() const { return getLabel(); }
79
84size_t CompositeCluster::size() const {
85 return std::accumulate(m_ownedClusters.cbegin(), m_ownedClusters.cend(), static_cast<size_t>(0),
86 [](auto sum, const auto &cluster) { return sum + cluster->size(); });
87}
88
90void CompositeCluster::addIndex(const size_t & /*index*/) {
91 throw std::runtime_error("addIndex not implemented on CompositeCluster");
92}
93
98 if (!m_ownedClusters.empty()) {
99 ICluster const *minCluster = m_ownedClusters.front().get();
100 size_t minLabel = minCluster->getLabel();
101 for (size_t i = 1; i < m_ownedClusters.size(); ++i) {
102 size_t temp = m_ownedClusters[i]->getLabel();
103 if (temp < minLabel) {
104 minLabel = temp;
105 }
106 }
107 m_label = minLabel;
108 }
109}
110
115void CompositeCluster::toUniformMinimum(std::vector<DisjointElement> &disjointSet) {
116 if (!m_ownedClusters.empty()) {
117 ICluster const *minCluster = m_ownedClusters.front().get();
118 size_t minLabel = minCluster->getLabel();
119 for (size_t i = 1; i < m_ownedClusters.size(); ++i) {
120 size_t temp = m_ownedClusters[i]->getLabel();
121 if (temp < minLabel) {
122 minLabel = temp;
123 minCluster = m_ownedClusters[i].get();
124 }
125 }
126 m_label = minLabel;
127
128 for (auto &ownedCluster : m_ownedClusters) {
129 ownedCluster->setRootCluster(minCluster);
130 ownedCluster->toUniformMinimum(disjointSet);
131 }
132 }
133}
134
140 return this->m_ownedClusters.front()->getRepresentitiveIndex();
141}
142
148 for (auto &ownedCluster : m_ownedClusters) {
149 ownedCluster->setRootCluster(root);
150 }
151}
152
157void CompositeCluster::add(std::shared_ptr<ICluster> &toOwn) {
158 if (toOwn->size() > 0) // Do not add empty clusters.
159 {
160 m_ownedClusters.emplace_back(toOwn);
161 }
162}
163
169bool CompositeCluster::containsLabel(const size_t &label) const {
170 Comparitor comparitor(label);
171 return m_ownedClusters.end() != std::find_if(m_ownedClusters.begin(), m_ownedClusters.end(), comparitor);
172}
173
174} // 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.
std::optional< size_t > m_label
Label used by cluster.
void addIndex(const size_t &index) override
Track a linear IMDHistoWorkspace index that belongs to the 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:24
virtual size_t getLabel() const =0
Get the cluster label.
virtual void setRootCluster(ICluster const *root)=0
Set the root cluster.
boost::tuple< double, double > ClusterIntegratedValues
Definition ICluster.h:26