ATLAS Offline Software
SgTEventMeta.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef ASGTOOLS_SGTEVENTMETA_ICC
6 #define ASGTOOLS_SGTEVENTMETA_ICC
7 
8 // System include(s):
9 #include <iostream>
10 #include <sstream>
11 #include <stdexcept>
12 
13 // xAOD include(s):
14 #ifdef XAOD_STANDALONE
15 # include "xAODRootAccess/TEvent.h"
16 #endif // XAOD_STANDALONE
17 
18 namespace asg {
19 
20  /// A constant used in the error message printouts
21  static const char* META_ERROR_SOURCE = "asg::SgTEventMeta ERROR ";
22 
23  template< typename T >
24  bool SgTEventMeta::contains( const std::string& name ) {
25 
26  // Make sure the object is initialised:
27  if( (!m_event || !m_event.load()) && initialize().isFailure() ) {
28  std::cout << META_ERROR_SOURCE << "Couldn't initialise the tool"
29  << std::endl;
30  return false;
31  }
32 
33  // Decide which function to call:
34  if( m_type == InputStore ) {
35  return m_event.load()->template containsMeta< T >( name );
36  } else if( m_type == OutputStore ) {
37  return m_event.load()->template transientContainsMeta< T >( name );
38  } else {
39  std::cout << META_ERROR_SOURCE << "Unknown store type (" << m_type
40  << ") defined" << std::endl;
41  return false;
42  }
43  }
44 
45  template< typename T >
46  bool SgTEventMeta::transientContains( const std::string& name ) const {
47 
48  // Make sure the object is initialised:
49  if( (!m_event || !m_event.load()) && initialize().isFailure() ) {
50  std::cout << META_ERROR_SOURCE << "Couldn't initialise the tool"
51  << std::endl;
52  return false;
53  }
54 
55  // Forward the call:
56  return m_event.load()->template transientContainsMeta< T >( name );
57  }
58 
59  template< typename T >
60  StatusCode SgTEventMeta::retrieve( T*& obj, const std::string& name ) {
61 
62  // Make sure the object is initialised:
63  if( (!m_event || !m_event.load()) && initialize().isFailure() ) {
64  std::cout << META_ERROR_SOURCE << "Couldn't initialise the tool"
65  << std::endl;
66  return StatusCode::FAILURE;
67  }
68 
69  // We can only non-const retrieve from the output store:
70  if( m_type == InputStore ) {
71  std::cout << META_ERROR_SOURCE << "It's not possible to non-const "
72  << "retrieve objects from the input metadata store"
73  << std::endl;
74  return StatusCode::FAILURE;
75  }
76 
77  // Forward the call:
78  if( ! m_event.load()->retrieveMetaOutput( obj, name ).isSuccess() ) {
79  std::cout << META_ERROR_SOURCE << "Failed to retrieve output metadata "
80  << "object with name " << name << std::endl;
81  return StatusCode::FAILURE;
82  }
83 
84  // We succeeded:
85  return StatusCode::SUCCESS;
86  }
87 
88  template< typename T >
89  StatusCode SgTEventMeta::retrieve( const T*& obj, const std::string& name ) {
90 
91  // Make sure the object is initialised:
92  if( (!m_event || !m_event.load()) && initialize().isFailure() ) {
93  std::cout << META_ERROR_SOURCE << "Couldn't initialise the tool"
94  << std::endl;
95  return StatusCode::FAILURE;
96  }
97 
98  // Decide which function to call:
99  if( m_type == InputStore ) {
100  // Forward the call:
101  if( ! m_event.load()->retrieveMetaInput( obj, name ).isSuccess() ) {
102  std::cout << META_ERROR_SOURCE << "Failed to retrieve input "
103  << "metadata object with name " << name << std::endl;
104  return StatusCode::FAILURE;
105  }
106  } else if( m_type == OutputStore ) {
107  // Forward the call:
108  if( ! m_event.load()->retrieveMetaOutput( obj, name ).isSuccess() ) {
109  std::cout << META_ERROR_SOURCE << "Failed to retrieve output "
110  << "metadata object with name " << name << std::endl;
111  return StatusCode::FAILURE;
112  }
113  } else {
114  std::cout << META_ERROR_SOURCE << "Unknown store type (" << m_type
115  << ") defined" << std::endl;
116  return StatusCode::FAILURE;
117  }
118 
119  // We succeeded:
120  return StatusCode::SUCCESS;
121  }
122 
123  template< typename T >
124  StatusCode SgTEventMeta::record( std::unique_ptr<T> obj, const std::string& name ) {
125 
126  // Make sure the object is initialised:
127  if( (!m_event || !m_event.load()) && initialize().isFailure() ) {
128  std::cout << META_ERROR_SOURCE << "Couldn't initialise the tool"
129  << std::endl;
130  return StatusCode::FAILURE;
131  }
132 
133  // We can't record stuff into the input store:
134  if( m_type == InputStore ) {
135  std::cout << META_ERROR_SOURCE << "It's not possible to record "
136  << "objects into the input metadata store" << std::endl;
137  return StatusCode::FAILURE;
138  }
139 
140  // Forward the call:
141  if( ! m_event.load()->recordMeta( std::move( obj ), name ).isSuccess() ) {
142  std::cout << META_ERROR_SOURCE << "Failed to record output metadata "
143  << "object with name " << name << std::endl;
144  return StatusCode::FAILURE;
145  }
146 
147  // We succeeded:
148  return StatusCode::SUCCESS;
149  }
150 
151  template< typename T >
152  StatusCode SgTEventMeta::record( T* obj, const std::string& name ) {
153  // this is the legacy interface, forwarding it to new one
154  return record( std::unique_ptr<T>(obj), name );
155  }
156 
157  template< typename T >
158  void SgTEventMeta::keys( std::vector< std::string >& vkeys ) const {
159 
160  // Try initialising the object if it hasn't been done yet
161  if (!m_event || !m_event.load()) {
162  initialize().ignore();
163  }
164  if (m_event && m_event.load()) {
165  if (m_event.load()->metaKeys<T>(vkeys).isFailure()) {
166  std::ostringstream msg;
167  msg << META_ERROR_SOURCE
168  << "Failed to retrieve metadata keys for type "
169  << SG::normalizedTypeinfoName(typeid(T));
170  throw std::runtime_error(msg.str());
171  }
172  }
173  }
174 
175 } // namespace asg
176 
177 #endif // ASGTOOLS_SGTEVENTMETA_ICC