1 // Dear emacs, this is -*- c++ -*-
4 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
7 // $Id: TrigComposite_v1.icc 784388 2016-11-15 17:08:58Z tamartin $
8 #ifndef XAODTRIGGER_VERSIONS_TRIGCOMPOSITE_V1_ICC
9 #define XAODTRIGGER_VERSIONS_TRIGCOMPOSITE_V1_ICC
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 if( ! hasDetail<TYPE>(name) ) {
63 // Object used to access the auxiliary data:
64 ConstAccessor< TYPE > acc( name );
69 template< typename TYPE >
70 TYPE TrigComposite_v1::getDetail( const std::string& name ) const {
72 if ( getDetail(name, temp) == false )
73 throw std::runtime_error( "xAOD::TrigComposite::getDetail<TYPE>("+name+") encountered missing detail");
77 template< class CONTAINER >
79 TrigComposite_v1::setObjectLink( const std::string& name,
80 const ElementLink< CONTAINER >& link ) {
82 // Check link has valid persistent state, i.e. hash key is not
83 // zero, otherwise attempting to access its string key will seg
84 // fault later, e.g. in remapping.
85 if( link.key() == 0 ) {
86 std::cerr << "xAOD::TrigComposite_v1::setObjectLink ERROR "
87 << "link has invalid key hash of zero" << std::endl;
91 if( ! link.isValid() ) {
92 std::cerr << "xAOD::TrigComposite_v1::setObjectLink ERROR "
93 << "link is not valid" << std::endl;
97 // Do different things depending on whether this variable already
99 if( hasObjectLink( name ) ) {
100 // Find the right object:
101 const std::vector< std::string >& names = linkColNames();
102 for( size_t i = 0; i < names.size(); ++i ) {
103 if( names[ i ] != name ) continue;
104 // Extract the information out of the ElementLink:
105 linkColKeysNC()[ i ] = link.key();
106 linkColIndicesNC()[ i ] = link.index();
107 linkColClidsNC()[ i ] = ClassID_traits< CONTAINER >::ID();
111 // Some error happened...
112 std::cerr << "xAOD::TrigComposite_v1::setObjectLink ERROR Internal "
113 << "logic error found" << std::endl;
117 linkColNamesNC().push_back( name );
118 linkColKeysNC().push_back( link.key() );
119 linkColIndicesNC().push_back( link.index() );
120 linkColClidsNC().push_back( ClassID_traits< CONTAINER >::ID() );
126 template< class CONTAINER >
127 ElementLink< CONTAINER >
128 TrigComposite_v1::objectLink( const std::string& name ) const {
130 // Find the right object:
131 const std::vector< std::string >& names = linkColNames();
132 for( size_t i = 0; i < names.size(); ++i ) {
133 if( names[ i ] != name ) continue;
134 checkTypes< CONTAINER >(linkColClids()[ i ], name);
135 ElementLink< CONTAINER > link( linkColKeys()[ i ],
136 linkColIndices()[ i ] );
138 if (link.isValid()) {
143 return ElementLink< CONTAINER >( linkColKeysRemap()[ i ],
144 linkColIndicesRemap()[ i ] );
150 // We didn't find the link. :-(
151 throw std::runtime_error( "xAOD::TrigComposite::objectLink: No link "
152 "name \"" + name + "\" found" );
153 return ElementLink< CONTAINER >();
156 template< class OBJECT >
157 const OBJECT* TrigComposite_v1::object( const std::string& name ) const {
159 // Check if the link exists:
160 if( ! hasObjectLink( name ) ) {
165 const std::vector< std::string >& names = linkColNames();
166 for( size_t i = 0; i < names.size(); ++i ) {
167 if( names[ i ] != name ) continue;
168 // Check that it is of the right type:
169 checkTypes< DataVector< OBJECT > >(linkColClids()[ i ], name);
170 // Create a temporary ElementLink:
171 ElementLink< DataVector< OBJECT > > link( linkColKeys()[ i ],
172 linkColIndices()[ i ] );
173 if( link.isValid() ) {
178 link = ElementLink< DataVector< OBJECT > >( linkColKeysRemap()[ i ],
179 linkColIndicesRemap()[ i ] );
180 if( link.isValid() ) {
188 // There was an internal error. :-(
189 std::cerr << "xAOD::TrigComposite_v1::object ERROR Internal error "
190 << "detected" << std::endl;
194 template< class CONTAINER >
196 TrigComposite_v1::addObjectCollectionLink( const std::string& collectionName,
197 const ElementLink< CONTAINER >& link ) {
199 // No method currently provided to update or check for uniqueness of a link
200 // being added to a container.
202 // Add a new object (unless it would duplicate an existing one):
203 const std::string mangledName = collectionName + s_collectionSuffix;
204 if (!hasObjectLinkExact(mangledName, link.key(), link.index(), ClassID_traits< CONTAINER >::ID())) {
205 linkColNamesNC().push_back( mangledName );
206 linkColKeysNC().push_back( link.key() );
207 linkColIndicesNC().push_back( link.index() );
208 linkColClidsNC().push_back( ClassID_traits< CONTAINER >::ID() );
213 template< class CONTAINER >
215 TrigComposite_v1::addObjectCollectionLinks( const std::string& collectionName,
216 const std::vector<ElementLink< CONTAINER >>& links ) {
218 for (const ElementLink< CONTAINER >& link : links ) {
219 addObjectCollectionLink( collectionName, link );
224 template< class CONTAINER >
225 std::vector<ElementLink< CONTAINER >>
226 TrigComposite_v1::objectCollectionLinks( const std::string& collectionName ) const {
227 std::vector<ElementLink< CONTAINER >> links;
228 const std::string mangledName = collectionName + s_collectionSuffix;
230 const std::vector< std::string >& names = linkColNames();
231 for( size_t i = 0; i < names.size(); ++i ) {
232 if( names[ i ] != mangledName ) continue;
233 // Check that it is of the right type:
234 checkTypes< CONTAINER >(linkColClids()[ i ], collectionName);
235 // Construct and add the link:
236 ElementLink< CONTAINER > link(linkColKeys()[ i ],
237 linkColIndices()[ i ]);
238 if (link.isValid()) {
239 links.push_back( link );
240 } else if (isRemapped()) {
241 link = ElementLink< CONTAINER >(linkColKeysRemap()[ i ],
242 linkColIndicesRemap()[ i ]);
243 links.push_back( link );
245 links.push_back( link );
251 template< class CONTAINER >
252 std::vector<std::string> TrigComposite_v1::getObjectNames() const {
254 std::vector< std::string > returnVec;
255 const std::vector< std::string >& names = linkColNames();
256 const std::vector< uint32_t >& clids = linkColClids();
258 for( size_t i = 0; i < names.size(); ++i ) {
259 if (names[i].find(s_collectionSuffix) != std::string::npos) { //Note: !=
260 continue; // ARE *NOT* interested in collection links here
262 bool clidMatch = false;
263 if (ClassID_traits< CONTAINER >::ID() == ClassID_traits< xAOD::IParticleContainer >::ID()) {
264 clidMatch = derivesFromIParticle(clids[i]);
265 } else if (ClassID_traits< CONTAINER >::ID() == clids[i]) {
269 returnVec.push_back( names[i] );
275 template< class CONTAINER >
276 std::vector<std::string> TrigComposite_v1::getObjectCollectionNames() const {
278 std::vector< std::string > returnVec;
279 const std::vector< std::string >& names = linkColNames();
280 const std::vector< uint32_t >& clids = linkColClids();
282 for( size_t i = 0; i < names.size(); ++i ) {
283 if (names[i].find(s_collectionSuffix) == std::string::npos) { // Note: ==
284 continue; // ARE interested in collection links here
286 bool clidMatch = false;
287 if (ClassID_traits< CONTAINER >::ID() == ClassID_traits< xAOD::IParticleContainer >::ID()) {
288 clidMatch = derivesFromIParticle(clids[i]);
289 } else if (ClassID_traits< CONTAINER >::ID() == clids[i]) {
293 // Unlike with single links, we expect to find multiple links here. Only add the name once.
294 // Name is mangled in storage, need to un-mangle it before returning it to the user.
295 const std::string unmangledName = names[i].substr(0, names[i].size() - s_collectionSuffix.size());
296 if ( std::none_of(returnVec.begin(), returnVec.end(), [&](const auto& s) {return unmangledName == s;}) ) {
297 returnVec.push_back( unmangledName );
304 template<class CONTAINER>
305 void TrigComposite_v1::checkTypes(const uint32_t storedCLID, const std::string& name) const {
306 if (ClassID_traits< CONTAINER >::ID() == ClassID_traits< xAOD::IParticleContainer >::ID()) {
307 if (!derivesFromIParticle(storedCLID)) {
308 throw ExcNotIParticleContainer( "xAOD::TrigComposite::checkTypes: "
309 "Cannot retrieve \"" + name + "\" as an IParticle");
311 } else if (ClassID_traits< CONTAINER >::ID() != storedCLID) {
312 const std::string typeName = SG::normalizedTypeinfoName( typeid( CONTAINER ) );
313 throw std::runtime_error( "xAOD::TrigComposite::checkTypes: "
314 "Wrong type (" + typeName + ") requested "
315 "for name \"" + name + "\"" );
321 #endif // XAODTRIGGER_VERSIONS_TRIGCOMPOSITE_V1_ICC