2#include "RawFile/EvtIdxHandler.h"
6#define DefaultIdxBlockSize 512*1024
8int EvtIdxHandler::_nHandler = 0;
13 if ( _instance == 0 ) {
24 if ( _nHandler > 0 && --_nHandler == 0 ) {
35 m_EIdBlock =
new uint32_t[m_blockSize];
36 m_PosBlock =
new uint32_t[m_blockSize];
38 m_curFile = m_fnames.begin();
47 m_EIdBlock =
new uint32_t[m_blockSize];
48 m_PosBlock =
new uint32_t[m_blockSize];
68 return m_PosBlock[m_idxPos++];
73 while ( m_totEvt > m_idxPos ) {
74 if ( m_EIdBlock[m_idxPos] == evtId ) {
75 return m_PosBlock[m_idxPos++];
86 if ( m_totEvt > m_blockSize ) enlarge_block( m_totEvt );
88 m_EIdBlock[m_totEvt-1] = evtId;
89 m_PosBlock[m_totEvt-1] = pos;
94 if ( m_idxPos >= 0 ) {
95 std::cerr <<
"[RawFile] Writing an EvtIdxHandler for reading !!!" << std::endl;
99 m_fs.open( fname.c_str(), std::ios::out | std::ios::binary );
102 m_fs.write( (
char*)&marker,
sizeof(marker) );
103 m_fs.write( (
char*)&m_totEvt,
sizeof(m_totEvt) );
106 m_fs.write( (
char*)&marker,
sizeof(marker) );
107 m_fs.write( (
char*)m_EIdBlock, m_totEvt*
sizeof(uint32_t) );
110 m_fs.write( (
char*)&marker,
sizeof(marker) );
111 m_fs.write( (
char*)m_PosBlock, m_totEvt*
sizeof(uint32_t) );
113 if ( ! m_fs.good() ) {
114 std::cerr <<
"[RawFile] Error writing IDX file: " << fname << std::endl;
120 std::cout <<
"[RawFile] Written IDX file: " << fname << std::endl;
125void EvtIdxHandler::init_idx()
127 if ( access( m_curFile->c_str(), F_OK ) < 0 ) {
128 std::cerr <<
"[RawFile] Invalid IDX file: " << *m_curFile << std::endl;
132 std::cout <<
"[RawFile] Initialize IDX with file: " << *m_curFile << std::endl;
134 m_fs.open( m_curFile->c_str(), std::ios::in | std::ios::binary );
138 m_fs.read( (
char*)&marker,
sizeof(marker) );
140 std::cerr <<
"[RawFile] Wrong IdxFileStartMarker!" << std::endl;
144 m_fs.read( (
char*)&m_totEvt,
sizeof(m_totEvt) );
146 if ( m_totEvt > m_blockSize ) enlarge_block( m_totEvt );
148 m_fs.read( (
char*)&marker,
sizeof(marker) );
150 std::cerr <<
"[RawFile] Wrong IdxIdBlockMarker!" << std::endl;
154 m_fs.read( (
char*)m_EIdBlock, m_totEvt*
sizeof(uint32_t) );
156 m_fs.read( (
char*)&marker,
sizeof(marker) );
158 std::cerr <<
"[RawFile] Wrong IdxPosBlockMarker!" << std::endl;
162 m_fs.read( (
char*)m_PosBlock, m_totEvt*
sizeof(uint32_t) );
164 if ( ! m_fs.good() ) {
165 std::cerr <<
"[RawFile] Error occured while initialize the IDX file!" << std::endl;
172void EvtIdxHandler::enlarge_block(
int min_size)
174 int _initSize = m_blockSize;
175 while ( min_size > m_blockSize ) {
179 uint32_t* _EIdBlock =
new uint32_t[m_blockSize];
180 uint32_t* _PosBlock =
new uint32_t[m_blockSize];
182 memcpy((
void*)_EIdBlock, (
const void*)m_EIdBlock,
sizeof(uint32_t)*_initSize);
183 memcpy((
void*)_PosBlock, (
const void*)m_PosBlock,
sizeof(uint32_t)*_initSize);
188 m_EIdBlock = _EIdBlock;
189 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()