Public Member Functions | Private Member Functions | Private Attributes | List of all members
detinfo::ElecClock Class Reference

Class representing the time measured by an electronics clock. More...

#include <ElecClock.h>

Public Member Functions

 ElecClock (double const time, double const frame_period, double const frequency)
 Constructor: sets all values. More...
 
constexpr ElecClock WithTime (double const time) const noexcept
 
constexpr ElecClock WithTick (int const tick, int const frame=0) const noexcept
 
constexpr ElecClock AdvanceTimeBy (double const time) const noexcept
 
constexpr ElecClock AdvanceTicksBy (int const ticks) const noexcept
 
constexpr double Time () const noexcept
 Current time (as stored) in microseconds. More...
 
constexpr double Time (int const sample, int const frame) const noexcept
 Returns the absolute time of the start of the specified sample. More...
 
constexpr double Time (double const time) const noexcept
 Returns the discretized value of the specified time. More...
 
constexpr double Time (int const ticks) const noexcept
 Returns the absolute start time of the specified tick. More...
 
constexpr double Frequency () const
 Frequency in MHz. More...
 
constexpr double FramePeriod () const noexcept
 A single frame period in microseconds. More...
 
constexpr int Ticks () const noexcept
 Current clock tick (that is, the number of tick Time() falls in). More...
 
constexpr int Ticks (double const time) const noexcept
 Returns the number of tick the specified time falls in. More...
 
constexpr int Ticks (int const sample, int const frame) const noexcept
 Returns the number of tick the specified sample falls in. More...
 
constexpr int Sample () const noexcept
 Returns number of the sample containing the clock current time. More...
 
constexpr int Sample (double const time) const noexcept
 Returns the number of the sample containing the specified time. More...
 
constexpr int Sample (int const tick) const noexcept
 Returns the number of the sample containing the specified tick. More...
 
constexpr int Frame () const noexcept
 Returns the number of the frame containing the clock current time. More...
 
constexpr int Frame (double const time) const noexcept
 Returns the number of the frame containing the specified time. More...
 
constexpr int Frame (int const tick) const noexcept
 Returns the number of the frame containing the specified tick. More...
 
constexpr unsigned int FrameTicks () const noexcept
 Number ticks in a frame. More...
 
constexpr double TickPeriod () const noexcept
 A single tick period in microseconds. More...
 
constexpr bool operator< (const ElecClock &rhs) const noexcept
 
constexpr bool operator> (const ElecClock &rhs) const noexcept
 
constexpr bool operator<= (const ElecClock &rhs) const noexcept
 
constexpr bool operator>= (const ElecClock &rhs) const noexcept
 

Private Member Functions

constexpr ElecClock (double const time, double const frame_period, double const frequency, std::nothrow_t) noexcept
 

Private Attributes

double fTime {}
 Time in microseconds. More...
 
double fFramePeriod {kTIME_MAX}
 Frame period in microseconds. More...
 
double fFrequency {1e9}
 Clock speed in MHz. More...
 

Detailed Description

Class representing the time measured by an electronics clock.

This class represents the status of a running electronics clock. As such, it has:

Note that this object is actually able to manage any (continuous) value of time, and the concepts of frame and sample are not used to store the clock state, which is instead defined by its current time.

All these quantities are stored in real time units as opposed to clock tick counts. The nominal units for all times and frequencies are microseconds and megahertz, respectively, but ElecClock will give consistent results as long as the units of frame period and time are the same, and reciprocal to the frequency unit.

The clock can update its time directly (SetTime()) or through operators.

The clock started at time 0, with the sample 0 of the frame 0 (that is also tick 0). This implies that all times and ticks returned by the clock implicitly have the same reference as the input time specified by the constructors or by the last call to SetTime().

Some usage examples:

