00001 #include "SIO/SIOIndexHandler.h"
00002
00003
00004 #include "SIO/LCSIO.h"
00005 #include "SIO_functions.h"
00006
00007 #include "EVENT/LCIO.h"
00008
00009 #include <iostream>
00010 #include <limits.h>
00011
00012 namespace SIO {
00013
00014
00015 SIOIndexHandler::SIOIndexHandler(const std::string& name, LCIORandomAccessMgr* raMgr) :
00016 SIO_block( name.c_str() ),
00017 _raMgr( raMgr ) {
00018
00019 }
00020
00021 SIOIndexHandler::~SIOIndexHandler(){ }
00022
00023 unsigned int SIOIndexHandler::xfer( SIO_stream* stream, SIO_operation op,
00024 unsigned int versionID){
00025
00026 LCSIO::checkVersion(versionID) ;
00027
00028 unsigned int status ;
00029
00030
00031 if( op == SIO_OP_READ ){
00032
00033 int control ;
00034 int runMin ;
00035 long64 baseOffset ;
00036 int size ;
00037
00038 SIO_DATA( stream , &control, 1 ) ;
00039
00040 bool oneRun = control & 1 ;
00041 bool longOffset = control & 2 ;
00042
00043
00044 if( control & 4 ){
00045
00046 std::cerr << " WARNING: SIOIndexHandler: parameters not implemented .... " << std::endl ;
00047
00048 return SIO_BLOCK_SKIP ;
00049 }
00050
00051 SIO_DATA( stream , &runMin, 1 ) ;
00052 SIO_DATA( stream , &baseOffset, 1 ) ;
00053 SIO_DATA( stream , &size, 1 ) ;
00054
00055
00056
00057
00058
00059 int runNum ;
00060 int evtNum ;
00061 int runOffset ;
00062 long64 pos ;
00063
00064 int dummy_int;
00065 long64 dummy_long ;
00066
00067 for (int i = 0; i < size; i++) {
00068
00069 runNum = runMin ;
00070
00071 if( !oneRun ) {
00072 SIO_DATA( stream , &runOffset, 1 ) ;
00073 runNum += runOffset ;
00074 }
00075 SIO_DATA( stream , &evtNum , 1 ) ;
00076
00077
00078
00079
00080
00081
00082 if( longOffset ){
00083
00084 SIO_DATA( stream , &dummy_long , 1 ) ;
00085 pos = dummy_long ;
00086
00087 } else {
00088
00089 SIO_DATA( stream , &dummy_int , 1 ) ;
00090 pos = dummy_int ;
00091 }
00092
00093 pos += baseOffset ;
00094
00095
00096
00097
00098 _raMgr->_runEvtMap.add( RunEvent( runNum , evtNum ) , pos ) ;
00099 }
00100
00101
00102
00103
00104
00105
00106 } else if( op == SIO_OP_WRITE ){
00107
00108
00109
00110
00111
00112 unsigned control = 0 ;
00113
00114 RunEvent minEntry = _raMgr->_runEvtMap.minRunEvent() ;
00115 RunEvent maxEntry = _raMgr->_runEvtMap.maxRunEvent() ;
00116
00117 bool oneRun = minEntry.RunNum == maxEntry.RunNum ;
00118
00119 if( oneRun )
00120 control |= 1 ;
00121
00122 long64 posMin = _raMgr->_runEvtMap.getPosition( minEntry ) ;
00123 long64 posMax = _raMgr->_runEvtMap.getPosition( maxEntry ) ;
00124
00125 bool longOffset = ( posMax - posMin ) > INT_MAX ;
00126
00127 if( longOffset )
00128 control |= 2 ;
00129
00130
00131
00132
00133 SIO_DATA( stream , &control, 1 ) ;
00134
00135 int runMin = minEntry.RunNum ;
00136 SIO_DATA( stream , &runMin, 1 ) ;
00137
00138
00139 long64 baseOffset = posMin ;
00140 SIO_DATA( stream , &baseOffset, 1 ) ;
00141
00142
00143 int size = _raMgr->_runEvtMap.size() ;
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154 SIO_DATA( stream , &size, 1 ) ;
00155
00156 for( RunEventMap::Map_IT it = _raMgr->_runEvtMap.begin() ; it != _raMgr->_runEvtMap.end() ; ++it ){
00157
00158 const RunEvent& re = it->first ;
00159
00160 if( !oneRun ) {
00161
00162 int runOffset = re.RunNum - runMin ;
00163
00164 SIO_DATA( stream , &runOffset, 1 ) ;
00165 }
00166
00167 int evtNum = re.EvtNum ;
00168 SIO_DATA( stream , &evtNum , 1 ) ;
00169
00170
00171
00172
00173
00174
00175
00176 if( longOffset ){
00177
00178 long64 dummyL = it->second - baseOffset ;
00179 SIO_DATA( stream , &dummyL , 1 ) ;
00180
00181 } else {
00182
00183 int dummyI = it->second - baseOffset ;
00184 SIO_DATA( stream , &dummyI , 1 ) ;
00185 }
00186
00187 }
00188
00189 }
00190
00191 return ( SIO_BLOCK_SUCCESS ) ;
00192 }
00193
00194 unsigned int SIOIndexHandler::version(){
00195
00196 int version = SIO_VERSION_ENCODE( EVENT::LCIO::MAJORVERSION, EVENT::LCIO::MINORVERSION ) ;
00197 return version ;
00198
00199 }
00200
00201 }