FACT++  1.0
DataWriteRaw.cc
Go to the documentation of this file.
1 #include "DataWriteRaw.h"
2 
3 #include "HeadersFAD.h"
4 #include "EventBuilder.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>
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 
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 
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 }
EVENT * fEvent
Definition: EventBuilder.h:132
Namespace to help with some general things in the program initialization.
Definition: Prediction.h:8
int i
Definition: db_dim_client.c:21
char str[80]
Definition: test_client.c:7
STL namespace.
bool Open(const RUN_HEAD &h, const FAD::RunDescription &d)
Definition: DataWriteRaw.cc:21
void WriteBlockHeader(uint32_t type, uint32_t ver, uint32_t cnt, uint32_t len)
Definition: DataWriteRaw.cc:8
Definition: FAD.h:92
int type
#define NPIX
Definition: BasicGlCamera.h:5
void WriteValue(const T &t)
Definition: DataWriteRaw.cc:16
bool WriteEvt(const EVT_CTRL2 &)
Definition: DataWriteRaw.cc:81
TT t
Definition: test_client.c:26
Error()
Definition: HeadersFTM.h:197
PEVNT_HEADER FADhead[NBOARDS]
Definition: FAD.h:105