1#include "RawFile/RawFileReader.h"
2#include "IRawFile/RawFileExceptions.h"
3#include "RawFile/EvtIdxHandler.h"
4#include "RawFile/RawFileTools.h"
5#include "RawFile/raw_ifstream.h"
9#define DefaultEventBufferSize 1024 * 32
10#define EstimatedEventSize 1024 * 14
14 std::vector<int> vNevt;
17 VFileNames_t::const_iterator it = newfnames.begin();
18 while ( it != newfnames.end() )
20 if ( access( it->c_str(), F_OK ) < 0 )
22 std::cerr <<
"[RawFile] Invalid IDX file: " << *it << std::endl;
26 std::ifstream
fs( it->c_str(), std::ios::binary );
28 fs.read( (
char*)( itmp ),
sizeof( uint32_t ) * 2 );
31 std::cerr <<
"[RawFile] Wrong IdxFileStartMarker!" << std::endl;
34 vNevt.push_back( itmp[1] );
49 for (
int i = 0; i < newfnames.size(); ++i )
51 int size = newfnames[i].size();
52 if ( size > 4 && newfnames[i].substr( size - 4 ) ==
".idx" )
53 { datafnames.push_back( newfnames[i].substr( 0, size - 4 ) ); }
55 if ( datafnames.size() == newfnames.size() )
71 for (
int i = 0; i < newfnames.size(); ++i )
73 int size = newfnames[i].size();
74 if ( size > 4 && newfnames[i].substr( size - 4 ) ==
".idx" )
75 { datafnames.push_back( newfnames[i].substr( 0, size - 4 ) ); }
77 if ( datafnames.size() == newfnames.size() )
91 if ( newidxfnames.size() != newfnames.size() )
93 std::cerr <<
"[RawFile] Num(IdxFiles) != Num(DataFiles)" << std::endl;
107 if ( newidxfnames.size() != newfnames.size() )
109 std::cerr <<
"[RawFile] Num(IdxFiles) != Num(DataFiles)" << std::endl;
129 if ( m_idxHandler == 0 ) { notSafeNextEvent(); }
149 int nnIgnore = nIgnore;
153 if ( m_idxHandler != 0 )
155 int nleft = m_idxHandler->nEvtLeft( nnIgnore );
158 m_rfs->seekg( m_idxHandler->nextPos( nnIgnore ) );
169 while ( nnIgnore > 0 )
171 ( *m_rfs ) >> m_dataSeparatorRecord;
172 uint32_t size = m_dataSeparatorRecord.getRecord().data_block_size;
173 m_rfs->seekg( size + m_rfs->tellg() );
180 { nextFile( e ).nextEvent( nnIgnore ); }
190 if ( m_idxHandler == 0 )
195 uint32_t curEvtId = m_buffer[m_buffer[5] + 8];
196 if ( curEvtId == evtId ) {
break; }
201 uint32_t pos = m_idxHandler->findPosById( evtId );
210 {
return nextFile( e ).findEventById( evtId ); }
219 assert( ( evtByte & 3 ) == 0 );
221 uint32_t prePos = m_rfs->tellg();
222 m_rfs->seekg( prePos + nIgnore * evtByte );
224 uint32_t halfEvtWord = evtByte / 8;
225 uint32_t halfEvtByte = halfEvtWord * 4;
227 while ( m_rfs->read( (
char*)m_buffer, halfEvtByte ).good() )
230 while ( i < halfEvtWord && m_buffer[i] != 0x1234cccc ) { ++i; }
231 if ( i < halfEvtWord )
233 uint32_t curPos = m_rfs->tellg();
234 m_rfs->seekg( curPos - ( halfEvtWord - i ) * 4 );
242 m_rfs->seekg( -40, std::ios::end );
243 uint32_t curPos = m_rfs->tellg();
245 int nnIgnore = nIgnore - ( curPos - prePos ) / evtByte;
246 if ( nnIgnore < 0 ) nnIgnore = 0;
249 return nextFile( e ).roughlyNextEvent( nnIgnore, evtByte );
260 if ( m_rfs->eof() )
stat |= 1;
261 if ( m_rfs->fail() )
stat |= 2;
262 if ( m_rfs->bad() )
stat |= 4;
267const uint32_t* RawFileReader::notSafeNextEvent() {
269 { read_one_event(); }
catch ( RawFileException& e )
270 { nextFile( e ).notSafeNextEvent(); }
275void RawFileReader::read_one_event() {
276 ( *m_rfs ) >> m_dataSeparatorRecord;
278 uint32_t size = m_dataSeparatorRecord.getRecord().data_block_size;
279 if ( size > m_bufferSize * 4 )
281 while ( size > m_bufferSize * 4 ) { m_bufferSize *= 2; }
283 m_buffer =
new uint32_t[m_bufferSize];
286 m_rfs->read( (
char*)m_buffer, size );
287 if ( !m_rfs->good() )
290 throw BadInputStream(
"event_data_block" );
300 if ( m_idxHandler != 0 ) { m_idxHandler->next_file(); }
std::vector< std::string > VFileNames_t
#define DefaultEventBufferSize
#define EstimatedEventSize
static EvtIdxHandler * instance(const std::vector< std::string > &fnames)
static uint32_t IdxFileStartMarker()
virtual void print() const
const uint32_t * findEventById(uint32_t evtId)
const uint32_t * roughlyNextEvent(int nIgnore, int evtByte=0)
RawFileReader(const std::string &fname)
const uint32_t * nextEvent()
static std::vector< int > getEventNumber(const VFileNames_t &idxfnames)
std::string currentFile()
virtual void print() const
static raw_ifstream * instance(const std::vector< std::string > &fnames)