BOSS 7.0.7
BESIII Offline Software System
Loading...
Searching...
No Matches
RawFileReader.cxx
Go to the documentation of this file.
6#include <cstdlib>
7
8#define DefaultEventBufferSize 1024*32
9#define EstimatedEventSize 1024*14
10
11std::vector<int> RawFileReader::getEventNumber(const VFileNames_t& idxfnames)
12{
13 uint32_t itmp[2];
14 std::vector<int> vNevt;
15
16 const VFileNames_t newfnames = RawFileTools::wildcard_correct(idxfnames);
17 VFileNames_t::const_iterator it = newfnames.begin();
18 while ( it != newfnames.end() ) {
19 if ( access( it->c_str(), F_OK ) < 0 ) {
20 std::cerr << "[RawFile] Invalid IDX file: " << *it << std::endl;
21 exit(1);
22 }
23
24 std::ifstream fs( it->c_str(), std::ios::binary );
25
26 fs.read( (char*)(itmp), sizeof(uint32_t)*2 );
27 if ( itmp[0] != EvtIdxHandler::IdxFileStartMarker() ) {
28 std::cerr << "[RawFile] Wrong IdxFileStartMarker!" << std::endl;
29 exit(1);
30 }
31 vNevt.push_back(itmp[1]);
32 ++it;
33 }
34
35 return vNevt;
36}
37
38RawFileReader::RawFileReader(const std::string& fname)
39 : m_bufferSize(DefaultEventBufferSize),
40 m_buffer( new uint32_t[DefaultEventBufferSize] ),
41 m_idxHandler(0)
42{
43 const VFileNames_t newfnames = RawFileTools::wildcard_correct(fname);
44
45 // in case of idx files
46 VFileNames_t datafnames;
47 for ( int i = 0; i < newfnames.size (); ++i ) {
48 int size = newfnames[i].size();
49 if ( size > 4 && newfnames[i].substr(size-4) == ".idx" ) {
50 datafnames.push_back(newfnames[i].substr(0, size-4));
51 }
52 }
53 if ( datafnames.size() == newfnames.size() ) {
54 m_rfs = raw_ifstream::instance(datafnames);
55 m_idxHandler = EvtIdxHandler::instance(newfnames);
56 }
57 else {
58 m_rfs = raw_ifstream::instance(newfnames);
59 }
60}
61
63 : m_bufferSize(DefaultEventBufferSize),
64 m_buffer( new uint32_t[DefaultEventBufferSize] ),
65 m_idxHandler(0)
66{
67 const VFileNames_t newfnames = RawFileTools::wildcard_correct(fnames);
68
69 // in case of idx files
70 VFileNames_t datafnames;
71 for ( int i = 0; i < newfnames.size (); ++i ) {
72 int size = newfnames[i].size();
73 if ( size > 4 && newfnames[i].substr(size-4) == ".idx" ) {
74 datafnames.push_back(newfnames[i].substr(0, size-4));
75 }
76 }
77 if ( datafnames.size() == newfnames.size() ) {
78 m_rfs = raw_ifstream::instance(datafnames);
79 m_idxHandler = EvtIdxHandler::instance(newfnames);
80 }
81 else {
82 m_rfs = raw_ifstream::instance(newfnames);
83 }
84}
85
86RawFileReader::RawFileReader(const std::string& fname, const std::string& idxfname)
87 : m_bufferSize(DefaultEventBufferSize),
88 m_buffer( new uint32_t[DefaultEventBufferSize] )
89{
90 const VFileNames_t newfnames = RawFileTools::wildcard_correct(fname);
91 const VFileNames_t newidxfnames = RawFileTools::wildcard_correct(idxfname);
92
93 if ( newidxfnames.size() != newfnames.size() ) {
94 std::cerr << "[RawFile] Num(IdxFiles) != Num(DataFiles)" << std::endl;
95 exit(1);
96 }
97
98 m_rfs = raw_ifstream::instance(newfnames);
99 m_idxHandler = EvtIdxHandler::instance(newidxfnames);
100}
101
103 : m_bufferSize(DefaultEventBufferSize),
104 m_buffer( new uint32_t[DefaultEventBufferSize] )
105{
106 const VFileNames_t newfnames = RawFileTools::wildcard_correct(fnames);
107 const VFileNames_t newidxfnames = RawFileTools::wildcard_correct(idxfnames);
108
109 if ( newidxfnames.size() != newfnames.size() ) {
110 std::cerr << "[RawFile] Num(IdxFiles) != Num(DataFiles)" << std::endl;
111 exit(1);
112 }
113
114 m_rfs = raw_ifstream::instance(newfnames);
115 m_idxHandler = EvtIdxHandler::instance(newidxfnames);
116}
117
119{
120 delete[] m_buffer;
123}
124
126{
127 // thread safe
129
130 try {
131 if ( m_idxHandler == 0 ) {
132 notSafeNextEvent();
133 }
134 else {
135 nextEvent(0);
136 }
137 }
138 catch ( ReachEndOfFileList& e) {
140 throw e;
141 }
142 catch ( RawFileException& e) {
144 e.print();
145 throw e;
146 }
147
149
150 return m_buffer;
151}
152
153const uint32_t* RawFileReader::nextEvent(int nIgnore)
154{
155 // not thread safe !!!
156 int nnIgnore = nIgnore;
157
158 try {
159 if ( m_idxHandler != 0 ) {
160 int nleft = m_idxHandler->nEvtLeft(nnIgnore);
161 if ( nleft > 0 ) {
162 m_rfs->seekg( m_idxHandler->nextPos( nnIgnore ) );
163 nnIgnore = 0;
164 }
165 else {
166 nnIgnore = -nleft;
167 throw ReachEndOfFile( currentFile().c_str() );
168 }
169 }
170 else {
171 while ( nnIgnore > 0 ) {
172 (*m_rfs) >> m_dataSeparatorRecord;
173 uint32_t size = m_dataSeparatorRecord.getRecord().data_block_size;
174 m_rfs->seekg( size + m_rfs->tellg() );
175 --nnIgnore;
176 }
177 }
178
179 read_one_event();
180 }
181 catch (RawFileException& e) {
182 nextFile(e).nextEvent(nnIgnore);
183 }
184
185 return m_buffer;
186}
187
188const uint32_t* RawFileReader::findEventById(uint32_t evtId)
189{
190 // not thread safe !!!
191 // find an event by ID ( only backward !!! )
192 try {
193 if ( m_idxHandler == 0 ) {
194 while ( true ) {
195 read_one_event();
196 uint32_t curEvtId = m_buffer[ m_buffer[5] + 8 ];
197 if ( curEvtId == evtId ) {
198 break;
199 }
200 }
201 }
202 else {
203 uint32_t pos = m_idxHandler->findPosById( evtId );
204 if ( pos != 0 ) {
205 m_rfs->seekg( pos );
206 read_one_event();
207 }
208 else {
209 throw ReachEndOfFile( currentFile().c_str() );
210 }
211 }
212 }
213 catch (RawFileException& e) {
214 return nextFile(e).findEventById(evtId);
215 }
216
217 return m_buffer;
218}
219
220const uint32_t* RawFileReader::roughlyNextEvent(int nIgnore, int evtByte)
221{
222 // not thread safe !!!
223 if ( evtByte == 0 ) evtByte = EstimatedEventSize;
224
225 assert( (evtByte&3) == 0 );
226
227 uint32_t prePos = m_rfs->tellg();
228 m_rfs->seekg( prePos + nIgnore*evtByte);
229
230 uint32_t halfEvtWord = evtByte / 8;
231 uint32_t halfEvtByte = halfEvtWord * 4;
232
233 while ( m_rfs->read((char*)m_buffer, halfEvtByte).good() ) {
234 uint32_t i = 0;
235 while ( i < halfEvtWord && m_buffer[i] != 0x1234cccc ) {
236 ++i;
237 }
238 if ( i < halfEvtWord ) {
239 uint32_t curPos = m_rfs->tellg();
240 m_rfs->seekg( curPos - (halfEvtWord-i)*4 );
241 read_one_event();
242 return m_buffer;
243 }
244 }
245
246 // reached the end of current data file! m_rfs->eof()
247 m_rfs->clear();
248 m_rfs->seekg( -40, std::ios::end ); //sizeof(FileEndRecord) == 40
249 uint32_t curPos = m_rfs->tellg();
250
251 int nnIgnore = nIgnore - (curPos - prePos) / evtByte;
252 if ( nnIgnore < 0 ) nnIgnore = 0;
253
254 ReachEndOfFile e( currentFile().c_str() );
255 return nextFile(e).roughlyNextEvent(nnIgnore, evtByte);
256}
258{
259 return m_rfs->runNo();
260}
261
263{
264 return m_rfs->currentFile();
265}
266
268{
269 return m_rfs->tellg();
270}
271
273{
274 uint32_t stat = 0;
275
276 if ( m_rfs->eof() ) stat |= 1;
277 if ( m_rfs->fail() ) stat |= 2;
278 if ( m_rfs->bad() ) stat |= 4;
279
280 return stat;
281}
282
283const uint32_t* RawFileReader::notSafeNextEvent()
284{
285 try {
286 read_one_event();
287 }
288 catch (RawFileException& e) {
289 nextFile(e).notSafeNextEvent();
290 }
291
292 return m_buffer;
293}
294
295void RawFileReader::read_one_event()
296{
297 (*m_rfs) >> m_dataSeparatorRecord;
298
299 uint32_t size = m_dataSeparatorRecord.getRecord().data_block_size;
300 if ( size > m_bufferSize*4 ) {
301 while ( size > m_bufferSize*4 ) {
302 m_bufferSize *= 2;
303 }
304 delete[] m_buffer;
305 m_buffer = new uint32_t[m_bufferSize];
306 }
307
308 m_rfs->read((char*)m_buffer, size);
309 if ( ! m_rfs->good() ) {
310 //std::cerr << "[RawFile] Failed to read FullEventFragment to buffer" << std::endl;
311 throw BadInputStream("event_data_block");
312 }
313}
314
315RawFileReader& RawFileReader::nextFile(RawFileException& e)
316{
317 e.print();
318
319 m_rfs->clear();
320 m_rfs->next_file();
321
322 if ( m_idxHandler != 0 ) {
323 m_idxHandler->next_file();
324 }
325
326 return *this;
327}
#define DefaultEventBufferSize
#define EstimatedEventSize
std::vector< std::string > VFileNames_t
Definition: RawFileReader.h:14
const data_separator_record & getRecord() const
Definition: RawFileUtil.h:132
static EvtIdxHandler * instance(const std::vector< std::string > &fnames)
uint32_t findPosById(uint32_t evtId)
static uint32_t IdxFileStartMarker()
Definition: EvtIdxHandler.h:13
static void release()
int nEvtLeft(int nIgnore) const
Definition: EvtIdxHandler.h:29
uint32_t nextPos(int nIgnore)
virtual void print() const
virtual ~RawFileReader()
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()
uint32_t tellg()
uint32_t runNo()
static std::vector< int > getEventNumber(const VFileNames_t &idxfnames)
uint32_t stat()
std::string currentFile()
virtual void print() const
void next_file()
static void unlock()
Definition: raw_ifstream.h:24
static void lock()
Definition: raw_ifstream.h:20
static void release()
static raw_ifstream * instance(const std::vector< std::string > &fnames)
uint32_t runNo()
std::string currentFile() const
Definition: raw_ifstream.h:29
std::vector< std::string > wildcard_correct(const std::string &fname)
Definition: RawFileTools.cxx:6