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