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#include "xAODBase/IParticleContainer.h"
25 template< typename TYPE >
26 bool TrigComposite_v1::hasDetail( const std::string& name ) const {
28 // Object used to check for the existence of an object:
29 ConstAccessor< TYPE > acc( name );
31 // Use the Accessor object for the check:
32 return acc.isAvailable( *this );
35 template< typename TYPE >
36 bool TrigComposite_v1::setDetail( const std::string& name, const TYPE& value ) {
37 // It should be pretty strange if this should fail:
38 SG::Accessor< TYPE > acc( name );
41 } catch(const std::exception& exc) {
42 std::cerr << "xAOD::TrigComposite_v1::setDetail ERROR Internal logic error found: [" << exc.what() << "]" << std::endl;
49 template< typename TYPE >
50 bool TrigComposite_v1::getDetail( const std::string& name, TYPE& value) const {
51 // Object used to access the auxiliary data:
52 ConstAccessor< TYPE > acc( name );
54 if( ! acc.isAvailable( *this ) ) {
61 template< typename TYPE >
62 TYPE TrigComposite_v1::getDetail( const std::string& name ) const {
63 ConstAccessor< TYPE > acc( name );
67 template< class CONTAINER >
69 TrigComposite_v1::setObjectLink( const std::string& name,
70 const ElementLink< CONTAINER >& link ) {
72 // Check link has valid persistent state, i.e. hash key is not
73 // zero, otherwise attempting to access its string key will seg
74 // fault later, e.g. in remapping.
75 if( link.key() == 0 ) {
76 std::cerr << "xAOD::TrigComposite_v1::setObjectLink ERROR "
77 << "link has invalid key hash of zero" << std::endl;
81 if( ! link.isValid() ) {
82 std::cerr << "xAOD::TrigComposite_v1::setObjectLink ERROR "
83 << "link is not valid" << std::endl;
87 // Do different things depending on whether this variable already
89 if( hasObjectLink( name ) ) {
90 // Find the right object:
91 const std::vector< std::string >& names = linkColNames();
92 for( size_t i = 0; i < names.size(); ++i ) {
93 if( names[ i ] != name ) continue;
94 // Extract the information out of the ElementLink:
95 linkColKeysNC()[ i ] = link.key();
96 linkColIndicesNC()[ i ] = link.index();
97 linkColClidsNC()[ i ] = ClassID_traits< CONTAINER >::ID();
101 // Some error happened...
102 std::cerr << "xAOD::TrigComposite_v1::setObjectLink ERROR Internal "
103 << "logic error found" << std::endl;
107 linkColNamesNC().push_back( name );
108 linkColKeysNC().push_back( link.key() );
109 linkColIndicesNC().push_back( link.index() );
110 linkColClidsNC().push_back( ClassID_traits< CONTAINER >::ID() );
116 template< class CONTAINER >
117 ElementLink< CONTAINER >
118 TrigComposite_v1::objectLink( const std::string& name ) const {
120 // Retrieve current store once (would be better to pass EventContext everywhere)
121 auto sg = SG::CurrentEventStore::store();
123 // Find the right object:
124 const std::vector< std::string >& names = linkColNames();
125 for( size_t i = 0; i < names.size(); ++i ) {
126 if( names[ i ] != name ) continue;
127 checkTypes< CONTAINER >(linkColClids()[ i ], name);
128 ElementLink< CONTAINER > link( linkColKeys()[ i ],
129 linkColIndices()[ i ],
132 if (link.isValid()) {
137 return ElementLink< CONTAINER >( linkColKeysRemap()[ i ],
138 linkColIndicesRemap()[ i ],
145 // We didn't find the link. :-(
146 throw std::runtime_error( "xAOD::TrigComposite::objectLink: No link "
147 "name \"" + name + "\" found" );
150 template< class OBJECT >
151 const OBJECT* TrigComposite_v1::object( const std::string& name ) const {
153 // Check if the link exists:
154 if( ! hasObjectLink( name ) ) {
158 // Retrieve current store once (would be better to pass EventContext everywhere)
159 auto sg = SG::CurrentEventStore::store();
162 const std::vector< std::string >& names = linkColNames();
163 for( size_t i = 0; i < names.size(); ++i ) {
164 if( names[ i ] != name ) continue;
165 // Check that it is of the right type:
166 checkTypes< DataVector< OBJECT > >(linkColClids()[ i ], name);
167 // Create a temporary ElementLink:
168 ElementLink< DataVector< OBJECT > > link( linkColKeys()[ i ],
169 linkColIndices()[ i ],
171 if( link.isValid() ) {
176 link = ElementLink< DataVector< OBJECT > >( linkColKeysRemap()[ i ],
177 linkColIndicesRemap()[ i ],
179 if( link.isValid() ) {
187 // There was an internal error. :-(
188 std::cerr << "xAOD::TrigComposite_v1::object ERROR Internal error "
189 << "detected" << std::endl;
193 template< class CONTAINER >
195 TrigComposite_v1::addObjectCollectionLink( const std::string& collectionName,
196 const ElementLink< CONTAINER >& link ) {
198 // No method currently provided to update or check for uniqueness of a link
199 // being added to a container.
201 // Add a new object (unless it would duplicate an existing one):
202 const std::string mangledName = collectionName + s_collectionSuffix;
203 if (!hasObjectLinkExact(mangledName, link.key(), link.index(), ClassID_traits< CONTAINER >::ID())) {
204 linkColNamesNC().push_back( std::move(mangledName) );
205 linkColKeysNC().push_back( link.key() );
206 linkColIndicesNC().push_back( link.index() );
207 linkColClidsNC().push_back( ClassID_traits< CONTAINER >::ID() );
212 template< class CONTAINER >
214 TrigComposite_v1::addObjectCollectionLinks( const std::string& collectionName,
215 const std::vector<ElementLink< CONTAINER >>& links ) {
217 for (const ElementLink< CONTAINER >& link : links ) {
218 addObjectCollectionLink( collectionName, link );
223 template< class CONTAINER >
224 std::vector<ElementLink< CONTAINER >>
225 TrigComposite_v1::objectCollectionLinks( const std::string& collectionName ) const {
226 std::vector<ElementLink< CONTAINER >> links;
227 const std::string mangledName = collectionName + s_collectionSuffix;
229 // Retrieve current store once (would be better to pass EventContext everywhere)
230 auto sg = SG::CurrentEventStore::store();
232 const std::vector< std::string >& names = linkColNames();
233 for( size_t i = 0; i < names.size(); ++i ) {
234 if( names[ i ] != mangledName ) continue;
235 // Check that it is of the right type:
236 checkTypes< CONTAINER >(linkColClids()[ i ], collectionName);
237 // Construct and add the link:
238 ElementLink< CONTAINER > link(linkColKeys()[ i ],
239 linkColIndices()[ i ],
241 if (link.isValid()) {
242 links.push_back( link );
243 } else if (isRemapped()) {
244 link = ElementLink< CONTAINER >(linkColKeysRemap()[ i ],
245 linkColIndicesRemap()[ i ],
247 links.push_back( link );
249 links.push_back( link );
255 template< class CONTAINER >
256 std::vector<std::string> TrigComposite_v1::getObjectNames() const {
257 return getObjectNames(ClassID_traits< CONTAINER >::ID());
260 template< class CONTAINER >
261 std::vector<std::string> TrigComposite_v1::getObjectCollectionNames() const {
262 return getObjectCollectionNames(ClassID_traits< CONTAINER >::ID());
265 template<class CONTAINER>
266 void TrigComposite_v1::checkTypes(const uint32_t storedCLID, const std::string& name) const {
267 if (ClassID_traits< CONTAINER >::ID() == ClassID_traits< xAOD::IParticleContainer >::ID()) {
268 if (!derivesFromIParticle(storedCLID)) {
269 throw ExcNotIParticleContainer( "xAOD::TrigComposite::checkTypes: "
270 "Cannot retrieve \"" + name + "\" as an IParticle");
272 } else if (ClassID_traits< CONTAINER >::ID() != storedCLID) {
273 const std::string typeName = SG::normalizedTypeinfoName( typeid( CONTAINER ) );
274 throw std::runtime_error( "xAOD::TrigComposite::checkTypes: "
275 "Wrong type (" + typeName + ") requested "
276 "for name \"" + name + "\"" );
282#endif // XAODTRIGGER_VERSIONS_TRIGCOMPOSITE_V1_ICC