Pottu
LogicalEventCollector.hpp
Go to the documentation of this file.
1 
6 #ifndef H_POTTU_LOGICALEVENTCOLLECTOR
7 #define H_POTTU_LOGICALEVENTCOLLECTOR
8 
9 
10 #include "DetectorEvent.hpp"
11 
12 #include <deque>
13 #include <cstdint>
14 #include <algorithm>
15 
16 
17 namespace pottu {
18 
19 
26  template <class Sort, class Tr>
28  public:
29 
30  Sort &_sort;
31  Tr &_tr;
32 
33  // Trigger and event lengths.
34  int64_t _triggerDeadTime{300};
35  int64_t _eventDelay{-100};
36  int64_t _maxEventLength{600};
37 
39  int64_t triggerTime;
40  int64_t startTime;
41  int64_t stopTime;
42  };
43 
44  std::deque<event_collection_window_t> _eventWindows;
45 
46  int64_t _tsLast{0};
47  std::deque<pottu::DetectorEvent> _queue;
48 
49 
50  LogicalEventCollector( Sort &sort, Tr &tr,
51  int64_t triggerDeadTime,
52  int64_t eventDelay, int64_t maxEventLength )
53  : _sort(sort),
54  _tr(tr),
55  _triggerDeadTime(triggerDeadTime),
56  _eventDelay(eventDelay),
57  _maxEventLength(maxEventLength)
58  {
59  }
60 
61 
62 
63 
69  template <class Cont>
70  void feed( const Cont &data ) noexcept {
71 
72  // Looping over all detector events
73  for (auto &det : data)
74  {
75  // Timestamp
76  // If the timestamp appears earlier than the last, empty all queues and settings
77  // (no sense in considering those at this point.)
78  const int64_t ts = det.time;
79  if( ts < _tsLast ) {
80  _queue.clear();
81  _tsLast = 0;
82  _eventWindows.clear();
83  _tr.reset();
84 
85  _sort.onTimeOrderError();
86  }
87 
88 
89  const bool newTrigger = _tr.feed( det );
90  const bool onDeadTime = _eventWindows.size() ? ts < _eventWindows.back().triggerTime + _triggerDeadTime
91  : false;
92 
93 
94  if( newTrigger && !onDeadTime ) {
95  // Checking the end of the previous window and
96  // shortening it if needed.
97  if( _eventWindows.size() && _eventWindows.back().stopTime > ts+_eventDelay ) {
98  _eventWindows.back().stopTime = ts+_eventDelay;
99  //fmt::print( "## Adjusted the stop time\n" );
100  }
101 
102  // Pushing new window to queue of windows. The end
103  // of the window might be shortened when the next
104  // trigger arrives.
105  const event_collection_window_t window{ts, ts+_eventDelay, ts+_eventDelay+_maxEventLength };
106  _eventWindows.push_back( window );
107  }
108 
109  // Always pushing the event into the queue
110  _queue.push_back( det );
111 
112 
113  // Throwing away events we do not need
114  const int64_t ignoreUntil = _eventWindows.size()
115  ? std::min(_eventWindows.front().startTime, ts+_eventDelay )
116  : ts+_eventDelay;
117  while( _queue.size() && _queue.front().time < ignoreUntil )
118  _queue.pop_front();
119 
120  // Do we have data for event to populate it fully
121  if( _eventWindows.size() && _eventWindows.front().stopTime < ts + _eventDelay ) {
122  const auto iterLast = std::lower_bound( _queue.begin(), _queue.end(), _eventWindows.front().stopTime,
123  []( const pottu::DetectorEvent &e, const int64_t &value ){
124  return e.time < value; } );
125 
126  _sort.constructAndProcessLogicalEvent( _queue.begin(), iterLast,
127  _eventWindows.front().triggerTime,
128  _eventWindows.front().startTime, _eventWindows.front().stopTime );
129 
130  _queue.erase( _queue.begin(), iterLast );
131  _eventWindows.pop_front();
132  }
133 
134 
135  // Needed to shield against time order error
136  _tsLast = ts;
137  }
138 
139  }
140 
141  };
142 
143 }
144 
145 
146 #endif
Event containing event information of one daq channel.
Definition: DetectorEvent.hpp:47
Class which creates the logical events from stream of detector events.
Definition: LogicalEventCollector.hpp:27
void feed(const Cont &data) noexcept
Use this to feed detector event to this machine.
Definition: LogicalEventCollector.hpp:70
Definition: mainpage.dox:6
Definition: LogicalEventCollector.hpp:38