Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

RawFileReader Class Reference

#include <RawFileReader.h>

Inheritance diagram for RawFileReader:

IRawReader IRawReader List of all members.

Public Types

typedef const VFileNames_t ReaderArgType
typedef const VFileNames_t ReaderArgType

Public Member Functions

const uint32_t * currentEvent () const
const uint32_t * currentEvent () const
std::string currentFile ()
std::string currentFile ()
const uint32_t * findEventById (uint32_t evtId)
const uint32_t * findEventById (uint32_t evtId)
const uint32_t * nextEvent (int nIgnore)
const uint32_t * nextEvent ()
const uint32_t * nextEvent (int nIgnore)
const uint32_t * nextEvent ()
 RawFileReader (const VFileNames_t &fnames, const VFileNames_t &idxfnames)
 RawFileReader (const std::string &fname, const std::string &idxfname)
 RawFileReader (const VFileNames_t &fnames)
 RawFileReader (const std::string &fname)
 RawFileReader (const VFileNames_t &fnames, const VFileNames_t &idxfnames)
 RawFileReader (const std::string &fname, const std::string &idxfname)
 RawFileReader (const VFileNames_t &fnames)
 RawFileReader (const std::string &fname)
const uint32_t * roughlyNextEvent (int nIgnore, int evtByte=0)
const uint32_t * roughlyNextEvent (int nIgnore, int evtByte=0)
uint32_t stat ()
uint32_t stat ()
uint32_t tellg ()
uint32_t tellg ()
virtual ~RawFileReader ()
virtual ~RawFileReader ()

Static Public Member Functions

std::vector< int > getEventNumber (const VFileNames_t &idxfnames)
std::vector< int > getEventNumber (const VFileNames_t &idxfnames)

Private Member Functions

RawFileReadernextFile (RawFileException &e)
RawFileReadernextFile (RawFileException &e)
const uint32_t * notSafeNextEvent ()
const uint32_t * notSafeNextEvent ()
 RawFileReader ()
 RawFileReader ()
void read_one_event ()
void read_one_event ()

Private Attributes

uint32_t * m_buffer
uint32_t * m_buffer
uint32_t m_bufferSize
DataSeparatorRecord m_dataSeparatorRecord
EvtIdxHandlerm_idxHandler
EvtIdxHandlerm_idxHandler
raw_ifstreamm_rfs
raw_ifstreamm_rfs

Member Typedef Documentation

typedef const VFileNames_t RawFileReader::ReaderArgType
 

typedef const VFileNames_t RawFileReader::ReaderArgType
 


Constructor & Destructor Documentation

RawFileReader::RawFileReader const std::string &  fname  ) 
 

00039    : m_bufferSize(DefaultEventBufferSize),
00040      m_buffer( new uint32_t[DefaultEventBufferSize] ),
00041      m_idxHandler(0)
00042 {
00043    const VFileNames_t newfnames = RawFileTools::wildcard_correct(fname);
00044 
00045    m_rfs =  raw_ifstream::instance(newfnames);
00046 }

RawFileReader::RawFileReader const VFileNames_t fnames  ) 
 

00049    : m_bufferSize(DefaultEventBufferSize),
00050      m_buffer( new uint32_t[DefaultEventBufferSize] ),
00051      m_idxHandler(0)
00052 {
00053    const VFileNames_t newfnames = RawFileTools::wildcard_correct(fnames);
00054 
00055    m_rfs =  raw_ifstream::instance(newfnames);
00056 }

RawFileReader::RawFileReader const std::string &  fname,
const std::string &  idxfname
 

