ElecClock.h
Go to the documentation of this file.
1 /**
2  * \file ElecClock.hh
3  *
4  * \ingroup DetectorClocksService
5  *
6  * \brief Class def header for a class ElecClock
7  *
8  * @author kterao
9  */
10 
11 /** \addtogroup TimeService
12 
13  @{*/
14 #ifndef ElecClock_H
15 #define ElecClock_H
16 
17 #include "DetectorInfo/ClockConstants.h"
18 #include "DetectorInfo/DetectorClocksException.h"
19 #include <iostream>
20 
21 namespace gar {
22  namespace detinfo {
23  /**
24  \class ElecClock
25  Electronics clock class which holds double precision time in nanosecond,
26  frequency in Hz, and frame period in seconds.
27  */
28  class ElecClock{
29 
30  public:
31 
32  /// Default constructor
33  ElecClock(double time = 0,
34  double frame_period = kTIME_MAX,
35  double frequency = 1e9)
36  : fTime(time)
37  , fFramePeriod(frame_period)
38  , fFrequency(frequency)
39  {
40  if( fFrequency <= 0 ) throw detinfo::DetectorClocksException("Negative frequency is prohibited!");
41  }
42 
43  /// Default destructor
45 
46  protected:
47 
48  //-- Attribute variables --//
49 
50  double fTime; ///< Time in nano-second
51  double fFramePeriod; ///< Frame period in micro-second
52  double fFrequency; ///< Clock speed in MHz
53 
54  public:
55 
56  //-- Setters --//
57 
58  void SetTime(double time) { fTime = time; }
59 
60  void SetTime(int sample,
61  int frame)
62  { fTime = Time(sample, frame); }
63 
64  void SetTime(unsigned int sample,
65  unsigned int frame)
66  { SetTime(int(sample),int(frame)); }
67 
68  void SetTime(int ticks)
69  { fTime = Time(ticks,0); }
70 
71  void SetTime(unsigned int ticks)
72  { SetTime(int(ticks)); }
73 
74  //-- Getters --//
75 
76  /// Time (stored) in nano-second
77  double Time() const { return fTime; }
78 
79  /// Given sample & frame number in *this* clock, returns double precision time [ns]
80  double Time(int sample, int frame) const { return (sample * 1.e3 / fFrequency + frame * fFramePeriod); }
81 
82  /// Given time [ns] w.r.t. electronics clock counting, return discretized time in double precision
83  double Time(double time) const { return Time(Sample(time),Frame(time)); }
84 
85  /// Given time in ticks w.r.t. electronics clock counting, return discretized time in double precision
86  double Time(int ticks) const {return ticks * 1.e3 / fFrequency; }
87 
88  /// Frequency in MHz
89  double Frequency() const { return fFrequency; }
90 
91  /// A single frame period in micro-second
92  double FramePeriod() const { return fFramePeriod; }
93 
94  /// # of Ticks
95  int Ticks() const { return Ticks(fTime); }
96 
97  /// Given time [ns] w.r.t. electronics clock T0, return # of ticks
98  int Ticks(double time) const { return (int)(time * 1.e-3 * fFrequency); }
99 
100  /// Given sample & frame returns # ticks
101  int Ticks(int sample, int frame) const { return sample + frame * FrameTicks(); }
102 
103  /// Sample number
104  int Sample() const { return Sample(fTime); }
105 
106  /// Given time [ns] w.r.t. electronics clock T0, return sample number
107  int Sample(double time) const { return (int)((time - Frame(time) * fFramePeriod) * 1.e-3 * fFrequency); }
108 
109  /// Given ticks w.r.t. electronics clock T0, return sample number
110  int Sample(int tick) const { return (tick % (int)(FrameTicks())); }
111 
112  /// Frame number
113  int Frame() const { return Frame(fTime); }
114 
115  /// Given time [ns] w.r.t. electronics clock T0, return frame number
116  int Frame(double time) const { return (int)(time / fFramePeriod); }
117 
118  /// Given ticks w.r.t. electronics clock T0, return frame number
119  int Frame(int tick) const { return (tick / (int)(FrameTicks())); }
120 
121  /// Number ticks in a frame
122  unsigned int FrameTicks() const { return (int)(fFramePeriod * 1.e-3 * fFrequency);}
123 
124  /// A single tick period in nano-second, frequency is in MHz
125  double TickPeriod() const { return 1.e3 / fFrequency; }
126 
127  public:
128 
129  //-- operators --//
130  // Remove these for now as they do not appear to be used.
131  // If they are used again they will have to be updated so that the units
132  // in the calculations are consistently ns
133 // ElecClock& operator++() { fTime += 1./fFrequency; return *this;}
134 // ElecClock operator++(int) {ElecClock tmp(*this); operator++(); return tmp;}
135 // ElecClock& operator--() { fTime -= 1./fFrequency; return *this;}
136 // ElecClock operator--(int) {ElecClock tmp(*this); operator--(); return tmp;}
137 // ElecClock& operator+=(const double &rhs) { fTime += rhs; return *this;}
138 // ElecClock& operator-=(const double &rhs) { fTime -= rhs; return *this;}
139 // ElecClock& operator+=(const float &rhs) { fTime += (double)rhs; return *this;}
140 // ElecClock& operator-=(const float &rhs) { fTime -= (double)rhs; return *this;}
141 // ElecClock& operator+=(const int &rhs) { fTime += Time(rhs); return *this;}
142 // ElecClock& operator-=(const int &rhs) { fTime -= Time(rhs); return *this;}
143 // ElecClock& operator+=(const unsigned int &rhs) { fTime += Time((int)rhs); return *this;}
144 // ElecClock& operator-=(const unsigned int &rhs) { fTime -= Time((int)rhs); return *this;}
145 // ElecClock& operator+=(const ElecClock& rhs) { fTime += rhs.Time(); return *this;}
146 // ElecClock& operator-=(const ElecClock& rhs) { fTime -= rhs.Time(); return *this;}
147 
148  inline ElecClock operator+(const ElecClock& rhs)
149  { return ElecClock(fTime + rhs.Time(),fFramePeriod,fFrequency); }
150 
151  inline ElecClock operator-(const ElecClock& rhs)
152  { return ElecClock(fTime - rhs.Time(),fFramePeriod,fFrequency); }
153 
154  inline bool operator< (const ElecClock& rhs) const { return fTime < rhs.Time(); }
155  inline bool operator> (const ElecClock& rhs) const { return fTime > rhs.Time(); }
156  inline bool operator<= (const ElecClock& rhs) const { return fTime <= rhs.Time(); }
157  inline bool operator>= (const ElecClock& rhs) const { return fTime >= rhs.Time(); }
158 
159  };
160 
161  }
162 } // gar
163 
164 #endif
165 /** @} */ // end of doxygen group
166 
int Frame(double time) const
Given time [ns] w.r.t. electronics clock T0, return frame number.
Definition: ElecClock.h:116
double Time(int sample, int frame) const
Given sample & frame number in this clock, returns double precision time [ns].
Definition: ElecClock.h:80
int Sample(int tick) const
Given ticks w.r.t. electronics clock T0, return sample number.
Definition: ElecClock.h:110
int Ticks(double time) const
Given time [ns] w.r.t. electronics clock T0, return # of ticks.
Definition: ElecClock.h:98
unsigned int FrameTicks() const
Number ticks in a frame.
Definition: ElecClock.h:122
~ElecClock()
Default destructor.
Definition: ElecClock.h:44
void SetTime(unsigned int sample, unsigned int frame)
Definition: ElecClock.h:64
int Frame() const
Frame number.
Definition: ElecClock.h:113
int Sample() const
Sample number.
Definition: ElecClock.h:104
tick ticks
Alias for common language habits.
Definition: electronics.h:78
double fTime
Time in nano-second.
Definition: ElecClock.h:44
double TickPeriod() const
A single tick period in nano-second, frequency is in MHz.
Definition: ElecClock.h:125
void SetTime(unsigned int ticks)
Definition: ElecClock.h:71
double FramePeriod() const
A single frame period in micro-second.
Definition: ElecClock.h:92
double fFramePeriod
Frame period in micro-second.
Definition: ElecClock.h:51
const double e
bool operator<=(const ElecClock &rhs) const
Definition: ElecClock.h:156
int Frame(int tick) const
Given ticks w.r.t. electronics clock T0, return frame number.
Definition: ElecClock.h:119
double Time(double time) const
Given time [ns] w.r.t. electronics clock counting, return discretized time in double precision...
Definition: ElecClock.h:83
ElecClock operator-(const ElecClock &rhs)
Definition: ElecClock.h:151
bool operator>=(const ElecClock &rhs) const
Definition: ElecClock.h:157
tick_as<> tick
Tick number, represented by std::ptrdiff_t.
Definition: electronics.h:75
ElecClock operator+(const ElecClock &rhs)
Definition: ElecClock.h:148
General LArSoft Utilities.
double Time(int ticks) const
Given time in ticks w.r.t. electronics clock counting, return discretized time in double precision...
Definition: ElecClock.h:86
double fFrequency
Clock speed in MHz.
Definition: ElecClock.h:52
General GArSoft Utilities.
void SetTime(int ticks)
Definition: ElecClock.h:68
int Ticks(int sample, int frame) const
Given sample & frame returns # ticks.
Definition: ElecClock.h:101
int Sample(double time) const
Given time [ns] w.r.t. electronics clock T0, return sample number.
Definition: ElecClock.h:107
bool operator<(const ElecClock &rhs) const
Definition: ElecClock.h:154
int Ticks() const
of Ticks
Definition: ElecClock.h:95
const double kTIME_MAX
Maximum time in nano-second.
double Frequency() const
Frequency in MHz.
Definition: ElecClock.h:89
ElecClock(double time=0, double frame_period=kTIME_MAX, double frequency=1e9)
Default constructor.
Definition: ElecClock.h:33
void SetTime(double time)
Definition: ElecClock.h:58
bool operator>(const ElecClock &rhs) const
Definition: ElecClock.h:155
void SetTime(int sample, int frame)
Definition: ElecClock.h:60
double Time() const
Time (stored) in nano-second.
Definition: ElecClock.h:77