ATLAS Offline Software
Loading...
Searching...
No Matches
AthenaHitsVector.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 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 <vector>
17
19#include "boost/iterator/transform_iterator.hpp"
20
21//
22// Gaudi includes, not provided to rootcint
23#ifndef __CINT__
25#include "GaudiKernel/IMessageSvc.h"
26#include "GaudiKernel/ISvcLocator.h"
27#include "GaudiKernel/MsgStream.h"
28#endif
29
30namespace AthHitVec{
35}
36
37
39 // This base class is used to store AthenaHitsVector
40 // and AtlasHitsVector in the same container, avoiding std::any RTTI.
41 // This should stay empty other than the virtual destructor which is required
42 // when converting a std::unique_ptr<Derived> to std::unique_ptr<Base>.
43 virtual ~HitsVectorBase() = default;
44};
45
46//
47template <typename T>
49 public:
50 //
51 // additional typedef
52 using base_value_type = T;
53 using CONT = std::vector<T*>;
54 using value_type = typename CONT::value_type;
55 using pointer = typename CONT::pointer;
56 using reference = typename CONT::reference;
57 using iterator = typename CONT::iterator;
58 using size_type = typename CONT::size_type;
59 using difference_type = typename CONT::difference_type;
60 using const_pointer = const T* const*;
61 using const_reference = const T* const&;
62
63 struct make_const {
64 const T* operator()(const T* x) const { return x; }
65 };
67 boost::transform_iterator<make_const, typename CONT::const_iterator>;
68#ifdef __CINT__
69 // default constructor for rootcint
71#else
72 // methods not provided to rootcint
73 AthenaHitsVector(const std::string& collectionName = "DefaultCollectionName",
75 : m_name (collectionName),
76 m_ownPolicy (ownPolicy)
77 {
78 IMessageSvc* msgSvc(Athena::getMessageSvc());
79 MsgStream log(msgSvc, "AthenaHitsVector");
80 log << MSG::DEBUG << " initialized " << collectionName
81 << " with ownership policy " << m_ownPolicy << endmsg;
82 }
83 ~AthenaHitsVector() override { Clear(); }
84
85 void Clear() {
86 // delete pointers if we own the elements
88 for (unsigned int i = 0; i < m_hitvector.size(); i++)
89 delete m_hitvector[i];
90 }
91 m_hitvector.clear();
92 }
93
95 // delete pointers if we own the elements
97 for (unsigned int i = 0; i < m_hitvector.size(); i++)
98 delete m_hitvector[i];
99 }
100 m_hitvector.clear();
101 m_ownPolicy = ownPolicy;
102 }
103
104 void Insert(T* h) { m_hitvector.push_back(h); }
105 int Size() const { return size(); }
106#endif // __CINT__
107
111 m_hitvector.reserve(rhs.m_hitvector.size());
112 const_iterator i(rhs.begin()), e(rhs.end());
113 while (i != e) {
114 m_hitvector.push_back((nullptr != *i) ? new T(**i) : nullptr);
115 ++i;
116 }
117 }
118
121 // cppcheck-suppress operatorEqVarError; m_ownPolicy deliberately not copied
123 if (this != &rhs) {
124 this->Clear();
126 m_hitvector.reserve(rhs.m_hitvector.size());
127 const_iterator i(rhs.begin()), e(rhs.end());
128 while (i != e) {
129 m_hitvector.push_back((nullptr != *i) ? new T(**i) : nullptr);
130 ++i;
131 }
132 } else {
133 this->m_hitvector = rhs.m_hitvector;
134 }
135 }
136 return *this;
137 }
138
139 const std::string& Name() const { return m_name; }
140
141 void setName(const std::string& name) { m_name = name; }
142 //
143 // vector methods.
144 const std::vector<T*>& getVector() { return m_hitvector; }
145
146 bool empty() const { return m_hitvector.empty(); }
147
149 return const_iterator(m_hitvector.begin(), make_const());
150 }
151
153 return const_iterator(m_hitvector.end(), make_const());
154 }
155
156 iterator begin() { return m_hitvector.begin(); }
157
158 iterator end() { return m_hitvector.end(); }
159
160 size_type size() const { return m_hitvector.size(); }
161
162 void push_back(T* t) { m_hitvector.push_back(t); }
163 void push_back(std::unique_ptr<T> t) { m_hitvector.push_back(t.release()); }
164
165 const T* At(unsigned int pos) const { return m_hitvector.at(pos); }
166
167 const T* operator[](size_type n) const { return m_hitvector[n]; }
168
170 if (sz < size()) {
172 iterator i(m_hitvector.begin() + sz), e(m_hitvector.end());
173 while (i != e) {
174 delete *i++;
175 }
176 }
177 m_hitvector.resize(sz);
178 } else {
179 m_hitvector.insert(m_hitvector.end(), sz - m_hitvector.size(), nullptr);
180 }
181 }
182
183 void clear() {
185 for (unsigned int i = 0; i < m_hitvector.size(); i++)
186 delete m_hitvector[i];
187 }
188 m_hitvector.clear();
189 }
190
191 void reserve(size_type n) { m_hitvector.reserve(n); }
192
193 protected:
194 std::string m_name;
195 std::vector<T*> m_hitvector;
197
198 public:
199 // Used to ensure that the DVLInfo gets registered
200 // when the dictionary for this class is loaded.
201 static const std::type_info* initHelper() {
203 };
204 static const std::type_info* const s_info;
205};
206
215template <class T>
216void dvl_makecontainer(size_t nreserve, AthenaHitsVector<T>*& cont) {
217 cont = new AthenaHitsVector<T>;
218 cont->reserve(nreserve);
219}
220
221// Ensure that the DVLInfo gets registered
222// when the dictionary for this class is loaded.
223template <class T>
224const std::type_info* const AthenaHitsVector<T>::s_info =
226
227#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.
boost::transform_iterator< make_const, typename CONT::const_iterator > const_iterator
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
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)
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