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