Pottu
TimeAdjuster.hpp
Go to the documentation of this file.
1 
6 #ifndef H_TIMEADJUSTER
7 #define H_TIMEADJUSTER
8 
9 #include "DetectorEvent.hpp"
10 #include "ContextBase.hpp"
12 
13 #include <array>
14 #include <deque>
15 #include <algorithm>
16 #include <stdexcept>
17 
18 namespace pottu {
19 
20 
21 
27  public:
28 
29  TimeAdjuster()
30  : _ctxh( ContextBase::getActive().createHandle("TimeAdjuster") )
31  {
32  _chTimeCorrections.fill(0);
33  }
34 
40  void setCorrection( uint16_t ch, int64_t correction ) {
41  if( ch > 4095 )
42  throw std::runtime_error( "Ch number is bigger than 4095!" );
43 
44  _chTimeCorrections[ch] = correction;
45 
46  if( correction > _maxCorrection )
47  _maxCorrection = correction;
48  if( correction < _minCorrection )
49  _minCorrection = correction;
50 
51  _correctionWidth = _maxCorrection-_minCorrection;
52  }
53 
54 
59  int64_t getCorrection( uint16_t ch ) const noexcept {
60  return ch < 4095 ? _chTimeCorrections[ch] : 0;
61  }
62 
63 
64 
65  // TO BE REMOVED: template <class ContInput, class ContOutput>
66  virtual void process( std::vector<DetectorEvent> &input, std::vector<DetectorEvent> &output ) {
67  /* TO BE REMOVED:
68  static_assert( std::is_same<typename ContInput::value_type,DetectorEvent>::value,
69  "TimeAdjuster requires DetectorEvent as a input::value_type" );
70  static_assert( std::is_same<typename ContOutput::value_type,DetectorEvent>::value,
71  "TimeAdjuster requires DetectorEvent as a output::value_type" );
72  */
73  auto proci = _ctxh->processInstance();
74 
75  output.clear();
76 
77  // Adding time correction to each event
78  for( auto &det : input ) {
79 
80  if( det.time < _tsPrev ) {
81  _ctxh->logWarning( fmt::format( "TimeOrder error: {} -> {}, {} s.",
82  _tsPrev, det.time, (det.time-_tsPrev)*1e-8 ) );
83  // Time order error! Clearing the queues
84  while( _queue.size() ) {
85  output.push_back( std::move( _queue.front() ) );
86  _queue.pop_front();
87  }
88  }
89  // Saving the time to see potential time order error later.
90  _tsPrev = det.time;
91 
92  det.time += getCorrection( det.ch );
93 
94  while( _queue.size() && _queue.front().time + _correctionWidth < det.time ) {
95  output.emplace_back( std::move( _queue.front() ) );
96  _queue.pop_front();
97  }
98 
99  const bool sortingNeeded = ( _queue.size() && _queue.back().time > det.time );
100  _queue.emplace_back( std::move(det) );
101  if( sortingNeeded )
102  std::sort( _queue.begin(), _queue.end(), DetectorEvent::compTimeLess() );
103 
104  }
105 
106  }
107 
108  ContextHandle *_ctxh;
109 
110  // Gives correction to be applied for a channel.
111  std::array<int64_t,4096> _chTimeCorrections;
112 
113  int64_t _maxCorrection{0};
114  int64_t _minCorrection{0};
115  int64_t _correctionWidth{0};
116 
117  int64_t _tsPrev{0};
118 
119  // Newest is last. These are in time order ALWAYS. However,
120  // there might be a new event which will be put in between.
121  std::deque<DetectorEvent> _queue;
122 
123  };
124 
125 
126 }
127 
128 
129 #endif
Handle to context used by specific class.
Definition: ContextBase.hpp:188
Abstract baseclass for all stages which transforms detector events to detector events.
Definition: StageDetectorEventToDetectorEvent.hpp:23
Adjusts individual channel timing and merges channels back in time order.
Definition: TimeAdjuster.hpp:26
void setCorrection(uint16_t ch, int64_t correction)
Registers timestamp correction for a channel.
Definition: TimeAdjuster.hpp:40
int64_t getCorrection(uint16_t ch) const noexcept
Returns correction of the timestamp registered to a channel.
Definition: TimeAdjuster.hpp:59
virtual void process(std::vector< DetectorEvent > &input, std::vector< DetectorEvent > &output)
Consumes detector events and returns different detector events.
Definition: TimeAdjuster.hpp:66
Definition: mainpage.dox:6
static ContextBase & getActive() noexcept
Returns active Context.
Definition: ContextBase.hpp:80
Functor to compare DetectorEvent instances based on time field (as std::less<>).
Definition: DetectorEvent.hpp:69