ATLAS Offline Software
Loading...
Searching...
No Matches
AthenaHitsVector.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5//
6// Templated class for the Hit collections in athena
7// There is a bunch of ifdef __CINT__ to make this class
8// intelligible to AthenaRoot and work out a persistency mechanism
9//
10
11#ifndef AthenaHitsVector_H
12#define AthenaHitsVector_H
13//
14//
15// vector class
16#include <concepts>
17#include <memory>
18#include <type_traits>
19#include <vector>
20
22#include "boost/iterator/transform_iterator.hpp"
23
24//
25// Gaudi includes, not provided to rootcint
26#ifndef __CINT__
28#include "GaudiKernel/IMessageSvc.h"
29#include "GaudiKernel/ISvcLocator.h"
30#include "GaudiKernel/MsgStream.h"
31#endif
32
34 // This base class is used to store AthenaHitsVector
35 // and AtlasHitsVector in the same container, avoiding std::any RTTI.
36 // This should stay empty other than the virtual destructor which is required
37 // when converting a std::unique_ptr<Derived> to std::unique_ptr<Base>.
38 virtual ~HitsVectorBase() = default;
39};
40
41
42namespace AthHitVec{
47
49 template <typename Cont_t> concept isHitVectorBase = std::is_base_of_v<HitsVectorBase, Cont_t>;
50
55 template <class ContainerT, class AuxContainerT>
57 using container_type = ContainerT;
58 using aux_container_type = AuxContainerT;
59
61 container->setStore(auxContainer.get());
62 }
63
64 std::unique_ptr<ContainerT> container{std::make_unique<ContainerT>()};
65 std::unique_ptr<AuxContainerT> auxContainer{std::make_unique<AuxContainerT>()};
66 };
67
70 template <typename Cont_t>
73 requires(Cont_t& collection) {
74 typename Cont_t::container_type;
75 typename Cont_t::aux_container_type;
76 { collection.container } ->
77 std::same_as<std::unique_ptr<typename Cont_t::container_type>&>;
78 { collection.auxContainer } ->
79 std::same_as<std::unique_ptr<typename Cont_t::aux_container_type>&>;
80 };
81}
82
83
84
85//
86template <typename T>
88 public:
89 //
90 // additional typedef
91 using base_value_type = T;
92 using CONT = std::vector<T*>;
93 using value_type = typename CONT::value_type;
94 using pointer = typename CONT::pointer;
95 using reference = typename CONT::reference;
96 using iterator = typename CONT::iterator;
97 using size_type = typename CONT::size_type;
98 using difference_type = typename CONT::difference_type;
99 using const_pointer = const T* const*;
100 using const_reference = const T* const&;
101
102 struct make_const {
103 const T* operator()(const T* x) const { return x; }
104 };
106 boost::transform_iterator<make_const, typename CONT::const_iterator>;
107#ifdef __CINT__
108 // default constructor for rootcint
110#else
111 // methods not provided to rootcint
112 AthenaHitsVector(const std::string& collectionName = "DefaultCollectionName",
114 : m_name (collectionName),
115 m_ownPolicy (ownPolicy)
116 {
117 IMessageSvc* msgSvc(Athena::getMessageSvc());
118 MsgStream log(msgSvc, "AthenaHitsVector");
119 log << MSG::DEBUG << " initialized " << collectionName
120 << " with ownership policy " << m_ownPolicy << endmsg;
121 }
122 ~AthenaHitsVector() override { Clear(); }
123
124 void Clear() {
125 // delete pointers if we own the elements
127 for (unsigned int i = 0; i < m_hitvector.size(); i++)
128 delete m_hitvector[i];
129 }
130 m_hitvector.clear();
131 }
132
134 // delete pointers if we own the elements
136 for (unsigned int i = 0; i < m_hitvector.size(); i++)
137 delete m_hitvector[i];
138 }
139 m_hitvector.clear();
140 m_ownPolicy = ownPolicy;
141 }
142
143 void Insert(T* h) { m_hitvector.push_back(h); }
144 int Size() const { return size(); }
145#endif // __CINT__
146
150 m_hitvector.reserve(rhs.m_hitvector.size());
151 const_iterator i(rhs.begin()), e(rhs.end());
152 while (i != e) {
153 m_hitvector.push_back((nullptr != *i) ? new T(**i) : nullptr);
154 ++i;
155 }
156 }
157
160 // cppcheck-suppress operatorEqVarError; m_ownPolicy deliberately not copied
162 if (this != &rhs) {
163 this->Clear();
165 m_hitvector.reserve(rhs.m_hitvector.size());
166 const_iterator i(rhs.begin()), e(rhs.end());
167 while (i != e) {
168 m_hitvector.push_back((nullptr != *i) ? new T(**i) : nullptr);
169 ++i;
170 }
171 } else {
172 this->m_hitvector = rhs.m_hitvector;
173 }
174 }
175 return *this;
176 }
177
178 const std::string& Name() const { return m_name; }
179
180 void setName(const std::string& name) { m_name = name; }
181 //
182 // vector methods.
183 const std::vector<T*>& getVector() { return m_hitvector; }
184
185 bool empty() const { return m_hitvector.empty(); }
186
188 return const_iterator(m_hitvector.begin(), make_const());
189 }
190
192 return const_iterator(m_hitvector.end(), make_const());
193 }
194
195 iterator begin() { return m_hitvector.begin(); }
196
197 iterator end() { return m_hitvector.end(); }
198
199 size_type size() const { return m_hitvector.size(); }
200
201 void push_back(T* t) { m_hitvector.push_back(t); }
202 void push_back(std::unique_ptr<T> t) { m_hitvector.push_back(t.release()); }
203
204 const T* At(unsigned int pos) const { return m_hitvector.at(pos); }
205
206 const T* operator[](size_type n) const { return m_hitvector[n]; }
207
209 if (sz < size()) {
211 iterator i(m_hitvector.begin() + sz), e(m_hitvector.end());
212 while (i != e) {
213 delete *i++;
214 }
215 }
216 m_hitvector.resize(sz);
217 } else {
218 m_hitvector.insert(m_hitvector.end(), sz - m_hitvector.size(), nullptr);
219 }
220 }
221
222 void clear() {
224 for (unsigned int i = 0; i < m_hitvector.size(); i++)
225 delete m_hitvector[i];
226 }
227 m_hitvector.clear();
228 }
229
230 void reserve(size_type n) { m_hitvector.reserve(n); }
231
232 protected:
233 std::string m_name;
234 std::vector<T*> m_hitvector;
236
237 public:
238 // Used to ensure that the DVLInfo gets registered
239 // when the dictionary for this class is loaded.
240 static const std::type_info* initHelper() {
242 };
243 static const std::type_info* const s_info;
244};
245
254template <class T>
255void dvl_makecontainer(size_t nreserve, AthenaHitsVector<T>*& cont) {
256 cont = new AthenaHitsVector<T>;
257 cont->reserve(nreserve);
258}
259
260// Ensure that the DVLInfo gets registered
261// when the dictionary for this class is loaded.
262template <class T>
263const std::type_info* const AthenaHitsVector<T>::s_info =
265
266#endif
#define endmsg
void dvl_makecontainer(size_t nreserve, AthenaHitsVector< T > *&cont)
Construct a new container.
Holder to implement conversion copies for DataVector/DataList.
static Double_t sz
#define x
Header file for AthHistogramAlgorithm.
const SiHit *const & const_reference
std::vector< SiHit * > m_hitvector
const_iterator end() const
void reserve(size_type n)
void Clear(AthHitVec::OwnershipPolicy ownPolicy)
typename CONT::reference reference
const std::vector< T * > & getVector()
void setName(const std::string &name)
typename CONT::value_type value_type
AthenaHitsVector(const AthenaHitsVector< T > &rhs)
copy constructor makes deep copy of elements, as by default the container is AthHitVec::OWN_ELEMENTS
AthenaHitsVector(const std::string &collectionName="DefaultCollectionName", AthHitVec::OwnershipPolicy ownPolicy=AthHitVec::OWN_ELEMENTS)
const SiHit *const * const_pointer
typename CONT::difference_type difference_type
void push_back(std::unique_ptr< T > t)
typename CONT::iterator iterator
const T * At(unsigned int pos) const
AthenaHitsVector< T > & operator=(const AthenaHitsVector< T > &rhs)
assignment deletes old elements and copies the new ones deep copy if AthHitVec::OWN_ELEMENTS shallow ...
typename CONT::pointer pointer
const_iterator begin() const
const T * operator[](size_type n) const
size_type size() const
const std::string & Name() const
boost::transform_iterator< make_const, typename CONT::const_iterator > const_iterator
static const std::type_info *const s_info
static const std::type_info * initHelper()
AthHitVec::OwnershipPolicy m_ownPolicy
typename CONT::size_type size_type
std::vector< SiHit * > CONT
~AthenaHitsVector() override
void resize(size_type sz)
Define the concept for hit-collection carriers owning an xAOD container and its auxiliary store.
Define the concept that the struct needs to inherit from the HitsVectorBase.
singleton-like access to IMessageSvc via open function and helper
@ OWN_ELEMENTS
this data object owns its elements
@ VIEW_ELEMENTS
this data object is a view, does not own its elmts
IMessageSvc * getMessageSvc(bool quiet=false)
const T * operator()(const T *x) const
virtual ~HitsVectorBase()=default