LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
ElecClock.h
Go to the documentation of this file.
1 
14 #ifndef ElecClock_H
15 #define ElecClock_H
16 
17 #include "ClockConstants.h"
19 
20 namespace detinfo {
91  class ElecClock {
92  public:
100  ElecClock(double const time, double const frame_period, double const frequency)
101  : ElecClock{time, frame_period, frequency, std::nothrow}
102  {
103  if (fFrequency <= 0)
104  throw detinfo::DetectorClocksException("Only positive frequency allowed.");
105  }
106 
107  constexpr ElecClock WithTime(double const time) const noexcept
108  {
109  return {time, fFramePeriod, fFrequency, std::nothrow};
110  }
111 
112  constexpr ElecClock WithTick(int const tick, int const frame = 0) const noexcept
113  {
114  return {Time(tick, frame), fFramePeriod, fFrequency, std::nothrow};
115  }
116 
117  constexpr ElecClock AdvanceTimeBy(double const time) const noexcept
118  {
119  return {fTime + time, fFramePeriod, fFrequency, std::nothrow};
120  }
121 
122  constexpr ElecClock AdvanceTicksBy(int const ticks) const noexcept
123  {
124  return {fTime + Time(ticks), fFramePeriod, fFrequency, std::nothrow};
125  }
126 
132  constexpr double Time() const noexcept { return fTime; }
133 
145  constexpr double Time(int const sample, int const frame) const noexcept
146  {
147  return (sample / fFrequency + frame * fFramePeriod);
148  }
149 
159  constexpr double Time(double const time) const noexcept
160  {
161  return Time(Sample(time), Frame(time));
162  }
163 
173  constexpr double Time(int const ticks) const noexcept { return ticks / fFrequency; }
174 
176  constexpr double Frequency() const { return fFrequency; }
177 
179  constexpr double FramePeriod() const noexcept { return fFramePeriod; }
180 
182  constexpr int Ticks() const noexcept { return Ticks(fTime); }
183 
194  constexpr int Ticks(double const time) const noexcept
195  {
196  return static_cast<int>(time * fFrequency);
197  }
198 
210  constexpr int Ticks(int const sample, int const frame) const noexcept
211  {
212  return sample + frame * FrameTicks();
213  }
214 
224  constexpr int Sample() const noexcept { return Sample(fTime); }
225 
239  constexpr int Sample(double const time) const noexcept
240  {
241  return static_cast<int>((time - Frame(time) * fFramePeriod) * fFrequency);
242  }
243 
257  constexpr int Sample(int const tick) const noexcept
258  {
259  return (tick % static_cast<int>(FrameTicks()));
260  }
261 
269  constexpr int Frame() const noexcept { return Frame(fTime); }
270 
282  constexpr int Frame(double const time) const noexcept
283  {
284  return static_cast<int>(time / fFramePeriod);
285  }
286 
298  constexpr int Frame(int const tick) const noexcept
299  {
300  return (tick / static_cast<int>(FrameTicks()));
301  }
302 
304  constexpr unsigned int FrameTicks() const noexcept
305  {
306  return static_cast<unsigned int>(fFramePeriod * fFrequency);
307  }
308 
310  constexpr double TickPeriod() const noexcept { return 1. / fFrequency; }
311 
312  //-- comparators --//
313 
314  constexpr bool operator<(const ElecClock& rhs) const noexcept { return fTime < rhs.Time(); }
315  constexpr bool operator>(const ElecClock& rhs) const noexcept { return fTime > rhs.Time(); }
316  constexpr bool operator<=(const ElecClock& rhs) const noexcept { return fTime <= rhs.Time(); }
317  constexpr bool operator>=(const ElecClock& rhs) const noexcept { return fTime >= rhs.Time(); }
318 
319  private:
320  constexpr ElecClock(double const time,
321  double const frame_period,
322  double const frequency,
323  std::nothrow_t) noexcept
324  : fTime(time), fFramePeriod(frame_period), fFrequency(frequency)
325  {}
326 
327  double fTime{};
329  double fFrequency{1e9};
330 
331  }; // class ElecClock
332 
333 }
334 #endif
335  // end of doxygen group
constexpr ElecClock WithTime(double const time) const noexcept
Definition: ElecClock.h:107
constexpr int Frame(int const tick) const noexcept
Returns the number of the frame containing the specified tick.
Definition: ElecClock.h:298
constexpr unsigned int FrameTicks() const noexcept
Number ticks in a frame.
Definition: ElecClock.h:304
constexpr double kTIME_MAX
Maximum time in microseconds.
Definition: ClockConstants.h:9
constexpr int Ticks(double const time) const noexcept
Returns the number of tick the specified time falls in.
Definition: ElecClock.h:194
constexpr int Sample(double const time) const noexcept
Returns the number of the sample containing the specified time.
Definition: ElecClock.h:239
constexpr double FramePeriod() const noexcept
A single frame period in microseconds.
Definition: ElecClock.h:179
constexpr int Ticks(int const sample, int const frame) const noexcept
Returns the number of tick the specified sample falls in.
Definition: ElecClock.h:210
constexpr ElecClock WithTick(int const tick, int const frame=0) const noexcept
Definition: ElecClock.h:112
constexpr ElecClock AdvanceTimeBy(double const time) const noexcept
Definition: ElecClock.h:117
Class def header for exception classes in DetectorClocks data provider.
constexpr int Frame(double const time) const noexcept
Returns the number of the frame containing the specified time.
Definition: ElecClock.h:282
constexpr int Frame() const noexcept
Returns the number of the frame containing the clock current time.
Definition: ElecClock.h:269
tick ticks
Alias for common language habits.
Definition: electronics.h:76
constexpr int Ticks() const noexcept
Current clock tick (that is, the number of tick Time() falls in).
Definition: ElecClock.h:182
constexpr bool operator>(const ElecClock &rhs) const noexcept
Definition: ElecClock.h:315
double fFrequency
Clock speed in MHz.
Definition: ElecClock.h:329
constexpr double TickPeriod() const noexcept
A single tick period in microseconds.
Definition: ElecClock.h:310
constexpr int Sample(int const tick) const noexcept
Returns the number of the sample containing the specified tick.
Definition: ElecClock.h:257
constexpr double Time(double const time) const noexcept
Returns the discretized value of the specified time.
Definition: ElecClock.h:159
constexpr ElecClock AdvanceTicksBy(int const ticks) const noexcept
Definition: ElecClock.h:122
constexpr int Sample() const noexcept
Returns number of the sample containing the clock current time.
Definition: ElecClock.h:224
tick_as<> tick
Tick number, represented by std::ptrdiff_t.
Definition: electronics.h:73
constexpr ElecClock(double const time, double const frame_period, double const frequency, std::nothrow_t) noexcept
Definition: ElecClock.h:320
constexpr double Time() const noexcept
Current time (as stored) in microseconds.
Definition: ElecClock.h:132
General LArSoft Utilities.
constexpr bool operator<=(const ElecClock &rhs) const noexcept
Definition: ElecClock.h:316
double fTime
Time in microseconds.
Definition: ElecClock.h:327
constexpr bool operator<(const ElecClock &rhs) const noexcept
Definition: ElecClock.h:314
ElecClock(double const time, double const frame_period, double const frequency)
Constructor: sets all values.
Definition: ElecClock.h:100
constexpr double Frequency() const
Frequency in MHz.
Definition: ElecClock.h:176
constexpr double Time(int const sample, int const frame) const noexcept
Returns the absolute time of the start of the specified sample.
Definition: ElecClock.h:145
constexpr bool operator>=(const ElecClock &rhs) const noexcept
Definition: ElecClock.h:317
Class representing the time measured by an electronics clock.
Definition: ElecClock.h:91
constexpr double Time(int const ticks) const noexcept
Returns the absolute start time of the specified tick.
Definition: ElecClock.h:173
double fFramePeriod
Frame period in microseconds.
Definition: ElecClock.h:328