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

Go to the documentation of this file.
00001 #include "SIO/SIOCollectionHandler.h"
00002 
00003 #include "SIO/LCSIO.h"
00004 
00005 #include "EVENT/LCIO.h"
00006 // #include "SIO/SIOLCParameters.h"
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     // here we need to get the handler for our type
00032     _myHandler = SIOHandlerMgr::instance()->getHandler( _myType  ) ;
00033     if( ! _myHandler ){
00034       // throw an exception that is caught in the SIOReader 
00035       throw Exception("SIOCollectionHandler: unsuported type") ;
00036     }
00037     //    std::cout << " creating SIOCollectionHandler " << _myType << std::endl ;
00038   }
00039 
00040   SIOCollectionHandler::~SIOCollectionHandler(){
00041 //     std::cout << " deleting SIOCollectionHandler " << _myType << std::endl ;
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     // if we don't have a handler we don't do anything ...
00055     if( !_myHandler) {
00056       return LCIO::SUCCESS ;
00057     }
00058     
00059     unsigned int status ; // needed by the SIO_DATA macro
00060     
00061     if( op == SIO_OP_READ ){ 
00062       
00063       // get address of this  handlers collection in the event
00064       
00065       LCCollectionIOVec* ioCol ;
00066 
00067 //       bool colIsInEvent = true ;
00068 
00069       // get the collection from event that has been attached by SIOEventHandler
00070       try{   // can safely cast - we know we have an LCEventImpl that has LCCollectionIOVecs
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                 // return LCIO::ERROR ; 
00080                 // this would handle corrupted header files - do we need this ?
00081                 //      colIsInEvent = false ;
00082                 //      ioCol = new LCCollectionIOVec ;
00083       }
00084 
00085 
00086       _myHandler->init( stream , SIO_OP_READ , ioCol , versionID ) ;
00087 
00088       int nObj ;
00089       SIO_DATA( stream ,  &nObj , 1  ) ;
00090 
00091 //     // now read all the objects :
00092 //      for( int i=0 ; i< nObj ; i ++ ){
00093 //        LCObject* obj ;
00094 //        status  = _myHandler->read( stream , &obj ) ;
00095 //        if( !( status & 1 ) ) return status ;
00096 //        ioCol->push_back( obj ) ;
00097 //      }
00098 
00099 
00100       // reserve the space for the pointers to all objects
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         //  now write all the objects :
00123         for( int i=0 ; i< nObj ; i ++ ){
00124           
00125           const LCObject* obj = _col->getElementAt(i)  ;
00126           
00127 //        status  =  _myHandler->write( stream , obj  ) ; 
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 } // namespace

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