ATLAS Offline Software
Loading...
Searching...
No Matches
ElementLinkResetAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5
6// Local include(s):
8
9// Framework include(s):
11
12// EDM include(s):
17#include "AthLinks/ElementLinkBase.h"
18
19
20
21// System include(s):
22#include <map>
23#include <limits>
24
25namespace xAODMaker {
26
28 ISvcLocator* svcLoc )
29 : AthAlgorithm( name, svcLoc ) {
30
31 declareProperty( "SGKeys", m_keys );
32 }
33
35
36 // Tell the user what's happening:
37 ATH_MSG_INFO( "Initialising" );
38 ATH_MSG_DEBUG( "SGKeys = " << m_keys );
39
40 // Return gracefully:
41 return StatusCode::SUCCESS;
42 }
43
45
46 // Collect all the container(s):
47 std::vector< std::pair< const SG::IConstAuxStore*, std::string > > stores;
48 if( !m_keys.empty() ) {
49 for( const std::string& key : m_keys ) {
50 const SG::IConstAuxStore* store = nullptr;
51 ATH_CHECK( evtStore()->retrieve( store, key ) );
52 stores.emplace_back( store, key );
53 }
54 } else {
56 ATH_CHECK( evtStore()->retrieve( itr, end ) );
57 for( ; itr != end; ++itr ) {
58 const SG::IConstAuxStore* store = nullptr;
59 ATH_CHECK( evtStore()->retrieve( store, itr.key() ) );
60 stores.emplace_back( store, itr.key() );
61 }
62 }
63 ATH_MSG_DEBUG( "Number of IConstAuxStore objects retrieved: "
64 << stores.size() );
65
66 // Reset the ElementLinks in all of them:
67 for( const auto& storeKey : stores ) {
68 ATH_MSG_VERBOSE( "Reseting element links in store: "
69 << storeKey.second );
70 ATH_CHECK( reset( *( storeKey.first ), storeKey.second ) );
71 }
72
73 // Return gracefully:
74 return StatusCode::SUCCESS;
75 }
76
77 StatusCode ElementLinkResetAlg::reset( const SG::IConstAuxStore& store,
78 const std::string& key ) {
79
80 // If the container is empty, return right away:
81 if( ! store.size() ) {
82 return StatusCode::SUCCESS;
83 }
84
85 // Get all the IDs stored in this object:
86 const SG::auxid_set_t& auxids = store.getAuxIDs();
87
88 // The auxiliary type registry:
90 static constexpr SG::auxid_t maxPossibleAuxId = std::numeric_limits<SG::auxid_t>::max();
91 // Loop over them:
92 for( SG::auxid_t auxid : auxids ) {
93 // Check/cache its type:
94 if( (m_typeCache.size() <= auxid) and (auxid < maxPossibleAuxId)) {
95 m_typeCache.resize( auxid + 1 );
96 }
97 if( ! m_typeCache[ auxid ].isSet ) {
98 const std::string tname =
99 SG::normalizedTypeinfoName( *( reg.getType( auxid ) ) );
100 static const std::string pat1 = "ElementLink<";
101 static const std::string pat2 = "std::vector<ElementLink<";
102 if( tname.substr( 0, pat1.size() ) == pat1 ) {
103 m_typeCache[ auxid ].isEL = true;
104 } else if( tname.substr( 0, pat2.size() ) == pat2 ) {
105 m_typeCache[ auxid ].isELVec = true;
106 }
107 m_typeCache[ auxid ].isSet = true;
108 ATH_MSG_VERBOSE( "Type for \"" << tname << "\": isEL = "
109 << m_typeCache[ auxid ].isEL << ", isELVec = "
110 << m_typeCache[ auxid ].isELVec );
111 }
112
113 // If it's not an EL type, then don't bother:
114 if( ! ( m_typeCache[ auxid ].isEL || m_typeCache[ auxid ].isELVec ) ) {
115 continue;
116 }
117
118 // Get a pointer to the vector variable. We need to cast away
119 // its constness in this ugly way, we can't afford to only ask for
120 // non-const pointers from the store. Since the ElementLink to be
121 // reset may very well be a const variable that was accessed already,
122 // and now needs its cache wiped.
123 void* ptr = const_cast< void* >( store.getData( auxid ) );
124
125 // If the pointer is null, then something dodgy happened with this
126 // (dynamic) variable.
127 if( ! ptr ) {
128 // Check if this is a static variable. If it is, it's not an error
129 // to get a null pointer for it. Since such a case can only happen
130 // when a new static variable was introduced into the EDM, and we're
131 // reading an old input file that doesn't have this variable in it
132 // yet. Which is an okay scenario.
133 const SG::IAuxStoreIO* storeIO =
134 dynamic_cast< const SG::IAuxStoreIO* >( &store );
135 if( ( ! storeIO ) || ( storeIO->getDynamicAuxIDs().find( auxid ) !=
136 storeIO->getDynamicAuxIDs().end() ) ) {
137 REPORT_MESSAGE( MSG::ERROR )
138 << "Invalid pointer received for variable: " << key
139 << reg.getName( auxid );
140 } else {
141 ATH_MSG_DEBUG( "Static variable " << key << reg.getName( auxid )
142 << " is empty" );
143 }
144 continue;
145 }
146
147 // Get the variable's element size:
148 const size_t eltSize = reg.getEltSize( auxid );
149
150 // Loop over its elements:
151 const size_t sz_i = store.size();
152 for( size_t i = 0; i < sz_i; ++i ) {
153
154 // Raw pointer to the object:
155 void* eltPtr = reinterpret_cast< char* >( ptr ) + i * eltSize;
156
157 // Do different things based on the variable type:
158 if( m_typeCache[ auxid ].isEL ) {
159 // Cast to an ElementLinkBase object:
160 ElementLinkBase* elb =
161 reinterpret_cast< ElementLinkBase* >( eltPtr );
162 // Reset the link only if it has its persistent variable(s) set:
163 if( elb->persKey() ) {
164 elb->toTransient();
165 }
166 } else if( m_typeCache[ auxid ].isELVec ) {
167 // For a vector of links we heavily rely on knowing how GCC/Clang
168 // lays out the memory for vectors.
169 std::vector< ElementLinkBase >& v =
170 *( reinterpret_cast< std::vector< ElementLinkBase >* >( eltPtr ) );
171 const size_t sz_j = v.size();
172 for( size_t j = 0; j < sz_j; ++j ) {
173 // Access the link:
174 ElementLinkBase& elb = v[ j ];
175 // Reset the link only if it has its persistent variable(s)
176 // set:
177 if( elb.persKey() ) {
178 elb.toTransient();
179 }
180 }
181 } else {
182 ATH_MSG_FATAL( "There is a logic error in the code" );
183 return StatusCode::FAILURE;
184 }
185 }
186 }
187
188 // Return gracefully:
189 return StatusCode::SUCCESS;
190 }
191
193 : isSet( false ), isEL( false ), isELVec( false ) {
194
195 }
196
197} // namespace xAODMaker
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_DEBUG(x)
Handle mappings between names and auxid_t.
Helpers for checking error return status codes and reporting errors.
#define REPORT_MESSAGE(LVL)
Report a message.
Interface providing I/O for a generic auxiliary store.
Interface for const operations on an auxiliary store.
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
Base class for ElementLinks to vectors of pointers.
sgkey_t persKey() const
Return the SG key that we reference, as a hash.
bool toTransient(IProxyDict *sg=0)
Finish initialization after link has been read.
Handle mappings between names and auxid_t.
static AuxTypeRegistry & instance()
Return the singleton registry instance.
a const_iterator facade to DataHandle.
Definition SGIterator.h:164
Interface providing I/O for a generic auxiliary store.
Definition IAuxStoreIO.h:44
virtual const SG::auxid_set_t & getDynamicAuxIDs() const =0
Get the list of all dynamically created variables.
A set of aux data identifiers.
Definition AuxTypes.h:47
const std::string & key() const
Get the key string with which the current object was stored.
bool isELVec
True of the type is an ElementLink vector.
bool isSet
Flag for whether this type was already set up.
bool isEL
True if the type is an ElementLink.
virtual StatusCode execute()
Function executing the algorithm.
std::vector< std::string > m_keys
StoreGate keys of the auxiliary objects to be processed.
virtual StatusCode initialize()
Function initialising the algorithm.
std::vector< AuxIDType > m_typeCache
Cached types of the auxiliary IDs.
ElementLinkResetAlg(const std::string &name, ISvcLocator *svcLoc)
Regular Algorithm constructor.
StatusCode reset(const SG::IConstAuxStore &store, const std::string &key)
Function reseting all the ElementLinks in one specific container.
std::string normalizedTypeinfoName(const std::type_info &info)
Convert a type_info to a normalized string representation (matching the names used in the root dictio...
size_t auxid_t
Identifier for a particular aux data item.
Definition AuxTypes.h:27
Convert a type_info to a normalized string representation (matching the names used in the root dictio...