00001 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00002
00003 #include "EcalTBDaqRFIOFile.h"
00004
00005 #include <sys/types.h>
00006 #include <sys/fcntl.h>
00007 #include <stdio.h>
00008
00009 #include <iosfwd>
00010 #include <algorithm>
00011 #include <iostream>
00012
00013 extern "C" {
00014 extern int rfio_open (const char *path, int flags, int mode);
00015 extern FILE *rfio_fopen (char *path, char *mode);
00016 extern int rfio_fread(void*, size_t, size_t, void*);
00017 extern int rfio_fclose (FILE *fd);
00018 extern int rfio_fseek (FILE *fp, long offset, int whence);
00019 extern int rfio_feof (FILE *fp);
00020 extern long rfio_ftell (FILE *fp);
00021 }
00022
00023 using namespace edm;
00024
00025 EcalTBDaqRFIOFile::EcalTBDaqRFIOFile(const std::string& filename, const bool& isBinary) : filename_(filename), isBinary_(isBinary)
00026 {
00027 if ( isBinary_ )
00028 {
00029
00030 LogInfo("EcalTBDaqRFIOFile") << "@SUB=EcalTBDaqRFIOFile::initialize" << "Opening binary data file " << filename;
00031 int fd = rfio_open(filename.c_str(), O_RDONLY, 0644);
00032 if( fd < 0 )
00033 {
00034 LogError("EcalTBDaqRFIOFile") << "@SUB=EcalTBDaqRFIOFile::initialize" << "the input file: " << filename << " cannot be opened. Exiting program... " ;
00035 exit(1);
00036 }
00037 infile_ = rfio_fopen((char*)filename.c_str(), "r");
00038 }
00039
00040 else
00041 {
00042 LogInfo("EcalTBDaqRFIOFile") << "@SUB=EcalTBDaqRFIOFile::initialize" << "Opening ASCII file " << filename;
00043 int fd = rfio_open(filename.c_str(), O_RDONLY, 0644);
00044 if( fd < 0 )
00045 {
00046 LogError("EcalTBDaqRFIOFile") << "@SUB=EcalTBDaqRFIOFile::initialize" << "the input file: " << filename << " cannot be opened. Exiting program... " ;
00047 exit(1);
00048 }
00049 infile_ = rfio_fopen((char*)filename.c_str(), "r");
00050 }
00051 }
00052
00053
00054 bool EcalTBDaqRFIOFile::getEventData(FedDataPair& data) {
00055
00056 if (isBinary_){
00057 ulong dataStore=1;
00058
00059
00060 if (rfio_fread(reinterpret_cast<char *>(&dataStore),sizeof(ulong),1,infile_) < 0)
00061 return false;
00062 if (rfio_fread(reinterpret_cast<char *>(&dataStore),sizeof(ulong),1,infile_) < 0)
00063 return false;
00064 if (rfio_fread(reinterpret_cast<char *>(&dataStore),sizeof(ulong),1,infile_) < 0)
00065 return false;
00066
00067 int size = dataStore & 0xffffff ;
00068
00069
00070 if (size > EcalTBDaqFile::maxEventSizeInBytes_){
00071 LogWarning("EcalTBDaqRFIOFile") << "@SUB=EcalTBDaqRFIOFile::getEventData" << "event size larger than allowed";
00072 return false;
00073 }
00074
00075
00076 if (rfio_fseek(infile_,-2*4,SEEK_CUR) < 0)
00077 return false ;
00078 if (rfio_fread(reinterpret_cast<char *>(&dataStore),sizeof(ulong),1,infile_) < 0)
00079 return false;
00080
00081 if (rfio_fseek(infile_,-2*4,SEEK_CUR) < 0)
00082 return false;
00083
00084
00085 char * bufferChar = new char [8* size];
00086 if (rfio_fread(bufferChar,size*8,1,infile_) < 0)
00087 return false;
00088
00089 data.len = size*8;
00090 data.fedData = reinterpret_cast<unsigned char*>(bufferChar);
00091
00092 }
00093
00094
00095 else{
00096 string myWord;
00097
00098
00099 int len=0;
00100 ulong* buf = new ulong [EcalTBDaqFile::maxEventSizeInBytes_];
00101 ulong* tmp=buf;
00102
00103
00104 for ( int i=0; i< EcalTBDaqFile::maxEventSizeInBytes_/4; ++i) {
00105
00106
00107 myWord = "";
00108 while ( 1 ) {
00109 char c[1];
00110 if (rfio_fread(c,1,1,infile_) < 0)
00111 return false;
00112 if ( c[0] == '\n' )
00113 break;
00114 myWord += c[0];
00115 }
00116
00117 if (sscanf( myWord.c_str(),"%x",buf) == EOF)
00118 return false;
00119 int val= *buf >> 28;
00120
00121
00122
00123
00124
00125 if ( len%2!=0 ) {
00126
00127 if ( val == EcalTBDaqFile::EOE_ ) {
00128
00129 len++;
00130 break;
00131 }
00132 }
00133 buf+=1;
00134 len++;
00135
00136 }
00137
00138
00139 len*=4;
00140
00141
00142 data.len = len;
00143
00144 data.fedData = reinterpret_cast<unsigned char*>(tmp);
00145
00146
00147 }
00148
00149 return true;
00150 }
00151
00152
00153 bool EcalTBDaqRFIOFile::checkEndOfFile()
00154 {
00155
00156 long curr=rfio_ftell(infile_);
00157 rfio_fseek(infile_,0,SEEK_END);
00158 long end=rfio_ftell(infile_);
00159 rfio_fseek(infile_,curr,SEEK_SET);
00160 return (curr==end);
00161 }
00162
00163
00164 void EcalTBDaqRFIOFile::close()
00165 {
00166 if (infile_) {
00167 rfio_fclose(infile_);
00168 infile_ = 0;
00169 }
00170 }