pxar
 All Classes Namespaces Functions Variables Typedefs Friends
datatypes.h
1 #ifndef PXAR_TYPES_H
2 #define PXAR_TYPES_H
3 
7 #include "pxardllexport.h"
8 
10 #if ((defined WIN32) && (defined __CINT__))
11 typedef int int32_t;
12 typedef short int int16_t;
13 typedef unsigned int uint32_t;
14 typedef unsigned short int uint16_t;
15 typedef unsigned char uint8_t;
16 #else
17 #include <stdint.h>
18 #endif
19 
20 #include <iostream>
21 #include <vector>
22 #include <map>
23 #include <limits>
24 #include <cmath>
25 
26 namespace pxar {
27 
30  class DLLEXPORT pixel {
31  public:
32 
35  pixel() : _roc_id(0), _column(0), _row(0), _mean(0), _variance(0) {}
36 
39  pixel(uint8_t roc_id, uint8_t column, uint8_t row, double value) : _roc_id(roc_id), _column(column), _row(row), _variance(0) { setValue(value); }
40 
43  pixel(uint32_t rawdata, uint8_t rocid, bool invertAddress = false) : _roc_id(rocid) { decodeRaw(rawdata,invertAddress); }
44 
47  uint8_t roc() const { return _roc_id; };
48 
51  void setRoc(uint8_t roc) { _roc_id = roc; };
52 
55  uint8_t column() const { return _column; };
56 
59  void setColumn(uint8_t column) { _column = column; };
60 
63  uint8_t row() const { return _row; };
64 
67  void setRow(uint8_t row) { _row = row; };
68 
71  double variance() { return expandFloat(_variance); };
72 
75  void setVariance(double var) { _variance = compactFloat(var); };
76 
79  double value() {
80  return static_cast<double>(_mean);
81  };
82 
85  void setValue(double val) {
86  _mean = static_cast<int16_t>(val);
87  };
88 
91  bool operator == (const pixel& px) {
92  return ((px.roc() == _roc_id )
93  && (px.column() == _column)
94  && (px.row() == _row));
95  }
96 
99  bool operator < (const pixel& px) const {
100  if(_roc_id == px.roc()) {
101  if(_column == px.column()) {
102  return _row < px.row();
103  }
104  return _column < px.column();
105  }
106  return _roc_id < px.roc();
107  }
108 
109  private:
113  uint8_t _roc_id;
114 
117  uint8_t _column;
118 
121  uint8_t _row;
122 
126  int16_t _mean;
127 
131  uint16_t _variance;
132 
139  void decodeRaw(uint32_t raw, bool invert);
140 
144  uint16_t compactFloat(double input) {
145  return static_cast<uint16_t>(round(input*std::numeric_limits<uint16_t>::max()));
146  }
147 
151  double expandFloat(uint16_t input) {
152  return static_cast<double>(input)/std::numeric_limits<uint16_t>::max();
153  }
154 
157  friend std::ostream & operator<<(std::ostream &out, pixel& px) {
158  out << "ROC " << static_cast<int>(px.roc())
159  << " [" << static_cast<int>(px.column()) << "," << static_cast<int>(px.row())
160  << "," << static_cast<double>(px.value()) << "]";
161  return out;
162  }
163  };
164 
167  class DLLEXPORT Event {
168  public:
169  Event() : header(0), trailer(0), pixels() {}
170  void Clear() { header = 0; trailer = 0; pixels.clear();}
171  uint16_t header;
172  uint16_t trailer;
173  std::vector<pixel> pixels;
174  private:
177  friend std::ostream & operator<<(std::ostream &out, Event& evt) {
178  out << "====== " << std::hex << static_cast<uint16_t>(evt.header) << std::dec << " ====== ";
179  for (std::vector<pixel>::iterator it = evt.pixels.begin(); it != evt.pixels.end(); ++it)
180  out << (*it) << " ";
181  return out;
182  }
183  };
184 
185 
190  class DLLEXPORT rawEvent {
191  public:
192  rawEvent() : data(), flags(0) {}
193  void SetStartError() { flags |= 1; }
194  void SetEndError() { flags |= 2; }
195  void SetOverflow() { flags |= 4; }
196  void ResetStartError() { flags &= static_cast<unsigned int>(~1); }
197  void ResetEndError() { flags &= static_cast<unsigned int>(~2); }
198  void ResetOverflow() { flags &= static_cast<unsigned int>(~4); }
199  void Clear() { flags = 0; data.clear(); }
200  bool IsStartError() { return (flags & 1) != 0; }
201  bool IsEndError() { return (flags & 2) != 0; }
202  bool IsOverflow() { return (flags & 4) != 0; }
203 
204  size_t GetSize() { return data.size(); }
205  void Add(uint16_t value) { data.push_back(value); }
206  uint16_t operator[](size_t index) { return data.at(index); }
207 
208  std::vector<uint16_t> data;
209 
210  private:
211  /*
212  bit 0 = misaligned start
213  bit 1 = no end detected
214  bit 2 = overflow
215  */
216  unsigned int flags;
217 
220  friend std::ostream & operator<<(std::ostream &out, rawEvent& record) {
221  out << "====== " << std::hex << static_cast<uint16_t>(record.flags) << std::dec << " ====== ";
222  for (std::vector<uint16_t>::iterator it = record.data.begin(); it != record.data.end(); ++it)
223  out << std::hex << (*it) << std::dec << " ";
224  return out;
225  }
226  };
227 
232  class DLLEXPORT pixelConfig {
233  public:
234  pixelConfig() :
235  _column(0), _row(0),
236  _trim(15), _mask(true), _enable(false) {}
237  pixelConfig(uint8_t column, uint8_t row, uint8_t trim, bool mask = true, bool enable = false) :
238  _column(column), _row(row), _trim(trim),
239  _mask(mask), _enable(enable) {}
240  uint8_t column() const { return _column; }
241  void setColumn(uint8_t column) { _column = column; }
242  uint8_t row() const { return _row; }
243  void setRow(uint8_t row) { _row = row; }
244  uint8_t roc() const { return _roc_id; }
245  void setRoc(uint8_t roc) { _roc_id = roc; }
246  uint8_t trim() const { return _trim; }
247  void setTrim(uint8_t trim) { _trim = trim; }
248  bool mask() const { return _mask; }
249  void setMask(bool mask) { _mask = mask; }
250  bool enable() const { return _enable; }
251  void setEnable(bool enable) { _enable = enable; }
252  private:
253  uint8_t _column;
254  uint8_t _row;
255  uint8_t _roc_id;
256  uint8_t _trim;
257  bool _mask;
258  bool _enable;
259  };
260 
266  class DLLEXPORT rocConfig {
267  public:
268  rocConfig() : pixels(), dacs(), type(0), _enable(true) {}
269  std::vector< pixelConfig > pixels;
270  std::map< uint8_t,uint8_t > dacs;
271  uint8_t type;
272  uint8_t i2c_address;
273  bool enable() const { return _enable; }
274  void setEnable(bool enable) { _enable = enable; }
275  private:
276  bool _enable;
277  };
278 
283  class DLLEXPORT tbmConfig {
284  public:
285  tbmConfig() : dacs(), type(0), enable(true) {}
286  std::map< uint8_t,uint8_t > dacs;
287  uint8_t type;
288  bool enable;
289  };
290 
293  class DLLEXPORT statistics {
296  friend class dtbEventDecoder;
297 
298  public:
299  statistics() :
300  m_errors_event_start(0),
301  m_errors_event_stop(0),
302  m_errors_event_overflow(0),
303  m_errors_event_invalid_words(0),
304  m_errors_event_invalid_xor(0),
305  m_errors_tbm_header(0),
306  m_errors_tbm_trailer(0),
307  m_errors_tbm_eventid_mismatch(0),
308  m_errors_roc_missing(0),
309  m_errors_roc_readback(0),
310  m_errors_pixel_address(0),
311  m_errors_pixel_pulseheight(0),
312  m_errors_pixel_buffer_corrupt(0)
313  {};
314  // Print all statistics to stdout:
315  void dump();
316  friend statistics& operator+=(statistics &lhs, const statistics &rhs);
317 
318  uint32_t info_words_read() {return m_info_words_read; }
319  uint32_t info_pixels_valid() {return m_info_pixels_valid; }
320 
321  uint32_t errors() {
322  return (errors_pixel() + errors_tbm() + errors_roc() + errors_event());
323  };
324  uint32_t errors_event() {
325  return (errors_event_start()
326  + errors_event_stop()
327  + errors_event_overflow()
328  + errors_event_invalid_words()
329  + errors_event_invalid_xor());
330  };
331  uint32_t errors_tbm() {
332  return (errors_tbm_header()
333  + errors_tbm_trailer()
334  + errors_tbm_eventid_mismatch());
335  };
336  uint32_t errors_roc() {
337  return (errors_roc_missing()
338  + errors_roc_readback());
339  };
340  uint32_t errors_pixel() {
341  return (errors_pixel_incomplete()
342  + errors_pixel_address()
343  + errors_pixel_pulseheight()
344  + errors_pixel_buffer_corrupt());
345  };
346  uint32_t errors_event_start() { return m_errors_event_start; }
347  uint32_t errors_event_stop() { return m_errors_event_stop; }
348  uint32_t errors_event_overflow() { return m_errors_event_overflow; }
349  uint32_t errors_event_invalid_words() { return m_errors_event_invalid_words; }
350  uint32_t errors_event_invalid_xor() { return m_errors_event_invalid_xor; }
351  uint32_t errors_tbm_header() { return m_errors_tbm_header; }
352  uint32_t errors_tbm_eventid_mismatch() { return m_errors_tbm_eventid_mismatch; }
353  uint32_t errors_tbm_trailer() { return m_errors_tbm_trailer; }
354  uint32_t errors_roc_missing() { return m_errors_roc_missing; }
355  uint32_t errors_roc_readback() { return m_errors_roc_readback; }
356  uint32_t errors_pixel_incomplete() { return m_errors_pixel_incomplete; }
357  uint32_t errors_pixel_address() { return m_errors_pixel_address; };
358  uint32_t errors_pixel_pulseheight() { return m_errors_pixel_pulseheight; };
359  uint32_t errors_pixel_buffer_corrupt() { return m_errors_pixel_buffer_corrupt; };
360  private:
361  // Clear all statistics:
362  void clear();
363 
364  // Total number of words read:
365  uint32_t m_info_words_read;
366  // Total number of empty events (no pixel hit):
367  //uint32_t m_info_events_empty;
368  // Total number of valid events (with pixel hits):
369  //uint32_t m_info_events_valid;
370  // Total number of pixel hits:
371  uint32_t m_info_pixels_valid;
372 
373  // Total number of events with misplaced start
374  uint32_t m_errors_event_start;
375  // Total number of events with misplaced stop:
376  uint32_t m_errors_event_stop;
377  // Total number of events with data overflow:
378  uint32_t m_errors_event_overflow;
379  // Total number of invalid 5bit words detected by DESER400:
380  uint32_t m_errors_event_invalid_words;
381  // Total number of events with invalid XOR eye diagram:
382  uint32_t m_errors_event_invalid_xor;
383 
384  // Total number of events with flawed TBM header:
385  uint32_t m_errors_tbm_header;
386  // Total number of events with flawed TBM trailer:
387  uint32_t m_errors_tbm_trailer;
388  // Total number of event ID mismatches in the datastream:
389  uint32_t m_errors_tbm_eventid_mismatch;
390 
391  // Total number of events with missing ROC header(s):
392  uint32_t m_errors_roc_missing;
393  // Total number of misplaced ROC readback start markers:
394  uint32_t m_errors_roc_readback;
395 
396  // Total number of undecodable pixels (data missing)
397  uint32_t m_errors_pixel_incomplete;
398  // Total number of undecodable pixels (by address)
399  uint32_t m_errors_pixel_address;
400  // Total number of undecodable pixels (by pulse height fill bit)
401  uint32_t m_errors_pixel_pulseheight;
402  // Total number of pixels with row 80:
403  uint32_t m_errors_pixel_buffer_corrupt;
404  };
405 }
406 #endif
pixel(uint32_t rawdata, uint8_t rocid, bool invertAddress=false)
Definition: datatypes.h:43
void setValue(double val)
Definition: datatypes.h:85
friend std::ostream & operator<<(std::ostream &out, pixel &px)
Definition: datatypes.h:157
void setRow(uint8_t row)
Definition: datatypes.h:67
uint8_t roc() const
Definition: datatypes.h:47
void setVariance(double var)
Definition: datatypes.h:75
void setColumn(uint8_t column)
Definition: datatypes.h:59
friend std::ostream & operator<<(std::ostream &out, Event &evt)
Definition: datatypes.h:177
double value()
Definition: datatypes.h:79
uint8_t column() const
Definition: datatypes.h:55
friend std::ostream & operator<<(std::ostream &out, rawEvent &record)
Definition: datatypes.h:220
void setRoc(uint8_t roc)
Definition: datatypes.h:51
double variance()
Definition: datatypes.h:71
pixel(uint8_t roc_id, uint8_t column, uint8_t row, double value)
Definition: datatypes.h:39
uint8_t row() const
Definition: datatypes.h:63