ICP  1
event_store.h
Go to the documentation of this file.
1 #ifndef EVENT_STORE_H
2 #define EVENT_STORE_H
3 
4 #include "IsisBase.h"
5 #include "icp_interfaces.h"
6 #include "isisvme.h"
7 #include "dae_events.h"
8 #include "SingleFile.h"
9 #include "SplitFile.h"
10 
11 class EventStoreCard;
13 
14 typedef boost::signals2::signal< void(const DAEEventHeader*, const DetectorEvent32*, int, int, const int*) > EventCallbackType;
15 typedef EventCallbackType::slot_type EventCallbackSlotType;
16 
17 typedef boost::signals2::signal< void(const DAEEventHeader*, int) > FrameCallbackType;
18 typedef FrameCallbackType::slot_type FrameCallbackSlotType;
19 
21 typedef boost::signals2::signal< void(bool, NoFrameCallbackMode, int) > NoFrameCallbackType;
22 typedef NoFrameCallbackType::slot_type NoFrameCallbackSlotType;
23 
25 class ISISRAW;
26 
27 class EventStore : public ISIS::Base
28 {
29 private:
30  static const int m_mempool_size;
31  Poco::MemoryPool m_mempool;
32  Poco::ThreadPool m_threads;
33  std::vector<FileEventSourceBase*> m_sources; // list of ones we created so we can remove. Should really use shared pointers...
35  {
37  bool enable;
38  EventStoreCardHolder() : card(NULL), enable(false) { }
39  EventStoreCardHolder(EventStoreCard* card_, bool enable_) : card(card_), enable(enable_) { }
40  // card freed explicitly in destructore of EventStore
41  };
42  std::vector<EventStoreCardHolder> m_stores;
43  static Poco::Path m_base_dir;
44  std::list<boost::signals2::connection> m_callbacks;
45 
46  template<typename SLOT>
47  std::list<boost::signals2::connection> addCallback(boost::signals2::connection (EventStoreCard::*func)(const SLOT&), const SLOT& slot);
48 
49  void clearAllCallbacks() { std::for_each(m_callbacks.begin(), m_callbacks.end(), boost::bind(&boost::signals2::connection::disconnect, _1)); m_callbacks.clear(); }
50  void clearSources();
51  void clearStores();
52  void findUnusedFileInputSources(int run_number, std::set<std::string>& files);
54 
55 public:
57  {
58  uint64_t raw_events;
59  uint64_t good_events;
60  uint32_t raw_frames;
61  uint32_t good_frames;
63  {
64  zero();
65  }
66  void zero()
67  {
69  }
70  void add(const FileEventSourceInfo& f)
71  {
72 
75  raw_frames = std::max(f.raw_frames, raw_frames);
76  good_frames = std::max(f.good_frames, good_frames);
77  }
78  };
79  EventStore();
81  void releaseBuffer(isisU32_t* buffer);
82  uint32_t bufferSize() { return m_mempool_size / sizeof(isisU32_t); }
83  ~EventStore();
84  void addCards(const std::vector<DAEEventSource*>& det_list);
85  void addCard(DAEEventSource* det_card, bool enable = false);
86  std::list<boost::signals2::connection> addGoodEventCallback(const EventCallbackSlotType& slot);
87  std::list<boost::signals2::connection> addAllEventCallback(const EventCallbackSlotType& slot);
88  std::list<boost::signals2::connection> addGoodFrameCallback(const FrameCallbackSlotType& slot);
89  std::list<boost::signals2::connection> addAllFrameCallback(const FrameCallbackSlotType& slot);
90  std::list<boost::signals2::connection> addNoFrameCallback(const NoFrameCallbackSlotType& slot);
91  void clearCallbacks(const std::list<boost::signals2::connection>& callback_list)
92  {
93  std::for_each(callback_list.begin(), callback_list.end(), boost::bind(&boost::signals2::connection::disconnect, _1));
94  }
95  void start();
96  void stop(bool immediate);
97  bool endHeaderSeen();
98  uint64_t nNewEventWords(DAEstatus& status);
99  void clear();
100  void setOutputFileName(int run_number, bool new_run, DAEstatus& status);
101  void addFileInputSource(int run_number, FileEventSourceInfo& info);
102  void addRawFileInputSource(ISISRAW* iraw, int source_id, time_t run_start);
103  void setCardStatus(int id, bool enable);
104  void setCardStatus(bool enable);
105  uint64_t numRawEvents();
106  uint64_t numGoodEvents();
107  void getEventSourceIDs(std::vector<int>& ids);
108  void getEnabledEventSourceIDs(std::vector<int>& ids);
109  static const Poco::Path& baseDir()
110  {
111  if (m_base_dir.toString().size() == 0)
112  {
113  m_base_dir = Poco::Util::Application::instance().config().getString("isisicp.eventdir", "c:\\data\\events");
114  }
115  return m_base_dir;
116  }
117 
118  static Poco::Path baseRunDir(int run_number)
119  {
120  Poco::Path the_dir(baseDir(), "");
121  the_dir.pushDirectory(Poco::format("run_%d", run_number));
122  return the_dir;
123  }
124 
125  static void deleteOutputFile(int run_number, DAEstatus& status);
126  void closeOutputFile(DAEstatus& status);
127  void closeAndDeleteOutputFile(int run_number, DAEstatus& status);
128  static int maxNumCallbackEvents() { return m_mempool_size / sizeof(DetectorEvent32); } // maximum number of events in a callback
129 
130 private:
131  void clearOutputFile(DAEstatus& status);
132  static void findFileInputSources(int run_number, std::set<std::string>& files);
133  static int eventSourceIDFromFileInputSource(const std::string& file);
134 
135 };
136 
137 
139 {
140  uint64_t file_words;
141  uint64_t good_events;
142  uint64_t raw_events;
144  uint32_t last_frame;
145  uint32_t num_events;
146  uint32_t header_words;
147  uint32_t good_frames;
148  uint32_t raw_frames;
149  uint32_t last_address_read;
151  void newFile()
152  {
153  memset(this, 0, sizeof(InfoFileData));
154  end_header_present = false;
155  --last_frame;
157  }
158 };
159 
160 class EventStoreCard : public ISIS::Base, public Poco::Runnable
161 {
162 public:
163  boost::signals2::connection addGoodEventCallback(const EventCallbackSlotType& slot);
164  boost::signals2::connection addAllEventCallback(const EventCallbackSlotType& slot);
165 
166  boost::signals2::connection addGoodFrameCallback(const FrameCallbackSlotType& slot);
167  boost::signals2::connection addAllFrameCallback(const FrameCallbackSlotType& slot);
168 
169  boost::signals2::connection addNoFrameCallback(const NoFrameCallbackSlotType& slot);
170 
171  void flushEvents(DAEstatus& status);
172 
174 
175  void run();
176 
178  void stop(bool immediate) { m_keep_going = false; m_immediate_stop = immediate; }
179 
181 
182  bool endHeaderSeen() const { return m_data.end_header_present; }
183  void setOutputFileName(int run_number, bool new_run, DAEstatus& status);
184  void clearOutputFile(DAEstatus& status);
185  int eventSourceID() const { return (m_dc != NULL ? m_dc->eventSourceID() : -1); }
186 
187  std::string getFileName() { return m_file_name; }
188  static std::string fileNameForDC(int run_number, int dc_number);
189  static std::string infoFileNameForDC(int run_number, int dc_number);
190  static void getInfoFileData(const std::string& file_name, InfoFileData& info);
191  void closeFiles();
192  uint64_t numRawEvents() { return m_data.raw_events; }
193  uint64_t numGoodEvents() { return m_data.good_events; }
194  uint64_t nNewEventWords(DAEstatus& status);
196  ~EventStoreCard();
197 
198 private:
202 // bool m_end_header_present;
203 // unsigned m_num_events; ///< total number of events read in frame m_header
204 // DAEEventHeader m_header; ///< header of current frame being processed
205 // unsigned m_header_words; ///< number of words of header read, if < DAE_EVENT_HEADER_WORDS then we have a partial header
206 // unsigned m_last_frame;
207 // uint32_t m_file_words;
209  Poco::ThreadPool m_threads;
213  uint32_t m_len_buffer;
215  std::string m_file_name;
216  std::string m_info_file_name;
222  void updateFilePosition(DAEstatus& status);
223  bool fixFrameHeader(DAEEventHeader& head);
224  void fixBadFrameHeader(DAEEventHeader& head);
225  virtual std::string loggingPrefix() const { return Poco::format("Source %d: ", eventSourceID()); }
226  // things we do not want to do
227  EventStoreCard() : m_dc(NULL), m_store(NULL), m_buffer(NULL) { }
228  void operator=(const EventStoreCard&) { }
230 };
231 
233 {
234 public:
235  virtual ~FileEventSourceBase() { }
236 };
237 
239 {
240  std::string m_file_name;
241  std::string m_info_file_name;
242  uint64_t m_file_words;
246 
247 public:
248  FileEventSource(int run_number, int source_id);
249  virtual ~FileEventSource();
250  uint32_t getNewEvents(isisU32_t* buffer, uint32_t maxlen, uint64_t& num_unread, bool& run_ended, DAEstatus& status);
251  uint64_t nNewEventWords(DAEstatus& status);
252  void updateDAEEventLastRead(DAEstatus& status);
253  int eventSourceID() const;
254  const int* getDAE2SpecMap();
255  void setLastAddressRead(isisU32_t addr);
258 private:
259  std::string loggingPrefix() const { return Poco::format("Source %d: ", m_source_id); }
260  // things we do not want to do
262  void operator=(const FileEventSource&) { }
264 };
265 
266 class ISISRAW;
267 
269 {
270  uint32_t m_events;
275  uint32_t m_events_sent;
276  uint32_t m_pos;
277  uint32_t m_pos_sent;
278  float m_good_ua;
281  time_t m_run_start;
282  std::vector<int> m_frame_events;
283 
284  int readEvents(isisU32_t* buffer, uint32_t max_num_to_read, uint64_t& num_unread);
285  int getFrame(uint32_t event_count);
286  int sendHeader(isisU32_t* buffer, int current_frame);
287 
288 public:
289  ISISRAWEventSource(ISISRAW* iraw, int source_id, time_t run_start);
290  virtual ~ISISRAWEventSource();
291  uint32_t getNewEvents(isisU32_t* buffer, uint32_t maxlen, uint64_t& num_unread, bool& run_ended, DAEstatus& status);
292  uint64_t nNewEventWords(DAEstatus& status);
293  void updateDAEEventLastRead(DAEstatus& status);
294  int eventSourceID() const;
295  const int* getDAE2SpecMap();
296  void setLastAddressRead(isisU32_t addr);
299 private:
300  std::string loggingPrefix() const { return Poco::format("Source %d: ", m_source_id); }
301 };
302 
303 #endif /* EVENT_STORE_H */
void flushEvents(DAEstatus &status)
const int * getDAE2SpecMap()
SplitFile m_file
Definition: event_store.h:214
EventStoreCard(const EventStoreCard &)
Definition: event_store.h:229
uint64_t numGoodEvents()
Definition: event_store.cpp:55
void setLastAddressRead(isisU32_t addr)
static std::string fileNameForDC(int run_number, int dc_number)
uint32_t bufferSize()
Definition: event_store.h:82
InfoFileData m_data
Definition: event_store.h:199
void clearAllCallbacks()
Definition: event_store.h:49
boost::signals2::signal< void(bool, NoFrameCallbackMode, int) > NoFrameCallbackType
Definition: event_store.h:21
isisU32_t * getBuffer()
NoFrameCallbackType m_no_frame_callbacks
Definition: event_store.h:221
DAEEventSource * m_dc
Definition: event_store.h:211
boost::signals2::connection addAllEventCallback(const EventCallbackSlotType &slot)
std::list< boost::signals2::connection > addGoodFrameCallback(const FrameCallbackSlotType &slot)
static std::string infoFileNameForDC(int run_number, int dc_number)
boost::signals2::connection addAllFrameCallback(const FrameCallbackSlotType &slot)
uint32_t raw_frames
total number of raw frames in file
Definition: event_store.h:148
static int eventSourceIDFromFileInputSource(const std::string &file)
Definition: event_store.cpp:85
uint32_t m_pos_sent
number of events sent from pos
Definition: event_store.h:277
Poco::MemoryPool m_mempool
Definition: event_store.h:31
uint64_t numRawEvents()
Definition: event_store.cpp:14
virtual int eventSourceID() const =0
std::list< boost::signals2::connection > m_callbacks
Definition: event_store.h:44
void newFile()
Definition: event_store.h:151
fixed header marker for DAEEventHeader
Definition: dae_events.h:44
uint64_t nNewEventWords(DAEstatus &status)
int eventSourceID() const
isisU32_t * m_buffer
buffer containing new events from detector card
Definition: event_store.h:212
NoFrameCallbackType::slot_type NoFrameCallbackSlotType
Definition: event_store.h:22
unsigned long isisU32_t
Definition: isisvme_types.h:8
uint32_t m_events_sent
events sent to date
Definition: event_store.h:275
void operator=(const EventStoreCard &)
Definition: event_store.h:228
Poco::ThreadPool m_threads
Definition: event_store.h:209
std::vector< FileEventSourceBase * > m_sources
Definition: event_store.h:33
std::string getFileName()
Definition: event_store.h:187
virtual std::string loggingPrefix() const
Definition: event_store.h:225
void releaseBuffer(isisU32_t *buffer)
uint64_t m_file_words
Definition: event_store.h:242
void clear()
InfoFileData m_info
Definition: event_store.h:243
void readInfo(EventStore::FileEventSourceInfo &info)
std::string m_info_file_name
Definition: event_store.h:241
uint32_t getNewEvents(isisU32_t *buffer, uint32_t maxlen, uint64_t &num_unread, bool &run_ended, DAEstatus &status)
void getEnabledEventSourceIDs(std::vector< int > &ids)
Definition: event_store.cpp:43
void setOutputFileName(int run_number, bool new_run, DAEstatus &status)
static Poco::Path baseRunDir(int run_number)
Definition: event_store.h:118
std::string loggingPrefix() const
Definition: event_store.h:259
void setCardStatus(int id, bool enable)
uint32_t last_frame
offset to start of last frame in words
Definition: event_store.h:144
void addCard(DAEEventSource *det_card, bool enable=false)
std::string loggingPrefix() const
Definition: event_store.h:300
uint32_t last_address_read
detector card address read which corresponds to file_words
Definition: event_store.h:149
virtual ~ISISRAWEventSource()
int eventSourceID() const
ISISRAWEventSource(ISISRAW *iraw, int source_id, time_t run_start)
boost::signals2::connection addNoFrameCallback(const NoFrameCallbackSlotType &slot)
void invokeNoFrameCallbacks(NoFrameCallbackMode mode)
std::string m_file_name
Definition: event_store.h:240
void closeOutputFile(DAEstatus &status)
void stop(bool immediate)
std::vector< EventStoreCardHolder > m_stores
Definition: event_store.h:42
uint64_t raw_events
total number of raw events in file
Definition: event_store.h:142
void add(const FileEventSourceInfo &f)
Definition: event_store.h:70
static const int m_mempool_size
Definition: event_store.h:30
void clearCallbacks(const std::list< boost::signals2::connection > &callback_list)
Definition: event_store.h:91
uint64_t nNewEventWords(DAEstatus &status)
Definition: event_store.cpp:24
uint64_t good_events
total number of good events in file
Definition: event_store.h:141
bool end_header_present
Definition: event_store.h:150
void checkAndHistogramEvents()
void fixBadFrameHeader(DAEEventHeader &head)
DAEEventHeader header
header of current frame being processed
Definition: event_store.h:143
std::list< boost::signals2::connection > addAllFrameCallback(const FrameCallbackSlotType &slot)
int sendHeader(isisU32_t *buffer, int current_frame)
std::list< boost::signals2::connection > addGoodEventCallback(const EventCallbackSlotType &slot)
bool endHeaderSeen()
EventStoreCardHolder(EventStoreCard *card_, bool enable_)
Definition: event_store.h:39
void invokeNoFrameCallbacks(NoFrameCallbackMode mode)
FrameCallbackType m_good_frame_callbacks
Definition: event_store.h:219
int readEvents(isisU32_t *buffer, uint32_t max_num_to_read, uint64_t &num_unread)
void stop(bool immediate)
request stop after processing finished
Definition: event_store.h:178
uint64_t numGoodEvents()
Definition: event_store.h:193
const int * getDAE2SpecMap()
uint64_t nNewEventWords(DAEstatus &status)
uint32_t m_len_buffer
number of words in m_buffer
Definition: event_store.h:213
isisU32_t getLastAddressRead()
void operator=(const FileEventSource &)
Definition: event_store.h:262
virtual ~FileEventSource()
void setLastAddressRead(isisU32_t addr)
bool fixFrameHeader(DAEEventHeader &head)
void closeAndDeleteOutputFile(int run_number, DAEstatus &status)
static void deleteOutputFile(int run_number, DAEstatus &status)
bool m_immediate_stop
Definition: event_store.h:201
std::string m_info_file_name
Definition: event_store.h:216
int getFrame(uint32_t event_count)
find out what frame to put event number event_count into - first frame is 0
std::list< boost::signals2::connection > addCallback(boost::signals2::connection(EventStoreCard::*func)(const SLOT &), const SLOT &slot)
void clearStores()
uint32_t m_events
total events to send
Definition: event_store.h:270
void setOutputFileName(int run_number, bool new_run, DAEstatus &status)
void updateFilePosition(DAEstatus &status)
virtual ~FileEventSourceBase()
Definition: event_store.h:235
int eventSourceID() const
Definition: event_store.h:185
uint32_t good_frames
total number of good frames in file
Definition: event_store.h:147
boost::signals2::connection addGoodEventCallback(const EventCallbackSlotType &slot)
void findUnusedFileInputSources(int run_number, std::set< std::string > &files)
static void getInfoFileData(const std::string &file_name, InfoFileData &info)
Poco::ThreadPool m_threads
Definition: event_store.h:32
EventCallbackType m_all_event_callbacks
Definition: event_store.h:218
static const Poco::Path & baseDir()
Definition: event_store.h:109
uint64_t file_words
size of all data in file in words
Definition: event_store.h:140
void clearOutputFile(DAEstatus &status)
void clearSources()
std::list< boost::signals2::connection > addNoFrameCallback(const NoFrameCallbackSlotType &slot)
boost::signals2::signal< void(const DAEEventHeader *, const DetectorEvent32 *, int, int, const int *) > EventCallbackType
Definition: event_store.h:12
static int maxNumCallbackEvents()
Definition: event_store.h:128
boost::signals2::connection addGoodFrameCallback(const FrameCallbackSlotType &slot)
FrameCallbackType::slot_type FrameCallbackSlotType
Definition: event_store.h:18
static Poco::Path m_base_dir
Definition: event_store.h:43
SplitFile m_file
Definition: event_store.h:245
FrameCallbackType m_all_frame_callbacks
Definition: event_store.h:220
NoFrameCallbackMode
Definition: event_store.h:20
void addCards(const std::vector< DAEEventSource * > &det_list)
uint32_t m_pos
current location in raw data
Definition: event_store.h:276
static void findFileInputSources(int run_number, std::set< std::string > &files)
Definition: event_store.cpp:65
uint64_t nNewEventWords(DAEstatus &status)
void updateDAEEventLastRead(DAEstatus &status)
std::string m_file_name
Definition: event_store.h:215
uint32_t getNewEvents(isisU32_t *buffer, uint32_t maxlen, uint64_t &num_unread, bool &run_ended, DAEstatus &status)
void addRawFileInputSource(ISISRAW *iraw, int source_id, time_t run_start)
void updateDAEEventLastRead(DAEstatus &status)
EventStore * m_store
Definition: event_store.h:210
std::vector< int > m_frame_events
Definition: event_store.h:282
uint32_t num_events
total number of events read in frame m_header
Definition: event_store.h:145
void start()
FileEventSource(const FileEventSource &)
Definition: event_store.h:263
EventCallbackType::slot_type EventCallbackSlotType
Definition: event_store.h:15
EventCallbackType m_good_event_callbacks
Definition: event_store.h:217
void addFileInputSource(int run_number, FileEventSourceInfo &info)
void getEventSourceIDs(std::vector< int > &ids)
Definition: event_store.cpp:34
std::list< boost::signals2::connection > addAllEventCallback(const EventCallbackSlotType &slot)
bool endHeaderSeen() const
Definition: event_store.h:182
uint32_t header_words
number of words of header read, if &lt; DAE_EVENT_HEADER_WORDS then we have a partial header ...
Definition: event_store.h:146
void clearOutputFile(DAEstatus &status)
isisU32_t getLastAddressRead()
boost::signals2::signal< void(const DAEEventHeader *, int) > FrameCallbackType
Definition: event_store.h:17
uint64_t numRawEvents()
Definition: event_store.h:192
void readInfo(EventStore::FileEventSourceInfo &info)