| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

RawDataReader.cc

Go to the documentation of this file.
00001 #include "RawData/RawDataReader.h"
00002 #include "RawData/RawRecord.h"
00003 #include "RawData/RawRecordFormat.h"
00004 #include "RawData/RawData.h"
00005 #include "RawData/RawDataFormat.h"
00006 #include "RawData/IFStream.h"
00007 #include "RawData/RRawStream.h"
00008 
00009 #include <fstream>
00010 #include <iostream>
00011 
00012 #include <cstdlib> 
00013 
00014 DayaBay::RawDataReader::RawDataReader()
00015   : m_input(0),
00016     m_buffer(0),
00017     m_size(0),
00018     m_version(0)
00019 {
00020   // Default constructor
00021 }
00022 
00023 DayaBay::RawDataReader::~RawDataReader()
00024 {
00025   // Destructor
00026   if(m_buffer) free(m_buffer);
00027   m_buffer=0;
00028   this->close();
00029   m_size=0;
00030   m_version=0;
00031 }
00032 
00033 bool DayaBay::RawDataReader::open(const char* filename)
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 }
00056 
00057 void DayaBay::RawDataReader::close()
00058 {
00059   if (m_input) delete m_input;
00060   m_input = 0;
00061 }
00062 
00063 bool DayaBay::RawDataReader::read(unsigned int size,
00064                                   unsigned int offset /*=0*/)
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 }
00095 
00096 DayaBay::RawRecord* DayaBay::RawDataReader::nextRecord()
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 }
00143 
00144 DayaBay::RawData* DayaBay::RawDataReader::nextData()
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 }
00162 
00163 DayaBay::RawEvent* DayaBay::RawDataReader::nextEvent()
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 }
00176 
00178 
00179 bool DayaBay::RawDataReader::processFileStart(){
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 }
00186 
00187 bool DayaBay::RawDataReader::processDataSeparator(){
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 }
00194 
00195 
00196 // Local Variables: **
00197 // c-basic-offset:2 **
00198 // indent-tabs-mode:nil **
00199 // End: **
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Mon Apr 11 20:07:27 2011 for RawData by doxygen 1.4.7