source: trunk/FACT++/src/EventBuilderWrapper.h@ 11711

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