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

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