ATLAS Offline Software
TrigComposite_v1.icc
Go to the documentation of this file.
1 // Dear emacs, this is -*- c++ -*-
2 
3 /*
4  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
5 */
6 
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
10 
11 // System include(s):
12 #include <iostream>
13 #include <stdexcept>
14 
15 // xAOD include(s):
16 #include "AthContainers/DataVector.h"
17 #include "AthContainers/normalizedTypeinfoName.h"
18 #include "AthContainers/Accessor.h"
19 #include "xAODCore/CLASS_DEF.h"
20 
21 // In "standalone mode" xAOD::IParticleContainer doesn't have a CLID
22 // defined for it. But this code requires one to be set.
23 //
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
31 
32 namespace xAOD {
33 
34  template< typename TYPE >
35  bool TrigComposite_v1::hasDetail( const std::string& name ) const {
36 
37  // Object used to check for the existence of an object:
38  ConstAccessor< TYPE > acc( name );
39 
40  // Use the Accessor object for the check:
41  return acc.isAvailable( *this );
42  }
43 
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 );
48  try {
49  acc( *this ) = value;
50  } catch(const std::exception& exc) {
51  std::cerr << "xAOD::TrigComposite_v1::setDetail ERROR Internal logic error found: [" << exc.what() << "]" << std::endl;
52  return false;
53  }
54  // Return gracefully:
55  return true;
56  }
57 
58  template< typename TYPE >
59  bool TrigComposite_v1::getDetail( const std::string& name, TYPE& value) const {
60  if( ! hasDetail<TYPE>(name) ) {
61  return false;
62  }
63  // Object used to access the auxiliary data:
64  ConstAccessor< TYPE > acc( name );
65  value = acc( *this );
66  return true;
67  }
68 
69  template< typename TYPE >
70  TYPE TrigComposite_v1::getDetail( const std::string& name ) const {
71  TYPE temp;
72  if ( getDetail(name, temp) == false )
73  throw std::runtime_error( "xAOD::TrigComposite::getDetail<TYPE>("+name+") encountered missing detail");
74  return temp; // RVO
75  }
76 
77  template< class CONTAINER >
78  bool
79  TrigComposite_v1::setObjectLink( const std::string& name,
80  const ElementLink< CONTAINER >& link ) {
81 
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;
88  return false;
89  }
90 
91  if( ! link.isValid() ) {
92  std::cerr << "xAOD::TrigComposite_v1::setObjectLink ERROR "
93  << "link is not valid" << std::endl;
94  return false;
95  }
96 
97  // Do different things depending on whether this variable already
98  // exists or not:
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();
108  // We're done:
109  return true;
110  }
111  // Some error happened...
112  std::cerr << "xAOD::TrigComposite_v1::setObjectLink ERROR Internal "
113  << "logic error found" << std::endl;
114  return false;
115  } else {
116  // Add a new object:
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() );
121  // And we're done:
122  return true;
123  }
124  }
125 
126  template< class CONTAINER >
127  ElementLink< CONTAINER >
128  TrigComposite_v1::objectLink( const std::string& name ) const {
129 
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 ] );
137 
138  if (link.isValid()) {
139  return link;
140  }
141 
142  if (isRemapped()) {
143  return ElementLink< CONTAINER >( linkColKeysRemap()[ i ],
144  linkColIndicesRemap()[ i ] );
145  }
146 
147  return link;
148  }
149 
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 >();
154  }
155 
156  template< class OBJECT >
157  const OBJECT* TrigComposite_v1::object( const std::string& name ) const {
158 
159  // Check if the link exists:
160  if( ! hasObjectLink( name ) ) {
161  return nullptr;
162  }
163 
164  // Now look for it:
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() ) {
174  return *link;
175  }
176 
177  if (isRemapped()) {
178  link = ElementLink< DataVector< OBJECT > >( linkColKeysRemap()[ i ],
179  linkColIndicesRemap()[ i ] );
180  if( link.isValid() ) {
181  return *link;
182  }
183  }
184 
185  return nullptr;
186  }
187 
188  // There was an internal error. :-(
189  std::cerr << "xAOD::TrigComposite_v1::object ERROR Internal error "
190  << "detected" << std::endl;
191  return nullptr;
192  }
193 
194  template< class CONTAINER >
195  bool
196  TrigComposite_v1::addObjectCollectionLink( const std::string& collectionName,
197  const ElementLink< CONTAINER >& link ) {
198 
199  // No method currently provided to update or check for uniqueness of a link
200  // being added to a container.
201 
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() );
209  }
210  return true;
211  }
212 
213  template< class CONTAINER >
214  bool
215  TrigComposite_v1::addObjectCollectionLinks( const std::string& collectionName,
216  const std::vector<ElementLink< CONTAINER >>& links ) {
217  // Add all links
218  for (const ElementLink< CONTAINER >& link : links ) {
219  addObjectCollectionLink( collectionName, link );
220  }
221  return true;
222  }
223 
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;
229 
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 );
244  } else {
245  links.push_back( link );
246  }
247  }
248  return links;
249  }
250 
251  template< class CONTAINER >
252  std::vector<std::string> TrigComposite_v1::getObjectNames() const {
253 
254  std::vector< std::string > returnVec;
255  const std::vector< std::string >& names = linkColNames();
256  const std::vector< uint32_t >& clids = linkColClids();
257 
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
261  }
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]) {
266  clidMatch = true;
267  }
268  if (clidMatch) {
269  returnVec.push_back( names[i] );
270  }
271  }
272  return returnVec;
273  }
274 
275  template< class CONTAINER >
276  std::vector<std::string> TrigComposite_v1::getObjectCollectionNames() const {
277 
278  std::vector< std::string > returnVec;
279  const std::vector< std::string >& names = linkColNames();
280  const std::vector< uint32_t >& clids = linkColClids();
281 
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
285  }
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]) {
290  clidMatch = true;
291  }
292  if (clidMatch) {
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 );
298  }
299  }
300  }
301  return returnVec;
302  }
303 
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");
310  }
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 + "\"" );
316  }
317  }
318 
319 } // namespace xAOD
320 
321 #endif // XAODTRIGGER_VERSIONS_TRIGCOMPOSITE_V1_ICC