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 static const std::string roiStr{"roi"};
65 for (const auto &info : links)
66 {
68 if (!seen.insert(std::make_pair(roi.link.persKey(), roi.link.persIndex())).second)
69 // Insert returns false if that item already exists in it
70 return false;
71 }
72 return true;
73 }
74
76 {
77 switch (filter){
78 case FilterType::All:
79 return [](const VecLInfo_t &) { return true; };
81 return uniqueObjects;
83 return uniqueRoIs;
84 default:
85 throw std::runtime_error("Unhandled FilterType enum value!");
86 }
87 }
88
90
92 const std::vector<std::tuple<std::size_t, LInfoItr_t, LInfoItr_t>> &pieces,
93 FilterFunc_t filter)
94 : m_filter(std::move(filter))
95 {
96 std::vector<KFromNItr> idxItrs;
97 idxItrs.reserve(pieces.size());
98 m_linkInfoItrs.reserve(pieces.size());
99 std::size_t size = 0;
100 for (const auto &tup : pieces)
101 {
102 std::size_t multiplicity = std::get<0>(tup);
103 LInfoItr_t begin = std::get<1>(tup);
104 LInfoItr_t end = std::get<2>(tup);
105 idxItrs.emplace_back(multiplicity, std::distance(begin, end));
106 m_linkInfoItrs.push_back(begin);
107 size += multiplicity;
108 }
109 m_idxItr = ProductItr<KFromNItr>(idxItrs, std::vector<KFromNItr>(idxItrs.size()));
110 m_current.assign(size, {});
111 readCurrent();
112 }
113
115 const std::vector<std::tuple<std::size_t, LInfoItr_t, LInfoItr_t>> &pieces,
116 FilterType filter)
117 : IPartCombItr(pieces, getFilter(filter))
118 {
119 }
120
122 {
123 // Reset each individual iterator and set our current value accordingly
124 m_idxItr.reset();
125 readCurrent();
126 }
127
129 {
130 return m_idxItr.exhausted();
131 }
132
134 {
135 if (exhausted())
136 throw std::runtime_error("Dereferencing past-the-end iterator");
137 return m_current;
138 }
139
141 {
142 if (exhausted())
143 throw std::runtime_error("Dereferencing past-the-end iterator");
144 return &m_current;
145 }
146
148 {
149 if (exhausted())
150 // Don't iterate an iterator that is already past the end
151 return *this;
152 ++m_idxItr;
153 readCurrent();
154 return *this;
155 }
156
158 {
159 IPartCombItr ret(*this);
160 this->operator++();
161 return ret;
162 }
163
164 bool IPartCombItr::operator==(const IPartCombItr &other) const
165 {
166 // All past-the-end iterators compare equal
167 if (exhausted() && other.exhausted())
168 return true;
169 return m_idxItr == other.m_idxItr && m_linkInfoItrs == other.m_linkInfoItrs;
170 }
171
172 bool IPartCombItr::operator!=(const IPartCombItr &other) const
173 {
174 return !(*this == other);
175 }
176
178 {
179 if (exhausted())
180 m_current.assign(m_current.size(), {});
181 else
182 {
183 auto currentItr = m_current.begin();
184 for (std::size_t iLeg = 0; iLeg < nLegs(); ++iLeg)
185 {
186 std::vector<std::size_t> indices = *(*m_idxItr)[iLeg];
187 for (std::size_t idx : indices)
188 *(currentItr++) = *(m_linkInfoItrs[iLeg] + idx);
189 }
190 if (!m_filter(m_current))
191 // If we fail the filter condition, advance the iterator again
192 this->operator++();
193 }
194 }
195
196} // 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