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 |
|
---|
30 | namespace ba = boost::asio;
|
---|
31 | namespace bs = boost::system;
|
---|
32 | namespace fs = boost::filesystem;
|
---|
33 |
|
---|
34 | using ba::ip::tcp;
|
---|
35 |
|
---|
36 | using namespace std;
|
---|
37 |
|
---|
38 | // ========================================================================
|
---|
39 |
|
---|
40 | #include "EventBuilder.h"
|
---|
41 |
|
---|
42 | void StartEvtBuild();
|
---|
43 | void CloseRunFile();
|
---|
44 |
|
---|
45 | // ========================================================================
|
---|
46 |
|
---|
47 | class EventBuilderWrapper
|
---|
48 | {
|
---|
49 | public:
|
---|
50 | // FIXME
|
---|
51 | static EventBuilderWrapper *This;
|
---|
52 |
|
---|
53 | MessageImp &fMsg;
|
---|
54 |
|
---|
55 | private:
|
---|
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 |
|
---|
111 | protected:
|
---|
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 |
|
---|
172 | public:
|
---|
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 auto delay = boost::posix_time::seconds(5);
|
---|
650 |
|
---|
651 | const Time &tm = get<0>(dat);
|
---|
652 |
|
---|
653 | const array<uint32_t,40> &clk = get<1>(dat);
|
---|
654 | const array<int16_t,160> &tmp = get<2>(dat);
|
---|
655 |
|
---|
656 | // --------------- RefClock ---------------
|
---|
657 |
|
---|
658 | // history, add current data to history
|
---|
659 | static list<pair<Time,array<uint32_t,40>>> listclk;
|
---|
660 | listclk.emplace_back(tm, clk);
|
---|
661 |
|
---|
662 | // --------------- Temperatures ---------------
|
---|
663 |
|
---|
664 | // history, add current data to history
|
---|
665 | static list<pair<Time,array<int16_t,160>>> listtmp;
|
---|
666 | listtmp.emplace_back(tm, tmp);
|
---|
667 |
|
---|
668 | // ========== Update dim services once a second =========
|
---|
669 |
|
---|
670 | static Time oldt(boost::date_time::neg_infin);
|
---|
671 | Time newt;
|
---|
672 |
|
---|
673 | if (newt<oldt+delay)
|
---|
674 | return;
|
---|
675 |
|
---|
676 | oldt = newt;
|
---|
677 |
|
---|
678 | // --------------- RefClock ---------------
|
---|
679 |
|
---|
680 | // remove expired data from history
|
---|
681 | while (1)
|
---|
682 | {
|
---|
683 | auto it=listclk.begin();
|
---|
684 | if (it==listclk.end() || it->first+delay>tm)
|
---|
685 | break;
|
---|
686 | listclk.pop_front();
|
---|
687 | }
|
---|
688 |
|
---|
689 | // Structure for dim service
|
---|
690 | struct Clock
|
---|
691 | {
|
---|
692 | uint16_t num;
|
---|
693 | float val[40];
|
---|
694 | Clock() { memset(this, 0, sizeof(Clock)); }
|
---|
695 | } __attribute__((__packed__));
|
---|
696 |
|
---|
697 | // Calculate average and fll structure
|
---|
698 | vector<uint16_t> clknum(40);
|
---|
699 |
|
---|
700 | Clock avgclk;
|
---|
701 | avgclk.num = listclk.size();
|
---|
702 | for (auto it=listclk.begin(); it!=listclk.end(); it++)
|
---|
703 | for (int i=0; i<40; i++)
|
---|
704 | if (it->second[i]!=UINT32_MAX)
|
---|
705 | {
|
---|
706 | avgclk.val[i] += it->second[i];
|
---|
707 | clknum[i]++;
|
---|
708 | }
|
---|
709 | for (int i=0; i<40; i++)
|
---|
710 | avgclk.val[i] *= 2.048/clknum[i];
|
---|
711 |
|
---|
712 | // Update dim service
|
---|
713 | fDimRefClock.setData(avgclk);
|
---|
714 | fDimRefClock.Update(tm);
|
---|
715 |
|
---|
716 | listclk.clear();
|
---|
717 |
|
---|
718 | // --------------- Temperatures ---------------
|
---|
719 |
|
---|
720 | // remove expired data from history
|
---|
721 | while (1)
|
---|
722 | {
|
---|
723 | auto it=listtmp.begin();
|
---|
724 | if (it==listtmp.end() || it->first+delay>tm)
|
---|
725 | break;
|
---|
726 | listtmp.pop_front();
|
---|
727 | }
|
---|
728 |
|
---|
729 | // Structure for dim service
|
---|
730 | struct Temp
|
---|
731 | {
|
---|
732 | uint16_t num;
|
---|
733 | float val[160];
|
---|
734 | Temp() { memset(this, 0, sizeof(Temp)); }
|
---|
735 | } __attribute__((__packed__));
|
---|
736 |
|
---|
737 | // Calculate average and fll structure
|
---|
738 | vector<uint32_t> tmpnum(160);
|
---|
739 |
|
---|
740 | Temp avgtmp;
|
---|
741 | avgtmp.num = listtmp.size();
|
---|
742 | for (auto it=listtmp.begin(); it!=listtmp.end(); it++)
|
---|
743 | for (int i=0; i<160; i++)
|
---|
744 | if (it->second[i]!=INT16_MIN)
|
---|
745 | {
|
---|
746 | avgtmp.val[i] += it->second[i];
|
---|
747 | tmpnum[i]++;
|
---|
748 | }
|
---|
749 | for (int i=0; i<160; i++)
|
---|
750 | avgtmp.val[i] /= tmpnum[i]*16;
|
---|
751 |
|
---|
752 | // Update dim service
|
---|
753 | fDimTemperature.setData(avgtmp);
|
---|
754 | fDimTemperature.Update(tm);
|
---|
755 |
|
---|
756 | listtmp.clear();
|
---|
757 | }
|
---|
758 |
|
---|
759 | bool eventCheck(const EVT_CTRL2 &evt)
|
---|
760 | {
|
---|
761 | const EVENT *event = evt.fEvent;
|
---|
762 |
|
---|
763 | const Time tm(evt.time);
|
---|
764 |
|
---|
765 | const array<uint16_t,2> roi = {{ event->Roi, event->RoiTM }};
|
---|
766 |
|
---|
767 | if (roi!=fVecRoi)
|
---|
768 | {
|
---|
769 | fQueueRoi.emplace(tm, roi);
|
---|
770 | fVecRoi = roi;
|
---|
771 | }
|
---|
772 |
|
---|
773 | const FAD::EventHeader *beg = reinterpret_cast<const FAD::EventHeader*>(evt.FADhead);
|
---|
774 | const FAD::EventHeader *end = reinterpret_cast<const FAD::EventHeader*>(evt.FADhead)+40;
|
---|
775 |
|
---|
776 | // FIMXE: Compare with target configuration
|
---|
777 |
|
---|
778 | // Copy data to array
|
---|
779 | array<uint32_t,40> clk;
|
---|
780 | array<int16_t,160> tmp;
|
---|
781 |
|
---|
782 | for (int i=0; i<40; i++)
|
---|
783 | clk[i] = UINT32_MAX;
|
---|
784 |
|
---|
785 | for (int i=0; i<160; i++)
|
---|
786 | tmp[i] = INT16_MIN;
|
---|
787 |
|
---|
788 | //fill(clk.data(), clk.data()+ 40, UINT32_MAX);
|
---|
789 | //fill(tmp.data(), tmp.data()+160, INT16_MIN);
|
---|
790 |
|
---|
791 | for (const FAD::EventHeader *ptr=beg; ptr!=end; ptr++)
|
---|
792 | {
|
---|
793 | // FIXME: Compare with expectations!!!
|
---|
794 | if (ptr->fStartDelimiter==0)
|
---|
795 | {
|
---|
796 | if (ptr==beg)
|
---|
797 | beg++;
|
---|
798 | continue;
|
---|
799 | }
|
---|
800 |
|
---|
801 | clk[ptr->Id()] = ptr->fFreqRefClock;
|
---|
802 | for (int i=0; i<4; i++)
|
---|
803 | tmp[ptr->Id()*4+i] = ptr->fTempDrs[i];
|
---|
804 |
|
---|
805 | if (beg->fStatus != ptr->fStatus)
|
---|
806 | {
|
---|
807 | fMsg.Error("Inconsistency in FAD status detected.... closing run.");
|
---|
808 | return false;
|
---|
809 | }
|
---|
810 |
|
---|
811 | if (beg->fRunNumber != ptr->fRunNumber)
|
---|
812 | {
|
---|
813 | fMsg.Error("Inconsistent run number detected.... closing run.");
|
---|
814 | return false;
|
---|
815 | }
|
---|
816 |
|
---|
817 | /*
|
---|
818 | if (beg->fVersion != ptr->fVersion)
|
---|
819 | {
|
---|
820 | Error("Inconsist firmware version detected.... closing run.");
|
---|
821 | CloseRunFile(runNr, 0, 0);
|
---|
822 | break;
|
---|
823 | }
|
---|
824 | */
|
---|
825 | if (beg->fEventCounter != ptr->fEventCounter)
|
---|
826 | {
|
---|
827 | fMsg.Error("Inconsistent FAD event number detected.... closing run.");
|
---|
828 | return false;
|
---|
829 | }
|
---|
830 |
|
---|
831 | if (beg->fTriggerCounter != ptr->fTriggerCounter)
|
---|
832 | {
|
---|
833 | fMsg.Error("Inconsistent FTM trigger number detected.... closing run.");
|
---|
834 | return false;
|
---|
835 | }
|
---|
836 |
|
---|
837 | // FIXME: Check with first event!
|
---|
838 | if (beg->fAdcClockPhaseShift != ptr->fAdcClockPhaseShift)
|
---|
839 | {
|
---|
840 | fMsg.Error("Inconsistent phase shift detected.... closing run.");
|
---|
841 | return false;
|
---|
842 | }
|
---|
843 |
|
---|
844 | // FIXME: Check with first event!
|
---|
845 | if (memcmp(beg->fDac, ptr->fDac, sizeof(beg->fDac)))
|
---|
846 | {
|
---|
847 | fMsg.Error("Inconsistent DAC values detected.... closing run.");
|
---|
848 | return false;
|
---|
849 | }
|
---|
850 |
|
---|
851 | if (beg->fTriggerType != ptr->fTriggerType)
|
---|
852 | {
|
---|
853 | fMsg.Error("Inconsistent trigger type detected.... closing run.");
|
---|
854 | return false;
|
---|
855 | }
|
---|
856 | }
|
---|
857 |
|
---|
858 | fQueueTempRefClk.emplace(tm, clk, tmp);
|
---|
859 |
|
---|
860 | // check REFCLK_frequency
|
---|
861 | // check consistency with command configuration
|
---|
862 | // how to log errors?
|
---|
863 | // need gotNewRun/closedRun to know it is finished
|
---|
864 |
|
---|
865 | return true;
|
---|
866 | }
|
---|
867 |
|
---|
868 | Time fLastDimRawData;
|
---|
869 | Time fLastDimEventData;
|
---|
870 |
|
---|
871 | void UpdateDimRawData(const vector<char> &v)
|
---|
872 | {
|
---|
873 | const EVENT *evt = reinterpret_cast<const EVENT*>(v.data());
|
---|
874 |
|
---|
875 | fDimRawData.setData(v);
|
---|
876 | fDimRawData.setQuality(evt->TriggerType);
|
---|
877 | fDimRawData.Update(Time(evt->PCTime, evt->PCUsec));
|
---|
878 | }
|
---|
879 | void UpdateDimEventData(const tuple<Time,uint32_t,array<float, 1440*4>> &tup)
|
---|
880 | {
|
---|
881 | fDimEventData.setQuality(get<1>(tup));
|
---|
882 | fDimEventData.setData(get<2>(tup));
|
---|
883 | fDimEventData.Update(get<0>(tup));
|
---|
884 | }
|
---|
885 |
|
---|
886 | void applyCalib(const EVT_CTRL2 &evt, const size_t &size)
|
---|
887 | {
|
---|
888 | const EVENT *event = evt.fEvent;
|
---|
889 | const int16_t *start = event->StartPix;
|
---|
890 |
|
---|
891 | // Get the reference to the run associated information
|
---|
892 | RUN_CTRL2 &run = *evt.runCtrl;
|
---|
893 |
|
---|
894 | if (size==1) // If there is more than one event waiting (including this one), throw them away
|
---|
895 | {
|
---|
896 | Time now;
|
---|
897 |
|
---|
898 | // ------------------- Copy event data to new memory --------------------
|
---|
899 | // (to make it thread safe; a static buffer might improve memory handling)
|
---|
900 | const uint16_t roi = event->Roi;
|
---|
901 |
|
---|
902 | // ------------------- Apply full DRS calibration ------------------------
|
---|
903 | // (Is that necessray, or would a simple offset correct do well already?)
|
---|
904 |
|
---|
905 | // There seems to be a problem using std::array... maybe the size is too big?
|
---|
906 | // array<float, (1440+160)*1024> vec2;
|
---|
907 | vector<float> vec((1440+160)*roi);
|
---|
908 | run.calib->Apply(vec.data(), event->Adc_Data, start, roi);
|
---|
909 |
|
---|
910 | // ------------------- Appy DRS-step correction --------------------------
|
---|
911 | for (auto it=run.prevStart.begin(); it!=run.prevStart.end(); it++)
|
---|
912 | {
|
---|
913 | DrsCalibrate::CorrectStep(vec.data(), 1440, roi, it->data(), start, roi+10);
|
---|
914 | DrsCalibrate::CorrectStep(vec.data(), 1440, roi, it->data(), start, 3);
|
---|
915 | }
|
---|
916 |
|
---|
917 | // ------------------------- Remove spikes --------------------------------
|
---|
918 | DrsCalibrate::RemoveSpikes3(vec.data(), roi);
|
---|
919 |
|
---|
920 | // -------------- Update raw data dim sevice (VERY SLOW) -----------------
|
---|
921 | if (fQueueRawData.empty() && now>fLastDimRawData+boost::posix_time::seconds(5))
|
---|
922 | {
|
---|
923 | vector<char> data1(sizeof(EVENT)+vec.size()*sizeof(float));
|
---|
924 | memcpy(data1.data(), event, sizeof(EVENT));
|
---|
925 | memcpy(data1.data()+sizeof(EVENT), vec.data(), vec.size()*sizeof(float));
|
---|
926 | fQueueRawData.emplace(data1);
|
---|
927 |
|
---|
928 | fLastDimRawData = now;
|
---|
929 | }
|
---|
930 |
|
---|
931 | // ------------------------- Basic statistics -----------------------------
|
---|
932 | DrsCalibrate::SlidingAverage(vec.data(), roi, 10);
|
---|
933 |
|
---|
934 | // If this is a cosmic event
|
---|
935 | array<float, 1440*4> stats; // Mean, RMS, Max, Pos
|
---|
936 | const float max = DrsCalibrate::GetPixelStats(stats.data(), vec.data(), roi);
|
---|
937 | if (evt.trgTyp==0 && max>fMaxEvent.first)
|
---|
938 | fMaxEvent = make_pair(max, stats);
|
---|
939 |
|
---|
940 | // ------------------ Update dim service (statistics) ---------------------
|
---|
941 |
|
---|
942 | if (fQueueEventData.empty() && now>fLastDimEventData+boost::posix_time::seconds(3))
|
---|
943 | {
|
---|
944 | fQueueEventData.emplace(evt.time, evt.trgTyp, evt.trgTyp==0 ? fMaxEvent.second : stats);
|
---|
945 | if (evt.trgTyp==0)
|
---|
946 | fMaxEvent.first = -FLT_MAX;
|
---|
947 |
|
---|
948 | fLastDimEventData = now;
|
---|
949 | }
|
---|
950 |
|
---|
951 | // === SendFeedbackData(PEVNT_HEADER *fadhd, EVENT *event)
|
---|
952 | //
|
---|
953 | // if (!ptr->HasTriggerLPext() && !ptr->HasTriggerLPint())
|
---|
954 | // return;
|
---|
955 | //
|
---|
956 | // vector<float> data2(1440); // Mean, RMS, Max, Pos, first, last
|
---|
957 | // DrsCalibrate::GetPixelMax(data2.data(), data.data(), event->Roi, 0, event->Roi-1);
|
---|
958 | //
|
---|
959 | // fDimFeedbackData.Update(data2);
|
---|
960 | }
|
---|
961 |
|
---|
962 | // Keep the start cells of the last five events for further corrections
|
---|
963 | // As a performance improvement we could also just store the
|
---|
964 | // pointers to the last five events...
|
---|
965 | // What if a new run is started? Do we mind?
|
---|
966 | auto &l = run.prevStart; // History for start cells of previous events (for step calibration)
|
---|
967 |
|
---|
968 | if (l.size()<5)
|
---|
969 | l.emplace_front();
|
---|
970 | else
|
---|
971 | {
|
---|
972 | auto it = l.end();
|
---|
973 | l.splice(l.begin(), l, --it);
|
---|
974 | }
|
---|
975 |
|
---|
976 | memcpy(l.front().data(), start, 1440*sizeof(int16_t));
|
---|
977 | }
|
---|
978 |
|
---|
979 | bool IsRunWaiting()
|
---|
980 | {
|
---|
981 | const lock_guard<mutex> lock(mtx_newrun);
|
---|
982 | return fExpectedRuns.find(fRunNumber-1)!=fExpectedRuns.end();
|
---|
983 | }
|
---|
984 |
|
---|
985 | uint32_t GetRunNumber() const
|
---|
986 | {
|
---|
987 | return fRunNumber;
|
---|
988 | }
|
---|
989 |
|
---|
990 | bool IncreaseRunNumber(uint32_t run)
|
---|
991 | {
|
---|
992 | if (!InitRunNumber())
|
---|
993 | return false;
|
---|
994 |
|
---|
995 | if (run<fRunNumber)
|
---|
996 | {
|
---|
997 | ostringstream msg;
|
---|
998 | msg <<
|
---|
999 | "Run number " << run << " smaller than next available "
|
---|
1000 | "run number " << fRunNumber << " in " << fPath << " [" << fNightAsInt << "]";
|
---|
1001 | fMsg.Error(msg);
|
---|
1002 | return false;
|
---|
1003 | }
|
---|
1004 |
|
---|
1005 | fRunNumber = run;
|
---|
1006 |
|
---|
1007 | return true;
|
---|
1008 | }
|
---|
1009 |
|
---|
1010 | void gotNewRun(RUN_CTRL2 &run)
|
---|
1011 | {
|
---|
1012 | // This is to secure iteration over fExpectedRuns
|
---|
1013 | const lock_guard<mutex> lock(mtx_newrun);
|
---|
1014 |
|
---|
1015 | map<uint32_t,FAD::RunDescription>::iterator it = fExpectedRuns.begin();
|
---|
1016 | while (it!=fExpectedRuns.end())
|
---|
1017 | {
|
---|
1018 | if (it->first<run.runId)
|
---|
1019 | {
|
---|
1020 | ostringstream str;
|
---|
1021 | str << "runOpen - Missed run " << it->first << ".";
|
---|
1022 | fMsg.Info(str);
|
---|
1023 |
|
---|
1024 | // Increase the iterator first, it becomes invalid with the next call
|
---|
1025 | const auto is = it++;
|
---|
1026 | fExpectedRuns.erase(is);
|
---|
1027 | continue;
|
---|
1028 | }
|
---|
1029 |
|
---|
1030 | if (it->first==run.runId)
|
---|
1031 | break;
|
---|
1032 |
|
---|
1033 | it++;
|
---|
1034 | }
|
---|
1035 |
|
---|
1036 | if (it==fExpectedRuns.end())
|
---|
1037 | {
|
---|
1038 | ostringstream str;
|
---|
1039 | str << "runOpen - Run " << run.runId << " wasn't expected (maybe manual triggers)";
|
---|
1040 | fMsg.Warn(str);
|
---|
1041 |
|
---|
1042 | // This is not ideal, but the best we can do
|
---|
1043 | run.night = fNightAsInt;
|
---|
1044 |
|
---|
1045 | return;
|
---|
1046 | }
|
---|
1047 |
|
---|
1048 | const FAD::RunDescription &conf = it->second;
|
---|
1049 |
|
---|
1050 | run.runType = conf.name;
|
---|
1051 | run.maxEvt = conf.maxevt;
|
---|
1052 | run.closeTime = conf.maxtime + run.openTime;
|
---|
1053 | run.night = conf.night;
|
---|
1054 |
|
---|
1055 | fExpectedRuns.erase(it);
|
---|
1056 |
|
---|
1057 | // Now signal the fadctrl (configuration process that a run is in progress)
|
---|
1058 | // Maybe this could be done earlier, but we are talking about a
|
---|
1059 | // negligible time scale here.
|
---|
1060 | fRunInProgress = run.runId;
|
---|
1061 | }
|
---|
1062 |
|
---|
1063 | void runFinished()
|
---|
1064 | {
|
---|
1065 | // This is called when the last event of a run (run time exceeded or
|
---|
1066 | // max number of events exceeded) has been received.
|
---|
1067 | fRunInProgress = -1;
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | //map<boost::thread::id, string> fLastMessage;
|
---|
1071 |
|
---|
1072 | void factOut(int severity, const char *message)
|
---|
1073 | {
|
---|
1074 | ostringstream str;
|
---|
1075 | str << "EventBuilder: " << message;
|
---|
1076 |
|
---|
1077 | /*
|
---|
1078 | string &old = fLastMessage[boost::this_thread::get_id()];
|
---|
1079 |
|
---|
1080 | if (str.str()==old)
|
---|
1081 | return;
|
---|
1082 | old = str.str();
|
---|
1083 | */
|
---|
1084 |
|
---|
1085 | fMsg.Update(str, severity);
|
---|
1086 | }
|
---|
1087 |
|
---|
1088 | /*
|
---|
1089 | void factStat(int64_t *stat, int len)
|
---|
1090 | {
|
---|
1091 | if (len!=7)
|
---|
1092 | {
|
---|
1093 | fMsg.Warn("factStat received unknown number of values.");
|
---|
1094 | return;
|
---|
1095 | }
|
---|
1096 |
|
---|
1097 | vector<int64_t> data(1, g_maxMem);
|
---|
1098 | data.insert(data.end(), stat, stat+len);
|
---|
1099 |
|
---|
1100 | static vector<int64_t> last(8);
|
---|
1101 | if (data==last)
|
---|
1102 | return;
|
---|
1103 | last = data;
|
---|
1104 |
|
---|
1105 | fDimStatistics.Update(data);
|
---|
1106 |
|
---|
1107 | // len ist die Laenge des arrays.
|
---|
1108 | // array[4] enthaelt wieviele bytes im Buffer aktuell belegt sind; daran
|
---|
1109 | // kannst Du pruefen, ob die 100MB voll sind ....
|
---|
1110 |
|
---|
1111 | ostringstream str;
|
---|
1112 | str
|
---|
1113 | << "Wait=" << stat[0] << " "
|
---|
1114 | << "Skip=" << stat[1] << " "
|
---|
1115 | << "Del=" << stat[2] << " "
|
---|
1116 | << "Tot=" << stat[3] << " "
|
---|
1117 | << "Mem=" << stat[4] << "/" << g_maxMem << " "
|
---|
1118 | << "Read=" << stat[5] << " "
|
---|
1119 | << "Conn=" << stat[6];
|
---|
1120 |
|
---|
1121 | fMsg.Info(str);
|
---|
1122 | }
|
---|
1123 | */
|
---|
1124 |
|
---|
1125 | void UpdateDimStatistics1(const pair<Time,GUI_STAT> &stat)
|
---|
1126 | {
|
---|
1127 | fDimStatistics1.setData(&stat.second, sizeof(GUI_STAT));
|
---|
1128 | fDimStatistics1.Update(stat.first);
|
---|
1129 | }
|
---|
1130 |
|
---|
1131 | void factStat(const GUI_STAT &stat)
|
---|
1132 | {
|
---|
1133 | fQueueStatistics1.emplace(Time(), stat);
|
---|
1134 | }
|
---|
1135 |
|
---|
1136 | void factReportIncomplete(uint64_t rep)
|
---|
1137 | {
|
---|
1138 | fDimIncomplete.setQuality(1);
|
---|
1139 | fDimIncomplete.Update(rep);
|
---|
1140 | }
|
---|
1141 |
|
---|
1142 | array<FAD::EventHeader, 40> fVecHeader;
|
---|
1143 |
|
---|
1144 | template<typename T, class S>
|
---|
1145 | array<T, 42> Compare(const S *vec, const T *t)
|
---|
1146 | {
|
---|
1147 | const int offset = reinterpret_cast<const char *>(t) - reinterpret_cast<const char *>(vec);
|
---|
1148 |
|
---|
1149 | const T *min = NULL;
|
---|
1150 | const T *val = NULL;
|
---|
1151 | const T *max = NULL;
|
---|
1152 |
|
---|
1153 | array<T, 42> arr;
|
---|
1154 |
|
---|
1155 | // bool rc = true;
|
---|
1156 | for (int i=0; i<40; i++)
|
---|
1157 | {
|
---|
1158 | const char *base = reinterpret_cast<const char*>(vec+i);
|
---|
1159 | const T *ref = reinterpret_cast<const T*>(base+offset);
|
---|
1160 |
|
---|
1161 | arr[i] = *ref;
|
---|
1162 |
|
---|
1163 | if (gi_NumConnect[i]==0)
|
---|
1164 | {
|
---|
1165 | arr[i] = 0;
|
---|
1166 | continue;
|
---|
1167 | }
|
---|
1168 |
|
---|
1169 | if (!val)
|
---|
1170 | {
|
---|
1171 | min = ref;
|
---|
1172 | val = ref;
|
---|
1173 | max = ref;
|
---|
1174 | }
|
---|
1175 |
|
---|
1176 | if (*ref<*min)
|
---|
1177 | min = ref;
|
---|
1178 |
|
---|
1179 | if (*ref>*max)
|
---|
1180 | max = ref;
|
---|
1181 |
|
---|
1182 | // if (*val!=*ref)
|
---|
1183 | // rc = false;
|
---|
1184 | }
|
---|
1185 |
|
---|
1186 | arr[40] = val ? *min : 1;
|
---|
1187 | arr[41] = val ? *max : 0;
|
---|
1188 |
|
---|
1189 | return arr;
|
---|
1190 | }
|
---|
1191 |
|
---|
1192 | template<typename T>
|
---|
1193 | array<T, 42> CompareBits(const FAD::EventHeader *h, const T *t)
|
---|
1194 | {
|
---|
1195 | const int offset = reinterpret_cast<const char *>(t) - reinterpret_cast<const char *>(h);
|
---|
1196 |
|
---|
1197 | T val = 0;
|
---|
1198 | T rc = 0;
|
---|
1199 |
|
---|
1200 | array<T, 42> vec;
|
---|
1201 |
|
---|
1202 | bool first = true;
|
---|
1203 |
|
---|
1204 | for (int i=0; i<40; i++)
|
---|
1205 | {
|
---|
1206 | const char *base = reinterpret_cast<const char*>(&fVecHeader[i]);
|
---|
1207 | const T *ref = reinterpret_cast<const T*>(base+offset);
|
---|
1208 |
|
---|
1209 | vec[i+2] = *ref;
|
---|
1210 |
|
---|
1211 | if (gi_NumConnect[i]==0)
|
---|
1212 | {
|
---|
1213 | vec[i+2] = 0;
|
---|
1214 | continue;
|
---|
1215 | }
|
---|
1216 |
|
---|
1217 | if (first)
|
---|
1218 | {
|
---|
1219 | first = false;
|
---|
1220 | val = *ref;
|
---|
1221 | rc = 0;
|
---|
1222 | }
|
---|
1223 |
|
---|
1224 | rc |= val^*ref;
|
---|
1225 | }
|
---|
1226 |
|
---|
1227 | vec[0] = rc;
|
---|
1228 | vec[1] = val;
|
---|
1229 |
|
---|
1230 | return vec;
|
---|
1231 | }
|
---|
1232 |
|
---|
1233 | template<typename T, size_t N>
|
---|
1234 | void Update(DimDescribedService &svc, const array<T, N> &data, const Time &t=Time(), int n=N)
|
---|
1235 | {
|
---|
1236 | svc.setData(const_cast<T*>(data.data()), sizeof(T)*n);
|
---|
1237 | svc.Update(t);
|
---|
1238 | }
|
---|
1239 |
|
---|
1240 | template<typename T>
|
---|
1241 | void Print(const char *name, const pair<bool,array<T, 43>> &data)
|
---|
1242 | {
|
---|
1243 | cout << name << "|" << data.first << "|" << data.second[1] << "|" << data.second[0] << "<x<" << data.second[1] << ":";
|
---|
1244 | for (int i=0; i<40;i++)
|
---|
1245 | cout << " " << data.second[i+3];
|
---|
1246 | cout << endl;
|
---|
1247 | }
|
---|
1248 |
|
---|
1249 | vector<uint> fNumConnected;
|
---|
1250 |
|
---|
1251 | void procHeader(const tuple<Time,bool,FAD::EventHeader> &dat)
|
---|
1252 | {
|
---|
1253 | const Time &t = get<0>(dat);
|
---|
1254 | const bool changed = get<1>(dat);
|
---|
1255 | const FAD::EventHeader &h = get<2>(dat);
|
---|
1256 |
|
---|
1257 | const FAD::EventHeader old = fVecHeader[h.Id()];
|
---|
1258 | fVecHeader[h.Id()] = h;
|
---|
1259 |
|
---|
1260 | if (old.fVersion != h.fVersion || changed)
|
---|
1261 | {
|
---|
1262 | const array<uint16_t,42> ver = Compare(&fVecHeader[0], &fVecHeader[0].fVersion);
|
---|
1263 |
|
---|
1264 | array<float,42> data;
|
---|
1265 | for (int i=0; i<42; i++)
|
---|
1266 | {
|
---|
1267 | ostringstream str;
|
---|
1268 | str << (ver[i]>>8) << '.' << (ver[i]&0xff);
|
---|
1269 | data[i] = stof(str.str());
|
---|
1270 | }
|
---|
1271 | Update(fDimFwVersion, data, t);
|
---|
1272 | }
|
---|
1273 |
|
---|
1274 | if (old.fRunNumber != h.fRunNumber || changed)
|
---|
1275 | {
|
---|
1276 | const array<uint32_t,42> run = Compare(&fVecHeader[0], &fVecHeader[0].fRunNumber);
|
---|
1277 | fDimRunNumber.setData(&run[0], 42*sizeof(uint32_t));
|
---|
1278 | fDimRunNumber.Update(t);
|
---|
1279 | }
|
---|
1280 |
|
---|
1281 | if (old.fTriggerGeneratorPrescaler != h.fTriggerGeneratorPrescaler || changed)
|
---|
1282 | {
|
---|
1283 | const array<uint16_t,42> pre = Compare(&fVecHeader[0], &fVecHeader[0].fTriggerGeneratorPrescaler);
|
---|
1284 | fDimPrescaler.setData(&pre[0], 42*sizeof(uint16_t));
|
---|
1285 | fDimPrescaler.Update(t);
|
---|
1286 | }
|
---|
1287 |
|
---|
1288 | if (old.fDNA != h.fDNA || changed)
|
---|
1289 | {
|
---|
1290 | const array<uint64_t,42> dna = Compare(&fVecHeader[0], &fVecHeader[0].fDNA);
|
---|
1291 | Update(fDimDNA, dna, t, 40);
|
---|
1292 | }
|
---|
1293 |
|
---|
1294 | if (old.fStatus != h.fStatus || changed)
|
---|
1295 | {
|
---|
1296 | const array<uint16_t,42> sts = CompareBits(&fVecHeader[0], &fVecHeader[0].fStatus);
|
---|
1297 | Update(fDimStatus, sts, t);
|
---|
1298 | }
|
---|
1299 |
|
---|
1300 | if (memcmp(old.fDac, h.fDac, sizeof(h.fDac)) || changed)
|
---|
1301 | {
|
---|
1302 | array<uint16_t, FAD::kNumDac*42> dacs;
|
---|
1303 |
|
---|
1304 | for (int i=0; i<FAD::kNumDac; i++)
|
---|
1305 | {
|
---|
1306 | const array<uint16_t, 42> dac = Compare(&fVecHeader[0], &fVecHeader[0].fDac[i]);
|
---|
1307 | memcpy(&dacs[i*42], &dac[0], sizeof(uint16_t)*42);
|
---|
1308 | }
|
---|
1309 |
|
---|
1310 | Update(fDimDac, dacs, t);
|
---|
1311 | }
|
---|
1312 | }
|
---|
1313 |
|
---|
1314 | void debugHead(const FAD::EventHeader &h)
|
---|
1315 | {
|
---|
1316 | const uint16_t id = h.Id();
|
---|
1317 | if (id>39)
|
---|
1318 | return;
|
---|
1319 |
|
---|
1320 | if (fNumConnected.size()!=40)
|
---|
1321 | fNumConnected.resize(40);
|
---|
1322 |
|
---|
1323 | const vector<uint> con(gi_NumConnect, gi_NumConnect+40);
|
---|
1324 |
|
---|
1325 | const bool changed = con!=fNumConnected || !IsThreadRunning();
|
---|
1326 |
|
---|
1327 | fNumConnected = con;
|
---|
1328 |
|
---|
1329 | fQueueProcHeader.emplace(Time(), changed, h);
|
---|
1330 | }
|
---|
1331 | };
|
---|
1332 |
|
---|
1333 | EventBuilderWrapper *EventBuilderWrapper::This = 0;
|
---|
1334 |
|
---|
1335 | // ----------- Event builder callbacks implementation ---------------
|
---|
1336 | bool runOpen(const EVT_CTRL2 &evt)
|
---|
1337 | {
|
---|
1338 | return EventBuilderWrapper::This->runOpen(evt);
|
---|
1339 | }
|
---|
1340 |
|
---|
1341 | bool runWrite(const EVT_CTRL2 &evt)
|
---|
1342 | {
|
---|
1343 | return EventBuilderWrapper::This->runWrite(evt);
|
---|
1344 | }
|
---|
1345 |
|
---|
1346 | void runClose(RUN_CTRL2 &run)
|
---|
1347 | {
|
---|
1348 | EventBuilderWrapper::This->runClose(run);
|
---|
1349 | }
|
---|
1350 |
|
---|
1351 | bool eventCheck(const EVT_CTRL2 &evt)
|
---|
1352 | {
|
---|
1353 | return EventBuilderWrapper::This->eventCheck(evt);
|
---|
1354 | }
|
---|
1355 |
|
---|
1356 | void gotNewRun(RUN_CTRL2 &run)
|
---|
1357 | {
|
---|
1358 | EventBuilderWrapper::This->gotNewRun(run);
|
---|
1359 | }
|
---|
1360 |
|
---|
1361 | void runFinished()
|
---|
1362 | {
|
---|
1363 | EventBuilderWrapper::This->runFinished();
|
---|
1364 | }
|
---|
1365 |
|
---|
1366 | void applyCalib(const EVT_CTRL2 &evt, const size_t &size)
|
---|
1367 | {
|
---|
1368 | EventBuilderWrapper::This->applyCalib(evt, size);
|
---|
1369 | }
|
---|
1370 |
|
---|
1371 | void factOut(int severity, const char *message)
|
---|
1372 | {
|
---|
1373 | EventBuilderWrapper::This->factOut(severity, message);
|
---|
1374 | }
|
---|
1375 |
|
---|
1376 | void factStat(const GUI_STAT &stat)
|
---|
1377 | {
|
---|
1378 | EventBuilderWrapper::This->factStat(stat);
|
---|
1379 | }
|
---|
1380 |
|
---|
1381 | void factReportIncomplete(uint64_t rep)
|
---|
1382 | {
|
---|
1383 | EventBuilderWrapper::This->factReportIncomplete(rep);
|
---|
1384 | }
|
---|
1385 |
|
---|
1386 | // ------
|
---|
1387 |
|
---|
1388 | void debugHead(void *buf)
|
---|
1389 | {
|
---|
1390 | const FAD::EventHeader &h = *reinterpret_cast<FAD::EventHeader*>(buf);
|
---|
1391 | EventBuilderWrapper::This->debugHead(h);
|
---|
1392 | }
|
---|
1393 |
|
---|
1394 | #endif
|
---|