00059    : m_bufferSize(DefaultEventBufferSize),
00060      m_buffer( new uint32_t[DefaultEventBufferSize] )
00061 {
00062    const VFileNames_t newfnames    = RawFileTools::wildcard_correct(fname);
00063    const VFileNames_t newidxfnames = RawFileTools::wildcard_correct(idxfname);
00064 
00065    if ( newidxfnames.size() != newfnames.size() ) {
00066       std::cerr << "[RawFile] Num(IdxFiles) != Num(DataFiles)" << std::endl;
00067       exit(1);
00068    }
00069 
00070    m_rfs =  raw_ifstream::instance(newfnames);
00071    m_idxHandler = EvtIdxHandler::instance(newidxfnames);
00072 }

RawFileReader::RawFileReader const VFileNames_t fnames,
const VFileNames_t idxfnames
 

00075    : m_bufferSize(DefaultEventBufferSize),
00076      m_buffer( new uint32_t[DefaultEventBufferSize] )
00077 {
00078    const VFileNames_t newfnames    = RawFileTools::wildcard_correct(fnames);
00079    const VFileNames_t newidxfnames = RawFileTools::wildcard_correct(idxfnames);
00080 
00081    if ( newidxfnames.size() != newfnames.size() ) {
00082       std::cerr << "[RawFile] Num(IdxFiles) != Num(DataFiles)" << std::endl;
00083       exit(1);
00084    }
00085 
00086    m_rfs =  raw_ifstream::instance(newfnames);
00087    m_idxHandler = EvtIdxHandler::instance(newidxfnames);
00088 }

RawFileReader::~RawFileReader  )  [virtual]
 

00091 {
00092    delete[] m_buffer;
00093    raw_ifstream::release();
00094    EvtIdxHandler::release();
00095 }

RawFileReader::RawFileReader  )  [private]
 

RawFileReader::RawFileReader const std::string &  fname  ) 
 

RawFileReader::RawFileReader const VFileNames_t fnames  ) 
 

RawFileReader::RawFileReader const std::string &  fname,
const std::string &  idxfname
 

RawFileReader::RawFileReader const VFileNames_t fnames,
const VFileNames_t idxfnames
 

virtual RawFileReader::~RawFileReader  )  [virtual]
 

RawFileReader::RawFileReader  )  [private]
 


Member Function Documentation

const uint32_t* RawFileReader::currentEvent  )  const [inline, virtual]
 

Implements IRawReader.

00033 { return m_buffer; }

const uint32_t* RawFileReader::currentEvent  )  const [inline, virtual]
 

Implements IRawReader.

00033 { return m_buffer; }

std::string RawFileReader::currentFile  )  [virtual]
 

Implements IRawReader.

std::string RawFileReader::currentFile  )  [virtual]
 

Implements IRawReader.

00226 {
00227    return m_rfs->currentFile();
00228 }

const uint32_t* RawFileReader::findEventById uint32_t  evtId  ) 
 

const uint32_t * RawFileReader::findEventById uint32_t  evtId  ) 
 

00156 {
00157    // not thread safe !!!
00158    // find an event by ID ( only backward !!! )
00159    try {
00160       if ( m_idxHandler == 0 ) {
00161          while ( true ) {
00162             read_one_event();
00163             uint32_t curEvtId = m_buffer[ m_buffer[5] + 8 ];
00164             if ( curEvtId == evtId ) {
00165                break;
00166             }
00167          }
00168       }
00169       else {
00170          uint32_t pos = m_idxHandler->findPosById( evtId );
00171          if ( pos != 0 ) {
00172             m_rfs->seekg( pos );
00173             read_one_event();
00174          }
00175          else {
00176             throw ReachEndOfFile( currentFile().c_str() );
00177          }
00178       }
00179    }
00180    catch (RawFileException& e) {
00181       return nextFile(e).findEventById(evtId);
00182    }
00183 
00184    return m_buffer;
00185 }

std::vector<int> RawFileReader::getEventNumber const VFileNames_t idxfnames  )  [static]
 

std::vector< int > RawFileReader::getEventNumber const VFileNames_t idxfnames  )  [static]
 

