ATLAS Offline Software
Loading...
Searching...
No Matches
IPartCombItr.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6
10#include <set>
11#include <ostream>
12
13namespace {
14 using namespace TrigCompositeUtils;
15 template <typename T>
16 std::ostream &operator<<(std::ostream &os, const LinkInfo<T> &info)
17 {
18 return os << info.source << ", (" << info.link.persKey() << ", " << info.link.persIndex() << ")";
19 }
20
21 template <typename T>
22 std::ostream &operator<<(std::ostream &os, const std::vector<T> &v)
23 {
24 os << "[";
25 if (v.size() > 0)
26 {
27 for (auto itr = v.begin(); itr != v.end() - 1; ++itr)
28 os << *itr << ", ";
29 os << v.back();
30 }
31 return os << "]";
32 }
33}
34
35namespace TrigCompositeUtils
36{
37 bool uniqueObjects(const std::vector<LinkInfo<xAOD::IParticleContainer>> &links)
38 {
39 std::set<const xAOD::IParticle *> seen;
40 for (const auto &info : links)
41 if (!seen.insert(*info.link).second)
42 return false;
43 return true;
44 }
45
47 {
48 const EventContext& ctx = Gaudi::Hive::currentContext();
49 std::set<std::pair<uint32_t, uint32_t>> seen;
50 for (const auto &info : links)
51 {
53 if (!seen.insert(std::make_pair(roi.link.persKey(), roi.link.persIndex())).second)
54 // Insert returns false if that item already exists in it
55 return false;
56 }
57 return true;
58 }
59
60 bool uniqueRoIs(const std::vector<LinkInfo<xAOD::IParticleContainer>> &links)
61 {
62 const EventContext& ctx = Gaudi::Hive::currentContext();
63 std::set<std::pair<uint32_t, uint32_t>> seen;
64 for (const auto &info : links)
65 {
67 if (!seen.insert(std::make_pair(roi.link.persKey(), roi.link.persIndex())).second)
68 // Insert returns false if that item already exists in it
69 return false;
70 }
71 return true;
72 }
73
75 {
76 switch (filter){
77 case FilterType::All:
78 return [](const VecLInfo_t &) { return true; };
80 return uniqueObjects;
82 return uniqueRoIs;
83 default:
84 throw std::runtime_error("Unhandled FilterType enum value!");
85 }
86 }
87
89
91 const std::vector<std::tuple<std::size_t, LInfoItr_t, LInfoItr_t>> &pieces,
92 FilterFunc_t filter)
93 : m_filter(std::move(filter))
94 {
95 std::vector<KFromNItr> idxItrs;
96 idxItrs.reserve(pieces.size());
97 m_linkInfoItrs.reserve(pieces.size());
98 std::size_t size = 0;
99 for (const auto &tup : pieces)
100 {
101 std::size_t multiplicity = std::get<0>(tup);
102 LInfoItr_t begin = std::get<1>(tup);
103 LInfoItr_t end = std::get<2>(tup);
104 idxItrs.emplace_back(multiplicity, std::distance(begin, end));
105 m_linkInfoItrs.push_back(begin);
106 size += multiplicity;
107 }
108 m_idxItr = ProductItr<KFromNItr>(idxItrs, std::vector<KFromNItr>(idxItrs.size()));
109 m_current.assign(size, {});
110 readCurrent();
111 }
112
114 const std::vector<std::tuple<std::size_t, LInfoItr_t, LInfoItr_t>> &pieces,
115 FilterType filter)
116 : IPartCombItr(pieces, getFilter(filter))
117 {
118 }
119
121 {
122 // Reset each individual iterator and set our current value accordingly
123 m_idxItr.reset();
124 readCurrent();
125 }
126
128 {
129 return m_idxItr.exhausted();
130 }
131
133 {
134 if (exhausted())
135 throw std::runtime_error("Dereferencing past-the-end iterator");
136 return m_current;
137 }
138
140 {
141 if (exhausted())
142 throw std::runtime_error("Dereferencing past-the-end iterator");
143 return &m_current;
144 }
145
147 {
148 if (exhausted())
149 // Don't iterate an iterator that is already past the end
150 return *this;
151 ++m_idxItr;
152 readCurrent();
153 return *this;
154 }
155
157 {
158 IPartCombItr ret(*this);
159 this->operator++();
160 return ret;
161 }
162
163 bool IPartCombItr::operator==(const IPartCombItr &other) const
164 {
165 // All past-the-end iterators compare equal
166 if (exhausted() && other.exhausted())
167 return true;
168 return m_idxItr == other.m_idxItr && m_linkInfoItrs == other.m_linkInfoItrs;
169 }
170
171 bool IPartCombItr::operator!=(const IPartCombItr &other) const
172 {
173 return !(*this == other);
174 }
175
177 {
178 if (exhausted())
179 m_current.assign(m_current.size(), {});
180 else
181 {
182 auto currentItr = m_current.begin();
183 for (std::size_t iLeg = 0; iLeg < nLegs(); ++iLeg)
184 {
185 std::vector<std::size_t> indices = *(*m_idxItr)[iLeg];
186 for (std::size_t idx : indices)
187 *(currentItr++) = *(m_linkInfoItrs[iLeg] + idx);
188 }
189 if (!m_filter(m_current))
190 // If we fail the filter condition, advance the iterator again
191 this->operator++();
192 }
193 }
194
195} // 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::vector< LinkInfo< xAOD::IParticleContainer > > VecLInfo_t
std::function< bool(const IPartCombItr::VecLInfo_t &)> FilterFunc_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.
static FilterFunc_t getFilter(FilterType filter)
Get a function corresponding to the specified FilterType enum.
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 function 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.
bool uniqueInitialRoIs(const std::vector< LinkInfo< xAOD::IParticleContainer > > &links)
Helper function that returns true if no objects share an initial RoI.
LinkInfo< T > findLink(const EventContext &ctx, 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...
@ UniqueRoIs
Do not allow any two objects to share an RoI.
@ All
Allow all combinations.
@ UniqueObjects
Do not allow any repeated objects.
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