source: trunk/FACT++/src/DataWriteFits2.cc@ 17050

Last change on this file since 17050 was 16585, checked in by tbretz, 11 years ago
Removed Errors and SoftTrig
File size: 11.9 KB
Line 
1#include "DataWriteFits2.h"
2
3#include <boost/filesystem.hpp>
4
5#include "HeadersFAD.h"
6#include "FAD.h"
7
8using namespace std;
9
10void DataWriteFits2::WriteDefaultKeys(ofits &file)
11{
12 const Time now;
13 file.SetStr( "TELESCOP", "FACT", "Telescope that acquired this data");
14 file.SetStr( "PACKAGE", PACKAGE_NAME, "Package name");
15 file.SetStr( "VERSION", PACKAGE_VERSION, "Package description");
16 file.SetStr( "CREATOR", "fadctrl", "Program that wrote this file");
17 file.SetFloat("EXTREL", 1.0, "Release Number");
18 file.SetStr( "COMPILED", __DATE__" "__TIME__, "Compile time");
19 file.SetStr( "REVISION", REVISION, "SVN revision");
20 file.SetStr( "ORIGIN", "FACT", "Institution that wrote the file");
21 file.SetStr( "DATE", now.Iso(), "File creation date");
22 file.SetInt( "NIGHT", now.NightAsInt(), "Night as int");
23 file.SetStr( "TIMESYS", "UTC", "Time system");
24 file.SetStr( "TIMEUNIT", "d", "Time given in days w.r.t. to MJDREF");
25 file.SetInt( "MJDREF", 40587, "MJD to UNIX time (seconds since 1970/1/1)");
26}
27
28void DataWriteFits2::WriteHeader(const RUN_HEAD &h, const FAD::RunDescription &d)
29{
30 const int16_t realRoiTM = (h.NroiTM >= 2*h.Nroi && h.Nroi<=512) ? h.Nroi : 0;
31
32 fFile.AddColumnInt("EventNum", "uint32", "FAD board event counter");
33 fFile.AddColumnInt("TriggerNum", "uint32", "FTM board trigger counter");
34 fFile.AddColumnShort("TriggerType", "uint16", "FTM board trigger type");
35 fFile.AddColumnInt("NumBoards", "uint32", "Number of connected boards");
36 fFile.AddColumnInt(2, "UnixTimeUTC", "uint32", "Unix time seconds and microseconds");
37 fFile.AddColumnInt(NBOARDS, "BoardTime", "uint32", "Board internal time counter");
38 fFile.AddColumnShort(NPIX, "StartCellData", "uint16", "DRS4 start cell of readout");
39 fFile.AddColumnShort(NTMARK, "StartCellTimeMarker", "uint16", "DRS4 start cell of readout time marker");
40 fFile.AddColumnShort(h.NPix*h.Nroi, "Data", "int16", "Digitized data");
41 fFile.AddColumnShort(h.NTm*realRoiTM, "TimeMarker", "int16", "Digitized time marker - if available");
42
43 const size_t sz = (h.NPix*h.Nroi + h.NTm*realRoiTM)*2;
44 if (fFile.GetBytesPerRow()-sz+4!=sizeof(EVENT))
45 {
46 ostringstream str;
47 str << "The EVENT structure size (" << sizeof(EVENT) << ") doesn't match the described FITS row (";
48 str << fFile.GetBytesPerRow()-sz+4 << ")";
49 throw runtime_error(str.str());
50 }
51
52 // =============== Default keys for all files ================
53 WriteDefaultKeys(fFile);
54
55 // ================ Header keys for raw-data =================
56 fFile.SetInt("BLDVER", h.Version, "Builder version");
57 fFile.SetInt("RUNID", GetRunId(), "Run number");
58 fFile.SetInt("NBOARD", h.NBoard, "Number of acquisition boards");
59 fFile.SetInt("NPIX", h.NPix, "Number of pixels");
60 fFile.SetInt("NTMARK", h.NTm, "Number of time marker channels");
61 fFile.SetInt("NCELLS", 1024, "Maximum number of slices per pixels");
62 fFile.SetInt("NROI", h.Nroi, "Number of slices per pixels");
63 fFile.SetInt("NROITM", realRoiTM, "Number of slices per time-marker");
64
65 const uint16_t realOffset = (h.NroiTM > h.Nroi) ? h.NroiTM - 2*h.Nroi : 0;
66 fFile.SetInt("TMSHIFT", realOffset, "Shift of marker readout w.r.t. to data");
67
68 //FIXME should we also put the start and stop time of the received data ?
69 //now the events header related variables
70 fFile.SetStr("CAMERA", "MGeomCamFACT", "MARS camera geometry class");
71 fFile.SetStr("DAQ", "DRS4", "Data acquisition type");
72 fFile.SetInt("ADCRANGE", 2000, "Dynamic range in mV");
73 fFile.SetInt("ADC", 12, "Resolution in bits");
74 fFile.SetStr("RUNTYPE", d.name, "File type according to FAD configuration");
75
76 // Write a single key for:
77 // -----------------------
78 // Start package flag
79 // package length
80 // version number
81 // status
82 // Prescaler
83
84 // Write 40 kays for (?)
85 // Phaseshift
86 // DAC
87
88 for (int i=0; i<h.NBoard; i++)
89 {
90 const PEVNT_HEADER &hh = h.FADhead[i];
91
92 ostringstream sout;
93 sout << "Board " << setw(2) << i<< ": ";
94
95 // Header values whihc won't change during the run
96 fFile.SetInt("ID", hh.board_id, sout.str()+"Board ID");
97 fFile.SetInt("FWVER", hh.version_no, sout.str()+"Firmware Version");
98 fFile.SetHex("DNA", hh.DNA, sout.str()+"Unique FPGA device identifier (DNA)");
99 }
100
101 // FIXME: Calculate average ref clock frequency
102 for (int i=0; i<h.NBoard; i++)
103 {
104 const PEVNT_HEADER &hh = h.FADhead[i];
105
106 if (hh.start_package_flag==0)
107 continue;
108
109 fFile.SetInt("BOARD", i, "Board number for RUN, PRESC, PHASE and DAC");
110 fFile.SetInt("PRESC", hh.trigger_generator_prescaler, "Trigger generator prescaler");
111 fFile.SetInt("PHASE", (int16_t)hh.adc_clock_phase_shift, "ADC clock phase shift");
112
113 for (int j=0; j<8; j++)
114 {
115 ostringstream dac, cmt;
116 dac << "DAC" << j;
117 cmt << "Command value for " << dac.str();
118 fFile.SetInt(dac.str(), hh.dac[j], cmt.str());
119 }
120
121 break;
122 }
123
124 double avg = 0;
125 int cnt = 0;
126 for (int i=0; i<h.NBoard; i++)
127 {
128 const PEVNT_HEADER &hh = h.FADhead[i];
129
130 if (hh.start_package_flag==0)
131 continue;
132
133 avg += hh.REFCLK_frequency;
134 cnt ++;
135 }
136
137 // FIXME: I cannot write a double! WHY?
138 fFile.SetFloat("REFCLK", avg/cnt*2.048, "Average reference clock frequency in Hz");
139
140 fFile.SetBool("DRSCALIB", GetDrsStep()>=0, "This file belongs to a DRS calibration");
141 if (GetDrsStep()>=0)
142 fFile.SetInt("DRSSTEP", GetDrsStep(), "Step of the DRS calibration");
143
144 fTstart[0] = h.RunTime;
145 fTstart[1] = h.RunUsec;
146
147 fTstop[0] = 0;
148 fTstop[1] = 0;
149
150 memset(fTriggerCounter.data(), 0, fTriggerCounter.size()*sizeof(uint32_t));
151
152 //Last but not least, add header keys that will be updated when closing the file
153 WriteFooter(NULL);
154
155 fFile.WriteTableHeader("Events");
156};
157
158// --------------------------------------------------------------------------
159//
160//! DataWriteFits constructor. This is the one that should be used, not the default one (parameter-less)
161//! @param runid This parameter should probably be removed. I first thought it was the run number, but apparently it is not
162//! @param h a pointer to the RUN_HEAD structure that contains the informations relative to this run
163//
164bool DataWriteFits2::Open(const RUN_HEAD &h, const FAD::RunDescription &d)
165{
166 //Form filename, based on runid and run-type
167 fFileName = FormFileName("fits");
168
169 if (boost::filesystem::exists(fFileName))
170 {
171 Error("ofits - file '"+fFileName+"' already exists.");
172 return false;
173 }
174
175 try
176 {
177 fFile.open(fFileName.c_str());
178 }
179 catch (const exception &e)
180 {
181 Error("ofits::open() failed for '"+fFileName+"': "+e.what());
182 return false;
183 }
184
185 if (!fFile)
186 {
187 ostringstream str;
188 str << "ofstream::open() failed for '" << fFileName << "': " << strerror(errno) << " [errno=" << errno << "]";
189 Error(str);
190 return false;
191 }
192
193 try
194 {
195 WriteHeader(h, d);
196 }
197 catch (const exception &e)
198 {
199 Error("ofits - Writing header failed for '"+fFileName+"': "+e.what());
200 return false;
201 }
202
203 if (!fFile)
204 {
205 ostringstream str;
206 str << "ofstream::write() failed for '" << fFileName << "': " << strerror(errno) << " [errno=" << errno << "]";
207 Error(str);
208 return false;
209 }
210
211 return true;
212}
213
214// --------------------------------------------------------------------------
215//
216//! This writes one event to the file
217//! @param e the pointer to the EVENT
218//
219bool DataWriteFits2::WriteEvt(const EVENT &e)
220{
221 if (e.TriggerType && !(e.TriggerType & FAD::EventHeader::kAll))
222 fTriggerCounter[0]++;
223 else if ((e.TriggerType&FAD::EventHeader::kPedestal) && !(e.TriggerType&FAD::EventHeader::kTIM))
224 fTriggerCounter[1]++;
225 else if (e.TriggerType & FAD::EventHeader::kLPext)
226 fTriggerCounter[2]++;
227 else if (e.TriggerType & (FAD::EventHeader::kTIM|FAD::EventHeader::kPedestal))
228 fTriggerCounter[3]++;
229 else if (e.TriggerType & FAD::EventHeader::kLPint)
230 fTriggerCounter[4]++;
231 else if (e.TriggerType & FAD::EventHeader::kExt1)
232 fTriggerCounter[5]++;
233 else if (e.TriggerType & FAD::EventHeader::kExt2)
234 fTriggerCounter[6]++;
235 else
236 fTriggerCounter[7]++;
237
238 fTstop[0] = e.PCTime;
239 fTstop[1] = e.PCUsec;
240
241 const int realRoiTM = (e.RoiTM > e.Roi) ? e.Roi : 0;
242 const size_t sz = sizeof(EVENT) + sizeof(e.StartPix)*e.Roi+sizeof(e.StartTM)*realRoiTM; //ETIENNE from RoiTm to Roi
243
244 try
245 {
246 fFile.WriteRow(reinterpret_cast<const char*>(&e)+4, sz-4);
247 }
248 catch (const exception &ex)
249 {
250 Error("ofits::WriteRow failed for '"+fFileName+"': "+ex.what());
251 return false;
252 }
253
254 if (!fFile)
255 {
256 ostringstream str;
257 str << "fstream::write() failed for '" << fFileName << "': " << strerror(errno) << " [errno=" << errno << "]";
258 Error(str);
259 return false;
260 }
261
262 return true;
263}
264
265void DataWriteFits2::WriteFooter(const RUN_TAIL */*rt*/)
266{
267 //FIXME shouldn't we convert start and stop time to MjD first ?
268 //FIXME shouldn't we also add an MjD reference ?
269
270 const Time start(fTstart[0], fTstart[1]);
271 const Time stop (fTstop[0], fTstop[1]);
272
273 fFile.SetInt("TSTARTI", uint32_t(floor(start.UnixDate())),
274 "Time when first evt received (integral part)");
275 fFile.SetFloat("TSTARTF", fmod(start.UnixDate(), 1),
276 "Time when first evt received (fractional part)");
277 fFile.SetInt("TSTOPI", uint32_t(floor(stop.UnixDate())),
278 "Time when last evt received (integral part)");
279 fFile.SetFloat("TSTOPF", fmod(stop.UnixDate(), 1),
280 "Time when last evt received (fractional part)");
281 fFile.SetStr("DATE-OBS", start.Iso(),
282 "Time when first event received");
283 fFile.SetStr("DATE-END", stop.Iso(),
284 "Time when last event received");
285
286 fFile.SetInt("NTRG", fTriggerCounter[0], "No of physics triggers written");
287 fFile.SetInt("NTRGPED", fTriggerCounter[1], "No of pure pedestal triggers written");
288 fFile.SetInt("NTRGLPE", fTriggerCounter[2], "No of external light pulser triggers written");
289 fFile.SetInt("NTRGTIM", fTriggerCounter[3], "No of time calibration triggers written");
290 fFile.SetInt("NTRGLPI", fTriggerCounter[4], "No of internal light pulser triggers written");
291 fFile.SetInt("NTRGEXT1", fTriggerCounter[5], "No of triggers from ext1 written");
292 fFile.SetInt("NTRGEXT2", fTriggerCounter[6], "No of triggers from ext2 written");
293 fFile.SetInt("NTRGMISC", fTriggerCounter[7], "No of all other triggers");
294}
295
296// --------------------------------------------------------------------------
297//
298//! Closes the file, and before this it write the TAIL data
299//! @param rt the pointer to the RUN_TAIL data structure
300//
301bool DataWriteFits2::Close(const RUN_TAIL *rt)
302{
303 if (!fFile.is_open())
304 {
305 Error("DataWriteFits2::Close() called but file '"+fFileName+"' not open.");
306 return false;
307 }
308
309 try
310 {
311 WriteFooter(rt);
312 }
313 catch (const exception &e)
314 {
315 Error("ofits - Setting footer key values failed for '"+fFileName+"': "+e.what());
316 return false;
317 }
318
319 try
320 {
321 fFile.close();
322 }
323 catch (const exception &e)
324 {
325 Error("ofits::close() failed for '"+fFileName+"': "+e.what());
326 return false;
327 }
328
329 if (!fFile)
330 {
331 ostringstream str;
332 str << "ofstream::close() failed for '" << fFileName << "': " << strerror(errno) << " [errno=" << errno << "]";
333 Error(str);
334 return false;
335 }
336
337 return true;
338}
Note: See TracBrowser for help on using the repository browser.