ATLAS Offline Software
Loading...
Searching...
No Matches
ElementLinkResetAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5// System include(s):
6#include <map>
7
8// Framework include(s):
10
11// EDM include(s):
16#include "AthLinks/ElementLinkBase.h"
17
18// Local include(s):
19#include "ElementLinkResetAlg.h"
20
21namespace xAODMaker {
22
24 ISvcLocator* svcLoc )
25 : AthAlgorithm( name, svcLoc ) {
26
27 declareProperty( "SGKeys", m_keys );
28 }
29
31
32 // Tell the user what's happening:
33 ATH_MSG_INFO( "Initialising" );
34 ATH_MSG_DEBUG( "SGKeys = " << m_keys );
35
36 // Return gracefully:
37 return StatusCode::SUCCESS;
38 }
39
41
42 // Collect all the container(s):
43 std::vector< std::pair< const SG::IConstAuxStore*, std::string > > stores;
44 if( !m_keys.empty() ) {
45 for( const std::string& key : m_keys ) {
46 const SG::IConstAuxStore* store = nullptr;
47 ATH_CHECK( evtStore()->retrieve( store, key ) );
48 stores.emplace_back( store, key );
49 }
50 } else {
52 ATH_CHECK( evtStore()->retrieve( itr, end ) );
53 for( ; itr != end; ++itr ) {
54 const SG::IConstAuxStore* store = nullptr;
55 ATH_CHECK( evtStore()->retrieve( store, itr.key() ) );
56 stores.emplace_back( store, itr.key() );
57 }
58 }
59 ATH_MSG_DEBUG( "Number of IConstAuxStore objects retrieved: "
60 << stores.size() );
61
62 // Reset the ElementLinks in all of them:
63 for( const auto& storeKey : stores ) {
64 ATH_MSG_VERBOSE( "Reseting element links in store: "
65 << storeKey.second );
66 ATH_CHECK( reset( *( storeKey.first ), storeKey.second ) );
67 }
68
69 // Return gracefully:
70 return StatusCode::SUCCESS;
71 }
72
74 const std::string& key ) {
75
76 // If the container is empty, return right away:
77 if( ! store.size() ) {
78 return StatusCode::SUCCESS;
79 }
80
81 // Get all the IDs stored in this object:
82 const SG::auxid_set_t& auxids = store.getAuxIDs();
83
84 // The auxiliary type registry:
86
87 // Loop over them:
88 for( SG::auxid_t auxid : auxids ) {
89
90 // Check/cache its type:
91 if( m_typeCache.size() <= auxid ) {
92 m_typeCache.resize( auxid + 1 );
93 }
94 if( ! m_typeCache[ auxid ].isSet ) {
95 const std::string tname =
96 SG::normalizedTypeinfoName( *( reg.getType( auxid ) ) );
97 static const std::string pat1 = "ElementLink<";
98 static const std::string pat2 = "std::vector<ElementLink<";
99 if( tname.substr( 0, pat1.size() ) == pat1 ) {
100 m_typeCache[ auxid ].isEL = true;
101 } else if( tname.substr( 0, pat2.size() ) == pat2 ) {
102 m_typeCache[ auxid ].isELVec = true;
103 }
104 m_typeCache[ auxid ].isSet = true;
105 ATH_MSG_VERBOSE( "Type for \"" << tname << "\": isEL = "
106 << m_typeCache[ auxid ].isEL << ", isELVec = "
107 << m_typeCache[ auxid ].isELVec );
108 }
109
110 // If it's not an EL type, then don't bother:
111 if( ! ( m_typeCache[ auxid ].isEL || m_typeCache[ auxid ].isELVec ) ) {
112 continue;
113 }
114
115 // Get a pointer to the vector variable. We need to cast away
116 // its constness in this ugly way, we can't afford to only ask for
117 // non-const pointers from the store. Since the ElementLink to be
118 // reset may very well be a const variable that was accessed already,
119 // and now needs its cache wiped.
120 void* ptr = const_cast< void* >( store.getData( auxid ) );
121
122 // If the pointer is null, then something dodgy happened with this
123 // (dynamic) variable.
124 if( ! ptr ) {
125 // Check if this is a static variable. If it is, it's not an error
126 // to get a null pointer for it. Since such a case can only happen
127 // when a new static variable was introduced into the EDM, and we're
128 // reading an old input file that doesn't have this variable in it
129 // yet. Which is an okay scenario.
130 const SG::IAuxStoreIO* storeIO =
131 dynamic_cast< const SG::IAuxStoreIO* >( &store );
132 if( ( ! storeIO ) || ( storeIO->getDynamicAuxIDs().find( auxid ) !=
133 storeIO->getDynamicAuxIDs().end() ) ) {
134 REPORT_MESSAGE( MSG::ERROR )
135 << "Invalid pointer received for variable: " << key
136 << reg.getName( auxid );
137 } else {
138 ATH_MSG_DEBUG( "Static variable " << key << reg.getName( auxid )
139 << " is empty" );
140 }
141 continue;
142 }
143
144 // Get the variable's element size:
145 const size_t eltSize = reg.getEltSize( auxid );
146
147 // Loop over its elements:
148 const size_t sz_i = store.size();
149 for( size_t i = 0; i < sz_i; ++i ) {
150
151 // Raw pointer to the object:
152 void* eltPtr = reinterpret_cast< char* >( ptr ) + i * eltSize;
153
154 // Do different things based on the variable type:
155 if( m_typeCache[ auxid ].isEL ) {
156 // Cast to an ElementLinkBase object:
157 ElementLinkBase* elb =
158 reinterpret_cast< ElementLinkBase* >( eltPtr );
159 // Reset the link only if it has its persistent variable(s) set:
160 if( elb->persKey() ) {
161 elb->toTransient();
162 }
163 } else if( m_typeCache[ auxid ].isELVec ) {
164 // For a vector of links we heavily rely on knowing how GCC/Clang
165 // lays out the memory for vectors.
166 std::vector< ElementLinkBase >& v =
167 *( reinterpret_cast< std::vector< ElementLinkBase >* >( eltPtr ) );
168 const size_t sz_j = v.size();
169 for( size_t j = 0; j < sz_j; ++j ) {
170 // Access the link:
171 ElementLinkBase& elb = v[ j ];
172 // Reset the link only if it has its persistent variable(s)
173 // set:
174 if( elb.persKey() ) {
175 elb.toTransient();
176 }
177 }
178 } else {
179 ATH_MSG_FATAL( "There is a logic error in the code" );
180 return StatusCode::FAILURE;
181 }
182 }
183 }
184
185 // Return gracefully:
186 return StatusCode::SUCCESS;
187 }
188
190 : isSet( false ), isEL( false ), isELVec( false ) {
191
192 }
193
194} // 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)
const_iterator end() const
Return an end iterator.
const_iterator find(bit_t bit) const
If bit bit is set, return an iterator pointing to it.
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.
Interface for const operations on an auxiliary store.
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...