// get a clock with a period of 500 ns (2 MHz) and a frame of 1.6 ms
detinfo::ElecClock clock(0.0, 1600.0, 2.0);
std::cout << "New clock:"
<< "\n current time: " << clock.Time() // 0.0 us
<< "\n samples per frame: " << clock.FrameTicks() // 3200
clock.SetTime(1, 20); // sets the time to sample #20 of frame 1
std::cout << "Time set to sample #20 of frame #1:"
<< "\n current time: " << clock.Time() // 1610.0 us
<< "\n current tick: " << clock.Tick() // 3220
clock += 3.7; // add 3.7 us to the current time
std::cout << "Added 3.7 microseconds:"
<< "\n current time: " << clock.Time() // 1613.7 us
<< "\n current tick: " << clock.Tick() // 3227
<< "\n discrete time: " << clock.Time(Time()) // 1613.5 us
clock += 3; // add 3 more ticks (1.5 us) to the current time
std::cout << "Added 3 ticks:"
<< "\n current time: " << clock.Time() // 1615.2 us
<< "\n current tick: " << clock.Tick() // 3230
<< "\n discrete time: " << clock.Time(Time()) // 1615.0 us
Note
Most methods and operators have different behaviour according to whether their argument is of type double, in which case it is interpreted as a time, or int, where it is interpreted as a clock tick. Be especially careful when utilizing data types which are neither int not double, because a conversion to one of them will occur, and you need to be in control of which one.

Definition at line 91 of file ElecClock.h.

Constructor & Destructor Documentation

detinfo::ElecClock::ElecClock ( double const  time,
double const  frame_period,
double const  frequency 
)
inline

Constructor: sets all values.

Parameters
timestarting time of the clock [µs]
frame_periodperiod of the clock [µs]
frequencyclock frequency [MHz]

Definition at line 100 of file ElecClock.h.

101  : ElecClock{time, frame_period, frequency, std::nothrow}
102  {
103  if (fFrequency <= 0)
104  throw detinfo::DetectorClocksException("Only positive frequency allowed.");
105  }
double fFrequency
Clock speed in MHz.
Definition: ElecClock.h:390
ElecClock(double const time, double const frame_period, double const frequency)
Constructor: sets all values.
Definition: ElecClock.h:100
constexpr detinfo::ElecClock::ElecClock ( double const  time,
double const  frame_period,
double const  frequency,
std::nothrow_t   
)
inlineprivatenoexcept

Definition at line 381 of file ElecClock.h.

385  : fTime(time), fFramePeriod(frame_period), fFrequency(frequency)
386  {}
double fFrequency
Clock speed in MHz.
Definition: ElecClock.h:390
double fTime
Time in microseconds.
Definition: ElecClock.h:388
double fFramePeriod
Frame period in microseconds.
Definition: ElecClock.h:389

Member Function Documentation

constexpr ElecClock detinfo::ElecClock::AdvanceTicksBy ( int const  ticks) const
inlinenoexcept

Definition at line 126 of file ElecClock.h.

127  {
128  return {fTime + Time(ticks), fFramePeriod, fFrequency, std::nothrow};
129  }
tick ticks
Alias for common language habits.
Definition: electronics.h:78
double fFrequency
Clock speed in MHz.
Definition: ElecClock.h:390
constexpr double Time() const noexcept
Current time (as stored) in microseconds.
Definition: ElecClock.h:137
double fTime
Time in microseconds.
Definition: ElecClock.h:388
double fFramePeriod
Frame period in microseconds.
Definition: ElecClock.h:389
constexpr ElecClock detinfo::ElecClock::AdvanceTimeBy ( double const  time) const
inlinenoexcept

Definition at line 120 of file ElecClock.h.

121  {
122  return {fTime + time, fFramePeriod, fFrequency, std::nothrow};
123  }
double fFrequency
Clock speed in MHz.
Definition: ElecClock.h:390
double fTime
Time in microseconds.
Definition: ElecClock.h:388
double fFramePeriod
Frame period in microseconds.
Definition: ElecClock.h:389
constexpr int detinfo::ElecClock::Frame ( ) const
inlinenoexcept

Returns the number of the frame containing the clock current time.

See also
Frame(double)

