BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
RawFileUtil.cxx
Go to the documentation of this file.
1#include "RawFile/RawFileUtil.h"
2#include "IRawFile/RawFileExceptions.h"
3#include "RawFile/raw_ifstream.h"
4#include "RawFile/raw_ofstream.h"
5#include <cstring>
6
8 // read data block
9 if ( is.read( (char*)( &record.m_record ), sizeof( file_start_record ) ).fail() )
10 {
11 // std::cerr << "[RawFile] Error occurred while reading files" << std::endl;
12 throw BadInputStream( "FileStartRecord" );
13 }
14
15 // data validation checks
16 if ( record.m_record.marker != 0x1234aaaa )
17 {
18 // std::cerr << "[RawFile] Get an invalid record marker" << std::endl;
19 throw WrongMarker( 0x1234aaaa, record.m_record.marker );
20 }
21
22 if ( record.m_record.record_size != 8 )
23 {
24 // std::cerr << "[RawFile] Get an unexpected record size" << std::endl;
25 throw UnexpectedRecordSize( "FileStartRecord", 8, record.m_record.record_size );
26 }
27
28 return is;
29}
30
32 // read marker
33 if ( is.read( (char*)( &record.m_record.marker ), sizeof( uint32_t ) ).fail() )
34 {
35 // std::cerr << "[RawFile] Error occurred while reading files" << std::endl;
36 throw BadInputStream( "FileNameStrings::marker" );
37 }
38
39 // marker validation
40 if ( record.m_record.marker != 0x1234aabb )
41 {
42 // std::cerr << "[RawFile] Get an invalid record marker" << std::endl;
43 throw WrongMarker( 0x1234aabb, record.m_record.marker );
44 }
45
46 // read length and strings
47 if ( is.read( (char*)( &record.m_record.length1 ), sizeof( uint32_t ) ).fail() )
48 {
49 // std::cerr << "[RawFile] Error occurred while reading files" << std::endl;
50 throw BadInputStream( "FileNameStrings::length1" );
51 }
52
53 uint32_t length1_word = ( record.m_record.length1 + 3 ) / 4;
54 char* appName = new char[length1_word * 4 + 1];
55 if ( is.read( appName, length1_word * 4 ).fail() )
56 {
57 // std::cerr << "[RawFile] Error occurred while reading files" << std::endl;
58 throw BadInputStream( "FileNameStrings::appName" );
59 }
60 appName[length1_word * 4] = '\0';
61 record.m_appName = appName;
62 delete[] appName;
63
64 if ( is.read( (char*)( &record.m_record.length2 ), sizeof( uint32_t ) ).fail() )
65 {
66 // std::cerr << "[RawFile] Error occurred while reading files" << std::endl;
67 throw BadInputStream( "FileNameStrings::length2" );
68 }
69
70 uint32_t length2_word = ( record.m_record.length2 + 3 ) / 4;
71 char* usrTag = new char[length2_word * 4 + 1];
72 if ( is.read( usrTag, length2_word * 4 ).fail() )
73 {
74 // std::cerr << "[RawFile] Error occurred while reading files" << std::endl;
75 throw BadInputStream( "FileNameStrings::usrTag" );
76 }
77 usrTag[length2_word * 4] = '\0';
78 record.m_usrTag = usrTag;
79 delete[] usrTag;
80
81 return is;
82}
83
85 // read data block
86 if ( is.read( (char*)( &record.m_record ), sizeof( run_parameters_record ) ).fail() )
87 {
88 // std::cerr << "[RawFile] Error occurred while reading files" << std::endl;
89 throw BadInputStream( "RunParametersRecord" );
90 }
91
92 // data validation checks
93 if ( record.m_record.marker != 0x1234bbbb )
94 {
95 // std::cerr << "[RawFile] Get an invalid record marker" << std::endl;
96 throw WrongMarker( 0x1234bbbb, record.m_record.marker );
97 }
98
99 if ( record.m_record.record_size != 9 )
100 {
101 // std::cerr << "[RawFile] Get an unexpected record size" << std::endl;
102 throw UnexpectedRecordSize( "RunParametersRecord", 9, record.m_record.record_size );
103 }
104
105 return is;
106}
107
109 // read data block
110 if ( is.read( (char*)( &record.m_record ), sizeof( data_separator_record ) ).fail() )
111 {
112 // std::cerr << "[RawFile] Error occurred while reading file" << std::endl;
113 throw BadInputStream( "DataSeparatorRecord" );
114 // throw ReachEndOfFile();
115 }
116
117 // data validation checks
118 if ( record.m_record.marker != 0x1234cccc )
119 {
120 if ( record.m_record.marker == 0x1234dddd )
121 { throw ReachEndOfFile( is.currentFile().c_str() ); }
122 // std::cerr << "[RawFile] Get an invalid record marker" << std::endl;
123 throw WrongMarker( 0x1234cccc, record.m_record.marker );
124 }
125
126 if ( record.m_record.record_size != 4 )
127 {
128 // std::cerr << "[RawFile] Get an unexpected record size" << std::endl;
129 throw UnexpectedRecordSize( "DataSeparatorRecord", 4, record.m_record.record_size );
130 }
131
132 return is;
133}
134
136 // read data block
137 if ( is.read( (char*)( &record.m_record ), sizeof( file_end_record ) ).fail() )
138 {
139 // std::cerr << "[RawFile] Error occurred while reading files" << std::endl;
140 throw BadInputStream( "FileEndRecord" );
141 }
142
143 // data validation checks
144 if ( record.m_record.marker != 0x1234dddd )
145 {
146 // std::cerr << "[RawFile] Get an invalid record marker" << std::endl;
147 throw WrongMarker( 0x1234dddd, record.m_record.marker );
148 }
149 if ( record.m_record.end_marker != 0x1234eeee )
150 {
151 // std::cerr << "[RawFile] Get an invalid record marker" << std::endl;
152 throw WrongMarker( 0x1234eeee, record.m_record.end_marker );
153 }
154
155 if ( record.m_record.record_size != 10 )
156 {
157 // std::cerr << "[RawFile] Get an unexpected record size" << std::endl;
158 throw UnexpectedRecordSize( "FileEndRecord", 10, record.m_record.record_size );
159 }
160
161 return is;
162}
163
165 FileEndRecord& record ) {
166 // copy file_end_record data that already read by data_separator_record
167 memcpy( (void*)&record.m_record, (const void*)&( sep.getRecord() ),
168 sizeof( data_separator_record ) );
169 // read data block
170 if ( is.read( (char*)( &record.m_record.events_in_file ),
171 ( sizeof( file_end_record ) - sizeof( data_separator_record ) ) )
172 .fail() )
173 {
174 // std::cerr << "[RawFile] Error occurred while reading files" << std::endl;
175 throw BadInputStream( "FileEndRecord" );
176 }
177
178 // data validation checks
179 if ( record.m_record.marker != 0x1234dddd )
180 {
181 // std::cerr << "[RawFile] Get an invalid record marker" << std::endl;
182 throw WrongMarker( 0x1234dddd, record.m_record.marker );
183 }
184 if ( record.m_record.end_marker != 0x1234eeee )
185 {
186 // std::cerr << "[RawFile] Get an invalid record marker" << std::endl;
187 throw WrongMarker( 0x1234eeee, record.m_record.end_marker );
188 }
189
190 if ( record.m_record.record_size != 10 )
191 {
192 // std::cerr << "[RawFile] Get an unexpected record size" << std::endl;
193 throw UnexpectedRecordSize( "FileEndRecord", 10, record.m_record.record_size );
194 }
195
196 return is;
197}
198
199std::ofstream& operator<<( std::ofstream& os, FileStartRecord& record ) {
200 // write data block
201 if ( os.write( (char*)( &record.m_record ), sizeof( file_start_record ) ).fail() )
202 {
203 std::cerr << "[RawFile] Error occurred while writing file" << std::endl;
204 throw FailedToWrite( "FileStartRecord" );
205 }
206
207 return os;
208}
209
210std::ofstream& operator<<( std::ofstream& os, FileNameStrings& record ) {
211 // write data block
212 os.write( (char*)( &record.m_record ), sizeof( uint32_t ) * 2 );
213 uint32_t sizebyte = record.m_record.length1;
214 uint32_t sizeword = ( sizebyte + 3 ) / 4;
215 os.write( record.m_appName.c_str(), sizeword * 4 );
216 os.write( (char*)( &record.m_record.length2 ), sizeof( uint32_t ) );
217 sizebyte = record.m_record.length2;
218 sizeword = ( sizebyte + 3 ) / 4;
219 os.write( record.m_usrTag.c_str(), sizeword * 4 );
220 if ( os.fail() )
221 {
222 std::cerr << "[RawFile] Error occurred while writing file" << std::endl;
223 throw FailedToWrite( "FileNameStrings" );
224 }
225 return os;
226}
227
228std::ofstream& operator<<( std::ofstream& os, RunParametersRecord& record ) {
229 // write data block
230 if ( os.write( (char*)( &record.m_record ), sizeof( run_parameters_record ) ).fail() )
231 {
232 std::cerr << "[RawFile] Error occurred while writing file" << std::endl;
233 throw FailedToWrite( "RunParametersRecord" );
234 }
235
236 return os;
237}
238
239std::ofstream& operator<<( std::ofstream& os, DataSeparatorRecord& record ) {
240 // write data block
241 if ( os.write( (char*)( &record.m_record ), sizeof( data_separator_record ) ).fail() )
242 {
243 std::cerr << "[RawFile] Error occurred while writing file" << std::endl;
244 throw FailedToWrite( "DataSeparatorRecord" );
245 }
246
247 return os;
248}
249
250std::ofstream& operator<<( std::ofstream& os, FileEndRecord& record ) {
251 // write data block
252 if ( os.write( (char*)( &record.m_record ), sizeof( file_end_record ) ).fail() )
253 {
254 std::cerr << "[RawFile] Error occurred while writing file" << std::endl;
255 throw FailedToWrite( "FileEndRecord" );
256 }
257
258 return os;
259}
260
262 m_record.marker = 0x1234aaaa;
263 m_record.record_size = 8;
264 // following members are not meaningful @ offline
265 m_record.version = 0;
266 m_record.file_number = 0;
267 m_record.date = 0;
268 m_record.time = 0;
269 m_record.sizeLimit_dataBlocks = 0;
270 m_record.sizeLimit_MB = 0;
271}
272
274 m_record.marker = 0x1234aabb;
275 m_record.length1 = 4;
276 m_record.length2 = 7;
277 m_appName = "BOSS";
278 m_usrTag = "offline";
279}
280
282 m_record.marker = 0x1234bbbb;
283 m_record.record_size = 9;
284 // following members are not meaningful @ offline
285 m_record.run_number = 0;
286 m_record.max_events = 0;
287 m_record.rec_enable = 0;
288 m_record.trigger_type = 0;
289 m_record.detector_mask = 0;
290 m_record.beam_type = 0;
291 m_record.beam_energy = 0;
292}
293
295 m_record.marker = 0x1234cccc;
296 m_record.record_size = 4;
297 // m_record.data_block_number; //set manually
298 // m_record.data_block_size; //set manually
299}
300
302 m_record.marker = 0x1234dddd;
303 m_record.record_size = 10;
304 // following members are not meaningful @ offline
305 m_record.date = 0;
306 m_record.time = 0;
307 // m_record.events_in_file; //set manually
308 m_record.data_in_file = 0;
309 m_record.events_in_run = 0;
310 m_record.data_in_run = 0;
311 m_record.status = 1;
312 m_record.end_marker = 0x1234eeee;
313}
314
315void FileStartRecord::dump( std::ostream& os ) const {
316 os << "[RawFile] FileStartRecord:" << std::endl
317 << std::hex << "[RawFile] \tmarker : 0x" << m_record.marker << std::endl
318 << "[RawFile] \trecord_size : 0x" << m_record.record_size << std::endl
319 << "[RawFile] \tversion : 0x" << m_record.version << std::endl
320 << "[RawFile] \tfile_number : 0x" << m_record.file_number << std::endl
321 << "[RawFile] \tdate : 0x" << m_record.date << std::endl
322 << "[RawFile] \ttime : 0x" << m_record.time << std::endl
323 << "[RawFile] \tsizeLimit_dataBlocks : 0x" << m_record.sizeLimit_dataBlocks << std::endl
324 << "[RawFile] \tsizeLimit_MB : 0x" << m_record.sizeLimit_MB << std::dec
325 << std::endl;
326}
327
328void FileNameStrings::dump( std::ostream& os ) const {
329 os << "[RawFile] FileNameStrings:" << std::endl
330 << std::hex << "[RawFile] \tmarker : 0x" << m_record.marker << std::endl
331 << "[RawFile] \tAppName length : 0x" << m_record.length1 << std::endl
332 << "[RawFile] \tAppName : " << m_appName << std::endl
333 << "[RawFile] \tUsrTag length : 0x" << m_record.length2 << std::endl
334 << "[RawFile] \tUsrTag : " << m_usrTag << std::dec << std::endl;
335}
336
337void RunParametersRecord::dump( std::ostream& os ) const {
338 os << "[RawFile] RunParametersRecord:" << std::endl
339 << std::hex << "[RawFile] \tmarker : 0x" << m_record.marker << std::endl
340 << "[RawFile] \trecord_size : 0x" << m_record.record_size << std::endl
341 << "[RawFile] \trun_number : 0x" << m_record.run_number << std::endl
342 << "[RawFile] \tmax_events : 0x" << m_record.max_events << std::endl
343 << "[RawFile] \trec_enable : 0x" << m_record.rec_enable << std::endl
344 << "[RawFile] \ttrigger_type : 0x" << m_record.trigger_type << std::endl
345 << "[RawFile] \tdetector_mask : 0x" << m_record.detector_mask << std::endl
346 << "[RawFile] \tbeam_type : 0x" << m_record.beam_type << std::endl
347 << "[RawFile] \tbeam_energy : 0x" << m_record.beam_energy << std::dec
348 << std::endl;
349}
350
351void DataSeparatorRecord::dump( std::ostream& os ) const {
352 os << "[RawFile] DataSeparatorRecord:" << std::endl
353 << std::hex << "[RawFile] \tmarker : 0x" << m_record.marker << std::endl
354 << "[RawFile] \trecord_size : 0x" << m_record.record_size << std::endl
355 << "[RawFile] \tdata_block_number : 0x" << m_record.data_block_number << std::endl
356 << "[RawFile] \tdata_block_size : 0x" << m_record.data_block_size << std::dec
357 << std::endl;
358}
359
360void FileEndRecord::dump( std::ostream& os ) const {
361 os << "[RawFile] FileEndRecord:" << std::endl
362 << std::hex << "[RawFile] \tmarker : 0x" << m_record.marker << std::endl
363 << "[RawFile] \trecord_size : 0x" << m_record.record_size << std::endl
364 << "[RawFile] \tdate : 0x" << m_record.date << std::endl
365 << "[RawFile] \ttime : 0x" << m_record.time << std::endl
366 << "[RawFile] \tevents_in_file : 0x" << m_record.events_in_file << std::endl
367 << "[RawFile] \tdata_in_file : 0x" << m_record.data_in_file << std::endl
368 << "[RawFile] \tevents_in_run : 0x" << m_record.events_in_run << std::endl
369 << "[RawFile] \tdata_in_run : 0x" << m_record.data_in_run << std::endl
370 << "[RawFile] \tstatus : 0x" << m_record.status << std::endl
371 << "[RawFile] \tend_marker : 0x" << m_record.end_marker << std::dec
372 << std::endl;
373}
raw_ifstream & makeEndRecord(DataSeparatorRecord &sep, raw_ifstream &is, FileEndRecord &record)
raw_ifstream & operator>>(raw_ifstream &is, FileStartRecord &record)
std::ofstream & operator<<(std::ofstream &os, FileStartRecord &record)
const data_separator_record & getRecord() const
void dump(std::ostream &os=std::cout) const
void dump(std::ostream &os=std::cout) const
void dump(std::ostream &os=std::cout) const
void dump(std::ostream &os=std::cout) const
void dump(std::ostream &os=std::cout) const