00001
00002 #ifndef ATLFAST_TESIO_H
00003 #define ATLFAST_TESIO_H
00004
00005
00006 #include <vector>
00007 #include <string>
00008
00009
00010
00011 #include "AtlfastCode/MCparticleCollection.h"
00012
00013
00014
00015 #include "GeneratorObjects/McEventCollection.h"
00016
00017 #// Gaudi includes
00018
00019
00020
00021 #include "GaudiKernel/Bootstrap.h"
00022 #include "GaudiKernel/ISvcLocator.h"
00023 #include "GaudiKernel/DataObject.h"
00024 #include "GaudiKernel/ObjectVector.h"
00025
00026 #include "StoreGate/StoreGateSvc.h"
00027 #include "StoreGate/DataHandle.h"
00028
00029 namespace HepMC_helper{
00030 class IMCselector;
00031 }
00032
00033 namespace Atlfast {
00034 class TesIoStat{
00035 public:
00036 TesIoStat(): m_status(StatusCode::SUCCESS),m_errMsg(""){}
00037 TesIoStat(StatusCode sc, std::string string): m_status(sc),
00038 m_errMsg(string){}
00039 TesIoStat(StatusCode s): m_status(s), m_errMsg(""){}
00040
00041 StatusCode status() const {return m_status;}
00042 std::string message() const {return m_errMsg;}
00043
00044 bool isValid() const {return m_status.isSuccess();}
00045 bool isNotValid() const {return !isValid();}
00046 bool operator !() const {return isNotValid();}
00047 operator bool() const {return isValid();}
00048 operator StatusCode() const {return m_status;}
00049 private:
00050 StatusCode m_status;
00051 std::string m_errMsg;
00052 };
00053
00054 class TesIO {
00055 public:
00056 typedef std::string String;
00057
00058 TesIO();
00059 TesIoStat getMC (MCparticleCollection&) const;
00060 TesIoStat getMC (MCparticleCollection&,
00061 const HepMC_helper::IMCselector* ) const;
00062
00063 template<class T> TesIoStat copy (std::vector<T*>&) const;
00064 template<class T> TesIoStat copy (std::vector<T*>&, String key) const;
00065 template<class T> TesIoStat getDH (const DataHandle<T>&) const;
00066 template<class T> TesIoStat getDH (const DataHandle<T>&, String key) const;
00067 template<class T> TesIoStat store (T*) const;
00068 template<class T> TesIoStat store (T*, String key) const;
00069
00070 private:
00071
00072 StoreGateSvc* m_sgSvc;
00073 };
00074
00075
00076
00077
00078
00079 inline TesIO::TesIO():m_sgSvc(0){
00080 ISvcLocator* svcLoc = Gaudi::svcLocator( );
00081 if( (svcLoc->service( "StoreGateSvc", m_sgSvc )).isFailure() ){
00082 throw TesIoStat(StatusCode::FAILURE, "TesIO could not find StoreGate");
00083 }
00084 }
00085
00086
00087
00088 template<class T> inline
00089 TesIoStat TesIO::copy(std::vector<T*>& localCopy) const {
00090 const DataHandle<ObjectVector<T> > objects;
00091 TesIoStat stat = this->getDH(objects);
00092 if(!stat) {
00093 return TesIoStat(StatusCode::FAILURE,
00094 "Could not copy collection: "+stat.message());
00095 }
00096
00097 std::copy(objects->begin(),objects->end(),back_inserter(localCopy));
00098 return TesIoStat(StatusCode::SUCCESS, "Copied Collection from TDS");
00099
00100 }
00101
00102
00103
00104 template<class T> inline
00105 TesIoStat TesIO::copy(std::vector<T*>& localCopy, String key) const {
00106 const DataHandle<ObjectVector<T> > objects;
00107 TesIoStat stat = this->getDH(objects, key);
00108 if(!stat){
00109 return TesIoStat(StatusCode::FAILURE,
00110 "Could not copy keyed collection: "+stat.message());
00111 }
00112
00113 std::copy(objects->begin(),objects->end(),back_inserter(localCopy));
00114 return TesIoStat(StatusCode::SUCCESS, "Copied Collection from TDS");
00115
00116 }
00117
00118
00119
00120 template<class T> inline
00121 TesIoStat TesIO::getDH(const DataHandle<T>& dh) const {
00122 if((m_sgSvc->retrieve(dh)).isFailure() ){
00123 return TesIoStat(StatusCode::FAILURE,
00124 "Could not find a valid DataHandle");
00125 }
00126 return TesIoStat(StatusCode::SUCCESS, "Found unkeyed DH in TDS");
00127 }
00128
00129
00130
00131 template<class T> inline
00132 TesIoStat TesIO::getDH(const DataHandle<T>& dh, String key) const {
00133 if( (m_sgSvc->retrieve(dh, key)).isFailure() ){
00134 return TesIoStat(StatusCode::FAILURE,
00135 "Could not find a valid keyed DataHandle");
00136 }
00137 return TesIoStat(StatusCode::SUCCESS, "Found keyed DH in TDS");
00138 }
00139
00140
00141
00142 template<class T> inline TesIoStat TesIO::store(T* pObject) const {
00143 return (TesIoStat(m_sgSvc->record(pObject)) ) ;
00144 }
00145
00146
00147
00148 template<class T> inline
00149 TesIoStat TesIO::store(T* storee, String key) const {
00150 return (TesIoStat(m_sgSvc->record(storee, key)) ) ;
00151 }
00152
00153 }
00154 #endif
00155
00156
00157
00158
00159
00160
00161
00162
00163