1 | #include "DataWriteFits.h"
|
---|
2 |
|
---|
3 | #include "HeadersFAD.h"
|
---|
4 | #include "EventBuilder.h"
|
---|
5 | #include "Converter.h"
|
---|
6 |
|
---|
7 |
|
---|
8 | using namespace std;
|
---|
9 |
|
---|
10 | DataWriteFits::~DataWriteFits()
|
---|
11 | {
|
---|
12 | if (fFile.IsOpen())
|
---|
13 | {
|
---|
14 | WriteFooter();
|
---|
15 | fFile.Close();
|
---|
16 | }
|
---|
17 |
|
---|
18 | delete fConv;
|
---|
19 | }
|
---|
20 |
|
---|
21 | template <typename T>
|
---|
22 | void DataWriteFits::WriteKey(const string &name, const int idx, const T &value, const string &comment)
|
---|
23 | {
|
---|
24 | ostringstream str;
|
---|
25 | str << name << idx;
|
---|
26 |
|
---|
27 | ostringstream com;
|
---|
28 | com << "Board " << setw(2) << idx << ": " << comment;
|
---|
29 |
|
---|
30 | fFile.WriteKey(str.str(), value, com.str());
|
---|
31 | }
|
---|
32 |
|
---|
33 | // --------------------------------------------------------------------------
|
---|
34 | //
|
---|
35 | //! DataWriteFits constructor. This is the one that should be used, not the default one (parameter-less)
|
---|
36 | //! @param runid This parameter should probably be removed. I first thought it was the run number, but apparently it is not
|
---|
37 | //! @param h a pointer to the RUN_HEAD structure that contains the informations relative to this run
|
---|
38 | //
|
---|
39 | bool DataWriteFits::Open(const RUN_HEAD &h, const FAD::RunDescription &d)
|
---|
40 | {
|
---|
41 | if (fConv)
|
---|
42 | {
|
---|
43 | Error("DataWriteFits::Open called twice.");
|
---|
44 | return false;
|
---|
45 | }
|
---|
46 |
|
---|
47 | const int16_t realRoiTM = (h.NroiTM >= 2*h.Nroi && h.Nroi<=512) ? h.Nroi : 0;
|
---|
48 |
|
---|
49 | fFile.AddColumn('I', "EventNum");
|
---|
50 | fFile.AddColumn('I', "TriggerNum");
|
---|
51 | fFile.AddColumn('S', "TriggerType");
|
---|
52 | fFile.AddColumn('I', "NumBoards");
|
---|
53 | fFile.AddColumn('C', "Errors", 4);
|
---|
54 | fFile.AddColumn('I', "SoftTrig");
|
---|
55 | fFile.AddColumn('I', "UnixTimeUTC", 2);
|
---|
56 | fFile.AddColumn('I', "BoardTime", NBOARDS);
|
---|
57 | fFile.AddColumn('S', "StartCellData", NPIX);
|
---|
58 | fFile.AddColumn('S', "StartCellTimeMarker", NTMARK);
|
---|
59 | fFile.AddColumn('S', "Data", h.NPix*h.Nroi);
|
---|
60 | fFile.AddColumn('S', "TimeMarker", h.NTm*realRoiTM);
|
---|
61 |
|
---|
62 | // Write length of physical pipeline (1024)
|
---|
63 | fConv = new Converter(Converter::ToFormat(fFile.GetColumnTypes()));
|
---|
64 |
|
---|
65 | const size_t sz = (h.NPix*h.Nroi + h.NTm*realRoiTM)*2;
|
---|
66 | if (fConv->GetSize()-sz+4!=sizeof(EVENT))
|
---|
67 | {
|
---|
68 | ostringstream str;
|
---|
69 | str << "The EVENT structure size (" << sizeof(EVENT) << ") doesn't match the described FITS row (";
|
---|
70 | str << fConv->GetSize()-sz+4 << ")";
|
---|
71 | Error(str);
|
---|
72 | return false;
|
---|
73 | }
|
---|
74 |
|
---|
75 | //Form filename, based on runid and run-type
|
---|
76 | fFileName = FormFileName("fits");
|
---|
77 |
|
---|
78 | if (!fFile.OpenFile(fFileName))
|
---|
79 | return false;
|
---|
80 |
|
---|
81 | if (!fFile.OpenTable("Events"))
|
---|
82 | return false;
|
---|
83 |
|
---|
84 | if (!fFile.WriteDefaultKeys("fadctrl"))
|
---|
85 | return false;
|
---|
86 |
|
---|
87 | Info("==> TODO: Write sampling frequency...");
|
---|
88 |
|
---|
89 | //write header data
|
---|
90 | //first the "standard" keys
|
---|
91 | try
|
---|
92 | {
|
---|
93 | fFile.WriteKey("BLDVER", h.Version, "Builder version");
|
---|
94 | fFile.WriteKey("RUNID", GetRunId(), "Run number");
|
---|
95 | // fFile.WriteKey("RUNTYPE", h.RunType, "Type of run");
|
---|
96 | fFile.WriteKey("NBOARD", h.NBoard, "Number of acquisition boards");
|
---|
97 | fFile.WriteKey("NPIX", h.NPix, "Number of pixels");
|
---|
98 | fFile.WriteKey("NTMARK", h.NTm, "Number of time marker channels");
|
---|
99 | fFile.WriteKey("NCELLS", 1024, "Maximum number of slices per pixels");
|
---|
100 | fFile.WriteKey("NROI", h.Nroi, "Number of slices per pixels");
|
---|
101 | fFile.WriteKey("NROITM", realRoiTM, "Number of slices per time-marker");
|
---|
102 |
|
---|
103 | const uint16_t realOffset = (h.NroiTM > h.Nroi) ? h.NroiTM - 2*h.Nroi : 0;
|
---|
104 | fFile.WriteKey("TMSHIFT", realOffset, "Shift of the start of the time marker readout wrt to data");
|
---|
105 |
|
---|
106 | //FIXME should we also put the start and stop time of the received data ?
|
---|
107 | //now the events header related variables
|
---|
108 | fFile.WriteKey("CAMERA", "MGeomCamFACT", "");
|
---|
109 | fFile.WriteKey("DAQ", "DRS4", "");
|
---|
110 | fFile.WriteKey("ADCRANGE", 2000, "Dynamic range in mV");
|
---|
111 | fFile.WriteKey("ADC", 12, "Resolution in bits");
|
---|
112 | fFile.WriteKey("RUNTYPE", d.name, "File type according to FAD configuration");
|
---|
113 |
|
---|
114 | // Write a single key for:
|
---|
115 | // -----------------------
|
---|
116 | // Start package flag
|
---|
117 | // package length
|
---|
118 | // version number
|
---|
119 | // status
|
---|
120 | // Prescaler
|
---|
121 |
|
---|
122 | // Write 40 kays for (?)
|
---|
123 | // Phaseshift
|
---|
124 | // DAC
|
---|
125 |
|
---|
126 | for (int i=0; i<h.NBoard; i++)
|
---|
127 | {
|
---|
128 | const PEVNT_HEADER &hh = h.FADhead[i];
|
---|
129 |
|
---|
130 | // Header values whihc won't change during the run
|
---|
131 | WriteKey("ID", i, hh.board_id, "Board ID");
|
---|
132 | WriteKey("FWVER", i, hh.version_no, "Firmware Version");
|
---|
133 |
|
---|
134 | ostringstream dna;
|
---|
135 | dna << "0x" << hex << hh.DNA;
|
---|
136 | WriteKey("DNA", i, dna.str(), "Unique FPGA device identifier (DNA)");
|
---|
137 | }
|
---|
138 |
|
---|
139 | // FIXME: Calculate average ref clock frequency
|
---|
140 | for (int i=0; i<h.NBoard; i++)
|
---|
141 | {
|
---|
142 | const PEVNT_HEADER &hh = h.FADhead[i];
|
---|
143 |
|
---|
144 | if (hh.start_package_flag==0)
|
---|
145 | continue;
|
---|
146 |
|
---|
147 | fFile.WriteKey("BOARD", i, "Board number for RUN, PRESC, PHASE and DAC");
|
---|
148 | // fFile.WriteKey("RUN", hh.runnumber, "Run number");
|
---|
149 | fFile.WriteKey("PRESC", hh.trigger_generator_prescaler, "Trigger generator prescaler");
|
---|
150 | fFile.WriteKey("PHASE", (int16_t)hh.adc_clock_phase_shift, "ADC clock phase shift");
|
---|
151 |
|
---|
152 | for (int j=0; j<8; j++)
|
---|
153 | {
|
---|
154 | ostringstream dac, cmt;
|
---|
155 | dac << "DAC" << j;
|
---|
156 | cmt << "Command value for " << dac.str();
|
---|
157 | fFile.WriteKey(dac.str(), hh.dac[j], cmt.str());
|
---|
158 | }
|
---|
159 |
|
---|
160 | break;
|
---|
161 | }
|
---|
162 |
|
---|
163 | double avg = 0;
|
---|
164 | int cnt = 0;
|
---|
165 | for (int i=0; i<h.NBoard; i++)
|
---|
166 | {
|
---|
167 | const PEVNT_HEADER &hh = h.FADhead[i];
|
---|
168 |
|
---|
169 | if (hh.start_package_flag==0)
|
---|
170 | continue;
|
---|
171 |
|
---|
172 | avg += hh.REFCLK_frequency;
|
---|
173 | cnt ++;
|
---|
174 | }
|
---|
175 |
|
---|
176 | // FIXME: I cannot write a double! WHY?
|
---|
177 | fFile.WriteKey("REFCLK", avg/cnt*2.048, "Average reference clock frequency in Hz");
|
---|
178 |
|
---|
179 | fFile.WriteKey("DRSCALIB", GetDrsStep()>=0, "This file belongs to a DRS calibration");
|
---|
180 | if (GetDrsStep()>=0)
|
---|
181 | fFile.WriteKey("DRSSTEP", GetDrsStep(), "Step of the DRS calibration");
|
---|
182 |
|
---|
183 | }
|
---|
184 | catch (const CCfits::FitsException &e)
|
---|
185 | {
|
---|
186 | Error("CCfits::Table::addKey failed in '"+fFileName+"': "+e.message());
|
---|
187 | return false;
|
---|
188 | }
|
---|
189 |
|
---|
190 | // This is a workaround for the missing information in the event builder
|
---|
191 | fTstart[0] = 0;
|
---|
192 | fTstop[0] = 0;
|
---|
193 |
|
---|
194 | fTriggerCounter.fill(0);
|
---|
195 |
|
---|
196 | //Last but not least, add header keys that will be updated when closing the file
|
---|
197 | return WriteFooter();
|
---|
198 | }
|
---|
199 |
|
---|
200 | // --------------------------------------------------------------------------
|
---|
201 | //
|
---|
202 | //! This writes one event to the file
|
---|
203 | //! @param e the pointer to the EVENT
|
---|
204 | //
|
---|
205 | bool DataWriteFits::WriteEvt(const EVT_CTRL2 &evt)
|
---|
206 | {
|
---|
207 | if (!fFile.AddRow())
|
---|
208 | return false;
|
---|
209 |
|
---|
210 | fTriggerCounter = evt.runCtrl->triggerCounter;
|
---|
211 |
|
---|
212 | const EVENT &e = *evt.fEvent;
|
---|
213 |
|
---|
214 | memcpy(fTstop, &e.PCTime, 2*sizeof(uint32_t));
|
---|
215 | if (fTstart[0]==0)
|
---|
216 | memcpy(fTstart, fTstop, 2*sizeof(uint32_t));
|
---|
217 |
|
---|
218 | const int realRoiTM = (e.RoiTM > e.Roi) ? e.Roi : 0;
|
---|
219 | const size_t sz = sizeof(EVENT) + sizeof(e.StartPix)*e.Roi+sizeof(e.StartTM)*realRoiTM; //ETIENNE from RoiTm to Roi
|
---|
220 |
|
---|
221 | const vector<char> data = fConv->ToFits(reinterpret_cast<const char*>(&e)+4, sz-4);
|
---|
222 |
|
---|
223 | return fFile.WriteData(data.data(), data.size());
|
---|
224 | }
|
---|
225 |
|
---|
226 | bool DataWriteFits::WriteFooter()
|
---|
227 | {
|
---|
228 | try
|
---|
229 | {
|
---|
230 | /*
|
---|
231 | fFile.WriteKey("NBEVTOK", rt ? rt->nEventsOk : uint32_t(0),
|
---|
232 | "How many events were written");
|
---|
233 |
|
---|
234 | fFile.WriteKey("NBEVTREJ", rt ? rt->nEventsRej : uint32_t(0),
|
---|
235 | "How many events were rejected by SW-trig");
|
---|
236 |
|
---|
237 | fFile.WriteKey("NBEVTBAD", rt ? rt->nEventsBad : uint32_t(0),
|
---|
238 | "How many events were rejected by Error");
|
---|
239 | */
|
---|
240 |
|
---|
241 | //FIXME shouldn't we convert start and stop time to MjD first ?
|
---|
242 | //FIXME shouldn't we also add an MjD reference ?
|
---|
243 |
|
---|
244 | const Time start(fTstart[0], fTstart[1]);
|
---|
245 | const Time stop (fTstop[0], fTstop[1]);
|
---|
246 |
|
---|
247 | fFile.WriteKey("TSTARTI", uint32_t(floor(start.UnixDate())),
|
---|
248 | "Time when first event received (integral part)");
|
---|
249 | fFile.WriteKey("TSTARTF", fmod(start.UnixDate(), 1),
|
---|
250 | "Time when first event received (fractional part)");
|
---|
251 | fFile.WriteKey("TSTOPI", uint32_t(floor(stop.UnixDate())),
|
---|
252 | "Time when last event received (integral part)");
|
---|
253 | fFile.WriteKey("TSTOPF", fmod(stop.UnixDate(), 1),
|
---|
254 | "Time when last event received (fractional part)");
|
---|
255 | fFile.WriteKey("DATE-OBS", start.Iso(),
|
---|
256 | "Time when first event received");
|
---|
257 | fFile.WriteKey("DATE-END", stop.Iso(),
|
---|
258 | "Time when last event received");
|
---|
259 |
|
---|
260 | fFile.WriteKey("NTRG", fTriggerCounter[0], "Number of physics triggers written");
|
---|
261 | fFile.WriteKey("NTRGPED", fTriggerCounter[1], "Number of pure pedestal triggers written");
|
---|
262 | fFile.WriteKey("NTRGLPE", fTriggerCounter[2], "Number of external light pulser triggers written");
|
---|
263 | fFile.WriteKey("NTRGTIM", fTriggerCounter[3], "Number of time calibration triggers written");
|
---|
264 | fFile.WriteKey("NTRGLPI", fTriggerCounter[4], "Number of internal light pulser triggers written");
|
---|
265 | fFile.WriteKey("NTRGEXT1", fTriggerCounter[5], "Number of triggers from ext1 written");
|
---|
266 | fFile.WriteKey("NTRGEXT2", fTriggerCounter[6], "Number of triggers from ext2 written");
|
---|
267 | fFile.WriteKey("NTRGMISC", fTriggerCounter[7], "Number of triggers belonging to no other category written");
|
---|
268 | }
|
---|
269 | catch (const CCfits::FitsException &e)
|
---|
270 | {
|
---|
271 | Error("CCfits::Table::addKey failed in '"+fFile.GetName()+"': "+e.message());
|
---|
272 | return false;
|
---|
273 | }
|
---|
274 | return true;
|
---|
275 | }
|
---|
276 |
|
---|
277 | // --------------------------------------------------------------------------
|
---|
278 | //
|
---|
279 | //! Closes the file, and before this it write the TAIL data
|
---|
280 | //! @param rt the pointer to the RUN_TAIL data structure
|
---|
281 | //
|
---|
282 | bool DataWriteFits::Close(const EVT_CTRL2 &evt)
|
---|
283 | {
|
---|
284 | if (!fFile.IsOpen())
|
---|
285 | return false;
|
---|
286 |
|
---|
287 | // Just for safety
|
---|
288 | fTriggerCounter = evt.triggerCounter;
|
---|
289 | WriteFooter();
|
---|
290 |
|
---|
291 | fFile.Close();
|
---|
292 |
|
---|
293 | return true;
|
---|
294 | }
|
---|