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

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