Pottu
TimeStamper.hpp
Go to the documentation of this file.
1 
7 #ifndef H_TIMESTAMPER
8 #define H_TIMESTAMPER
9 
10 #include "dataitem.hpp"
11 #include "DetectorEvent.hpp"
12 #include "ContextBase.hpp"
13 #include "StageRawToStampedRaw.hpp"
14 
15 #include <deque>
16 #include <vector>
17 #include <type_traits>
18 
19 #include <iostream>
20 
21 
22 namespace pottu {
23 
32  public:
33 
34  TimeStamper()
35  : _ctxh( ContextBase::getActive().createHandle("TimeStamper") )
36  {}
37 
45  //TO BE REMOVED: template <class ContInput, class ContOutput>
46  void process( const std::vector<dataitem_t> &input, std::vector<dataitem_ts_t> &output ) {
47  /* TO BE REMOVED:
48  static_assert( std::is_same<typename ContInput::value_type,dataitem_t>::value,
49  "TimeStamper requires dataitem_t as a input datapacket value_type" );
50  static_assert( std::is_same<typename ContOutput::value_type,dataitem_ts_t>::value,
51  "TimeStamper requires dataitem_ts_t as a output datapacket value_type" );
52  */
53  auto proci = _ctxh->processInstance();
54 
55  output.clear();
56 
57  for( const dataitem_t &di : input ) {
58 
59  if( di.getType() == itemtype_e::info
60  && ( di.info.getCode() == infocode_e::sync100ts
61  || di.info.getCode() == infocode_e::extendedts ) ) {
62 
63  // We have full time stamp!
64  int64_t tsHigh = di.info.info;
65  if( tsHigh == _tsHighPrev || tsHigh == _tsHighPrev+1 ) {
66 
67  if( _tsHighQueue-_tsHighPrev < 2 ) {
68  // Not more than one lowts overflow occurred during the fill of the queue
69 
70  if( output.size() && _itemqueue.size() && output.back().ts > _itemqueue.front().ts ) {
71  _ctxh->logError( "HAUKI" );
72  }
73 
74  std::move( _itemqueue.begin(), _itemqueue.end(), std::back_inserter(output) );
75  // Dont need to erase since the _itemqueue.clear() is soon called.
76  } else {
77  _ctxh->logWarning( "Timeorder error in low time bits." );
78  }
79 
80  } else {
81  _ctxh->logWarning( fmt::format( "Jump in high bits of timestamp: {} -> {}, dt = {} s.",
82  _tsHighPrev, tsHigh, (tsHigh-_tsHighPrev)*(1<<28ULL)*1e-8 ) );
83  }
84  // Always clear the queue.
85  _itemqueue.clear();
86 
87  // Always push this event to the output directly
88  int64_t ts = ( tsHigh << 28ULL | di.info.timestamp );
89 
90  output.push_back( {di, ts} );
91 
92  // Queue "starts" with this time stamp although
93  // this item is not put there.
94  _tsPrevIntoQueue = ts;
95  _tsHighPrev = tsHigh;
96  _tsHighQueue = tsHigh;
97 
98  } else if( di.getType() == itemtype_e::tracedata ) {
99 
100  // We dont have lowest time bits. Using time of
101  // the previous one.
102  _itemqueue.push_back( {di,_tsPrevIntoQueue} );
103 
104  } else {
105 
106  // We have lowest time bits. Using here "info" but
107  // tracehead and data contains the timestamp in
108  // same bits.
109  int64_t ts = ( _tsHighQueue << 28ULL | di.info.timestamp );
110  if( ts < _tsPrevIntoQueue ) {
111  //_ctxh->logDebug( "lowts wrapped around" );
112  ++_tsHighQueue;
113  ts = ( _tsHighQueue << 28ULL | di.info.timestamp );
114  }
115  _itemqueue.push_back( {di,ts} );
116  _tsPrevIntoQueue = ts;
117 
118  }
119 
120 
121  }
122 
123 
124  }
125 
126 
127  private:
128 
129  ContextHandle *_ctxh;
130 
131  std::deque<dataitem_ts_t> _itemqueue;
132 
133  int64_t _tsHighPrev{0};
134  int64_t _tsHighQueue{0};
135 
136  int64_t _tsPrevIntoQueue{0};
137 
138  };
139 
140 
141 }
142 
143 
144 #endif
145 
146 
Handle to context used by specific class.
Definition: ContextBase.hpp:188
Abstract baseclass for all raw data processing stages.
Definition: StageRawToStampedRaw.hpp:21
Class to assign full time stamps to raw events.
Definition: TimeStamper.hpp:31
void process(const std::vector< dataitem_t > &input, std::vector< dataitem_ts_t > &output)
Assigns full time stamps to raw events.
Definition: TimeStamper.hpp:46
Defines the raw low level dataitems used in tdr datastreams and files.
Definition: mainpage.dox:6
static ContextBase & getActive() noexcept
Returns active Context.
Definition: ContextBase.hpp:80
Raw dataitem in tdr binary data.
Definition: dataitem.hpp:343