Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HDEVIO.h
Go to the documentation of this file.
1 // $Id$
2 //
3 // File: HDEVIO.h
4 // Created: Wed Dec 10 07:22:00 EST 2014
5 // Creator: davidl (on Darwin harriet 13.4.0 i386)
6 //
7 
8 #ifndef _HDEVIO_
9 #define _HDEVIO_
10 
11 #ifndef _DBG_
12 #define _DBG_ cout<<__FILE__<<":"<<__LINE__<<" "
13 #define _DBG__ cout<<__FILE__<<":"<<__LINE__<<endl
14 #endif
15 
16 
17 #include <stdint.h>
18 #include <vector>
19 #include <set>
20 #include <string>
21 #include <fstream>
22 #include <sstream>
23 #include <iostream>
24 using namespace std;
25 
26 // ----- Stolen from evio.h -----------
27 #define swap64(x) ( (((x) >> 56) & 0x00000000000000FFL) | \
28  (((x) >> 40) & 0x000000000000FF00L) | \
29  (((x) >> 24) & 0x0000000000FF0000L) | \
30  (((x) >> 8) & 0x00000000FF000000L) | \
31  (((x) << 8) & 0x000000FF00000000L) | \
32  (((x) << 24) & 0x0000FF0000000000L) | \
33  (((x) << 40) & 0x00FF000000000000L) | \
34  (((x) << 56) & 0xFF00000000000000L) )
35 
36 #define swap32(x) ( (((x) >> 24) & 0x000000FF) | \
37  (((x) >> 8) & 0x0000FF00) | \
38  (((x) << 8) & 0x00FF0000) | \
39  (((x) << 24) & 0xFF000000) )
40 
41 #define swap16(x) ( (((x) >> 8) & 0x00FF) | \
42  (((x) << 8) & 0xFF00) )
43 //---------------------------------------
44 
45 class HDEVIO{
46  public:
47  HDEVIO(string filename, bool read_map_file=true, int verbose=1);
48  virtual ~HDEVIO();
49 
50  enum{
51  HDEVIO_OK=0,
62  HDEVIO_UNKNOWN_BANK_TYPE
63  }ERRORCODE_t;
64 
65  enum BLOCKTYPE{
74  kBT_PHYSICS
75  };
76 
77  // The following structs are used to overlay the
78  // the EVIO header and first few words. This is
79  // used for scanning/mapping the file.
80  typedef struct{
81  uint32_t ts_header;
82  uint32_t timestamp;
84 
85  typedef struct{
86  uint32_t first_crate_len;
88  }BORHEADER_t;
89 
90  typedef struct{
91  uint32_t trigger_bank_len;
94  uint32_t first_event_hi; // n.b. contradicts documentation!
95  uint32_t first_event_lo;
97 
98  typedef struct{
99  uint32_t roc1_bank_len;
102  uint32_t first_event; // 32bit only in CDAQ
104 
105  typedef struct{
106  // Standard EVIO 8 word block header
107  uint32_t length;
108  uint32_t blocknum;
109  uint32_t headerlen;
110  uint32_t eventcnt;
111  uint32_t reserved1;
112  uint32_t bitinfo;
113  uint32_t reserved2;
114  uint32_t magic;
115 
116  // first EVIO event in block
117  uint32_t event_len;
118  uint32_t header;
119 
120  // Next few words depend on type of data in block
121  union {
126  };
127  }BLOCKHEADER_t;
128 
129  typedef struct{
130 
131  uint32_t event_len;
132  uint32_t header;
133 
134  // Next few words depend on type of data in event
135  union {
140  };
141  }EVENTHEADER_t;
142 
144  public:
145  streampos pos;
146  uint32_t event_len;
147  uint32_t event_header;
148  uint64_t first_event;
149  uint64_t last_event;
151  };
152 
154  public:
155  streampos pos;
156  uint32_t block_len;
158  vector<EVIOEventRecord> evio_events;
159  uint64_t first_event;
160  uint64_t last_event;
162  };
163 
164  string filename;
165  ifstream ifs;
166  bool is_open;
167  uint32_t *fbuff;
168  uint32_t *fnext;
169  uint32_t *fbuff_end;
170  uint64_t fbuff_size;
171  uint64_t fbuff_len;
172  uint64_t _gcount;
173 
174 
175  uint32_t *buff; // buffer holding current block (if any)
176  uint32_t *next; // Pointer to start of next EVIO event within buff
177  uint32_t *buff_end; // Pointer to word just past end of valid buffer words
178  uint32_t buff_size; // memory currently allocated for buff in words
179  uint32_t buff_len; // valid words in buff
180  uint32_t buff_limit; // maximum allowed allocation for buff_max
181  bool swap_needed; // true if block header indicates swapping is needed
182  BLOCKHEADER_t *bh; // =buff, but cast as a BLOCKHEADER_t*
183  streampos last_event_pos; // used to hold file position at last event read in
184  uint32_t last_event_len; // used to hold last event length in words if user buffer was
185  // too small, this is how big is should be allocated
186 
187  int VERBOSE;
190 
191  stringstream err_mess; // last error message
192  uint32_t err_code; // last error code
193 
194  uint64_t Nblocks;
195  uint64_t Nevents;
196  uint64_t Nerrors;
197  uint64_t Nbad_blocks;
198  uint64_t Nbad_events;
199 
200  bool ReadBlock(void);
201  bool read(uint32_t *user_buff, uint32_t user_buff_len, bool allow_swap=true);
202  bool readSparse(uint32_t *user_buff, uint32_t user_buff_len, bool allow_swap=true);
203  bool readNoFileBuff(uint32_t *user_buff, uint32_t user_buff_len, bool allow_swap=true);
204  void rewind(void);
205  uint64_t GetNWordsLeftInFile(void);
206 
207  uint32_t swap_bank(uint32_t *outbuff, uint32_t *inbuff, uint32_t len);
208  uint32_t swap_tagsegment(uint32_t *outbuff, uint32_t *inbuff, uint32_t len);
209  uint32_t swap_segment(uint32_t *outbuff, uint32_t *inbuff, uint32_t len);
210  void Print_fbuff(void);
211  void PrintEVIOBlockHeader(void);
212  void PrintStats(void);
213  void PrintFileSummary(void);
214  void SaveFileMap(string fname="");
215  void ReadFileMap(string fname="", bool warn_if_not_found=false);
216 
217  uint32_t GetEventMask(void) { return event_type_mask; }
218  uint32_t SetEventMask(uint32_t mask);
219  uint32_t SetEventMask(string types_str);
220  uint32_t AddToEventMask(string type_str);
221  vector<EVIOBlockRecord>& GetEVIOBlockRecords(void);
222 
223  protected:
224 
225  uint32_t event_type_mask;
226 
227  bool is_mapped;
229  vector<EVIOBlockRecord> evio_blocks;
230  void MapBlocks(bool print_ticker=true);
231  void MapEvents(BLOCKHEADER_t &bh, EVIOBlockRecord &br);
232  vector<EVIOBlockRecord>::iterator sparse_block_iter;
235  streampos NB_next_pos;
236 
237  void ClearErrorMessage(void){ err_mess.str(""); err_mess.clear();}
238  void SetErrorMessage(string mess){ ClearErrorMessage(); err_mess<<mess;}
239 
240  void buff_read(char* s, streamsize n);
241  void buff_seekg (streamoff off, ios_base::seekdir way);
242  streamsize buff_gcount() const { return (streamsize)_gcount; }
243 
244 
245  public:
246 
247  //------------------------
248  // HexStr
249  //------------------------
250  inline string HexStr(uint32_t v)
251  {
252  char str[256];
253  sprintf(str, "0x%08x", v);
254 
255  return string(str);
256  }
257 
258  //------------------------
259  // HexStr
260  //------------------------
261  inline string HexStr(uint16_t v)
262  {
263  char str[256];
264  sprintf(str, "0x%04x", v);
265 
266  return string(str);
267  }
268 
269  //------------------------
270  // HexStr
271  //------------------------
272  inline string HexStr(uint8_t v)
273  {
274  char str[256];
275  sprintf(str, "0x%02x", v);
276 
277  return string(str);
278  }
279 
280  //---------------------------------
281  // swap_block
282  //---------------------------------
283  inline void swap_block(uint16_t *inbuff, uint16_t len, uint16_t *outbuff)
284  {
285  for(uint32_t i=0; i<len; i++, inbuff++, outbuff++){
286  *outbuff = swap16(*inbuff);
287  }
288  }
289 
290  //---------------------------------
291  // swap_block
292  //---------------------------------
293  inline void swap_block(uint32_t *inbuff, uint32_t len, uint32_t *outbuff)
294  {
295  for(uint32_t i=0; i<len; i++, inbuff++, outbuff++){
296  *outbuff = swap32(*inbuff);
297  }
298  }
299 
300  //---------------------------------
301  // swap_block
302  //---------------------------------
303  inline void swap_block(uint64_t *inbuff, uint64_t len, uint64_t *outbuff)
304  {
305  for(uint64_t i=0; i<len; i++, inbuff++, outbuff++){
306  *outbuff = swap64(*inbuff);
307  }
308  }
309 
310 };
311 
312 #endif // _HDEVIO_
313 
uint32_t first_event_lo
Definition: HDEVIO.h:95
uint32_t trigger_bank_len
Definition: HDEVIO.h:91
uint32_t sparse_event_idx
Definition: HDEVIO.h:233
vector< EVIOBlockRecord > evio_blocks
Definition: HDEVIO.h:229
PHYSICSHEADER_t physics
Definition: HDEVIO.h:124
BLOCKTYPE event_type
Definition: HDEVIO.h:150
BLOCKHEADER_t * bh
Definition: HDEVIO.h:182
uint32_t swap_bank(uint32_t *outbuff, uint32_t *inbuff, uint32_t len)
Definition: swap_bank.cc:15
uint32_t err_code
Definition: HDEVIO.h:192
uint32_t eventcnt
Definition: HDEVIO.h:110
#define swap64(x)
Definition: HDEVIO.h:27
string HexStr(uint8_t v)
Definition: HDEVIO.h:272
char str[256]
ifstream ifs
Definition: HDEVIO.h:165
uint32_t reserved2
Definition: HDEVIO.h:113
bool is_mapped
Definition: HDEVIO.h:227
streampos last_event_pos
Definition: HDEVIO.h:183
PHYSICSHEADER_t physics
Definition: HDEVIO.h:138
uint32_t timestamp
Definition: HDEVIO.h:82
char string[256]
uint32_t * fnext
Definition: HDEVIO.h:168
sprintf(text,"Post KinFit Cut")
uint32_t swap_tagsegment(uint32_t *outbuff, uint32_t *inbuff, uint32_t len)
Definition: swap_bank.cc:102
TString filename
bool is_open
Definition: HDEVIO.h:166
bool SKIP_EVENT_MAPPING
Definition: HDEVIO.h:189
string HexStr(uint32_t v)
Definition: HDEVIO.h:250
void swap_block(uint32_t *inbuff, uint32_t len, uint32_t *outbuff)
Definition: HDEVIO.h:293
uint64_t _gcount
Definition: HDEVIO.h:172
uint32_t * fbuff_end
Definition: HDEVIO.h:169
uint32_t trigger_bank_segment_header
Definition: HDEVIO.h:93
uint64_t first_event
Definition: HDEVIO.h:159
uint32_t ts_header
Definition: HDEVIO.h:81
int VERBOSE
Definition: HDEVIO.h:187
streampos NB_next_pos
Definition: HDEVIO.h:235
vector< EVIOEventRecord > evio_events
Definition: HDEVIO.h:158
uint32_t GetEventMask(void)
Definition: HDEVIO.h:217
vector< EVIOBlockRecord >::iterator sparse_block_iter
Definition: HDEVIO.h:232
uint64_t first_event
Definition: HDEVIO.h:148
uint32_t headerlen
Definition: HDEVIO.h:109
#define swap16(x)
Definition: HDEVIO.h:41
EPICSHEADER_t epics
Definition: HDEVIO.h:122
void SetErrorMessage(string mess)
Definition: HDEVIO.h:238
uint32_t blocknum
Definition: HDEVIO.h:108
uint32_t swap_segment(uint32_t *outbuff, uint32_t *inbuff, uint32_t len)
Definition: swap_bank.cc:152
uint32_t event_header
Definition: HDEVIO.h:147
BORHEADER_t bor
Definition: HDEVIO.h:123
stringstream err_mess
Definition: HDEVIO.h:191
uint32_t buff_len
Definition: HDEVIO.h:179
uint32_t * buff_end
Definition: HDEVIO.h:177
#define swap32(x)
Definition: HDEVIO.h:36
uint32_t event_type_mask
Definition: HDEVIO.h:225
uint32_t last_event_len
Definition: HDEVIO.h:184
CDAQPHYSICSHEADER_t cdaqphysics
Definition: HDEVIO.h:139
uint32_t reserved1
Definition: HDEVIO.h:111
uint32_t * next
Definition: HDEVIO.h:176
void ClearErrorMessage(void)
Definition: HDEVIO.h:237
string filename
Definition: HDEVIO.h:164
Definition: HDEVIO.h:45
uint64_t Nblocks
Definition: HDEVIO.h:194
string HexStr(uint16_t v)
Definition: HDEVIO.h:261
uint32_t roc1_trigger_bank_segment_header
Definition: HDEVIO.h:101
streamsize buff_gcount() const
Definition: HDEVIO.h:242
uint32_t first_crate_len
Definition: HDEVIO.h:86
uint64_t fbuff_size
Definition: HDEVIO.h:170
uint64_t total_size_bytes
Definition: HDEVIO.h:228
uint64_t Nbad_blocks
Definition: HDEVIO.h:197
uint64_t Nbad_events
Definition: HDEVIO.h:198
BLOCKTYPE
Definition: HDEVIO.h:65
BLOCKTYPE block_type
Definition: HDEVIO.h:161
EPICSHEADER_t epics
Definition: HDEVIO.h:136
uint32_t first_crate_header
Definition: HDEVIO.h:87
uint32_t event_len
Definition: HDEVIO.h:131
uint64_t fbuff_len
Definition: HDEVIO.h:171
uint32_t first_event_hi
Definition: HDEVIO.h:94
uint32_t buff_limit
Definition: HDEVIO.h:180
uint32_t trigger_bank_header
Definition: HDEVIO.h:92
bool IGNORE_EMPTY_BOR
Definition: HDEVIO.h:188
EVIOBlockRecord NB_block_record
Definition: HDEVIO.h:234
uint32_t * fbuff
Definition: HDEVIO.h:167
bool swap_needed
Definition: HDEVIO.h:181
BORHEADER_t bor
Definition: HDEVIO.h:137
uint64_t Nerrors
Definition: HDEVIO.h:196
uint32_t buff_size
Definition: HDEVIO.h:178
void swap_block(uint16_t *inbuff, uint16_t len, uint16_t *outbuff)
Definition: HDEVIO.h:283
CDAQPHYSICSHEADER_t cdaqphysics
Definition: HDEVIO.h:125
uint64_t Nevents
Definition: HDEVIO.h:195
void swap_block(uint64_t *inbuff, uint64_t len, uint64_t *outbuff)
Definition: HDEVIO.h:303
uint32_t * buff
Definition: HDEVIO.h:175
uint32_t bitinfo
Definition: HDEVIO.h:112
uint32_t event_len
Definition: HDEVIO.h:117