![]() |
|
|
Generated: 8 Jan 2009 |
00001 // $Id: GaudiCommonImp.h,v 1.11 2008/10/10 08:06:33 marcocle Exp $ 00002 // ============================================================================ 00003 #ifndef GAUDIALG_GAUDICOMMONIMP_H 00004 #define GAUDIALG_GAUDICOMMONIMP_H 1 00005 // ============================================================================ 00006 // Include files 00007 // ============================================================================ 00008 // GaudiAlg 00009 // ============================================================================ 00010 #include "GaudiAlg/GetData.h" 00011 #include "GaudiAlg/GaudiCommon.h" 00012 // ============================================================================ 00020 // ============================================================================ 00021 // Returns the full correct event location given the rootInTes settings 00022 // ============================================================================ 00023 template < class PBASE > 00024 inline const std::string 00025 GaudiCommon<PBASE>::fullTESLocation( const std::string & location, 00026 const bool useRootInTES ) const 00027 { 00028 // The logic is: 00029 // if no R.I.T., give back location 00030 // if R.I.T., this is the mapping: 00031 // (note that R.I.T. contains a trailing '/') 00032 // location -> result 00033 // ------------------------------------------------- 00034 // "" -> R.I.T.[:-1] ("rit") 00035 // "/Event" -> R.I.T.[:-1] ("rit") 00036 // "/Event/MyObj" -> R.I.T. + "MyObj" ("rit/MyObj") 00037 // "MyObj" -> R.I.T. + "MyObj" ("rit/MyObj") 00038 return ( !useRootInTES || rootInTES().empty() ? 00039 location 00040 : 00041 location.empty() || ( location == "/Event" ) ? 00042 rootInTES().substr(0,rootInTES().size()-1) 00043 : 00044 0 == location.find("/Event/") ? 00045 rootInTES() + location.substr(7) 00046 : 00047 rootInTES() + location ); 00048 } 00049 // ============================================================================ 00050 // Templated access to the data in Gaudi Transient Store 00051 // ============================================================================ 00052 template < class PBASE > 00053 template < class TYPE > 00054 inline typename Gaudi::Utils::GetData<TYPE>::return_type 00055 GaudiCommon<PBASE>::get( IDataProviderSvc* service , 00056 const std::string& location, 00057 const bool useRootInTES ) const 00058 { 00059 // check the environment 00060 Assert( 0 != service , "get():: IDataProvider* points to NULL!" ) ; 00061 // get the helper object: 00062 Gaudi::Utils::GetData<TYPE> helper ; 00063 return helper ( *this , service , 00064 fullTESLocation ( location , useRootInTES ) ) ; 00065 } 00066 // ============================================================================ 00067 // check the existence of objects in Gaudi Transient Store 00068 // ============================================================================ 00069 template < class PBASE > 00070 template < class TYPE > 00071 inline bool GaudiCommon<PBASE>::exist( IDataProviderSvc* service , 00072 const std::string& location , 00073 const bool useRootInTES ) const 00074 { 00075 // check the environment 00076 Assert( 0 != service , "exist():: IDataProvider* points to NULL!" ) ; 00077 // check the data object 00078 const std::string & fullLoc = fullTESLocation( location, useRootInTES ); 00079 SmartDataPtr<TYPE> obj( service , fullLoc ) ; 00080 return obj ? true : false ; 00081 } 00082 // ============================================================================ 00083 00084 // ============================================================================ 00085 // get the existing object from Gaudi Event Transient store 00086 // or create new object register in in TES and return if object 00087 // does not exist 00088 // ============================================================================ 00089 template <class PBASE> 00090 template <class TYPE,class TYPE2> 00091 inline TYPE* GaudiCommon<PBASE>::getOrCreate( IDataProviderSvc* svc , 00092 const std::string& location , 00093 const bool useRootInTES ) const 00094 { 00095 // check the environment 00096 Assert( 0 != svc , "getOrCreate():: svc points to NULL!" ) ; 00097 const std::string & fullLoc = fullTESLocation( location, useRootInTES ); 00098 SmartDataPtr<TYPE> obj ( svc , fullLoc ) ; 00099 if ( !obj ) 00100 { 00101 TYPE2* o = new TYPE2() ; 00102 put ( svc , o , location ) ; // do not use fullLoc here as put recreates it 00103 return o ; 00104 } 00105 TYPE* aux = obj ; 00106 if( !aux ) 00107 { Exception ( "getOrCreate():: No valid data at '" + fullLoc + "'" ) ; } 00108 // return located *VALID* data 00109 return aux ; 00110 } 00111 // ============================================================================ 00112 00113 // ============================================================================ 00114 // the useful method for location of tools. 00115 // ============================================================================ 00116 template < class PBASE > 00117 template < class TOOL > 00118 inline TOOL* GaudiCommon<PBASE>::tool( const std::string& type , 00119 const std::string& name , 00120 const IInterface* parent , 00121 bool create ) const 00122 { 00123 TOOL* Tool = 0 ; 00124 // for empty names delegate to another method 00125 if ( name.empty() ) 00126 { 00127 Tool = tool<TOOL>( type , parent , create ) ; 00128 } 00129 else 00130 { 00131 Assert( 0 != this->toolSvc() , "tool():: IToolSvc* points to NULL!" ) ; 00132 // get the tool from Tool Service 00133 const StatusCode sc = 00134 this->toolSvc()->retrieveTool ( type , name , Tool , parent , create ) ; 00135 if ( sc.isFailure() ) 00136 { Exception("tool():: Could not retrieve Tool '" + type + "'/'" + name + "'", sc ) ; } 00137 if ( 0 == Tool ) 00138 { Exception("tool():: Could not retrieve Tool '" + type + "'/'" + name + "'" ) ; } 00139 // add the tool into list of known tools to be properly released 00140 addToToolList( Tool ); 00141 } 00142 // return *VALID* located tool 00143 return Tool ; 00144 } 00145 // ============================================================================ 00146 00147 // ============================================================================ 00148 // the useful method for location of tools. 00149 // ============================================================================ 00150 template < class PBASE > 00151 template < class TOOL > 00152 inline TOOL* GaudiCommon<PBASE>::tool( const std::string& type , 00153 const IInterface* parent , 00154 bool create ) const 00155 { 00156 // check the environment 00157 Assert ( 0 != PBASE::toolSvc() , "IToolSvc* points to NULL!" ); 00158 // retrieve the tool from Tool Service 00159 TOOL* Tool = 0 ; 00160 const StatusCode sc = 00161 this->toolSvc() -> retrieveTool ( type, Tool, parent , create ); 00162 if ( sc.isFailure() ) 00163 { Exception("tool():: Could not retrieve Tool '" + type + "'", sc ) ; } 00164 if ( 0 == Tool ) 00165 { Exception("tool():: Could not retrieve Tool '" + type + "'" ) ; } 00166 // add the tool into the list of known tools to be properly released 00167 addToToolList( Tool ); 00168 // return *VALID* located tool 00169 return Tool ; 00170 } 00171 // ============================================================================ 00172 00173 // ============================================================================ 00174 // the useful method for location of services 00175 // ============================================================================ 00176 template < class PBASE > 00177 template < class SERVICE > 00178 inline SERVICE* GaudiCommon<PBASE>::svc( const std::string& name , 00179 const bool create ) const 00180 { 00181 SERVICE* s = 0 ; 00182 Assert ( 0 != this->svcLoc() , "ISvcLocator* points to NULL!" ); 00183 // retrieve the service using templated method Algorithm::service 00184 StatusCode sc = this->svcLoc() -> service( name , s , create ) ; 00185 // check the results 00186 if ( sc.isFailure() ) 00187 { Exception ( "svc():: Could not retrieve Svc '" + name + "'", sc ) ; } 00188 if ( 0 == s ) 00189 { Exception ( "svc():: Could not retrieve Svc '" + name + "'" ) ; } 00190 // add the tool into list of known tools, to be properly released 00191 addToServiceList( s, name ) ; 00192 // return *VALID* located service 00193 return s ; 00194 } 00195 // ============================================================================ 00196 00197 // ============================================================================ 00198 // Short-cut to get a pointer to the UpdateManagerSvc 00199 // ============================================================================ 00200 template <class PBASE> 00201 inline IUpdateManagerSvc * 00202 GaudiCommon<PBASE>::updMgrSvc() const 00203 { 00204 if ( !m_updMgrSvc ) 00205 { m_updMgrSvc = svc<IUpdateManagerSvc>("UpdateManagerSvc",true); } 00206 return m_updMgrSvc ; 00207 } 00208 // ============================================================================ 00209 00210 // ============================================================================ 00211 // Short-cut to get a pointer to the 00212 // ============================================================================ 00213 template <class PBASE> 00214 inline IDataProviderSvc * 00215 GaudiCommon<PBASE>::fastContainersSvc() const 00216 { 00217 if ( !m_fastContainersSvc ) 00218 { m_fastContainersSvc = svc<IDataProviderSvc>("FastContainersSvc",true); } 00219 return m_fastContainersSvc ; 00220 } 00221 00222 // ============================================================================ 00223 // Get a fast container 00224 // ============================================================================ 00225 template < class PBASE > 00226 template < class T > 00227 inline TransientFastContainer<T> * 00228 GaudiCommon<PBASE>::getFastContainer( const std::string &location, 00229 typename TransientFastContainer<T>::size_type initial ) 00230 { 00231 typedef TransientFastContainer<T> container_type; 00232 00233 IDataProviderSvc* svc = fastContainersSvc(); 00234 Assert( 0 != svc , "getFastContainer(): cannot get FastContainersSvc" ); 00235 00236 container_type *ptr = NULL; 00237 SmartDataPtr<container_type> obj( svc, location ); 00238 if (!obj){ 00239 ptr = new container_type(initial); 00240 StatusCode status = svc->registerObject(location,ptr); 00241 if ( !status.isSuccess() ){ 00242 Exception("getFastContainer(): cannot register '" + 00243 System::typeinfoName( typeid( *ptr ) ) + 00244 "' at address '" + location + "'" , status ); 00245 } 00246 } else { 00247 ptr = obj; 00248 if ( !ptr ){ 00249 Exception("getFastContainer(): No valid container at '" + location + "'"); 00250 } 00251 } 00252 00253 return ptr; 00254 } 00255 // ============================================================================ 00256 00257 // ============================================================================ 00258 // predefined configurable message stream for the effective printouts 00259 // ============================================================================ 00260 template <class PBASE> 00261 inline MsgStream& 00262 GaudiCommon<PBASE>::msgStream ( const MSG::Level level ) const 00263 { 00264 if ( !m_msgStream ) 00265 { m_msgStream = new MsgStream ( PBASE::msgSvc() , this->name() ) ; } 00266 return *m_msgStream << level ; 00267 } 00268 // ============================================================================ 00269 00270 // ============================================================================ 00271 // Assertion - throw exception, if condition is not fulfilled 00272 // ============================================================================ 00273 template <class PBASE> 00274 inline StatusCode GaudiCommon<PBASE>::Assert( const bool OK , 00275 const std::string& msg , 00276 const StatusCode sc ) const 00277 { 00278 return ( OK ? StatusCode(StatusCode::SUCCESS, true) : Exception( msg , sc ) ) ; 00279 } 00280 // ============================================================================ 00281 00282 // ============================================================================ 00283 // Delete the current message stream object 00284 // ============================================================================ 00285 template <class PBASE> 00286 inline void GaudiCommon<PBASE>::resetMsgStream() const 00287 { 00288 if ( 0 != m_msgStream ) { delete m_msgStream; m_msgStream = 0; } 00289 } 00290 // ============================================================================ 00291 00292 // ============================================================================ 00293 // Assertion - throw exception, if condition is not fulfilled 00294 // ============================================================================ 00295 template <class PBASE> 00296 inline StatusCode 00297 GaudiCommon<PBASE>::Assert( const bool OK , 00298 const char* msg , 00299 const StatusCode sc ) const 00300 { 00301 return OK ? StatusCode(StatusCode::SUCCESS, true) : Exception( msg , sc ) ; 00302 } 00303 // ============================================================================ 00304 00305 // ============================================================================ 00319 // ============================================================================ 00320 #define ALG_ERROR( message , code ) \ 00321 ( Error( message + \ 00322 std::string ( " [ at line " ) + \ 00323 GaudiAlg::fileLine ( __LINE__ ) + \ 00324 std::string ( " in file '" ) + \ 00325 std::string ( __FILE__ ) + "']" , code ) ) 00326 00327 // ============================================================================ 00328 00329 #endif // GAUDIALG_GAUDICOMMONIMP_H