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