97 auto const excess = value % period;
98 auto const base = value - excess;
99 return (excess < (period / T(2)))? base: base + period;
105 template <
typename Clock,
typename Unit>
125 template <
typename TimeInterval>
129 (std::chrono::duration_cast<Unit>(dt).count());
133 template <
typename Rep,
typename Period>
135 {
return toDuration(std::chrono::duration<Rep, Period>(1)); }
138 template <
typename TimePo
int>
140 {
return toDuration(t.time_since_epoch()); }
145 template <
typename Clock,
typename Duration>
162 using clock_t = Clock;
163 using system_clock_t = std::chrono::system_clock;
167 if (std::is_same<clock_t, system_clock_t>()) {
169 <<
"Using system clock for timestamp: no offset needed.";
173 auto clock_time = clock_t::now();
174 auto sys_clock_time = system_clock_t::now();
180 (timeFromEpoch(sys_clock_time) - timeFromEpoch(clock_time))
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." 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<
205 system_clock_period_t
208 constexpr
auto largest_period
209 = periodToDuration<typename clock_t::rep, largest_period_t>();
214 constexpr
unsigned int times = 10U;
216 for (
unsigned int i = 0; i < times; ++i) {
219 (timeFromEpoch(sys_clock_time) - timeFromEpoch(clock_time));
220 clock_time = clock_t::now();
221 sys_clock_time = system_clock_t::now();
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 <<
")" 243 template <
typename Clock,
typename Duration,
typename =
void>
248 template <
typename Clock,
typename Duration>
250 (Clock::period::num * Duration::period::den > Duration::period::num * Clock::period::den)
266 static constexpr intmax_t paddingfactor
274 {
return Base_t::read_clock() + flat(engine); }
278 std::uniform_int_distribution<duration_t>
flat;
299 <<
"Timestamp plugin: timestamp from local clock time in nanoseconds";
323 <<
"Generated time stamp: " << ts.
value() <<
" for event " << id;
std::ratio< ClockPeriod::num *ReqPeriod::den, ReqPeriod::num *ClockPeriod::den > PeriodRatio
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
auto discretize(T value, T period)
Returns the multiple of period closest to value.
Plugin to assign an empty event a time stamp from the clock.
static constexpr auto periodToDuration()
Returns the duration (duration_t) of a period type.
void insert(data_t value)
constexpr TimeValue_t value() const
Class reading a Clock and converting the value to a specific Unit.
static duration_t read_clock()
Reads and returns the current time the clock.
typename Duration::period ReqPeriod
GeneratedEventTimestamp(fhicl::ParameterSet const &pset)
Constructor: nothing specific.
std::uniform_int_distribution< duration_t > flat
art::TimeValue_t const fOffsetFromEpoch
Offset to be added to the chosen clock to get an absolute time.
#define DEFINE_ART_EMPTYEVENTTIMESTAMP_PLUGIN(klass)
static constexpr duration_t toDuration(TimeInterval dt)
Converts a std::chrono::duration into our duration metric.
std::uint64_t TimeValue_t
virtual art::Timestamp eventTimestamp(art::EventID const &id) override
Returns the time stamp for the specified event.
art::TimeValue_t duration_t
Type of the time duration as returned by this class.
art::TimeValue_t duration_t
Type of the time duration as returned by this class.
typename Clock::period ClockPeriod
MaybeLogger_< ELseverityLevel::ELsev_success, true > LogTrace
std::default_random_engine engine
static duration_t currentOffsetFromEpoch()
Event Generation using GENIE, cosmics or single particles.
static duration_t timeFromEpoch(TimePoint t)
Returns the time elapsed from the epoch to t.
nanosecond nanoseconds
Alias for common language habits.