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

Last change on this file since 16694 was 16694, checked in by tbretz, 11 years ago
Removed the min/max of temperature and refclock and replaced it by 5s wise averages.
File size: 44.3 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/filesystem.hpp>
13#include <boost/date_time/posix_time/posix_time_types.hpp>
14
15#include "DimWriteStatistics.h"
16
17#include "DataCalib.h"
18#include "DataWriteRaw.h"
19
20#ifdef HAVE_FITS
21#include "DataWriteFits.h"
22#else
23#define DataWriteFits DataWriteFits2
24#endif
25
26#include "DataWriteFits2.h"
27
28#include "queue.h"
29
30namespace ba = boost::asio;
31namespace bs = boost::system;
32namespace fs = boost::filesystem;
33
34using ba::ip::tcp;
35
36using namespace std;
37
38// ========================================================================
39
40#include "EventBuilder.h"
41
42void StartEvtBuild();
43void CloseRunFile();
44
45// ========================================================================
46
47class EventBuilderWrapper
48{
49public:
50 // FIXME
51 static EventBuilderWrapper *This;
52
53 MessageImp &fMsg;
54
55private:
56 boost::thread fThreadMain;
57
58 enum
59 {
60 kCurrent = 0,
61 kTotal = 1,
62 kEventId = 2,
63 kTriggerId = 3,
64 };
65
66 FAD::FileFormat_t fFileFormat;
67
68 //uint32_t fMaxRun;
69 uint32_t fLastOpened;
70 uint32_t fLastClosed;
71 array<uint32_t,4> fNumEvts;
72
73 DimWriteStatistics fDimWriteStats;
74 DimDescribedService fDimRuns;
75 DimDescribedService fDimEvents;
76 DimDescribedService fDimRawData;
77 DimDescribedService fDimEventData;
78 DimDescribedService fDimFeedbackData;
79 DimDescribedService fDimFwVersion;
80 DimDescribedService fDimRunNumber;
81 DimDescribedService fDimStatus;
82 DimDescribedService fDimDNA;
83 DimDescribedService fDimTemperature;
84 DimDescribedService fDimPrescaler;
85 DimDescribedService fDimRefClock;
86 DimDescribedService fDimRoi;
87 DimDescribedService fDimDac;
88 DimDescribedService fDimDrsRuns;
89 DimDescribedService fDimDrsCalibration;
90 DimDescribedService fDimStatistics1;
91 //DimDescribedService fDimStatistics2;
92 DimDescribedService fDimFileFormat;
93 DimDescribedService fDimIncomplete;
94
95 Queue<pair<Time,GUI_STAT>> fQueueStatistics1;
96 Queue<tuple<Time,bool,FAD::EventHeader>> fQueueProcHeader;
97 Queue<pair<Time,array<uint32_t,4>>> fQueueEvents;
98 Queue<pair<Time,array<uint16_t,2>>> fQueueRoi;
99 Queue<vector<char>> fQueueRawData;
100 Queue<tuple<Time,uint32_t,array<float,1440*4>>> fQueueEventData;
101 Queue<tuple<Time, array<uint32_t,40>, array<int16_t,160>>> fQueueTempRefClk;
102
103 string fPath;
104 uint32_t fNightAsInt;
105 uint32_t fRunNumber;
106 int64_t fRunInProgress;
107
108 array<uint16_t,2> fVecRoi;
109 pair<float,array<float, 1440*4>> fMaxEvent; // Maximum event from applyCalib
110
111protected:
112 bool InitRunNumber(const string &path="")
113 {
114 if (!path.empty())
115 {
116 if (!DimWriteStatistics::DoesPathExist(path, fMsg))
117 {
118 fMsg.Error("Data path "+path+" does not exist!");
119 return false;
120 }
121
122 fPath = path;
123 fDimWriteStats.SetCurrentFolder(fPath);
124
125 fMsg.Info("Data path set to "+path+".");
126 }
127
128 // Get current night
129 const uint32_t night = Time().NightAsInt();
130 if (night==fNightAsInt)
131 return true;
132
133 // Check for run numbers
134 fRunNumber = 1000;
135
136 while (--fRunNumber>0)
137 {
138 const string name = DataProcessorImp::FormFileName(fPath, night, fRunNumber, "");
139
140 if (access((name+"bin").c_str(), F_OK) == 0)
141 break;
142 if (access((name+"fits").c_str(), F_OK) == 0)
143 break;
144 if (access((name+"drs.fits").c_str(), F_OK) == 0)
145 break;
146 }
147
148 // This is now the first file which does not exist
149 fRunNumber++;
150 fLastOpened = 0;
151
152 // Check if we have exceeded the maximum
153 if (fRunNumber==1000)
154 {
155 fMsg.Error("You have a file with run-number 1000 in "+fPath+" ["+to_string(night)+"]");
156 return false;
157 }
158
159 ostringstream str;
160 if (fNightAsInt==0)
161 str << "First night...";
162 else
163 str << "Night has changd from " << fNightAsInt << "... new";
164 str << " run-number is " << night << "-" << setfill('0') << setw(3) << fRunNumber << " [" << (fPath.empty()?".":fPath) << "]";
165 fMsg.Message(str);
166
167 fNightAsInt = night;
168
169 return true;
170 }
171
172public:
173 EventBuilderWrapper(MessageImp &imp) : fMsg(imp),
174 fFileFormat(FAD::kNone), /*fMaxRun(0),*/ fLastOpened(0), fLastClosed(0),
175 fDimWriteStats ("FAD_CONTROL", imp),
176 fDimRuns ("FAD_CONTROL/RUNS", "I:2;C",
177 "Run files statistics"
178 "|stats[int]:last opened or closed run"
179 "|file[string]:filename of last opened file"),
180 fDimEvents ("FAD_CONTROL/EVENTS", "I:4",
181 "Event counts"
182 "|evtsCount[int]:Num evts cur. run, total (all run), evt ID, trig. Num"),
183 fDimRawData ("FAD_CONTROL/RAW_DATA", "S:1;S:1;I:1;I:1;S:1;I:1;I:2;I:40;S:1440;S:160;F",
184 "|roi[uint16]:number of samples per pixel"
185 "|roi_tm[uint16]:number of samples per time-marker channel"
186 "|num_fad[uint32]:event number from FADs"
187 "|num_ftm[uint32]:trigger number from FTM"
188 "|type[uint16]:trigger type from FTM"
189 "|num_boards[uint32]:number of active boards"
190 "|time[uint32]:PC time as unix time stamp"
191 "|time_board[uint32]:Time stamp of FAD boards"
192 "|start_pix[int16]:start sample of pixels"
193 "|start_tm[int16]:start sample of time marker channels"
194 "|adc[int16]:adc data"),
195 fDimEventData ("FAD_CONTROL/EVENT_DATA", "F:1440;F:1440;F:1440;F:1440", "|avg:|rms:|max:|pos"),
196 fDimFeedbackData("FAD_CONTROL/FEEDBACK_DATA", "F:1440", ""),
197 fDimFwVersion ("FAD_CONTROL/FIRMWARE_VERSION", "F:42",
198 "Firmware version number of fad boards"
199 "|firmware[float]:Version number of firmware, for each board. 40=min, 41=max"),
200 fDimRunNumber ("FAD_CONTROL/RUN_NUMBER", "I:42",
201 "Run numbers coming from FAD boards"
202 "|runNumbers[int]:current run number of each FAD board. 40=min, 41=max"),
203 fDimStatus ("FAD_CONTROL/STATUS", "S:42",
204 "Status of FAD boards"
205 "|status[bitpattern]:Status of each FAD board. Maybe buggy"),
206 fDimDNA ("FAD_CONTROL/DNA", "X:40",
207 "DNA of FAD boards"
208 "|DNA[hex]:Hex identifier of each FAD board"),
209 fDimTemperature ("FAD_CONTROL/TEMPERATURE", "S:1;F:160",
210 "FADs temperatures"
211 "|cnt[uint16]:Counter of averaged values",
212 "|temp[deg C]:average temp of all DRS chips"),
213 fDimPrescaler ("FAD_CONTROL/PRESCALER", "S:42",
214 "Trigger generator prescaler of fad boards"
215 "|prescaler[int]:Trigger generator prescaler value, for each board"),
216 fDimRefClock ("FAD_CONTROL/REFERENCE_CLOCK", "S:1;F:40",
217 "Reference clock of FAD boards"
218 "|cnt[uint16]:Counter of averaged values"
219 "|clk[Hz]:Averaged clock of ref clocks of FAD boards"),
220 fDimRoi ("FAD_CONTROL/REGION_OF_INTEREST", "S:2", "roi:|roi_rm:"),
221 fDimDac ("FAD_CONTROL/DAC", "S:336",
222 "DAC settings of each FAD board"
223 "|DAC[int]:DAC counts, sequentially DAC 0 board 0, 0/1, 0/2... (plus min max)"),
224 fDimDrsRuns ("FAD_CONTROL/DRS_RUNS", "I:1;I:3",
225 "|roi:Region of interest of secondary baseline"
226 "|run:Run numbers of DRS runs (0=none)"),
227 fDimDrsCalibration("FAD_CONTROL/DRS_CALIBRATION", "I:1;I:3;F:1474560;F:1474560;F:1474560;F:1474560;F:1474560;F:1474560;F:163840;F:163840",
228 "|roi:Region of interest of secondary baseline"
229 "|run:Run numbers of DRS runs (0=none)"),
230 fDimStatistics1 ("FAD_CONTROL/STATISTICS1", "I:5;X:3;I:1;I:2;C:40;I:40;I:40",
231 "Event Builder status for GUI display"
232 "|bufferInfo[int]:Events in buffer, incomp., comp., write, proc., tot."
233 "|memInfo[int]:total mem allocated, used mem, max memory"
234 "|deltaT[ms]:Time in ms for rates"
235 "|rateNew[int]:Number of new start events received"
236 "|numConn[int]:Number of connections per board"
237 "|rateBytes[int]:Bytes read during last cylce"
238 "|relBytes[int]:Relative number of total bytes received (received - released)"),
239 fDimFileFormat("FAD_CONTROL/FILE_FORMAT", "S:1", "|format[int]:Current file format"),
240 fDimIncomplete("FAD_CONTROL/INCOMPLETE", "X:1", "|incomplete[bits]:One bit per board"),
241 // It is important to instantiate them after the DimServices
242 fQueueStatistics1(std::bind(&EventBuilderWrapper::UpdateDimStatistics1, this, placeholders::_1)),
243 fQueueProcHeader( std::bind(&EventBuilderWrapper::procHeader, this, placeholders::_1)),
244 fQueueEvents( std::bind(&EventBuilderWrapper::UpdateDimEvents, this, placeholders::_1)),
245 fQueueRoi( std::bind(&EventBuilderWrapper::UpdateDimRoi, this, placeholders::_1)),
246 fQueueRawData( std::bind(&EventBuilderWrapper::UpdateDimRawData, this, placeholders::_1)),
247 fQueueEventData( std::bind(&EventBuilderWrapper::UpdateDimEventData, this, placeholders::_1)),
248 fQueueTempRefClk( std::bind(&EventBuilderWrapper::UpdateDimTempRefClk, this, placeholders::_1)),
249 fNightAsInt(0), fRunInProgress(-1),
250 fMaxEvent(make_pair(-FLT_MAX, array<float,1440*4>()))
251 {
252 if (This)
253 throw logic_error("EventBuilderWrapper cannot be instantiated twice.");
254
255 This = this;
256
257 fVecRoi.fill(0);
258
259 memset(fNumEvts.data(), 0, sizeof(fNumEvts));
260 fDimEvents.Update(fNumEvts);
261
262 for (size_t i=0; i<40; i++)
263 ConnectSlot(i, tcp::endpoint());
264 }
265
266 virtual ~EventBuilderWrapper()
267 {
268 Abort();
269
270 // FIXME: Used timed_join and abort afterwards
271 // What's the maximum time the eb need to abort?
272 fThreadMain.join();
273 }
274
275 map<uint32_t, FAD::RunDescription> fExpectedRuns;
276
277 mutex mtx_newrun;
278
279 uint32_t StartNewRun(int64_t maxtime, int64_t maxevt, const pair<string, FAD::Configuration> &ref)
280 {
281 if (maxtime<=0 || maxtime>24*60*60)
282 maxtime = 24*60*60;
283 if (maxevt<=0 || maxevt>INT32_MAX)
284 maxevt = INT32_MAX;
285
286 if (!InitRunNumber())
287 return 0;
288
289 const FAD::RunDescription descr =
290 {
291 uint32_t(maxtime),
292 uint32_t(maxevt),
293 fNightAsInt,
294 ref.first,
295 ref.second,
296 };
297
298 const lock_guard<mutex> lock(mtx_newrun);
299 fExpectedRuns[fRunNumber] = descr;
300 return fRunNumber++;
301 }
302
303 bool IsThreadRunning()
304 {
305 if (fThreadMain.get_id()==boost::this_thread::get_id())
306 return true;
307 return !fThreadMain.timed_join(boost::posix_time::microseconds(0));
308 }
309
310 void SetMaxMemory(unsigned int mb) const
311 {
312 g_maxMem = size_t(mb)*1000000;
313 }
314 void SetEventTimeout(uint16_t to) const
315 {
316 g_evtTimeout = to;
317 }
318
319 void StartThread(const vector<tcp::endpoint> &addr)
320 {
321 if (IsThreadRunning())
322 {
323 fMsg.Warn("Start - EventBuilder still running");
324 return;
325 }
326
327 //fLastMessage.clear();
328
329 for (size_t i=0; i<40; i++)
330 ConnectSlot(i, addr[i]);
331
332 fMsg.Message("Starting EventBuilder thread");
333
334 fThreadMain = boost::thread(StartEvtBuild);
335 }
336
337 void ConnectSlot(unsigned int i, const tcp::endpoint &addr)
338 {
339 if (i>39)
340 return;
341
342 fRunInProgress = -1;
343
344 if (addr==tcp::endpoint())
345 {
346 // In this order
347 g_port[i].sockDef = 0;
348
349 fDimIncomplete.setQuality(0);
350 fDimIncomplete.Update(uint64_t(0));
351 return;
352 }
353
354 struct sockaddr_in sockaddr; //IP for each socket
355 sockaddr.sin_family = AF_INET;
356 sockaddr.sin_addr.s_addr = htonl(addr.address().to_v4().to_ulong());
357 sockaddr.sin_port = htons(addr.port());
358 memcpy(&g_port[i].sockAddr, &sockaddr, sizeof(struct sockaddr_in));
359
360 // In this order
361 g_port[i].sockDef = 1;
362
363 fDimIncomplete.setQuality(0);
364 fDimIncomplete.Update(uint64_t(0));
365 }
366
367 void IgnoreSlot(unsigned int i)
368 {
369 if (i>39)
370 return;
371
372 if (g_port[i].sockAddr.sin_port==0)
373 return;
374
375 g_port[i].sockDef = -1;
376 }
377
378
379 void Abort()
380 {
381 fMsg.Message("Signal abort to EventBuilder thread...");
382 g_reset = 2;
383 }
384
385 void ResetThread(bool soft)
386 {
387 fMsg.Message("Signal reset to EventBuilder thread...");
388 g_reset = soft ? 101 : 102;
389 }
390
391 void Exit()
392 {
393 fMsg.Message("Signal exit to EventBuilder thread...");
394 g_reset = 1;
395 }
396
397 bool IsConnected(int i) const { return gi_NumConnect[i]==1; }
398 bool IsConnecting(int i) const { return gi_NumConnect[i]==0 && g_port[i].sockDef!=0; }
399 bool IsDisconnected(int i) const { return gi_NumConnect[i]==0 && g_port[i].sockDef==0; }
400 bool IsRunInProgress() const { return fRunInProgress>=0; }
401
402 void SetIgnore(int i, bool b) const { if (g_port[i].sockDef!=0) g_port[i].sockDef=b?-1:1; }
403 bool IsIgnored(int i) const { return g_port[i].sockDef==-1; }
404
405 void SetOutputFormat(FAD::FileFormat_t f)
406 {
407 const bool changed = f!=fFileFormat;
408
409 fFileFormat = f;
410 fDimFileFormat.Update(uint16_t(f));
411
412 string msg = "File format set to: ";
413 switch (f)
414 {
415 case FAD::kNone: msg += "kNone."; break;
416 case FAD::kDebug: msg += "kDebug."; break;
417 case FAD::kFits: msg += "kFits."; break;
418 case FAD::kCfitsio: msg += "kCfitsio"; break;
419 case FAD::kRaw: msg += "kRaw"; break;
420 case FAD::kCalib:
421 DataCalib::Restart();
422 DataCalib::Update(fDimDrsCalibration, fDimDrsRuns);
423 fMsg.Message("Resetted DRS calibration.");
424 return;
425 }
426
427 if (changed)
428 fMsg.Message(msg);
429 }
430
431 virtual int ResetSecondaryDrsBaseline()
432 {
433 if (DataCalib::ResetTrgOff(fDimDrsCalibration, fDimDrsRuns))
434 {
435 fFileFormat = FAD::kCalib;
436 fDimFileFormat.Update(uint16_t(fFileFormat));
437 fMsg.Message("Resetted DRS calibration for secondary baseline.");
438 }
439 else
440 fMsg.Warn("Could not reset DRS calibration of secondary baseline.");
441
442 return 0;
443 }
444
445 void LoadDrsCalibration(const char *fname)
446 {
447 if (!DataCalib::ReadFits(fname, fMsg))
448 return;
449 fMsg.Info("Successfully loaded DRS calibration from "+string(fname));
450 DataCalib::Update(fDimDrsCalibration, fDimDrsRuns);
451 }
452
453 virtual int CloseOpenFiles() { CloseRunFile(); fRunInProgress = -1; return 0; }
454
455
456 // -------------- Mapped event builder callbacks ------------------
457
458 void UpdateRuns(const string &fname="")
459 {
460 uint32_t values[2] =
461 {
462 fLastOpened,
463 fLastClosed
464 };
465
466 vector<char> data(sizeof(values)+fname.size()+1);
467 memcpy(data.data(), values, sizeof(values));
468 strcpy(data.data()+sizeof(values), fname.c_str());
469 fDimRuns.setQuality((bool)fFile);
470 fDimRuns.Update(data);
471
472 if (!fname.empty())
473 fDimWriteStats.FileOpened(fname);
474 }
475
476 shared_ptr<DataProcessorImp> fFile;
477
478 void UpdateDimEvents(const pair<Time,array<uint32_t,4>> &stat)
479 {
480 fDimEvents.setData(stat.second.data(), sizeof(uint32_t)*4);
481 fDimEvents.Update(stat.first);
482 }
483
484 bool runOpen(const EVT_CTRL2 &evt)
485 {
486 const uint32_t night = evt.runCtrl->night;
487 const uint32_t runid = evt.runNum>0 ? evt.runNum : time(NULL);
488
489 // If there is still an open file: close it
490 if (fFile)
491 runClose(*evt.runCtrl);
492
493 // Keep a copy of the currently valid drs calibration
494 // and associate it to the run control structure
495 evt.runCtrl->calib = shared_ptr<DrsCalibration>(new DrsCalibration(DataCalib::GetCalibration()));
496
497 /*
498 evt.runCtrl->calibInt.resize(1024*1440);
499
500 const int16_t *off = evt.runCtrl->zcalib.data();
501 int32_t *ptr = evt.runCtrl->calib.data();
502
503 const uint64_t num = evt.runCtrl->calib.fNumOffset;
504 for (int i=0; i<1024*1440)
505 ptr[i] = off[i]/num;
506 */
507
508 // FIMXE: Check if file already exists...
509
510 // Crate the file
511 DataProcessorImp *file = 0;
512 switch (fFileFormat)
513 {
514 case FAD::kNone: file = new DataDump(fPath, night, runid, fMsg); break;
515 case FAD::kDebug: file = new DataDebug(fPath, night, runid, fMsg); break;
516 case FAD::kCfitsio: file = new DataWriteFits(fPath, night, runid, fMsg); break;
517 case FAD::kFits: file = new DataWriteFits2(fPath, night, runid, fMsg); break;
518 case FAD::kRaw: file = new DataWriteRaw(fPath, night, runid, fMsg); break;
519 case FAD::kCalib: file = new DataCalib(fPath, night, runid, fDimDrsCalibration, fDimDrsRuns, fMsg); break;
520 }
521
522 try
523 {
524 // Try to open the file
525 FAD::RunDescription desc;
526 desc.name = evt.runCtrl->runType;
527
528 if (!file->Open(evt, desc))
529 return false;
530 }
531 catch (const exception &e)
532 {
533 fMsg.Error("Exception trying to open file: "+string(e.what()));
534 return false;
535 }
536
537 fLastOpened = runid;
538
539 // Signal that a file is open
540 fFile = shared_ptr<DataProcessorImp>(file);
541
542 // Now do all the calls which potentially block (dim)
543
544 // Time for update runs before time for update events
545 UpdateRuns(file->GetFileName());
546 fNumEvts[kEventId] = 0;
547 fNumEvts[kTriggerId] = 0;
548 fNumEvts[kCurrent] = 0;
549 fQueueEvents.emplace(Time(), fNumEvts);
550
551 ostringstream str;
552 str << "Opened: " << file->GetFileName() << " (" << file->GetRunId() << ")";
553 fMsg.Info(str);
554
555 return true;
556 }
557
558 bool runWrite(const EVT_CTRL2 &e)
559 {
560 /*
561 const size_t size = sizeof(EVENT)+1440*(evt.Roi+evt.RoiTM)*2;
562 vector evt(e.fEvent, e.fEvent+size);
563
564 const EVENT &evt = *reinterpret_cast<EVENT*>(evt.data());
565
566 int16_t *val = evt.Adc_Data;
567 const int16_t *off = e.runCtrl->zcalib.data();
568 for (const int16_t *start=evt.StartPix; start<evt.StartPix+1440; val+=1024, off+=1024, start++)
569 {
570 if (*start<0)
571 continue;
572
573 for (size_t i=0; i<roi; i++)
574 val[i] -= offset[(*start+i)%1024];
575 }*/
576
577 const EVENT &evt = *e.fEvent;
578 if (!fFile->WriteEvt(evt))
579 return false;
580
581 fNumEvts[kCurrent]++;
582 fNumEvts[kEventId] = evt.EventNum;
583 fNumEvts[kTriggerId] = evt.TriggerNum;
584 fNumEvts[kTotal]++;
585
586 static Time oldt(boost::date_time::neg_infin);
587 Time newt;
588 if (newt>oldt+boost::posix_time::seconds(1))
589 {
590 fQueueEvents.emplace(newt, fNumEvts);
591 oldt = newt;
592 }
593
594 return true;
595 }
596
597 void runClose(RUN_CTRL2 &run)
598 {
599 if (!fFile)
600 return;
601
602 // It can happen that runFinished was never called
603 // (e.g. runWrite failed)
604 if (fRunInProgress==fFile->GetRunId())
605 fRunInProgress = -1;
606
607 // Close the file
608 const bool rc = fFile->Close(NULL);
609
610 fLastClosed = fFile->GetRunId();
611
612 ostringstream str;
613 str << "Closed: " << fFile->GetFileName() << " (" << fFile->GetRunId() << ")";
614 if (!rc)
615 str << "... failed!";
616
617 // Signal that the file is closed
618
619 fFile.reset();
620
621 // Now do all the calls which can potentially block (dim)
622
623 CloseRun(fLastClosed);
624
625 // Time for update events before time for update runs
626 fQueueEvents.emplace(Time(), fNumEvts);
627 UpdateRuns();
628
629 // Do the potentially blocking call after all others
630 rc ? fMsg.Info(str) : fMsg.Error(str);
631
632 // If a Drs Calibration has just been finished, all following events
633 // should also be processed with this calibration
634 const DrsCalibration &cal = DataCalib::GetCalibration();
635 if (!run.calib || run.calib->fStep != cal.fStep || run.calib->fRoi!=cal.fRoi)
636 run.calib = shared_ptr<DrsCalibration>(new DrsCalibration(cal));
637 }
638
639 virtual void CloseRun(uint32_t /*runid*/) { }
640
641 void UpdateDimRoi(const pair<Time, array<uint16_t,2>> &roi)
642 {
643 fDimRoi.setData(roi.second.data(), sizeof(uint16_t)*2);
644 fDimRoi.Update(roi.first);
645 }
646
647 void UpdateDimTempRefClk(const tuple<Time, array<uint32_t,40>, array<int16_t,160>> &dat)
648 {
649 const Time &tm = get<0>(dat);
650
651 const array<uint32_t,40> &clk = get<1>(dat);
652 const array<int16_t,160> &tmp = get<2>(dat);
653
654 // --------------- RefClock ---------------
655
656 // history, add current data to history
657 static list<pair<Time,array<uint32_t,40>>> listclk;
658 listclk.emplace_back(tm, clk);
659
660 // --------------- Temperatures ---------------
661
662 // history, add current data to history
663 static list<pair<Time,array<int16_t,160>>> listtmp;
664 listtmp.emplace_back(tm, tmp);
665
666 // ========== Update dim services once a second =========
667
668 static Time oldt(boost::date_time::neg_infin);
669 Time newt;
670
671 if (newt<oldt+boost::posix_time::seconds(1))
672 return;
673
674 oldt = newt;
675
676 // --------------- RefClock ---------------
677
678 // remove expired data from history
679 while (1)
680 {
681 auto it=listclk.begin();
682 if (it==listclk.end() || it->first+boost::posix_time::seconds(1)>tm)
683 break;
684 listclk.pop_front();
685 }
686
687 // Structure for dim service
688 struct Clock
689 {
690 uint16_t num;
691 float val[40];
692 Clock() { memset(this, 0, sizeof(Clock)); }
693 } __attribute__((__packed__));
694
695 // Calculate average and fll structure
696 vector<uint16_t> clknum(40);
697
698 Clock avgclk;
699 avgclk.num = listclk.size();
700 for (auto it=listclk.begin(); it!=listclk.end(); it++)
701 for (int i=0; i<40; i++)
702 if (it->second[i]!=UINT32_MAX)
703 {
704 avgclk.val[i] += it->second[i];
705 clknum[i]++;
706 }
707 for (int i=0; i<40; i++)
708 avgclk.val[i] *= 2.048/clknum[i];
709
710 // Update dim service
711 fDimRefClock.setData(avgclk);
712 fDimRefClock.Update(tm);
713
714 // --------------- Temperatures ---------------
715
716 // remove expired data from history
717 while (1)
718 {
719 auto it=listtmp.begin();
720 if (it==listtmp.end() || it->first+boost::posix_time::seconds(5)>tm)
721 break;
722 listtmp.pop_front();
723 }
724
725 // Structure for dim service
726 struct Temp
727 {
728 uint16_t num;
729 float val[160];
730 Temp() { memset(this, 0, sizeof(Temp)); }
731 } __attribute__((__packed__));
732
733 // Calculate average and fll structure
734 vector<uint32_t> tmpnum(160);
735
736 Temp avgtmp;
737 avgtmp.num = listtmp.size();
738 for (auto it=listtmp.begin(); it!=listtmp.end(); it++)
739 for (int i=0; i<160; i++)
740 if (it->second[i]!=INT16_MIN)
741 {
742 avgtmp.val[i] += it->second[i];
743 tmpnum[i]++;
744 }
745 for (int i=0; i<160; i++)
746 avgtmp.val[i] /= tmpnum[i]*16;
747
748 // Update dim service
749 fDimTemperature.setData(avgtmp);
750 fDimTemperature.Update(tm);
751 }
752
753 bool eventCheck(const EVT_CTRL2 &evt)
754 {
755 const EVENT *event = evt.fEvent;
756
757 const Time tm(evt.time);
758
759 const array<uint16_t,2> roi = {{ event->Roi, event->RoiTM }};
760
761 if (roi!=fVecRoi)
762 {
763 fQueueRoi.emplace(tm, roi);
764 fVecRoi = roi;
765 }
766
767 const FAD::EventHeader *beg = reinterpret_cast<const FAD::EventHeader*>(evt.FADhead);
768 const FAD::EventHeader *end = reinterpret_cast<const FAD::EventHeader*>(evt.FADhead)+40;
769
770 // FIMXE: Compare with target configuration
771
772 // Copy data to array
773 array<uint32_t,40> clk;
774 array<int16_t,160> tmp;
775
776 for (int i=0; i<40; i++)
777 clk[i] = UINT32_MAX;
778
779 for (int i=0; i<160; i++)
780 tmp[i] = INT16_MIN;
781
782 //fill(clk.data(), clk.data()+ 40, UINT32_MAX);
783 //fill(tmp.data(), tmp.data()+160, INT16_MIN);
784
785 for (const FAD::EventHeader *ptr=beg; ptr!=end; ptr++)
786 {
787 // FIXME: Compare with expectations!!!
788 if (ptr->fStartDelimiter==0)
789 {
790 if (ptr==beg)
791 beg++;
792 continue;
793 }
794
795 clk[ptr->Id()] = ptr->fFreqRefClock;
796 for (int i=0; i<4; i++)
797 tmp[ptr->Id()*4+i] = ptr->fTempDrs[i];
798
799 if (beg->fStatus != ptr->fStatus)
800 {
801 fMsg.Error("Inconsistency in FAD status detected.... closing run.");
802 return false;
803 }
804
805 if (beg->fRunNumber != ptr->fRunNumber)
806 {
807 fMsg.Error("Inconsistent run number detected.... closing run.");
808 return false;
809 }
810
811 /*
812 if (beg->fVersion != ptr->fVersion)
813 {
814 Error("Inconsist firmware version detected.... closing run.");
815 CloseRunFile(runNr, 0, 0);
816 break;
817 }
818 */
819 if (beg->fEventCounter != ptr->fEventCounter)
820 {
821 fMsg.Error("Inconsistent FAD event number detected.... closing run.");
822 return false;
823 }
824
825 if (beg->fTriggerCounter != ptr->fTriggerCounter)
826 {
827 fMsg.Error("Inconsistent FTM trigger number detected.... closing run.");
828 return false;
829 }
830
831 // FIXME: Check with first event!
832 if (beg->fAdcClockPhaseShift != ptr->fAdcClockPhaseShift)
833 {
834 fMsg.Error("Inconsistent phase shift detected.... closing run.");
835 return false;
836 }
837
838 // FIXME: Check with first event!
839 if (memcmp(beg->fDac, ptr->fDac, sizeof(beg->fDac)))
840 {
841 fMsg.Error("Inconsistent DAC values detected.... closing run.");
842 return false;
843 }
844
845 if (beg->fTriggerType != ptr->fTriggerType)
846 {
847 fMsg.Error("Inconsistent trigger type detected.... closing run.");
848 return false;
849 }
850 }
851
852 fQueueTempRefClk.emplace(tm, clk, tmp);
853
854 // check REFCLK_frequency
855 // check consistency with command configuration
856 // how to log errors?
857 // need gotNewRun/closedRun to know it is finished
858
859 return true;
860 }
861
862 Time fLastDimRawData;
863 Time fLastDimEventData;
864
865 void UpdateDimRawData(const vector<char> &v)
866 {
867 const EVENT *evt = reinterpret_cast<const EVENT*>(v.data());
868
869 fDimRawData.setData(v);
870 fDimRawData.setQuality(evt->TriggerType);
871 fDimRawData.Update(Time(evt->PCTime, evt->PCUsec));
872 }
873 void UpdateDimEventData(const tuple<Time,uint32_t,array<float, 1440*4>> &tup)
874 {
875 fDimEventData.setQuality(get<1>(tup));
876 fDimEventData.setData(get<2>(tup));
877 fDimEventData.Update(get<0>(tup));
878 }
879
880 void applyCalib(const EVT_CTRL2 &evt, const size_t &size)
881 {
882 const EVENT *event = evt.fEvent;
883 const int16_t *start = event->StartPix;
884
885 // Get the reference to the run associated information
886 RUN_CTRL2 &run = *evt.runCtrl;
887
888 if (size==1) // If there is more than one event waiting (including this one), throw them away
889 {
890 Time now;
891
892 // ------------------- Copy event data to new memory --------------------
893 // (to make it thread safe; a static buffer might improve memory handling)
894 const uint16_t roi = event->Roi;
895
896 // ------------------- Apply full DRS calibration ------------------------
897 // (Is that necessray, or would a simple offset correct do well already?)
898
899 // There seems to be a problem using std::array... maybe the size is too big?
900 // array<float, (1440+160)*1024> vec2;
901 vector<float> vec((1440+160)*roi);
902 run.calib->Apply(vec.data(), event->Adc_Data, start, roi);
903
904 // ------------------- Appy DRS-step correction --------------------------
905 for (auto it=run.prevStart.begin(); it!=run.prevStart.end(); it++)
906 {
907 DrsCalibrate::CorrectStep(vec.data(), 1440, roi, it->data(), start, roi+10);
908 DrsCalibrate::CorrectStep(vec.data(), 1440, roi, it->data(), start, 3);
909 }
910
911 // ------------------------- Remove spikes --------------------------------
912 DrsCalibrate::RemoveSpikes3(vec.data(), roi);
913
914 // -------------- Update raw data dim sevice (VERY SLOW) -----------------
915 if (fQueueRawData.empty() && now>fLastDimRawData+boost::posix_time::seconds(5))
916 {
917 vector<char> data1(sizeof(EVENT)+vec.size()*sizeof(float));
918 memcpy(data1.data(), event, sizeof(EVENT));
919 memcpy(data1.data()+sizeof(EVENT), vec.data(), vec.size()*sizeof(float));
920 fQueueRawData.emplace(data1);
921
922 fLastDimRawData = now;
923 }
924
925 // ------------------------- Basic statistics -----------------------------
926 DrsCalibrate::SlidingAverage(vec.data(), roi, 10);
927
928 // If this is a cosmic event
929 array<float, 1440*4> stats; // Mean, RMS, Max, Pos
930 const float max = DrsCalibrate::GetPixelStats(stats.data(), vec.data(), roi);
931 if (evt.trgTyp==0 && max>fMaxEvent.first)
932 fMaxEvent = make_pair(max, stats);
933
934 // ------------------ Update dim service (statistics) ---------------------
935
936 if (fQueueEventData.empty() && now>fLastDimEventData+boost::posix_time::seconds(3))
937 {
938 fQueueEventData.emplace(evt.time, evt.trgTyp, evt.trgTyp==0 ? fMaxEvent.second : stats);
939 if (evt.trgTyp==0)
940 fMaxEvent.first = -FLT_MAX;
941
942 fLastDimEventData = now;
943 }
944
945 // === SendFeedbackData(PEVNT_HEADER *fadhd, EVENT *event)
946 //
947 // if (!ptr->HasTriggerLPext() && !ptr->HasTriggerLPint())
948 // return;
949 //
950 // vector<float> data2(1440); // Mean, RMS, Max, Pos, first, last
951 // DrsCalibrate::GetPixelMax(data2.data(), data.data(), event->Roi, 0, event->Roi-1);
952 //
953 // fDimFeedbackData.Update(data2);
954 }
955
956 // Keep the start cells of the last five events for further corrections
957 // As a performance improvement we could also just store the
958 // pointers to the last five events...
959 // What if a new run is started? Do we mind?
960 auto &l = run.prevStart; // History for start cells of previous events (for step calibration)
961
962 if (l.size()<5)
963 l.emplace_front();
964 else
965 {
966 auto it = l.end();
967 l.splice(l.begin(), l, --it);
968 }
969
970 memcpy(l.front().data(), start, 1440*sizeof(int16_t));
971 }
972
973 bool IsRunWaiting()
974 {
975 const lock_guard<mutex> lock(mtx_newrun);
976 return fExpectedRuns.find(fRunNumber-1)!=fExpectedRuns.end();
977 }
978
979 uint32_t GetRunNumber() const
980 {
981 return fRunNumber;
982 }
983
984 bool IncreaseRunNumber(uint32_t run)
985 {
986 if (!InitRunNumber())
987 return false;
988
989 if (run<fRunNumber)
990 {
991 ostringstream msg;
992 msg <<
993 "Run number " << run << " smaller than next available "
994 "run number " << fRunNumber << " in " << fPath << " [" << fNightAsInt << "]";
995 fMsg.Error(msg);
996 return false;
997 }
998
999 fRunNumber = run;
1000
1001 return true;
1002 }
1003
1004 void gotNewRun(RUN_CTRL2 &run)
1005 {
1006 // This is to secure iteration over fExpectedRuns
1007 const lock_guard<mutex> lock(mtx_newrun);
1008
1009 map<uint32_t,FAD::RunDescription>::iterator it = fExpectedRuns.begin();
1010 while (it!=fExpectedRuns.end())
1011 {
1012 if (it->first<run.runId)
1013 {
1014 ostringstream str;
1015 str << "runOpen - Missed run " << it->first << ".";
1016 fMsg.Info(str);
1017
1018 // Increase the iterator first, it becomes invalid with the next call
1019 const auto is = it++;
1020 fExpectedRuns.erase(is);
1021 continue;
1022 }
1023
1024 if (it->first==run.runId)
1025 break;
1026
1027 it++;
1028 }
1029
1030 if (it==fExpectedRuns.end())
1031 {
1032 ostringstream str;
1033 str << "runOpen - Run " << run.runId << " wasn't expected (maybe manual triggers)";
1034 fMsg.Warn(str);
1035
1036 // This is not ideal, but the best we can do
1037 run.night = fNightAsInt;
1038
1039 return;
1040 }
1041
1042 const FAD::RunDescription &conf = it->second;
1043
1044 run.runType = conf.name;
1045 run.maxEvt = conf.maxevt;
1046 run.closeTime = conf.maxtime + run.openTime;
1047 run.night = conf.night;
1048
1049 fExpectedRuns.erase(it);
1050
1051 // Now signal the fadctrl (configuration process that a run is in progress)
1052 // Maybe this could be done earlier, but we are talking about a
1053 // negligible time scale here.
1054 fRunInProgress = run.runId;
1055 }
1056
1057 void runFinished()
1058 {
1059 // This is called when the last event of a run (run time exceeded or
1060 // max number of events exceeded) has been received.
1061 fRunInProgress = -1;
1062 }
1063
1064 //map<boost::thread::id, string> fLastMessage;
1065
1066 void factOut(int severity, const char *message)
1067 {
1068 ostringstream str;
1069 str << "EventBuilder: " << message;
1070
1071 /*
1072 string &old = fLastMessage[boost::this_thread::get_id()];
1073
1074 if (str.str()==old)
1075 return;
1076 old = str.str();
1077 */
1078
1079 fMsg.Update(str, severity);
1080 }
1081
1082/*
1083 void factStat(int64_t *stat, int len)
1084 {
1085 if (len!=7)
1086 {
1087 fMsg.Warn("factStat received unknown number of values.");
1088 return;
1089 }
1090
1091 vector<int64_t> data(1, g_maxMem);
1092 data.insert(data.end(), stat, stat+len);
1093
1094 static vector<int64_t> last(8);
1095 if (data==last)
1096 return;
1097 last = data;
1098
1099 fDimStatistics.Update(data);
1100
1101 // len ist die Laenge des arrays.
1102 // array[4] enthaelt wieviele bytes im Buffer aktuell belegt sind; daran
1103 // kannst Du pruefen, ob die 100MB voll sind ....
1104
1105 ostringstream str;
1106 str
1107 << "Wait=" << stat[0] << " "
1108 << "Skip=" << stat[1] << " "
1109 << "Del=" << stat[2] << " "
1110 << "Tot=" << stat[3] << " "
1111 << "Mem=" << stat[4] << "/" << g_maxMem << " "
1112 << "Read=" << stat[5] << " "
1113 << "Conn=" << stat[6];
1114
1115 fMsg.Info(str);
1116 }
1117 */
1118
1119 void UpdateDimStatistics1(const pair<Time,GUI_STAT> &stat)
1120 {
1121 fDimStatistics1.setData(&stat.second, sizeof(GUI_STAT));
1122 fDimStatistics1.Update(stat.first);
1123 }
1124
1125 void factStat(const GUI_STAT &stat)
1126 {
1127 fQueueStatistics1.emplace(Time(), stat);
1128 }
1129
1130 void factReportIncomplete(uint64_t rep)
1131 {
1132 fDimIncomplete.setQuality(1);
1133 fDimIncomplete.Update(rep);
1134 }
1135
1136 array<FAD::EventHeader, 40> fVecHeader;
1137
1138 template<typename T, class S>
1139 array<T, 42> Compare(const S *vec, const T *t)
1140 {
1141 const int offset = reinterpret_cast<const char *>(t) - reinterpret_cast<const char *>(vec);
1142
1143 const T *min = NULL;
1144 const T *val = NULL;
1145 const T *max = NULL;
1146
1147 array<T, 42> arr;
1148
1149 // bool rc = true;
1150 for (int i=0; i<40; i++)
1151 {
1152 const char *base = reinterpret_cast<const char*>(vec+i);
1153 const T *ref = reinterpret_cast<const T*>(base+offset);
1154
1155 arr[i] = *ref;
1156
1157 if (gi_NumConnect[i]==0)
1158 {
1159 arr[i] = 0;
1160 continue;
1161 }
1162
1163 if (!val)
1164 {
1165 min = ref;
1166 val = ref;
1167 max = ref;
1168 }
1169
1170 if (*ref<*min)
1171 min = ref;
1172
1173 if (*ref>*max)
1174 max = ref;
1175
1176 // if (*val!=*ref)
1177 // rc = false;
1178 }
1179
1180 arr[40] = val ? *min : 1;
1181 arr[41] = val ? *max : 0;
1182
1183 return arr;
1184 }
1185
1186 template<typename T>
1187 array<T, 42> CompareBits(const FAD::EventHeader *h, const T *t)
1188 {
1189 const int offset = reinterpret_cast<const char *>(t) - reinterpret_cast<const char *>(h);
1190
1191 T val = 0;
1192 T rc = 0;
1193
1194 array<T, 42> vec;
1195
1196 bool first = true;
1197
1198 for (int i=0; i<40; i++)
1199 {
1200 const char *base = reinterpret_cast<const char*>(&fVecHeader[i]);
1201 const T *ref = reinterpret_cast<const T*>(base+offset);
1202
1203 vec[i+2] = *ref;
1204
1205 if (gi_NumConnect[i]==0)
1206 {
1207 vec[i+2] = 0;
1208 continue;
1209 }
1210
1211 if (first)
1212 {
1213 first = false;
1214 val = *ref;
1215 rc = 0;
1216 }
1217
1218 rc |= val^*ref;
1219 }
1220
1221 vec[0] = rc;
1222 vec[1] = val;
1223
1224 return vec;
1225 }
1226
1227 template<typename T, size_t N>
1228 void Update(DimDescribedService &svc, const array<T, N> &data, const Time &t=Time(), int n=N)
1229 {
1230 svc.setData(const_cast<T*>(data.data()), sizeof(T)*n);
1231 svc.Update(t);
1232 }
1233
1234 template<typename T>
1235 void Print(const char *name, const pair<bool,array<T, 43>> &data)
1236 {
1237 cout << name << "|" << data.first << "|" << data.second[1] << "|" << data.second[0] << "<x<" << data.second[1] << ":";
1238 for (int i=0; i<40;i++)
1239 cout << " " << data.second[i+3];
1240 cout << endl;
1241 }
1242
1243 vector<uint> fNumConnected;
1244
1245 void procHeader(const tuple<Time,bool,FAD::EventHeader> &dat)
1246 {
1247 const Time &t = get<0>(dat);
1248 const bool changed = get<1>(dat);
1249 const FAD::EventHeader &h = get<2>(dat);
1250
1251 const FAD::EventHeader old = fVecHeader[h.Id()];
1252 fVecHeader[h.Id()] = h;
1253
1254 if (old.fVersion != h.fVersion || changed)
1255 {
1256 const array<uint16_t,42> ver = Compare(&fVecHeader[0], &fVecHeader[0].fVersion);
1257
1258 array<float,42> data;
1259 for (int i=0; i<42; i++)
1260 {
1261 ostringstream str;
1262 str << (ver[i]>>8) << '.' << (ver[i]&0xff);
1263 data[i] = stof(str.str());
1264 }
1265 Update(fDimFwVersion, data, t);
1266 }
1267
1268 if (old.fRunNumber != h.fRunNumber || changed)
1269 {
1270 const array<uint32_t,42> run = Compare(&fVecHeader[0], &fVecHeader[0].fRunNumber);
1271 fDimRunNumber.setData(&run[0], 42*sizeof(uint32_t));
1272 fDimRunNumber.Update(t);
1273 }
1274
1275 if (old.fTriggerGeneratorPrescaler != h.fTriggerGeneratorPrescaler || changed)
1276 {
1277 const array<uint16_t,42> pre = Compare(&fVecHeader[0], &fVecHeader[0].fTriggerGeneratorPrescaler);
1278 fDimPrescaler.setData(&pre[0], 42*sizeof(uint16_t));
1279 fDimPrescaler.Update(t);
1280 }
1281
1282 if (old.fDNA != h.fDNA || changed)
1283 {
1284 const array<uint64_t,42> dna = Compare(&fVecHeader[0], &fVecHeader[0].fDNA);
1285 Update(fDimDNA, dna, t, 40);
1286 }
1287
1288 if (old.fStatus != h.fStatus || changed)
1289 {
1290 const array<uint16_t,42> sts = CompareBits(&fVecHeader[0], &fVecHeader[0].fStatus);
1291 Update(fDimStatus, sts, t);
1292 }
1293
1294 if (memcmp(old.fDac, h.fDac, sizeof(h.fDac)) || changed)
1295 {
1296 array<uint16_t, FAD::kNumDac*42> dacs;
1297
1298 for (int i=0; i<FAD::kNumDac; i++)
1299 {
1300 const array<uint16_t, 42> dac = Compare(&fVecHeader[0], &fVecHeader[0].fDac[i]);
1301 memcpy(&dacs[i*42], &dac[0], sizeof(uint16_t)*42);
1302 }
1303
1304 Update(fDimDac, dacs, t);
1305 }
1306 }
1307
1308 void debugHead(const FAD::EventHeader &h)
1309 {
1310 const uint16_t id = h.Id();
1311 if (id>39)
1312 return;
1313
1314 if (fNumConnected.size()!=40)
1315 fNumConnected.resize(40);
1316
1317 const vector<uint> con(gi_NumConnect, gi_NumConnect+40);
1318
1319 const bool changed = con!=fNumConnected || !IsThreadRunning();
1320
1321 fNumConnected = con;
1322
1323 fQueueProcHeader.emplace(Time(), changed, h);
1324 }
1325};
1326
1327EventBuilderWrapper *EventBuilderWrapper::This = 0;
1328
1329// ----------- Event builder callbacks implementation ---------------
1330bool runOpen(const EVT_CTRL2 &evt)
1331{
1332 return EventBuilderWrapper::This->runOpen(evt);
1333}
1334
1335bool runWrite(const EVT_CTRL2 &evt)
1336{
1337 return EventBuilderWrapper::This->runWrite(evt);
1338}
1339
1340void runClose(RUN_CTRL2 &run)
1341{
1342 EventBuilderWrapper::This->runClose(run);
1343}
1344
1345bool eventCheck(const EVT_CTRL2 &evt)
1346{
1347 return EventBuilderWrapper::This->eventCheck(evt);
1348}
1349
1350void gotNewRun(RUN_CTRL2 &run)
1351{
1352 EventBuilderWrapper::This->gotNewRun(run);
1353}
1354
1355void runFinished()
1356{
1357 EventBuilderWrapper::This->runFinished();
1358}
1359
1360void applyCalib(const EVT_CTRL2 &evt, const size_t &size)
1361{
1362 EventBuilderWrapper::This->applyCalib(evt, size);
1363}
1364
1365void factOut(int severity, const char *message)
1366{
1367 EventBuilderWrapper::This->factOut(severity, message);
1368}
1369
1370void factStat(const GUI_STAT &stat)
1371{
1372 EventBuilderWrapper::This->factStat(stat);
1373}
1374
1375void factReportIncomplete(uint64_t rep)
1376{
1377 EventBuilderWrapper::This->factReportIncomplete(rep);
1378}
1379
1380// ------
1381
1382void debugHead(void *buf)
1383{
1384 const FAD::EventHeader &h = *reinterpret_cast<FAD::EventHeader*>(buf);
1385 EventBuilderWrapper::This->debugHead(h);
1386}
1387
1388#endif
Note: See TracBrowser for help on using the repository browser.