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

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