EUDAQ
 All Classes Namespaces Files Functions Variables Pages
RawDataEvent.hh
1 #ifndef EUDAQ_INCLUDED_RawDataEvent
2 #define EUDAQ_INCLUDED_RawDataEvent
3 
4 #include <sstream>
5 
6 #include <vector>
7 #include "eudaq/Event.hh"
8 #include "eudaq/Platform.hh"
9 namespace eudaq {
10 
14  class DLLEXPORT RawDataEvent : public Event {
15  EUDAQ_DECLARE_EVENT(RawDataEvent);
16 
17  public:
18  typedef unsigned char byte_t;
19  typedef std::vector<byte_t> data_t;
20  struct DLLEXPORT block_t : public Serializable {
21  block_t(unsigned id, const data_t& data)
22  : id(id), data(data) {}
23  block_t(unsigned id, data_t&& data)
24  : id(id), data(std::move(data)) {}
25  block_t()
26  : id(-1), data(data_t()) {}
27  block_t(Deserializer &);
28  void Serialize(Serializer &) const;
29  void Append(const data_t &data);
30  unsigned id;
31  data_t data;
32  };
33 
34  RawDataEvent(std::string type, unsigned run, unsigned event);
36 
38  size_t AddBlock(unsigned id) {
39  m_blocks.push_back(block_t(id, data_t()));
40  return m_blocks.size() - 1;
41  }
42 
44  template <typename T>
45  size_t AddBlock(unsigned id, const std::vector<T> &data) {
46  m_blocks.push_back(block_t(id, make_vector(data)));
47  return m_blocks.size() - 1;
48  }
49 
51  template <typename T>
52  size_t AddBlock(unsigned id, const T *data, size_t bytes) {
53  m_blocks.push_back(block_t(id, make_vector(data, bytes)));
54  return m_blocks.size() - 1;
55  }
56 
58  template <typename T>
59  void AppendBlock(size_t index, const std::vector<T> &data) {
60  m_blocks[index].Append(make_vector(data));
61  }
62 
64  template <typename T>
65  void AppendBlock(size_t index, const T *data, size_t bytes) {
66  m_blocks[index].Append(make_vector(data, bytes));
67  }
68 
69  unsigned GetID(size_t i) const;
76  const data_t &GetBlock(size_t i) const;
77  byte_t GetByte(size_t block, size_t index) const;
78 
80  size_t NumBlocks() const { return m_blocks.size(); }
81 
82  virtual void Print(std::ostream &) const;
83  static RawDataEvent BORE(std::string type, unsigned run) {
84  return RawDataEvent(type, run, (unsigned)-1, Event::FLAG_BORE);
85  }
86  static RawDataEvent *newBORE(std::string type, unsigned run) {
87 
88  return new RawDataEvent(type, run, (unsigned)-1, Event::FLAG_BORE);
89  }
90  static RawDataEvent EORE(std::string type, unsigned run, unsigned event) {
91  return RawDataEvent(type, run, event, Event::FLAG_EORE);
92  }
93  static RawDataEvent *newEORE(std::string type, unsigned run,
94  unsigned event) {
95 
96  return new RawDataEvent(type, run, event, Event::FLAG_EORE);
97  }
98  virtual void Serialize(Serializer &) const;
99 
101  virtual std::string GetSubType() const { return m_type; }
102 
103  private:
104  // private constructor to create BORE and EORE
105  // make sure that event number is 0 for BORE
106  RawDataEvent(std::string type, unsigned run, unsigned event,
107  Event::Flags flag)
108  : Event(run, event, NOTIMESTAMP, flag), m_type(type) {}
109 
110  template <typename T>
111  static data_t make_vector(const T *data, size_t bytes) {
112  const unsigned char *ptr = reinterpret_cast<const byte_t *>(data);
113  return data_t(ptr, ptr + bytes);
114  }
115 
116  template <typename T>
117  static data_t make_vector(const std::vector<T> &data) {
118  const unsigned char *ptr = reinterpret_cast<const byte_t *>(&data[0]);
119  return data_t(ptr, ptr + data.size() * sizeof(T));
120  }
121 
122  std::string m_type;
123  std::vector<block_t> m_blocks;
124  };
125 }
126 
127 #endif // EUDAQ_INCLUDED_RawDataEvent
size_t NumBlocks() const
Return the number of data blocks in the RawDataEvent.
Definition: RawDataEvent.hh:80
size_t AddBlock(unsigned id, const T *data, size_t bytes)
Add a data block as array with given size.
Definition: RawDataEvent.hh:52
size_t AddBlock(unsigned id, const std::vector< T > &data)
Add a data block as std::vector.
Definition: RawDataEvent.hh:45
virtual std::string GetSubType() const
Return the type string.
Definition: RawDataEvent.hh:101
Definition: Serializer.hh:156
void AppendBlock(size_t index, const T *data, size_t bytes)
Append data to a block as array with given size.
Definition: RawDataEvent.hh:65
Definition: RawDataEvent.hh:20
void AppendBlock(size_t index, const std::vector< T > &data)
Append data to a block as std::vector.
Definition: RawDataEvent.hh:59
Definition: Serializable.hh:13
Definition: RawDataEvent.hh:14
Definition: Serializer.hh:19
Definition: Event.hh:38
size_t AddBlock(unsigned id)
Add an empty block.
Definition: RawDataEvent.hh:38