The returned value is the number of the frame which the current clock time falls in.

Definition at line 304 of file ElecClock.h.

305  {
306  return Frame(fTime);
307  }
constexpr int Frame() const noexcept
Returns the number of the frame containing the clock current time.
Definition: ElecClock.h:304
double fTime
Time in microseconds.
Definition: ElecClock.h:388
constexpr int detinfo::ElecClock::Frame ( double const  time) const
inlinenoexcept

Returns the number of the frame containing the specified time.

Parameters
timea clock time [µs]
Returns
number of the frame containing the specified time
See also
Sample(double)

The returned value is the number of the frame which the specified time falls in.

The result is not related to the current time of the clock.

Definition at line 321 of file ElecClock.h.

322  {
323  return static_cast<int>(time / fFramePeriod);
324  }
double fFramePeriod
Frame period in microseconds.
Definition: ElecClock.h:389
constexpr int detinfo::ElecClock::Frame ( int const  tick) const
inlinenoexcept

Returns the number of the frame containing the specified tick.

Parameters
ticka clock tick number
Returns
number of the frame containing the specified tick
See also
Frame(int)

The returned value is the number of the frame the specified tick belongs to.

The result is not related to the current time of the clock.

Definition at line 338 of file ElecClock.h.

339  {
340  return (tick / static_cast<int>(FrameTicks()));
341  }
constexpr unsigned int FrameTicks() const noexcept
Number ticks in a frame.
Definition: ElecClock.h:345
tick_as<> tick
Tick number, represented by std::ptrdiff_t.
Definition: electronics.h:75
constexpr double detinfo::ElecClock::FramePeriod ( ) const
inlinenoexcept

A single frame period in microseconds.

Definition at line 198 of file ElecClock.h.

199  {
200  return fFramePeriod;
201  }
double fFramePeriod
Frame period in microseconds.
Definition: ElecClock.h:389
constexpr unsigned int detinfo::ElecClock::FrameTicks ( ) const
inlinenoexcept

Number ticks in a frame.

Definition at line 345 of file ElecClock.h.

346  {
347  return static_cast<unsigned int>(fFramePeriod * fFrequency);
348  }
double fFrequency
Clock speed in MHz.
Definition: ElecClock.h:390
double fFramePeriod
Frame period in microseconds.
Definition: ElecClock.h:389
constexpr double detinfo::ElecClock::Frequency ( ) const
inline

Frequency in MHz.

Definition at line 191 of file ElecClock.h.

192  {
193  return fFrequency;
194  }
double fFrequency
Clock speed in MHz.
Definition: ElecClock.h:390
constexpr bool detinfo::ElecClock::operator< ( const ElecClock rhs) const
inlinenoexcept

Definition at line 360 of file ElecClock.h.

361  {
362  return fTime < rhs.Time();
363  }
double fTime
Time in microseconds.
Definition: ElecClock.h:388
constexpr bool detinfo::ElecClock::operator<= ( const ElecClock rhs) const
inlinenoexcept

Definition at line 370 of file ElecClock.h.

371  {
372  return fTime <= rhs.Time();
373  }
double fTime
Time in microseconds.
Definition: ElecClock.h:388
constexpr bool detinfo::ElecClock::operator> ( const ElecClock rhs) const
inlinenoexcept

Definition at line 365 of file ElecClock.h.

366  {
367  return fTime > rhs.Time();
368  }
double fTime
Time in microseconds.
Definition: ElecClock.h:388
constexpr bool detinfo::ElecClock::operator>= ( const ElecClock rhs) const
inlinenoexcept

Definition at line 375 of file ElecClock.h.

376  {
377  return fTime >= rhs.Time();
378  }
double fTime
Time in microseconds.
Definition: ElecClock.h:388
constexpr int detinfo::ElecClock::Sample ( ) const
inlinenoexcept

Returns number of the sample containing the clock current time.

See also
Sample(double)

The returned value is the number of the sample within a frame, which the current clock time falls in. The number of the frame is not returned. To univocally define the sample, the number of the frame must also be obtained, e.g. via Frame().

