00001 #include "SIO/SIOCollectionHandler.h"
00002
00003 #include "SIO/LCSIO.h"
00004
00005 #include "EVENT/LCIO.h"
00006
00007
00008 #include "IOIMPL/LCCollectionIOVec.h"
00009
00010 #include "SIO/SIOHandlerMgr.h"
00011 #include "SIO/SIOObjectHandler.h"
00012
00013 #include "SIO_functions.h"
00014
00015 #include <iostream>
00016
00017 using namespace EVENT ;
00018 using namespace IOIMPL ;
00019
00020 namespace SIO {
00021
00022
00023 SIOCollectionHandler::SIOCollectionHandler(const std::string& name,
00024 const std::string& type,
00025 LCEventIOImpl** anEvtP)
00026 throw (Exception) :
00027 SIO_block( name.c_str() ),
00028 _evtP( anEvtP ) , _col(0) ,
00029 _myType( type ) {
00030
00031
00032 _myHandler = SIOHandlerMgr::instance()->getHandler( _myType ) ;
00033 if( ! _myHandler ){
00034
00035 throw Exception("SIOCollectionHandler: unsuported type") ;
00036 }
00037
00038 }
00039
00040 SIOCollectionHandler::~SIOCollectionHandler(){
00041
00042 }
00043
00044 void SIOCollectionHandler::setCollection(const LCCollection *col){
00045 _col = col ;
00046 }
00047 void SIOCollectionHandler::setEvent(LCEventIOImpl** anEvtP){
00048 _evtP = anEvtP ;
00049 }
00050
00051 unsigned int SIOCollectionHandler::xfer( SIO_stream* stream, SIO_operation op,
00052 unsigned int versionID){
00053
00054
00055 if( !_myHandler) {
00056 return LCIO::SUCCESS ;
00057 }
00058
00059 unsigned int status ;
00060
00061 if( op == SIO_OP_READ ){
00062
00063
00064
00065 LCCollectionIOVec* ioCol ;
00066
00067
00068
00069
00070 try{
00071 ioCol = dynamic_cast<LCCollectionIOVec*>( (*_evtP)->getCollection( getName()->c_str() )) ;
00072 }
00073 catch(DataNotAvailableException& e ){
00074
00075 std::string mess( e.what() ) ;
00076 mess += "\n The LCIO file seems to have a corrupted EventHeader that doesn't list"
00077 " all the collections in the event properly" ;
00078 throw Exception( mess ) ;
00079
00080
00081
00082
00083 }
00084
00085
00086 _myHandler->init( stream , SIO_OP_READ , ioCol , versionID ) ;
00087
00088 int nObj ;
00089 SIO_DATA( stream , &nObj , 1 ) ;
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 ioCol->resize( nObj ) ;
00102
00103 for( int i=0 ; i< nObj ; i ++ ){
00104
00105 status = _myHandler->readBase( stream , & (ioCol->operator[](i) ) ) ;
00106
00107 if( !( status & 1 ) ) return status ;
00108 }
00109
00110
00111 } else if( op == SIO_OP_WRITE ){
00112
00113 if( _col != 0 ){
00114
00115 _myHandler->init( stream , SIO_OP_WRITE , const_cast<LCCollection*>(_col) , version() ) ;
00116
00117
00118 int nObj = _col->getNumberOfElements() ;
00119
00120 SIO_DATA( stream, &nObj , 1 ) ;
00121
00122
00123 for( int i=0 ; i< nObj ; i ++ ){
00124
00125 const LCObject* obj = _col->getElementAt(i) ;
00126
00127
00128 status = _myHandler->writeBase( stream , obj ) ;
00129
00130 if( !( status & 1 ) ) return status ;
00131 }
00132
00133 }else{
00134 return 0 ;
00135 }
00136 }
00137
00138 return ( SIO_BLOCK_SUCCESS ) ;
00139 }
00140
00141 unsigned int SIOCollectionHandler::version(){
00142
00143 int version = SIO_VERSION_ENCODE( LCIO::MAJORVERSION, LCIO::MINORVERSION ) ;
00144
00145 return version ;
00146 }
00147
00148 }