1 | #ifndef FACT_EventBuilderWrapper
|
---|
2 | #define FACT_EventBuilderWrapper
|
---|
3 |
|
---|
4 | /*
|
---|
5 | #if BOOST_VERSION < 104400
|
---|
6 | #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 4))
|
---|
7 | #undef BOOST_HAS_RVALUE_REFS
|
---|
8 | #endif
|
---|
9 | #endif
|
---|
10 | #include <boost/thread.hpp>
|
---|
11 |
|
---|
12 | using namespace std;
|
---|
13 | */
|
---|
14 |
|
---|
15 | #include <boost/date_time/posix_time/posix_time_types.hpp>
|
---|
16 |
|
---|
17 | #include <CCfits/CCfits>
|
---|
18 |
|
---|
19 | #include "EventBuilder.h"
|
---|
20 |
|
---|
21 | extern "C" {
|
---|
22 | extern void StartEvtBuild();
|
---|
23 | extern int CloseRunFile(uint32_t runId, uint32_t closeTime);
|
---|
24 | }
|
---|
25 |
|
---|
26 | class DataFileImp
|
---|
27 | {
|
---|
28 | uint32_t fRunId;
|
---|
29 |
|
---|
30 | public:
|
---|
31 | DataFileImp(uint32_t id) : fRunId(id) { }
|
---|
32 |
|
---|
33 | virtual bool OpenFile(RUN_HEAD* h) = 0;
|
---|
34 | virtual bool Write(EVENT *) = 0;
|
---|
35 | virtual bool Close(RUN_TAIL * = 0) = 0;
|
---|
36 |
|
---|
37 | uint32_t GetRunId() const { return fRunId; }
|
---|
38 |
|
---|
39 | // --------------------------------------------------------------------------
|
---|
40 | //
|
---|
41 | //! This creates an appropriate file name for a particular run number and type
|
---|
42 | //! @param runNumber the run number for which a filename is to be created
|
---|
43 | //! @param runType an int describing the kind of run. 0=Data, 1=Pedestal, 2=Calibration, 3=Calibrated data
|
---|
44 | //! @param extension a string containing the extension to be appened to the file name
|
---|
45 | //
|
---|
46 | string FormFileName(uint32_t runType, string extension)
|
---|
47 | {
|
---|
48 | //TODO where am I supposed to get the base directory from ?
|
---|
49 | //TODO also, for creating subsequent directories, should I use the functions from the dataLogger ?
|
---|
50 | string baseDirectory = "./Run";
|
---|
51 |
|
---|
52 | ostringstream result;
|
---|
53 | // result << baseDirectory;
|
---|
54 | // result << Time::fmt("/%Y/%m/%d/") << (Time() - boost::posix_time::time_duration(12,0,0));
|
---|
55 | result << setfill('0') << setw(8) << fRunId;
|
---|
56 | result << ".001_";
|
---|
57 | switch (runType)
|
---|
58 | {
|
---|
59 | case -1:
|
---|
60 | result << 'T';
|
---|
61 | break;
|
---|
62 | case 0:
|
---|
63 | result << 'D';
|
---|
64 | break;
|
---|
65 | case 1:
|
---|
66 | result << 'P';
|
---|
67 | break;
|
---|
68 | case 2:
|
---|
69 | result << 'C';
|
---|
70 | break;
|
---|
71 | case 3:
|
---|
72 | result << 'N';
|
---|
73 | break;
|
---|
74 | default:
|
---|
75 | result << runType;
|
---|
76 | };
|
---|
77 | result << "." << extension;
|
---|
78 |
|
---|
79 | return result.str();
|
---|
80 | }
|
---|
81 |
|
---|
82 | };
|
---|
83 |
|
---|
84 |
|
---|
85 | #include "FAD.h"
|
---|
86 |
|
---|
87 | class DataFileRaw : public DataFileImp
|
---|
88 | {
|
---|
89 | ofstream fOut;
|
---|
90 |
|
---|
91 | off_t fPosTail;
|
---|
92 |
|
---|
93 | uint32_t fCounter;
|
---|
94 |
|
---|
95 |
|
---|
96 | // WRITE uint32_t 0xFAC77e1e (FACT Tele)
|
---|
97 | // ===
|
---|
98 | // WRITE uint32_t TYPE(>0) == 1
|
---|
99 | // WRITE uint32_t ID(>0) == 0
|
---|
100 | // WRITE uint32_t VERSION(>0) == 1
|
---|
101 | // WRITE uint32_t LENGTH
|
---|
102 | // -
|
---|
103 | // WRITE uint32_t TELESCOPE ID
|
---|
104 | // WRITE uint32_t RUNID
|
---|
105 | // ===
|
---|
106 | // WRITE uint32_t TYPE(>0) == 2
|
---|
107 | // WRITE uint32_t ID(>0) == 0
|
---|
108 | // WRITE uint32_t VERSION(>0) == 1
|
---|
109 | // WRITE uint32_t LENGTH
|
---|
110 | // -
|
---|
111 | // WRITE HEADER
|
---|
112 | // ===
|
---|
113 | // [ 40 TIMES
|
---|
114 | // WRITE uint32_t TYPE(>0) == 3
|
---|
115 | // WRITE uint32_t ID(>0) == 0..39
|
---|
116 | // WRITE uint32_t VERSION(>0) == 1
|
---|
117 | // WRITE uint32_t LENGTH
|
---|
118 | // -
|
---|
119 | // WRITE BOARD-HEADER
|
---|
120 | // ]
|
---|
121 | // ===
|
---|
122 | // WRITE uint32_t TYPE(>0) == 4
|
---|
123 | // WRITE uint32_t ID(>0) == 0
|
---|
124 | // WRITE uint32_t VERSION(>0) == 1
|
---|
125 | // WRITE uint32_t LENGTH
|
---|
126 | // -
|
---|
127 | // WRITE FOOTER (empty)
|
---|
128 | // ===
|
---|
129 | // [ N times
|
---|
130 | // WRITE uint32_t TYPE(>0) == 10
|
---|
131 | // WRITE uint32_t ID(>0) == counter
|
---|
132 | // WRITE uint32_t VERSION(>0) == 1
|
---|
133 | // WRITE uint32_t LENGTH HEADER
|
---|
134 | // -
|
---|
135 | // WRITE HEADER+DATA
|
---|
136 | // ]
|
---|
137 | // ===
|
---|
138 | // WRITE uint32_t TYPE ==0
|
---|
139 | // WRITE uint32_t VERSION==0
|
---|
140 | // WRITE uint32_t LENGTH ==0
|
---|
141 | // ===
|
---|
142 | // Go back and write footer
|
---|
143 |
|
---|
144 | public:
|
---|
145 | DataFileRaw(uint32_t id) : DataFileImp(id) { }
|
---|
146 | ~DataFileRaw() { Close(); }
|
---|
147 |
|
---|
148 | void WriteBlockHeader(uint32_t type, uint32_t ver, uint32_t cnt, uint32_t len)
|
---|
149 | {
|
---|
150 | const uint32_t val[4] = { type, ver, cnt, len };
|
---|
151 |
|
---|
152 | fOut.write(reinterpret_cast<const char*>(val), sizeof(val));
|
---|
153 | }
|
---|
154 |
|
---|
155 | template<typename T>
|
---|
156 | void WriteValue(const T &t)
|
---|
157 | {
|
---|
158 | fOut.write(reinterpret_cast<const char*>(&t), sizeof(T));
|
---|
159 | }
|
---|
160 |
|
---|
161 | enum
|
---|
162 | {
|
---|
163 | kEndOfFile = 0,
|
---|
164 | kIdentifier = 1,
|
---|
165 | kRunHeader,
|
---|
166 | kBoardHeader,
|
---|
167 | kRunSummary,
|
---|
168 | kEvent,
|
---|
169 | };
|
---|
170 |
|
---|
171 | virtual bool OpenFile(RUN_HEAD *h)
|
---|
172 | {
|
---|
173 | const string name = FormFileName(h->RunType, "bin");
|
---|
174 |
|
---|
175 | errno = 0;
|
---|
176 | fOut.open(name.c_str(), ios_base::out);
|
---|
177 | if (!fOut)
|
---|
178 | {
|
---|
179 | //ostringstream str;
|
---|
180 | //str << "Open file " << name << ": " << strerror(errno) << " (errno=" << errno << ")";
|
---|
181 | //Error(str);
|
---|
182 |
|
---|
183 | return false;
|
---|
184 | }
|
---|
185 |
|
---|
186 | fCounter = 0;
|
---|
187 |
|
---|
188 | static uint32_t FACT = 0xFAC77e1e;
|
---|
189 |
|
---|
190 | fOut.write(reinterpret_cast<char*>(&FACT), 4);
|
---|
191 |
|
---|
192 | WriteBlockHeader(kIdentifier, 1, 0, 8);
|
---|
193 | WriteValue(uint32_t(0));
|
---|
194 | WriteValue(GetRunId());
|
---|
195 |
|
---|
196 | WriteBlockHeader(kRunHeader, 1, 0, sizeof(RUN_HEAD)-sizeof(PEVNT_HEADER*));
|
---|
197 | fOut.write(reinterpret_cast<char*>(h), sizeof(RUN_HEAD)-sizeof(PEVNT_HEADER*));
|
---|
198 |
|
---|
199 | for (int i=0; i<40; i++)
|
---|
200 | {
|
---|
201 | WriteBlockHeader(kBoardHeader, 1, i, sizeof(PEVNT_HEADER));
|
---|
202 | fOut.write(reinterpret_cast<char*>(h->FADhead+i), sizeof(PEVNT_HEADER));
|
---|
203 | }
|
---|
204 |
|
---|
205 | // FIXME: Split this
|
---|
206 | const vector<char> block(sizeof(uint32_t)+sizeof(RUN_TAIL));
|
---|
207 | WriteBlockHeader(kRunSummary, 1, 0, block.size());
|
---|
208 |
|
---|
209 | fPosTail = fOut.tellp();
|
---|
210 | fOut.write(block.data(), block.size());
|
---|
211 |
|
---|
212 | if (!fOut)
|
---|
213 | {
|
---|
214 | //ostringstream str;
|
---|
215 | //str << "Open file " << name << ": " << strerror(errno) << " (errno=" << errno << ")";
|
---|
216 | //Error(str);
|
---|
217 |
|
---|
218 | return false;
|
---|
219 | }
|
---|
220 |
|
---|
221 | return true;
|
---|
222 | }
|
---|
223 | virtual bool Write(EVENT *evt)
|
---|
224 | {
|
---|
225 | const int sh = sizeof(EVENT)-2 + NPIX*evt->Roi*2;
|
---|
226 |
|
---|
227 | WriteBlockHeader(kEvent, 1, fCounter++, sh);
|
---|
228 | fOut.write(reinterpret_cast<char*>(evt)+2, sh);
|
---|
229 | return true;
|
---|
230 | }
|
---|
231 | virtual bool Close(RUN_TAIL *tail= 0)
|
---|
232 | {
|
---|
233 | WriteBlockHeader(kEndOfFile, 0, 0, 0);
|
---|
234 |
|
---|
235 | if (tail)
|
---|
236 | {
|
---|
237 | fOut.seekp(fPosTail);
|
---|
238 |
|
---|
239 | WriteValue(uint32_t(1));
|
---|
240 | fOut.write(reinterpret_cast<char*>(tail), sizeof(RUN_TAIL));
|
---|
241 | }
|
---|
242 |
|
---|
243 | if (!fOut)
|
---|
244 | {
|
---|
245 | //ostringstream str;
|
---|
246 | //str << "Open file " << name << ": " << strerror(errno) << " (errno=" << errno << ")";
|
---|
247 | //Error(str);
|
---|
248 |
|
---|
249 | return false;
|
---|
250 | }
|
---|
251 |
|
---|
252 | fOut.close();
|
---|
253 |
|
---|
254 | if (!fOut)
|
---|
255 | {
|
---|
256 | //ostringstream str;
|
---|
257 | //str << "Open file " << name << ": " << strerror(errno) << " (errno=" << errno << ")";
|
---|
258 | //Error(str);
|
---|
259 |
|
---|
260 | return false;
|
---|
261 | }
|
---|
262 |
|
---|
263 | return true;
|
---|
264 | }
|
---|
265 | };
|
---|
266 |
|
---|
267 |
|
---|
268 | class DataFileFits : public DataFileImp
|
---|
269 | {
|
---|
270 | CCfits::FITS* fFile; /// The pointer to the CCfits FITS file
|
---|
271 | CCfits::Table* fTable; /// The pointer to the CCfits binary table
|
---|
272 |
|
---|
273 | uint64_t fNumRows; ///the number of rows that have been written already to the FITS file.
|
---|
274 |
|
---|
275 | public:
|
---|
276 | DataFileFits(uint32_t runid) : DataFileImp(runid), fFile(0)
|
---|
277 | {
|
---|
278 | }
|
---|
279 |
|
---|
280 | // --------------------------------------------------------------------------
|
---|
281 | //
|
---|
282 | //! Default destructor
|
---|
283 | //! The Fits file SHOULD have been closed already, otherwise the informations
|
---|
284 | //! related to the RUN_TAIL will NOT be written to the file.
|
---|
285 | //
|
---|
286 | ~DataFileFits() { Close(); }
|
---|
287 |
|
---|
288 | // --------------------------------------------------------------------------
|
---|
289 | //
|
---|
290 | //! Add a new column to the vectors storing the column data.
|
---|
291 | //! @param names the vector of string storing the columns names
|
---|
292 | //! @param types the vector of string storing the FITS data format
|
---|
293 | //! @param numElems the number of elements in this column
|
---|
294 | //! @param type the char describing the FITS data format
|
---|
295 | //! @param name the name of the particular column to be added.
|
---|
296 | //
|
---|
297 | inline void AddColumnEntry(vector<string>& names, vector<string>& types, int numElems, char type, string name)
|
---|
298 | {
|
---|
299 | names.push_back(name);
|
---|
300 |
|
---|
301 | ostringstream str;
|
---|
302 | if (numElems != 1)
|
---|
303 | str << numElems;
|
---|
304 | str << type;
|
---|
305 | types.push_back(str.str());
|
---|
306 | }
|
---|
307 |
|
---|
308 | // --------------------------------------------------------------------------
|
---|
309 | //
|
---|
310 | //! Writes a single header key entry
|
---|
311 | //! @param name the name of the key
|
---|
312 | //! @param value its value
|
---|
313 | //! @param comment the comment associated to that key
|
---|
314 | //
|
---|
315 | //FIXME this function is a duplicate from the class Fits. should we try to merge it ?
|
---|
316 | template <typename T>
|
---|
317 | void WriteKey(const string &name, const T &value, const string &comment)
|
---|
318 | {
|
---|
319 | try
|
---|
320 | {
|
---|
321 | fTable->addKey(name, value, comment);
|
---|
322 | }
|
---|
323 | catch (CCfits::FitsException e)
|
---|
324 | {
|
---|
325 | ostringstream str;
|
---|
326 | str << "Could not add header key ";
|
---|
327 | //TODO pipe the error message somewhere
|
---|
328 | }
|
---|
329 | }
|
---|
330 |
|
---|
331 | template <typename T>
|
---|
332 | void WriteKey(const string &name, const int idx, const T &value, const string &comment)
|
---|
333 | {
|
---|
334 | ostringstream str;
|
---|
335 | str << name << idx;
|
---|
336 |
|
---|
337 | WriteKey(str.str(), value, comment);
|
---|
338 | }
|
---|
339 |
|
---|
340 | // --------------------------------------------------------------------------
|
---|
341 | //
|
---|
342 | //! DataFileFits constructor. This is the one that should be used, not the default one (parameter-less)
|
---|
343 | //! @param runid This parameter should probably be removed. I first thought it was the run number, but apparently it is not
|
---|
344 | //! @param h a pointer to the RUN_HEAD structure that contains the informations relative to this run
|
---|
345 | //
|
---|
346 | bool OpenFile(RUN_HEAD* h)
|
---|
347 | {
|
---|
348 | //Form filename, based on runid and run-type
|
---|
349 | const string fileName = FormFileName(h->RunType, "fits");
|
---|
350 |
|
---|
351 | //create the FITS object
|
---|
352 | try
|
---|
353 | {
|
---|
354 | fFile = new CCfits::FITS(fileName, CCfits::RWmode::Write);
|
---|
355 | }
|
---|
356 | catch (CCfits::FitsException e)
|
---|
357 | {
|
---|
358 | ostringstream str;
|
---|
359 | str << "Could not open FITS file " << fileName << " reason: " << e.message();
|
---|
360 | //TODO display the message somewhere
|
---|
361 |
|
---|
362 | return false;
|
---|
363 | }
|
---|
364 |
|
---|
365 | //create columns according to header
|
---|
366 | ostringstream arrayTypes;
|
---|
367 |
|
---|
368 | // uint32_t EventNum ; // EventNumber as from FTM
|
---|
369 | // uint16_t TriggerType ; // Trigger Type from FTM
|
---|
370 | // uint32_t SoftTrig ; // SoftTrigger Info (TBD)
|
---|
371 | // uint32_t PCTime ; // when did event start to arrive at PC
|
---|
372 | // uint32_t BoardTime[NBOARDS];//
|
---|
373 | // int16_t StartPix[NPIX]; // First Channel per Pixel (Pixels sorted according Software ID) ; -1 if not filled
|
---|
374 | // int16_t StartTM[NTMARK]; // First Channel for TimeMark (sorted Hardware ID) ; -1 if not filled
|
---|
375 | // uint16_t Adc_Data[]; // final length defined by malloc ....
|
---|
376 |
|
---|
377 | vector<string> colNames;
|
---|
378 | vector<string> dataTypes;
|
---|
379 | AddColumnEntry(colNames, dataTypes, 1, 'V', "EventNum");
|
---|
380 | AddColumnEntry(colNames, dataTypes, 1, 'U', "TriggerType");
|
---|
381 | AddColumnEntry(colNames, dataTypes, 1, 'V', "SoftTrig");
|
---|
382 | AddColumnEntry(colNames, dataTypes, 1, 'V', "PCTime");
|
---|
383 | AddColumnEntry(colNames, dataTypes, NBOARDS, 'V', "BoardTime");
|
---|
384 | AddColumnEntry(colNames, dataTypes, NPIX, 'I', "StartPix");
|
---|
385 | AddColumnEntry(colNames, dataTypes, NTMARK, 'I', "StartTM");
|
---|
386 | AddColumnEntry(colNames, dataTypes, NPIX*h->Nroi, 'U', "Data");
|
---|
387 |
|
---|
388 | //actually create the table
|
---|
389 | try
|
---|
390 | {
|
---|
391 | fTable = fFile->addTable("Events", 0, colNames, dataTypes);
|
---|
392 | if (fTable->rows() != 0)
|
---|
393 | {
|
---|
394 | ostringstream str;
|
---|
395 | str << "Error: table created on the fly looks non-empty.";
|
---|
396 | //TODO giev the error text to some error handler
|
---|
397 | //FIXME I guess that this error checking is useless. remove it for performances.
|
---|
398 | }
|
---|
399 | }
|
---|
400 | catch (const CCfits::FitsException &e)
|
---|
401 | {
|
---|
402 | ostringstream str;
|
---|
403 | str << "Could not create FITS table " << "Events" << " in file " << fileName << " reason: " << e.message();
|
---|
404 | //TODO give the error text to some error handler
|
---|
405 |
|
---|
406 | Close();
|
---|
407 | return false;
|
---|
408 | }
|
---|
409 |
|
---|
410 | //write header data
|
---|
411 | //first the "standard" keys
|
---|
412 | string stringValue;
|
---|
413 | WriteKey("EXTREL", 1.0f, "Release Number");
|
---|
414 | WriteKey("TELESCOP", "FACT", "Telescope that acquired this data");
|
---|
415 | WriteKey("ORIGIN", "ISDC", "Institution that wrote the file");
|
---|
416 | WriteKey("CREATOR", "FACT++ Event Builder", "Program that wrote this file");
|
---|
417 |
|
---|
418 | stringValue = Time().GetAsStr();
|
---|
419 | stringValue[10]= 'T';
|
---|
420 | WriteKey("DATE", stringValue, "File creation data");
|
---|
421 | WriteKey("TIMESYS", "TT", "Time frame system");
|
---|
422 | WriteKey("TIMEUNIT", "d", "Time unit");
|
---|
423 | WriteKey("TIMEREF", "UTC", "Time reference frame");
|
---|
424 | //FIXME should we also put the start and stop time of the received data ?
|
---|
425 | //now the events header related variables
|
---|
426 | WriteKey("VERSION", h->Version, "Builder version");
|
---|
427 | WriteKey("RUNTYPE", h->RunType, "Type of run");
|
---|
428 | WriteKey("NBOARD", h->NBoard, "Number of acquisition boards");
|
---|
429 | WriteKey("NPIX", h->NPix, "Number of pixels");
|
---|
430 | WriteKey("NTM", h->NTm, "Number of Time marks");
|
---|
431 | WriteKey("NROI", h->Nroi, "Number of slices per pixels");
|
---|
432 |
|
---|
433 | //now the boards related keywords
|
---|
434 | for (int i=0; i<h->NBoard; i++)
|
---|
435 | {
|
---|
436 | const PEVNT_HEADER &hh = h->FADhead[i];
|
---|
437 |
|
---|
438 | WriteKey("STPKGFG", i, hh.start_package_flag,
|
---|
439 | "Start package flag");
|
---|
440 |
|
---|
441 | WriteKey("PKGLEN", i, hh.package_length,
|
---|
442 | "Package length");
|
---|
443 |
|
---|
444 | WriteKey("VERNO", i, hh.version_no,
|
---|
445 | "Version number");
|
---|
446 |
|
---|
447 | WriteKey("PLLLCK", i, hh.PLLLCK,
|
---|
448 | "");
|
---|
449 | /*
|
---|
450 | WriteKey("TRIGCRC", i, hh.trigger_crc,
|
---|
451 | "Trigger CRC");
|
---|
452 |
|
---|
453 | WriteKey("TRIGTYP", i, hh.trigger_type,
|
---|
454 | "Trigger type");
|
---|
455 |
|
---|
456 | WriteKey("TRIGID", i, hh.trigger_id,
|
---|
457 | "Trigger ID");
|
---|
458 |
|
---|
459 | WriteKey("EVTCNTR", i, hh.fad_evt_counter,
|
---|
460 | "FAD Event Counter");
|
---|
461 | */
|
---|
462 | WriteKey("REFCLK", i, hh.REFCLK_frequency,
|
---|
463 | "Reference Clock Frequency");
|
---|
464 |
|
---|
465 | WriteKey("BOARDID", i, hh.board_id,
|
---|
466 | "Board ID");
|
---|
467 |
|
---|
468 | WriteKey("PHASESH", i, hh.adc_clock_phase_shift,
|
---|
469 | "ADC clock phase shift");
|
---|
470 |
|
---|
471 | //WriteKey("TRGGEN", i, hh.number_of_triggers_to_generate,
|
---|
472 | // "Number of triggers to generate");
|
---|
473 |
|
---|
474 | WriteKey("PRESC", i, hh.trigger_generator_prescaler,
|
---|
475 | "Trigger generator prescaler");
|
---|
476 |
|
---|
477 | WriteKey("DNA", i, hh.DNA, "DNA");
|
---|
478 | WriteKey("TIME", i, hh.time, "Time");
|
---|
479 | WriteKey("RUNNB", i, hh.runnumber, "Run number");
|
---|
480 | /*
|
---|
481 | for (int j=0;j<NTemp;j++)
|
---|
482 | {
|
---|
483 | str.str(""); str2.str("");
|
---|
484 | str << "DRS_T" << i << j;
|
---|
485 | str2 << "DRS temperature #" << i << " " << j;
|
---|
486 | WriteKey(str.str(), h->FADhead[i].drs_temperature[j], str2.str());
|
---|
487 | }
|
---|
488 | */
|
---|
489 | for (int j=0;j<NDAC;j++)
|
---|
490 | WriteKey("DAC", i*NDAC+j, hh.dac[j], "DAC");
|
---|
491 |
|
---|
492 | //Last but not least, add header keys that will be updated when closing the file
|
---|
493 | WriteFooter(NULL);
|
---|
494 | }
|
---|
495 |
|
---|
496 | return true;
|
---|
497 | }
|
---|
498 |
|
---|
499 |
|
---|
500 | int WriteColumns(size_t &start, size_t size, void *e)
|
---|
501 | {
|
---|
502 | int status = 0;
|
---|
503 | fits_write_tblbytes(fFile->fitsPointer(), fNumRows, start, size, reinterpret_cast<unsigned char*>(e), &status);
|
---|
504 | if (status)
|
---|
505 | {
|
---|
506 | char text[30];//max length of cfitsio error strings (from doc)
|
---|
507 | fits_get_errstatus(status, text);
|
---|
508 | //ostringstream str;
|
---|
509 | //str << "Writing FITS row " << i << " in " << groupName << ": " << text << " (file_write_tblbytes, rc=" << status << ")";
|
---|
510 | //Error(str);
|
---|
511 | }
|
---|
512 |
|
---|
513 | start += size;
|
---|
514 | return status;
|
---|
515 | }
|
---|
516 |
|
---|
517 | // --------------------------------------------------------------------------
|
---|
518 | //
|
---|
519 | //! This writes one event to the file
|
---|
520 | //! @param e the pointer to the EVENT
|
---|
521 | //
|
---|
522 | virtual bool Write(EVENT *e)
|
---|
523 | {
|
---|
524 | //FIXME As discussed earlier, we do not swap the bytes yet.
|
---|
525 | fTable->makeThisCurrent();
|
---|
526 |
|
---|
527 | //insert a new row
|
---|
528 | int status(0);
|
---|
529 | if (fits_insert_rows(fTable->fitsPointer(), fNumRows, 1, &status))
|
---|
530 | {
|
---|
531 | //ostringstream str;
|
---|
532 | //str << "Inserting row into " << fFileName << " failed (fits_insert_rows, rc=" << status << ")";
|
---|
533 | //fMess->Error(str);
|
---|
534 | //TODO pipe this error message to the appropriate error stream
|
---|
535 | }
|
---|
536 | fNumRows++;
|
---|
537 |
|
---|
538 | const int sh = sizeof(EVENT)+NPIX*e->Roi*2;
|
---|
539 |
|
---|
540 | // column size pointer
|
---|
541 | size_t col = 1;
|
---|
542 | if (!WriteColumns(col, sh, e))
|
---|
543 | return true;
|
---|
544 |
|
---|
545 | //TODO output an error
|
---|
546 | return false;
|
---|
547 |
|
---|
548 | /*
|
---|
549 | //write the data, chunk by chunk
|
---|
550 | //FIXME hard-coded size corresponds to current variables of the event, in bytes.
|
---|
551 | //FIXME no padding was taken into account. Because smallest member is 2 bytes, I don't think that this should be a problem.
|
---|
552 | const long sizeInBytesOfEventBeforePointers = 16;
|
---|
553 |
|
---|
554 | long col = 1;
|
---|
555 | if (FitsWriteTblBytes(col, sizeInBytesOfEventBeforePointers, e))
|
---|
556 | {
|
---|
557 | //TODO output an error
|
---|
558 | return false;
|
---|
559 | }
|
---|
560 | if (FitsWriteTblBytes(col, NBOARDS*2, e->BoardTime))
|
---|
561 | {
|
---|
562 | //TODO output an error
|
---|
563 | return false;
|
---|
564 | }
|
---|
565 | if (FitsWriteTblBytes(col, NPIX*2, e->StartPix))
|
---|
566 | {
|
---|
567 | //TODO output an error
|
---|
568 | return false;
|
---|
569 | }
|
---|
570 | if (FitsWriteTblBytes(col, NTMARK*2, e->StartTM))
|
---|
571 | {
|
---|
572 | //TODO output an error
|
---|
573 | return false;
|
---|
574 | }
|
---|
575 | if (FitsWriteTblBytes(col, NPIX*fRoi*2, e->Adc_Data))
|
---|
576 | {
|
---|
577 | //TODO output an error
|
---|
578 | return false;
|
---|
579 | }
|
---|
580 | return true;*/
|
---|
581 | }
|
---|
582 |
|
---|
583 | void WriteFooter(RUN_TAIL *rt)
|
---|
584 | {
|
---|
585 | //write final header keys
|
---|
586 | fTable->makeThisCurrent();
|
---|
587 |
|
---|
588 | WriteKey("NBEVTOK", rt ? rt->nEventsOk : uint32_t(0),
|
---|
589 | "How many events were written");
|
---|
590 |
|
---|
591 | WriteKey("NBEVTREJ", rt ? rt->nEventsRej : uint32_t(0),
|
---|
592 | "How many events were rejected by SW-trig");
|
---|
593 |
|
---|
594 | WriteKey("NBEVTBAD", rt ? rt->nEventsBad : uint32_t(0),
|
---|
595 | "How many events were rejected by Error");
|
---|
596 |
|
---|
597 | //FIXME shouldn't we convert start and stop time to MjD first ?
|
---|
598 | //FIXME shouldn't we also add an MjD reference ?
|
---|
599 |
|
---|
600 | WriteKey("TSTART", rt ? rt->PCtime0 : uint32_t(0),
|
---|
601 | "Time when first event received");
|
---|
602 |
|
---|
603 | WriteKey("TSTOP", rt ? rt->PCtimeX : uint32_t(0),
|
---|
604 | "Time when last event received");
|
---|
605 | }
|
---|
606 |
|
---|
607 | // --------------------------------------------------------------------------
|
---|
608 | //
|
---|
609 | //! Closes the file, and before this it write the TAIL data
|
---|
610 | //! @param rt the pointer to the RUN_TAIL data structure
|
---|
611 | //
|
---|
612 | virtual bool Close(RUN_TAIL *rt = 0)
|
---|
613 | {
|
---|
614 | if (!fFile)
|
---|
615 | return false;
|
---|
616 |
|
---|
617 | WriteFooter(rt);
|
---|
618 |
|
---|
619 | delete fFile;
|
---|
620 | fFile = NULL;
|
---|
621 |
|
---|
622 | return true;
|
---|
623 | }
|
---|
624 |
|
---|
625 | };
|
---|
626 |
|
---|
627 | class EventBuilderWrapper
|
---|
628 | {
|
---|
629 | public:
|
---|
630 | // FIXME
|
---|
631 | static EventBuilderWrapper *This;
|
---|
632 |
|
---|
633 | private:
|
---|
634 | boost::thread fThread;
|
---|
635 |
|
---|
636 | enum CommandStates_t // g_runStat
|
---|
637 | {
|
---|
638 | kAbort = -2, // quit as soon as possible ('abort')
|
---|
639 | kExit = -1, // stop reading, quit when buffered events done ('exit')
|
---|
640 | kInitialize = 0, // 'initialize' (e.g. dim not yet started)
|
---|
641 | kHybernate = 1, // do nothing for long time ('hybernate') [wakeup within ~1sec]
|
---|
642 | kSleep = 2, // do nothing ('sleep') [wakeup within ~10msec]
|
---|
643 | kModeFlush = 10, // read data from camera, but skip them ('flush')
|
---|
644 | kModeTest = 20, // read data and process them, but do not write to disk ('test')
|
---|
645 | kModeFlag = 30, // read data, process and write all to disk ('flag')
|
---|
646 | kModeRun = 40, // read data, process and write selected to disk ('run')
|
---|
647 | };
|
---|
648 |
|
---|
649 | MessageImp &fMsg;
|
---|
650 |
|
---|
651 | enum
|
---|
652 | {
|
---|
653 | kCurrent = 0,
|
---|
654 | kTotal = 1
|
---|
655 | };
|
---|
656 |
|
---|
657 | bool fFitsFormat;
|
---|
658 |
|
---|
659 | uint32_t fMaxRun;
|
---|
660 | uint32_t fNumEvts[2];
|
---|
661 |
|
---|
662 | DimDescribedService fDimFiles;
|
---|
663 | DimDescribedService fDimRuns;
|
---|
664 | DimDescribedService fDimEvents;
|
---|
665 | DimDescribedService fDimCurrentEvent;
|
---|
666 |
|
---|
667 | public:
|
---|
668 | EventBuilderWrapper(MessageImp &msg) : fMsg(msg),
|
---|
669 | fFitsFormat(false), fMaxRun(0),
|
---|
670 | fDimFiles ("FAD_CONTROL/FILES", "X:1", ""),
|
---|
671 | fDimRuns ("FAD_CONTROL/RUNS", "I:1", ""),
|
---|
672 | fDimEvents("FAD_CONTROL/EVENTS", "I:2", ""),
|
---|
673 | fDimCurrentEvent("FAD_CONTROL/CURRENT_EVENT", "I:1", "")
|
---|
674 | {
|
---|
675 | if (This)
|
---|
676 | throw logic_error("EventBuilderWrapper cannot be instantiated twice.");
|
---|
677 |
|
---|
678 | This = this;
|
---|
679 |
|
---|
680 | memset(fNumEvts, 0, sizeof(fNumEvts));
|
---|
681 |
|
---|
682 | Update(fDimRuns, uint32_t(0));
|
---|
683 | Update(fDimCurrentEvent, uint32_t(0));
|
---|
684 | Update(fDimEvents, fNumEvts);
|
---|
685 | }
|
---|
686 | ~EventBuilderWrapper()
|
---|
687 | {
|
---|
688 | Abort();
|
---|
689 | // FIXME: Used timed_join and abort afterwards
|
---|
690 | // What's the maximum time the eb need to abort?
|
---|
691 | fThread.join();
|
---|
692 | //fMsg.Info("EventBuilder stopped.");
|
---|
693 |
|
---|
694 | for (vector<DataFileImp*>::iterator it=fFiles.begin(); it!=fFiles.end(); it++)
|
---|
695 | delete *it;
|
---|
696 | }
|
---|
697 |
|
---|
698 | void Update(ostringstream &msg, int severity)
|
---|
699 | {
|
---|
700 | fMsg.Update(msg, severity);
|
---|
701 | }
|
---|
702 |
|
---|
703 | bool IsThreadRunning()
|
---|
704 | {
|
---|
705 | return !fThread.timed_join(boost::posix_time::microseconds(0));
|
---|
706 | }
|
---|
707 |
|
---|
708 | void SetMaxMemory(unsigned int mb) const
|
---|
709 | {
|
---|
710 | if (mb*1000000<GetUsedMemory())
|
---|
711 | {
|
---|
712 | // fMsg.Warn("...");
|
---|
713 | return;
|
---|
714 | }
|
---|
715 |
|
---|
716 | g_maxMem = mb*1000000;
|
---|
717 | }
|
---|
718 |
|
---|
719 | void Start(const vector<tcp::endpoint> &addr)
|
---|
720 | {
|
---|
721 | if (IsThreadRunning())
|
---|
722 | {
|
---|
723 | fMsg.Warn("Start - EventBuilder still running");
|
---|
724 | return;
|
---|
725 | }
|
---|
726 |
|
---|
727 | int cnt = 0;
|
---|
728 | for (size_t i=0; i<40; i++)
|
---|
729 | {
|
---|
730 | if (addr[i]==tcp::endpoint())
|
---|
731 | {
|
---|
732 | g_port[i].sockDef = -1;
|
---|
733 | continue;
|
---|
734 | }
|
---|
735 |
|
---|
736 | // -1: if entry shell not be used
|
---|
737 | // 0: event builder will connect but ignore the events
|
---|
738 | // 1: event builder will connect and build events
|
---|
739 | g_port[i].sockDef = 1;
|
---|
740 |
|
---|
741 | g_port[i].sockAddr.sin_family = AF_INET;
|
---|
742 | g_port[i].sockAddr.sin_addr.s_addr = htonl(addr[i].address().to_v4().to_ulong());
|
---|
743 | g_port[i].sockAddr.sin_port = htons(addr[i].port());
|
---|
744 |
|
---|
745 | cnt++;
|
---|
746 | }
|
---|
747 |
|
---|
748 | // g_maxBoards = cnt;
|
---|
749 | g_actBoards = cnt;
|
---|
750 |
|
---|
751 | g_runStat = kModeRun;
|
---|
752 |
|
---|
753 | fMsg.Message("Starting EventBuilder thread");
|
---|
754 |
|
---|
755 | fThread = boost::thread(StartEvtBuild);
|
---|
756 | }
|
---|
757 | void Abort()
|
---|
758 | {
|
---|
759 | fMsg.Message("Signal abort to EventBuilder thread...");
|
---|
760 | g_runStat = kAbort;
|
---|
761 | }
|
---|
762 |
|
---|
763 | void Exit()
|
---|
764 | {
|
---|
765 | fMsg.Message("Signal exit to EventBuilder thread...");
|
---|
766 | g_runStat = kExit;
|
---|
767 | }
|
---|
768 |
|
---|
769 | /*
|
---|
770 | void Wait()
|
---|
771 | {
|
---|
772 | fThread.join();
|
---|
773 | fMsg.Message("EventBuilder stopped.");
|
---|
774 | }*/
|
---|
775 |
|
---|
776 | void Hybernate() const { g_runStat = kHybernate; }
|
---|
777 | void Sleep() const { g_runStat = kSleep; }
|
---|
778 | void FlushMode() const { g_runStat = kModeFlush; }
|
---|
779 | void TestMode() const { g_runStat = kModeTest; }
|
---|
780 | void FlagMode() const { g_runStat = kModeFlag; }
|
---|
781 | void RunMode() const { g_runStat = kModeRun; }
|
---|
782 |
|
---|
783 | // FIXME: To be removed
|
---|
784 | void SetMode(int mode) const { g_runStat = mode; }
|
---|
785 |
|
---|
786 | bool IsConnected(int i) const { return gi_NumConnect[i]==7; }
|
---|
787 | bool IsDisconnected(int i) const { return gi_NumConnect[i]<=0; }
|
---|
788 | int GetNumConnected(int i) const { return gi_NumConnect[i]; }
|
---|
789 |
|
---|
790 | size_t GetUsedMemory() const { return gi_usedMem; }
|
---|
791 |
|
---|
792 | virtual int CloseOpenFiles() { CloseRunFile(0, 0); return 0; }
|
---|
793 |
|
---|
794 |
|
---|
795 | /*
|
---|
796 | struct OpenFileToDim
|
---|
797 | {
|
---|
798 | int code;
|
---|
799 | char fileName[FILENAME_MAX];
|
---|
800 | };
|
---|
801 |
|
---|
802 | SignalRunOpened(runid, filename);
|
---|
803 | // Send num open files
|
---|
804 | // Send runid, (more info about the run?), filename via dim
|
---|
805 |
|
---|
806 | SignalEvtWritten(runid);
|
---|
807 | // Send num events written of newest file
|
---|
808 |
|
---|
809 | SignalRunClose(runid);
|
---|
810 | // Send new num open files
|
---|
811 | // Send empty file-name if no file is open
|
---|
812 |
|
---|
813 | */
|
---|
814 |
|
---|
815 | // -------------- Mapped event builder callbacks ------------------
|
---|
816 |
|
---|
817 | vector<DataFileImp*> fFiles;
|
---|
818 |
|
---|
819 | template<class T>
|
---|
820 | void Update(DimDescribedService &svc, const T &data) const
|
---|
821 | {
|
---|
822 | cout << "Update: " << svc.getName() << " (" << sizeof(T) << ")" << endl;
|
---|
823 | svc.setData(const_cast<T*>(&data), sizeof(T));
|
---|
824 | svc.updateService();
|
---|
825 | }
|
---|
826 |
|
---|
827 | FileHandle_t runOpen(uint32_t runid, RUN_HEAD *h, size_t)
|
---|
828 | {
|
---|
829 | // Check if file already exists...
|
---|
830 | DataFileImp *file = fFitsFormat ?
|
---|
831 | static_cast<DataFileImp*>(new DataFileFits(runid)) :
|
---|
832 | static_cast<DataFileImp*>(new DataFileRaw(runid));
|
---|
833 |
|
---|
834 | try
|
---|
835 | {
|
---|
836 | if (!file->OpenFile(h))
|
---|
837 | return 0;
|
---|
838 | }
|
---|
839 | catch (const exception &e)
|
---|
840 | {
|
---|
841 | return 0;
|
---|
842 | }
|
---|
843 |
|
---|
844 | cout << "OPEN_FILE #" << runid << " (" << file << ")" << endl;
|
---|
845 | cout << " Ver= " << h->Version << endl;
|
---|
846 | cout << " Typ= " << h->RunType << endl;
|
---|
847 | cout << " Nb = " << h->NBoard << endl;
|
---|
848 | cout << " Np = " << h->NPix << endl;
|
---|
849 | cout << " NTm= " << h->NTm << endl;
|
---|
850 | cout << " roi= " << h->Nroi << endl;
|
---|
851 |
|
---|
852 | fFiles.push_back(file);
|
---|
853 |
|
---|
854 | if (runid>fMaxRun)
|
---|
855 | {
|
---|
856 | fMaxRun = runid;
|
---|
857 | fNumEvts[kCurrent] = 0;
|
---|
858 |
|
---|
859 | Update(fDimRuns, fMaxRun);
|
---|
860 | Update(fDimEvents, fNumEvts);
|
---|
861 | Update(fDimCurrentEvent, uint32_t(0));
|
---|
862 | }
|
---|
863 |
|
---|
864 | Update(fDimFiles, fFiles.size());
|
---|
865 |
|
---|
866 | // fDimFiles.setData(fFiles.size());
|
---|
867 | // fDimFiles.update();
|
---|
868 |
|
---|
869 | return reinterpret_cast<FileHandle_t>(file);
|
---|
870 | }
|
---|
871 |
|
---|
872 | int runWrite(FileHandle_t handler, EVENT *e, size_t)
|
---|
873 | {
|
---|
874 | DataFileImp *file = reinterpret_cast<DataFileImp*>(handler);
|
---|
875 |
|
---|
876 | cout << "WRITE_EVENT " << file->GetRunId() << endl;
|
---|
877 |
|
---|
878 | cout << " Evt=" << e->EventNum << endl;
|
---|
879 | cout << " Typ=" << e->TriggerType << endl;
|
---|
880 | cout << " roi=" << e->Roi << endl;
|
---|
881 | cout << " trg=" << e->SoftTrig << endl;
|
---|
882 | cout << " tim=" << e->PCTime << endl;
|
---|
883 |
|
---|
884 | if (!file->Write(e))
|
---|
885 | return -1;
|
---|
886 |
|
---|
887 | if (file->GetRunId()==fMaxRun)
|
---|
888 | {
|
---|
889 | Update(fDimCurrentEvent, e->EventNum);
|
---|
890 | fNumEvts[kCurrent]++;
|
---|
891 | }
|
---|
892 |
|
---|
893 | fNumEvts[kTotal]++;
|
---|
894 | Update(fDimEvents, fNumEvts);
|
---|
895 |
|
---|
896 | // ===> SignalEvtWritten(runid);
|
---|
897 | // Send num events written of newest file
|
---|
898 |
|
---|
899 | /* close run runId (all all runs if runId=0) */
|
---|
900 | /* return: 0=close scheduled / >0 already closed / <0 does not exist */
|
---|
901 | //CloseRunFile(file->GetRunId(), time(NULL)+2) ;
|
---|
902 |
|
---|
903 | return 0;
|
---|
904 | }
|
---|
905 |
|
---|
906 | int runClose(FileHandle_t handler, RUN_TAIL *tail, size_t)
|
---|
907 | {
|
---|
908 | DataFileImp *file = reinterpret_cast<DataFileImp*>(handler);
|
---|
909 |
|
---|
910 | const vector<DataFileImp*>::iterator it = find(fFiles.begin(), fFiles.end(), file);
|
---|
911 | if (it==fFiles.end())
|
---|
912 | {
|
---|
913 | ostringstream str;
|
---|
914 | str << "File handler (" << handler << ") requested to close by event builder doesn't exist.";
|
---|
915 | fMsg.Fatal(str);
|
---|
916 | return -1;
|
---|
917 | }
|
---|
918 |
|
---|
919 | ostringstream str;
|
---|
920 | str << "CLOSE_RUN requested for " << file->GetRunId() << " (" << file << ")" <<endl;
|
---|
921 | fMsg.Debug(str);
|
---|
922 |
|
---|
923 | fFiles.erase(it);
|
---|
924 |
|
---|
925 | Update(fDimFiles, fFiles.size());
|
---|
926 |
|
---|
927 | //fDimFiles.setData(fFiles.size());
|
---|
928 | //fDimFiles.update();
|
---|
929 |
|
---|
930 | const bool rc = file->Close(tail);
|
---|
931 | if (!rc)
|
---|
932 | {
|
---|
933 | // Error message
|
---|
934 | }
|
---|
935 |
|
---|
936 | delete file;
|
---|
937 |
|
---|
938 | // ==> SignalRunClose(runid);
|
---|
939 | // Send new num open files
|
---|
940 | // Send empty file-name if no file is open
|
---|
941 |
|
---|
942 | return rc ? 0 : -1;
|
---|
943 | }
|
---|
944 | };
|
---|
945 |
|
---|
946 | EventBuilderWrapper *EventBuilderWrapper::This = 0;
|
---|
947 |
|
---|
948 | // ----------- Event builder callbacks implementation ---------------
|
---|
949 | extern "C"
|
---|
950 | {
|
---|
951 | FileHandle_t runOpen(uint32_t irun, RUN_HEAD *runhd, size_t len)
|
---|
952 | {
|
---|
953 | return EventBuilderWrapper::This->runOpen(irun, runhd, len);
|
---|
954 | }
|
---|
955 |
|
---|
956 | int runWrite(FileHandle_t fileId, EVENT *event, size_t len)
|
---|
957 | {
|
---|
958 | return EventBuilderWrapper::This->runWrite(fileId, event, len);
|
---|
959 | }
|
---|
960 |
|
---|
961 | int runClose(FileHandle_t fileId, RUN_TAIL *runth, size_t len)
|
---|
962 | {
|
---|
963 | return EventBuilderWrapper::This->runClose(fileId, runth, len);
|
---|
964 | }
|
---|
965 |
|
---|
966 | void factOut(int severity, int err, char *message)
|
---|
967 | {
|
---|
968 | // FIXME: Make the output to the console stream thread-safe
|
---|
969 | ostringstream str;
|
---|
970 | str << "EventBuilder(";
|
---|
971 | if (err<0)
|
---|
972 | str << "---";
|
---|
973 | else
|
---|
974 | str << err;
|
---|
975 | str << "): " << message;
|
---|
976 | EventBuilderWrapper::This->Update(str, severity);
|
---|
977 | }
|
---|
978 |
|
---|
979 | void factStat(int severity, int err, char* message )
|
---|
980 | {
|
---|
981 | if (err!=-1)
|
---|
982 | {
|
---|
983 | factOut(severity, err, message);
|
---|
984 | return;
|
---|
985 | }
|
---|
986 |
|
---|
987 | static string last;
|
---|
988 | if (message==last)
|
---|
989 | return;
|
---|
990 |
|
---|
991 | last = message;
|
---|
992 |
|
---|
993 | ostringstream str("Status: ");
|
---|
994 | str << message;
|
---|
995 |
|
---|
996 | EventBuilderWrapper::This->Update(str, severity);
|
---|
997 | }
|
---|
998 |
|
---|
999 | /*
|
---|
1000 | void message(int severity, const char *msg)
|
---|
1001 | {
|
---|
1002 | EventBuilderWrapper::This->Update(msg, severity);
|
---|
1003 | }*/
|
---|
1004 | }
|
---|
1005 |
|
---|
1006 | #endif
|
---|