BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
EvtIdxHandler.cxx
Go to the documentation of this file.
1#include "RawFile/EvtIdxHandler.h"
2#include <cstdio>
3#include <cstdlib>
4#include <cstring>
5#include <iostream>
6#include <string>
7#include <unistd.h>
8using namespace std;
9
10#define DefaultIdxBlockSize 512 * 1024
11
12int EvtIdxHandler::_nHandler = 0;
13EvtIdxHandler* EvtIdxHandler::_instance = 0;
14
15EvtIdxHandler* EvtIdxHandler::instance( const std::vector<std::string>& idxfnames ) {
16 if ( _instance == 0 ) { _instance = new EvtIdxHandler( idxfnames ); }
17
18 ++_nHandler;
19
20 return _instance;
21}
22
24 if ( _nHandler > 0 && --_nHandler == 0 )
25 {
26 delete _instance;
27 _instance = 0;
28 }
29}
30
31EvtIdxHandler::EvtIdxHandler( const std::vector<std::string>& idxfnames )
32 : m_idxPos( 0 ), m_blockSize( DefaultIdxBlockSize ), m_fnames( idxfnames ) {
33 m_EIdBlock = new uint32_t[m_blockSize];
34 m_PosBlock = new uint32_t[m_blockSize];
35
36 m_curFile = m_fnames.begin();
37 init_idx();
38}
39
41 : m_totEvt( 0 ), m_idxPos( -1 ), m_blockSize( DefaultIdxBlockSize ) {
42 m_EIdBlock = new uint32_t[m_blockSize];
43 m_PosBlock = new uint32_t[m_blockSize];
44}
45
47 delete[] m_EIdBlock;
48 delete[] m_PosBlock;
49}
50
52 ++m_curFile;
53
54 m_idxPos = 0;
55 init_idx();
56}
57
58uint32_t EvtIdxHandler::nextPos( int nIgnore ) {
59 m_idxPos += nIgnore;
60 return m_PosBlock[m_idxPos++];
61}
62
63uint32_t EvtIdxHandler::findPosById( uint32_t evtId ) {
64 while ( m_totEvt > m_idxPos )
65 {
66 if ( m_EIdBlock[m_idxPos] == evtId ) { return m_PosBlock[m_idxPos++]; }
67 ++m_idxPos;
68 }
69 return 0;
70}
71
72void EvtIdxHandler::addPos( uint32_t evtId, uint32_t pos ) {
73 ++m_totEvt;
74
75 if ( m_totEvt > m_blockSize ) enlarge_block( m_totEvt );
76
77 m_EIdBlock[m_totEvt - 1] = evtId;
78 m_PosBlock[m_totEvt - 1] = pos;
79}
80
81void EvtIdxHandler::write( std::string fname ) {
82 if ( m_idxPos >= 0 )
83 {
84 std::cerr << "[RawFile] Writing an EvtIdxHandler for reading !!!" << std::endl;
85 exit( 1 );
86 }
87
88 m_fs.open( fname.c_str(), std::ios::out | std::ios::binary );
89
90 uint32_t marker = EvtIdxHandler::IdxFileStartMarker();
91 m_fs.write( (char*)&marker, sizeof( marker ) );
92 m_fs.write( (char*)&m_totEvt, sizeof( m_totEvt ) );
93
95 m_fs.write( (char*)&marker, sizeof( marker ) );
96 m_fs.write( (char*)m_EIdBlock, m_totEvt * sizeof( uint32_t ) );
97
99 m_fs.write( (char*)&marker, sizeof( marker ) );
100 m_fs.write( (char*)m_PosBlock, m_totEvt * sizeof( uint32_t ) );
101
102 if ( !m_fs.good() )
103 {
104 std::cerr << "[RawFile] Error writing IDX file: " << fname << std::endl;
105 exit( 1 );
106 }
107
108 m_fs.close();
109
110 std::cout << "[RawFile] Written IDX file: " << fname << std::endl;
111
112 m_totEvt = 0;
113}
114
115void EvtIdxHandler::init_idx() {
116 if ( access( m_curFile->c_str(), F_OK ) < 0 )
117 {
118 std::cerr << "[RawFile] Invalid IDX file: " << *m_curFile << std::endl;
119 exit( 1 );
120 }
121
122 std::cout << "[RawFile] Initialize IDX with file: " << *m_curFile << std::endl;
123
124 m_fs.open( m_curFile->c_str(), std::ios::in | std::ios::binary );
125
126 uint32_t marker;
127
128 m_fs.read( (char*)&marker, sizeof( marker ) );
129 if ( marker != EvtIdxHandler::IdxFileStartMarker() )
130 {
131 std::cerr << "[RawFile] Wrong IdxFileStartMarker!" << std::endl;
132 exit( 1 );
133 }
134
135 m_fs.read( (char*)&m_totEvt, sizeof( m_totEvt ) );
136
137 if ( m_totEvt > m_blockSize ) enlarge_block( m_totEvt );
138
139 m_fs.read( (char*)&marker, sizeof( marker ) );
140 if ( marker != EvtIdxHandler::IdxIdBlockMarker() )
141 {
142 std::cerr << "[RawFile] Wrong IdxIdBlockMarker!" << std::endl;
143 exit( 1 );
144 }
145
146 m_fs.read( (char*)m_EIdBlock, m_totEvt * sizeof( uint32_t ) );
147
148 m_fs.read( (char*)&marker, sizeof( marker ) );
149 if ( marker != EvtIdxHandler::IdxPosBlockMarker() )
150 {
151 std::cerr << "[RawFile] Wrong IdxPosBlockMarker!" << std::endl;
152 exit( 1 );
153 }
154
155 m_fs.read( (char*)m_PosBlock, m_totEvt * sizeof( uint32_t ) );
156
157 if ( !m_fs.good() )
158 {
159 std::cerr << "[RawFile] Error occured while initialize the IDX file!" << std::endl;
160 exit( 1 );
161 }
162
163 m_fs.close();
164}
165
166void EvtIdxHandler::enlarge_block( int min_size ) {
167 int _initSize = m_blockSize;
168 while ( min_size > m_blockSize ) { m_blockSize *= 2; }
169
170 uint32_t* _EIdBlock = new uint32_t[m_blockSize];
171 uint32_t* _PosBlock = new uint32_t[m_blockSize];
172
173 memcpy( (void*)_EIdBlock, (const void*)m_EIdBlock, sizeof( uint32_t ) * _initSize );
174 memcpy( (void*)_PosBlock, (const void*)m_PosBlock, sizeof( uint32_t ) * _initSize );
175
176 delete[] m_EIdBlock;
177 delete[] m_PosBlock;
178
179 m_EIdBlock = _EIdBlock;
180 m_PosBlock = _PosBlock;
181}
#define DefaultIdxBlockSize
static EvtIdxHandler * instance(const std::vector< std::string > &fnames)
uint32_t findPosById(uint32_t evtId)
void write(std::string fname)
virtual ~EvtIdxHandler()
static void release()
void addPos(uint32_t evtId, uint32_t pos)
uint32_t nextPos(int nIgnore)