1 | #ifndef FACT_EventBuilderWrapper
|
---|
2 | #define FACT_EventBuilderWrapper
|
---|
3 |
|
---|
4 | #include <sstream>
|
---|
5 |
|
---|
6 | #if BOOST_VERSION < 104400
|
---|
7 | #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 4))
|
---|
8 | #undef BOOST_HAS_RVALUE_REFS
|
---|
9 | #endif
|
---|
10 | #endif
|
---|
11 | #include <boost/thread.hpp>
|
---|
12 | #include <boost/date_time/posix_time/posix_time_types.hpp>
|
---|
13 |
|
---|
14 |
|
---|
15 | #include <CCfits/CCfits>
|
---|
16 |
|
---|
17 | #include "EventBuilder.h"
|
---|
18 |
|
---|
19 | extern "C" {
|
---|
20 | extern void StartEvtBuild();
|
---|
21 | extern int CloseRunFile(uint32_t runId, uint32_t closeTime, uint32_t maxEvt);
|
---|
22 | }
|
---|
23 |
|
---|
24 | namespace ba = boost::asio;
|
---|
25 | namespace bs = boost::system;
|
---|
26 |
|
---|
27 | using ba::ip::tcp;
|
---|
28 |
|
---|
29 | using namespace std;
|
---|
30 |
|
---|
31 | class DataFileImp : public MessageImp
|
---|
32 | {
|
---|
33 | uint32_t fRunId;
|
---|
34 |
|
---|
35 | int Write(const Time &time, const std::string &txt, int qos)
|
---|
36 | {
|
---|
37 | return fMsg.Write(time, txt, qos);
|
---|
38 | }
|
---|
39 |
|
---|
40 | protected:
|
---|
41 | MessageImp &fMsg;
|
---|
42 | string fFileName;
|
---|
43 |
|
---|
44 | public:
|
---|
45 | DataFileImp(uint32_t id, MessageImp &imp) : fRunId(id), fMsg(imp) { }
|
---|
46 | virtual ~DataFileImp() { }
|
---|
47 |
|
---|
48 | virtual bool OpenFile(RUN_HEAD* h) = 0;
|
---|
49 | virtual bool WriteEvt(EVENT *) = 0;
|
---|
50 | virtual bool Close(RUN_TAIL * = 0) = 0;
|
---|
51 |
|
---|
52 | const string &GetFileName() const { return fFileName; }
|
---|
53 |
|
---|
54 | uint32_t GetRunId() const { return fRunId; }
|
---|
55 |
|
---|
56 | // --------------------------------------------------------------------------
|
---|
57 | //
|
---|
58 | //! This creates an appropriate file name for a particular run number and type
|
---|
59 | //! @param runNumber the run number for which a filename is to be created
|
---|
60 | //! @param runType an int describing the kind of run. 0=Data, 1=Pedestal, 2=Calibration, 3=Calibrated data
|
---|
61 | //! @param extension a string containing the extension to be appened to the file name
|
---|
62 | //
|
---|
63 | static string FormFileName(uint32_t runid, string extension)
|
---|
64 | {
|
---|
65 | ostringstream name;
|
---|
66 | name << Time().NightAsInt() << '_' << setfill('0') << setw(3) << runid << '.' << extension;
|
---|
67 | return name.str();
|
---|
68 | }
|
---|
69 | };
|
---|
70 |
|
---|
71 | class DataFileNone : public DataFileImp
|
---|
72 | {
|
---|
73 | public:
|
---|
74 | DataFileNone(uint32_t id, MessageImp &imp) : DataFileImp(id, imp) { }
|
---|
75 |
|
---|
76 | Time fTime;
|
---|
77 |
|
---|
78 | bool OpenFile(RUN_HEAD* h)
|
---|
79 | {
|
---|
80 | fFileName = "/dev/null";
|
---|
81 |
|
---|
82 | ostringstream str;
|
---|
83 | str << this << " - "
|
---|
84 | << "OPEN_FILE #" << GetRunId() << ":"
|
---|
85 | << " Ver=" << h->Version
|
---|
86 | << " Typ=" << h->RunType
|
---|
87 | << " Nb=" << h->NBoard
|
---|
88 | << " Np=" << h->NPix
|
---|
89 | << " NTm=" << h->NTm
|
---|
90 | << " roi=" << h->Nroi;
|
---|
91 |
|
---|
92 | Debug(str);
|
---|
93 |
|
---|
94 | fTime = Time();
|
---|
95 |
|
---|
96 | return true;
|
---|
97 | }
|
---|
98 | bool WriteEvt(EVENT *e)
|
---|
99 | {
|
---|
100 | const Time now;
|
---|
101 | if (now-fTime<boost::posix_time::seconds(5))
|
---|
102 | return true;
|
---|
103 |
|
---|
104 | fTime = now;
|
---|
105 |
|
---|
106 | ostringstream str;
|
---|
107 | str << this << " - EVENT #" << e->EventNum;
|
---|
108 | Debug(str);
|
---|
109 |
|
---|
110 | return true;
|
---|
111 | }
|
---|
112 | bool Close(RUN_TAIL * = 0)
|
---|
113 | {
|
---|
114 | ostringstream str;
|
---|
115 | str << this << " - CLOSE FILE #" << GetRunId();
|
---|
116 |
|
---|
117 | Debug(str);
|
---|
118 |
|
---|
119 | return true;
|
---|
120 | }
|
---|
121 | };
|
---|
122 |
|
---|
123 | class DataFileDebug : public DataFileNone
|
---|
124 | {
|
---|
125 | public:
|
---|
126 | DataFileDebug(uint32_t id, MessageImp &imp) : DataFileNone(id, imp) { }
|
---|
127 |
|
---|
128 | bool WriteEvt(EVENT *e)
|
---|
129 | {
|
---|
130 | cout << "WRITE_EVENT #" << GetRunId() << " (" << e->EventNum << ")" << endl;
|
---|
131 | cout << " Typ=" << e->TriggerType << endl;
|
---|
132 | cout << " roi=" << e->Roi << endl;
|
---|
133 | cout << " trg=" << e->SoftTrig << endl;
|
---|
134 | cout << " tim=" << e->PCTime << endl;
|
---|
135 |
|
---|
136 | return true;
|
---|
137 | }
|
---|
138 | };
|
---|
139 |
|
---|
140 | #include "FAD.h"
|
---|
141 |
|
---|
142 | class DataFileRaw : public DataFileImp
|
---|
143 | {
|
---|
144 | ofstream fOut;
|
---|
145 |
|
---|
146 | off_t fPosTail;
|
---|
147 |
|
---|
148 | uint32_t fCounter;
|
---|
149 |
|
---|
150 |
|
---|
151 | // WRITE uint32_t 0xFAC77e1e (FACT Tele)
|
---|
152 | // ===
|
---|
153 | // WRITE uint32_t TYPE(>0) == 1
|
---|
154 | // WRITE uint32_t ID(>0) == 0
|
---|
155 | // WRITE uint32_t VERSION(>0) == 1
|
---|
156 | // WRITE uint32_t LENGTH
|
---|
157 | // -
|
---|
158 | // WRITE uint32_t TELESCOPE ID
|
---|
159 | // WRITE uint32_t RUNID
|
---|
160 | // ===
|
---|
161 | // WRITE uint32_t TYPE(>0) == 2
|
---|
162 | // WRITE uint32_t ID(>0) == 0
|
---|
163 | // WRITE uint32_t VERSION(>0) == 1
|
---|
164 | // WRITE uint32_t LENGTH
|
---|
165 | // -
|
---|
166 | // WRITE HEADER
|
---|
167 | // ===
|
---|
168 | // [ 40 TIMES
|
---|
169 | // WRITE uint32_t TYPE(>0) == 3
|
---|
170 | // WRITE uint32_t ID(>0) == 0..39
|
---|
171 | // WRITE uint32_t VERSION(>0) == 1
|
---|
172 | // WRITE uint32_t LENGTH
|
---|
173 | // -
|
---|
174 | // WRITE BOARD-HEADER
|
---|
175 | // ]
|
---|
176 | // ===
|
---|
177 | // WRITE uint32_t TYPE(>0) == 4
|
---|
178 | // WRITE uint32_t ID(>0) == 0
|
---|
179 | // WRITE uint32_t VERSION(>0) == 1
|
---|
180 | // WRITE uint32_t LENGTH
|
---|
181 | // -
|
---|
182 | // WRITE FOOTER (empty)
|
---|
183 | // ===
|
---|
184 | // [ N times
|
---|
185 | // WRITE uint32_t TYPE(>0) == 10
|
---|
186 | // WRITE uint32_t ID(>0) == counter
|
---|
187 | // WRITE uint32_t VERSION(>0) == 1
|
---|
188 | // WRITE uint32_t LENGTH HEADER
|
---|
189 | // -
|
---|
190 | // WRITE HEADER+DATA
|
---|
191 | // ]
|
---|
192 | // ===
|
---|
193 | // WRITE uint32_t TYPE ==0
|
---|
194 | // WRITE uint32_t VERSION==0
|
---|
195 | // WRITE uint32_t LENGTH ==0
|
---|
196 | // ===
|
---|
197 | // Go back and write footer
|
---|
198 |
|
---|
199 | public:
|
---|
200 | DataFileRaw(uint32_t id, MessageImp &imp) : DataFileImp(id, imp), fPosTail(0) { }
|
---|
201 | ~DataFileRaw() { if (fOut.is_open()) Close(); }
|
---|
202 |
|
---|
203 | void WriteBlockHeader(uint32_t type, uint32_t ver, uint32_t cnt, uint32_t len)
|
---|
204 | {
|
---|
205 | const uint32_t val[4] = { type, ver, cnt, len };
|
---|
206 |
|
---|
207 | fOut.write(reinterpret_cast<const char*>(val), sizeof(val));
|
---|
208 | }
|
---|
209 |
|
---|
210 | template<typename T>
|
---|
211 | void WriteValue(const T &t)
|
---|
212 | {
|
---|
213 | fOut.write(reinterpret_cast<const char*>(&t), sizeof(T));
|
---|
214 | }
|
---|
215 |
|
---|
216 | enum
|
---|
217 | {
|
---|
218 | kEndOfFile = 0,
|
---|
219 | kIdentifier = 1,
|
---|
220 | kRunHeader,
|
---|
221 | kBoardHeader,
|
---|
222 | kRunSummary,
|
---|
223 | kEvent,
|
---|
224 | };
|
---|
225 |
|
---|
226 | bool OpenFile(RUN_HEAD *h)
|
---|
227 | {
|
---|
228 | const string name = FormFileName(GetRunId(), "bin");
|
---|
229 | if (access(name.c_str(), F_OK)==0)
|
---|
230 | {
|
---|
231 | Error("File '"+name+"' already exists.");
|
---|
232 | return false;
|
---|
233 | }
|
---|
234 |
|
---|
235 | fFileName = name;
|
---|
236 |
|
---|
237 | errno = 0;
|
---|
238 | fOut.open(name.c_str(), ios_base::out);
|
---|
239 | if (!fOut)
|
---|
240 | {
|
---|
241 | ostringstream str;
|
---|
242 | str << "ofstream::open() failed for '" << name << "': " << strerror(errno) << " [errno=" << errno << "]";
|
---|
243 | Error(str);
|
---|
244 |
|
---|
245 | return false;
|
---|
246 | }
|
---|
247 |
|
---|
248 | fCounter = 0;
|
---|
249 |
|
---|
250 | static uint32_t FACT = 0xFAC77e1e;
|
---|
251 |
|
---|
252 | fOut.write(reinterpret_cast<char*>(&FACT), 4);
|
---|
253 |
|
---|
254 | WriteBlockHeader(kIdentifier, 1, 0, 8);
|
---|
255 | WriteValue(uint32_t(0));
|
---|
256 | WriteValue(GetRunId());
|
---|
257 |
|
---|
258 | WriteBlockHeader(kRunHeader, 1, 0, sizeof(RUN_HEAD)-sizeof(PEVNT_HEADER*));
|
---|
259 | fOut.write(reinterpret_cast<char*>(h), sizeof(RUN_HEAD)-sizeof(PEVNT_HEADER*));
|
---|
260 |
|
---|
261 | for (int i=0; i<40; i++)
|
---|
262 | {
|
---|
263 | WriteBlockHeader(kBoardHeader, 1, i, sizeof(PEVNT_HEADER));
|
---|
264 | fOut.write(reinterpret_cast<char*>(h->FADhead+i), sizeof(PEVNT_HEADER));
|
---|
265 | }
|
---|
266 |
|
---|
267 | // FIXME: Split this
|
---|
268 | const vector<char> block(sizeof(uint32_t)+sizeof(RUN_TAIL));
|
---|
269 | WriteBlockHeader(kRunSummary, 1, 0, block.size());
|
---|
270 |
|
---|
271 | fPosTail = fOut.tellp();
|
---|
272 | fOut.write(block.data(), block.size());
|
---|
273 |
|
---|
274 | if (!fOut)
|
---|
275 | {
|
---|
276 | ostringstream str;
|
---|
277 | str << "ofstream::write() failed for '" << name << "': " << strerror(errno) << " [errno=" << errno << "]";
|
---|
278 | Error(str);
|
---|
279 |
|
---|
280 | return false;
|
---|
281 | }
|
---|
282 |
|
---|
283 | return true;
|
---|
284 | }
|
---|
285 | bool WriteEvt(EVENT *evt)
|
---|
286 | {
|
---|
287 | const int sh = sizeof(EVENT)-2 + NPIX*evt->Roi*2;
|
---|
288 |
|
---|
289 | WriteBlockHeader(kEvent, 1, fCounter++, sh);
|
---|
290 | fOut.write(reinterpret_cast<char*>(evt)+2, sh);
|
---|
291 | return true;
|
---|
292 | }
|
---|
293 | bool Close(RUN_TAIL *tail= 0)
|
---|
294 | {
|
---|
295 | WriteBlockHeader(kEndOfFile, 0, 0, 0);
|
---|
296 |
|
---|
297 | if (tail)
|
---|
298 | {
|
---|
299 | fOut.seekp(fPosTail);
|
---|
300 |
|
---|
301 | WriteValue(uint32_t(1));
|
---|
302 | fOut.write(reinterpret_cast<char*>(tail), sizeof(RUN_TAIL));
|
---|
303 | }
|
---|
304 |
|
---|
305 | if (!fOut)
|
---|
306 | {
|
---|
307 | ostringstream str;
|
---|
308 | str << "ofstream::write() failed for '" << name << "': " << strerror(errno) << " [errno=" << errno << "]";
|
---|
309 | Error(str);
|
---|
310 |
|
---|
311 | return false;
|
---|
312 | }
|
---|
313 |
|
---|
314 | fOut.close();
|
---|
315 |
|
---|
316 | if (!fOut)
|
---|
317 | {
|
---|
318 | ostringstream str;
|
---|
319 | str << "ofstream::close() failed for '" << name << "': " << strerror(errno) << " [errno=" << errno << "]";
|
---|
320 | Error(str);
|
---|
321 |
|
---|
322 | return false;
|
---|
323 | }
|
---|
324 |
|
---|
325 | return true;
|
---|
326 | }
|
---|
327 | };
|
---|
328 |
|
---|
329 | #ifdef HAVE_FITS
|
---|
330 | class DataFileFits : public DataFileImp
|
---|
331 | {
|
---|
332 | CCfits::FITS* fFile; /// The pointer to the CCfits FITS file
|
---|
333 | CCfits::Table* fTable; /// The pointer to the CCfits binary table
|
---|
334 |
|
---|
335 | uint64_t fNumRows; ///the number of rows that have been written already to the FITS file.
|
---|
336 |
|
---|
337 | Converter *fConv;
|
---|
338 |
|
---|
339 | public:
|
---|
340 | DataFileFits(uint32_t runid, MessageImp &imp) :
|
---|
341 | DataFileImp(runid, imp), fFile(0), fNumRows(0), fConv(0)
|
---|
342 | {
|
---|
343 | }
|
---|
344 |
|
---|
345 | // --------------------------------------------------------------------------
|
---|
346 | //
|
---|
347 | //! Default destructor
|
---|
348 | //! The Fits file SHOULD have been closed already, otherwise the informations
|
---|
349 | //! related to the RUN_TAIL will NOT be written to the file.
|
---|
350 | //
|
---|
351 | ~DataFileFits() { Close(); delete fConv; }
|
---|
352 |
|
---|
353 | // --------------------------------------------------------------------------
|
---|
354 | //
|
---|
355 | //! Add a new column to the vectors storing the column data.
|
---|
356 | //! @param names the vector of string storing the columns names
|
---|
357 | //! @param types the vector of string storing the FITS data format
|
---|
358 | //! @param numElems the number of elements in this column
|
---|
359 | //! @param type the char describing the FITS data format
|
---|
360 | //! @param name the name of the particular column to be added.
|
---|
361 | //
|
---|
362 | inline void AddColumnEntry(vector<string>& names, vector<string>& types, int numElems, char type, string name)
|
---|
363 | {
|
---|
364 | names.push_back(name);
|
---|
365 |
|
---|
366 | ostringstream str;
|
---|
367 | if (numElems != 1)
|
---|
368 | str << numElems;
|
---|
369 | str << type;
|
---|
370 | types.push_back(str.str());
|
---|
371 | }
|
---|
372 |
|
---|
373 | // --------------------------------------------------------------------------
|
---|
374 | //
|
---|
375 | //! Writes a single header key entry
|
---|
376 | //! @param name the name of the key
|
---|
377 | //! @param value its value
|
---|
378 | //! @param comment the comment associated to that key
|
---|
379 | //
|
---|
380 | //FIXME this function is a duplicate from the class Fits. should we try to merge it ?
|
---|
381 | template <typename T>
|
---|
382 | void WriteKey(const string &name, const T &value, const string &comment)
|
---|
383 | {
|
---|
384 | try
|
---|
385 | {
|
---|
386 | fTable->addKey(name, value, comment);
|
---|
387 | }
|
---|
388 | catch (CCfits::FitsException e)
|
---|
389 | {
|
---|
390 | ostringstream str;
|
---|
391 | str << "Could not add header key " << name;
|
---|
392 | Error(str);
|
---|
393 | }
|
---|
394 | }
|
---|
395 |
|
---|
396 | template <typename T>
|
---|
397 | void WriteKey(const string &name, const int idx, const T &value, const string &comment)
|
---|
398 | {
|
---|
399 | ostringstream str;
|
---|
400 | str << name << idx;
|
---|
401 |
|
---|
402 | ostringstream com;
|
---|
403 | com << "Board " << setw(2) << idx << ": " << comment;
|
---|
404 |
|
---|
405 | WriteKey(str.str(), value, com.str());
|
---|
406 | }
|
---|
407 |
|
---|
408 | // --------------------------------------------------------------------------
|
---|
409 | //
|
---|
410 | //! DataFileFits constructor. This is the one that should be used, not the default one (parameter-less)
|
---|
411 | //! @param runid This parameter should probably be removed. I first thought it was the run number, but apparently it is not
|
---|
412 | //! @param h a pointer to the RUN_HEAD structure that contains the informations relative to this run
|
---|
413 | //
|
---|
414 | bool OpenFile(RUN_HEAD* h)
|
---|
415 | {
|
---|
416 | //Form filename, based on runid and run-type
|
---|
417 | const string fileName = FormFileName(GetRunId(), "fits");
|
---|
418 | if (access(fileName.c_str(), F_OK)==0)
|
---|
419 | {
|
---|
420 | Error("File '"+fileName+"' already exists.");
|
---|
421 | return false;
|
---|
422 | }
|
---|
423 |
|
---|
424 | fFileName = fileName;
|
---|
425 |
|
---|
426 | /*
|
---|
427 | out <<
|
---|
428 | "SIMPLE = T / file does conform to FITS standard "
|
---|
429 | "BITPIX = 8 / number of bits per data pixel "
|
---|
430 | "NAXIS = 0 / number of data axes "
|
---|
431 | "EXTEND = T / FITS dataset may contain extensions "
|
---|
432 | "COMMENT FITS (Flexible Image Transport System) format is defined in 'Astronomy"
|
---|
433 | "COMMENT and Astrophysics', volume 376, page 359; bibcode: 2001A&A...376..359H "
|
---|
434 | "END ";
|
---|
435 | for (int i=0; i<29; i++)
|
---|
436 | out << " "
|
---|
437 | */
|
---|
438 |
|
---|
439 | //create the FITS object
|
---|
440 | try
|
---|
441 | {
|
---|
442 | fFile = new CCfits::FITS(fileName, CCfits::RWmode::Write);
|
---|
443 | }
|
---|
444 | catch (CCfits::FitsException e)
|
---|
445 | {
|
---|
446 | ostringstream str;
|
---|
447 | str << "Could not open FITS file " << fileName << ": " << e.message();
|
---|
448 | Error(str);
|
---|
449 | return false;
|
---|
450 | }
|
---|
451 |
|
---|
452 | vector<string> colNames;
|
---|
453 | vector<string> dataTypes;
|
---|
454 | AddColumnEntry(colNames, dataTypes, 1, 'J', "EventNum");
|
---|
455 | AddColumnEntry(colNames, dataTypes, 1, 'I', "TriggerType");
|
---|
456 | AddColumnEntry(colNames, dataTypes, 1, 'I', "NumBoards");
|
---|
457 | AddColumnEntry(colNames, dataTypes, 1, 'I', "reserved");
|
---|
458 | AddColumnEntry(colNames, dataTypes, 1, 'J', "SoftTrig");
|
---|
459 | AddColumnEntry(colNames, dataTypes, 2, 'J', "PCTime");
|
---|
460 | AddColumnEntry(colNames, dataTypes, NBOARDS, 'J', "BoardTime");
|
---|
461 | AddColumnEntry(colNames, dataTypes, NPIX, 'I', "StartCell");
|
---|
462 | AddColumnEntry(colNames, dataTypes, NTMARK, 'I', "StartTM");
|
---|
463 | AddColumnEntry(colNames, dataTypes, NPIX*h->Nroi, 'I', "Data");
|
---|
464 |
|
---|
465 | // Write length of physical pipeline (1024)
|
---|
466 |
|
---|
467 | ostringstream fmt;
|
---|
468 | fmt << "I:1;S:1;I:1;I:1;I:1;I:2";
|
---|
469 | fmt << ";I:" << NBOARDS;
|
---|
470 | fmt << ";S:" << NPIX;
|
---|
471 | fmt << ";S:" << NTMARK;
|
---|
472 | fmt << ";S:" << NPIX*h->Nroi;
|
---|
473 |
|
---|
474 | fConv = new Converter(fmt.str());
|
---|
475 |
|
---|
476 | //actually create the table
|
---|
477 | try
|
---|
478 | {
|
---|
479 | fTable = fFile->addTable("Events", 0, colNames, dataTypes);
|
---|
480 | }
|
---|
481 | catch (const CCfits::FitsException &e)
|
---|
482 | {
|
---|
483 | ostringstream str;
|
---|
484 | str << "Could not create FITS table 'Events' in file " << fileName << " reason: " << e.message();
|
---|
485 | Error(str);
|
---|
486 | return false;
|
---|
487 | }
|
---|
488 |
|
---|
489 | if (fTable->rows() != 0)
|
---|
490 | {
|
---|
491 | Error("FITS table created on the fly looks non-empty.");
|
---|
492 | return false;
|
---|
493 | }
|
---|
494 |
|
---|
495 | //write header data
|
---|
496 | //first the "standard" keys
|
---|
497 | WriteKey("EXTREL", 1.0f, "Release Number");
|
---|
498 | WriteKey("TELESCOP", "FACT", "Telescope that acquired this data");
|
---|
499 | WriteKey("ORIGIN", "ISDC", "Institution that wrote the file");
|
---|
500 | WriteKey("CREATOR", "fadctrl", "Program that wrote this file (FACT++ Event Builder)");
|
---|
501 |
|
---|
502 | WriteKey("PACKAGE", PACKAGE_NAME, "Package name");
|
---|
503 | WriteKey("VERSION", PACKAGE_VERSION, "Package description");
|
---|
504 | WriteKey("COMPILED", __DATE__" "__TIME__, "Compile time");
|
---|
505 | WriteKey("REVISION", REVISION, "SVN revision");
|
---|
506 | //WriteKey("CONTACT", PACKAGE_BUGREPORT, "Current package maintainer");
|
---|
507 | //WriteKey("URL", PACKAGE_URL, "Current repositiory location");
|
---|
508 |
|
---|
509 | WriteKey("BLDVER", h->Version, "Builder version");
|
---|
510 | WriteKey("RUNID", GetRunId(), "Run number");
|
---|
511 | WriteKey("RUNTYPE", h->RunType, "Type of run");
|
---|
512 | WriteKey("NBOARD", h->NBoard, "Number of acquisition boards");
|
---|
513 | WriteKey("NPIX", h->NPix, "Number of pixels");
|
---|
514 | WriteKey("NTMARK", h->NTm, "Number of Time marks");
|
---|
515 | WriteKey("NROI", h->Nroi, "Number of slices per pixels");
|
---|
516 |
|
---|
517 | const Time now;
|
---|
518 | WriteKey("TIMESYS", "UTC", "Time system");
|
---|
519 | WriteKey("DATE", now.Iso(), "File creation date");
|
---|
520 | WriteKey("NIGHT", now.NightAsInt(), "Night as int");
|
---|
521 |
|
---|
522 | //FIXME should we also put the start and stop time of the received data ?
|
---|
523 | //now the events header related variables
|
---|
524 | WriteKey("CAMERA", "MGeomCamFACT", "");
|
---|
525 | WriteKey("DAQ", "DRS4", "");
|
---|
526 |
|
---|
527 | Fatal("Write sampling frequency...");
|
---|
528 |
|
---|
529 | // Write a single key for:
|
---|
530 | // -----------------------
|
---|
531 | // Start package flag
|
---|
532 | // package length
|
---|
533 | // version number
|
---|
534 | // status
|
---|
535 | // Prescaler
|
---|
536 |
|
---|
537 | // Write 40 kays for (?)
|
---|
538 | // Phaseshift
|
---|
539 | // DAC
|
---|
540 |
|
---|
541 | for (int i=0; i<h->NBoard; i++)
|
---|
542 | {
|
---|
543 | const PEVNT_HEADER &hh = h->FADhead[i];
|
---|
544 |
|
---|
545 | // Header values whihc won't change during the run
|
---|
546 | WriteKey("BOARDID", i, hh.board_id, "ID");
|
---|
547 | WriteKey("DNA", i, hh.DNA, "DNA");
|
---|
548 | WriteKey("FWVER", i, hh.version_no, "Firmware Version");
|
---|
549 | }
|
---|
550 |
|
---|
551 |
|
---|
552 | /*
|
---|
553 | //now the boards related keywords
|
---|
554 | for (int i=0; i<h->NBoard; i++)
|
---|
555 | {
|
---|
556 | const PEVNT_HEADER &hh = h->FADhead[i];
|
---|
557 |
|
---|
558 | WriteKey("STPKGFG", i, hh.start_package_flag, "Start package flag");
|
---|
559 | WriteKey("PKGLEN", i, hh.package_length, "Package length");
|
---|
560 | WriteKey("STATUS", i, hh.PLLLCK, "");
|
---|
561 |
|
---|
562 | // WriteKey("TRIGCRC", i, hh.trigger_crc, "Trigger CRC");
|
---|
563 | // WriteKey("TRIGTYP", i, hh.trigger_type, "Trigger type");
|
---|
564 | // WriteKey("TRIGID", i, hh.trigger_id, "Trigger ID");
|
---|
565 | // WriteKey("EVTCNTR", i, hh.fad_evt_counter, "FAD Event Counter");
|
---|
566 | // WriteKey("REFCLK", i, hh.REFCLK_frequency, "Reference Clock Frequency");
|
---|
567 |
|
---|
568 | WriteKey("PHASESH", i, hh.adc_clock_phase_shift, "ADC clock phase shift");
|
---|
569 | WriteKey("TRGGEN", i, hh.number_of_triggers_to_generate, "Number of triggers to generate");
|
---|
570 | WriteKey("PRESC", i, hh.trigger_generator_prescaler, "Trigger generator prescaler");
|
---|
571 | WriteKey("RUNNB", i, hh.runnumber, "Run number");
|
---|
572 |
|
---|
573 | WriteKey("TIME", i, hh.time, "Time");
|
---|
574 |
|
---|
575 | // for (int j=0;j<NTemp;j++)
|
---|
576 | // {
|
---|
577 | // str.str(""); str2.str("");
|
---|
578 | // str << "DRS_T" << i << j;
|
---|
579 | // str2 << "DRS temperature #" << i << " " << j;
|
---|
580 | // WriteKey(str.str(), h->FADhead[i].drs_temperature[j], str2.str());
|
---|
581 | // }
|
---|
582 | for (int j=0;j<NDAC;j++)
|
---|
583 | WriteKey("DAC", i*NDAC+j, hh.dac[j], "DAC");
|
---|
584 | }
|
---|
585 | */
|
---|
586 |
|
---|
587 | //Last but not least, add header keys that will be updated when closing the file
|
---|
588 | WriteFooter(NULL);
|
---|
589 |
|
---|
590 | return true;
|
---|
591 | }
|
---|
592 |
|
---|
593 |
|
---|
594 | int WriteColumns(size_t &start, size_t size, const void *e)
|
---|
595 | {
|
---|
596 | int status = 0;
|
---|
597 | fits_write_tblbytes(fFile->fitsPointer(), fNumRows, start, size,
|
---|
598 | (unsigned char*)e, &status);
|
---|
599 | if (status)
|
---|
600 | {
|
---|
601 | char text[30];//max length of cfitsio error strings (from doc)
|
---|
602 | fits_get_errstatus(status, text);
|
---|
603 |
|
---|
604 | ostringstream str;
|
---|
605 | str << "Writing FITS row " << fNumRows << ": " << text << " (file_write_tblbytes, rc=" << status << ")";
|
---|
606 | Error(str);
|
---|
607 | }
|
---|
608 |
|
---|
609 | start += size;
|
---|
610 | return status;
|
---|
611 | }
|
---|
612 |
|
---|
613 | // --------------------------------------------------------------------------
|
---|
614 | //
|
---|
615 | //! This writes one event to the file
|
---|
616 | //! @param e the pointer to the EVENT
|
---|
617 | //
|
---|
618 | virtual bool WriteEvt(EVENT *e)
|
---|
619 | {
|
---|
620 | //FIXME As discussed earlier, we do not swap the bytes yet.
|
---|
621 | fTable->makeThisCurrent();
|
---|
622 |
|
---|
623 | //insert a new row
|
---|
624 | int status(0);
|
---|
625 | if (fits_insert_rows(fTable->fitsPointer(), fNumRows, 1, &status))
|
---|
626 | {
|
---|
627 | char text[30];//max length of cfitsio error strings (from doc)
|
---|
628 | fits_get_errstatus(status, text);
|
---|
629 |
|
---|
630 | ostringstream str;
|
---|
631 | str << "Inserting row " << fNumRows << " into " << fFileName << ": " << text << " (fits_insert_rows, rc=" << status << ")";
|
---|
632 | Error(str);
|
---|
633 |
|
---|
634 | return false;
|
---|
635 | }
|
---|
636 | fNumRows++;
|
---|
637 |
|
---|
638 | const vector<char> data = fConv->ToFits(((char*)e)+2, sizeof(EVENT)+NPIX*e->Roi*2-2);
|
---|
639 |
|
---|
640 | // column size pointer
|
---|
641 | size_t col = 1;
|
---|
642 | if (!WriteColumns(col, data.size(), data.data()))
|
---|
643 | return true;
|
---|
644 |
|
---|
645 | //TODO output an error
|
---|
646 | return false;
|
---|
647 |
|
---|
648 | /*
|
---|
649 | //write the data, chunk by chunk
|
---|
650 | //FIXME hard-coded size corresponds to current variables of the event, in bytes.
|
---|
651 | //FIXME no padding was taken into account. Because smallest member is 2 bytes, I don't think that this should be a problem.
|
---|
652 | const long sizeInBytesOfEventBeforePointers = 16;
|
---|
653 |
|
---|
654 | long col = 1;
|
---|
655 | if (FitsWriteTblBytes(col, sizeInBytesOfEventBeforePointers, e))
|
---|
656 | {
|
---|
657 | //TODO output an error
|
---|
658 | return false;
|
---|
659 | }
|
---|
660 | if (FitsWriteTblBytes(col, NBOARDS*2, e->BoardTime))
|
---|
661 | {
|
---|
662 | //TODO output an error
|
---|
663 | return false;
|
---|
664 | }
|
---|
665 | if (FitsWriteTblBytes(col, NPIX*2, e->StartPix))
|
---|
666 | {
|
---|
667 | //TODO output an error
|
---|
668 | return false;
|
---|
669 | }
|
---|
670 | if (FitsWriteTblBytes(col, NTMARK*2, e->StartTM))
|
---|
671 | {
|
---|
672 | //TODO output an error
|
---|
673 | return false;
|
---|
674 | }
|
---|
675 | if (FitsWriteTblBytes(col, NPIX*fRoi*2, e->Adc_Data))
|
---|
676 | {
|
---|
677 | //TODO output an error
|
---|
678 | return false;
|
---|
679 | }
|
---|
680 | return true;*/
|
---|
681 | }
|
---|
682 |
|
---|
683 | void WriteFooter(RUN_TAIL *rt)
|
---|
684 | {
|
---|
685 | //write final header keys
|
---|
686 | fTable->makeThisCurrent();
|
---|
687 |
|
---|
688 | WriteKey("NBEVTOK", rt ? rt->nEventsOk : uint32_t(0),
|
---|
689 | "How many events were written");
|
---|
690 |
|
---|
691 | WriteKey("NBEVTREJ", rt ? rt->nEventsRej : uint32_t(0),
|
---|
692 | "How many events were rejected by SW-trig");
|
---|
693 |
|
---|
694 | WriteKey("NBEVTBAD", rt ? rt->nEventsBad : uint32_t(0),
|
---|
695 | "How many events were rejected by Error");
|
---|
696 |
|
---|
697 | //FIXME shouldn't we convert start and stop time to MjD first ?
|
---|
698 | //FIXME shouldn't we also add an MjD reference ?
|
---|
699 |
|
---|
700 | WriteKey("TSTART", rt ? rt->PCtime0 : uint32_t(0),
|
---|
701 | "Time when first event received");
|
---|
702 |
|
---|
703 | WriteKey("TSTOP", rt ? rt->PCtimeX : uint32_t(0),
|
---|
704 | "Time when last event received");
|
---|
705 | }
|
---|
706 |
|
---|
707 | // --------------------------------------------------------------------------
|
---|
708 | //
|
---|
709 | //! Closes the file, and before this it write the TAIL data
|
---|
710 | //! @param rt the pointer to the RUN_TAIL data structure
|
---|
711 | //
|
---|
712 | virtual bool Close(RUN_TAIL *rt = 0)
|
---|
713 | {
|
---|
714 | if (!fFile)
|
---|
715 | return false;
|
---|
716 |
|
---|
717 | WriteFooter(rt);
|
---|
718 |
|
---|
719 | delete fFile;
|
---|
720 | fFile = NULL;
|
---|
721 |
|
---|
722 | return true;
|
---|
723 | }
|
---|
724 |
|
---|
725 | };
|
---|
726 | #else
|
---|
727 | #define DataFileFits DataFileRaw
|
---|
728 | #endif
|
---|
729 |
|
---|
730 | #include "DimWriteStatistics.h"
|
---|
731 |
|
---|
732 | class EventBuilderWrapper
|
---|
733 | {
|
---|
734 | public:
|
---|
735 | // FIXME
|
---|
736 | static EventBuilderWrapper *This;
|
---|
737 |
|
---|
738 | MessageImp &fMsg;
|
---|
739 |
|
---|
740 | private:
|
---|
741 | boost::thread fThread;
|
---|
742 |
|
---|
743 | enum CommandStates_t // g_runStat
|
---|
744 | {
|
---|
745 | kAbort = -2, // quit as soon as possible ('abort')
|
---|
746 | kExit = -1, // stop reading, quit when buffered events done ('exit')
|
---|
747 | kInitialize = 0, // 'initialize' (e.g. dim not yet started)
|
---|
748 | kHybernate = 1, // do nothing for long time ('hybernate') [wakeup within ~1sec]
|
---|
749 | kSleep = 2, // do nothing ('sleep') [wakeup within ~10msec]
|
---|
750 | kModeFlush = 10, // read data from camera, but skip them ('flush')
|
---|
751 | kModeTest = 20, // read data and process them, but do not write to disk ('test')
|
---|
752 | kModeFlag = 30, // read data, process and write all to disk ('flag')
|
---|
753 | kModeRun = 40, // read data, process and write selected to disk ('run')
|
---|
754 | };
|
---|
755 |
|
---|
756 | enum
|
---|
757 | {
|
---|
758 | kCurrent = 0,
|
---|
759 | kTotal = 1,
|
---|
760 | kEventId = 2,
|
---|
761 | kTriggerId = 3,
|
---|
762 | };
|
---|
763 |
|
---|
764 | enum FileFormat_t
|
---|
765 | {
|
---|
766 | kNone = 0,
|
---|
767 | kDebug,
|
---|
768 | kFits,
|
---|
769 | kRaw
|
---|
770 | };
|
---|
771 |
|
---|
772 | FileFormat_t fFileFormat;
|
---|
773 |
|
---|
774 |
|
---|
775 | uint32_t fMaxRun;
|
---|
776 | uint32_t fLastOpened;
|
---|
777 | uint32_t fLastClosed;
|
---|
778 | uint32_t fNumEvts[4];
|
---|
779 |
|
---|
780 | DimWriteStatistics fDimWriteStats;
|
---|
781 | DimDescribedService fDimRuns;
|
---|
782 | DimDescribedService fDimEvents;
|
---|
783 | DimDescribedService fDimEventData;
|
---|
784 | DimDescribedService fDimFwVersion;
|
---|
785 | DimDescribedService fDimRunNumber;
|
---|
786 | DimDescribedService fDimStatus;
|
---|
787 | DimDescribedService fDimDNA;
|
---|
788 | DimDescribedService fDimTemperature;
|
---|
789 | DimDescribedService fDimPrescaler;
|
---|
790 | DimDescribedService fDimRefClock;
|
---|
791 | DimDescribedService fDimStatistics1;
|
---|
792 | DimDescribedService fDimStatistics2;
|
---|
793 |
|
---|
794 | bool fDebugStream;
|
---|
795 | bool fDebugRead;
|
---|
796 | bool fDebugLog;
|
---|
797 |
|
---|
798 | uint32_t fRunNumber;
|
---|
799 |
|
---|
800 | void InitRunNumber()
|
---|
801 | {
|
---|
802 | // FIXME: Add a check that we are not too close to noon!
|
---|
803 | const int night = Time().NightAsInt();
|
---|
804 |
|
---|
805 | fRunNumber = 1000;
|
---|
806 |
|
---|
807 | while (--fRunNumber>0)
|
---|
808 | {
|
---|
809 | const string name = DataFileImp::FormFileName(fRunNumber, "");
|
---|
810 |
|
---|
811 | if (access((name+"bin").c_str(), F_OK) == 0)
|
---|
812 | break;
|
---|
813 | if (access((name+"fits").c_str(), F_OK) == 0)
|
---|
814 | break;
|
---|
815 | }
|
---|
816 |
|
---|
817 | fRunNumber++;
|
---|
818 |
|
---|
819 | ostringstream str;
|
---|
820 | str << "Starting with run number " << fRunNumber;
|
---|
821 | fMsg.Message(str);
|
---|
822 |
|
---|
823 | fMsg.Info(" ==> TODO: Run-number detection doesn't work when noon passes!");
|
---|
824 | fMsg.Info(" ==> TODO: Crosscheck with database!");
|
---|
825 | }
|
---|
826 |
|
---|
827 | public:
|
---|
828 | EventBuilderWrapper(MessageImp &imp) : fMsg(imp),
|
---|
829 | fFileFormat(kNone), fMaxRun(0), fLastOpened(0), fLastClosed(0),
|
---|
830 | fDimWriteStats ("FAD_CONTROL", imp),
|
---|
831 | fDimRuns ("FAD_CONTROL/RUNS", "I:5;C", ""),
|
---|
832 | fDimEvents ("FAD_CONTROL/EVENTS", "I:4", ""),
|
---|
833 | fDimEventData ("FAD_CONTROL/EVENT_DATA", "S:1;I:1;S:1;I:1;I:2;I:40;S:1440;S:160;S", ""),
|
---|
834 | fDimFwVersion ("FAD_CONTROL/FIRMWARE_VERSION", "F:42", ""),
|
---|
835 | fDimRunNumber ("FAD_CONTROL/RUN_NUMBER", "I:42", ""),
|
---|
836 | fDimStatus ("FAD_CONTROL/STATUS", "S:42", ""),
|
---|
837 | fDimDNA ("FAD_CONTROL/DNA", "X:40", ""),
|
---|
838 | fDimTemperature ("FAD_CONTROL/TEMPERATURE", "F:82", ""),
|
---|
839 | fDimPrescaler ("FAD_CONTROL/PRESCALER", "S:42", ""),
|
---|
840 | fDimRefClock ("FAD_CONTROL/REFERENCE_CLOCK", "I:42", ""),
|
---|
841 | fDimStatistics1 ("FAD_CONTROL/STATISTICS1", "I:3;I:2;X:4;I:3;I:3;I:40;I:1;I:2;C:40;I:40;I:40;X:40", ""),
|
---|
842 | fDimStatistics2 ("FAD_CONTROL/STATISTICS2", "I:1;I:280;X:40;I:40;I:4;I:4;I:2;I:2;I:3;C:40", ""),
|
---|
843 | fDebugStream(false), fDebugRead(false), fDebugLog(false)
|
---|
844 | {
|
---|
845 | if (This)
|
---|
846 | throw logic_error("EventBuilderWrapper cannot be instantiated twice.");
|
---|
847 |
|
---|
848 | This = this;
|
---|
849 |
|
---|
850 | memset(fNumEvts, 0, sizeof(fNumEvts));
|
---|
851 |
|
---|
852 | fDimEvents.Update(fNumEvts);
|
---|
853 |
|
---|
854 | for (size_t i=0; i<40; i++)
|
---|
855 | ConnectSlot(i, tcp::endpoint());
|
---|
856 |
|
---|
857 | InitRunNumber();
|
---|
858 | }
|
---|
859 | virtual ~EventBuilderWrapper()
|
---|
860 | {
|
---|
861 | Abort();
|
---|
862 |
|
---|
863 | // FIXME: Used timed_join and abort afterwards
|
---|
864 | // What's the maximum time the eb need to abort?
|
---|
865 | fThread.join();
|
---|
866 | //ffMsg.Info("EventBuilder stopped.");
|
---|
867 |
|
---|
868 | for (vector<DataFileImp*>::iterator it=fFiles.begin(); it!=fFiles.end(); it++)
|
---|
869 | delete *it;
|
---|
870 | }
|
---|
871 |
|
---|
872 | struct RunDescription
|
---|
873 | {
|
---|
874 | uint32_t maxtime;
|
---|
875 | uint32_t maxevt;
|
---|
876 |
|
---|
877 | FAD::Configuration reference;
|
---|
878 | };
|
---|
879 |
|
---|
880 | map<uint32_t, RunDescription> fExpectedRuns;
|
---|
881 |
|
---|
882 | uint32_t StartNewRun(int64_t maxtime, int64_t maxevt, const FAD::Configuration &ref)
|
---|
883 | {
|
---|
884 | if (maxtime<=0 || maxtime>24*60*60)
|
---|
885 | maxtime = 24*60*60;
|
---|
886 | if (maxevt<=0 || maxevt>INT32_MAX)
|
---|
887 | maxevt = INT32_MAX;
|
---|
888 |
|
---|
889 | const RunDescription descr =
|
---|
890 | {
|
---|
891 | uint32_t(maxtime),
|
---|
892 | uint32_t(maxevt),
|
---|
893 | ref
|
---|
894 | };
|
---|
895 |
|
---|
896 | fExpectedRuns[fRunNumber] = descr;
|
---|
897 | return fRunNumber++;
|
---|
898 | }
|
---|
899 |
|
---|
900 | bool IsThreadRunning()
|
---|
901 | {
|
---|
902 | return !fThread.timed_join(boost::posix_time::microseconds(0));
|
---|
903 | }
|
---|
904 |
|
---|
905 | void SetMaxMemory(unsigned int mb) const
|
---|
906 | {
|
---|
907 | /*
|
---|
908 | if (mb*1000000<GetUsedMemory())
|
---|
909 | {
|
---|
910 | // ffMsg.Warn("...");
|
---|
911 | return;
|
---|
912 | }*/
|
---|
913 |
|
---|
914 | g_maxMem = size_t(mb)*1000000;
|
---|
915 | }
|
---|
916 |
|
---|
917 | void StartThread(const vector<tcp::endpoint> &addr)
|
---|
918 | {
|
---|
919 | if (IsThreadRunning())
|
---|
920 | {
|
---|
921 | fMsg.Warn("Start - EventBuilder still running");
|
---|
922 | return;
|
---|
923 | }
|
---|
924 |
|
---|
925 | fLastMessage.clear();
|
---|
926 |
|
---|
927 | for (size_t i=0; i<40; i++)
|
---|
928 | ConnectSlot(i, addr[i]);
|
---|
929 |
|
---|
930 | g_runStat = kModeRun;
|
---|
931 |
|
---|
932 | fMsg.Message("Starting EventBuilder thread");
|
---|
933 |
|
---|
934 | fThread = boost::thread(StartEvtBuild);
|
---|
935 | }
|
---|
936 | void ConnectSlot(unsigned int i, const tcp::endpoint &addr)
|
---|
937 | {
|
---|
938 | if (i>39)
|
---|
939 | return;
|
---|
940 |
|
---|
941 | if (addr==tcp::endpoint())
|
---|
942 | {
|
---|
943 | DisconnectSlot(i);
|
---|
944 | return;
|
---|
945 | }
|
---|
946 |
|
---|
947 | g_port[i].sockAddr.sin_family = AF_INET;
|
---|
948 | g_port[i].sockAddr.sin_addr.s_addr = htonl(addr.address().to_v4().to_ulong());
|
---|
949 | g_port[i].sockAddr.sin_port = htons(addr.port());
|
---|
950 | // In this order
|
---|
951 | g_port[i].sockDef = 1;
|
---|
952 | }
|
---|
953 | void DisconnectSlot(unsigned int i)
|
---|
954 | {
|
---|
955 | if (i>39)
|
---|
956 | return;
|
---|
957 |
|
---|
958 | g_port[i].sockDef = 0;
|
---|
959 | // In this order
|
---|
960 | g_port[i].sockAddr.sin_family = AF_INET;
|
---|
961 | g_port[i].sockAddr.sin_addr.s_addr = 0;
|
---|
962 | g_port[i].sockAddr.sin_port = 0;
|
---|
963 | }
|
---|
964 | void IgnoreSlot(unsigned int i)
|
---|
965 | {
|
---|
966 | if (i>39)
|
---|
967 | return;
|
---|
968 | if (g_port[i].sockAddr.sin_port==0)
|
---|
969 | return;
|
---|
970 |
|
---|
971 | g_port[i].sockDef = -1;
|
---|
972 | }
|
---|
973 |
|
---|
974 |
|
---|
975 | void Abort()
|
---|
976 | {
|
---|
977 | fMsg.Message("Signal abort to EventBuilder thread...");
|
---|
978 | g_runStat = kAbort;
|
---|
979 | }
|
---|
980 |
|
---|
981 | void ResetThread(bool soft)
|
---|
982 | {
|
---|
983 | /*
|
---|
984 | if (g_reset > 0)
|
---|
985 |
|
---|
986 | * suspend reading
|
---|
987 | * reset = g_reset;
|
---|
988 | * g_reset=0
|
---|
989 |
|
---|
990 | * reset% 10
|
---|
991 | == 0 leave event Buffers as they are
|
---|
992 | == 1 let all buffers drain (write (incomplete) events)
|
---|
993 | > 1 flush all buffers (do not write buffered events)
|
---|
994 |
|
---|
995 | * (reset/10)%10
|
---|
996 | > 0 close all sockets and destroy them (also free the
|
---|
997 | allocated read-buffers)
|
---|
998 | recreate before resuming operation
|
---|
999 | [ this is more than just close/open that can be
|
---|
1000 | triggered by e.g. close/open the base-socket ]
|
---|
1001 |
|
---|
1002 | * (reset/100)%10
|
---|
1003 | > 0 close all open run-files
|
---|
1004 |
|
---|
1005 | * (reset/1000)
|
---|
1006 | sleep so many seconds before resuming operation
|
---|
1007 | (does not (yet) take into account time left when waiting
|
---|
1008 | for buffers getting empty ...)
|
---|
1009 |
|
---|
1010 | * resume_reading
|
---|
1011 |
|
---|
1012 | */
|
---|
1013 | fMsg.Message("Signal reset to EventBuilder thread...");
|
---|
1014 | g_reset = soft ? 101 : 102;
|
---|
1015 | }
|
---|
1016 |
|
---|
1017 | void Exit()
|
---|
1018 | {
|
---|
1019 | fMsg.Message("Signal exit to EventBuilder thread...");
|
---|
1020 | g_runStat = kExit;
|
---|
1021 | }
|
---|
1022 |
|
---|
1023 | /*
|
---|
1024 | void Wait()
|
---|
1025 | {
|
---|
1026 | fThread.join();
|
---|
1027 | ffMsg.Message("EventBuilder stopped.");
|
---|
1028 | }*/
|
---|
1029 |
|
---|
1030 | void Hybernate() const { g_runStat = kHybernate; }
|
---|
1031 | void Sleep() const { g_runStat = kSleep; }
|
---|
1032 | void FlushMode() const { g_runStat = kModeFlush; }
|
---|
1033 | void TestMode() const { g_runStat = kModeTest; }
|
---|
1034 | void FlagMode() const { g_runStat = kModeFlag; }
|
---|
1035 | void RunMode() const { g_runStat = kModeRun; }
|
---|
1036 |
|
---|
1037 | // FIXME: To be removed
|
---|
1038 | void SetMode(int mode) const { g_runStat = mode; }
|
---|
1039 |
|
---|
1040 | bool IsConnected(int i) const { return gi_NumConnect[i]==7; }
|
---|
1041 | bool IsConnecting(int i) const { return !IsConnected(i) && !IsDisconnected(i); }
|
---|
1042 | bool IsDisconnected(int i) const { return gi_NumConnect[i]<=0 && g_port[i].sockDef==0; }
|
---|
1043 | int GetNumConnected(int i) const { return gi_NumConnect[i]; }
|
---|
1044 |
|
---|
1045 | void SetIgnore(int i, bool b) const { if (g_port[i].sockDef!=0) g_port[i].sockDef=b?-1:1; }
|
---|
1046 | bool IsIgnored(int i) const { return g_port[i].sockDef==-1; }
|
---|
1047 |
|
---|
1048 | void SetOutputFormat(FileFormat_t f) { fFileFormat = f; }
|
---|
1049 |
|
---|
1050 | void SetDebugLog(bool b) { fDebugLog = b; }
|
---|
1051 |
|
---|
1052 | void SetDebugStream(bool b)
|
---|
1053 | {
|
---|
1054 | fDebugStream = b;
|
---|
1055 | if (b)
|
---|
1056 | return;
|
---|
1057 |
|
---|
1058 | for (int i=0; i<40; i++)
|
---|
1059 | {
|
---|
1060 | if (!fDumpStream[i].is_open())
|
---|
1061 | continue;
|
---|
1062 |
|
---|
1063 | fDumpStream[i].close();
|
---|
1064 |
|
---|
1065 | ostringstream name;
|
---|
1066 | name << "socket_dump-" << setfill('0') << setw(2) << i << ".bin";
|
---|
1067 | fMsg.Message("Closed file '"+name.str()+"'");
|
---|
1068 | }
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 | void SetDebugRead(bool b)
|
---|
1072 | {
|
---|
1073 | fDebugRead = b;
|
---|
1074 | if (b || !fDumpRead.is_open())
|
---|
1075 | return;
|
---|
1076 |
|
---|
1077 | fDumpRead.close();
|
---|
1078 | fMsg.Message("Closed file 'socket_events.txt'");
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 | // size_t GetUsedMemory() const { return gi_usedMem; }
|
---|
1082 |
|
---|
1083 | virtual int CloseOpenFiles() { CloseRunFile(0, 0, 0); return 0; }
|
---|
1084 |
|
---|
1085 |
|
---|
1086 | /*
|
---|
1087 | struct OpenFileToDim
|
---|
1088 | {
|
---|
1089 | int code;
|
---|
1090 | char fileName[FILENAME_MAX];
|
---|
1091 | };
|
---|
1092 |
|
---|
1093 | SignalRunOpened(runid, filename);
|
---|
1094 | // Send num open files
|
---|
1095 | // Send runid, (more info about the run?), filename via dim
|
---|
1096 |
|
---|
1097 | SignalEvtWritten(runid);
|
---|
1098 | // Send num events written of newest file
|
---|
1099 |
|
---|
1100 | SignalRunClose(runid);
|
---|
1101 | // Send new num open files
|
---|
1102 | // Send empty file-name if no file is open
|
---|
1103 |
|
---|
1104 | */
|
---|
1105 |
|
---|
1106 | // -------------- Mapped event builder callbacks ------------------
|
---|
1107 |
|
---|
1108 | void UpdateRuns(const string &fname="")
|
---|
1109 | {
|
---|
1110 | uint32_t values[5] =
|
---|
1111 | {
|
---|
1112 | static_cast<uint32_t>(fFiles.size()),
|
---|
1113 | 0xffffffff,
|
---|
1114 | 0,
|
---|
1115 | fLastOpened,
|
---|
1116 | fLastClosed
|
---|
1117 | };
|
---|
1118 |
|
---|
1119 | for (vector<DataFileImp*>::const_iterator it=fFiles.begin();
|
---|
1120 | it!=fFiles.end(); it++)
|
---|
1121 | {
|
---|
1122 | const DataFileImp *file = *it;
|
---|
1123 |
|
---|
1124 | if (file->GetRunId()<values[1])
|
---|
1125 | values[1] = file->GetRunId();
|
---|
1126 |
|
---|
1127 | if (file->GetRunId()>values[2])
|
---|
1128 | values[2] = file->GetRunId();
|
---|
1129 | }
|
---|
1130 |
|
---|
1131 | fMaxRun = values[2];
|
---|
1132 |
|
---|
1133 | vector<char> data(sizeof(values)+fname.size()+1);
|
---|
1134 | memcpy(data.data(), values, sizeof(values));
|
---|
1135 | strcpy(data.data()+sizeof(values), fname.c_str());
|
---|
1136 |
|
---|
1137 | fDimRuns.Update(data);
|
---|
1138 | }
|
---|
1139 |
|
---|
1140 | vector<DataFileImp*> fFiles;
|
---|
1141 |
|
---|
1142 | FileHandle_t runOpen(uint32_t runid, RUN_HEAD *h, size_t)
|
---|
1143 | {
|
---|
1144 | // Check if file already exists...
|
---|
1145 | DataFileImp *file = 0;
|
---|
1146 | switch (fFileFormat)
|
---|
1147 | {
|
---|
1148 | case kNone: file = new DataFileNone(runid, fMsg); break;
|
---|
1149 | case kDebug: file = new DataFileDebug(runid, fMsg); break;
|
---|
1150 | case kFits: file = new DataFileFits(runid, fMsg); break;
|
---|
1151 | case kRaw: file = new DataFileRaw(runid, fMsg); break;
|
---|
1152 | }
|
---|
1153 |
|
---|
1154 | try
|
---|
1155 | {
|
---|
1156 | if (!file->OpenFile(h))
|
---|
1157 | return 0;
|
---|
1158 | }
|
---|
1159 | catch (const exception &e)
|
---|
1160 | {
|
---|
1161 | return 0;
|
---|
1162 | }
|
---|
1163 |
|
---|
1164 | fFiles.push_back(file);
|
---|
1165 |
|
---|
1166 | ostringstream str;
|
---|
1167 | str << "Opened: " << file->GetFileName() << " (" << file->GetRunId() << ")";
|
---|
1168 | fMsg.Info(str);
|
---|
1169 |
|
---|
1170 | fDimWriteStats.FileOpened(file->GetFileName());
|
---|
1171 |
|
---|
1172 | fLastOpened = runid;
|
---|
1173 | UpdateRuns(file->GetFileName());
|
---|
1174 |
|
---|
1175 | fNumEvts[kEventId] = 0;
|
---|
1176 | fNumEvts[kTriggerId] = 0;
|
---|
1177 |
|
---|
1178 | fNumEvts[kCurrent] = 0;
|
---|
1179 | fDimEvents.Update(fNumEvts);
|
---|
1180 | // fDimCurrentEvent.Update(uint32_t(0));
|
---|
1181 |
|
---|
1182 | return reinterpret_cast<FileHandle_t>(file);
|
---|
1183 | }
|
---|
1184 |
|
---|
1185 | int runWrite(FileHandle_t handler, EVENT *e, size_t)
|
---|
1186 | {
|
---|
1187 | DataFileImp *file = reinterpret_cast<DataFileImp*>(handler);
|
---|
1188 |
|
---|
1189 | if (!file->WriteEvt(e))
|
---|
1190 | return -1;
|
---|
1191 |
|
---|
1192 | if (file->GetRunId()==fMaxRun)
|
---|
1193 | {
|
---|
1194 | fNumEvts[kCurrent]++;
|
---|
1195 | fNumEvts[kEventId] = e->EventNum;
|
---|
1196 | fNumEvts[kTriggerId] = e->TriggerType;
|
---|
1197 | }
|
---|
1198 |
|
---|
1199 | fNumEvts[kTotal]++;
|
---|
1200 |
|
---|
1201 | static Time oldt(boost::date_time::neg_infin);
|
---|
1202 | Time newt;
|
---|
1203 | if (newt>oldt+boost::posix_time::seconds(1))
|
---|
1204 | {
|
---|
1205 | fDimEvents.Update(fNumEvts);
|
---|
1206 | oldt = newt;
|
---|
1207 | }
|
---|
1208 |
|
---|
1209 |
|
---|
1210 | // ===> SignalEvtWritten(runid);
|
---|
1211 | // Send num events written of newest file
|
---|
1212 |
|
---|
1213 | /* close run runId (all all runs if runId=0) */
|
---|
1214 | /* return: 0=close scheduled / >0 already closed / <0 does not exist */
|
---|
1215 | //CloseRunFile(file->GetRunId(), time(NULL)+2) ;
|
---|
1216 |
|
---|
1217 | return 0;
|
---|
1218 | }
|
---|
1219 |
|
---|
1220 | int runClose(FileHandle_t handler, RUN_TAIL *tail, size_t)
|
---|
1221 | {
|
---|
1222 | DataFileImp *file = reinterpret_cast<DataFileImp*>(handler);
|
---|
1223 |
|
---|
1224 | const vector<DataFileImp*>::iterator it = find(fFiles.begin(), fFiles.end(), file);
|
---|
1225 | if (it==fFiles.end())
|
---|
1226 | {
|
---|
1227 | ostringstream str;
|
---|
1228 | str << "File handler (" << handler << ") requested to close by event builder doesn't exist.";
|
---|
1229 | fMsg.Fatal(str);
|
---|
1230 | return -1;
|
---|
1231 | }
|
---|
1232 |
|
---|
1233 | fFiles.erase(it);
|
---|
1234 |
|
---|
1235 | fLastClosed = file->GetRunId();
|
---|
1236 | UpdateRuns();
|
---|
1237 |
|
---|
1238 | fDimEvents.Update(fNumEvts);
|
---|
1239 |
|
---|
1240 | const bool rc = file->Close(tail);
|
---|
1241 | if (!rc)
|
---|
1242 | {
|
---|
1243 | // Error message
|
---|
1244 | }
|
---|
1245 |
|
---|
1246 | ostringstream str;
|
---|
1247 | str << "Closed: " << file->GetFileName() << " (" << file->GetRunId() << ")";
|
---|
1248 | fMsg.Info(str);
|
---|
1249 |
|
---|
1250 | delete file;
|
---|
1251 |
|
---|
1252 | // ==> SignalRunClose(runid);
|
---|
1253 | // Send new num open files
|
---|
1254 | // Send empty file-name if no file is open
|
---|
1255 |
|
---|
1256 | return rc ? 0 : -1;
|
---|
1257 | }
|
---|
1258 |
|
---|
1259 | ofstream fDumpStream[40];
|
---|
1260 |
|
---|
1261 | void debugStream(int isock, void *buf, int len)
|
---|
1262 | {
|
---|
1263 | if (!fDebugStream)
|
---|
1264 | return;
|
---|
1265 |
|
---|
1266 | const int slot = isock/7;
|
---|
1267 | if (slot<0 || slot>39)
|
---|
1268 | return;
|
---|
1269 |
|
---|
1270 | if (!fDumpStream[slot].is_open())
|
---|
1271 | {
|
---|
1272 | ostringstream name;
|
---|
1273 | name << "socket_dump-" << setfill('0') << setw(2) << slot << ".bin";
|
---|
1274 |
|
---|
1275 | fDumpStream[slot].open(name.str().c_str(), ios::app);
|
---|
1276 | if (!fDumpStream[slot])
|
---|
1277 | {
|
---|
1278 | ostringstream str;
|
---|
1279 | str << "Open file '" << name << "': " << strerror(errno) << " (errno=" << errno << ")";
|
---|
1280 | fMsg.Error(str);
|
---|
1281 |
|
---|
1282 | return;
|
---|
1283 | }
|
---|
1284 |
|
---|
1285 | fMsg.Message("Opened file '"+name.str()+"' for writing.");
|
---|
1286 | }
|
---|
1287 |
|
---|
1288 | fDumpStream[slot].write(reinterpret_cast<const char*>(buf), len);
|
---|
1289 | }
|
---|
1290 |
|
---|
1291 | ofstream fDumpRead; // Stream to possibly dump docket events
|
---|
1292 |
|
---|
1293 | void debugRead(int isock, int ibyte, uint32_t event, uint32_t ftmevt, uint32_t runno, int state, uint32_t tsec, uint32_t tusec)
|
---|
1294 | {
|
---|
1295 | // isock = socketID (0-279)
|
---|
1296 | // ibyte = #bytes gelesen
|
---|
1297 | // event = eventId (oder 0 wenn noch nicht bekannt)
|
---|
1298 | // state : 1=finished reading data
|
---|
1299 | // 0=reading data
|
---|
1300 | // -1=start reading data (header)
|
---|
1301 | // -2=start reading data,
|
---|
1302 | // eventId not known yet (too little data)
|
---|
1303 | // tsec, tusec = time when reading seconds, microseconds
|
---|
1304 | //
|
---|
1305 | if (!fDebugRead || ibyte==0)
|
---|
1306 | return;
|
---|
1307 |
|
---|
1308 | if (!fDumpRead.is_open())
|
---|
1309 | {
|
---|
1310 | fDumpRead.open("socket_events.txt", ios::app);
|
---|
1311 | if (!fDumpRead)
|
---|
1312 | {
|
---|
1313 | ostringstream str;
|
---|
1314 | str << "Open file 'socket_events.txt': " << strerror(errno) << " (errno=" << errno << ")";
|
---|
1315 | fMsg.Error(str);
|
---|
1316 |
|
---|
1317 | return;
|
---|
1318 | }
|
---|
1319 |
|
---|
1320 | fMsg.Message("Opened file 'socket_events.txt' for writing.");
|
---|
1321 |
|
---|
1322 | fDumpRead << "# START: " << Time().GetAsStr() << endl;
|
---|
1323 | fDumpRead << "# state time_sec time_usec socket slot runno event_id trigger_id bytes_received" << endl;
|
---|
1324 | }
|
---|
1325 |
|
---|
1326 | fDumpRead
|
---|
1327 | << setw(2) << state << " "
|
---|
1328 | << setw(8) << tsec << " "
|
---|
1329 | << setw(9) << tusec << " "
|
---|
1330 | << setw(3) << isock << " "
|
---|
1331 | << setw(2) << isock/7 << " "
|
---|
1332 | << runno << " "
|
---|
1333 | << event << " "
|
---|
1334 | << ftmevt << " "
|
---|
1335 | << ibyte << endl;
|
---|
1336 | }
|
---|
1337 |
|
---|
1338 | int eventCheck(PEVNT_HEADER *fadhd, EVENT *event)
|
---|
1339 | {
|
---|
1340 | /*
|
---|
1341 | fadhd[i] ist ein array mit den 40 fad-headers
|
---|
1342 | (falls ein board nicht gelesen wurde, ist start_package_flag =0 )
|
---|
1343 |
|
---|
1344 | event ist die Struktur, die auch die write routine erhaelt;
|
---|
1345 | darin sind im header die 'soll-werte' fuer z.B. eventID
|
---|
1346 | als auch die ADC-Werte (falls Du die brauchst)
|
---|
1347 |
|
---|
1348 | Wenn die routine einen negativen Wert liefert, wird das event
|
---|
1349 | geloescht (nicht an die write-routine weitergeleitet [mind. im Prinzip]
|
---|
1350 | */
|
---|
1351 |
|
---|
1352 | /*
|
---|
1353 | const FAD::EventHeader *beg = reinterpret_cast<FAD::EventHeader*>(fadhd);
|
---|
1354 | const FAD::EventHeader *end = reinterpret_cast<FAD::EventHeader*>(fadhd)+41;
|
---|
1355 |
|
---|
1356 | for (const FAD::EventHeader *ptr=beg; ptr!=end; ptr++)
|
---|
1357 | {
|
---|
1358 | // Event incomplete
|
---|
1359 | if (ptr->fStartDelimiter==0)
|
---|
1360 | return -1;
|
---|
1361 |
|
---|
1362 | // Either one of
|
---|
1363 | // * fStatus
|
---|
1364 | // * fRunNumber
|
---|
1365 | // * fEventCounter
|
---|
1366 | // * fAdcClockPhaseShift
|
---|
1367 | // * fTriggerGeneratorPrescaler
|
---|
1368 | // * fDac
|
---|
1369 | // inconsistent
|
---|
1370 | if (*ptr != *beg)
|
---|
1371 | return -1;
|
---|
1372 |
|
---|
1373 | if (ptr->fTriggerType != beg->fTriggerType)
|
---|
1374 | return -1;
|
---|
1375 | if (ptr->fTriggerId != beg->fTriggerId)
|
---|
1376 | return -1;
|
---|
1377 | if (ptr->fVersion != beg->fVersion)
|
---|
1378 | return -1;
|
---|
1379 | }*/
|
---|
1380 |
|
---|
1381 | // check REFCLK_frequency
|
---|
1382 | // check consistency with command configuration
|
---|
1383 | // how to log errors?
|
---|
1384 | // need gotNewRun/closedRun to know it is finished
|
---|
1385 |
|
---|
1386 | static Time oldt(boost::date_time::neg_infin);
|
---|
1387 | Time newt;
|
---|
1388 |
|
---|
1389 | if (newt<oldt+boost::posix_time::seconds(1))
|
---|
1390 | return 0;
|
---|
1391 |
|
---|
1392 | oldt = newt;
|
---|
1393 |
|
---|
1394 | const size_t sz = sizeof(EVENT)+event->Roi*2*1440;
|
---|
1395 |
|
---|
1396 | fDimEventData.setData(event, sz);
|
---|
1397 | fDimEventData.updateService();
|
---|
1398 |
|
---|
1399 | return 0;
|
---|
1400 | }
|
---|
1401 |
|
---|
1402 | bool IsRunStarted() const
|
---|
1403 | {
|
---|
1404 | return fExpectedRuns.find(fRunNumber-1)==fExpectedRuns.end();
|
---|
1405 | }
|
---|
1406 |
|
---|
1407 | void gotNewRun(int runnr, PEVNT_HEADER *headers)
|
---|
1408 | {
|
---|
1409 | // This function is called even when writing is switched off
|
---|
1410 | const map<uint32_t,RunDescription>::iterator it = fExpectedRuns.find(runnr);
|
---|
1411 | if (it==fExpectedRuns.end())
|
---|
1412 | {
|
---|
1413 | ostringstream str;
|
---|
1414 | str << "gotNewRun - Run " << runnr << " wasn't expected." << endl;
|
---|
1415 | return;
|
---|
1416 | }
|
---|
1417 |
|
---|
1418 | CloseRunFile(runnr, time(NULL)+it->second.maxtime, it->second.maxevt);
|
---|
1419 | // return: 0=close scheduled / >0 already closed / <0 does not exist
|
---|
1420 |
|
---|
1421 | fExpectedRuns.erase(it);
|
---|
1422 | }
|
---|
1423 |
|
---|
1424 | map<boost::thread::id, string> fLastMessage;
|
---|
1425 |
|
---|
1426 | void factOut(int severity, int err, const char *message)
|
---|
1427 | {
|
---|
1428 | if (!fDebugLog && severity==99)
|
---|
1429 | return;
|
---|
1430 |
|
---|
1431 | ostringstream str;
|
---|
1432 | //str << boost::this_thread::get_id() << " ";
|
---|
1433 | str << "EventBuilder(";
|
---|
1434 | if (err<0)
|
---|
1435 | str << "---";
|
---|
1436 | else
|
---|
1437 | str << err;
|
---|
1438 | str << "): " << message;
|
---|
1439 |
|
---|
1440 | string &old = fLastMessage[boost::this_thread::get_id()];
|
---|
1441 |
|
---|
1442 | if (str.str()==old)
|
---|
1443 | return;
|
---|
1444 | old = str.str();
|
---|
1445 |
|
---|
1446 | fMsg.Update(str, severity);
|
---|
1447 | }
|
---|
1448 | /*
|
---|
1449 | void factStat(int64_t *stat, int len)
|
---|
1450 | {
|
---|
1451 | if (len!=7)
|
---|
1452 | {
|
---|
1453 | fMsg.Warn("factStat received unknown number of values.");
|
---|
1454 | return;
|
---|
1455 | }
|
---|
1456 |
|
---|
1457 | vector<int64_t> data(1, g_maxMem);
|
---|
1458 | data.insert(data.end(), stat, stat+len);
|
---|
1459 |
|
---|
1460 | static vector<int64_t> last(8);
|
---|
1461 | if (data==last)
|
---|
1462 | return;
|
---|
1463 | last = data;
|
---|
1464 |
|
---|
1465 | fDimStatistics.Update(data);
|
---|
1466 |
|
---|
1467 | // len ist die Laenge des arrays.
|
---|
1468 | // array[4] enthaelt wieviele bytes im Buffer aktuell belegt sind; daran
|
---|
1469 | // kannst Du pruefen, ob die 100MB voll sind ....
|
---|
1470 |
|
---|
1471 | ostringstream str;
|
---|
1472 | str
|
---|
1473 | << "Wait=" << stat[0] << " "
|
---|
1474 | << "Skip=" << stat[1] << " "
|
---|
1475 | << "Del=" << stat[2] << " "
|
---|
1476 | << "Tot=" << stat[3] << " "
|
---|
1477 | << "Mem=" << stat[4] << "/" << g_maxMem << " "
|
---|
1478 | << "Read=" << stat[5] << " "
|
---|
1479 | << "Conn=" << stat[6];
|
---|
1480 |
|
---|
1481 | fMsg.Info(str);
|
---|
1482 | }
|
---|
1483 | */
|
---|
1484 |
|
---|
1485 | void factStat(const EVT_STAT &stat)
|
---|
1486 | {
|
---|
1487 | fDimStatistics2.Update(stat);
|
---|
1488 | /*
|
---|
1489 | //some info about what happened since start of program (or last 'reset')
|
---|
1490 | uint32_t reset ; //#if increased, reset all counters
|
---|
1491 | uint32_t numRead[MAX_SOCK] ; //how often succesfull read from N sockets per loop
|
---|
1492 |
|
---|
1493 | uint64_t gotByte[NBOARDS] ; //#Bytes read per Board
|
---|
1494 | uint32_t gotErr[NBOARDS] ; //#Communication Errors per Board
|
---|
1495 | uint32_t evtGet; //#new Start of Events read
|
---|
1496 | uint32_t evtTot; //#complete Events read
|
---|
1497 | uint32_t evtErr; //#Events with Errors
|
---|
1498 | uint32_t evtSkp; //#Events incomplete (timeout)
|
---|
1499 |
|
---|
1500 | uint32_t procTot; //#Events processed
|
---|
1501 | uint32_t procErr; //#Events showed problem in processing
|
---|
1502 | uint32_t procTrg; //#Events accepted by SW trigger
|
---|
1503 | uint32_t procSkp; //#Events rejected by SW trigger
|
---|
1504 |
|
---|
1505 | uint32_t feedTot; //#Events used for feedBack system
|
---|
1506 | uint32_t feedErr; //#Events rejected by feedBack
|
---|
1507 |
|
---|
1508 | uint32_t wrtTot; //#Events written to disk
|
---|
1509 | uint32_t wrtErr; //#Events with write-error
|
---|
1510 |
|
---|
1511 | uint32_t runOpen; //#Runs opened
|
---|
1512 | uint32_t runClose; //#Runs closed
|
---|
1513 | uint32_t runErr; //#Runs with open/close errors
|
---|
1514 |
|
---|
1515 |
|
---|
1516 | //info about current connection status
|
---|
1517 | uint8_t numConn[NBOARDS] ; //#Sockets succesfully open per board
|
---|
1518 | */
|
---|
1519 | }
|
---|
1520 |
|
---|
1521 | void factStat(const GUI_STAT &stat)
|
---|
1522 | {
|
---|
1523 | fDimStatistics1.Update(stat);
|
---|
1524 | /*
|
---|
1525 | //info about status of the main threads
|
---|
1526 | int32_t readStat ; //read thread
|
---|
1527 | int32_t procStat ; //processing thread(s)
|
---|
1528 | int32_t writStat ; //write thread
|
---|
1529 |
|
---|
1530 | //info about some rates
|
---|
1531 | int32_t deltaT ; //time in milli-seconds for rates
|
---|
1532 | int32_t readEvt ; //#events read
|
---|
1533 | int32_t procEvt ; //#events processed
|
---|
1534 | int32_t writEvt ; //#events written
|
---|
1535 | int32_t skipEvt ; //#events skipped
|
---|
1536 |
|
---|
1537 | //some info about current state of event buffer (snapspot)
|
---|
1538 | int32_t evtBuf; //#Events currently waiting in Buffer
|
---|
1539 | uint64_t totMem; //#Bytes available in Buffer
|
---|
1540 | uint64_t usdMem; //#Bytes currently used
|
---|
1541 | uint64_t maxMem; //max #Bytes used during past Second
|
---|
1542 | */
|
---|
1543 | }
|
---|
1544 |
|
---|
1545 |
|
---|
1546 | array<FAD::EventHeader, 40> fVecHeader;
|
---|
1547 |
|
---|
1548 | template<typename T>
|
---|
1549 | array<T, 42> Compare(const FAD::EventHeader *h, const T *t)
|
---|
1550 | {
|
---|
1551 | const int offset = reinterpret_cast<const char *>(t) - reinterpret_cast<const char *>(h);
|
---|
1552 |
|
---|
1553 | const T *min = NULL;
|
---|
1554 | const T *val = NULL;
|
---|
1555 | const T *max = NULL;
|
---|
1556 |
|
---|
1557 | array<T, 42> vec;
|
---|
1558 |
|
---|
1559 | bool rc = true;
|
---|
1560 | for (int i=0; i<40; i++)
|
---|
1561 | {
|
---|
1562 | const char *base = reinterpret_cast<const char*>(&fVecHeader[i]);
|
---|
1563 | const T *ref = reinterpret_cast<const T*>(base+offset);
|
---|
1564 |
|
---|
1565 | vec[i] = *ref;
|
---|
1566 |
|
---|
1567 | if (gi_NumConnect[i]!=7)
|
---|
1568 | {
|
---|
1569 | vec[i] = 0;
|
---|
1570 | continue;
|
---|
1571 | }
|
---|
1572 |
|
---|
1573 | if (!val)
|
---|
1574 | {
|
---|
1575 | min = ref;
|
---|
1576 | val = ref;
|
---|
1577 | max = ref;
|
---|
1578 | }
|
---|
1579 |
|
---|
1580 | if (*val<*min)
|
---|
1581 | min = val;
|
---|
1582 |
|
---|
1583 | if (*val>*max)
|
---|
1584 | max = val;
|
---|
1585 |
|
---|
1586 | if (*val!=*ref)
|
---|
1587 | rc = false;
|
---|
1588 | }
|
---|
1589 |
|
---|
1590 | vec[40] = val ? *min : 0xffffffff;
|
---|
1591 | vec[41] = val ? *max : 0;
|
---|
1592 |
|
---|
1593 | return vec;
|
---|
1594 | }
|
---|
1595 |
|
---|
1596 | template<typename T>
|
---|
1597 | array<T, 42> CompareBits(const FAD::EventHeader *h, const T *t)
|
---|
1598 | {
|
---|
1599 | const int offset = reinterpret_cast<const char *>(t) - reinterpret_cast<const char *>(h);
|
---|
1600 |
|
---|
1601 | T val = 0;
|
---|
1602 | T rc = 0;
|
---|
1603 |
|
---|
1604 | array<T, 42> vec;
|
---|
1605 |
|
---|
1606 | bool first = true;
|
---|
1607 |
|
---|
1608 | for (int i=0; i<40; i++)
|
---|
1609 | {
|
---|
1610 | const char *base = reinterpret_cast<const char*>(&fVecHeader[i]);
|
---|
1611 | const T *ref = reinterpret_cast<const T*>(base+offset);
|
---|
1612 |
|
---|
1613 | vec[i+2] = *ref;
|
---|
1614 |
|
---|
1615 | if (gi_NumConnect[i]!=7)
|
---|
1616 | {
|
---|
1617 | vec[i+2] = 0;
|
---|
1618 | continue;
|
---|
1619 | }
|
---|
1620 |
|
---|
1621 | if (first)
|
---|
1622 | {
|
---|
1623 | first = false;
|
---|
1624 | val = *ref;
|
---|
1625 | rc = 0;
|
---|
1626 | }
|
---|
1627 |
|
---|
1628 | rc |= val^*ref;
|
---|
1629 | }
|
---|
1630 |
|
---|
1631 | vec[0] = rc;
|
---|
1632 | vec[1] = val;
|
---|
1633 |
|
---|
1634 | return vec;
|
---|
1635 | }
|
---|
1636 |
|
---|
1637 | template<typename T, size_t N>
|
---|
1638 | void Update(DimDescribedService &svc, const array<T, N> &data, int n=N)
|
---|
1639 | {
|
---|
1640 | // svc.setQuality(vec[40]<=vec[41]);
|
---|
1641 | svc.setData(const_cast<T*>(data.data()), sizeof(T)*n);
|
---|
1642 | svc.updateService();
|
---|
1643 | }
|
---|
1644 |
|
---|
1645 | template<typename T>
|
---|
1646 | void Print(const char *name, const pair<bool,array<T, 43>> &data)
|
---|
1647 | {
|
---|
1648 | cout << name << "|" << data.first << "|" << data.second[1] << "|" << data.second[0] << "<x<" << data.second[1] << ":";
|
---|
1649 | for (int i=0; i<40;i++)
|
---|
1650 | cout << " " << data.second[i+3];
|
---|
1651 | cout << endl;
|
---|
1652 | }
|
---|
1653 |
|
---|
1654 | vector<uint> fNumConnected;
|
---|
1655 |
|
---|
1656 | void debugHead(int socket, const FAD::EventHeader &h)
|
---|
1657 | {
|
---|
1658 | const uint16_t id = h.Id();
|
---|
1659 | if (id>39)
|
---|
1660 | return;
|
---|
1661 |
|
---|
1662 | if (fNumConnected.size()!=40)
|
---|
1663 | fNumConnected.resize(40);
|
---|
1664 |
|
---|
1665 | const vector<uint> con(gi_NumConnect, gi_NumConnect+40);
|
---|
1666 |
|
---|
1667 | const bool changed = con!=fNumConnected || !IsThreadRunning();
|
---|
1668 |
|
---|
1669 | fNumConnected = con;
|
---|
1670 |
|
---|
1671 | const FAD::EventHeader old = fVecHeader[id];
|
---|
1672 | fVecHeader[id] = h;
|
---|
1673 |
|
---|
1674 | if (old.fVersion != h.fVersion || changed)
|
---|
1675 | {
|
---|
1676 | const array<uint16_t,42> ver = Compare(&h, &h.fVersion);
|
---|
1677 |
|
---|
1678 | array<float,42> data;
|
---|
1679 | for (int i=0; i<42; i++)
|
---|
1680 | {
|
---|
1681 | ostringstream str;
|
---|
1682 | str << (ver[i]>>8) << '.' << (ver[i]&0xff);
|
---|
1683 | data[i] = stof(str.str());
|
---|
1684 | }
|
---|
1685 | Update(fDimFwVersion, data);
|
---|
1686 | }
|
---|
1687 |
|
---|
1688 | if (old.fRunNumber != h.fRunNumber || changed)
|
---|
1689 | {
|
---|
1690 | const array<uint32_t,42> run = Compare(&h, &h.fRunNumber);
|
---|
1691 | fDimRunNumber.Update(run);
|
---|
1692 | }
|
---|
1693 |
|
---|
1694 | if (old.fTriggerGeneratorPrescaler != h.fTriggerGeneratorPrescaler || changed)
|
---|
1695 | {
|
---|
1696 | const array<uint16_t,42> pre = Compare(&h, &h.fTriggerGeneratorPrescaler);
|
---|
1697 | fDimPrescaler.Update(pre);
|
---|
1698 | }
|
---|
1699 |
|
---|
1700 | if (old.fDNA != h.fDNA || changed)
|
---|
1701 | {
|
---|
1702 | const array<uint64_t,42> dna = Compare(&h, &h.fDNA);
|
---|
1703 | Update(fDimDNA, dna, 40);
|
---|
1704 | }
|
---|
1705 |
|
---|
1706 | if (old.fStatus != h.fStatus || changed)
|
---|
1707 | {
|
---|
1708 | const array<uint16_t,42> sts = CompareBits(&h, &h.fStatus);
|
---|
1709 | Update(fDimStatus, sts);
|
---|
1710 | }
|
---|
1711 |
|
---|
1712 | // -----------
|
---|
1713 |
|
---|
1714 | static Time oldt(boost::date_time::neg_infin);
|
---|
1715 | Time newt;
|
---|
1716 |
|
---|
1717 | if (newt>oldt+boost::posix_time::seconds(1))
|
---|
1718 | {
|
---|
1719 | oldt = newt;
|
---|
1720 |
|
---|
1721 | // --- RefClock
|
---|
1722 |
|
---|
1723 | const array<uint32_t,42> clk = Compare(&h, &h.fFreqRefClock);
|
---|
1724 | Update(fDimRefClock, clk);
|
---|
1725 |
|
---|
1726 | // --- Temperatures
|
---|
1727 |
|
---|
1728 | const array<int16_t,42> tmp[4] =
|
---|
1729 | {
|
---|
1730 | Compare(&h, &h.fTempDrs[0]), // 0-39:val, 40:min, 41:max
|
---|
1731 | Compare(&h, &h.fTempDrs[1]), // 0-39:val, 40:min, 41:max
|
---|
1732 | Compare(&h, &h.fTempDrs[2]), // 0-39:val, 40:min, 41:max
|
---|
1733 | Compare(&h, &h.fTempDrs[3]) // 0-39:val, 40:min, 41:max
|
---|
1734 | };
|
---|
1735 |
|
---|
1736 | vector<int16_t> data;
|
---|
1737 | data.reserve(82);
|
---|
1738 | data.push_back(tmp[0][40]); // min: 0
|
---|
1739 | data.insert(data.end(), tmp[0].data(), tmp[0].data()+40); // val: 1-40
|
---|
1740 | data.push_back(tmp[0][41]); // max: 41
|
---|
1741 | data.insert(data.end(), tmp[0].data(), tmp[0].data()+40); // val: 42-81
|
---|
1742 |
|
---|
1743 | for (int j=0; j<=3; j++)
|
---|
1744 | {
|
---|
1745 | const array<int16_t,42> &ref = tmp[j];
|
---|
1746 |
|
---|
1747 | // Gloabl min
|
---|
1748 | if (ref[40]<data[0]) // 40=min
|
---|
1749 | data[0] = ref[40];
|
---|
1750 |
|
---|
1751 | // Global max
|
---|
1752 | if (ref[41]>data[40]) // 41=max
|
---|
1753 | data[40] = ref[41];
|
---|
1754 |
|
---|
1755 | for (int i=0; i<40; i++)
|
---|
1756 | {
|
---|
1757 | // min per board
|
---|
1758 | if (ref[i]<data[i+1]) // data: 1-40
|
---|
1759 | data[i+1] = ref[i]; // ref: 0-39
|
---|
1760 |
|
---|
1761 | // max per board
|
---|
1762 | if (ref[i]>data[i+42]) // data: 42-81
|
---|
1763 | data[i+42] = ref[i]; // ref: 0-39
|
---|
1764 | }
|
---|
1765 | }
|
---|
1766 |
|
---|
1767 | vector<float> deg(82); // 0: global min, 1-40: min
|
---|
1768 | for (int i=0; i<82; i++) // 41: global max, 42-81: max
|
---|
1769 | deg[i] = data[i]/16.;
|
---|
1770 | fDimTemperature.Update(deg);
|
---|
1771 | }
|
---|
1772 |
|
---|
1773 | /*
|
---|
1774 | uint16_t fTriggerType;
|
---|
1775 | uint32_t fTriggerId;
|
---|
1776 | uint32_t fEventCounter;
|
---|
1777 | uint16_t fAdcClockPhaseShift;
|
---|
1778 | uint16_t fNumTriggersToGenerate;
|
---|
1779 | uint16_t fTriggerGeneratorPrescaler;
|
---|
1780 | uint32_t fTimeStamp;
|
---|
1781 | int16_t fTempDrs[kNumTemp]; // In units of 1/16 deg(?)
|
---|
1782 | uint16_t fDac[kNumDac];
|
---|
1783 | */
|
---|
1784 | }
|
---|
1785 | };
|
---|
1786 |
|
---|
1787 | EventBuilderWrapper *EventBuilderWrapper::This = 0;
|
---|
1788 |
|
---|
1789 | // ----------- Event builder callbacks implementation ---------------
|
---|
1790 | extern "C"
|
---|
1791 | {
|
---|
1792 | FileHandle_t runOpen(uint32_t irun, RUN_HEAD *runhd, size_t len)
|
---|
1793 | {
|
---|
1794 | return EventBuilderWrapper::This->runOpen(irun, runhd, len);
|
---|
1795 | }
|
---|
1796 |
|
---|
1797 | int runWrite(FileHandle_t fileId, EVENT *event, size_t len)
|
---|
1798 | {
|
---|
1799 | return EventBuilderWrapper::This->runWrite(fileId, event, len);
|
---|
1800 | }
|
---|
1801 |
|
---|
1802 | int runClose(FileHandle_t fileId, RUN_TAIL *runth, size_t len)
|
---|
1803 | {
|
---|
1804 | return EventBuilderWrapper::This->runClose(fileId, runth, len);
|
---|
1805 | }
|
---|
1806 |
|
---|
1807 | void factOut(int severity, int err, const char *message)
|
---|
1808 | {
|
---|
1809 | EventBuilderWrapper::This->factOut(severity, err, message);
|
---|
1810 | }
|
---|
1811 |
|
---|
1812 | void factStat(GUI_STAT stat)
|
---|
1813 | {
|
---|
1814 | EventBuilderWrapper::This->factStat(stat);
|
---|
1815 | }
|
---|
1816 |
|
---|
1817 | void factStatNew(EVT_STAT stat)
|
---|
1818 | {
|
---|
1819 | EventBuilderWrapper::This->factStat(stat);
|
---|
1820 | }
|
---|
1821 |
|
---|
1822 | void debugHead(int socket, int/*board*/, void *buf)
|
---|
1823 | {
|
---|
1824 | const uint16_t *ptr = reinterpret_cast<uint16_t*>(buf);
|
---|
1825 |
|
---|
1826 | EventBuilderWrapper::This->debugHead(socket, FAD::EventHeader(ptr));
|
---|
1827 | }
|
---|
1828 |
|
---|
1829 | void debugStream(int isock, void *buf, int len)
|
---|
1830 | {
|
---|
1831 | return EventBuilderWrapper::This->debugStream(isock, buf, len);
|
---|
1832 | }
|
---|
1833 |
|
---|
1834 | void debugRead(int isock, int ibyte, int32_t event, int32_t ftmevt, int32_t runno, int state, uint32_t tsec, uint32_t tusec)
|
---|
1835 | {
|
---|
1836 | EventBuilderWrapper::This->debugRead(isock, ibyte, event, ftmevt, runno, state, tsec, tusec);
|
---|
1837 | }
|
---|
1838 |
|
---|
1839 | int eventCheck(PEVNT_HEADER *fadhd, EVENT *event)
|
---|
1840 | {
|
---|
1841 | return EventBuilderWrapper::This->eventCheck(fadhd, event);
|
---|
1842 | }
|
---|
1843 |
|
---|
1844 | void gotNewRun( int runnr, PEVNT_HEADER *headers )
|
---|
1845 | {
|
---|
1846 | return EventBuilderWrapper::This->gotNewRun(runnr, headers);
|
---|
1847 | }
|
---|
1848 | }
|
---|
1849 |
|
---|
1850 | #endif
|
---|