00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "mimotera++/MimoteraReader.hh"
00025 #include "mimotera++/MimoteraEvent.hh"
00026 #include "mimotera++/MimoteraException.hh"
00027 #include "mimotera++/MimoteraUtils.hh"
00028
00029
00030 #include <iostream>
00031 #include <fstream>
00032 #include <sys/types.h>
00033 #include <sys/stat.h>
00034 #include <cerrno>
00035
00036 using namespace std;
00037
00038 namespace Mimotera {
00039
00040
00041 MimoteraReader::MimoteraReader() {
00042
00043
00044 init();
00045
00046 }
00047
00048 MimoteraReader::MimoteraReader( const char * filename ) {
00049
00050
00051 init();
00052
00053
00054 open( filename );
00055
00056 }
00057
00058 void MimoteraReader::init() {
00059
00060 _iEvt = 0;
00061 _currentPosition = 0;
00062 _previousPosition = 0;
00063 _mimoteraFile = NULL ;
00064 _hasValidFile = false;
00065 _isEventReady = false;
00066 _mimoteraEvent = new MimoteraEvent;
00067 }
00068
00069 bool MimoteraReader::readNextEvent() {
00070
00071 _previousPosition = _currentPosition;
00072
00073
00074
00075 _mimoteraFile->read( reinterpret_cast< char * > ( _mimoteraEvent->frameA() ), kSubFrameSize ) ;
00076 _mimoteraFile->read( reinterpret_cast< char * > ( _mimoteraEvent->frameB() ), kSubFrameSize ) ;
00077 _mimoteraFile->read( reinterpret_cast< char * > ( _tempHeader ), kMimoteraInfoSize );
00078
00079
00080 unsigned short gl0s = getbigendian<unsigned short>( reinterpret_cast< const unsigned char * > ( &_tempHeader[1] ) );
00081 unsigned short gl1s = getbigendian<unsigned short>( reinterpret_cast< const unsigned char * > ( &_tempHeader[0] ) );
00082 _mimoteraEvent->info()->_globalFrameNo = (( (int) gl1s ) << 16 ) + (int) gl0s;
00083 _mimoteraEvent->info()->_frameInPacket = getbigendian<unsigned short>( reinterpret_cast< const unsigned char *>( &_tempHeader[2] ));
00084 _mimoteraEvent->info()->_triggeredFrameAB = getbigendian<unsigned short>( reinterpret_cast< const unsigned char *>( &_tempHeader[3] ));
00085 _mimoteraEvent->info()->_triggerNo = getbigendian<unsigned short>( reinterpret_cast< const unsigned char *>( &_tempHeader[4] ));
00086
00087 _mimoteraFile->seekg( _mimoteraFile->tellg() + (streampos) kTrashSize );
00088 if ( _mimoteraFile->eof() ) return false;
00089
00090 _currentPosition = _mimoteraFile->tellg();
00091
00092 if ( ( _currentPosition - _previousPosition != kEventSize ) ) {
00093 MIMOTERA_THROWX( FileReadException, "Wrong event size (" + to_string( _currentPosition - _previousPosition ) + to_string( " bytes)." ));
00094 }
00095
00096 ++_iEvt;
00097
00098
00099 _mimoteraEvent->_areArraysReady = false;
00100
00101
00102 if ( !_isEventReady ) _isEventReady = true;
00103
00104 return true;
00105 }
00106
00107 MimoteraEvent * MimoteraReader::getEvent() {
00108
00109 if ( ! _isEventReady ) MIMOTERA_THROWX( EventNotReady, "Before getting the event, you must call readNextEvent()" );
00110
00111 return _mimoteraEvent;
00112
00113 }
00114
00115 off_t MimoteraReader::open( const char * filename ) {
00116
00117 if ( _mimoteraFile ) {
00118
00119
00120 _mimoteraFile->close();
00121 delete _mimoteraFile;
00122 _mimoteraFile = NULL;
00123 }
00124
00125
00126 struct stat statbuf;
00127 int status = stat( filename, &statbuf );
00128 if ( status == -1 ) {
00129 MIMOTERA_THROWX( FileNotFoundException, to_string( "Problem opening file " ) + to_string( filename )
00130 + to_string(".\n errno = " ) + to_string( errno ) + to_string( " --> " ) + to_string( strerror( errno ) ) );
00131 }
00132
00133
00134 if ( ! ( S_ISREG( statbuf.st_mode )) || ( S_ISLNK( statbuf.st_mode ) ) ) {
00135 MIMOTERA_THROWX( NotRegularFileException, to_string( "File " ) + to_string( filename ) + " is not a regular file" );
00136 }
00137
00138
00139 off_t filesize = statbuf.st_size;
00140 off_t eventsize = kEventSize;
00141
00142 if ( filesize % eventsize != 0 ) {
00143 MIMOTERA_THROWX( WrongFileSizeException, to_string( "The actual file size is " ) + to_string( filesize ) +
00144 to_string( " and it is not an integer multiple of the event size (" ) + to_string( kEventSize ) + to_string( ")."));
00145 }
00146
00147
00148 _mimoteraFile = new ifstream( filename, ios::binary );
00149
00150 if ( _mimoteraFile ) {
00151 _hasValidFile = true;
00152 cout << "File: " << filename << endl
00153 << "Size: " << filesize << endl
00154 << "Evt : " << filesize/eventsize << endl;
00155
00156 } else {
00157 _hasValidFile = false;
00158 MIMOTERA_THROWX( FileException, "Unknown error opening file " + to_string( filename ) );
00159 }
00160
00161 return filesize;
00162
00163 }
00164
00165
00166 MimoteraReader::~MimoteraReader() {
00167
00168 if ( ! _mimoteraFile ) delete _mimoteraFile;
00169 if ( ! _mimoteraEvent ) delete _mimoteraEvent;
00170 }
00171
00172
00173 MimoteraReader * MimoteraReader_create_cpp( const char * filename ) {
00174 return new MimoteraReader( filename );
00175 }
00176
00177 void MimoteraReader_dispose_cpp( MimoteraReader * reader ) {
00178 if ( ! reader ) delete reader;
00179 reader = NULL;
00180 }
00181
00182 void MimoteraReader_open_cpp( MimoteraReader * reader, const char * filename ) {
00183 if ( ! reader ) return;
00184 reader->open( filename );
00185 }
00186
00187 int MimoteraReader_readNextEvent_cpp( MimoteraReader * reader ) {
00188 if ( ! reader ) return 0;
00189 if ( reader->readNextEvent() ) return 1;
00190 else return 0;
00191 }
00192
00193 MimoteraEvent * MimoteraReader_getEvent_cpp( MimoteraReader * reader ) {
00194 if ( ! reader ) return NULL;
00195 return reader->getEvent();
00196 }
00197
00198
00199 }
00200