1 // Dear emacs, this is -*- c++ -*-
4 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
7 #ifndef XAODTRIGGER_VERSIONS_TRIGCOMPOSITE_V1_ICC
8 #define XAODTRIGGER_VERSIONS_TRIGCOMPOSITE_V1_ICC
15 #include "AthContainers/tools/CurrentEventStore.h"
16 #include "AthContainers/DataVector.h"
17 #include "AthContainers/normalizedTypeinfoName.h"
18 #include "AthContainers/Accessor.h"
19 #include "xAODCore/CLASS_DEF.h"
21 // In "standalone mode" xAOD::IParticleContainer doesn't have a CLID
22 // defined for it. But this code requires one to be set.
24 // The following is incredibly ugly, but this is the best that I could
25 // come up with on short notice. Note that the CLID value is copy-pasted
26 // from the IParticleContainer.h header.
27 #include "xAODBase/IParticleContainer.h"
28 #ifdef XAOD_STANDALONE
29 CLASS_DEF( xAOD::IParticleContainer, 1241842700, 1 )
30 #endif // XAOD_STANDALONE
34 template< typename TYPE >
35 bool TrigComposite_v1::hasDetail( const std::string& name ) const {
37 // Object used to check for the existence of an object:
38 ConstAccessor< TYPE > acc( name );
40 // Use the Accessor object for the check:
41 return acc.isAvailable( *this );
44 template< typename TYPE >
45 bool TrigComposite_v1::setDetail( const std::string& name, const TYPE& value ) {
46 // It should be pretty strange if this should fail:
47 SG::Accessor< TYPE > acc( name );
50 } catch(const std::exception& exc) {
51 std::cerr << "xAOD::TrigComposite_v1::setDetail ERROR Internal logic error found: [" << exc.what() << "]" << std::endl;
58 template< typename TYPE >
59 bool TrigComposite_v1::getDetail( const std::string& name, TYPE& value) const {
60 // Object used to access the auxiliary data:
61 ConstAccessor< TYPE > acc( name );
63 if( ! acc.isAvailable( *this ) ) {
70 template< typename TYPE >
71 TYPE TrigComposite_v1::getDetail( const std::string& name ) const {
72 ConstAccessor< TYPE > acc( name );
76 template< class CONTAINER >
78 TrigComposite_v1::setObjectLink( const std::string& name,
79 const ElementLink< CONTAINER >& link ) {
81 // Check link has valid persistent state, i.e. hash key is not
82 // zero, otherwise attempting to access its string key will seg
83 // fault later, e.g. in remapping.
84 if( link.key() == 0 ) {
85 std::cerr << "xAOD::TrigComposite_v1::setObjectLink ERROR "
86 << "link has invalid key hash of zero" << std::endl;
90 if( ! link.isValid() ) {
91 std::cerr << "xAOD::TrigComposite_v1::setObjectLink ERROR "
92 << "link is not valid" << std::endl;
96 // Do different things depending on whether this variable already
98 if( hasObjectLink( name ) ) {
99 // Find the right object:
100 const std::vector< std::string >& names = linkColNames();
101 for( size_t i = 0; i < names.size(); ++i ) {
102 if( names[ i ] != name ) continue;
103 // Extract the information out of the ElementLink:
104 linkColKeysNC()[ i ] = link.key();
105 linkColIndicesNC()[ i ] = link.index();
106 linkColClidsNC()[ i ] = ClassID_traits< CONTAINER >::ID();
110 // Some error happened...
111 std::cerr << "xAOD::TrigComposite_v1::setObjectLink ERROR Internal "
112 << "logic error found" << std::endl;
116 linkColNamesNC().push_back( name );
117 linkColKeysNC().push_back( link.key() );
118 linkColIndicesNC().push_back( link.index() );
119 linkColClidsNC().push_back( ClassID_traits< CONTAINER >::ID() );
125 template< class CONTAINER >
126 ElementLink< CONTAINER >
127 TrigComposite_v1::objectLink( const std::string& name ) const {
129 // Retrieve current store once (would be better to pass EventContext everywhere)
130 auto sg = SG::CurrentEventStore::store();
132 // Find the right object:
133 const std::vector< std::string >& names = linkColNames();
134 for( size_t i = 0; i < names.size(); ++i ) {
135 if( names[ i ] != name ) continue;
136 checkTypes< CONTAINER >(linkColClids()[ i ], name);
137 ElementLink< CONTAINER > link( linkColKeys()[ i ],
138 linkColIndices()[ i ],
141 if (link.isValid()) {
146 return ElementLink< CONTAINER >( linkColKeysRemap()[ i ],
147 linkColIndicesRemap()[ i ],
154 // We didn't find the link. :-(
155 throw std::runtime_error( "xAOD::TrigComposite::objectLink: No link "
156 "name \"" + name + "\" found" );
159 template< class OBJECT >
160 const OBJECT* TrigComposite_v1::object( const std::string& name ) const {
162 // Check if the link exists:
163 if( ! hasObjectLink( name ) ) {
167 // Retrieve current store once (would be better to pass EventContext everywhere)
168 auto sg = SG::CurrentEventStore::store();
171 const std::vector< std::string >& names = linkColNames();
172 for( size_t i = 0; i < names.size(); ++i ) {
173 if( names[ i ] != name ) continue;
174 // Check that it is of the right type:
175 checkTypes< DataVector< OBJECT > >(linkColClids()[ i ], name);
176 // Create a temporary ElementLink:
177 ElementLink< DataVector< OBJECT > > link( linkColKeys()[ i ],
178 linkColIndices()[ i ],
180 if( link.isValid() ) {
185 link = ElementLink< DataVector< OBJECT > >( linkColKeysRemap()[ i ],
186 linkColIndicesRemap()[ i ],
188 if( link.isValid() ) {
196 // There was an internal error. :-(
197 std::cerr << "xAOD::TrigComposite_v1::object ERROR Internal error "
198 << "detected" << std::endl;
202 template< class CONTAINER >
204 TrigComposite_v1::addObjectCollectionLink( const std::string& collectionName,
205 const ElementLink< CONTAINER >& link ) {
207 // No method currently provided to update or check for uniqueness of a link
208 // being added to a container.
210 // Add a new object (unless it would duplicate an existing one):
211 const std::string mangledName = collectionName + s_collectionSuffix;
212 if (!hasObjectLinkExact(mangledName, link.key(), link.index(), ClassID_traits< CONTAINER >::ID())) {
213 linkColNamesNC().push_back( std::move(mangledName) );
214 linkColKeysNC().push_back( link.key() );
215 linkColIndicesNC().push_back( link.index() );
216 linkColClidsNC().push_back( ClassID_traits< CONTAINER >::ID() );
221 template< class CONTAINER >
223 TrigComposite_v1::addObjectCollectionLinks( const std::string& collectionName,
224 const std::vector<ElementLink< CONTAINER >>& links ) {
226 for (const ElementLink< CONTAINER >& link : links ) {
227 addObjectCollectionLink( collectionName, link );
232 template< class CONTAINER >
233 std::vector<ElementLink< CONTAINER >>
234 TrigComposite_v1::objectCollectionLinks( const std::string& collectionName ) const {
235 std::vector<ElementLink< CONTAINER >> links;
236 const std::string mangledName = collectionName + s_collectionSuffix;
238 // Retrieve current store once (would be better to pass EventContext everywhere)
239 auto sg = SG::CurrentEventStore::store();
241 const std::vector< std::string >& names = linkColNames();
242 for( size_t i = 0; i < names.size(); ++i ) {
243 if( names[ i ] != mangledName ) continue;
244 // Check that it is of the right type:
245 checkTypes< CONTAINER >(linkColClids()[ i ], collectionName);
246 // Construct and add the link:
247 ElementLink< CONTAINER > link(linkColKeys()[ i ],
248 linkColIndices()[ i ],
250 if (link.isValid()) {
251 links.push_back( link );
252 } else if (isRemapped()) {
253 link = ElementLink< CONTAINER >(linkColKeysRemap()[ i ],
254 linkColIndicesRemap()[ i ],
256 links.push_back( link );
258 links.push_back( link );
264 template< class CONTAINER >
265 std::vector<std::string> TrigComposite_v1::getObjectNames() const {
267 std::vector< std::string > returnVec;
268 const std::vector< std::string >& names = linkColNames();
269 const std::vector< uint32_t >& clids = linkColClids();
271 for( size_t i = 0; i < names.size(); ++i ) {
272 if (names[i].find(s_collectionSuffix) != std::string::npos) { //Note: !=
273 continue; // ARE *NOT* interested in collection links here
275 bool clidMatch = false;
276 if (ClassID_traits< CONTAINER >::ID() == ClassID_traits< xAOD::IParticleContainer >::ID()) {
277 clidMatch = derivesFromIParticle(clids[i]);
278 } else if (ClassID_traits< CONTAINER >::ID() == clids[i]) {
282 returnVec.push_back( names[i] );
288 template< class CONTAINER >
289 std::vector<std::string> TrigComposite_v1::getObjectCollectionNames() const {
291 std::vector< std::string > returnVec;
292 const std::vector< std::string >& names = linkColNames();
293 const std::vector< uint32_t >& clids = linkColClids();
295 for( size_t i = 0; i < names.size(); ++i ) {
296 if (names[i].find(s_collectionSuffix) == std::string::npos) { // Note: ==
297 continue; // ARE interested in collection links here
299 bool clidMatch = false;
300 if (ClassID_traits< CONTAINER >::ID() == ClassID_traits< xAOD::IParticleContainer >::ID()) {
301 clidMatch = derivesFromIParticle(clids[i]);
302 } else if (ClassID_traits< CONTAINER >::ID() == clids[i]) {
306 // Unlike with single links, we expect to find multiple links here. Only add the name once.
307 // Name is mangled in storage, need to un-mangle it before returning it to the user.
308 const std::string unmangledName = names[i].substr(0, names[i].size() - s_collectionSuffix.size());
309 if ( std::none_of(returnVec.begin(), returnVec.end(), [&](const auto& s) {return unmangledName == s;}) ) {
310 returnVec.push_back( std::move(unmangledName) );
317 template<class CONTAINER>
318 void TrigComposite_v1::checkTypes(const uint32_t storedCLID, const std::string& name) const {
319 if (ClassID_traits< CONTAINER >::ID() == ClassID_traits< xAOD::IParticleContainer >::ID()) {
320 if (!derivesFromIParticle(storedCLID)) {
321 throw ExcNotIParticleContainer( "xAOD::TrigComposite::checkTypes: "
322 "Cannot retrieve \"" + name + "\" as an IParticle");
324 } else if (ClassID_traits< CONTAINER >::ID() != storedCLID) {
325 const std::string typeName = SG::normalizedTypeinfoName( typeid( CONTAINER ) );
326 throw std::runtime_error( "xAOD::TrigComposite::checkTypes: "
327 "Wrong type (" + typeName + ") requested "
328 "for name \"" + name + "\"" );
334 #endif // XAODTRIGGER_VERSIONS_TRIGCOMPOSITE_V1_ICC