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