10#define DefaultIdxBlockSize 512*1024
12int EvtIdxHandler::_nHandler = 0;
17 if ( _instance == 0 ) {
28 if ( _nHandler > 0 && --_nHandler == 0 ) {
39 m_EIdBlock =
new uint32_t[m_blockSize];
40 m_PosBlock =
new uint32_t[m_blockSize];
42 m_curFile = m_fnames.begin();
51 m_EIdBlock =
new uint32_t[m_blockSize];
52 m_PosBlock =
new uint32_t[m_blockSize];
72 return m_PosBlock[m_idxPos++];
77 while ( m_totEvt > m_idxPos ) {
78 if ( m_EIdBlock[m_idxPos] == evtId ) {
79 return m_PosBlock[m_idxPos++];
90 if ( m_totEvt > m_blockSize ) enlarge_block( m_totEvt );
92 m_EIdBlock[m_totEvt-1] = evtId;
93 m_PosBlock[m_totEvt-1] = pos;
98 if ( m_idxPos >= 0 ) {
99 std::cerr <<
"[RawFile] Writing an EvtIdxHandler for reading !!!" << std::endl;
103 m_fs.open( fname.c_str(), std::ios::out | std::ios::binary );
106 m_fs.write( (
char*)&marker,
sizeof(marker) );
107 m_fs.write( (
char*)&m_totEvt,
sizeof(m_totEvt) );
110 m_fs.write( (
char*)&marker,
sizeof(marker) );
111 m_fs.write( (
char*)m_EIdBlock, m_totEvt*
sizeof(uint32_t) );
114 m_fs.write( (
char*)&marker,
sizeof(marker) );
115 m_fs.write( (
char*)m_PosBlock, m_totEvt*
sizeof(uint32_t) );
117 if ( ! m_fs.good() ) {
118 std::cerr <<
"[RawFile] Error writing IDX file: " << fname << std::endl;
124 std::cout <<
"[RawFile] Written IDX file: " << fname << std::endl;
129void EvtIdxHandler::init_idx()
131 if ( access( m_curFile->c_str(), F_OK ) < 0 ) {
132 std::cerr <<
"[RawFile] Invalid IDX file: " << *m_curFile << std::endl;
136 std::cout <<
"[RawFile] Initialize IDX with file: " << *m_curFile << std::endl;
138 m_fs.open( m_curFile->c_str(), std::ios::in | std::ios::binary );
142 m_fs.read( (
char*)&marker,
sizeof(marker) );
144 std::cerr <<
"[RawFile] Wrong IdxFileStartMarker!" << std::endl;
148 m_fs.read( (
char*)&m_totEvt,
sizeof(m_totEvt) );
150 if ( m_totEvt > m_blockSize ) enlarge_block( m_totEvt );
152 m_fs.read( (
char*)&marker,
sizeof(marker) );
154 std::cerr <<
"[RawFile] Wrong IdxIdBlockMarker!" << std::endl;
158 m_fs.read( (
char*)m_EIdBlock, m_totEvt*
sizeof(uint32_t) );
160 m_fs.read( (
char*)&marker,
sizeof(marker) );
162 std::cerr <<
"[RawFile] Wrong IdxPosBlockMarker!" << std::endl;
166 m_fs.read( (
char*)m_PosBlock, m_totEvt*
sizeof(uint32_t) );
168 if ( ! m_fs.good() ) {
169 std::cerr <<
"[RawFile] Error occured while initialize the IDX file!" << std::endl;
176void EvtIdxHandler::enlarge_block(
int min_size)
178 int _initSize = m_blockSize;
179 while ( min_size > m_blockSize ) {
183 uint32_t* _EIdBlock =
new uint32_t[m_blockSize];
184 uint32_t* _PosBlock =
new uint32_t[m_blockSize];
186 memcpy((
void*)_EIdBlock, (
const void*)m_EIdBlock,
sizeof(uint32_t)*_initSize);
187 memcpy((
void*)_PosBlock, (
const void*)m_PosBlock,
sizeof(uint32_t)*_initSize);
192 m_EIdBlock = _EIdBlock;
193 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()