2 Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
5 #ifndef ASGTOOLS_SGTEVENTMETA_ICC
6 #define ASGTOOLS_SGTEVENTMETA_ICC
14 #ifdef XAOD_STANDALONE
15 # include "xAODRootAccess/TEvent.h"
16 #endif // XAOD_STANDALONE
20 /// A constant used in the error message printouts
21 static const char* META_ERROR_SOURCE = "asg::SgTEventMeta ERROR ";
23 template< typename T >
24 bool SgTEventMeta::contains( const std::string& name ) {
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"
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 );
39 std::cout << META_ERROR_SOURCE << "Unknown store type (" << m_type
40 << ") defined" << std::endl;
45 template< typename T >
46 bool SgTEventMeta::transientContains( const std::string& name ) const {
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"
56 return m_event.load()->template transientContainsMeta< T >( name );
59 template< typename T >
60 StatusCode SgTEventMeta::retrieve( T*& obj, const std::string& name ) {
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"
66 return StatusCode::FAILURE;
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"
74 return StatusCode::FAILURE;
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;
85 return StatusCode::SUCCESS;
88 template< typename T >
89 StatusCode SgTEventMeta::retrieve( const T*& obj, const std::string& name ) {
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"
95 return StatusCode::FAILURE;
98 // Decide which function to call:
99 if( m_type == InputStore ) {
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;
106 } else if( m_type == OutputStore ) {
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;
114 std::cout << META_ERROR_SOURCE << "Unknown store type (" << m_type
115 << ") defined" << std::endl;
116 return StatusCode::FAILURE;
120 return StatusCode::SUCCESS;
123 template< typename T >
124 StatusCode SgTEventMeta::record( std::unique_ptr<T> obj, const std::string& name ) {
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"
130 return StatusCode::FAILURE;
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;
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;
148 return StatusCode::SUCCESS;
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 );
157 template< typename T >
158 void SgTEventMeta::keys( std::vector< std::string >& vkeys ) const {
160 // Try initialising the object if it hasn't been done yet
161 if (!m_event || !m_event.load()) {
162 initialize().ignore();
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());
177 #endif // ASGTOOLS_SGTEVENTMETA_ICC