ATLAS Offline Software
Loading...
Searching...
No Matches
IPartCombItr.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3*/
4
6#include <set>
9
10namespace {
11 using namespace TrigCompositeUtils;
12 template <typename T>
13 std::ostream &operator<<(std::ostream &os, const LinkInfo<T> &info)
14 {
15 return os << info.source << ", (" << info.link.persKey() << ", " << info.link.persIndex() << ")";
16 }
17
18 template <typename T>
19 std::ostream &operator<<(std::ostream &os, const std::vector<T> &v)
20 {
21 os << "[";
22 if (v.size() > 0)
23 {
24 for (auto itr = v.begin(); itr != v.end() - 1; ++itr)
25 os << *itr << ", ";
26 os << v.back();
27 }
28 return os << "]";
29 }
30}
31
32namespace TrigCompositeUtils
33{
34 bool uniqueObjects(const std::vector<LinkInfo<xAOD::IParticleContainer>> &links)
35 {
36 std::set<const xAOD::IParticle *> seen;
37 for (const auto &info : links)
38 if (!seen.insert(*info.link).second)
39 return false;
40 return true;
41 }
42
44 {
45 std::set<std::pair<uint32_t, uint32_t>> seen;
46 for (const auto &info : links)
47 {
49 if (!seen.insert(std::make_pair(roi.link.persKey(), roi.link.persIndex())).second)
50 // Insert returns false if that item already exists in it
51 return false;
52 }
53 return true;
54 }
55
56 bool uniqueRoIs(const std::vector<LinkInfo<xAOD::IParticleContainer>> &links)
57 {
58 std::set<std::pair<uint32_t, uint32_t>> seen;
59 for (const auto &info : links)
60 {
62 if (!seen.insert(std::make_pair(roi.link.persKey(), roi.link.persIndex())).second)
63 // Insert returns false if that item already exists in it
64 return false;
65 }
66 return true;
67 }
68
69 std::function<bool(const std::vector<LinkInfo<xAOD::IParticleContainer>> &)> getFilter(FilterType filter)
70 {
71 switch (filter)
72 {
73 case FilterType::All:
74 return [](const std::vector<LinkInfo<xAOD::IParticleContainer>> &) { return true; };
76 return uniqueObjects;
78 return uniqueRoIs;
79 default:
80 throw std::runtime_error("Unhandled FilterType enum value!");
81 return {};
82 }
83 }
84
86
88 const std::vector<std::tuple<std::size_t, LInfoItr_t, LInfoItr_t>> &pieces,
89 std::function<bool(const VecLInfo_t &)> filter)
90 : m_filter(std::move(filter))
91 {
92 std::vector<KFromNItr> idxItrs;
93 idxItrs.reserve(pieces.size());
94 m_linkInfoItrs.reserve(pieces.size());
95 std::size_t size = 0;
96 for (const auto &tup : pieces)
97 {
98 std::size_t multiplicity = std::get<0>(tup);
99 LInfoItr_t begin = std::get<1>(tup);
100 LInfoItr_t end = std::get<2>(tup);
101 idxItrs.emplace_back(multiplicity, std::distance(begin, end));
102 m_linkInfoItrs.push_back(begin);
103 size += multiplicity;
104 }
105 m_idxItr = ProductItr<KFromNItr>(idxItrs, std::vector<KFromNItr>(idxItrs.size()));
106 m_current.assign(size, {});
107 readCurrent();
108 }
109
111 const std::vector<std::tuple<std::size_t, LInfoItr_t, LInfoItr_t>> &pieces,
112 FilterType filter)
113 : IPartCombItr(pieces, getFilter(filter))
114 {
115 }
116
118 {
119 // Reset each individual iterator and set our current value accordingly
120 m_idxItr.reset();
121 readCurrent();
122 }
123
125 {
126 return m_idxItr.exhausted();
127 }
128
130 {
131 if (exhausted())
132 throw std::runtime_error("Dereferencing past-the-end iterator");
133 return m_current;
134 }
135
137 {
138 if (exhausted())
139 throw std::runtime_error("Dereferencing past-the-end iterator");
140 return &m_current;
141 }
142
144 {
145 if (exhausted())
146 // Don't iterate an iterator that is already past the end
147 return *this;
148 ++m_idxItr;
149 readCurrent();
150 return *this;
151 }
152
154 {
155 IPartCombItr ret(*this);
156 this->operator++();
157 return ret;
158 }
159
160 bool IPartCombItr::operator==(const IPartCombItr &other) const
161 {
162 // All past-the-end iterators compare equal
163 if (exhausted() && other.exhausted())
164 return true;
165 return m_idxItr == other.m_idxItr && m_linkInfoItrs == other.m_linkInfoItrs;
166 }
167
168 bool IPartCombItr::operator!=(const IPartCombItr &other) const
169 {
170 return !(*this == other);
171 }
172
174 {
175 if (exhausted())
176 m_current.assign(m_current.size(), {});
177 else
178 {
179 auto currentItr = m_current.begin();
180 for (std::size_t iLeg = 0; iLeg < nLegs(); ++iLeg)
181 {
182 std::vector<std::size_t> indices = *(*m_idxItr)[iLeg];
183 for (std::size_t idx : indices)
184 *(currentItr++) = *(m_linkInfoItrs[iLeg] + idx);
185 }
186 if (!m_filter(m_current))
187 // If we fail the filter condition, advance the iterator again
188 this->operator++();
189 }
190 }
191
192} // namespace TrigCompositeUtils
std::ostream & operator<<(std::ostream &lhs, const TestGaudiProperty &rhs)
stored_index_type persIndex() const
Return the index of the link.
sgkey_t persKey() const
Return the SG key that we reference, as a hash.
void reset()
Reset the iterator to its starting point.
std::vector< LInfoItr_t > m_linkInfoItrs
std::size_t size() const
The size of each combination.
IPartCombItr()
A default constructed iterator acts as a past-the-end iterator.
bool operator!=(const IPartCombItr &other) const
std::function< bool(const VecLInfo_t &)> m_filter
std::vector< LinkInfo< xAOD::IParticleContainer > > VecLInfo_t
IPartCombItr & operator++()
Pre-increment operator.
std::size_t nLegs() const
The number of legs.
VecLInfo_t::const_iterator LInfoItr_t
reference operator*() const
Dereference.
ProductItr< KFromNItr > m_idxItr
bool exhausted() const
True if this iterator is past the end.
bool operator==(const IPartCombItr &other) const
Iterator comparison functions.
Iterates over all combinations of the provided input iterators.
Definition ProductItr.h:43
bool uniqueObjects(const std::vector< LinkInfo< xAOD::IParticleContainer > > &links)
Helper fucntion that returns true if no objects are repeated.
bool uniqueRoIs(const std::vector< LinkInfo< xAOD::IParticleContainer > > &links)
Helper function that returns true if no objects share a final RoI.
std::function< bool(const std::vector< LinkInfo< xAOD::IParticleContainer > > &)> getFilter(FilterType filter)
Get a lambda corresponding to the specified FilterType enum.
bool uniqueInitialRoIs(const std::vector< LinkInfo< xAOD::IParticleContainer > > &links)
Helper function that returns true if no objects share an initial RoI.
@ UniqueRoIs
Do not allow any two objects to share an RoI.
@ All
Allow all combinations.
@ UniqueObjects
Do not allow any repeated objects.
LinkInfo< T > findLink(const Decision *start, const std::string &linkName, const bool suppressMultipleLinksWarning=false)
Perform a recursive search for ElementLinks of type T and name 'linkName', starting from Decision obj...
const std::string & initialRoIString()
STL namespace.
Helper to keep a Decision object, ElementLink and ActiveState (with respect to some requested ChainGr...
Definition LinkInfo.h:22
ElementLink< T > link
Link to the feature.
Definition LinkInfo.h:55