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

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