The Gaudi Framework  v29r0 (ff2e7097)
GaudiCommonImp.h
Go to the documentation of this file.
1 #ifndef GAUDIALG_GAUDICOMMONIMP_H
2 #define GAUDIALG_GAUDICOMMONIMP_H 1
3 // ============================================================================
4 // Include files
5 // ============================================================================
6 #include <algorithm>
7 // ============================================================================
8 // GaudiAlg
9 // ============================================================================
10 #include "GaudiAlg/GaudiCommon.h"
11 #include "GaudiAlg/GetData.h"
12 // ============================================================================
20 // ============================================================================
21 // Returns the full correct event location given the rootInTes settings
22 // ============================================================================
23 template <class PBASE>
25  const bool useRootInTES ) const
26 {
27  // The logic is:
28  // if no R.I.T., give back location
29  // if R.I.T., this is the mapping:
30  // (note that R.I.T. contains a trailing '/')
31  // location -> result
32  // -------------------------------------------------
33  // "" -> R.I.T.[:-1] ("rit")
34  // "/Event" -> R.I.T.[:-1] ("rit")
35  // "/Event/MyObj" -> R.I.T. + "MyObj" ("rit/MyObj")
36  // "MyObj" -> R.I.T. + "MyObj" ("rit/MyObj")
37  return ( !useRootInTES || rootInTES().empty()
38  ? location
39  : location.empty() || ( location == "/Event" )
40  ? rootInTES().substr( 0, rootInTES().size() - 1 )
41  : 0 == location.find( "/Event/" )
42  ? rootInTES() + location.substr( 7 )
43  : location[0] == '/' ? rootInTES() + location.substr( 1 ) : rootInTES() + location );
44 }
45 // ============================================================================
46 // Templated access to the data in Gaudi Transient Store
47 // ============================================================================
48 template <class PBASE>
49 template <class TYPE>
51 GaudiCommon<PBASE>::get( IDataProviderSvc* service, const std::string& location, const bool useRootInTES ) const
52 {
53  // check the environment
54  Assert( service, "get():: IDataProvider* points to NULL!" );
55  // get the helper object:
57  return getter( *this, service, fullTESLocation( location, useRootInTES ) );
58 }
59 // ============================================================================
60 // Templated access to the data in Gaudi Transient Store, no check on the content
61 // ============================================================================
62 template <class PBASE>
63 template <class TYPE>
65 GaudiCommon<PBASE>::getIfExists( IDataProviderSvc* service, const std::string& location, const bool useRootInTES ) const
66 {
67  // check the environment
68  Assert( service, "get():: IDataProvider* points to NULL!" );
69  // get the helper object:
71  return getter( *this, service, fullTESLocation( location, useRootInTES ), false );
72 }
73 // ============================================================================
74 // check the existence of objects in Gaudi Transient Store
75 // ============================================================================
76 template <class PBASE>
77 template <class TYPE>
79  const bool useRootInTES ) const
80 {
81  // check the environment
82  Assert( service, "exist():: IDataProvider* points to NULL!" );
83  // check the data object
85  return checker( service, fullTESLocation( location, useRootInTES ) );
86 }
87 // ============================================================================
88 // get the existing object from Gaudi Event Transient store
89 // or create new object register in in TES and return if object
90 // does not exist
91 // ============================================================================
92 template <class PBASE>
93 template <class TYPE, class TYPE2>
95 GaudiCommon<PBASE>::getOrCreate( IDataProviderSvc* service, const std::string& location, const bool useRootInTES ) const
96 {
97  // check the environment
98  Assert( service, "getOrCreate():: svc points to NULL!" );
99  // get the helper object
101  return getter( *this, service, fullTESLocation( location, useRootInTES ), location );
102 }
103 // ============================================================================
104 // the useful method for location of tools.
105 // ============================================================================
106 template <class PBASE>
107 template <class TOOL>
108 inline TOOL* GaudiCommon<PBASE>::tool( const std::string& type, const std::string& name, const IInterface* parent,
109  bool create ) const
110 {
111  // for empty names delegate to another method
112  if ( name.empty() ) return tool<TOOL>( type, parent, create );
113  Assert( this->toolSvc(), "tool():: IToolSvc* points to NULL!" );
114  // get the tool from Tool Service
115  TOOL* Tool = nullptr;
116  const StatusCode sc = this->toolSvc()->retrieveTool( type, name, Tool, parent, create );
117  if ( sc.isFailure() ) {
118  Exception( "tool():: Could not retrieve Tool '" + type + "'/'" + name + "'", sc );
119  }
120  if ( !Tool ) {
121  Exception( "tool():: Could not retrieve Tool '" + type + "'/'" + name + "'" );
122  }
123  // insert tool into list of tools
124  PBASE::registerTool( Tool );
125  m_managedTools.push_back( Tool );
126  // return *VALID* located tool
127  return Tool;
128 }
129 // ============================================================================
130 // the useful method for location of tools.
131 // ============================================================================
132 template <class PBASE>
133 template <class TOOL>
134 inline TOOL* GaudiCommon<PBASE>::tool( const std::string& type, const IInterface* parent, bool create ) const
135 {
136  // check the environment
137  Assert( PBASE::toolSvc(), "IToolSvc* points to NULL!" );
138  // retrieve the tool from Tool Service
139  TOOL* Tool = nullptr;
140  const StatusCode sc = this->toolSvc()->retrieveTool( type, Tool, parent, create );
141  if ( sc.isFailure() ) {
142  Exception( "tool():: Could not retrieve Tool '" + type + "'", sc );
143  }
144  if ( !Tool ) {
145  Exception( "tool():: Could not retrieve Tool '" + type + "'" );
146  }
147  // add the tool into the list of known tools to be properly released
148  PBASE::registerTool( Tool );
149  m_managedTools.push_back( Tool );
150  // return *VALID* located tool
151  return Tool;
152 }
153 // ============================================================================
154 // the useful method for location of services
155 // ============================================================================
156 template <class PBASE>
157 template <class SERVICE>
158 inline SmartIF<SERVICE> GaudiCommon<PBASE>::svc( const std::string& name, const bool create ) const
159 {
160  Assert( this->svcLoc(), "ISvcLocator* points to NULL!" );
162  // check if we already have this service
163  auto it = std::lower_bound( std::begin( m_services ), std::end( m_services ), name, GaudiCommon_details::svc_lt );
164  if ( it != std::end( m_services ) && GaudiCommon_details::svc_eq( *it, name ) ) {
165  // Try to get the requested interface
166  s = *it;
167  // check the results
168  if ( !s ) {
169  Exception( "svc():: Could not retrieve Svc '" + name + "'", StatusCode::FAILURE );
170  }
171  } else {
172  auto baseSvc = this->svcLoc()->service( name, create );
173  // Try to get the requested interface
174  s = baseSvc;
175  // check the results
176  if ( !baseSvc || !s ) {
177  Exception( "svc():: Could not retrieve Svc '" + name + "'", StatusCode::FAILURE );
178  }
179  // add the tool into list of known tools, to be properly released
180  addToServiceList( baseSvc );
181  }
182  // return *VALID* located service
183  return s;
184 }
185 // ============================================================================
186 // Short-cut to get a pointer to the UpdateManagerSvc
187 // ============================================================================
188 template <class PBASE>
190 {
191  if ( !m_updMgrSvc ) {
192  m_updMgrSvc = svc<IUpdateManagerSvc>( "UpdateManagerSvc", true );
193  }
194  return m_updMgrSvc;
195 }
196 // ============================================================================
197 // Assertion - throw exception, if condition is not fulfilled
198 // ============================================================================
199 template <class PBASE>
200 inline void GaudiCommon<PBASE>::Assert( const bool ok, const std::string& msg, const StatusCode sc ) const
201 {
202  if ( !ok ) Exception( msg, sc );
203 }
204 // ============================================================================
205 // Assertion - throw exception, if condition is not fulfilled
206 // ============================================================================
207 template <class PBASE>
208 inline void GaudiCommon<PBASE>::Assert( const bool ok, const char* msg, const StatusCode sc ) const
209 {
210  if ( !ok ) Exception( msg, sc );
211 }
212 // ============================================================================
226 // ============================================================================
227 #define ALG_ERROR( message, code ) \
228  ( Error( message + std::string( " [ at line " ) + std::to_string( __LINE__ ) + std::string( " in file '" ) + \
229  std::string( __FILE__ ) + "']", \
230  code ) )
231 
232 // ============================================================================
233 // The END
234 // ============================================================================
235 #endif // GAUDIALG_GAUDICOMMONIMP_H
Small smart pointer class with automatic reference counting for IInterface.
Definition: IConverter.h:15
T empty(T...args)
IUpdateManagerSvc * updMgrSvc() const
Short-cut to locate the Update Manager Service.
constexpr const struct GaudiCommon_details::svc_eq_t svc_eq
Gaudi::Utils::GetData< TYPE >::return_type getOrCreate(IDataProviderSvc *svc, const std::string &location, const bool useRootInTES=true) const
Get the existing data object from Gaudi Event Transient store.
Helper structure for implementation of "get"-functions for GaudiCommon<BASE>
Definition: GaudiCommon.h:41
bool exist(IDataProviderSvc *svc, const std::string &location, const bool useRootInTES=true) const
Check the existence of a data object or container in the Gaudi Transient Event Store.
T end(T...args)
Data provider interface definition.
T lower_bound(T...args)
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
STL class.
constexpr const struct GaudiCommon_details::svc_lt_t svc_lt
Helper structure for implementation of "exists"-functions for GaudiCommon<BASE>
Definition: GetData.h:337
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
Definition of the basic interface.
Definition: IInterface.h:277
Helper structure for implementation of "getOrCreate"-functions for GaudiCommon<BASE> ...
Definition: GetData.h:420
Interface class to the Update Manager service.
Gaudi::Utils::GetData< TYPE >::return_type get(IDataProviderSvc *svc, const std::string &location, const bool useRootInTES=true) const
Templated access to the data in Gaudi Transient Store.
T find(T...args)
T begin(T...args)
void Assert(const bool ok, const std::string &message="", const StatusCode sc=StatusCode(StatusCode::FAILURE, true)) const
Assertion - throw exception if the given condition is not fulfilled.
string s
Definition: gaudirun.py:253
T substr(T...args)
const std::string fullTESLocation(const std::string &location, const bool useRootInTES) const
Returns the full correct event location given the rootInTes settings.
Gaudi::Utils::GetData< TYPE >::return_type getIfExists(IDataProviderSvc *svc, const std::string &location, const bool useRootInTES=true) const
Quicker version of the get function which bypasses the check on the retrieved data.
TOOL * tool(const std::string &type, const std::string &name, const IInterface *parent=0, bool create=true) const
Useful method for the easy location of tools.
SmartIF< SERVICE > svc(const std::string &name, const bool create=true) const
A useful method for the easy location of services.