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