Definition at line 253 of file ElecClock.h.

254  {
255  return Sample(fTime);
256  }
constexpr int Sample() const noexcept
Returns number of the sample containing the clock current time.
Definition: ElecClock.h:253
double fTime
Time in microseconds.
Definition: ElecClock.h:388
constexpr int detinfo::ElecClock::Sample ( double const  time) const
inlinenoexcept

Returns the number of the sample containing the specified time.

Parameters
timea clock time [µs]
Returns
number of the sample containing the specified time
See also
Frame(double)

The returned value is the number of the sample within a frame, which the specified time falls in. The number of the frame is not returned. To univocally define the sample, the number of the frame must also be obtained, e.g. via Frame(double).

The result is not related to the current time of the clock.

Definition at line 272 of file ElecClock.h.

273  {
274  return static_cast<int>((time - Frame(time) * fFramePeriod) * fFrequency);
275  }
constexpr int Frame() const noexcept
Returns the number of the frame containing the clock current time.
Definition: ElecClock.h:304
double fFrequency
Clock speed in MHz.
Definition: ElecClock.h:390
double fFramePeriod
Frame period in microseconds.
Definition: ElecClock.h:389
constexpr int detinfo::ElecClock::Sample ( int const  tick) const
inlinenoexcept

Returns the number of the sample containing the specified tick.

Parameters
ticka clock tick number
Returns
number of the sample containing the specified tick
See also
Frame(int)

The returned value is the number of the sample within a frame, of the specified tick. The number of the frame is not returned. To univocally define the sample, the number of the frame must also be obtained, e.g. via Frame(int).

The result is not related to the current time of the clock.

Definition at line 291 of file ElecClock.h.

292  {
293  return (tick % static_cast<int>(FrameTicks()));
294  }
constexpr unsigned int FrameTicks() const noexcept
Number ticks in a frame.
Definition: ElecClock.h:345
tick_as<> tick
Tick number, represented by std::ptrdiff_t.
Definition: electronics.h:75
constexpr double detinfo::ElecClock::TickPeriod ( ) const
inlinenoexcept

A single tick period in microseconds.

Definition at line 352 of file ElecClock.h.

353  {
354  return 1. / fFrequency;
355  }
double fFrequency
Clock speed in MHz.
Definition: ElecClock.h:390
constexpr int detinfo::ElecClock::Ticks ( ) const
inlinenoexcept

Current clock tick (that is, the number of tick Time() falls in).

Definition at line 205 of file ElecClock.h.

206  {
207  return Ticks(fTime);
208  }
constexpr int Ticks() const noexcept
Current clock tick (that is, the number of tick Time() falls in).
Definition: ElecClock.h:205
double fTime
Time in microseconds.
Definition: ElecClock.h:388
constexpr int detinfo::ElecClock::Ticks ( double const  time) const
inlinenoexcept

Returns the number of tick the specified time falls in.

Parameters
timetime to be converted in ticks [µs]
Returns
the number of the tick containing the specified time

The tick number 0 starts at time 0.0 µs.

Note
The returned value (number of tick) can wrap if the real number of the tick is beyond the range of the returned data type (int).

Definition at line 221 of file ElecClock.h.

222  {
223  return static_cast<int>(time * fFrequency);
224  }
double fFrequency
Clock speed in MHz.
Definition: ElecClock.h:390
constexpr int detinfo::ElecClock::Ticks ( int const  sample,
int const  frame 
) const
inlinenoexcept

Returns the number of tick the specified sample falls in.

Parameters
samplenumber of sample in the specified frame
framenumber of frame the specified sample is in
Returns
the number of the tick containing the specified sample

The sample 0 of frame 0 is the tick number 0.

Note
The returned value (number of tick) can wrap if the real number of the tick is beyond the range of the returned data type (int).

Definition at line 238 of file ElecClock.h.

