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 //
38 template <typename T>
40  public:
41  //
42  // additional typedef
43  using base_value_type = T;
44  using CONT = std::vector<T*>;
45  using value_type = typename CONT::value_type;
46  using pointer = typename CONT::pointer;
47  using reference = typename CONT::reference;
48  using iterator = typename CONT::iterator;
49  using size_type = typename CONT::size_type;
50  using difference_type = typename CONT::difference_type;
51  using const_pointer = const T* const*;
52  using const_reference = const T* const&;
53 
54  struct make_const {
55  const T* operator()(const T* x) const { return x; }
56  };
58  boost::transform_iterator<make_const, typename CONT::const_iterator>;
59 #ifdef __CINT__
60  // default constructor for rootcint
61  AthenaHitsVector() {}
62 #else
63  // methods not provided to rootcint
64  AthenaHitsVector(const std::string& collectionName = "DefaultCollectionName",
66  : m_name (collectionName),
67  m_ownPolicy (ownPolicy)
68  {
69  IMessageSvc* msgSvc(Athena::getMessageSvc());
70  MsgStream log(msgSvc, "AthenaHitsVector");
71  log << MSG::DEBUG << " initialized " << collectionName
72  << " with ownership policy " << m_ownPolicy << endmsg;
73  }
75 
76  void Clear() {
77  // delete pointers if we own the elements
79  for (unsigned int i = 0; i < m_hitvector.size(); i++)
80  delete m_hitvector[i];
81  }
82  m_hitvector.clear();
83  }
84 
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  m_ownPolicy = ownPolicy;
93  }
94 
95  void Insert(T* h) { m_hitvector.push_back(h); }
96  int Size() const { return size(); }
97 #endif // __CINT__
98 
101  explicit AthenaHitsVector(const AthenaHitsVector<T>& rhs) {
102  m_hitvector.reserve(rhs.m_hitvector.size());
103  const_iterator i(rhs.begin()), e(rhs.end());
104  while (i != e) {
105  m_hitvector.push_back((nullptr != *i) ? new T(**i) : nullptr);
106  ++i;
107  }
108  }
109 
112  // cppcheck-suppress operatorEqVarError; m_ownPolicy deliberately not copied
114  if (this != &rhs) {
115  this->Clear();
116  if (this->m_ownPolicy == AthHitVec::OWN_ELEMENTS) {
117  m_hitvector.reserve(rhs.m_hitvector.size());
118  const_iterator i(rhs.begin()), e(rhs.end());
119  while (i != e) {
120  m_hitvector.push_back((nullptr != *i) ? new T(**i) : nullptr);
121  ++i;
122  }
123  } else {
124  this->m_hitvector = rhs.m_hitvector;
125  }
126  }
127  return *this;
128  }
129 
130  const std::string& Name() const { return m_name; }
131 
132  void setName(const std::string& name) { m_name = name; }
133  //
134  // vector methods.
135  const std::vector<T*>& getVector() { return m_hitvector; }
136 
137  bool empty() const { return m_hitvector.empty(); }
138 
140  return const_iterator(m_hitvector.begin(), make_const());
141  }
142 
143  const_iterator end() const {
144  return const_iterator(m_hitvector.end(), make_const());
145  }
146 
147  iterator begin() { return m_hitvector.begin(); }
148 
149  iterator end() { return m_hitvector.end(); }
150 
151  size_type size() const { return m_hitvector.size(); }
152 
153  void push_back(T* t) { m_hitvector.push_back(t); }
154  void push_back(std::unique_ptr<T> t) { m_hitvector.push_back(t.release()); }
155 
156  const T* At(unsigned int pos) const { return m_hitvector.at(pos); }
157 
158  const T* operator[](size_type n) const { return m_hitvector[n]; }
159 
161  if (sz < size()) {
163  iterator i(m_hitvector.begin() + sz), e(m_hitvector.end());
164  while (i != e) {
165  delete *i++;
166  }
167  }
168  m_hitvector.resize(sz);
169  } else {
170  m_hitvector.insert(m_hitvector.end(), sz - m_hitvector.size(), nullptr);
171  }
172  }
173 
174  void clear() {
176  for (unsigned int i = 0; i < m_hitvector.size(); i++)
177  delete m_hitvector[i];
178  }
179  m_hitvector.clear();
180  }
181 
182  void reserve(size_type n) { m_hitvector.reserve(n); }
183 
184  protected:
185  std::string m_name;
186  std::vector<T*> m_hitvector;
188 
189  public:
190  // Used to ensure that the DVLInfo gets registered
191  // when the dictionary for this class is loaded.
192  static const std::type_info* initHelper() {
194  };
195  static const std::type_info* const s_info;
196 };
197 
206 template <class T>
207 void dvl_makecontainer(size_t nreserve, AthenaHitsVector<T>*& cont) {
208  cont = new AthenaHitsVector<T>;
209  cont->reserve(nreserve);
210 }
211 
212 // Ensure that the DVLInfo gets registered
213 // when the dictionary for this class is loaded.
214 template <class T>
215 const std::type_info* const AthenaHitsVector<T>::s_info =
217 
218 #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:130
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:154
dvl_makecontainer
void dvl_makecontainer(size_t nreserve, AthenaHitsVector< T > *&cont)
Construct a new container.
Definition: AthenaHitsVector.h:207
AthenaHitsVector::begin
iterator begin()
Definition: AthenaHitsVector.h:147
AthenaHitsVector::clear
void clear()
Definition: AthenaHitsVector.h:174
AthenaHitsVector::end
iterator end()
Definition: AthenaHitsVector.h:149
DVLInfo.h
Holder to implement conversion copies for DataVector/DataList.
AthenaHitsVector::Clear
void Clear()
Definition: AthenaHitsVector.h:76
AthenaHitsVector< LArHit >::const_reference
const LArHit *const & const_reference
Definition: AthenaHitsVector.h:52
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
AthenaHitsVector< LArHit >::pointer
typename CONT::pointer pointer
Definition: AthenaHitsVector.h:46
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:158
AthenaHitsVector::m_hitvector
std::vector< T * > m_hitvector
Definition: AthenaHitsVector.h:186
AthenaHitsVector< LArHit >::const_iterator
boost::transform_iterator< make_const, typename CONT::const_iterator > const_iterator
Definition: AthenaHitsVector.h:58
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:182
AthenaHitsVector::Clear
void Clear(AthHitVec::OwnershipPolicy ownPolicy)
Definition: AthenaHitsVector.h:85
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:113
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:101
AthenaHitsVector::push_back
void push_back(T *t)
Definition: AthenaHitsVector.h:153
AthenaHitsVector::s_info
static const std::type_info *const s_info
Definition: AthenaHitsVector.h:194
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:731
AthenaHitsVector< LArHit >::difference_type
typename CONT::difference_type difference_type
Definition: AthenaHitsVector.h:50
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
AthenaHitsVector::initHelper
static const std::type_info * initHelper()
Definition: AthenaHitsVector.h:192
AthenaHitsVector< LArHit >::const_pointer
const LArHit *const * const_pointer
Definition: AthenaHitsVector.h:51
AthenaHitsVector::make_const::operator()
const T * operator()(const T *x) const
Definition: AthenaHitsVector.h:55
AthenaHitsVector::getVector
const std::vector< T * > & getVector()
Definition: AthenaHitsVector.h:135
AthenaHitsVector::make_const
Definition: AthenaHitsVector.h:54
AthenaHitsVector::Insert
void Insert(T *h)
Definition: AthenaHitsVector.h:95
AthenaHitsVector< LArHit >::CONT
std::vector< LArHit * > CONT
Definition: AthenaHitsVector.h:44
AthenaHitsVector::setName
void setName(const std::string &name)
Definition: AthenaHitsVector.h:132
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
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:48
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
AthenaHitsVector::resize
void resize(size_type sz)
Definition: AthenaHitsVector.h:160
h
AthenaHitsVector::Size
int Size() const
Definition: AthenaHitsVector.h:96
AthHitVec
Definition: AthenaHitsVector.h:30
AthenaHitsVector::m_name
std::string m_name
Definition: AthenaHitsVector.h:185
DEBUG
#define DEBUG
Definition: page_access.h:11
dq_make_web_display.reference
reference
Definition: dq_make_web_display.py:44
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
AthenaHitsVector::size
size_type size() const
Definition: AthenaHitsVector.h:151
AthenaHitsVector::end
const_iterator end() const
Definition: AthenaHitsVector.h:143
AthenaHitsVector< LArHit >::size_type
typename CONT::size_type size_type
Definition: AthenaHitsVector.h:49
AthenaHitsVector::AthenaHitsVector
AthenaHitsVector(const std::string &collectionName="DefaultCollectionName", AthHitVec::OwnershipPolicy ownPolicy=AthHitVec::OWN_ELEMENTS)
Definition: AthenaHitsVector.h:64
AthenaHitsVector
Definition: AthenaHitsVector.h:39
AthenaHitsVector::~AthenaHitsVector
~AthenaHitsVector()
Definition: AthenaHitsVector.h:74
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:156
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35
AthenaHitsVector::begin
const_iterator begin() const
Definition: AthenaHitsVector.h:139
AthenaHitsVector::m_ownPolicy
AthHitVec::OwnershipPolicy m_ownPolicy
Definition: AthenaHitsVector.h:187
AthenaHitsVector::empty
bool empty() const
Definition: AthenaHitsVector.h:137