#include <RawDataReader.h>
Collaboration diagram for DayaBay::RawDataReader:
Public Member Functions | |
RawDataReader () | |
~RawDataReader () | |
bool | open (const char *filename) |
Open new file, any previously open file will be closed. | |
void | close () |
explicitly close current file, if any | |
DayaBay::RawRecord * | nextRecord () |
navigate data | |
DayaBay::RawData * | nextData () |
DayaBay::RawEvent * | nextEvent () |
Private Member Functions | |
bool | read (unsigned int size, unsigned int offset=0) |
void | checkFileHeader () |
bool | processFileStart () |
bool | processDataSeparator () |
Private Attributes | |
IInputStream * | m_input |
unsigned int * | m_buffer |
unsigned int | m_size |
unsigned int | m_version |
Definition at line 20 of file RawDataReader.h.
DayaBay::RawDataReader::RawDataReader | ( | ) |
DayaBay::RawDataReader::~RawDataReader | ( | ) |
bool DayaBay::RawDataReader::open | ( | const char * | filename | ) |
Open new file, any previously open file will be closed.
Definition at line 33 of file RawDataReader.cc.
00034 { 00035 // Open the file for reading 00036 // Return true on success, false on failure 00037 string fname = filename; 00038 string ext = fname.substr(fname.size()-5,5); 00039 00040 IInputStream *input = 0; 00041 00042 if (ext == ".data") { 00043 input = new IFStream(filename); 00044 } 00045 00046 if (ext == ".rraw") { 00047 input = new RRawStream(filename); 00048 } 00049 00050 if (!input || !input->is_open()) return false; 00051 this->close(); 00052 m_input = input; 00053 00054 return true; 00055 }
void DayaBay::RawDataReader::close | ( | ) |
DayaBay::RawRecord * DayaBay::RawDataReader::nextRecord | ( | ) |
navigate data
Definition at line 96 of file RawDataReader.cc.
00097 { 00098 // Read the next record from the input stream 00099 00100 // First read type, size information 00101 bool readOk = this->read(RawRecord::minimumSize()); 00102 if( !readOk ){ 00103 std::cerr << "DayaBay::RawDataReader::nextRecord(): " 00104 << "Failed to read start of next record." << std::endl; 00105 return 0; 00106 } 00107 DayaBay::RawRecord record(m_buffer); 00108 // Check block size 00109 if(record.size() > record.minimumSize() ){ 00110 // Read the rest of this data block 00111 readOk = this->read(record.size() 00112 -RawRecord::minimumSize(), 00113 RawRecord::minimumSize()); 00114 if( !readOk ){ 00115 std::cerr << "DayaBay::RawDataReader::nextRecord(): " 00116 << "Failed to complete read of next record." << std::endl; 00117 return 0; 00118 } 00119 } 00120 00121 DayaBay::RawRecord* fullRecord = 00122 DayaBay::RawRecord::makeNewRecord(m_buffer, m_version); 00123 00124 // Additional actions for specific data records 00125 if( fullRecord->isFileStart() ){ 00126 // Process file start information 00127 bool ok = this->processFileStart(); 00128 if( !ok ){ 00129 delete fullRecord; 00130 return 0; 00131 } 00132 }else if( fullRecord->isDataSeparator() ){ 00133 // Process data separators 00134 bool ok = this->processDataSeparator(); 00135 if( !ok ){ 00136 delete fullRecord; 00137 return 0; 00138 } 00139 } 00140 00141 return fullRecord; 00142 }
DayaBay::RawData * DayaBay::RawDataReader::nextData | ( | ) |
Definition at line 144 of file RawDataReader.cc.
00145 { 00146 // Read until the next data payload is found, and return it 00147 DayaBay::RawData* data = 0; 00148 while(!data){ 00149 DayaBay::RawRecord* record = this->nextRecord(); 00150 if( !record ) return 0; 00151 if( record->isDataSeparator() ){ 00152 // Found a data payload 00153 DayaBay::RawDataSeparator* dataSeparator = 00154 dynamic_cast<DayaBay::RawDataSeparator*>(record); 00155 if( !dataSeparator ) return 0; 00156 data = dataSeparator->data(); 00157 } 00158 delete record; 00159 } 00160 return data; 00161 }
DayaBay::RawEvent * DayaBay::RawDataReader::nextEvent | ( | ) |
Definition at line 163 of file RawDataReader.cc.
00164 { 00165 // Read until the next event data is found, and return it 00166 DayaBay::RawData* data = 0; 00167 while( (data = this->nextData()) ){ 00168 if( data->isEventType() ){ 00169 // Found an event data payload 00170 return dynamic_cast<DayaBay::RawEvent*>(data); 00171 } 00172 delete data; 00173 } 00174 return 0; 00175 }
bool DayaBay::RawDataReader::read | ( | unsigned int | size, | |
unsigned int | offset = 0 | |||
) | [private] |
Definition at line 63 of file RawDataReader.cc.
00065 { 00066 // Read data from stream into buffer 00067 // size = size of data to read (in integer-size blocks) 00068 // offset = size of data to read (in integer-size blocks) 00069 00070 if(!m_input) return false; 00071 00072 if(!m_buffer){ 00073 // Initialize buffer 00074 m_size = 0; 00075 static const unsigned int minBufferSize = 16000; 00076 unsigned int bufSize = minBufferSize; 00077 if( bufSize < size+offset ) bufSize = size+offset; 00078 m_buffer = new unsigned int[bufSize]; 00079 if(!m_buffer) return false; 00080 m_size = bufSize; 00081 }else if(m_size < (size+offset)){ 00082 // Increase buffer size 00083 m_size = 0; 00084 m_buffer = (unsigned int*)realloc(m_buffer, 00085 (size+offset)*sizeof(unsigned int)); 00086 if(!m_buffer) return false; 00087 m_size = size+offset; 00088 } 00089 00090 // Read input data into buffer 00091 m_input->read( (char*)(m_buffer+offset), size*sizeof(unsigned int) ); 00092 if(!m_input->good()) return false; 00093 return true; 00094 }
void DayaBay::RawDataReader::checkFileHeader | ( | ) | [private] |
bool DayaBay::RawDataReader::processFileStart | ( | ) | [private] |
Definition at line 179 of file RawDataReader.cc.
00179 { 00180 // Catch the file version and keep track of it in the reader 00181 // Pass this information on to other data records 00182 DayaBay::RawFileStart fileStart(m_buffer); 00183 m_version = fileStart.version(); 00184 return true; 00185 }
bool DayaBay::RawDataReader::processDataSeparator | ( | ) | [private] |
Definition at line 187 of file RawDataReader.cc.
00187 { 00188 // Catch data separators, and read payload data block as well 00189 DayaBay::RawDataSeparator dataSeparator(m_buffer, m_version); 00190 unsigned int bytes = dataSeparator.blockSize(); 00191 unsigned int lines = (bytes+sizeof(unsigned int)-1)/sizeof(unsigned int); 00192 return this->read(lines /*size*/, dataSeparator.size() /*offset*/); 00193 }
IInputStream* DayaBay::RawDataReader::m_input [private] |
Definition at line 40 of file RawDataReader.h.
unsigned int* DayaBay::RawDataReader::m_buffer [private] |
Definition at line 41 of file RawDataReader.h.
unsigned int DayaBay::RawDataReader::m_size [private] |
Definition at line 42 of file RawDataReader.h.
unsigned int DayaBay::RawDataReader::m_version [private] |
Definition at line 43 of file RawDataReader.h.