EUDAQ
 All Classes Namespaces Files Functions Variables Pages
Event.hh
1 #ifndef EUDAQ_INCLUDED_Event
2 #define EUDAQ_INCLUDED_Event
3 
4 #include <string>
5 #include <vector>
6 #include <map>
7 #include <iosfwd>
8 #include <iostream>
9 
10 #include "eudaq/Serializable.hh"
11 #include "eudaq/Serializer.hh"
12 #include "eudaq/Exception.hh"
13 #include "eudaq/Utils.hh"
14 #include "eudaq/Platform.hh"
15 
16 #define EUDAQ_DECLARE_EVENT(type) \
17 public: \
18  static unsigned eudaq_static_id(); \
19  virtual unsigned get_id() const { return eudaq_static_id(); } \
20  \
21 private: \
22  static const int EUDAQ_DUMMY_VAR_DONT_USE = 0
23 
24 #define EUDAQ_DEFINE_EVENT(type, id) \
25  unsigned type::eudaq_static_id() { \
26  static const unsigned id_(id); \
27  return id_; \
28  } \
29  namespace _eudaq_dummy_ { \
30  static eudaq::RegisterEventType<type> eudaq_reg; \
31  } \
32  static const int EUDAQ_DUMMY_VAR_DONT_USE = 0
33 
34 namespace eudaq {
35 
36  static const uint64_t NOTIMESTAMP = (uint64_t)-1;
37 
38  class DLLEXPORT Event : public Serializable {
39  public:
40  enum Flags {
41  FLAG_BORE = 1,
42  FLAG_EORE = 2,
43  FLAG_HITS = 4,
44  FLAG_FAKE = 8,
45  FLAG_SIMU = 16,
46  FLAG_EUDAQ2 = 32,
47  FLAG_PACKET = 64,
48  FLAG_BROKEN = 128,
49  FLAG_STATUS = 256,
50  FLAG_ALL = (unsigned)-1
51  }; // Matches FLAGNAMES in .cc file
52  Event(unsigned run, unsigned event, uint64_t timestamp = NOTIMESTAMP,
53  unsigned flags = 0)
54  : m_flags(flags), m_runnumber(run), m_eventnumber(event),
55  m_timestamp(timestamp) {}
56  Event(Deserializer &ds);
57  virtual void Serialize(Serializer &) const = 0;
58 
59  unsigned GetRunNumber() const { return m_runnumber; }
60  unsigned GetEventNumber() const { return m_eventnumber; }
61  uint64_t GetTimestamp() const { return m_timestamp; }
62 
66  virtual std::string GetSubType() const { return ""; }
67 
68  virtual void Print(std::ostream &os) const = 0;
69 
70  bool HasTag(const std::string &name) const;
71  std::vector<std::string> GetTagList(const std::string &prefix) const;
72 
73  Event &SetTag(const std::string &name, const std::string &val);
74  template <typename T> Event &SetTag(const std::string &name, const T &val) {
75  return SetTag(name, eudaq::to_string(val));
76  }
77  std::string GetTag(const std::string &name,
78  const std::string &def = "") const;
79  std::string GetTag(const std::string &name, const char *def) const {
80  return GetTag(name, std::string(def));
81  }
82  template <typename T> T GetTag(const std::string &name, T def) const {
83  return eudaq::from_string(GetTag(name), def);
84  }
85 
86  bool IsBORE() const { return GetFlags(FLAG_BORE) != 0; }
87  bool IsEORE() const { return GetFlags(FLAG_EORE) != 0; }
88  bool HasHits() const { return GetFlags(FLAG_HITS) != 0; }
89  bool IsFake() const { return GetFlags(FLAG_FAKE) != 0; }
90  bool IsSimulation() const { return GetFlags(FLAG_SIMU) != 0; }
91 
92  static unsigned str2id(const std::string &idstr);
93  static std::string id2str(unsigned id);
94  unsigned GetFlags(unsigned f = FLAG_ALL) const { return m_flags & f; }
95  void SetFlags(unsigned f) { m_flags |= f; }
96  void SetTimeStampToNow();
97  void setTimeStamp(uint64_t timeStamp) { m_timestamp = timeStamp; }
98  void ClearFlags(unsigned f = FLAG_ALL) { m_flags &= ~f; }
99  virtual unsigned get_id() const = 0;
100 
101  protected:
102  typedef std::map<std::string, std::string> map_t;
103 
104  unsigned m_flags, m_runnumber, m_eventnumber;
105  uint64_t m_timestamp;
106  map_t m_tags;
107  };
108 
109  DLLEXPORT std::ostream &operator<<(std::ostream &, const Event &);
110 
111  class DLLEXPORT EventFactory {
112  public:
113  static Event *Create(Deserializer &ds) {
114  unsigned id = 0;
115  ds.read(id);
116  // std::cout << "Create id = " << std::hex << id << std::dec << std::endl;
117  event_creator cr = GetCreator(id);
118  if (!cr)
119  EUDAQ_THROW("Unrecognised Event type (" + Event::id2str(id) + ")");
120  return cr(ds);
121  }
122 
123  typedef Event *(*event_creator)(Deserializer &ds);
124  static void Register(uint32_t id, event_creator func);
125  static event_creator GetCreator(uint32_t id);
126 
127  private:
128  typedef std::map<uint32_t, event_creator> map_t;
129  static map_t &get_map();
130  };
131 
134  template <typename T_Evt> struct RegisterEventType {
136  EventFactory::Register(T_Evt::eudaq_static_id(), &factory_func);
137  }
138  static Event *factory_func(Deserializer &ds) { return new T_Evt(ds); }
139  };
140 }
141 
142 #endif // EUDAQ_INCLUDED_Event
virtual std::string GetSubType() const
Definition: Event.hh:66
Definition: Event.hh:111
map_t m_tags
Metadata tags in (name=value) pairs of strings.
Definition: Event.hh:106
Definition: Serializer.hh:156
Definition: Serializable.hh:13
T DLLEXPORT from_string(const std::string &x, const T &def=0)
Definition: Utils.hh:115
Definition: Event.hh:134
std::string to_string(const T &x, int digits=0)
Definition: Utils.hh:54
Definition: Serializer.hh:19
Definition: Event.hh:38