source: trunk/FACT++/src/DataWriteRaw.cc@ 17689

Last change on this file since 17689 was 17330, checked in by tbretz, 11 years ago
Give access to the full information in Open/Write/Close
File size: 2.9 KB
Line 
1#include "DataWriteRaw.h"
2
3#include "HeadersFAD.h"
4#include "EventBuilder.h"
5
6using namespace std;
7
8void 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
15template<typename T>
16void DataWriteRaw::WriteValue(const T &t)
17{
18 fOut.write(reinterpret_cast<const char*>(&t), sizeof(T));
19}
20
21bool 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
81bool DataWriteRaw::WriteEvt(const EVT_CTRL2 &evt)
82{
83 const EVENT &e = *evt.fEvent;
84
85 const int sh = sizeof(EVENT)-2 + NPIX*e.Roi*2;
86
87 WriteBlockHeader(kEvent, 1, fCounter++, sh);
88 fOut.write(reinterpret_cast<const char*>(&e)+2, sh);
89 return true;
90}
91
92bool DataWriteRaw::Close()
93{
94 WriteBlockHeader(kEndOfFile, 0, 0, 0);
95
96 /*
97 if (tail)
98 {
99 fOut.seekp(fPosTail);
100
101 WriteValue(uint32_t(1));
102 fOut.write(reinterpret_cast<const char*>(tail), sizeof(RUN_TAIL));
103 }*/
104
105 if (!fOut)
106 {
107 ostringstream str;
108
109 str << "ofstream::write() failed for '" << GetFileName() << "': " << strerror(errno) << " [errno=" << errno << "]";
110 Error(str);
111
112 return false;
113 }
114
115 fOut.close();
116
117 if (!fOut)
118 {
119 ostringstream str;
120 str << "ofstream::close() failed for '" << GetFileName() << "': " << strerror(errno) << " [errno=" << errno << "]";
121 Error(str);
122
123 return false;
124 }
125
126 return true;
127}
Note: See TracBrowser for help on using the repository browser.