12 #include "DAQDataFormats/DAQDataFormats.h" 13 #include "DAQDataFormats/RawRun.h" 14 #include "DAQDataFormats/RawRunHeader.h" 15 #include "DAQDataFormats/RawEvent.h" 16 #include "DAQDataFormats/RawEventHeader.h" 17 #include "DAQDataFormats/RawTrigger.h" 18 #include "DAQDataFormats/RawMicroBlock.h" 19 #include "DAQDataFormats/RawNanoSlice.h" 20 #include "DAQDataFormats/RawConfigurationBlock.h" 22 #include "RawFileParser/RawFileParser.h" 24 #pragma GCC diagnostic ignored "-Wformat=" 41 #define DEBUGMSG(...) if(debug_flag){fprintf(stderr, __VA_ARGS__);} 48 "NOvA File, Corrupted",
64 config_block_start(-1),
68 current_event_index(-1),
100 this->
open(filename);
151 struct stat statbuff;
158 infilename = (
char*)malloc(strlen(filename)+1);
173 if(fstat(
infile,&statbuff) < 0){
174 perror(
"fstat error");
179 if( statbuff.st_size < 1){
181 printf(
"Empty File. Returned size: %Zd\n",statbuff.st_size);
182 DEBUGMSG(
"Empty File. Returned size: %Zd\n",statbuff.st_size);
197 (
mmfile_start = mmap(NULL, statbuff.st_size, PROT_READ, MAP_SHARED,
infile, 0)) == (caddr_t) -1){
198 perror(
"mmap error on file");
219 struct stat statbuff;
227 if(fstat(
infile, &statbuff) < 0){
228 perror(
"fstat error");
231 if( (ret = munmap(
mmfile_start, statbuff.st_size)) < 0){
232 perror(
"Error unmapping file from memory");
248 perror(
"File Close");
273 DEBUGMSG(
"NOVA Run File detected\n");
277 DEBUGMSG(
"Corrupt Run File detected\n");
285 DEBUGMSG(
"NOVA Event File detected\n");
288 DEBUGMSG(
"Unrecognized NOvA File type\n");
298 unsigned int buff[4]={0,0,0,0};
312 bytes_read = ::read(
infile, &buff, 16);
322 DEBUGMSG(
"NOVA Run File detected\n");
326 DEBUGMSG(
"Corrupt Run File detected\n");
334 DEBUGMSG(
"NOVA Event File detected\n");
337 DEBUGMSG(
"Unrecognized NOvA File type\n");
397 bool found_block=
false;
406 if(
checkWord(theHeader.getMarkerLo()) > 0){found_block=
true;};
410 DEBUGMSG(
"End of file reached during run file header block start search, no header block found\n");
416 DEBUGMSG(
"Read from file failed during run header block search");
420 DEBUGMSG(
"Unknown error in wordsearch(0x%08x)\n",theHeader.getMarkerHi());
447 DEBUGMSG(
"AdvanceToRunHeader(): File not openned for parsing\n");
455 DEBUGMSG(
"Unable to advance to cache position of config block start\n");
467 DEBUGMSG(
"AdvanceToRunHeader(): unable to save file position\n");
475 bool found_block=
false;
484 if(
checkWord(theHeader.getMarkerLo()) > 0){found_block=
true;};
485 backup(
sizeof(
unsigned int));
489 DEBUGMSG(
"End of file reached during run file header block start search, no header block found\n");
495 DEBUGMSG(
"Read from file failed during run header block search");
499 DEBUGMSG(
"Unknown error in wordsearch(0x%08x)\n",theHeader.getMarkerHi());
553 bool found_block=
false;
556 ret =
wordsearch(theConfigHeader.getMarkerHi1());
561 if(
checkWord(theConfigHeader.getMarkerHi2()) > 0){found_block=
true;};
565 DEBUGMSG(
"End of file reached during config block start search, no config block found\n");
572 DEBUGMSG(
"Read from file failed during config block start search");
576 DEBUGMSG(
"Unknown error in wordsearch(0x%08x)\n",theConfigHeader.getMarkerHi1());
602 DEBUGMSG(
"AdvanceToConfigStart(): File not openned for parsing\n");
610 DEBUGMSG(
"Unable to advance to cache position of config block start\n");
622 DEBUGMSG(
"AdvanceToRunStart(): unable to save file position\n");
626 bool found_block=
false;
629 ret =
wordsearch(theConfigHeader.getMarkerHi1());
635 if(
checkWord(theConfigHeader.getMarkerHi2()) > 0){found_block=
true;};
636 backup(
sizeof(
unsigned int));
640 DEBUGMSG(
"End of file reached during config block start search, no config block found\n");
646 DEBUGMSG(
"Read from file failed during config block start search");
650 DEBUGMSG(
"Unknown error in wordsearch(0x%08x)\n",theConfigHeader.getMarkerHi1());
679 DEBUGMSG(
"AdvanceToConfigEnd(): File not openned for parsing\n");
687 DEBUGMSG(
"Unable to advance to cache position of config block tail\n");
699 DEBUGMSG(
"AdvanceToConfigEnd(): unable to save file position\n");
703 bool found_block=
false;
706 ret =
wordsearch(theConfigTail.getMarkerLo1());
712 if(
checkWord(theConfigTail.getMarkerLo2()) > 0){found_block=
true;};
713 backup(
sizeof(
unsigned int));
717 DEBUGMSG(
"End of file reached during config block end search, no config tail found\n");
723 DEBUGMSG(
"Read from file failed during config block tail search");
727 DEBUGMSG(
"Unknown error in wordsearch(0x%08x)\n",theConfigTail.getMarkerLo1());
788 DEBUGMSG(
"End of file reached during run tail search, no tail found\n");
794 DEBUGMSG(
"Read from file failed during run tail search");
820 DEBUGMSG(
"AdvanceToRunTail(): File not openned for parsing\n");
828 DEBUGMSG(
"Unable to advance to cache position of run tail\n");
840 DEBUGMSG(
"AdvanceToRunTail(): unable to save file position\n");
844 bool found_block=
false;
854 backup(
sizeof(
unsigned int));
858 DEBUGMSG(
"End of file reached during run tail search, no tail found\n");
864 DEBUGMSG(
"Read from file failed during run tail search");
927 DEBUGMSG(
"End of file reached during first event search, no event block found\n");
933 DEBUGMSG(
"Read from file failed during event header search");
948 std::vector<uint32_t*>::iterator it_addr;
952 std::vector<off_t>::iterator it_offset;
967 DEBUGMSG(
"AdvanceToFirstEvent(): File not openned for parsing\n");
974 DEBUGMSG(
"Using Cached position of first event\n");
976 DEBUGMSG(
"Unable to advance to cache position of first event\n");
988 DEBUGMSG(
"wordsearch(): unable to save file position\n");
1007 DEBUGMSG(
"End of file reached during first event search, no event block found\n");
1013 DEBUGMSG(
"Read from file failed during event header search");
1055 uint32_t* start_pos;
1076 DEBUGMSG(
"trying reverse direct...");
1083 DEBUGMSG(
"AdvanceToPreviousEvent(): End of file reached during event search, no event block found\n");
1089 DEBUGMSG(
"AdvanceToPreviousEvent(): Read from file failed during event header search");
1132 uint32_t* start_pos;
1156 DEBUGMSG(
"AdvanceToNextEvent(): End of file reached during event search, no event block found\n");
1162 DEBUGMSG(
"AdvanceToNextEvent(): Read from file failed during event header search");
1184 DEBUGMSG(
"Unable to retrieve event size in AdvanceToNextEvent()\n");
1193 DEBUGMSG(
"Error in advancing to next event in AdvanceToNextEvent()\n");
1197 next_event_pos =
curpos();
1212 DEBUGMSG(
"AdvanceToNextEvent(): End of file reached during event search, no event block found\n");
1218 DEBUGMSG(
"AdvanceToNextEvent(): Read from file failed during event header search");
1246 next_event_pos =
pos;
1259 DEBUGMSG(
"AdvanceToNextEvent(): File not openned for parsing\n");
1265 DEBUGMSG(
"AdvanceToNextEvent(): unable to save file position\n");
1286 DEBUGMSG(
"AdvanceToNextEvent(): End of file reached during event search, no event block found\n");
1292 DEBUGMSG(
"AdvanceToNextEvent(): Read from file failed during event header search");
1310 next_event_pos =
curpos();
1322 DEBUGMSG(
"Unable to retrieve event size in AdvanceToNextEvent()\n");
1328 perror(
"Unable to advance file in AdvanceToNextEvent()");
1333 DEBUGMSG(
"Error in advancing to next event in AdvanceToNextEvent()\n");
1337 next_event_pos =
curpos();
1351 DEBUGMSG(
"AdvanceToNextEvent(): File not openned for parsing\n");
1394 static int recursion_depth=0;
1397 if(recursion_depth > 2){
return FAIL;}
1402 recursion_depth = 0;
1405 DEBUGMSG(
"GotoEvent_mem(): Requested index exceeds maximum event in file\n");
1406 recursion_depth = 0;
1415 recursion_depth = 0;
1425 DEBUGMSG(
"GotoEvent(): File not openned for parsing\n");
1436 DEBUGMSG(
"Error going to event: %d under current index\n",event_no);
1444 DEBUGMSG(
"Requested event number: %d, exceeds maximum number of events in index\n",event_no);
1454 for(
int num_events=0; num_events<event_no; ++num_events){
1456 if(ret ==
TRUE){
continue;}
1491 for(
int num_events=
event_offset_list.size(); num_events<event_no; ++num_events){
1493 if(ret ==
TRUE){
continue;}
1553 if(ret > 0){num_events++;}
1564 off_t current_event_position;
1602 DEBUGMSG(
"Error Building event index at event %d\n",num_events+1);
1609 if(num_events > max_idx){
return TRUE;}
1640 return theHeader.getEventSize();
1645 uint32_t event_size;
1646 static unsigned char* header_buff=NULL;
1651 if(header_buff == NULL){
1652 header_buff = (
unsigned char*) malloc(theHeader.sizeofdata()*
sizeof(uint32_t));
1657 bytes_read = ::read(
infile, header_buff, theHeader.sizeofdata()*
sizeof(uint32_t));
1662 perror(
"File read error during event header read");
1666 if(bytes_read == 0){
1668 DEBUGMSG(
"End of file reached during event header read");
1673 if(bytes_read != (
int)(theHeader.sizeofdata()*
sizeof(uint32_t))){
1674 perror(
"Incomplete read encountered while reading event header");
1679 theHeader.setBufferSource(header_buff);
1682 event_size = theHeader.getEventSize();
1690 if(header_buff != NULL){
1696 return (event_size)*(
sizeof(uint32_t));
1723 uint32_t* start_pos;
1724 bool notfound =
true;
1735 DEBUGMSG(
"End of file reached, no event block found\n");
1755 uint32_t* start_pos;
1756 bool notfound =
true;
1763 DEBUGMSG(
"End of file reached, no event block found\n");
1786 DEBUGMSG(
"wordsearch(): File not openned for parsing\n");
1793 DEBUGMSG(
"wordsearch(): unable to save file position\n");
1797 int bytes_read = -1;
1798 unsigned int buff = 0;
1800 if(buff == searchword) buff = searchword + 1;
1803 while(buff != searchword){
1804 bytes_read = ::read(
infile, &buff,
sizeof(
int));
1809 perror(
"Read from file failed during event header search");
1814 DEBUGMSG(
"End of file reached, no event block found\n");
1832 if(
backup(bytes_read) < 0 ){
1833 DEBUGMSG(
"Unable to rewind file in wordsearch()\n");
1870 bytes_read = ::read(
infile, &buff,
sizeof(
unsigned int));
1875 perror(
"Read Failed during checkWord()");
1879 DEBUGMSG(
"End of file reached during checkWord()\n");
1889 if(
backup(bytes_read) < 0){
1890 perror(
"Unable to rewind file in checkWord()");
1894 if( buff == testword){
1903 position = lseek(
infile, 0, SEEK_CUR);
1906 perror(
"Unable to save file position, savepos()");
1922 words = bytes/
sizeof(
int);
1923 for(i=0; i<words; ++
i){
1925 if(((i+1)%col)==0){
DEBUGMSG(
"\n");}
1937 buff32 = (
int*)buff;
1938 words = bytes/
sizeof(
int);
1939 for(i=0; i<words; ++
i){
1940 printf(
"0x%08x ",buff32[i]);
1941 if(((i+1)%col)==0){
DEBUGMSG(
"\n");}
1952 buff32 = (uint32_t*)buff;
1966 buff32 = (uint32_t*)buff;
1968 if( (buff32[0]==theConfigHeader.getMarkerHi1()) && (buff32[1]==theConfigHeader.getMarkerHi2())){
1978 buff32 = (uint32_t*)buff;
1990 buff32 = (uint32_t*)buff;
2002 buff32 = (uint32_t*)buff;
2127 s = tmpEvent.getEventSize();
int wordsearch(unsigned int searchword)
int open(const char *filename)
General initialization and reinitialization.
int AdvanceToConfigStart_file(off_t &conf_start_pos)
int AdvanceToNextEvent_file(off_t &next_event_pos, bool index_entries=false)
uint32_t * mmfile_start32
int BuildEventIndex(int max_idx=-1, bool force_rebuilt=false)
Return the type of file that is open.
int AdvanceToNextEvent_mem(off_t &next_event_pos, bool index_entries=false)
uint32_t * mmfile_current32
int checkWord_file(unsigned int testword)
Memory mapped IO version.
int AdvanceToRunHeader_mem(off_t &header_pos)
Method that builds a full event index.
int AdvanceToRunTail(off_t &tail_start_pos)
Moves to the end of the configuration block.
off_t forward(size_t bytes)
int AdvanceToRunTail_mem(off_t &tail_start_pos)
int GotoEvent_file(int event_no)
Direct access method for events.
int GotoEvent(int event_no)
Moves to the start of the run tail block.
bool checkRunHeader(void *buff)
Return the current parse position address as an unsigned 32bit int*.
void dumpBuffer(int bytes=128, int col=4)
Check if the buffer is a valid Run Tail.
int wordsearch_mem(unsigned int searchword)
int getRunHeader(daqdataformats::RawRunHeader &theRunHeader)
Method that builds a full event index.
std::vector< uint32_t * > event_addr_list
int CheckFileType_file()
File type checking for memory mapped IO.
int AdvanceToFirstEvent(off_t &first_event_pos, bool index_entries=false)
Moves the current parse point to the Config Block start.
int AdvanceToConfigStart(off_t &conf_start_pos)
Moves the current parse point to the Run header.
int wordsearch_file(unsigned int searchword)
int getPreviousEvent(daqdataformats::RawEvent &theEvent)
Retrieve the Nth event from the file.
int checkWord(unsigned int testword)
File type checking for raw file IO.
int wordsearchreverse_mem(unsigned int searchword)
ssize_t readEventSize_mem(bool freemem_flag=false)
int CheckFileType_mem()
Search for the given 32bit word in the file.
bool isMemoryMappedIO()
Set the access mode for IO calls (Memory Mapped or system read)
int AdvanceToFirstEvent_mem(off_t &first_event_pos, bool index_entries=false)
off_t savepos(off_t &position)
int AdvanceToPreviousEvent_mem(off_t &next_event_pos, bool index_entries=false)
~RawFileParser()
Constructor with input filename (file is openned)
int AdvanceToConfigEnd(off_t &conf_end_pos)
Moves to the most appropriate event in the file.
std::vector< off_t > event_offset_list
int wordsearchreverse(unsigned int searchword)
Search for the given 32bit word in the file.
int CheckFileType()
Obtain the current IO access mode.
int GotoEvent_mem(int event_no)
Raw file IO version.
bool checkConfigStart(void *buff)
Check if the buffer is a valid Run Header.
void * config_block_end_addr
int close()
Open a file for parsing.
printf("%d Experimental points found\n", nlines)
ssize_t readEventSize(bool freemem_flag=false)
bool checkEvent(void *buff)
Check if the buffer is a valid Event Header.
void * config_block_start_addr
bool checkEventHeader(void *buff)
Check if the buffer is a valid Run configuration block.
int AdvanceToRunHeader(off_t &header_pos)
Retrieve the run header from the file.
int AdvanceToFirstEvent_file(off_t &first_event_pos, bool index_entries=false)
static const char * filetypes[]
int BuildEventIndex_mem(int max_idx=-1, bool force_rebuilt=false)
Direct access method for events.
ssize_t readEventSize_file(bool freemem_flag=false)
int checkWord_mem(unsigned int testword)
Check if the word currently pointed to in the file matches the test word.
int BuildEventIndex_file(int max_idx=-1, bool force_rebuilt=false)
Method that builds a full event index.
int AdvanceToConfigStart_mem(off_t &conf_start_pos)
int getFileType()
Attempt to determine the type of file that is open.
int getNextEvent(daqdataformats::RawEvent &theEvent)
Retrieve the first event from the file.
int getRunTail(daqdataformats::RawRunHeader &theRunTail)
Retrieve the previous event from the file.
int getEvent(daqdataformats::RawEvent &theEvent)
Retrieve the next event from the file.
int AdvanceToPreviousEvent(off_t &next_event_pos, bool index_entries=false)
Moves the current parse point to the Next event in the file.
off_t rewind(off_t position)
int getFirstEvent(daqdataformats::RawEvent &theEvent)
Retrieve the configuration block from the file.
int AdvanceToEvent(off_t &event_pos, bool index_entries=false)
Moves the current parse point to the Previous event in the file.
int AdvanceToRunTail_file(off_t &tail_start_pos)
int AdvanceToNextEvent(off_t &next_event_pos, bool index_entries=false)
Moves the current parse point to the First event in the file.
bool checkRunTail(void *buff)
Check if the buffer is a valid Event.
int AdvanceToRunHeader_file(off_t &header_pos)
int getConfigBlock(daqdataformats::RawConfigurationBlock &theConfigBlock)
Retrieve the run header from the file.
off_t backup(size_t bytes)
Return the raw file descriptor of the file that has been opened by the parser.
uint8_t * mmfile_current8