EUDAQ
 All Classes Namespaces Files Functions Variables Pages
Mupix.hh
1 
8 #ifndef __MUPIX_HH_4GZYCGCM__
9 #define __MUPIX_HH_4GZYCGCM__
10 
11 #ifndef WIN32
12 #include <inttypes.h> /* uint32_t */
13 #endif
14 #include <vector>
15 
16 #include "eudaq/Logger.hh"
17 #include "eudaq/RawDataEvent.hh"
18 #include "eudaq/Utils.hh"
19 
20 namespace eudaq {
21  namespace mupix {
22 
29  inline uint8_t gray_decode(uint8_t graystamp) {
30  // Here we convert the (binary reflected) graycode to binary using
31  // the safe but necessarily optimal algorithm from wikipedia
32  uint8_t mask;
33  for (mask = graystamp >> 1; mask != 0; mask = mask >> 1) {
34  graystamp = graystamp ^ mask;
35  }
36  return graystamp;
37  }
38 
39  inline uint8_t gray_encode(uint8_t timestamp) {
40  // Here we convert binary to binary reflected graycode
41  return (timestamp >> 1) ^ timestamp;
42  }
43 
60  public:
61  Mupix4DataProxy() : _data(NULL) {}
62  Mupix4DataProxy(const uint8_t *data) : _data(data) {}
63 
64  static Mupix4DataProxy from_event(const RawDataEvent &e) {
65  if (e.NumBlocks() != 1) {
66  EUDAQ_WARN("event " + to_string(e.GetEventNumber()) +
67  ": invalid number of data blocks " +
68  to_string(e.NumBlocks()));
69  return Mupix4DataProxy();
70  }
71 
72  const std::vector<uint8_t> &block = e.GetBlock(0);
73 
74  if (block.size() < 9) {
75  EUDAQ_WARN("event " + to_string(e.GetEventNumber()) +
76  ": data block is too small");
77  return Mupix4DataProxy();
78  }
79 
80  Mupix4DataProxy proxy(block.data());
81  if (block.size() != proxy.size()) {
82  EUDAQ_WARN("event " + to_string(e.GetEventNumber()) +
83  ": inconsistent data size. " + "received " +
84  to_string(block.size()) + " " + "from header " +
85  to_string(proxy.size()) + " ");
86  return Mupix4DataProxy();
87  }
88 
89  return proxy;
90  }
91  static Mupix4DataProxy from_event(const Event &e) {
92  return from_event(dynamic_cast<const RawDataEvent &>(e));
93  }
94 
95  operator bool() const { return (_data != NULL); }
96 
97  uint32_t event_id() const {
98  return eudaq::getlittleendian<uint32_t>(&_data[0]);
99  }
100  uint16_t trigger_id() const {
101  return eudaq::getlittleendian<uint16_t>(&_data[4]);
102  }
103  uint8_t trigger_graystamp() const { return _data[6]; }
104  uint8_t trigger_timestamp() const {
105  return gray_decode(trigger_graystamp());
106  }
107  bool overflow() const { return static_cast<bool>(_data[7]); }
108 
109  uint8_t num_hits() const { return _data[8]; }
110  uint8_t hit_col(unsigned i) const { return _data[9 + i * 3 + 0]; }
111  uint8_t hit_row(unsigned i) const { return _data[9 + i * 3 + 1]; }
112  uint8_t hit_graystamp(unsigned i) const { return _data[9 + i * 3 + 2]; }
113  uint8_t hit_timestamp(unsigned i) const {
114  return gray_decode(hit_graystamp(i));
115  }
116 
117  unsigned size() const { return 9 + 3 * num_hits(); }
118 
119  private:
120  const uint8_t *_data;
121  };
122 
123  } // namespace mupix
124 } // namespace eudaq
125 
126 #endif // __MUPIX_HH_4GZYCGCM__
size_t NumBlocks() const
Return the number of data blocks in the RawDataEvent.
Definition: RawDataEvent.hh:80
Definition: Mupix.hh:59
std::string to_string(const T &x, int digits=0)
Definition: Utils.hh:54
Definition: RawDataEvent.hh:14
Definition: Event.hh:38
const data_t & GetBlock(size_t i) const
Definition: RawDataEvent.cc:34