ATLAS Offline Software
NestedContainer.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 
57 // $Id: NestedContainer.h,v 1.6 2009-02-11 13:46:55 dwhittin Exp $
58 
59 
60 #ifndef TRTConditionsData_NestedContainer_h
61 #define TRTConditionsData_NestedContainer_h
62 
63 #include <vector>
64 #include <map>
65 #include <functional>
66 #include <algorithm>
69 #include "GaudiKernel/MsgStream.h"
70 
71 
72 namespace TRTCond
73 {
74 
78  template <class Daughter, class T, class Trait>
80  {
82  static const T& get(const Daughter& d, const ExpandedIdentifier& id) { return d.get(id) ; }
84  static const T& get(const Daughter& d, const ExpandedIdentifier& id , size_t& resolvelevel) { return d.get(id,resolvelevel) ; }
86  static const T& get(const Daughter& d) { return d.get() ; }
88  static void set(Daughter& d, const ExpandedIdentifier& id, const T& t) { d.set(id,t) ; }
90  static bool empty(Daughter& d) { return d.daughters().empty() ; }
92  static void clear(Daughter& d, const ExpandedIdentifier& id) { d.clear(id) ; } ;
94  static void clear(Daughter& d) { d.clear() ; } ;
96  static void print(const Daughter& d) { d.print() ; }
98  static void getall(const Daughter& d, std::vector< std::pair<ExpandedIdentifier, const T*> >& entries) {
99  d.getall(entries) ; }
101  static Daughter initialvalue() { return Daughter() ; }
103  static void copy(Daughter& out, const Daughter& in) { out = in ; }
105  static size_t footprint(const Daughter& d) { return d.footprint() ; }
107  static void crunch(Daughter& d) { return d.crunch() ; }
109  static bool isequal(const Daughter& lhs, const Daughter& rhs) { return lhs==rhs ; }
111  static size_t numObjects(const Daughter& d) { return d.numObjects() ; }
112  } ;
113 
118  template <class T, class Trait>
120  {
121  static const T& get(const T& d, const ExpandedIdentifier&) { return d ; }
122  static const T& get(const T& d, const ExpandedIdentifier& , size_t& ) { return d ; }
123  static const T& get(const T& d) { return d ; }
124  static void set(T& d, const ExpandedIdentifier&, const T& t) { Trait::copy(d,t) ; }
125  static bool empty(T&) { return true ; }
126  static void clear(T& d, const ExpandedIdentifier&) { Trait::erase(d) ; }
127  static void clear(T& d) { Trait::erase(d) ; }
128  static void print(const T& d) { Trait::print(d) ; }
129  static void getall(const T& d, std::vector< std::pair<ExpandedIdentifier, const T*> >& entries) {
130  if(Trait::isvalid(d) ) {
132  entries.push_back(std::pair<ExpandedIdentifier, const T*>(id,&d)) ;
133  }
134  }
135  static T initialvalue() { return Trait::initialvalue() ; }
136  static void copy(T& out, const T& in) { Trait::copy(out,in) ; }
137  static size_t footprint(const T& d) { return Trait::footprint(d) ; }
138  static void crunch(T&) {}
139  static bool isequal(const T& lhs, const T& rhs) { return Trait::isequal(lhs,rhs) ; }
140  static size_t numObjects(const T& d) { return Trait::isvalid(d) ? 1 : 0 ; }
141  } ;
142 
143 
144 
149  template <int NestingLevel, class Daughter, class T, class Trait>
151  {
152  private:
153 
154  typedef std::vector<Daughter> DaughterContainer ;
155  //typedef std::vector<Daughter>::const_iterator ConstIterator ;
158 
159  public:
160  typedef T value_type ;
161  typedef const T const_value_type ;
162  typedef Trait trait_type ;
163 
165  NestedContainerBase() : m_daughters(0), m_default(Trait::initialvalue()) {}
166  // NestedContainerBase(const NestedContainerBase& rhs)
167  // : m_daughters(0), m_default(Trait::initialvalue()) { *this = rhs ; }
168 
170  ~NestedContainerBase() = default;
171  //{
172  //cannot delete the daughter here, because of copy constructors
173  //std::cout << "in ~NestedContainerBase(): " << std::flush ;
174  //Trait::print(m_default) ;
175  //Trait::erase(m_default) ;
176  //std::cout << "done." << std::endl ;
177  //}
178 
179  // NestedContainerBase& operator=(const NestedContainerBase& rhs) {
180  // std::cout << "operator=" << m_daughters.size() << " " << rhs.m_daughters.size() << std::endl ;
181  // clear() ;
182  // Trait::copy(m_default,rhs.m_default) ;
183  // m_daughters.resize(rhs.m_daughters.size(),NestedContainerDaughterAccessor<Daughter,T,Trait>::initialvalue()) ;
184  // for(unsigned int i=0; i<m_daughters.size(); ++i)
185  // NestedContainerDaughterAccessor<Daughter,T,Trait>::copy( m_daughters[i], rhs.m_daughters[i] ) ;
186  // std::cout << "done with operator=" << std::endl ;
187  // return *this ;
188  // }
189 
191  void set( const ExpandedIdentifier& id, const T& t) {
192  if( NestingLevel == id.level() ) {
193  // set the default value
195  } else {
196  unsigned int thisindex = id.index(NestingLevel+1) ;
197  // expand daughter array if necessary
198  if(thisindex>=m_daughters.size())
201  }
202  }
203 
205  const T& get( const ExpandedIdentifier& id ) const {
206  if( NestingLevel < id.level() ) {
207  unsigned int thisindex = id.index(NestingLevel+1) ;
208  if(thisindex<m_daughters.size()) {
210  // only return this value if it is valid. otherwise, return the default.
211  if( Trait::isvalid(rc) ) return rc ;
212  }
213  }
214  return m_default ;
215  }
216 
217  const T& get( const ExpandedIdentifier& id, size_t& resolvelevel) const
218  {
219  if( NestingLevel < id.level() ) {
220  unsigned int thisindex = id.index(NestingLevel+1) ;
221  if(thisindex<m_daughters.size()) {
222  const T& rc = NestedContainerDaughterAccessor<Daughter,T,Trait>::get(m_daughters[thisindex],id,resolvelevel) ;
223  if( Trait::isvalid(rc) ) return rc ;
224  }
225  }
226  resolvelevel = NestingLevel ;
227  return m_default ;
228  }
229 
230 
232  const T& get() const { return m_default ; }
233 
235  void set(const T& t) { Trait::copy(m_default,t) ; }
236 
238  void clear() {
239  for( typename DaughterContainer::iterator it = m_daughters.begin() ; it != m_daughters.end(); ++it )
241  m_daughters.clear() ;
242  Trait::erase(m_default) ;
243  }
244 
246  void clear( const ExpandedIdentifier& id ) {
247  if( NestingLevel == id.level() ) clear() ;
248  else {
249  unsigned int thisindex = id.index(NestingLevel+1) ;
250  if(thisindex<m_daughters.size())
252  }
253  }
254 
256  const DaughterContainer& daughters() const { return m_daughters ; }
257 
258 
260  size_t numObjects() const {
261  size_t rc(0) ;
262  assert( std::distance(m_daughters.begin(),m_daughters.end())==(int)m_daughters.size()) ;
263  assert( m_daughters.size() < 1000 ) ;
264  for( typename DaughterContainer::const_iterator it = m_daughters.begin() ; it != m_daughters.end(); ++it )
266  if( Trait::isvalid(m_default) ) ++rc ;
267  return rc ;
268  }
269 
270 
272  bool operator==(const NestedContainerBase& rhs) const {
273  return m_daughters.size()==0 && rhs.m_daughters.size()==0 && Trait::isequal(m_default,rhs.m_default) ;
274  }
275 
277  bool operator==(const T& rhs) const {
278  return m_daughters.size()==0 && Trait::isequal(m_default,rhs) ;
279  }
280 
282  size_t footprint() const {
283  size_t total = Trait::footprint(m_default) + (m_daughters.capacity()-m_daughters.size())*sizeof(Daughter) ;
284  for( typename DaughterContainer::const_iterator it = m_daughters.begin() ; it != m_daughters.end(); ++it )
286  return total ;
287  }
288 
292  void crunch() {
293  // first the daughters
294  for( typename DaughterContainer::iterator it = m_daughters.begin() ; it != m_daughters.end(); ++it )
296  // now use, if anything is there
297  if( m_daughters.size()>0 ) {
298  // find the first daughter that is valid
299  size_t firstvaliddaughter(0);
300  bool allequal(true) ;
301  for(size_t idau=0; idau<m_daughters.size() && allequal; ++idau) {
303  if( allequal ) {
304  bool isvalid = Trait::isvalid( NestedContainerDaughterAccessor<Daughter,T,Trait>::get(m_daughters[idau])) ;
305  if( isvalid ) {
306  if( firstvaliddaughter==0 ) firstvaliddaughter = idau ;
307  } else {
309  }
310  }
311  }
312  if(allequal) {
313  // set the default value to the value form the first daughter
314  T defaultvalue(Trait::initialvalue()) ;
315  if( firstvaliddaughter < m_daughters.size() )
317  clear() ; // this clears pointers in the daughters. would rather replace that with destructors.
320  } else if(m_daughters.size()<m_daughters.capacity()) {
321  DaughterContainer newdaughters(0) ;
322  newdaughters.reserve( m_daughters.size() ) ;
323  newdaughters.insert( newdaughters.end(), m_daughters.begin(), m_daughters.end() ) ;
324  newdaughters.swap(m_daughters) ;
325  }
326  }
327  }
328 
330  void printindent() const {
331  for(int i=0; i<NestingLevel; ++i)
332  std::cout << '\t' ;
333  }
334  void print() const {
335  printindent() ;
337  std::cout << "level = " << id.name(NestingLevel) << " (" << NestingLevel << ")" << std::endl ;
338  for(unsigned int i=0; i< m_daughters.size(); ++i) {
339  printindent() ;
340  std::cout << "daughter " << i << std::endl ;
342  }
343  }
344 
346  typedef std::vector< std::pair<ExpandedIdentifier, const T*> > FlatContainer ;
347  void getall( FlatContainer& entries ) const {
348  // add the daughters
349  for(unsigned int idau = 0; idau<m_daughters.size(); ++idau) {
350  FlatContainer dauentries ;
351  // first get them
353  // now update the identifiers to have the dauhgter index set correctly
354  for(unsigned int j=0; j< dauentries.size(); ++j )
355  dauentries[j].first.index(NestingLevel+1) = idau ;
356  // and append
357  entries.insert(entries.end(),dauentries.begin(),dauentries.end()) ;
358  }
359  // add the default value
360  if( Trait::isvalid(m_default) ) {
361  ExpandedIdentifier id(0,0,0,0,0,NestingLevel) ;
362  entries.push_back( std::pair<ExpandedIdentifier, const T*>(id,&m_default)) ;
363  }
364  }
365  } ;
366 
367 
370  template <int NestingLevel, class T, class Trait>
372  : public NestedContainerBase<NestingLevel, NestedContainer<NestingLevel+1,T,Trait>, T, Trait>
373  {
374  } ;
375 
376 
378  template <class T, class Trait>
379  class NestedContainer<ExpandedIdentifier::STRAWLAYER,T,Trait>
380  : public NestedContainerBase<ExpandedIdentifier::STRAWLAYER,T,T,Trait>
381  {
382  } ;
383 
384 
387  template <class T>
389  {
390  public:
391  using pointer = T;
392  using base_type = std::remove_pointer_t<pointer>;
393  using const_pointer = const base_type*;
394  static void erase( pointer& x) { delete x ; x = 0 ; }
395  static void copy( pointer& out, const_pointer in) { if(out) erase(out) ; if(in) out = in->clone() ; }
396  static bool isvalid( const_pointer x ) { return x!=0 ; }
397  static void initialize( pointer& x) { x = 0 ; }
398  static size_t footprint( const_pointer x) { return sizeof(pointer) + (x ? x->footprint() : 0) ; }
399  static void print(const_pointer x) { std::cout << x << std::endl ; }
400  static pointer initialvalue() { return 0 ; }
401  static bool isequal(const_pointer lhs, const_pointer rhs) { return lhs==rhs || (lhs && rhs && *lhs==*rhs) ; }
402  } ;
403 }
404 
405 
406 #endif
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
TRTCond::NestedContainerPointerTrait::footprint
static size_t footprint(const_pointer x)
Definition: NestedContainer.h:398
TRTCond::NestedContainerDaughterAccessor::clear
static void clear(Daughter &d, const ExpandedIdentifier &id)
clear container for given id
Definition: NestedContainer.h:92
TRTCond::NestedContainerPointerTrait< TRTCond::RtRelation * >::base_type
std::remove_pointer_t< pointer > base_type
Definition: NestedContainer.h:392
TRTCond::NestedContainerBase::value_type
T value_type
Type of atomic element.
Definition: NestedContainer.h:160
TRTCond::NestedContainerDaughterAccessor::crunch
static void crunch(Daughter &d)
compress daugther size
Definition: NestedContainer.h:107
getMessageSvc.h
singleton-like access to IMessageSvc via open function and helper
TRTCond::NestedContainerBase::numObjects
size_t numObjects() const
return the total number of valid calibration objects
Definition: NestedContainer.h:260
TRTCond::NestedContainerDaughterAccessor< T, T, Trait >::get
static const T & get(const T &d, const ExpandedIdentifier &, size_t &)
Definition: NestedContainer.h:122
TRTCond::NestedContainerDaughterAccessor::empty
static bool empty(Daughter &d)
empty daughter container
Definition: NestedContainer.h:90
TRTCond::NestedContainerPointerTrait::isequal
static bool isequal(const_pointer lhs, const_pointer rhs)
Definition: NestedContainer.h:401
hist_file_dump.d
d
Definition: hist_file_dump.py:137
TRTCond::NestedContainerDaughterAccessor< T, T, Trait >::isequal
static bool isequal(const T &lhs, const T &rhs)
Definition: NestedContainer.h:139
skel.it
it
Definition: skel.GENtoEVGEN.py:396
TRTCond::NestedContainerDaughterAccessor< T, T, Trait >::copy
static void copy(T &out, const T &in)
Definition: NestedContainer.h:136
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
TRTCond::NestedContainerBase::set
void set(const ExpandedIdentifier &id, const T &t)
set a value.
Definition: NestedContainer.h:191
TRTCond::NestedContainerDaughterAccessor::get
static const T & get(const Daughter &d, const ExpandedIdentifier &id)
get value for given id
Definition: NestedContainer.h:82
TRTCond::NestedContainerDaughterAccessor::set
static void set(Daughter &d, const ExpandedIdentifier &id, const T &t)
set value for given id
Definition: NestedContainer.h:88
TRTCond::NestedContainerBase::m_daughters
DaughterContainer m_daughters
Container of Daughters.
Definition: NestedContainer.h:156
TRTCond::NestedContainerDaughterAccessor< T, T, Trait >::crunch
static void crunch(T &)
Definition: NestedContainer.h:138
TRTCond::NestedContainerBase::print
void print() const
Definition: NestedContainer.h:334
ExpandedIdentifier.h
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
TRTCond::NestedContainerPointerTrait< TRTCond::RtRelation * >::const_pointer
const base_type * const_pointer
Definition: NestedContainer.h:393
x
#define x
TRTCond::NestedContainerBase::FlatContainer
std::vector< std::pair< ExpandedIdentifier, const T * > > FlatContainer
fill vector with all entries in the container. needed to dump to Peter's flat file format
Definition: NestedContainer.h:346
TRTCond::NestedContainerDaughterAccessor::print
static void print(const Daughter &d)
print daugther info
Definition: NestedContainer.h:96
TRTCond::NestedContainerDaughterAccessor::get
static const T & get(const Daughter &d)
get default for daughter container
Definition: NestedContainer.h:86
python.iconfTool.models.loaders.level
level
Definition: loaders.py:20
TRTCond::NestedContainerBase::set
void set(const T &t)
set the default value
Definition: NestedContainer.h:235
TRTCond::RtRelation
Definition: RtRelation.h:27
TRTCond::NestedContainer
Definition: NestedContainer.h:373
TRTCond::NestedContainerDaughterAccessor::clear
static void clear(Daughter &d)
clear container
Definition: NestedContainer.h:94
TRTCond::NestedContainerDaughterAccessor< T, T, Trait >::getall
static void getall(const T &d, std::vector< std::pair< ExpandedIdentifier, const T * > > &entries)
Definition: NestedContainer.h:129
TRTCond::NestedContainerDaughterAccessor< T, T, Trait >::initialvalue
static T initialvalue()
Definition: NestedContainer.h:135
TRTCond::NestedContainerBase::get
const T & get(const ExpandedIdentifier &id) const
get a value.
Definition: NestedContainer.h:205
TRTCond::NestedContainerBase::operator==
bool operator==(const T &rhs) const
another equality operator, used for compressing
Definition: NestedContainer.h:277
TRTCond::NestedContainerPointerTrait::copy
static void copy(pointer &out, const_pointer in)
Definition: NestedContainer.h:395
lumiFormat.i
int i
Definition: lumiFormat.py:85
TRTCond::NestedContainerPointerTrait
Definition: NestedContainer.h:389
TRTCond::NestedContainerPointerTrait::pointer
T pointer
Definition: NestedContainer.h:391
TRTCond::NestedContainerPointerTrait::isvalid
static bool isvalid(const_pointer x)
Definition: NestedContainer.h:396
TRTCond::NestedContainerDaughterAccessor< T, T, Trait >::print
static void print(const T &d)
Definition: NestedContainer.h:128
TRTCond::NestedContainerBase::printindent
void printindent() const
some IO for debugging
Definition: NestedContainer.h:330
TRTCond::NestedContainerDaughterAccessor
Definition: NestedContainer.h:80
TRTCond::NestedContainerDaughterAccessor< T, T, Trait >::get
static const T & get(const T &d, const ExpandedIdentifier &)
Definition: NestedContainer.h:121
TRTCond::NestedContainerDaughterAccessor< T, T, Trait >::numObjects
static size_t numObjects(const T &d)
Definition: NestedContainer.h:140
TRTCond::NestedContainerBase::daughters
const DaughterContainer & daughters() const
return the vector of daughters
Definition: NestedContainer.h:256
TRTCond::NestedContainerPointerTrait::initialvalue
static pointer initialvalue()
Definition: NestedContainer.h:400
id
SG::auxid_t id
Definition: Control/AthContainers/Root/debug.cxx:220
TRTCond::NestedContainerBase
Definition: NestedContainer.h:151
TRTCond::NestedContainerDaughterAccessor< T, T, Trait >::clear
static void clear(T &d)
Definition: NestedContainer.h:127
TRTCond::NestedContainerDaughterAccessor< T, T, Trait >::set
static void set(T &d, const ExpandedIdentifier &, const T &t)
Definition: NestedContainer.h:124
TRTCond::ExpandedIdentifier::STRAW
@ STRAW
Definition: InnerDetector/InDetConditions/TRT_ConditionsData/TRT_ConditionsData/ExpandedIdentifier.h:37
TRTCond::NestedContainerDaughterAccessor< T, T, Trait >::get
static const T & get(const T &d)
Definition: NestedContainer.h:123
TRTCond::NestedContainerDaughterAccessor::getall
static void getall(const Daughter &d, std::vector< std::pair< ExpandedIdentifier, const T * > > &entries)
copy all object pointers into a vector of (id,pointer) pairs
Definition: NestedContainer.h:98
TRTCond::NestedContainerDaughterAccessor< T, T, Trait >::empty
static bool empty(T &)
Definition: NestedContainer.h:125
TRTCond::NestedContainerBase::m_default
T m_default
Type of atomic element.
Definition: NestedContainer.h:157
TRTCond::NestedContainerDaughterAccessor::isequal
static bool isequal(const Daughter &lhs, const Daughter &rhs)
equality operator
Definition: NestedContainer.h:109
TRTCond
Definition: BasicRtRelation.cxx:8
TRTCond::NestedContainerBase::get
const T & get(const ExpandedIdentifier &id, size_t &resolvelevel) const
Definition: NestedContainer.h:217
TRTCond::NestedContainerDaughterAccessor::get
static const T & get(const Daughter &d, const ExpandedIdentifier &id, size_t &resolvelevel)
resolve the nesting level for a given id
Definition: NestedContainer.h:84
TRTCond::NestedContainerBase::trait_type
Trait trait_type
Type of trait.
Definition: NestedContainer.h:162
TRTCond::NestedContainerBase::get
const T & get() const
get the default value
Definition: NestedContainer.h:232
TRTCond::NestedContainerPointerTrait::print
static void print(const_pointer x)
Definition: NestedContainer.h:399
TRTCond::NestedContainerBase::NestedContainerBase
NestedContainerBase()
constructor
Definition: NestedContainer.h:165
DeMoScan.first
bool first
Definition: DeMoScan.py:536
TRTCond::NestedContainerBase::clear
void clear(const ExpandedIdentifier &id)
clear only entries specified
Definition: NestedContainer.h:246
TRTCond::NestedContainerDaughterAccessor< T, T, Trait >::clear
static void clear(T &d, const ExpandedIdentifier &)
Definition: NestedContainer.h:126
entries
double entries
Definition: listroot.cxx:49
CaloCellTimeCorrFiller.defaultvalue
float defaultvalue
Definition: CaloCellTimeCorrFiller.py:25
TRTCond::NestedContainerDaughterAccessor::footprint
static size_t footprint(const Daughter &d)
return allocated memory
Definition: NestedContainer.h:105
dbg::print
void print(std::FILE *stream, std::format_string< Args... > fmt, Args &&... args)
Definition: SGImplSvc.cxx:70
TRTCond::NestedContainerDaughterAccessor::numObjects
static size_t numObjects(const Daughter &d)
total number of atomic objects in daugther
Definition: NestedContainer.h:111
TRTCond::NestedContainerBase::clear
void clear()
clear entire container
Definition: NestedContainer.h:238
calibdata.copy
bool copy
Definition: calibdata.py:27
TRTCond::NestedContainerBase::getall
void getall(FlatContainer &entries) const
Definition: NestedContainer.h:347
TRTCond::NestedContainerBase::DaughterContainer
std::vector< Daughter > DaughterContainer
typedef of container
Definition: NestedContainer.h:154
TRTCond::NestedContainerPointerTrait::erase
static void erase(pointer &x)
Definition: NestedContainer.h:394
TRTCond::ExpandedIdentifier
Identifier for TRT detector elements in the conditions code.
Definition: InnerDetector/InDetConditions/TRT_ConditionsData/TRT_ConditionsData/ExpandedIdentifier.h:30
Amg::distance
float distance(const Amg::Vector3D &p1, const Amg::Vector3D &p2)
calculates the distance between two point in 3D space
Definition: GeoPrimitivesHelpers.h:54
TRTCond::NestedContainerDaughterAccessor::initialvalue
static Daughter initialvalue()
make a default daugther
Definition: NestedContainer.h:101
TRTCond::NestedContainerDaughterAccessor::copy
static void copy(Daughter &out, const Daughter &in)
copy daugthers
Definition: NestedContainer.h:103
TRTCond::NestedContainerBase::const_value_type
const T const_value_type
const Type of atomic element.
Definition: NestedContainer.h:161
TRTCond::NestedContainerBase::footprint
size_t footprint() const
return the memory allocated by the container and its daughters
Definition: NestedContainer.h:282
TRTCond::NestedContainerBase::~NestedContainerBase
~NestedContainerBase()=default
destructor
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35
TRTCond::NestedContainerPointerTrait::initialize
static void initialize(pointer &x)
Definition: NestedContainer.h:397
TRTCond::NestedContainerDaughterAccessor< T, T, Trait >::footprint
static size_t footprint(const T &d)
Definition: NestedContainer.h:137
TRTCond::NestedContainerBase::operator==
bool operator==(const NestedContainerBase &rhs) const
equality operator, used for compressing
Definition: NestedContainer.h:272
TRTCond::NestedContainerBase::crunch
void crunch()
reduce the footprint as much as possible by removing the extra space allocated by the vectors
Definition: NestedContainer.h:292