1#include "RawFile/EvtIdxHandler.h"
10#define DefaultIdxBlockSize 512 * 1024
12int EvtIdxHandler::_nHandler = 0;
16 if ( _instance == 0 ) { _instance =
new EvtIdxHandler( idxfnames ); }
24 if ( _nHandler > 0 && --_nHandler == 0 )
33 m_EIdBlock =
new uint32_t[m_blockSize];
34 m_PosBlock =
new uint32_t[m_blockSize];
36 m_curFile = m_fnames.begin();
42 m_EIdBlock =
new uint32_t[m_blockSize];
43 m_PosBlock =
new uint32_t[m_blockSize];
60 return m_PosBlock[m_idxPos++];
64 while ( m_totEvt > m_idxPos )
66 if ( m_EIdBlock[m_idxPos] == evtId ) {
return m_PosBlock[m_idxPos++]; }
75 if ( m_totEvt > m_blockSize ) enlarge_block( m_totEvt );
77 m_EIdBlock[m_totEvt - 1] = evtId;
78 m_PosBlock[m_totEvt - 1] = pos;
84 std::cerr <<
"[RawFile] Writing an EvtIdxHandler for reading !!!" << std::endl;
88 m_fs.open( fname.c_str(), std::ios::out | std::ios::binary );
91 m_fs.write( (
char*)&marker,
sizeof( marker ) );
92 m_fs.write( (
char*)&m_totEvt,
sizeof( m_totEvt ) );
95 m_fs.write( (
char*)&marker,
sizeof( marker ) );
96 m_fs.write( (
char*)m_EIdBlock, m_totEvt *
sizeof( uint32_t ) );
99 m_fs.write( (
char*)&marker,
sizeof( marker ) );
100 m_fs.write( (
char*)m_PosBlock, m_totEvt *
sizeof( uint32_t ) );
104 std::cerr <<
"[RawFile] Error writing IDX file: " << fname << std::endl;
110 std::cout <<
"[RawFile] Written IDX file: " << fname << std::endl;
115void EvtIdxHandler::init_idx() {
116 if ( access( m_curFile->c_str(), F_OK ) < 0 )
118 std::cerr <<
"[RawFile] Invalid IDX file: " << *m_curFile << std::endl;
122 std::cout <<
"[RawFile] Initialize IDX with file: " << *m_curFile << std::endl;
124 m_fs.open( m_curFile->c_str(), std::ios::in | std::ios::binary );
128 m_fs.read( (
char*)&marker,
sizeof( marker ) );
131 std::cerr <<
"[RawFile] Wrong IdxFileStartMarker!" << std::endl;
135 m_fs.read( (
char*)&m_totEvt,
sizeof( m_totEvt ) );
137 if ( m_totEvt > m_blockSize ) enlarge_block( m_totEvt );
139 m_fs.read( (
char*)&marker,
sizeof( marker ) );
142 std::cerr <<
"[RawFile] Wrong IdxIdBlockMarker!" << std::endl;
146 m_fs.read( (
char*)m_EIdBlock, m_totEvt *
sizeof( uint32_t ) );
148 m_fs.read( (
char*)&marker,
sizeof( marker ) );
151 std::cerr <<
"[RawFile] Wrong IdxPosBlockMarker!" << std::endl;
155 m_fs.read( (
char*)m_PosBlock, m_totEvt *
sizeof( uint32_t ) );
159 std::cerr <<
"[RawFile] Error occured while initialize the IDX file!" << std::endl;
166void EvtIdxHandler::enlarge_block(
int min_size ) {
167 int _initSize = m_blockSize;
168 while ( min_size > m_blockSize ) { m_blockSize *= 2; }
170 uint32_t* _EIdBlock =
new uint32_t[m_blockSize];
171 uint32_t* _PosBlock =
new uint32_t[m_blockSize];
173 memcpy( (
void*)_EIdBlock, (
const void*)m_EIdBlock,
sizeof( uint32_t ) * _initSize );
174 memcpy( (
void*)_PosBlock, (
const void*)m_PosBlock,
sizeof( uint32_t ) * _initSize );
179 m_EIdBlock = _EIdBlock;
180 m_PosBlock = _PosBlock;
#define DefaultIdxBlockSize
static uint32_t IdxPosBlockMarker()
static EvtIdxHandler * instance(const std::vector< std::string > &fnames)
uint32_t findPosById(uint32_t evtId)
void write(std::string fname)
static uint32_t IdxFileStartMarker()
void addPos(uint32_t evtId, uint32_t pos)
uint32_t nextPos(int nIgnore)
static uint32_t IdxIdBlockMarker()