LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
evgen::details::TimeInUnitsBase< Clock, Unit > Class Template Reference

Class reading a Clock and converting the value to a specific Unit. More...

Public Types

using duration_t = art::TimeValue_t
 Type of the time duration as returned by this class. More...
 

Public Member Functions

duration_t operator() ()
 Reads and returns the current time the clock. More...
 

Static Public Member Functions

static duration_t read_clock ()
 Reads and returns the current time the clock. More...
 
static duration_t currentOffsetFromEpoch ()
 

Static Protected Member Functions

template<typename TimeInterval >
static constexpr duration_t toDuration (TimeInterval dt)
 Converts a std::chrono::duration into our duration metric. More...
 
template<typename Rep , typename Period >
static constexpr auto periodToDuration ()
 Returns the duration (duration_t) of a period type. More...
 
template<typename TimePoint >
static duration_t timeFromEpoch (TimePoint t)
 Returns the time elapsed from the epoch to t. More...
 

Detailed Description

template<typename Clock, typename Unit>
class evgen::details::TimeInUnitsBase< Clock, Unit >

Class reading a Clock and converting the value to a specific Unit.

Definition at line 106 of file GeneratedEventTimestamp_plugin.cc.

Member Typedef Documentation

template<typename Clock, typename Unit>
using evgen::details::TimeInUnitsBase< Clock, Unit >::duration_t = art::TimeValue_t

Type of the time duration as returned by this class.

Definition at line 110 of file GeneratedEventTimestamp_plugin.cc.

Member Function Documentation

template<typename Clock , typename Duration >
auto evgen::details::TimeInUnitsBase< Clock, Duration >::currentOffsetFromEpoch ( )
static

Computes an approximation of the offset of the current time from the epoch.

Definition at line 146 of file GeneratedEventTimestamp_plugin.cc.

References evgen::details::Average< T >::average(), den, evgen::details::discretize(), evgen::details::Average< T >::insert(), and MF_LOG_DEBUG.

148  {
149  /*
150  * The plan is to compare the `Clock` we use with the system clock, which
151  * is guaranteed by the C++20 standard to refer to a well defined absolute
152  * time point (the UNIX epoch, January 1, 1970).
153  * Chances are that the resolution of the system clock is not as good as
154  * the one of `Clock`. If the difference between the two clocks is less
155  * than a few seconds, we attribute the difference to chance and we don't
156  * correct for it.
157  *
158  * Otherwise, the same time (almost!) is taken from the two clocks, and
159  * the difference in `Duration` units is used as a correction.
160  *
161  */
162  using clock_t = Clock;
163  using system_clock_t = std::chrono::system_clock;
164  using namespace std::chrono_literals;
165 
166  // no point in doing the exercise if we are already using the system clock
167  if (std::is_same<clock_t, system_clock_t>()) {
168  MF_LOG_DEBUG("GeneratedEventTimestamp")
169  << "Using system clock for timestamp: no offset needed.";
170  return static_cast<duration_t>(0);
171  }
172 
173  auto clock_time = clock_t::now();
174  auto sys_clock_time = system_clock_t::now();
175 
176  // if the system clock is close to our clock, of if it is ahead of it,
177  // use no offset (the latter stems from the consideration that that the
178  // two clocks are equivalent although they suffer from some jitter)
179  if (
180  (timeFromEpoch(sys_clock_time) - timeFromEpoch(clock_time))
181  < toDuration(5s)
182  )
183  {
184  MF_LOG_DEBUG("GeneratedEventTimestamp")
185  << "Offset with system clock is small ("
186  << (timeFromEpoch(sys_clock_time) - timeFromEpoch(clock_time))
187  << ", " << timeFromEpoch(sys_clock_time)
188  << " vs. " << timeFromEpoch(clock_time)
189  << "): no offset needed."
190  ;
191  return static_cast<duration_t>(0);
192  }
193 
194  //
195  // pick the largest of the resolutions for the comparison
196  //
197  using clock_period_t = typename clock_t::period;
198  using system_clock_period_t = system_clock_t::period;
199  using largest_period_t = std::conditional_t<
200  (
201  clock_period_t::num * system_clock_period_t::den
202  > system_clock_period_t::num * clock_period_t::den
203  ),
204  clock_period_t,
205  system_clock_period_t
206  >;
207  // this is the period expressed in the Duration unit
208  constexpr auto largest_period
209  = periodToDuration<typename clock_t::rep, largest_period_t>();
210 
211  //
212  // compare and round
213  //
214  constexpr unsigned int times = 10U; // average 10 samples
215  Average<duration_t> offset;
216  for (unsigned int i = 0; i < times; ++i) {
217 
218  offset.insert
219  (timeFromEpoch(sys_clock_time) - timeFromEpoch(clock_time));
220  clock_time = clock_t::now();
221  sys_clock_time = system_clock_t::now();
222 
223  } // for
224 
225  MF_LOG_DEBUG("GeneratedEventTimestamp")
226  << "System clock period: "
227  << periodToDuration<typename clock_t::rep, system_clock_period_t>()
228  << "\nUser clock period: "
229  << periodToDuration<typename clock_t::rep, clock_period_t>()
230  << "\nOffset: " << offset.average()
231  << " (rounded to: " << largest_period << ")"
232  ;
233 
234  // round off the offset with one "largest period"
235  return discretize(offset.average(), largest_period);
236 
237  } // TimeInUnitsBase<>::currentOffsetFromEpoch()
Float_t den
Definition: plot.C:35
auto discretize(T value, T period)
Returns the multiple of period closest to value.
static constexpr duration_t toDuration(TimeInterval dt)
Converts a std::chrono::duration into our duration metric.
#define MF_LOG_DEBUG(id)
art::TimeValue_t duration_t
Type of the time duration as returned by this class.
static duration_t timeFromEpoch(TimePoint t)
Returns the time elapsed from the epoch to t.
template<typename Clock, typename Unit>
duration_t evgen::details::TimeInUnitsBase< Clock, Unit >::operator() ( )
inline