00012 {
00013    uint32_t itmp[2];
00014    std::vector<int> vNevt;
00015 
00016    const VFileNames_t newfnames = RawFileTools::wildcard_correct(idxfnames);
00017    VFileNames_t::const_iterator it = newfnames.begin();
00018    while ( it != newfnames.end() ) {
00019       if ( access( it->c_str(), F_OK ) < 0 ) {
00020          std::cerr << "[RawFile] Invalid IDX file: " << *it << std::endl;
00021          exit(1);
00022       }
00023 
00024       std::ifstream fs( it->c_str(), std::ios::binary );
00025 
00026       fs.read( (char*)(itmp), sizeof(uint32_t)*2 );
00027       if ( itmp[0] != EvtIdxHandler::IdxFileStartMarker() ) {
00028          std::cerr << "[RawFile] Wrong IdxFileStartMarker!" << std::endl;
00029          exit(1);
00030       }
00031       vNevt.push_back(itmp[1]);
00032       ++it;
00033    }
00034 
00035    return vNevt;
00036 }

const uint32_t* RawFileReader::nextEvent int  nIgnore  ) 
 

const uint32_t* RawFileReader::nextEvent  )  [virtual]
 

Implements IRawReader.

const uint32_t * RawFileReader::nextEvent int  nIgnore  ) 
 

00121 {
00122    // not thread safe !!!
00123    int nnIgnore = nIgnore;
00124 
00125    try {
00126       if ( m_idxHandler != 0 ) {
00127          int nleft = m_idxHandler->nEvtLeft(nnIgnore);
00128          if ( nleft > 0 ) {
00129             m_rfs->seekg( m_idxHandler->nextPos( nnIgnore ) );
00130             nnIgnore = 0;
00131          }
00132          else {
00133             nnIgnore = -nleft;
00134             throw ReachEndOfFile( currentFile().c_str() );
00135          }
00136       }
00137       else {
00138          while ( nnIgnore > 0 ) {
00139             (*m_rfs) >> m_dataSeparatorRecord;
00140             uint32_t size = m_dataSeparatorRecord.getRecord().data_block_size;
00141             m_rfs->seekg( size + m_rfs->tellg() );
00142             --nnIgnore;
00143          }
00144       }
00145 
00146       read_one_event();
00147    }
00148    catch (RawFileException& e) {
00149       nextFile(e).nextEvent(nnIgnore);
00150    }
00151 
00152    return m_buffer;
00153 }

const uint32_t * RawFileReader::nextEvent  )  [virtual]
 

Implements IRawReader.

00098 {
00099    // thread safe
00100    raw_ifstream::lock();
00101 
00102    try {
00103       notSafeNextEvent();
00104    }
00105    catch ( ReachEndOfFileList& e) {
00106       raw_ifstream::unlock();
00107       throw e;
00108    }
00109    catch ( RawFileException& e) {
00110       raw_ifstream::unlock();
00111       e.print();
00112       throw e;
00113    }
00114 
00115    raw_ifstream::unlock();
00116 
00117    return m_buffer;
00118 }

RawFileReader& RawFileReader::nextFile RawFileException e  )  [private]
 

RawFileReader & RawFileReader::nextFile RawFileException e  )  [private]
 

00279 {
00280    e.print();
00281 
00282    m_rfs->clear();
00283    m_rfs->next_file();
00284 
00285    if ( m_idxHandler != 0 ) {
00286       m_idxHandler->next_file();
00287    }
00288 
00289    return *this;
00290 }

const uint32_t* RawFileReader::notSafeNextEvent  )  [private]
 

const uint32_t * RawFileReader::notSafeNextEvent  )  [private]
 

00247 {
00248    try {
00249       read_one_event();
00250    }
00251    catch (RawFileException& e) {
00252       nextFile(e).notSafeNextEvent();
00253    }
00254 
00255    return m_buffer;
00256 }

void RawFileReader::read_one_event  )  [private]
 

void RawFileReader::read_one_event  )  [private]
 

