source: trunk/FACT++/src/DataWriteFits.cc@ 12555

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