Jpp
JDAQUTCExtended.hh
Go to the documentation of this file.
1 #ifndef __JDAQUTCEXTENDED__
2 #define __JDAQUTCEXTENDED__
3 
4 /**
5  * \author rbruijn
6  */
7 
8 //#include <cstdint> // only in C++0x, will give uint32_t
9 #include <istream>
10 #include <ostream>
11 #include <iomanip>
12 #include <limits>
13 
14 #include "JDAQ/JDAQRoot.hh"
15 #include "JIO/JSerialisable.hh"
16 
17 
18 namespace KM3NETDAQ {
19 
20  using JIO::JReader;
21  using JIO::JWriter;
22 
23 
24  /**
25  * Data structure for UTC time.
26  */
28  {
29  public:
30 
31  typedef unsigned int JUINT32_t; // preferably uint32_t
32 
33 
34  /**
35  * Default constructor.
36  */
38  UTC_seconds(0),
40  {}
41 
42 
43  /**
44  * Constructor.
45  *
46  * \param seconds seconds [s]
47  * \param cycles cycles [16 ns]
48  */
49  JDAQUTCExtended(const JUINT32_t seconds,
50  const JUINT32_t cycles):
51  UTC_seconds(seconds),
53  {}
54 
55 
56  /**
57  * Constructor.
58  *
59  * \param nanoseconds time [ns]
60  */
61  JDAQUTCExtended(const double nanoseconds)
62  {
63  setTimeNanoSecond(nanoseconds);
64  }
65 
66 
67  /**
68  * Virtual destructor.
69  */
70  virtual ~JDAQUTCExtended()
71  {}
72 
73 
74  /**
75  * Get time.
76  *
77  * \return time [s]
78  */
80  {
81  return (UTC_seconds & getMask());
82  }
83 
84 
85  /**
86  * Get time.
87  *
88  * \return time [16 ns]
89  */
91  {
93  }
94 
95 
96  /**
97  * Get time (limited to 16 ns cycles).
98  *
99  * \return time [ns]
100  */
101  double getTimeNanoSecond() const
102  {
103  return getUTCseconds() * 1.0e9 + getUTC16nanosecondcycles() * getTick();
104  }
105 
106 
107  /**
108  * Set time.
109  *
110  * \param utc_ns time [ns]
111  */
112  void setTimeNanoSecond(const double utc_ns)
113  {
114  UTC_seconds = (unsigned int) ( utc_ns / 1.0e9);
115  UTC_16nanosecondcycles = (unsigned int) ((utc_ns - UTC_seconds*1.0e9) / getTick());
116  }
117 
118 
119  /**
120  * Get minimum possible value.
121  *
122  * \return minimum possible value
123  */
125  {
126  return JDAQUTCExtended(0,0);
127  }
128 
129 
130  /**
131  * Get maximum possible value.
132  *
133  * \return maximum possible value
134  */
136  {
137  return JDAQUTCExtended(std::numeric_limits<JUINT32_t>::max(),
138  std::numeric_limits<JUINT32_t>::max());
139  }
140 
141 
142  /**
143  * Get mask for seconds data.
144  *
145  * \return mask
146  */
147  static JUINT32_t getMask()
148  {
149  return 0x7FFFFFFF;
150  }
151 
152 
153  /**
154  * Get number of nano-seconds per tick.
155  *
156  * \return time [ns]
157  */
158  static double getTick()
159  {
160  return 16.0;
161  }
162 
163 
164  /**
165  * Read UTC time.
166  *
167  * \param in intput stream
168  * \param utc UTC extended time
169  * \return intput stream
170  */
171  friend inline std::istream& operator>>(std::istream& in, JDAQUTCExtended& utc)
172  {
173  in >> utc.UTC_seconds;
174  in.get();
175  in >> utc.UTC_16nanosecondcycles;
176 
177  return in;
178  }
179 
180 
181  /**
182  * Write UTC time.
183  *
184  * \param out output stream
185  * \param utc UTC extended time
186  * \return output stream
187  */
188  friend inline std::ostream& operator<<(std::ostream& out, const JDAQUTCExtended& utc)
189  {
190  using namespace std;
191 
192  const char c = out.fill();
193 
194  out << setw(10) << utc.getUTCseconds();
195  out << ':';
196  out << setw(10) << setfill('0') << utc.getUTC16nanosecondcycles() << setfill(c);
197 
198  return out;
199  }
200 
201 
202  /**
203  * Read UTC from input.
204  *
205  * \param in reader
206  * \param utc UTC
207  * \return reader
208  */
209  friend inline JReader& operator>>(JReader& in, JDAQUTCExtended& utc)
210  {
211  in >> utc.UTC_seconds;
212  in >> utc.UTC_16nanosecondcycles;
213 
214  return in;
215  }
216 
217 
218  /**
219  * Write UTC to output.
220  *
221  * \param out writer
222  * \param utc UTC
223  * \return writer
224  */
225  friend inline JWriter& operator<<(JWriter& out, const JDAQUTCExtended& utc)
226  {
227  out << utc.UTC_seconds;
228  out << utc.UTC_16nanosecondcycles;
229 
230  return out;
231  }
232 
233 
234  /**
235  * Get size of object.
236  *
237  * \return number of bytes
238  */
239  static int sizeOf()
240  {
241  return 2*sizeof(JUINT32_t);
242  }
243 
244 
246 
247 
248  protected:
251  };
252 
253 
254  /**
255  * Less than operator for UTC times.
256  *
257  * \param first UTC time
258  * \param second UTC time
259  * \result true if first UTC time earlier than second UTC time; else false
260  */
261  inline bool operator<(const JDAQUTCExtended& first, const JDAQUTCExtended& second)
262  {
263  if (first.getUTCseconds() == second.getUTCseconds())
264  return first.getUTC16nanosecondcycles() < second.getUTC16nanosecondcycles();
265  else
266  return first.getUTCseconds() < second.getUTCseconds();
267  }
268 
269 
270  /**
271  * Equal operator for UTC times.
272  *
273  * \param first UTC time
274  * \param second UTC time
275  * \result true if first UTC time equal second UTC time; else false
276  */
277  inline bool operator==(const JDAQUTCExtended& first, const JDAQUTCExtended& second)
278  {
279  return (first.getUTCseconds() == second.getUTCseconds() &&
281  }
282 
283 
284  /**
285  * Not equal operator for UTC times.
286  *
287  * \param first UTC time
288  * \param second UTC time
289  * \result true if first UTC time not equal second UTC time; else false
290  */
291  inline bool operator!=(const JDAQUTCExtended& first, const JDAQUTCExtended& second)
292  {
293  return !(first == second);
294  }
295 }
296 
297 #endif
KM3NETDAQ::JDAQUTCExtended::setTimeNanoSecond
void setTimeNanoSecond(const double utc_ns)
Set time.
Definition: JDAQUTCExtended.hh:112
JIO::JReader
Interface for binary input.
Definition: JSerialisable.hh:62
KM3NETDAQ::JDAQUTCExtended::JDAQUTCExtended
JDAQUTCExtended(const JUINT32_t seconds, const JUINT32_t cycles)
Constructor.
Definition: JDAQUTCExtended.hh:49
KM3NETDAQ::operator<
bool operator<(const JDAQHit &first, const JDAQHit &second)
Less than operator for DAQ hits.
Definition: JDAQHit.hh:222
KM3NETDAQ::JDAQUTCExtended::JUINT32_t
unsigned int JUINT32_t
Definition: JDAQUTCExtended.hh:31
KM3NETDAQ::JDAQUTCExtended::getMask
static JUINT32_t getMask()
Get mask for seconds data.
Definition: JDAQUTCExtended.hh:147
KM3NETDAQ::JDAQUTCExtended
Data structure for UTC time.
Definition: JDAQUTCExtended.hh:27
KM3NETDAQ::JDAQUTCExtended::getUTCseconds
JUINT32_t getUTCseconds() const
Get time.
Definition: JDAQUTCExtended.hh:79
KM3NETDAQ::JDAQUTCExtended::UTC_16nanosecondcycles
JUINT32_t UTC_16nanosecondcycles
Definition: JDAQUTCExtended.hh:250
KM3NETDAQ::JDAQUTCExtended::UTC_seconds
JUINT32_t UTC_seconds
Definition: JDAQUTCExtended.hh:249
KM3NETDAQ::JDAQUTCExtended::~JDAQUTCExtended
virtual ~JDAQUTCExtended()
Virtual destructor.
Definition: JDAQUTCExtended.hh:70
KM3NETDAQ::JDAQUTCExtended::operator<<
friend JWriter & operator<<(JWriter &out, const JDAQUTCExtended &utc)
Write UTC to output.
Definition: JDAQUTCExtended.hh:225
KM3NETDAQ::JDAQUTCExtended::JDAQUTCExtended
JDAQUTCExtended()
Default constructor.
Definition: JDAQUTCExtended.hh:37
KM3NETDAQ::JDAQUTCExtended::sizeOf
static int sizeOf()
Get size of object.
Definition: JDAQUTCExtended.hh:239
JSerialisable.hh
KM3NETDAQ::JDAQUTCExtended::max
static JDAQUTCExtended max()
Get maximum possible value.
Definition: JDAQUTCExtended.hh:135
KM3NETDAQ::operator!=
bool operator!=(const JDAQChronometer &first, const JDAQChronometer &second)
Not-equal operator for DAQ chronometers.
Definition: JDAQChronometer.hh:303
KM3NETDAQ::JDAQUTCExtended::operator<<
friend std::ostream & operator<<(std::ostream &out, const JDAQUTCExtended &utc)
Write UTC time.
Definition: JDAQUTCExtended.hh:188
JIO::JWriter
Interface for binary output.
Definition: JSerialisable.hh:130
KM3NETDAQ::JDAQUTCExtended::getUTC16nanosecondcycles
JUINT32_t getUTC16nanosecondcycles() const
Get time.
Definition: JDAQUTCExtended.hh:90
KM3NETDAQ::JDAQUTCExtended::operator>>
friend JReader & operator>>(JReader &in, JDAQUTCExtended &utc)
Read UTC from input.
Definition: JDAQUTCExtended.hh:209
JDAQRoot.hh
KM3NETDAQ::operator==
bool operator==(const JDAQChronometer &first, const JDAQChronometer &second)
Equal operator for DAQ chronometers.
Definition: JDAQChronometer.hh:286
KM3NETDAQ::JDAQUTCExtended::operator>>
friend std::istream & operator>>(std::istream &in, JDAQUTCExtended &utc)
Read UTC time.
Definition: JDAQUTCExtended.hh:171
KM3NETDAQ::JDAQUTCExtended::min
static JDAQUTCExtended min()
Get minimum possible value.
Definition: JDAQUTCExtended.hh:124
KM3NETDAQ::JDAQUTCExtended::getTick
static double getTick()
Get number of nano-seconds per tick.
Definition: JDAQUTCExtended.hh:158
std
Definition: jaanetDictionary.h:36
KM3NETDAQ
KM3NeT DAQ data structures and auxiliaries.
Definition: DataQueue.cc:39
KM3NETDAQ::JDAQUTCExtended::getTimeNanoSecond
double getTimeNanoSecond() const
Get time (limited to 16 ns cycles).
Definition: JDAQUTCExtended.hh:101
KM3NETDAQ::JDAQUTCExtended::JDAQUTCExtended
JDAQUTCExtended(const double nanoseconds)
Constructor.
Definition: JDAQUTCExtended.hh:61
KM3NETDAQ::JDAQUTCExtended::ClassDef
ClassDef(JDAQUTCExtended, 1)