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