Timer.h
Go to the documentation of this file.
1 //File: Timer.h
2 //Brief: A CRT::Timer computes the current and/or elapsed time since its' construction. CRT timestamps have
3 // a UNIX timestamp that is not synchronized with the clock timestamp. So, a CRT::Timer uses the times
4 // that it is asked to convert to infer when the lower 32 bits of the timestamp have reset. This means
5 // that you will get less accurate time values (on the order of single seconds) if you don't "show"
6 // a CRT::Timer all of the timestamps in an event.
7 //Author: Andrew Olivier aolivier@ur.rochester.edu
8 
9 #ifndef CRT_TIMER_H
10 #define CRT_TIMER_H
11 
12 //c++ includes
13 #include <cstdint>
14 
15 namespace CRT
16 {
17  class Trigger;
18 
19  //Represent a CRT timestamp in two parts
20  struct Timestamp
21  {
22  //Create a Timestamp from the format it is saved in in a CRT::Trigger
23  Timestamp(const uint64_t timestamp)
24  {
25  lower = timestamp; //Let the compiler strip off the upper 32 bits
26  upper = timestamp >> 32; //Push the lower 32 bits off the end of the number, then let the compiler
27  //strip off the remaining 32 bits
28  }
29 
30  //Two parts of a CRT timestamp:
31  uint32_t lower; //Time in ns since last Sync
32  uint32_t upper; //UNIX timestamp in seconds
33  };
34 
35  class Timer
36  {
37  public:
38  //TODO: using statment to make sure timestamp type is whatever CRT::Trigger uses as a timestamp?
39  //Construct a CRT::Timer from the timestamp relative to which it will report elapsed time
40  Timer(const CRT::Trigger& trigger);
41  Timer(const uint64_t timestamp);
42 
43  //Get the time in seconds represented by a trigger or timestamp
44  double seconds(const CRT::Trigger& trigger);
45  double seconds(const uint64_t timestamp);
46 
47  //Get the elapsed time since the timestamp with which this Timer was constructed
48  double elapsed(const CRT::Trigger& trigger);
49  double elapsed(const uint64_t timestamp);
50 
51  //Get the elapsed time between the timestamp with which this Timer was constructed and the last timestamp it saw
52  double elapsed();
53 
54  private:
55  //State that Timer uses to estimate the real time of a CRT timestamp
56  uint32_t fLastUNIXSecond; //Last UNIX second seen by Timer
57  uint32_t fNsOfLastUNIXSecond; //Inferred ns since the UNIX timestamp last went to a new second
58  uint32_t fNsOfLastSync; //Inferred number of ns when the last time a sync signal was sent to the CRT boards and they
59  //reset their
60  uint32_t fPrevNs; //Previous ns component of time
61  const double fFirstTime; //Time of the timestamp with which this CRT::Timer was constructed
62 
63  //Turn a 64 bit timestamp into seconds
64  //TODO: Move this to the Timestamp struct below as a cast to double?
65  double GetSeconds(const uint64_t timestamp);
66  };
67 }
68 
69 #endif //CRT_TIMER_H
uint32_t fPrevNs
Definition: Timer.h:60
const double fFirstTime
Definition: Timer.h:61
uint32_t lower
Definition: Timer.h:31
Timestamp(const uint64_t timestamp)
Definition: Timer.h:23
uint32_t fNsOfLastSync
Definition: Timer.h:58
unsigned int uint32_t
Definition: stdint.h:126
unsigned __int64 uint64_t
Definition: stdint.h:136
uint32_t fLastUNIXSecond
Definition: Timer.h:56
uint32_t upper
Definition: Timer.h:32
uint32_t fNsOfLastUNIXSecond
Definition: Timer.h:57