LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LCEventLazyImpl.cc
Go to the documentation of this file.
1 
3 #include "SIO/LCIORecords.h"
6 
7 // -- std headers
8 #include <algorithm>
9 
10 // -- sio headers
11 #include <sio/api.h>
12 #include <sio/block.h>
13 #include <sio/compression/zlib.h>
14 
15 namespace IOIMPL {
16 
18  const_cast<LCEventLazyImpl*>(this)->unpackEvent() ;
19  return LCEventImpl::getCollection( name ) ;
20  }
21 
22  //----------------------------------------------------------------------------
23 
25  const_cast<LCEventLazyImpl*>(this)->unpackEvent() ;
26  return LCEventImpl::takeCollection( name ) ;
27  }
28 
29  //----------------------------------------------------------------------------
30 
32  std::remove_if( _blocks.begin(), _blocks.end(), [&]( const sio::block_ptr &blk ){ return (blk->name() == name) ; } ) ;
33  LCEventImpl::removeCollection( name ) ;
34  }
35 
36  //----------------------------------------------------------------------------
37 
38  void LCEventLazyImpl::setBuffer( const sio::record_info &recinfo, sio::buffer &&recordBuffer ) {
39  if( nullptr != _recordBuffer ) {
40  throw IO::IOException( "LCEventLazyImpl::setBuffer: tried to setup a SIO buffer but already set !" ) ;
41  }
42  // take the buffer and shrink the memory to fit its size
43  _recordBuffer.reset( new sio::buffer( std::move( recordBuffer ) ) ) ;
44  _recordBuffer->shrink() ;
45  _recordInfo = recinfo ;
46  // setup the block now as collections may be added before unpacking
47  setupBlocks() ;
48  }
49 
50  //----------------------------------------------------------------------------
51 
53  if( _unpacked ) {
54  return ;
55  }
56  if( nullptr == _recordBuffer ) {
57  throw IO::IOException( "LCEventLazyImpl::unpackEvent: SIO buffer not set !" ) ;
58  }
59  const bool compressed = sio::api::is_compressed( _recordInfo._options ) ;
60  // uncompress and unpack
61  if( compressed ) {
62  // uncompress
63  sio::buffer compBuffer( _recordInfo._uncompressed_length ) ;
64  sio::zlib_compression compressor ;
65  compressor.uncompress( _recordBuffer->span( _recordInfo._header_length ), compBuffer ) ;
66  // unpack the event
67  sio::api::read_blocks( compBuffer.span(), _blocks ) ;
68  }
69  else {
70  sio::api::read_blocks( _recordBuffer->span( _recordInfo._header_length ), _blocks ) ;
71  }
72  // the event record has been unpacked. Release the buffer memory
73  _recordBuffer->clear( true ) ;
74  _unpacked = true ;
76  }
77 
78  //----------------------------------------------------------------------------
79 
81  // restore the daughter relations from the parent relations
83  // check subset collection's pointers
84  char* rColChar = getenv ("LCIO_IGNORE_NULL_IN_SUBSET_COLLECTIONS") ;
85  if( nullptr != rColChar ) {
86  return;
87  }
88  const std::vector< std::string >* strVec = this->getCollectionNames() ;
89  for( auto name = strVec->begin() ; name != strVec->end() ; name++) {
90  EVENT::LCCollection* col = this->getCollection( *name ) ;
91  if( col->isSubset() ) {
92  for( int i=0,N=col->getNumberOfElements() ; i<N ; ++i ) {
93  if( col->getElementAt( i ) == 0 ) {
94  std::stringstream sts ;
95  sts << " LCEventLazyImpl::postProcessEvent: null pointer in subset collection "
96  << *name << " at position: " << i << std::endl ;
97  throw EVENT::Exception( sts.str() ) ;
98  }
99  }
100  }
101  }
102  }
103 
104  //----------------------------------------------------------------------------
105 
107  // auto collectionNames = this->getCollectionNames();
108  for ( auto i = _colMap.begin() ; i != _colMap.end() ; i++ ) {
109  auto handler = _eventHandlerMgr.getHandler( i->second->getTypeName() ) ;
110  auto block = std::make_shared<SIO::SIOCollectionHandler>( i->first, handler ) ;
111  block->setCollection( i->second );
112  _blocks.push_back( block ) ;
113  }
114  }
115 
116 }
Base exception class for LCIO - all other exceptions extend this.
Definition: Exceptions.h:21
void setBuffer(const sio::record_info &recinfo, sio::buffer &&recordBuffer)
Set the event record buffer.
static void restoreParentDaughterRelations(EVENT::LCEvent *evt)
Restore the MCParticle parent &lt;-&gt; daughter relations.
EVENT::LCCollection * takeCollection(const std::string &name) const
Returns the collection for the given name and transfers the ownership of the collection to the caller...
void unpackEvent()
Unpack the event record.
T endl(T...args)
virtual const std::vector< std::string > * getCollectionNames() const
Returns the names of the collections in the event.
Definition: LCEventImpl.cc:60
T end(T...args)
T remove_if(T...args)
virtual LCObject * getElementAt(int index) const =0
Returns pointer to element at index - no range check, use getNumberOfEntries().
STL class.
EVENT::LCCollection * getCollection(const std::string &name) const
Returns the collection for the given name.
void postProcessEvent()
Post process the event after unpacking.
virtual bool isSubset() const =0
True if the collection holds a subset of objects from other collections.
T str(T...args)
LCCollectionMap _colMap
Definition: LCEventImpl.h:180
IOException used for reading/writing errors.
Definition: Exceptions.h:92
T reset(T...args)
std::unique_ptr< sio::buffer > _recordBuffer
virtual int getNumberOfElements() const =0
Returns the number of elements in the collection.
SIO::SIOHandlerMgr _eventHandlerMgr
void setupBlocks()
Setup the collection blocks before unpacking.
Implementation of the event class with a lazy record unpacking.
The generic collection used in LCIO.
Definition: LCCollection.h:29
T begin(T...args)
std::shared_ptr< SIOObjectHandler > getHandler(const std::string &type) const
Returns the instance of the handler for the given type - null if it doesn&#39;t exist.
void removeCollection(const std::string &name)
Removes (and deletes) the collection with name (if it exists in the event).
sio::record_info _recordInfo
sio::block_list _blocks