Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
13 #include "JDAQ/JDAQRoot.hh"
14 #include "JIO/JSerialisable.hh"
15 
16 
17 namespace KM3NETDAQ {
18 
19  using JIO::JReader;
20  using JIO::JWriter;
21 
22 
23  /**
24  * Data structure for UTC time.
25  */
27  {
28  public:
29 
30  typedef unsigned int JUINT32_t; // preferably uint32_t
31 
32 
33  /**
34  * Default constructor.
35  */
37  UTC_seconds(0),
39  {}
40 
41 
42  /**
43  * Constructor.
44  *
45  * \param seconds seconds [s]
46  * \param cycles cycles [16 ns]
47  */
48  JDAQUTCExtended(const JUINT32_t seconds,
49  const JUINT32_t cycles):
50  UTC_seconds(seconds),
52  {}
53 
54 
55  /**
56  * Constructor.
57  *
58  * \param nanoseconds time [ns]
59  */
60  JDAQUTCExtended(const double nanoseconds)
61  {
62  setTimeNanoSecond(nanoseconds);
63  }
64 
65 
66  /**
67  * Virtual destructor.
68  */
69  virtual ~JDAQUTCExtended()
70  {}
71 
72 
73  /**
74  * Get time.
75  *
76  * \return time [s]
77  */
79  {
80  return UTC_seconds;
81  }
82 
83 
84  /**
85  * Get time.
86  *
87  * \return time [16 ns]
88  */
90  {
92  }
93 
94 
95  /**
96  * Get time (limited to 16 ns cycles).
97  *
98  * \return time [ns]
99  */
100  double getTimeNanoSecond() const
101  {
102  return UTC_seconds*1.0e9 + UTC_16nanosecondcycles*16.0;
103  }
104 
105 
106  /**
107  * Set time.
108  *
109  * \param utc_ns time [ns]
110  */
111  void setTimeNanoSecond(const double utc_ns)
112  {
113  UTC_seconds = (unsigned int) ( utc_ns / 1.0e9);
114  UTC_16nanosecondcycles = (unsigned int) ((utc_ns - UTC_seconds*1.0e9) / 16.0);
115  }
116 
117 
118  /**
119  * Read UTC time.
120  *
121  * \param in intput stream
122  * \param utc UTC extended time
123  * \return intput stream
124  */
125  friend inline std::istream& operator>>(std::istream& in, JDAQUTCExtended& utc)
126  {
127  in >> utc.UTC_seconds;
128  in.get();
129  in >> utc.UTC_16nanosecondcycles;
130 
131  return in;
132  }
133 
134 
135  /**
136  * Write UTC time.
137  *
138  * \param out output stream
139  * \param utc UTC extended time
140  * \return output stream
141  */
142  friend inline std::ostream& operator<<(std::ostream& out, const JDAQUTCExtended& utc)
143  {
144  using namespace std;
145 
146  const char c = out.fill();
147 
148  out << setw(10) << utc.getUTCseconds();
149  out << ':';
150  out << setw(10) << setfill('0') << utc.getUTC16nanosecondcycles() << setfill(c);
151 
152  return out;
153  }
154 
155 
156  /**
157  * Read UTC from input.
158  *
159  * \param in reader
160  * \param utc UTC
161  * \return reader
162  */
163  friend inline JReader& operator>>(JReader& in, JDAQUTCExtended& utc)
164  {
165  in >> utc.UTC_seconds;
166  in >> utc.UTC_16nanosecondcycles;
167 
168  return in;
169  }
170 
171 
172  /**
173  * Write UTC to output.
174  *
175  * \param out writer
176  * \param utc UTC
177  * \return writer
178  */
179  friend inline JWriter& operator<<(JWriter& out, const JDAQUTCExtended& utc)
180  {
181  out << utc.UTC_seconds;
182  out << utc.UTC_16nanosecondcycles;
183 
184  return out;
185  }
186 
187 
188  /**
189  * Get size of object.
190  *
191  * \return number of bytes
192  */
193  static int sizeOf()
194  {
195  return 2*sizeof(JUINT32_t);
196  }
197 
198 
200 
201 
202  protected:
205  };
206 
207 
208  /**
209  * Less than operator for UTC times.
210  *
211  * \param first UTC time
212  * \param second UTC time
213  * \result true if first UTC time earlier than second UTC time; else false
214  */
215  inline bool operator<(const JDAQUTCExtended& first, const JDAQUTCExtended& second)
216  {
217  if (first.getUTCseconds() == second.getUTCseconds())
218  return first.getUTC16nanosecondcycles() < second.getUTC16nanosecondcycles();
219  else
220  return first.getUTCseconds() < second.getUTCseconds();
221  }
222 
223 
224  /**
225  * Equal operator for UTC times.
226  *
227  * \param first UTC time
228  * \param second UTC time
229  * \result true if first UTC time equal second UTC time; else false
230  */
231  inline bool operator==(const JDAQUTCExtended& first, const JDAQUTCExtended& second)
232  {
233  return (first.getUTCseconds() == second.getUTCseconds() &&
235  }
236 
237 
238  /**
239  * Not equal operator for UTC times.
240  *
241  * \param first UTC time
242  * \param second UTC time
243  * \result true if first UTC time not equal second UTC time; else false
244  */
245  inline bool operator!=(const JDAQUTCExtended& first, const JDAQUTCExtended& second)
246  {
247  return !(first == second);
248  }
249 }
250 
251 #endif
bool operator==(const JDAQChronometer &first, const JDAQChronometer &second)
Equal operator for DAQ chronometers.
Interface for binary output.
friend std::istream & operator>>(std::istream &in, JDAQUTCExtended &utc)
Read UTC time.
JDAQUTCExtended(const double nanoseconds)
Constructor.
Data structure for UTC time.
ClassDef(JDAQUTCExtended, 1)
JUINT32_t getUTC16nanosecondcycles() const
Get time.
JDAQUTCExtended(const JUINT32_t seconds, const JUINT32_t cycles)
Constructor.
void setTimeNanoSecond(const double utc_ns)
Set time.
Interface for binary input.
JUINT32_t getUTCseconds() const
Get time.
JDAQUTCExtended()
Default constructor.
virtual ~JDAQUTCExtended()
Virtual destructor.
friend JWriter & operator<<(JWriter &out, const JDAQUTCExtended &utc)
Write UTC to output.
bool operator!=(const JDAQChronometer &first, const JDAQChronometer &second)
Not-equal operator for DAQ chronometers.
bool operator<(const JDAQHit &first, const JDAQHit &second)
Less than operator for DAQ hits.
Definition: JDAQHit.hh:178
static int sizeOf()
Get size of object.
friend std::ostream & operator<<(std::ostream &out, const JDAQUTCExtended &utc)
Write UTC time.
double getTimeNanoSecond() const
Get time (limited to 16 ns cycles).
friend JReader & operator>>(JReader &in, JDAQUTCExtended &utc)
Read UTC from input.