1 | #include "DataWriteFits.h"
|
---|
2 |
|
---|
3 | #include "FAD.h"
|
---|
4 | #include "Converter.h"
|
---|
5 |
|
---|
6 | using namespace std;
|
---|
7 |
|
---|
8 | DataWriteFits::~DataWriteFits()
|
---|
9 | {
|
---|
10 | Close();
|
---|
11 | delete fConv;
|
---|
12 | }
|
---|
13 |
|
---|
14 | template <typename T>
|
---|
15 | void DataWriteFits::WriteKey(const string &name, const int idx, const T &value, const string &comment)
|
---|
16 | {
|
---|
17 | ostringstream str;
|
---|
18 | str << name << idx;
|
---|
19 |
|
---|
20 | ostringstream com;
|
---|
21 | com << "Board " << setw(2) << idx << ": " << comment;
|
---|
22 |
|
---|
23 | fFile.WriteKey(str.str(), value, com.str());
|
---|
24 | }
|
---|
25 |
|
---|
26 | // --------------------------------------------------------------------------
|
---|
27 | //
|
---|
28 | //! DataWriteFits constructor. This is the one that should be used, not the default one (parameter-less)
|
---|
29 | //! @param runid This parameter should probably be removed. I first thought it was the run number, but apparently it is not
|
---|
30 | //! @param h a pointer to the RUN_HEAD structure that contains the informations relative to this run
|
---|
31 | //
|
---|
32 | bool DataWriteFits::Open(RUN_HEAD* h)
|
---|
33 | {
|
---|
34 | if (fConv)
|
---|
35 | {
|
---|
36 | Error("DataWriteFits::Open called twice.");
|
---|
37 | return false;
|
---|
38 | }
|
---|
39 |
|
---|
40 | fFile.AddColumn('I', "EventNum");
|
---|
41 | fFile.AddColumn('I', "TriggerNum");
|
---|
42 | fFile.AddColumn('S', "TriggerType");
|
---|
43 | fFile.AddColumn('I', "NumBoards");
|
---|
44 | fFile.AddColumn('C', "Errors", 4);
|
---|
45 | fFile.AddColumn('I', "SoftTrig");
|
---|
46 | fFile.AddColumn('I', "UnixTimeUTC", 2);
|
---|
47 | fFile.AddColumn('I', "BoardTime", NBOARDS);
|
---|
48 | fFile.AddColumn('S', "StartCellData", NPIX);
|
---|
49 | fFile.AddColumn('S', "StartCellTimeMarker", NTMARK);
|
---|
50 | fFile.AddColumn('S', "Data", h->NPix*h->Nroi);
|
---|
51 | fFile.AddColumn('S', "TimeMarker", h->NTm*h->NroiTM);
|
---|
52 |
|
---|
53 | // Write length of physical pipeline (1024)
|
---|
54 |
|
---|
55 | fConv = new Converter(Converter::ToFormat(fFile.GetColumnTypes()));
|
---|
56 |
|
---|
57 | const size_t sz = (h->NPix*h->Nroi + h->NTm*h->NroiTM)*2;
|
---|
58 | if (fConv->GetSize()-sz+4!=sizeof(EVENT))
|
---|
59 | {
|
---|
60 | ostringstream str;
|
---|
61 | str << "The EVENT structure size (" << sizeof(EVENT) << ") doesn't match the described FITS row (";
|
---|
62 | str << fConv->GetSize()-sz+4 << ")";
|
---|
63 | Error(str);
|
---|
64 | return false;
|
---|
65 | }
|
---|
66 |
|
---|
67 | //Form filename, based on runid and run-type
|
---|
68 | fFileName = FormFileName("fits");
|
---|
69 |
|
---|
70 | if (!fFile.OpenFile(fFileName))
|
---|
71 | return false;
|
---|
72 |
|
---|
73 | if (!fFile.OpenTable("Events"))
|
---|
74 | return false;
|
---|
75 |
|
---|
76 | if (!fFile.WriteDefaultKeys("fadctrl"))
|
---|
77 | return false;
|
---|
78 |
|
---|
79 | Info("==> TODO: Write sampling frequency...");
|
---|
80 |
|
---|
81 | //write header data
|
---|
82 | //first the "standard" keys
|
---|
83 | try
|
---|
84 | {
|
---|
85 | fFile.WriteKey("BLDVER", h->Version, "Builder version");
|
---|
86 | fFile.WriteKey("RUNID", GetRunId(), "Run number");
|
---|
87 | fFile.WriteKey("RUNTYPE", h->RunType, "Type of run");
|
---|
88 | fFile.WriteKey("NBOARD", h->NBoard, "Number of acquisition boards");
|
---|
89 | fFile.WriteKey("NPIX", h->NPix, "Number of pixels");
|
---|
90 | fFile.WriteKey("NTMARK", h->NTm, "Number of Time marks");
|
---|
91 | fFile.WriteKey("NROI", h->Nroi, "Number of slices per pixels");
|
---|
92 | fFile.WriteKey("NROITM", h->NroiTM, "Number of slices per time-marker");
|
---|
93 |
|
---|
94 | //FIXME should we also put the start and stop time of the received data ?
|
---|
95 | //now the events header related variables
|
---|
96 | fFile.WriteKey("CAMERA", "MGeomCamFACT", "");
|
---|
97 | fFile.WriteKey("DAQ", "DRS4", "");
|
---|
98 | fFile.WriteKey("ADCRANGE", 2000, "Dynamic range in mV");
|
---|
99 | fFile.WriteKey("ADC", 12, "Resolution in bits");
|
---|
100 |
|
---|
101 |
|
---|
102 | // Write a single key for:
|
---|
103 | // -----------------------
|
---|
104 | // Start package flag
|
---|
105 | // package length
|
---|
106 | // version number
|
---|
107 | // status
|
---|
108 | // Prescaler
|
---|
109 |
|
---|
110 | // Write 40 kays for (?)
|
---|
111 | // Phaseshift
|
---|
112 | // DAC
|
---|
113 |
|
---|
114 | for (int i=0; i<h->NBoard; i++)
|
---|
115 | {
|
---|
116 | const PEVNT_HEADER &hh = h->FADhead[i];
|
---|
117 |
|
---|
118 | // Header values whihc won't change during the run
|
---|
119 | WriteKey("ID", i, hh.board_id, "Board ID");
|
---|
120 | WriteKey("DNA", i, hh.DNA, "DNA");
|
---|
121 | WriteKey("FWVER", i, hh.version_no, "Firmware Version");
|
---|
122 | }
|
---|
123 |
|
---|
124 | // FIXME: Calculate average ref clock frequency
|
---|
125 | for (int i=0; i<h->NBoard; i++)
|
---|
126 | {
|
---|
127 | const PEVNT_HEADER &hh = h->FADhead[i];
|
---|
128 |
|
---|
129 | if (hh.start_package_flag==0)
|
---|
130 | continue;
|
---|
131 |
|
---|
132 | fFile.WriteKey("BOARD", i, "Board number for RUN, PRESC, PHASE and DAC");
|
---|
133 | fFile.WriteKey("RUN", hh.runnumber, "Run number");
|
---|
134 | fFile.WriteKey("PRESC", hh.trigger_generator_prescaler, "Trigger generator prescaler");
|
---|
135 | fFile.WriteKey("PHASE", (int16_t)hh.adc_clock_phase_shift, "ADC clock phase shift");
|
---|
136 |
|
---|
137 | for (int j=0; j<8; j++)
|
---|
138 | {
|
---|
139 | ostringstream dac, cmt;
|
---|
140 | dac << "DAC" << j;
|
---|
141 | cmt << "Command value for " << dac.str();
|
---|
142 | fFile.WriteKey(dac.str(), hh.dac[j], cmt.str());
|
---|
143 | }
|
---|
144 |
|
---|
145 | break;
|
---|
146 | }
|
---|
147 |
|
---|
148 | double avg = 0;
|
---|
149 | int cnt = 0;
|
---|
150 | for (int i=0; i<h->NBoard; i++)
|
---|
151 | {
|
---|
152 | const PEVNT_HEADER &hh = h->FADhead[i];
|
---|
153 |
|
---|
154 | if (hh.start_package_flag==0)
|
---|
155 | continue;
|
---|
156 |
|
---|
157 | avg += hh.REFCLK_frequency;
|
---|
158 | cnt ++;
|
---|
159 | }
|
---|
160 |
|
---|
161 | // FIXME: I cannot write a double! WHY?
|
---|
162 | fFile.WriteKey("REFCLK", avg/cnt, "Average reference clock frequency in Hz");
|
---|
163 | }
|
---|
164 | catch (const CCfits::FitsException &e)
|
---|
165 | {
|
---|
166 | Error("CCfits::Table::addKey failed in '"+fFileName+"': "+e.message());
|
---|
167 | return false;
|
---|
168 | }
|
---|
169 |
|
---|
170 | //Last but not least, add header keys that will be updated when closing the file
|
---|
171 | return WriteFooter(NULL);
|
---|
172 | }
|
---|
173 |
|
---|
174 | // --------------------------------------------------------------------------
|
---|
175 | //
|
---|
176 | //! This writes one event to the file
|
---|
177 | //! @param e the pointer to the EVENT
|
---|
178 | //
|
---|
179 | bool DataWriteFits::WriteEvt(EVENT *e)
|
---|
180 | {
|
---|
181 | if (!fFile.AddRow())
|
---|
182 | return false;
|
---|
183 |
|
---|
184 | const size_t sz = sizeof(EVENT) + sizeof(e->StartPix)*e->Roi+sizeof(e->StartTM)*e->RoiTM;
|
---|
185 |
|
---|
186 | const vector<char> data = fConv->ToFits(reinterpret_cast<char*>(e)+4, sz-4);
|
---|
187 |
|
---|
188 | return fFile.WriteData(data.data(), data.size());
|
---|
189 | }
|
---|
190 |
|
---|
191 | bool DataWriteFits::WriteFooter(RUN_TAIL *rt)
|
---|
192 | {
|
---|
193 | try
|
---|
194 | {
|
---|
195 | /*
|
---|
196 | fFile.WriteKey("NBEVTOK", rt ? rt->nEventsOk : uint32_t(0),
|
---|
197 | "How many events were written");
|
---|
198 |
|
---|
199 | fFile.WriteKey("NBEVTREJ", rt ? rt->nEventsRej : uint32_t(0),
|
---|
200 | "How many events were rejected by SW-trig");
|
---|
201 |
|
---|
202 | fFile.WriteKey("NBEVTBAD", rt ? rt->nEventsBad : uint32_t(0),
|
---|
203 | "How many events were rejected by Error");
|
---|
204 | */
|
---|
205 |
|
---|
206 | //FIXME shouldn't we convert start and stop time to MjD first ?
|
---|
207 | //FIXME shouldn't we also add an MjD reference ?
|
---|
208 |
|
---|
209 | fFile.WriteKey("TSTART", rt ? rt->PCtime0 : uint32_t(0),
|
---|
210 | "Time when first event received");
|
---|
211 |
|
---|
212 | fFile.WriteKey("TSTOP", rt ? rt->PCtimeX : uint32_t(0),
|
---|
213 | "Time when last event received");
|
---|
214 | }
|
---|
215 | catch (const CCfits::FitsException &e)
|
---|
216 | {
|
---|
217 | Error("CCfits::Table::addKey failed in '"+fFile.GetName()+"': "+e.message());
|
---|
218 | return false;
|
---|
219 | }
|
---|
220 | return true;
|
---|
221 | }
|
---|
222 |
|
---|
223 | // --------------------------------------------------------------------------
|
---|
224 | //
|
---|
225 | //! Closes the file, and before this it write the TAIL data
|
---|
226 | //! @param rt the pointer to the RUN_TAIL data structure
|
---|
227 | //
|
---|
228 | bool DataWriteFits::Close(RUN_TAIL *rt)
|
---|
229 | {
|
---|
230 | if (!fFile.IsOpen())
|
---|
231 | return false;
|
---|
232 |
|
---|
233 | WriteFooter(rt);
|
---|
234 |
|
---|
235 | fFile.Close();
|
---|
236 |
|
---|
237 | return true;
|
---|
238 | }
|
---|