00259 {
00260    (*m_rfs) >> m_dataSeparatorRecord;
00261 
00262    uint32_t size = m_dataSeparatorRecord.getRecord().data_block_size;
00263    if ( size > m_bufferSize*4 ) {
00264       while ( size > m_bufferSize*4 ) {
00265          m_bufferSize *= 2;
00266       }
00267       delete[] m_buffer;
00268       m_buffer = new uint32_t[m_bufferSize];
00269    }
00270 
00271    m_rfs->read((char*)m_buffer, size);
00272    if ( ! m_rfs->good() ) {
00273       //std::cerr << "[RawFile] Failed to read FullEventFragment to buffer" << std::endl;
00274       throw BadInputStream("event_data_block");
00275    }
00276 }

const uint32_t* RawFileReader::roughlyNextEvent int  nIgnore,
int  evtByte = 0
 

const uint32_t * RawFileReader::roughlyNextEvent int  nIgnore,
int  evtByte = 0
 

00188 {
00189    // not thread safe !!!
00190    if ( evtByte == 0 ) evtByte = EstimatedEventSize;
00191 
00192    assert( (evtByte&3) == 0 );
00193 
00194    uint32_t prePos = m_rfs->tellg();
00195    m_rfs->seekg( prePos + nIgnore*evtByte);
00196 
00197    uint32_t halfEvtWord = evtByte / 8;
00198    uint32_t halfEvtByte = halfEvtWord * 4;
00199 
00200    while ( m_rfs->read((char*)m_buffer, halfEvtByte).good() ) {
00201       uint32_t i = 0;
00202       while ( i < halfEvtWord && m_buffer[i] != 0x1234cccc ) {
00203          ++i;
00204       }
00205       if ( i < halfEvtWord ) {
00206          uint32_t curPos = m_rfs->tellg();
00207          m_rfs->seekg( curPos - (halfEvtWord-i)*4 );
00208          read_one_event();
00209          return m_buffer;
00210       }
00211    }
00212 
00213    // reached the end of current data file! m_rfs->eof()
00214    m_rfs->clear();
00215    m_rfs->seekg( -40, std::ios::end );  //sizeof(FileEndRecord) == 40
00216    uint32_t curPos = m_rfs->tellg();
00217 
00218    int nnIgnore = nIgnore - (curPos - prePos) / evtByte;
00219    if ( nnIgnore < 0 ) nnIgnore = 0;
00220 
00221    ReachEndOfFile e( currentFile().c_str() );
00222    return nextFile(e).roughlyNextEvent(nnIgnore, evtByte);
00223 }

uint32_t RawFileReader::stat  )  [virtual]
 

Implements IRawReader.

uint32_t RawFileReader::stat  )  [virtual]
 

Implements IRawReader.

00236 {
00237    uint32_t stat = 0;
00238 
00239    if ( m_rfs->eof() )  stat |= 1;
00240    if ( m_rfs->fail() ) stat |= 2;
00241    if ( m_rfs->bad() )  stat |= 4;
00242 
00243    return stat;
00244 }

uint32_t RawFileReader::tellg  ) 
 

uint32_t RawFileReader::tellg  ) 
 

00231 {
00232    return m_rfs->tellg();
00233 }


Member Data Documentation

uint32_t* RawFileReader::m_buffer [private]
 

uint32_t* RawFileReader::m_buffer [private]
 

uint32_t RawFileReader::m_bufferSize [private]
 

DataSeparatorRecord RawFileReader::m_dataSeparatorRecord [private]
 

EvtIdxHandler* RawFileReader::m_idxHandler [private]
 

EvtIdxHandler* RawFileReader::m_idxHandler [private]
 

raw_ifstream* RawFileReader::m_rfs [private]
 

raw_ifstream* RawFileReader::m_rfs [private]
 


The documentation for this class was generated from the following files:
Generated on Wed Feb 2 16:43:21 2011 for BOSS6.5.5 by  doxygen 1.3.9.1