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

Last change on this file since 14819 was 14550, checked in by tbretz, 12 years ago
Some of the units were wrong.
File size: 12.0 KB
Line 
1#include "DataWriteFits2.h"
2
3#include "HeadersFAD.h"
4#include "FAD.h"
5
6using namespace std;
7
8void DataWriteFits2::WriteDefaultKeys(ofits &file)
9{
10 const Time now;
11 file.SetStr( "TELESCOP", "FACT", "Telescope that acquired this data");
12 file.SetStr( "PACKAGE", PACKAGE_NAME, "Package name");
13 file.SetStr( "VERSION", PACKAGE_VERSION, "Package description");
14 file.SetStr( "CREATOR", "fadctrl", "Program that wrote this file");
15 file.SetFloat("EXTREL", 1.0, "Release Number");
16 file.SetStr( "COMPILED", __DATE__" "__TIME__, "Compile time");
17 file.SetStr( "REVISION", REVISION, "SVN revision");
18 file.SetStr( "ORIGIN", "FACT", "Institution that wrote the file");
19 file.SetStr( "DATE", now.Iso(), "File creation date");
20 file.SetInt( "NIGHT", now.NightAsInt(), "Night as int");
21 file.SetStr( "TIMESYS", "UTC", "Time system");
22 file.SetStr( "TIMEUNIT", "d", "Time given in days w.r.t. to MJDREF");
23 file.SetInt( "MJDREF", 40587, "MJD to UNIX time (seconds since 1970/1/1)");
24}
25
26void DataWriteFits2::WriteHeader(const RUN_HEAD* h, const FAD::RunDescription &d)
27{
28 const int16_t realRoiTM = (h->NroiTM >= 2*h->Nroi && h->Nroi<=512) ? h->Nroi : 0;
29
30 fFile.AddColumnInt("EventNum", "uint32", "FAD board event counter");
31 fFile.AddColumnInt("TriggerNum", "uint32", "FTM board trigger counter");
32 fFile.AddColumnShort("TriggerType", "uint16", "FTM board trigger type");
33 fFile.AddColumnInt("NumBoards", "uint32", "Number of connected boards");
34 fFile.AddColumnByte(4, "Errors", "uint8", "Communication error counters");
35 fFile.AddColumnInt("SoftTrig", "", "(not yet implemented)");
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 // This is a workaround for the missing information in the event builder
145 fTstart[0] = 0;
146 fTstop[0] = 0;
147
148 memset(fTriggerCounter.data(), 0, fTriggerCounter.size()*sizeof(uint32_t));
149
150 //Last but not least, add header keys that will be updated when closing the file
151 WriteFooter(NULL);
152
153 fFile.WriteTableHeader("Events");
154};
155
156// --------------------------------------------------------------------------
157//
158//! DataWriteFits constructor. This is the one that should be used, not the default one (parameter-less)
159//! @param runid This parameter should probably be removed. I first thought it was the run number, but apparently it is not
160//! @param h a pointer to the RUN_HEAD structure that contains the informations relative to this run
161//
162bool DataWriteFits2::Open(const RUN_HEAD* h, const FAD::RunDescription &d)
163{
164 //Form filename, based on runid and run-type
165 fFileName = FormFileName("fits");
166
167 try
168 {
169 fFile.open(fFileName.c_str());
170 }
171 catch (const exception &e)
172 {
173 Error("ofits::open() failed for '"+fFileName+"': "+e.what());
174 return false;
175 }
176
177 if (!fFile)
178 {
179 ostringstream str;
180 str << "ofstream::open() failed for '" << fFileName << "': " << strerror(errno) << " [errno=" << errno << "]";
181 Error(str);
182 return false;
183 }
184
185 try
186 {
187 WriteHeader(h, d);
188 }
189 catch (const exception &e)
190 {
191 Error("ofits - Writing header failed for '"+fFileName+"': "+e.what());
192 return false;
193 }
194
195 if (!fFile)
196 {
197 ostringstream str;
198 str << "ofstream::write() failed for '" << fFileName << "': " << strerror(errno) << " [errno=" << errno << "]";
199 Error(str);
200 return false;
201 }
202
203 return true;
204}
205
206// --------------------------------------------------------------------------
207//
208//! This writes one event to the file
209//! @param e the pointer to the EVENT
210//
211bool DataWriteFits2::WriteEvt(EVENT *e)
212{
213 if (e->TriggerType && !(e->TriggerType & FAD::EventHeader::kAll))
214 fTriggerCounter[0]++;
215 else if ((e->TriggerType&FAD::EventHeader::kPedestal) && !(e->TriggerType&FAD::EventHeader::kTIM))
216 fTriggerCounter[1]++;
217 else if (e->TriggerType & FAD::EventHeader::kLPext)
218 fTriggerCounter[2]++;
219 else if (e->TriggerType & (FAD::EventHeader::kTIM|FAD::EventHeader::kPedestal))
220 fTriggerCounter[3]++;
221 else if (e->TriggerType & FAD::EventHeader::kLPint)
222 fTriggerCounter[4]++;
223 else if (e->TriggerType & FAD::EventHeader::kExt1)
224 fTriggerCounter[5]++;
225 else if (e->TriggerType & FAD::EventHeader::kExt2)
226 fTriggerCounter[6]++;
227 else
228 fTriggerCounter[7]++;
229
230 memcpy(fTstop, &e->PCTime, 2*sizeof(uint32_t));
231 if (fTstart[0]==0)
232 memcpy(fTstart, fTstop, 2*sizeof(uint32_t));
233
234 const int realRoiTM = (e->RoiTM > e->Roi) ? e->Roi : 0;
235 const size_t sz = sizeof(EVENT) + sizeof(e->StartPix)*e->Roi+sizeof(e->StartTM)*realRoiTM; //ETIENNE from RoiTm to Roi
236
237 try
238 {
239 fFile.WriteRow(reinterpret_cast<char*>(e)+4, sz-4);
240 }
241 catch (const exception &ex)
242 {
243 Error("ofits::WriteRow failed for '"+fFileName+"': "+ex.what());
244 return false;
245 }
246
247 if (!fFile)
248 {
249 ostringstream str;
250 str << "fstream::write() failed for '" << fFileName << "': " << strerror(errno) << " [errno=" << errno << "]";
251 Error(str);
252 return false;
253 }
254
255 return true;
256}
257
258void DataWriteFits2::WriteFooter(RUN_TAIL */*rt*/)
259{
260 //FIXME shouldn't we convert start and stop time to MjD first ?
261 //FIXME shouldn't we also add an MjD reference ?
262
263 const Time start(fTstart[0], fTstart[1]);
264 const Time stop (fTstop[0], fTstop[1]);
265
266 fFile.SetInt("TSTARTI", uint32_t(floor(start.UnixDate())),
267 "Time when first evt received (integral part)");
268 fFile.SetFloat("TSTARTF", fmod(start.UnixDate(), 1),
269 "Time when first evt received (fractional part)");
270 fFile.SetInt("TSTOPI", uint32_t(floor(stop.UnixDate())),
271 "Time when last evt received (integral part)");
272 fFile.SetFloat("TSTOPF", fmod(stop.UnixDate(), 1),
273 "Time when last evt received (fractional part)");
274 fFile.SetStr("DATE-OBS", start.Iso(),
275 "Time when first event received");
276 fFile.SetStr("DATE-END", stop.Iso(),
277 "Time when last event received");
278
279 fFile.SetInt("NTRG", fTriggerCounter[0], "No of physics triggers written");
280 fFile.SetInt("NTRGPED", fTriggerCounter[1], "No of pure pedestal triggers written");
281 fFile.SetInt("NTRGLPE", fTriggerCounter[2], "No of external light pulser triggers written");
282 fFile.SetInt("NTRGTIM", fTriggerCounter[3], "No of time calibration triggers written");
283 fFile.SetInt("NTRGLPI", fTriggerCounter[4], "No of internal light pulser triggers written");
284 fFile.SetInt("NTRGEXT1", fTriggerCounter[5], "No of triggers from ext1 written");
285 fFile.SetInt("NTRGEXT2", fTriggerCounter[6], "No of triggers from ext2 written");
286 fFile.SetInt("NTRGMISC", fTriggerCounter[7], "No of all other triggers");
287}
288
289// --------------------------------------------------------------------------
290//
291//! Closes the file, and before this it write the TAIL data
292//! @param rt the pointer to the RUN_TAIL data structure
293//
294bool DataWriteFits2::Close(RUN_TAIL *rt)
295{
296 if (!fFile.is_open())
297 {
298 Error("DataWriteFits2::Close() called but file '"+fFileName+"' not open.");
299 return false;
300 }
301
302 try
303 {
304 WriteFooter(rt);
305 }
306 catch (const exception &e)
307 {
308 Error("ofits - Setting footer key values failed for '"+fFileName+"': "+e.what());
309 return false;
310 }
311
312 try
313 {
314 fFile.close();
315 }
316 catch (const exception &e)
317 {
318 Error("ofits::close() failed for '"+fFileName+"': "+e.what());
319 return false;
320 }
321
322 if (!fFile)
323 {
324 ostringstream str;
325 str << "ofstream::close() failed for '" << fFileName << "': " << strerror(errno) << " [errno=" << errno << "]";
326 Error(str);
327 return false;
328 }
329
330 return true;
331}
Note: See TracBrowser for help on using the repository browser.