/data3/calcul/jacquem/working_dir/Micromegas/micromegasFrameWork/lcio/src/cpp/src/SIO/SIOEventHandler.cc

Go to the documentation of this file.
00001 #include "SIO/SIOEventHandler.h"
00002 #include "SIO/LCSIO.h"
00003 
00004 #include "EVENT/LCEvent.h"
00005 #include "EVENT/LCIO.h"
00006 #include "IOIMPL/LCEventIOImpl.h"
00007 #include "IOIMPL/LCCollectionIOVec.h"
00008 // #include "IOIMPL/LCRelationIOImpl.h"
00009 #include "LCIOSTLTypes.h"
00010 #include "SIO/SIOLCParameters.h"
00011 
00012 #include "SIO_functions.h"
00013 #include <sstream> 
00014 #include <iostream>
00015 
00016 #define SUBSETPOSTFIX "_References"
00017 
00018 using namespace EVENT ;
00019 using namespace IOIMPL ;
00020 
00021 namespace SIO  {
00022 
00023 
00024   SIOEventHandler::SIOEventHandler(const std::string& name) : 
00025     SIO_block( name.c_str() ),
00026     _evtP(0) {
00027   }
00028 
00029   SIOEventHandler::SIOEventHandler(const std::string& name, LCEventIOImpl** anEvtP) : 
00030     SIO_block( name.c_str() ),
00031     _evtP( anEvtP ) {
00032  
00033     *_evtP = 0 ;
00034  }
00035 
00036 
00037   SIOEventHandler::~SIOEventHandler(){
00038     //     if (*_evtP )  delete *_evtP ; 
00039   }
00040 
00041   
00042   void SIOEventHandler::setEvent(const LCEvent* evt ){
00043     _evt = evt ;
00044   }
00045   void SIOEventHandler::setEventPtr(IOIMPL::LCEventIOImpl** evtP ){
00046     _evtP = evtP ;
00047   } 
00048 
00049 
00050   unsigned int SIOEventHandler::xfer( SIO_stream* stream, SIO_operation op, 
00051                                       unsigned int versionID){
00052   
00053     unsigned int status ; // needed by the SIO_DATA macro
00054   
00055 
00056     if( op == SIO_OP_READ ){ 
00057 
00058       LCSIO::checkVersion(versionID) ;
00059 
00060       // delete the old event object 
00061       // -> for every handler there will only be one event object at any given time      
00062       if (*_evtP ) {
00063 //      std::cout << " ---------- deleting " << *_evtP << "  at " << _evtP << std::endl ;
00064          delete *_evtP ;
00065       }
00066        *_evtP = new LCEventIOImpl ;
00067 //        std::cout << " ---------- created  " << *_evtP << "  at " << _evtP << std::endl ;
00068       
00069       SIO_DATA( stream ,  &(*_evtP)->_runNumber  , 1  ) ;
00070       SIO_DATA( stream ,  &(*_evtP)->_eventNumber  , 1  ) ;
00071       SIO_DATA( stream ,  &(*_evtP)->_timeStamp  , 1  ) ;
00072     
00073       char* detName ; 
00074       LCSIO_READ( stream,  &detName ) ; 
00075       (*_evtP)->setDetectorName( detName  )  ;
00076 
00077       // read collection types and names
00078       // not needed for the event record 
00079       // but SIO crashes if block is not read completely ...
00080       int nCol ;
00081       SIO_DATA( stream ,  &nCol , 1 ) ;
00082       for( int i=0; i<nCol ; i++ ){
00083         char* dummy ; 
00084         LCSIO_READ( stream,  &dummy ) ; 
00085         std::string colName( dummy ) ;
00086         char* type ;
00087         // read type 
00088         LCSIO_READ( stream,  &type ) ; 
00089         
00090         //  we have to attach a new collection or relation object to the event for every type in the header
00091 //       std::string typeStr( type ) ;
00092 //       if( typeStr.find( LCIO::LCRELATION) == 0  ){ // typeStr starts with LCRelation
00093 //         try{ (*_evtP)->addRelation( new LCRelationIOImpl() , colName) ; 
00094 //         }
00095 //         catch( EventException ){  return LCIO::ERROR ; }
00096 //       }else{
00097 
00098         std::string colType( type ) ;
00099 //      unsigned  idx ;
00100         std::string::size_type idx ;
00101         if( ( idx = colType.rfind( SUBSETPOSTFIX ) ) != std::string::npos ){
00102           colType = std::string( colType , 0 , idx ) ;
00103         }
00104 
00105         try { 
00106           
00107           (*_evtP)->addCollection( new LCCollectionIOVec( colType ) , colName) ; 
00108           
00109         }
00110         catch( EventException ){  return LCIO::ERROR ; }
00111 
00112       }
00113 
00114       // read parameters
00115       if( versionID > SIO_VERSION_ENCODE( 1, 1)   ) 
00116         SIOLCParameters::read( stream ,  (*_evtP)->parameters() , versionID) ;
00117       
00118 
00119     }  else if( op == SIO_OP_WRITE ){ 
00120     
00121       if( _evt ){
00122         LCSIO_WRITE( stream, _evt->getRunNumber() ) ; 
00123         LCSIO_WRITE( stream, _evt->getEventNumber() ) ;
00124         LCSIO_WRITE( stream, _evt->getTimeStamp()  ) ;
00125         LCSIO_WRITE( stream, _evt->getDetectorName() ) ;
00126       
00127         // now write a list of colection types and names
00128       
00129 //      const std::vector<std::string>* strVec = _evt->getCollectionNames() ;
00130 //      int nCol = strVec->size() ;
00131 //      SIO_DATA( stream, &nCol, 1 ) ;
00132       
00133 //      for( std::vector<std::string>::const_iterator name = strVec->begin() ; name != strVec->end() ; name++){
00134         
00135 //        const LCCollection* col = _evt->getCollection( *name ) ;
00136 //        LCSIO_WRITE( stream, *name ) ;
00137 //        LCSIO_WRITE( stream, col->getTypeName() ) ;
00138                 
00139 //      } 
00140 
00141 //      const std::vector<std::string>* strVec = _evt->getCollectionNames() ;
00142         const StringVec* colNames =   _evt->getCollectionNames() ;
00143 //      const StringVec* relNames =   _evt->getRelationNames() ;
00144         
00145         int nCol = colNames->size()  ; //+ relNames->size()  ;
00146 
00147         for(unsigned int i=0 ; i < colNames->size() ; i++ ) {   
00148           if( _evt->getCollection( (*colNames)[i] )->isTransient() ) nCol-- ;
00149         }
00150 
00151         SIO_DATA( stream, &nCol, 1 ) ;
00152       
00153         //      for( std::vector<std::string>::const_iterator name = strVec->begin() ; name != strVec->end() ; name++){
00154         for(unsigned int i=0 ; i < colNames->size() ; i++ ) {   
00155           const LCCollection* col = _evt->getCollection( (*colNames)[i] ) ;
00156           if( ! col->isTransient() ){
00157             LCSIO_WRITE( stream, (*colNames)[i] ) ;
00158 
00159             std::string colType( col->getTypeName() ) ;
00160 
00161             if( col->isSubset() ) 
00162               colType += SUBSETPOSTFIX ;
00163 
00164             LCSIO_WRITE( stream,  colType ) ;
00165           }
00166         } 
00167 //      for(unsigned int i=0 ; i < relNames->size() ; i++ ) {   
00168 //        const LCRelation* rel = _evt->getRelation( (*relNames)[i] ) ;
00169 //        std::stringstream  relTypeName  ;
00170 //        relTypeName <<  LCIO::LCRELATION ; 
00171 //        // << "_" <<   rel->getFromType() << "_"  << rel->getToType() << std::ends ;
00172 //        LCSIO_WRITE( stream, (*relNames)[i] ) ;
00173 //        LCSIO_WRITE( stream, relTypeName.str() ) ;
00174 //      } 
00175         
00176         // write parameters
00177         if( version() > SIO_VERSION_ENCODE( 1, 1) ) 
00178           SIOLCParameters::write( stream ,  _evt->getParameters() ) ;
00179 
00180       } else {
00181         return 0 ;
00182       }
00183     }
00184   
00185   
00186     return ( SIO_BLOCK_SUCCESS ) ;
00187   }
00188 
00189   unsigned int   SIOEventHandler::version(){
00190 
00191     int version = SIO_VERSION_ENCODE( LCIO::MAJORVERSION, LCIO::MINORVERSION ) ;
00192     return version ;
00193 
00194   }
00195 
00196 }

Generated on Mon Jan 7 13:15:21 2013 for MicromegasFramework by  doxygen 1.4.7