LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SIOLCGenericObjectHandler.cc
Go to the documentation of this file.
2 
3 #include "EVENT/LCIO.h"
6 #include "IMPL/LCFlagImpl.h"
7 
8 // -- sio headers
9 #include <sio/io_device.h>
10 #include <sio/version.h>
11 
12 namespace SIO {
13 
15  SIOObjectHandler( EVENT::LCIO::LCGENERICOBJECT ) {
16  /* nop */
17  }
18 
19  //----------------------------------------------------------------------------
20 
21  void SIOLCGenericObjectHandler::initReading( sio::read_device &device, EVENT::LCCollection *collection, sio::version_type vers ) {
22  SIOObjectHandler::initReading( device, collection, vers ) ;
23  _isFixedSize = IMPL::LCFlagImpl(_flag).bitSet( EVENT::LCIO::GOBIT_FIXED ) ;
24  if( _isFixedSize ) { // need to read the size variables once
25  SIO_SDATA( device , _nInt ) ;
26  SIO_SDATA( device , _nFloat ) ;
27  SIO_SDATA( device , _nDouble ) ;
28  }
29  }
30 
31  //----------------------------------------------------------------------------
32 
33  void SIOLCGenericObjectHandler::initWriting( sio::write_device &device, EVENT::LCCollection *collection ) {
34  _isFixedSize = true ;
35  // need to check whether we have fixed size objects only
36  unsigned int nObj = collection->getNumberOfElements() ;
37  for( unsigned int i=0 ; i < nObj ; i++ ) {
38  auto obj = dynamic_cast< EVENT::LCGenericObject* >( collection->getElementAt( i ) ) ;
39  if( ! obj->isFixedSize() ) {
40  _isFixedSize = false ;
41  break ;
42  }
43  }
44  IMPL::LCFlagImpl colflag( collection->getFlag() ) ;
45  EVENT::LCGenericObject* gObj = nullptr ;
46  if( collection->getNumberOfElements() > 0 ) {
47  gObj = dynamic_cast< EVENT::LCGenericObject* >( collection->getElementAt( 0 ) ) ;
48  }
49  // if the collection doesn't have the TypeName/DataDescription parameters set,
50  // we use the ones from the first object
51  if( collection->parameters().getStringVal( "TypeName" ).empty() && nullptr != gObj ) {
52  collection->parameters().setValue( "TypeName", gObj->getTypeName() ) ;
53  }
54  if( _isFixedSize ) {
55  colflag.setBit( EVENT::LCIO::GOBIT_FIXED ) ;
56  if( collection->parameters().getStringVal( "DataDescription" ).empty() && nullptr != gObj ) {
57  collection->parameters().setValue( "DataDescription", gObj->getDataDescription() ) ;
58  }
59  }
60  _flag = colflag.getFlag() ;
61  collection->setFlag( _flag ) ;
62  SIOObjectHandler::initWriting( device, collection ) ;
63  if( _isFixedSize ) { // need to write the size variables once
64  if( gObj != 0 ){
65  _nInt = gObj->getNInt() ;
66  _nFloat = gObj->getNFloat() ;
67  _nDouble = gObj->getNDouble() ;
68  }
69  else {
70  _nInt = 0 ;
71  _nFloat = 0 ;
72  _nDouble = 0 ;
73  }
74  SIO_SDATA( device , _nInt ) ;
75  SIO_SDATA( device , _nFloat ) ;
76  SIO_SDATA( device , _nDouble ) ;
77  }
78  }
79 
80  //----------------------------------------------------------------------------
81 
82  void SIOLCGenericObjectHandler::read( sio::read_device& device, EVENT::LCObject* objP, sio::version_type /*vers*/ ) {
83  auto gobj = dynamic_cast<IOIMPL::LCGenericObjectIOImpl*>( objP ) ;
84  gobj->_isFixedSize = _isFixedSize ;
85  if( ! _isFixedSize ) {
86  SIO_SDATA( device , _nInt ) ;
87  SIO_SDATA( device , _nFloat ) ;
88  SIO_SDATA( device , _nDouble ) ;
89  }
90  gobj->_intVec.resize( _nInt ) ;
91  gobj->_floatVec.resize( _nFloat ) ;
92  gobj->_doubleVec.resize( _nDouble ) ;
93  SIO_DATA( device , &(gobj->_intVec[0]), _nInt ) ;
94  SIO_DATA( device , &(gobj->_floatVec[0]), _nFloat ) ;
95  SIO_DATA( device , &(gobj->_doubleVec[0]), _nDouble ) ;
96  SIO_PTAG( device , gobj ) ;
97  }
98 
99  //----------------------------------------------------------------------------
100 
101  void SIOLCGenericObjectHandler::write( sio::write_device& device, const EVENT::LCObject* obj ) {
102  auto gObj = dynamic_cast<const EVENT::LCGenericObject*>( obj ) ;
103  if( ! _isFixedSize ) {
104  _nInt = gObj->getNInt() ;
105  _nFloat = gObj->getNFloat() ;
106  _nDouble = gObj->getNDouble() ;
107  SIO_SDATA( device , _nInt ) ;
108  SIO_SDATA( device , _nFloat ) ;
109  SIO_SDATA( device , _nDouble ) ;
110  }
111 
112  for( int i=0 ; i< _nInt ; i++) {
113  SIO_SDATA( device , gObj->getIntVal( i ) ) ;
114  }
115  for( int i=0 ; i< _nFloat ; i++) {
116  SIO_SDATA( device , gObj->getFloatVal( i ) ) ;
117  }
118  for( int i=0 ; i< _nDouble ; i++) {
119  SIO_SDATA( device , gObj->getDoubleVal( i ) ) ;
120  }
121  SIO_PTAG( device , gObj ) ;
122  }
123 
124  //----------------------------------------------------------------------------
125 
127  return new IOIMPL::LCGenericObjectIOImpl() ;
128  }
129 
130 } // namespace
The generic object that is held in an LCCollection.
Definition: LCObject.h:30
T empty(T...args)
virtual bool bitSet(int index) const
Returns true if bit at given index is set.
Definition: LCFlagImpl.cc:13
void initWriting(sio::write_device &device, EVENT::LCCollection *collection) override
Init collection reading.
Adding stuff needed for io (friend declarations, etc.)
void read(sio::read_device &device, EVENT::LCObject *objP, sio::version_type vers) override
Reads lcio objects from an SIO stream.
virtual void setFlag(int flag)=0
Set the flag word.
EVENT::LCObject * create() const override
Factory method to create an object of the type of the collection.
Interface for all lcio object SIO-handlers, has to be implemented for all event entities (hits...
unsigned int _flag
The collection flag.
virtual void initReading(sio::read_device &device, EVENT::LCCollection *collection, sio::version_type vers)
Init collection reading.
virtual LCObject * getElementAt(int index) const =0
Returns pointer to element at index - no range check, use getNumberOfEntries().
virtual int getFlag() const =0
Returns flag word for collection.
virtual void initWriting(sio::write_device &device, EVENT::LCCollection *collection)
Init collection writing.
virtual LCParameters & parameters()=0
Parameters defined for this collection.
Implementation of helper class to create and interpret the 32-bit flag word in LCCollections.
Definition: LCFlagImpl.h:15
Simple interface to store generic user data.
virtual void setValue(const std::string &key, int value)=0
Set integer value for the given key.
void write(sio::write_device &device, const EVENT::LCObject *obj) override
Writes lcio objects to an SIO stream.
virtual int getNumberOfElements() const =0
Returns the number of elements in the collection.
The generic collection used in LCIO.
Definition: LCCollection.h:29
virtual const std::string & getStringVal(const std::string &key) const =0
Returns the first string value for the given key.
void initReading(sio::read_device &device, EVENT::LCCollection *collection, sio::version_type vers) override
Init collection reading.