RawFileParser.h
Go to the documentation of this file.
1 #ifndef RAWFILEPARSER_H
2 #define RAWFILEPARSER_H
3 
4 #include <cstdlib>
5 #include <cstdio>
6 extern "C" {
7 #include <fcntl.h>
8 #include <stdint.h>
9 #include <sys/stat.h>
10 #include <sys/types.h>
11 #include <unistd.h>
12 }
13 
14 #include <vector>
15 
16 namespace daqdataformats {
17  class RawRunHeader;
18  class RawConfigurationBlock;
19  class RawEvent;
20 }
21 
22 namespace rawfileparser {
23 
24  class RawFileParser {
25 
26  public:
27 
28  // Class Enums and Static data
30  NO_FILE = 0,
31  NOVA_RUNFILE = 1,
35  };
36 
37  // NOVA File magic numbers
39  NOVA_ASCII = 0x41764F4E,
40  E929_ASCII = 0x39323945,
41  EVENT_MARKER = 0xE929E929,
42  TAIL_MARKER = 0x4c494154
43  };
44 
46  NO_INDEX = -1,
47  PARTIAL_INDEX = 0,
48  FULL_INDEX = 1
49  };
50 
51  static const char* filetypes[ ];
52 
53  // Constructors
54 
55  RawFileParser(); //! Default constructor
56  RawFileParser(const char* filename); //! Constructor with input filename (file is openned)
57 
58  ~RawFileParser(); //! Destructor
59 
60  void Init(); //! General initialization and reinitialization
61 
62  int open(const char* filename); //! Open a file for parsing
63  int close(); //! Close any currently open file
64 
65  void setDebug(bool f){debug_flag = f;}; //! Set the debug level on or off
66  void setMemoryMappedIO(bool m){memmap_io_flag = m;}; //! Set the access mode for IO calls (Memory Mapped or system read)
67  bool isMemoryMappedIO(){return memmap_io_flag;}; //! Obtain the current IO access mode
68 
69  int CheckFileType(); //! Attempt to determine the type of file that is open
70  int getFileType(); //! Return the type of file that is open
71 
72  // Indexing method
73  int BuildEventIndex(int max_idx=-1, bool force_rebuilt = false); //! Method that builds a full event index
74 
75  // Data Retrieval methods
76  int getRunHeader(daqdataformats::RawRunHeader& theRunHeader); //! Retrieve the run header from the file
77  int getConfigBlock(daqdataformats::RawConfigurationBlock& theConfigBlock); //! Retrieve the configuration block from the file
78  int getFirstEvent(daqdataformats::RawEvent& theEvent); //! Retrieve the first event from the file
79  int getNextEvent(daqdataformats::RawEvent& theEvent); //! Retrieve the next event from the file
80  int getEvent(daqdataformats::RawEvent& theEvent); //! Retrieve the next event from the file
81  int getEvent(daqdataformats::RawEvent& theEvent, int event_no); //! Retrieve the Nth event from the file
82  int getPreviousEvent(daqdataformats::RawEvent& theEvent); //! Retrieve the previous event from the file
83  int getRunTail(daqdataformats::RawRunHeader& theRunTail); //! Retrieve the run header from the file
84 
85  // AdvanceToXXX Function Declarations
86  int AdvanceToRunHeader (off_t& header_pos); //! Moves the current parse point to the Run header
87  int AdvanceToConfigStart (off_t& conf_start_pos); //! Moves the current parse point to the Config Block start
88  int AdvanceToFirstEvent (off_t& first_event_pos, bool index_entries=false); //! Moves the current parse point to the First event in the file
89  int AdvanceToNextEvent (off_t& next_event_pos, bool index_entries=false); //! Moves the current parse point to the Next event in the file
90  int AdvanceToPreviousEvent (off_t& next_event_pos, bool index_entries=false); //! Moves the current parse point to the Previous event in the file
91  int AdvanceToEvent (off_t& event_pos, bool index_entries=false); //! Moves to the most appropriate event in the file
92  int AdvanceToConfigEnd (off_t& conf_end_pos); //! Moves to the end of the configuration block
93  int AdvanceToRunTail (off_t& tail_start_pos); //! Moves to the start of the run tail block
94 
95  int GotoEvent(int event_no); //! Direct access method for events
96 
97  void* getFileStart(){return mmfile_start;};
98  uint8_t* getFileStart8(){return mmfile_start8;};
99  uint16_t* getFileStart16(){return mmfile_start16;};
100  uint32_t* getFileStart32(){return mmfile_start32;};
101 
102  void* getFileEnd() {return mmfile_end;};
103  uint8_t* getFileEnd8() {return mmfile_end8;};
104  uint16_t* getFileEnd16(){return mmfile_end16;};
105  uint32_t* getFileEnd32(){return mmfile_end32;};
106 
107  void* getCurrentBuffer() {return mmfile_current;}; //! Return the current parse position address
108  uint8_t* getCurrentBuffer8() {return mmfile_current8;}; //! Return the current parse position address as an unsigned 8bit char*
109  uint16_t* getCurrentBuffer16(){return mmfile_current16;}; //! Return the current parse position address as an unsigned 16bit int*
110  uint32_t* getCurrentBuffer32(){return mmfile_current32;}; //! Return the current parse position address as an unsigned 32bit int*
111 
112  // Verification methods for buffers
113  bool checkRunHeader(void* buff); //! Check if the buffer is a valid Run Header
114  bool checkConfigStart(void* buff); //! Check if the buffer is a valid Run configuration block
115  bool checkEventHeader(void* buff); //! Check if the buffer is a valid Event Header
116  bool checkEvent(void* buff); //! Check if the buffer is a valid Event
117  bool checkRunTail(void* buff); //! Check if the buffer is a valid Run Tail
118 
119  // Buffer Utilities
120  void dumpBuffer(int bytes=128, int col=4); //! Perform a hex dump of the current position extending X bytes in N columns
121  void dumpBuffer(void* buff, int bytes, int col=4); //! Perform a hex dump of the memory pointed to by buff, extending X bytes in N columns
122 
123  int getFilefd(){return infile;}; //! Return the raw file descriptor of the file that has been opened by the parser
124 
125  // File IO utility functions
126  off_t backup(size_t bytes) {return lseek(infile,-1*bytes, SEEK_CUR);};
127  off_t forward(size_t bytes){return lseek(infile,bytes, SEEK_CUR);};
128  off_t rewind(off_t position){return lseek(infile, position, SEEK_SET);};
129  off_t savepos(off_t& position);
130  off_t curpos(){return lseek(infile, 0, SEEK_CUR);}
131 
132  protected:
133 
134  int wordsearch(unsigned int searchword); //! Search for the given 32bit word in the file
135  int wordsearchreverse(unsigned int searchword); //! Search for the given 32bit word in the file
136 
137  int CheckFileType_mem(); //! File type checking for memory mapped IO
138  int CheckFileType_file(); //! File type checking for raw file IO
139 
140  int checkWord(unsigned int testword); //! Check if the word currently pointed to in the file matches the test word
141  int checkWord_mem(unsigned int testword); //! Memory mapped IO version
142  int checkWord_file(unsigned int testword); //! Raw file IO version
143 
144  int GotoEvent_mem(int event_no); //! Direct access method for events
145  int GotoEvent_file(int event_no); //! Direct access method for events
146 
147  int BuildEventIndex_mem(int max_idx=-1, bool force_rebuilt = false); //! Method that builds a full event index
148  int BuildEventIndex_file(int max_idx=-1, bool force_rebuilt = false); //! Method that builds a full event index
149 
150  int AdvanceToRunHeader_mem (off_t& header_pos);
151  int AdvanceToRunHeader_file (off_t& header_pos);
152 
153  int AdvanceToConfigStart_mem (off_t& conf_start_pos);
154  int AdvanceToConfigStart_file (off_t& conf_start_pos);
155 
156  int AdvanceToFirstEvent_mem (off_t& first_event_pos, bool index_entries=false);
157  int AdvanceToFirstEvent_file (off_t& first_event_pos, bool index_entries=false);
158 
159  int AdvanceToNextEvent_mem (off_t& next_event_pos, bool index_entries=false);
160  int AdvanceToNextEvent_file(off_t& next_event_pos, bool index_entries=false);
161 
162  int AdvanceToPreviousEvent_mem (off_t& next_event_pos, bool index_entries=false);
163 
164  int AdvanceToRunTail_mem (off_t& tail_start_pos);
165  int AdvanceToRunTail_file (off_t& tail_start_pos);
166 
167  ssize_t readEventSize (bool freemem_flag=false);
168  ssize_t readEventSize_mem (bool freemem_flag=false);
169  ssize_t readEventSize_file(bool freemem_flag=false);
170 
171  int wordsearch_mem(unsigned int searchword);
172  int wordsearch_file(unsigned int searchword);
173 
174  int wordsearchreverse_mem(unsigned int searchword);
175 
176  private:
177 
178  // Private and nimplemented to prevent copy.
180  RawFileParser & operator=(RawFileParser const &other);
181 
182  bool fileopenned;
183  int infile;
184  char* infilename;
185  int filetype;
187 
189 
190  // Memory Mapped IO pointers for the file
191 
192  union {
194  uint8_t* mmfile_start8;
195  uint16_t* mmfile_start16;
196  uint32_t* mmfile_start32;
197  };
198 
199  union {
200  void* mmfile_end;
201  uint8_t* mmfile_end8;
202  uint16_t* mmfile_end16;
203  uint32_t* mmfile_end32;
204  };
205 
206  union {
208  uint8_t* mmfile_current8;
209  uint16_t* mmfile_current16;
210  uint32_t* mmfile_current32;
211  };
212 
213  // Memory Mapped IO pointers for the individual parts
214  // of the data file
220 
221  // File offsets for old fashion read() based file IO
227 
229 
230  // Event index
234  std::vector <off_t> event_offset_list;
235  std::vector <uint32_t*> event_addr_list;
236 
238 
239  };
240 
241 } // endnamespace rawfileparser
242 #endif /* RAWFILEPARSER_H */
void setDebug(bool f)
Close any currently open file.
Definition: RawFileParser.h:65
off_t forward(size_t bytes)
std::vector< uint32_t * > event_addr_list
string filename
Definition: shutoffs.py:106
bool isMemoryMappedIO()
Set the access mode for IO calls (Memory Mapped or system read)
Definition: RawFileParser.h:67
std::vector< off_t > event_offset_list
uint16_t * getCurrentBuffer16()
Return the current parse position address as an unsigned 8bit char*.
string infile
int getFilefd()
Perform a hex dump of the memory pointed to by buff, extending X bytes in N columns.
Int_t col[ntarg]
Definition: Style.C:29
void Init(void)
Definition: gXSecComp.cxx:138
#define off_t
Definition: macconfig.h:47
procfile open("FD_BRL_v0.txt")
uint32_t * getCurrentBuffer32()
Return the current parse position address as an unsigned 16bit int*.
uint8_t * getCurrentBuffer8()
Return the current parse position address.
Class to hold the data from the FEBs in correct formats.
void * getFileStart()
Direct access method for events.
Definition: RawFileParser.h:97
off_t rewind(off_t position)
off_t backup(size_t bytes)
Return the raw file descriptor of the file that has been opened by the parser.
procfile close()
void setMemoryMappedIO(bool m)
Set the debug level on or off.
Definition: RawFileParser.h:66