LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SIOIndexHandler.cc
Go to the documentation of this file.
1 #include "SIO/SIOIndexHandler.h"
2 
3 // -- lcio headers
4 #include "SIO/LCSIO.h"
5 #include "EVENT/LCIO.h"
6 
7 // -- sio headers
8 #include <sio/io_device.h>
9 #include <sio/version.h>
10 
11 // -- std headers
12 #include <limits>
13 
14 namespace SIO {
15 
17  sio::block( LCSIO::IndexBlockName, LCSIO::blockVersion() ),
18  _runEventMap( std::make_shared<RunEventMap>() ) {
19  /* nop */
20  }
21 
22  //----------------------------------------------------------------------------
23 
25  _runEventMap = remap ;
26  }
27 
28  //----------------------------------------------------------------------------
29 
31  return _runEventMap ;
32  }
33 
34  //----------------------------------------------------------------------------
35 
36  void SIOIndexHandler::read( sio::read_device &device, sio::version_type vers ) {
37  LCSIO::checkVersion( vers ) ;
38  int control ;
39  SIO_DATA( device , &control, 1 ) ;
40  bool oneRun = control & 1 ;
41  bool longOffset = control & 2 ;
42  //FIXME: do we need this ?
43  if( control & 4 ) { // parameters
44  SIO_THROW( sio::error_code::io_failure, "SIOIndexHandler: parameters not implemented ...." ) ;
45  }
46  int runMin ;
47  SIO_DATA( device , &runMin, 1 ) ;
48  long64 baseOffset ;
49  SIO_DATA( device , &baseOffset, 1 ) ;
50  int size ;
51  SIO_DATA( device , &size, 1 ) ;
52 
53  int runNum ;
54  int evtNum ;
55  int runOffset ;
56  long64 pos ;
57  int dummy_int;
58  long64 dummy_long ;
59 
60  for (int i = 0; i < size; i++) {
61  runNum = runMin ;
62  if( !oneRun ) {
63  SIO_DATA( device , &runOffset, 1 ) ;
64  runNum += runOffset ;
65  }
66  SIO_DATA( device , &evtNum , 1 ) ;
67  if( longOffset ) {
68  SIO_DATA( device , &dummy_long , 1 ) ;
69  pos = dummy_long ;
70  }
71  else {
72  SIO_DATA( device , &dummy_int , 1 ) ;
73  pos = dummy_int ;
74  }
75  pos += baseOffset ;
76  _runEventMap->add( RunEvent( runNum , evtNum ) , pos ) ;
77  }
78  }
79 
80  //----------------------------------------------------------------------------
81 
82  void SIOIndexHandler::write( sio::write_device &device ) {
83  unsigned control = 0 ;
84  auto minEntry = _runEventMap->minRunEvent() ;
85  auto maxEntry = _runEventMap->maxRunEvent() ;
86  bool oneRun = minEntry.RunNum == maxEntry.RunNum ;
87  if( oneRun ) {
88  control |= 1 ;
89  }
90  auto posMin = _runEventMap->getPosition( minEntry ) ;
91  auto posMax = _runEventMap->getPosition( maxEntry ) ;
92  bool longOffset = ( posMax - posMin ) > std::numeric_limits<int>::max() ;
93  if( longOffset ) {
94  control |= 2 ;
95  }
96  SIO_DATA( device , &control, 1 ) ;
97  int runMin = minEntry.RunNum ;
98  SIO_DATA( device , &runMin, 1 ) ;
99  long64 baseOffset = posMin ;
100  SIO_DATA( device , &baseOffset, 1 ) ;
101  int size = _runEventMap->size() ;
102  SIO_DATA( device , &size, 1 ) ;
103  for( auto it = _runEventMap->begin() ; it != _runEventMap->end() ; ++it ) {
104  if( !oneRun ) {
105  int runOffset = it->first.RunNum - runMin ;
106  SIO_DATA( device , &runOffset, 1 ) ;
107  }
108  int evtNum = it->first.EvtNum ;
109  SIO_DATA( device , &evtNum , 1 ) ;
110  if( longOffset ) {
111  long64 dummyL = it->second - baseOffset ;
112  SIO_DATA( device , &dummyL , 1 ) ;
113  }
114  else {
115  int dummyI = it->second - baseOffset ;
116  SIO_DATA( device , &dummyI , 1 ) ;
117  }
118  }
119  }
120 
121 }
std::shared_ptr< RunEventMap > runEventMap() const
Get the run / event map.
void read(sio::read_device &device, sio::version_type vers) override
void write(sio::write_device &device) override
void setRunEventMap(std::shared_ptr< RunEventMap > remap)
Set the run / event map to read or write.
Helper struct that stores run and event positions in the file.
Definition: RunEventMap.h:14
static void checkVersion(sio::version_type versionID)
Check for old version of LCIO (&gt; v01-08 ar no longer supported) Throws an exception if not supported...
Definition: LCSIO.cc:10
Map that holds positions of Run and Event records.
Definition: RunEventMap.h:37
Collection of constants and helper functions.
Definition: LCSIO.h:16
RunEvent::long64 long64
std::shared_ptr< RunEventMap > _runEventMap
The run / event map to read/write.
SIOIndexHandler()
Constructor.