239  {
240  return sample + frame * FrameTicks();
241  }
constexpr unsigned int FrameTicks() const noexcept
Number ticks in a frame.
Definition: ElecClock.h:345
constexpr double detinfo::ElecClock::Time ( ) const
inlinenoexcept

Current time (as stored) in microseconds.

Note that this is different than Time(Time()), which is discretized.

Definition at line 137 of file ElecClock.h.

138  {
139  return fTime;
140  }
double fTime
Time in microseconds.
Definition: ElecClock.h:388
constexpr double detinfo::ElecClock::Time ( int const  sample,
int const  frame 
) const
inlinenoexcept

Returns the absolute time of the start of the specified sample.

Parameters
samplesample number within the specified frame
framenumber of the frame the specified sample is in
Returns
time [us] corresponding to the start of the specified sample

The sample number is not checked to be actually within the specified frame.

The returned time is not related to the current time of the clock.

Definition at line 154 of file ElecClock.h.

155  {
156  return (sample / fFrequency + frame * fFramePeriod);
157  }
double fFrequency
Clock speed in MHz.
Definition: ElecClock.h:390
double fFramePeriod
Frame period in microseconds.
Definition: ElecClock.h:389
constexpr double detinfo::ElecClock::Time ( double const  time) const
inlinenoexcept

Returns the discretized value of the specified time.

Parameters
timea clock time [µs]
Returns
discretized time [µs] (as a floating point number)

The returned time is the start time of the sample time falls in.

It is not related to the current time of the clock.

Definition at line 169 of file ElecClock.h.

170  {
171  return Time(Sample(time), Frame(time));
172  }
constexpr int Frame() const noexcept
Returns the number of the frame containing the clock current time.
Definition: ElecClock.h:304
constexpr int Sample() const noexcept
Returns number of the sample containing the clock current time.
Definition: ElecClock.h:253
constexpr double Time() const noexcept
Current time (as stored) in microseconds.
Definition: ElecClock.h:137
constexpr double detinfo::ElecClock::Time ( int const  ticks) const
inlinenoexcept

Returns the absolute start time of the specified tick.

Parameters
ticksa clock time [µs]
Returns
discretized time [µs] (as a floating point number)

The returned time is the start time of the tick which time falls in.

It is not related to the current time of the clock.

Definition at line 184 of file ElecClock.h.

185  {
186  return ticks / fFrequency;
187  }
tick ticks
Alias for common language habits.
Definition: electronics.h:78
double fFrequency
Clock speed in MHz.
Definition: ElecClock.h:390
constexpr ElecClock detinfo::ElecClock::WithTick ( int const  tick,
int const  frame = 0 
) const
inlinenoexcept

Definition at line 114 of file ElecClock.h.

115  {
116  return {Time(tick, frame), fFramePeriod, fFrequency, std::nothrow};
117  }
double fFrequency
Clock speed in MHz.
Definition: ElecClock.h:390
tick_as<> tick
Tick number, represented by std::ptrdiff_t.
Definition: electronics.h:75
constexpr double Time() const noexcept
Current time (as stored) in microseconds.
Definition: ElecClock.h:137
double fFramePeriod
Frame period in microseconds.
Definition: ElecClock.h:389
constexpr ElecClock detinfo::ElecClock::WithTime ( double const  time) const
inlinenoexcept

Definition at line 108 of file ElecClock.h.

109  {
110  return {time, fFramePeriod, fFrequency, std::nothrow};
111  }
double fFrequency
Clock speed in MHz.
Definition: ElecClock.h:390
double fFramePeriod
Frame period in microseconds.
Definition: ElecClock.h:389

Member Data Documentation

double detinfo::ElecClock::fFramePeriod {kTIME_MAX}
private

Frame period in microseconds.

Definition at line 389 of file ElecClock.h.

double detinfo::ElecClock::fFrequency {1e9}
private

Clock speed in MHz.

Definition at line 390 of file ElecClock.h.

double detinfo::ElecClock::fTime {}
private

Time in microseconds.

Definition at line 388 of file ElecClock.h.


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