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