1 | #include "DataWriteRaw.h"
|
---|
2 |
|
---|
3 | #include "FAD.h"
|
---|
4 |
|
---|
5 | using namespace std;
|
---|
6 |
|
---|
7 | void DataWriteRaw::WriteBlockHeader(uint32_t type, uint32_t ver, uint32_t cnt, uint32_t len)
|
---|
8 | {
|
---|
9 | const uint32_t val[4] = { type, ver, cnt, len };
|
---|
10 |
|
---|
11 | fOut.write(reinterpret_cast<const char*>(val), sizeof(val));
|
---|
12 | }
|
---|
13 |
|
---|
14 | template<typename T>
|
---|
15 | void DataWriteRaw::WriteValue(const T &t)
|
---|
16 | {
|
---|
17 | fOut.write(reinterpret_cast<const char*>(&t), sizeof(T));
|
---|
18 | }
|
---|
19 |
|
---|
20 | bool DataWriteRaw::Open(RUN_HEAD *h)
|
---|
21 | {
|
---|
22 | const string name = FormFileName("bin");
|
---|
23 | if (access(name.c_str(), F_OK)==0)
|
---|
24 | {
|
---|
25 | Error("File '"+name+"' already exists.");
|
---|
26 | return false;
|
---|
27 | }
|
---|
28 |
|
---|
29 | fFileName = name;
|
---|
30 |
|
---|
31 | errno = 0;
|
---|
32 | fOut.open(name.c_str(), ios_base::out);
|
---|
33 | if (!fOut)
|
---|
34 | {
|
---|
35 | ostringstream str;
|
---|
36 | str << "ofstream::open() failed for '" << name << "': " << strerror(errno) << " [errno=" << errno << "]";
|
---|
37 | Error(str);
|
---|
38 |
|
---|
39 | return false;
|
---|
40 | }
|
---|
41 |
|
---|
42 | fCounter = 0;
|
---|
43 |
|
---|
44 | static uint32_t FACT = 0xFAC77e1e;
|
---|
45 |
|
---|
46 | fOut.write(reinterpret_cast<char*>(&FACT), 4);
|
---|
47 |
|
---|
48 | WriteBlockHeader(kIdentifier, 1, 0, 8);
|
---|
49 | WriteValue(uint32_t(0));
|
---|
50 | WriteValue(GetRunId());
|
---|
51 |
|
---|
52 | WriteBlockHeader(kRunHeader, 1, 0, sizeof(RUN_HEAD)-sizeof(PEVNT_HEADER*));
|
---|
53 | fOut.write(reinterpret_cast<char*>(h), sizeof(RUN_HEAD)-sizeof(PEVNT_HEADER*));
|
---|
54 |
|
---|
55 | for (int i=0; i<40; i++)
|
---|
56 | {
|
---|
57 | WriteBlockHeader(kBoardHeader, 1, i, sizeof(PEVNT_HEADER));
|
---|
58 | fOut.write(reinterpret_cast<char*>(h->FADhead+i), sizeof(PEVNT_HEADER));
|
---|
59 | }
|
---|
60 |
|
---|
61 | // FIXME: Split this
|
---|
62 | const vector<char> block(sizeof(uint32_t)+sizeof(RUN_TAIL));
|
---|
63 | WriteBlockHeader(kRunSummary, 1, 0, block.size());
|
---|
64 |
|
---|
65 | fPosTail = fOut.tellp();
|
---|
66 | fOut.write(block.data(), block.size());
|
---|
67 |
|
---|
68 | if (!fOut)
|
---|
69 | {
|
---|
70 | ostringstream str;
|
---|
71 | str << "ofstream::write() failed for '" << name << "': " << strerror(errno) << " [errno=" << errno << "]";
|
---|
72 | Error(str);
|
---|
73 |
|
---|
74 | return false;
|
---|
75 | }
|
---|
76 |
|
---|
77 | return true;
|
---|
78 | }
|
---|
79 |
|
---|
80 | bool DataWriteRaw::WriteEvt(EVENT *evt)
|
---|
81 | {
|
---|
82 | const int sh = sizeof(EVENT)-2 + NPIX*evt->Roi*2;
|
---|
83 |
|
---|
84 | WriteBlockHeader(kEvent, 1, fCounter++, sh);
|
---|
85 | fOut.write(reinterpret_cast<char*>(evt)+2, sh);
|
---|
86 | return true;
|
---|
87 | }
|
---|
88 |
|
---|
89 | bool DataWriteRaw::Close(RUN_TAIL *tail)
|
---|
90 | {
|
---|
91 | WriteBlockHeader(kEndOfFile, 0, 0, 0);
|
---|
92 |
|
---|
93 | if (tail)
|
---|
94 | {
|
---|
95 | fOut.seekp(fPosTail);
|
---|
96 |
|
---|
97 | WriteValue(uint32_t(1));
|
---|
98 | fOut.write(reinterpret_cast<char*>(tail), sizeof(RUN_TAIL));
|
---|
99 | }
|
---|
100 |
|
---|
101 | if (!fOut)
|
---|
102 | {
|
---|
103 | ostringstream str;
|
---|
104 |
|
---|
105 | str << "ofstream::write() failed for '" << GetFileName() << "': " << strerror(errno) << " [errno=" << errno << "]";
|
---|
106 | Error(str);
|
---|
107 |
|
---|
108 | return false;
|
---|
109 | }
|
---|
110 |
|
---|
111 | fOut.close();
|
---|
112 |
|
---|
113 | if (!fOut)
|
---|
114 | {
|
---|
115 | ostringstream str;
|
---|
116 | str << "ofstream::close() failed for '" << GetFileName() << "': " << strerror(errno) << " [errno=" << errno << "]";
|
---|
117 | Error(str);
|
---|
118 |
|
---|
119 | return false;
|
---|
120 | }
|
---|
121 |
|
---|
122 | return true;
|
---|
123 | }
|
---|