ATLAS Offline Software
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 
30 namespace AthHitVec{
33  VIEW_ELEMENTS = 1
34  };
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 //
47 template <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
70  AthenaHitsVector() {}
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 
110  explicit AthenaHitsVector(const AthenaHitsVector<T>& rhs) {
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();
125  if (this->m_ownPolicy == AthHitVec::OWN_ELEMENTS) {
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 
152  const_iterator end() const {
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 
215 template <class T>
216 void 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.
223 template <class T>
224 const std::type_info* const AthenaHitsVector<T>::s_info =
226 
227 #endif
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
AthenaHitsVector::Name
const std::string & Name() const
Definition: AthenaHitsVector.h:139
HitsVectorBase
Definition: AthenaHitsVector.h:38
fitman.sz
sz
Definition: fitman.py:527
getMessageSvc.h
singleton-like access to IMessageSvc via open function and helper
AthenaHitsVector::push_back
void push_back(std::unique_ptr< T > t)
Definition: AthenaHitsVector.h:163
dvl_makecontainer
void dvl_makecontainer(size_t nreserve, AthenaHitsVector< T > *&cont)
Construct a new container.
Definition: AthenaHitsVector.h:216
AthenaHitsVector::begin
iterator begin()
Definition: AthenaHitsVector.h:156
AthenaHitsVector::clear
void clear()
Definition: AthenaHitsVector.h:183
AthenaHitsVector::end
iterator end()
Definition: AthenaHitsVector.h:158
DVLInfo.h
Holder to implement conversion copies for DataVector/DataList.
AthenaHitsVector::Clear
void Clear()
Definition: AthenaHitsVector.h:85
AthenaHitsVector< LArHit >::const_reference
const LArHit *const & const_reference
Definition: AthenaHitsVector.h:61
reference
Definition: hcg.cxx:437
AthHitVec::OWN_ELEMENTS
@ OWN_ELEMENTS
this data object owns its elements
Definition: AthenaHitsVector.h:32
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
HitsVectorBase::~HitsVectorBase
virtual ~HitsVectorBase()=default
AthenaHitsVector< LArHit >::pointer
typename CONT::pointer pointer
Definition: AthenaHitsVector.h:55
x
#define x
Athena::getMessageSvc
IMessageSvc * getMessageSvc(bool quiet=false)
Definition: getMessageSvc.cxx:20
AthenaHitsVector::operator[]
const T * operator[](size_type n) const
Definition: AthenaHitsVector.h:167
AthenaHitsVector::m_hitvector
std::vector< T * > m_hitvector
Definition: AthenaHitsVector.h:195
AthenaHitsVector< LArHit >::const_iterator
boost::transform_iterator< make_const, typename CONT::const_iterator > const_iterator
Definition: AthenaHitsVector.h:67
AthHitVec::VIEW_ELEMENTS
@ VIEW_ELEMENTS
this data object is a view, does not own its elmts
Definition: AthenaHitsVector.h:33
AthenaHitsVector::reserve
void reserve(size_type n)
Definition: AthenaHitsVector.h:191
AthenaHitsVector::Clear
void Clear(AthHitVec::OwnershipPolicy ownPolicy)
Definition: AthenaHitsVector.h:94
AthenaHitsVector::operator=
AthenaHitsVector< T > & operator=(const AthenaHitsVector< T > &rhs)
assignment deletes old elements and copies the new ones deep copy if AthHitVec::OWN_ELEMENTS shallow ...
Definition: AthenaHitsVector.h:122
AthenaHitsVector::AthenaHitsVector
AthenaHitsVector(const AthenaHitsVector< T > &rhs)
copy constructor makes deep copy of elements, as by default the container is AthHitVec::OWN_ELEMENTS
Definition: AthenaHitsVector.h:110
AthenaHitsVector::~AthenaHitsVector
~AthenaHitsVector() override
Definition: AthenaHitsVector.h:83
AthenaHitsVector::push_back
void push_back(T *t)
Definition: AthenaHitsVector.h:162
AthenaHitsVector::s_info
static const std::type_info *const s_info
Definition: AthenaHitsVector.h:203
StdJOSetup.msgSvc
msgSvc
Provide convenience handles for various services.
Definition: StdJOSetup.py:36
AthHitVec::OwnershipPolicy
OwnershipPolicy
Definition: AthenaHitsVector.h:31
lumiFormat.i
int i
Definition: lumiFormat.py:85
beamspotman.n
n
Definition: beamspotman.py:727
AthenaHitsVector< LArHit >::difference_type
typename CONT::difference_type difference_type
Definition: AthenaHitsVector.h:59
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
AthenaHitsVector::initHelper
static const std::type_info * initHelper()
Definition: AthenaHitsVector.h:201
AthenaHitsVector< LArHit >::const_pointer
const LArHit *const * const_pointer
Definition: AthenaHitsVector.h:60
AthenaHitsVector::make_const::operator()
const T * operator()(const T *x) const
Definition: AthenaHitsVector.h:64
AthenaHitsVector::getVector
const std::vector< T * > & getVector()
Definition: AthenaHitsVector.h:144
AthenaHitsVector::make_const
Definition: AthenaHitsVector.h:63
AthenaHitsVector::Insert
void Insert(T *h)
Definition: AthenaHitsVector.h:104
AthenaHitsVector< LArHit >::CONT
std::vector< LArHit * > CONT
Definition: AthenaHitsVector.h:53
AthenaHitsVector::setName
void setName(const std::string &name)
Definition: AthenaHitsVector.h:141
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
LArHit
Class to store hit energy and time in LAr cell from G4 simulation.
Definition: LArHit.h:25
AthenaHitsVector< LArHit >::iterator
typename CONT::iterator iterator
Definition: AthenaHitsVector.h:57
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:16
AthenaHitsVector::resize
void resize(size_type sz)
Definition: AthenaHitsVector.h:169
h
AthenaHitsVector::Size
int Size() const
Definition: AthenaHitsVector.h:105
AthHitVec
Definition: AthenaHitsVector.h:30
AthenaHitsVector::m_name
std::string m_name
Definition: AthenaHitsVector.h:194
DEBUG
#define DEBUG
Definition: page_access.h:11
dq_make_web_display.reference
reference
Definition: dq_make_web_display.py:43
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
AthenaHitsVector::size
size_type size() const
Definition: AthenaHitsVector.h:160
AthenaHitsVector::end
const_iterator end() const
Definition: AthenaHitsVector.h:152
AthenaHitsVector< LArHit >::size_type
typename CONT::size_type size_type
Definition: AthenaHitsVector.h:58
AthenaHitsVector::AthenaHitsVector
AthenaHitsVector(const std::string &collectionName="DefaultCollectionName", AthHitVec::OwnershipPolicy ownPolicy=AthHitVec::OWN_ELEMENTS)
Definition: AthenaHitsVector.h:73
AthenaHitsVector
Definition: AthenaHitsVector.h:48
DataModel_detail::DVLInfo
Definition: DVLInfo.h:237
value_type
Definition: EDM_MasterSearch.h:11
AthenaHitsVector::At
const T * At(unsigned int pos) const
Definition: AthenaHitsVector.h:165
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35
AthenaHitsVector::begin
const_iterator begin() const
Definition: AthenaHitsVector.h:148
AthenaHitsVector::m_ownPolicy
AthHitVec::OwnershipPolicy m_ownPolicy
Definition: AthenaHitsVector.h:196
AthenaHitsVector::empty
bool empty() const
Definition: AthenaHitsVector.h:146