LArSoft  v06_85_00
Liquid Argon Software toolkit - http://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 110 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 114 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 150 of file GeneratedEventTimestamp_plugin.cc.

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

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

117 { 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 138 of file GeneratedEventTimestamp_plugin.cc.

139  { 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 120 of file GeneratedEventTimestamp_plugin.cc.

120 { 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 143 of file GeneratedEventTimestamp_plugin.cc.

144  { 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 130 of file GeneratedEventTimestamp_plugin.cc.

131  {
132  return static_cast<duration_t>
133  (std::chrono::duration_cast<Unit>(dt).count());
134  }
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: