ATLAS Offline Software
Loading...
Searching...
No Matches
TrigComposite_v1.icc
Go to the documentation of this file.
1// Dear emacs, this is -*- c++ -*-
2
3/*
4 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
5*/
6
7#ifndef XAODTRIGGER_VERSIONS_TRIGCOMPOSITE_V1_ICC
8#define XAODTRIGGER_VERSIONS_TRIGCOMPOSITE_V1_ICC
9
10// System include(s):
11#include <iostream>
12#include <stdexcept>
13
14// xAOD include(s):
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"
20
21#include "xAODBase/IParticleContainer.h"
22
23namespace xAOD {
24
25 template< typename TYPE >
26 bool TrigComposite_v1::hasDetail( const std::string& name ) const {
27
28 // Object used to check for the existence of an object:
29 ConstAccessor< TYPE > acc( name );
30
31 // Use the Accessor object for the check:
32 return acc.isAvailable( *this );
33 }
34
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 );
39 try {
40 acc( *this ) = value;
41 } catch(const std::exception& exc) {
42 std::cerr << "xAOD::TrigComposite_v1::setDetail ERROR Internal logic error found: [" << exc.what() << "]" << std::endl;
43 return false;
44 }
45 // Return gracefully:
46 return true;
47 }
48
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 );
53
54 if( ! acc.isAvailable( *this ) ) {
55 return false;
56 }
57 value = acc( *this );
58 return true;
59 }
60
61 template< typename TYPE >
62 TYPE TrigComposite_v1::getDetail( const std::string& name ) const {
63 ConstAccessor< TYPE > acc( name );
64 return acc( *this );
65 }
66
67 template< class CONTAINER >
68 bool
69 TrigComposite_v1::setObjectLink( const std::string& name,
70 const ElementLink< CONTAINER >& link ) {
71
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;
78 return false;
79 }
80
81 if( ! link.isValid() ) {
82 std::cerr << "xAOD::TrigComposite_v1::setObjectLink ERROR "
83 << "link is not valid" << std::endl;
84 return false;
85 }
86
87 // Do different things depending on whether this variable already
88 // exists or not:
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();
98 // We're done:
99 return true;
100 }
101 // Some error happened...
102 std::cerr << "xAOD::TrigComposite_v1::setObjectLink ERROR Internal "
103 << "logic error found" << std::endl;
104 return false;
105 } else {
106 // Add a new object:
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() );
111 // And we're done:
112 return true;
113 }
114 }
115
116 template< class CONTAINER >
117 ElementLink< CONTAINER >
118 TrigComposite_v1::objectLink( const std::string& name ) const {
119
120 // Retrieve current store once (would be better to pass EventContext everywhere)
121 auto sg = SG::CurrentEventStore::store();
122
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 ],
130 sg );
131
132 if (link.isValid()) {
133 return link;
134 }
135
136 if (isRemapped()) {
137 return ElementLink< CONTAINER >( linkColKeysRemap()[ i ],
138 linkColIndicesRemap()[ i ],
139 sg );
140 }
141
142 return link;
143 }
144
145 // We didn't find the link. :-(
146 throw std::runtime_error( "xAOD::TrigComposite::objectLink: No link "
147 "name \"" + name + "\" found" );
148 }
149
150 template< class OBJECT >
151 const OBJECT* TrigComposite_v1::object( const std::string& name ) const {
152
153 // Check if the link exists:
154 if( ! hasObjectLink( name ) ) {
155 return nullptr;
156 }
157
158 // Retrieve current store once (would be better to pass EventContext everywhere)
159 auto sg = SG::CurrentEventStore::store();
160
161 // Now look for it:
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 ],
170 sg );
171 if( link.isValid() ) {
172 return *link;
173 }
174
175 if (isRemapped()) {
176 link = ElementLink< DataVector< OBJECT > >( linkColKeysRemap()[ i ],
177 linkColIndicesRemap()[ i ],
178 sg );
179 if( link.isValid() ) {
180 return *link;
181 }
182 }
183
184 return nullptr;
185 }
186
187 // There was an internal error. :-(
188 std::cerr << "xAOD::TrigComposite_v1::object ERROR Internal error "
189 << "detected" << std::endl;
190 return nullptr;
191 }
192
193 template< class CONTAINER >
194 bool
195 TrigComposite_v1::addObjectCollectionLink( const std::string& collectionName,
196 const ElementLink< CONTAINER >& link ) {
197
198 // No method currently provided to update or check for uniqueness of a link
199 // being added to a container.
200
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() );
208 }
209 return true;
210 }
211
212 template< class CONTAINER >
213 bool
214 TrigComposite_v1::addObjectCollectionLinks( const std::string& collectionName,
215 const std::vector<ElementLink< CONTAINER >>& links ) {
216 // Add all links
217 for (const ElementLink< CONTAINER >& link : links ) {
218 addObjectCollectionLink( collectionName, link );
219 }
220 return true;
221 }
222
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;
228
229 // Retrieve current store once (would be better to pass EventContext everywhere)
230 auto sg = SG::CurrentEventStore::store();
231
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 ],
240 sg);
241 if (link.isValid()) {
242 links.push_back( link );
243 } else if (isRemapped()) {
244 link = ElementLink< CONTAINER >(linkColKeysRemap()[ i ],
245 linkColIndicesRemap()[ i ],
246 sg);
247 links.push_back( link );
248 } else {
249 links.push_back( link );
250 }
251 }
252 return links;
253 }
254
255 template< class CONTAINER >
256 std::vector<std::string> TrigComposite_v1::getObjectNames() const {
257 return getObjectNames(ClassID_traits< CONTAINER >::ID());
258 }
259
260 template< class CONTAINER >
261 std::vector<std::string> TrigComposite_v1::getObjectCollectionNames() const {
262 return getObjectCollectionNames(ClassID_traits< CONTAINER >::ID());
263 }
264
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");
271 }
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 + "\"" );
277 }
278 }
279
280} // namespace xAOD
281
282#endif // XAODTRIGGER_VERSIONS_TRIGCOMPOSITE_V1_ICC