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