TimingFragment.hh
Go to the documentation of this file.
1 #ifndef dune_artdaq_Overlays_TimingFragment_hh
2 #define dune_artdaq_Overlays_TimingFragment_hh
3 
4 #include "artdaq-core/Data/Fragment.hh"
5 //#include "artdaq-core/Data/features.hh"
6 #include "cetlib_except/exception.h"
7 
8 #include <ostream>
9 #include <vector>
10 
11 // Implementation of "TimingFragment".
12 //
13 // From Dave Newbold mail May 2017
14 // The data words (uint32_t) given by the hardware are
15 // d(0) <= X"aa000600"; -- DAQ word 0 (cookie)
16 // d(1) <= X"0000000" & scmd; -- DAQ word 1 (scmd = trigger type, tcmd = the reserved zero bits)
17 // d(2) <= tstamp(31 downto 0); -- DAQ word 2 (tstampl = low bits of timestamp)
18 // d(3) <= tstamp(63 downto 32); -- DAQ word 3 (tstamph = high bits of time stamp)
19 // d(4) <= evtctr; -- DAQ word 4 (evtctr = event counter)
20 // d(5) <= X"000000000"; -- Dummy checksum, not implemented yet (cksum)
21 //
22 // These are stored in the fragment exactly as received and are manipulated
23 // to be readable by the getters in this class.
24 //
25 
26 namespace dune {
27  class TimingFragment;
28 
29  enum class TimingCommand {
30  // From https://twiki.cern.ch/twiki/bin/view/CENF/TimingSystemAdvancedOp retrieved on 2018-10-02
31  // The 'sync' bus has the following commands at the moment:
32  TimeSync = 0x0,
33  Echo = 0x1,
34  SpillStart = 0x2,
35  SpillStop = 0x3,
36  RunStart = 0x4,
37  RunStop = 0x5,
38  WibCalib = 0x6,
39  SSPCalib = 0x7,
40  FakeTrig0 = 0x8,
41  FakeTrig1 = 0x9,
42  FakeTrig2 = 0xa,
43  FakeTrig3 = 0xb,
44  BeamTrig = 0xc,
45  NoBeamTrig = 0xd,
46  ExtFakeTrig = 0xe
47  };
48 
49  // Let the "<<" operator dump the TimingFragment's data to stdout
50  std::ostream & operator << (std::ostream &, TimingFragment const &);
51 }
52 
54 
55  public:
56 
57  struct Metadata {
58  typedef uint32_t data_t;
59 
60  Metadata(data_t v) : fragment_version(v) {}
61  // "8 bits ought to be enough for anyone"
62  data_t fragment_version : 8;
63  // Give a name to the rest of the bits in case we ever want them for something else
64  data_t unused : 24;
65 
66  static size_t const size_words = 1ul; // Units of Metadata::data_t
67  };
68 
69  // Constructor. This class keeps a copy of a pointer to the fragment.
70  // Basically that is how it works.
71  public:
72  TimingFragment(artdaq::Fragment const & f ) : artdaq_Fragment_(f)
73  {}
74 
75 
76  // The following structure is overlayed onto the data in the
77  // fragment, starting at the beginning. The spill timestamps are
78  // added by the board reader, and don't exist in the object read
79  // directly from the timing board.
80  //
81  // *******************************************************
82  // * If you change anything here, update *
83  // * TimingFragment::VERSION below! *
84  // *******************************************************
85  struct Body {
86  uint32_t cookie : 32;
87  uint32_t scmd : 4;
88  uint32_t tcmd : 28;
89  uint32_t tstampl : 32;
90  uint32_t tstamph : 32;
91  uint32_t evtctr : 32;
92  uint32_t cksum : 32;
99  static size_t const size = 12ul; // Units of header uint32_t
100  };
101  // Update this version number if you update anything in the class!
102  static constexpr uint32_t VERSION = 3;
103 
104  // Here are the getters
105  uint32_t get_cookie() const { return body_()->cookie; }
106  uint32_t get_scmd() const { return body_()->scmd; }
107  uint32_t get_tcmd() const { return body_()->tcmd; }
108  uint32_t get_tstampl() const { return body_()->tstampl; }
109  uint32_t get_tstamph() const { return body_()->tstamph; }
110  uint32_t get_evtctr() const { return body_()->evtctr; }
111  uint32_t get_cksum() const { return body_()->cksum; }
112 
113  uint64_t get_tstamp() const {
114  return make_tstamp64(body_()->tstampl, body_()->tstamph);
115  }
116 
117  uint64_t get_last_runstart_timestamp() const {
118  return make_tstamp64(body_()->last_runstart_tstampl,
119  body_()->last_runstart_tstamph);
120  }
121  uint64_t get_last_spillstart_timestamp() const {
122  return make_tstamp64(body_()->last_spillstart_tstampl,
123  body_()->last_spillstart_tstamph);
124  }
125  uint64_t get_last_spillend_timestamp() const {
126  return make_tstamp64(body_()->last_spillend_tstampl,
127  body_()->last_spillend_tstamph);
128  }
129 
130  static size_t size() { return Body::size; /* body_()->size; */}
131  // This returns the constant size of the block (cureently 6 uint32_ts)
132 
133 protected:
134  // This function allows the getter classes below to be simple.
135  // It is modelled after header_() in ToyFragment.hh
136  // header_() in ToyFragment.hh is protected as well.
137  Body const * body_() const {
138  return reinterpret_cast<TimingFragment::Body const *>( artdaq_Fragment_.dataBeginBytes());
139  }
140 
141  uint64_t make_tstamp64(uint32_t tstampl, uint32_t tstamph) const {
142  uint64_t l = tstampl;
143  uint64_t h = tstamph;
144  return (l | (h<<32));
145  }
146  // This asks the compiler to check if we change te format that we didn't forget to cheange the size as well
147  // However if we do change the format, we must figure out how to make this class backward compatible.
148  static_assert (sizeof (Body) == Body::size * sizeof (uint32_t), "TimingFragment::Data block size changed");
149 
150 private:
151  artdaq::Fragment const & artdaq_Fragment_;
152 };
153 
154 #endif /* dune_artdaq_Overlays_TimingFragment_hh */
uint64_t get_last_spillend_timestamp() const
uint32_t get_tstampl() const
std::ostream & operator<<(std::ostream &out, CTBFragment const &f)
Definition: CTBFragment.cc:6
uint64_t get_last_runstart_timestamp() const
uint32_t get_tcmd() const
uint64_t make_tstamp64(uint32_t tstampl, uint32_t tstamph) const
static QStrList * l
Definition: config.cpp:1044
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
uint32_t get_cookie() const
TimingFragment(artdaq::Fragment const &f)
uint32_t get_cksum() const
artdaq::Fragment const & artdaq_Fragment_
uint64_t get_last_spillstart_timestamp() const
Body const * body_() const
uint64_t get_tstamp() const
uint32_t get_tstamph() const
uint32_t get_scmd() const
uint32_t get_evtctr() const
static size_t size()