ATLAS Offline Software
JetMapBase.icc
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 /*
4  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
5 */
6 
7 
8 #include <algorithm>
9 #include <iterator>
10 
11 #include <cmath>
12 
13 // -*- C++ -*-
14 template<class P>
15 const typename JetMapBase<P>::data_t JetMapBase<P>::m_nullData = data_t();
16 
17 template<class P>
18 inline
19 JetMapBase<P>::JetMapBase()
20  : m_ownPolicy (SG::OWN_ELEMENTS)
21 {
22 }
23 
24 #if 0
25 template<class P>
26 inline
27 JetMapBase<P>::JetMapBase(const JetMapBase& /*base*/)
28 // : m_lastIndex(base.m_lastIndex)
29 {
30 
31  // not yet clear what we have to do here.
32 
33 
34  // std::copy(base.m_store.begin(),base.m_store.end(),
35  // std::back_insert_iterator<map_t>(m_store));
36 }
37 #endif
38 
39 template<class P>
40 inline
41 JetMapBase<P>::~JetMapBase()
42 { JetMapBase::clear() ;}
43 
44 
45 template<class P>
46 bool JetMapBase<P>::addRecord(size_t jetIndex) const
47 {
48  auto r = m_store.try_emplace (jetIndex);
49  return r.second;
50 }
51 
52 template<class P>
53 const typename JetMapBase<P>::record_t* JetMapBase<P>::getRecord(size_t jetIndex) const
54 {
55  typename map_t::iterator jetpos = m_store.find(jetIndex);
56  // if record does not exist, create it
57  if ( jetpos == m_store.end() ) return NULL;
58  return (*jetpos).second.get();
59 }
60 
61 template<class P>
62 void
63 JetMapBase<P>::assignRecord(size_t jetIndex, record_t* rec) const
64 {
65  record_ptr_t & rec_p = m_store[jetIndex];
66  if( rec_p.isValid() ) rec_p.destroy();
67  rec_p.set(rec);
68 }
69 
70 template<class P>
71 void
72 JetMapBase<P>::removeRecord(size_t jetIndex) const
73 {
74  typename map_t::iterator jetpos = m_store.find(jetIndex);
75  if ( jetpos == m_store.end() ) return;
76  (*jetpos).second.destroy();
77  m_store.erase( jetpos ) ;
78 }
79 
80 
81 template<class P>
82 void
83 JetMapBase<P>::transferRecord(const JetMapBase<P>* fromMap, size_t oldIndex, size_t newIndex ) const {
84  typename map_t::iterator jetpos = fromMap->m_store.find(oldIndex);
85  if ( jetpos == fromMap->m_store.end() ) return;
86  if((*jetpos).second.isValid()) m_store[newIndex] = (*jetpos).second;
87  if( m_ownPolicy == SG::OWN_ELEMENTS ) fromMap->m_store.erase(jetpos);
88 }
89 
90 template<class P>
91 void
92 JetMapBase<P>::addData(size_t jetIndex,size_t keyIndex,const data_t& data)
93  const
94 {
95 
96  record_ptr_t & rec_ptr = m_store[jetIndex];
97  if( ! rec_ptr.isValid() ) rec_ptr.set( new record_t() ) ;
98  record_t & record = *rec_ptr;
99  if ( keyIndex >= record.size() )
100  record.resize(keyIndex+1,data_t());
101  record[keyIndex] = data;
102 
103 }
104 
105 template<class P>
106 bool
107 JetMapBase<P>::retrieveData(size_t jetIndex,size_t keyIndex,data_t& data) const
108 {
109  // check jet and moment index validity
110  typename map_t::iterator jetpos = m_store.find(jetIndex);
111  if ( jetpos == m_store.end() ) return false;
112  record_ptr_t & record = (*jetpos).second;
113  if( ! record.isValid() ) return false;// this should never happen
114  if ( keyIndex >= record->size() ) return false;
115 
116  data= record->operator[](keyIndex) ;
117  return true;
118 }
119 
120 template<class P>
121 const typename JetMapBase<P>::data_t&
122 JetMapBase<P>::accessData(size_t jetIndex,size_t keyIndex) const
123 {
124  // check jet and moment index validity
125  typename map_t::iterator jetpos = m_store.find(jetIndex);
126  if ( jetpos == m_store.end() ) return m_nullData;
127  record_ptr_t & record = (*jetpos).second;
128  if( ! record.isValid() ) return m_nullData; // this should never happen
129  if ( keyIndex >= record->size() ) return m_nullData;
130 
131  return record->operator[](keyIndex) ;
132 }
133 
134 template<class P>
135 size_t JetMapBase<P>::numberOfMoments(size_t jetIndex) const
136 {
137  typename map_t::iterator jetpos = m_store.find(jetIndex);
138  if ( jetpos == m_store.end() ) return 0;
139  if((*jetpos).second.isValid()) return (*jetpos).second->size() ;
140  return 0;
141 }
142 
143 template<class P>
144 inline typename JetMapBase<P>::map_t& JetMapBase<P>::map() { return m_store; }
145 
146 template<class P>
147 inline const typename JetMapBase<P>::map_t& JetMapBase<P>::map() const
148 { return m_store; }
149 
150 template<class P>
151 void JetMapBase<P>::clear(){
152  if( m_ownPolicy == SG::OWN_ELEMENTS ) {
153  for (auto& p : m_store) {
154  p.second.destroy() ;
155  }
156  m_store.clear();
157  }
158 }