Reads and returns the current time the clock.

Definition at line 113 of file GeneratedEventTimestamp_plugin.cc.

113 { return read_clock(); }
static duration_t read_clock()
Reads and returns the current time the clock.
template<typename Clock, typename Unit>
template<typename Rep , typename Period >
static constexpr auto evgen::details::TimeInUnitsBase< Clock, Unit >::periodToDuration ( )
inlinestaticprotected

Returns the duration (duration_t) of a period type.

Definition at line 134 of file GeneratedEventTimestamp_plugin.cc.

135  { return toDuration(std::chrono::duration<Rep, Period>(1)); }
static constexpr duration_t toDuration(TimeInterval dt)
Converts a std::chrono::duration into our duration metric.
template<typename Clock, typename Unit>
static duration_t evgen::details::TimeInUnitsBase< Clock, Unit >::read_clock ( )
inlinestatic

Reads and returns the current time the clock.

Definition at line 116 of file GeneratedEventTimestamp_plugin.cc.

116 { return timeFromEpoch(Clock::now()); }
static duration_t timeFromEpoch(TimePoint t)
Returns the time elapsed from the epoch to t.
template<typename Clock, typename Unit>
template<typename TimePoint >
static duration_t evgen::details::TimeInUnitsBase< Clock, Unit >::timeFromEpoch ( TimePoint  t)
inlinestaticprotected

Returns the time elapsed from the epoch to t.

Definition at line 139 of file GeneratedEventTimestamp_plugin.cc.

140  { return toDuration(t.time_since_epoch()); }
static constexpr duration_t toDuration(TimeInterval dt)
Converts a std::chrono::duration into our duration metric.
template<typename Clock, typename Unit>
template<typename TimeInterval >
static constexpr duration_t evgen::details::TimeInUnitsBase< Clock, Unit >::toDuration ( TimeInterval  dt)
inlinestaticprotected

Converts a std::chrono::duration into our duration metric.

Definition at line 126 of file GeneratedEventTimestamp_plugin.cc.

127  {
128  return static_cast<duration_t>
129  (std::chrono::duration_cast<Unit>(dt).count());
130  }
art::TimeValue_t duration_t
Type of the time duration as returned by this class.

The documentation for this class was generated from the following file: