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