CMS 3D CMS Logo

FileReaderSPNewFormat.cc

Go to the documentation of this file.
00001 #include <IORawData/CSCTFCommissioning/src/FileReaderSPNewFormat.h>
00002 
00003 #include <iostream>
00004 #include <fstream>
00005 #include <cstdio>
00006 #include <unistd.h>
00007 
00008 #include <FWCore/MessageLogger/interface/MessageLogger.h>
00009 
00010 FileReaderSPNewFormat::FileReaderSPNewFormat()
00011 {
00012   pointer = 0;
00013   trailer[0] = 0xf000;
00014   trailer[1] = 0xf07f;
00015   trailer[2] = 0xf000;
00016   trailer[3] = 0xf000;
00017   trailer[4] = 0xe000;
00018   trailer[5] = 0xe000;
00019   trailer[6] = 0xe000;
00020   trailer[7] = 0xe000;
00021 
00022   header[0] = 0x9000;
00023   header[1] = 0x9000;
00024   header[2] = 0x9000;
00025   header[3] = 0x9000;
00026   header[4] = 0xa000;
00027   header[5] = 0xa000;
00028   header[6] = 0xa000;
00029   header[7] = 0xa000;
00030 }
00031 
00032 void FileReaderSPNewFormat::Configure() {
00033 }
00034 
00035 
00036 int FileReaderSPNewFormat::chunkSize() {
00037   // The smallest piece of the data format 
00038   // is 64 bits so read in that amount.
00039   return sizeof(long long);
00040 }
00041 
00042 
00043 int FileReaderSPNewFormat::readSP(unsigned short **buf, const bool debug) 
00044 {
00045   unsigned short a[50000];
00046   int bytes_read=0;
00047   unsigned trailer_pos;
00048   unsigned counter = 0;
00049 
00050   do
00051     {
00052       if(pointer == 0)
00053         {
00054           bytes_read = read(fd_schar, a, chunkSize());  //read 1 chunk
00055           counter += bytes_read;
00056           pointer += counter;
00057           if(bytes_read != chunkSize())
00058             {
00059               edm::LogInfo("FileReaderSPNewFormat") << "Reached end of file.";
00060               return 0; // we can safely return zero since an event size less than one chunk is useless.
00061             }     
00062         }
00063       else
00064         {
00065           bytes_read = read(fd_schar, a + counter/sizeof(short), chunkSize());
00066           counter += bytes_read;
00067           pointer += counter;
00068           if(bytes_read != chunkSize())
00069             {
00070               edm::LogInfo("FileReaderSPNewFormat") << "Reached end of file, recovering last data.";
00071               if( !findTrailer2(a, counter/sizeof(short)) )
00072                 {
00073                   if( (trailer_pos = findTrailer1(a, counter)) ) // if the first trailer is there
00074                     {
00075                       memcpy(a + trailer_pos + 4, trailer + 4, 4*sizeof(short));
00076                       counter += 4;
00077                     }
00078                   else
00079                     {
00080                       memcpy(a + counter/sizeof(short), trailer, 8*sizeof(short));
00081                       counter += 8;
00082                     }
00083                 }
00084             }
00085         }
00086       
00087     }while( !hasTrailer(a, counter/sizeof(short)) );
00088   
00089   void * event = malloc(counter);
00090   memset(event,0,counter);
00091   memcpy(event,a,counter);
00092   *buf=reinterpret_cast<unsigned short*>(event);
00093     
00094   //printf("\n\n\n  returning %d: \n\n\n",(count*8));
00095 
00096   return counter;  //Total number of bytes read
00097 
00098 } //readSP
00099 
00100 unsigned FileReaderSPNewFormat::findTrailer1(const unsigned short* buf, unsigned length)
00101 {
00102   unsigned short* itr = const_cast<unsigned short*>(buf);
00103 
00104   unsigned pos = 0;
00105    
00106   while( itr + 3 <= buf + length )
00107     {
00108       if(isTrailer1(itr)) pos = (unsigned)itr; 
00109       ++itr;
00110     }
00111   
00112   return pos;
00113 }
00114 
00115 unsigned FileReaderSPNewFormat::findTrailer2(const unsigned short* buf, unsigned length)
00116 {
00117   unsigned short* itr = const_cast<unsigned short*>(buf);
00118 
00119   unsigned pos = 0;
00120  
00121   while( itr + 3 <= buf + length )
00122     {
00123       if(isTrailer2(itr)) pos = (unsigned)itr; // I hate to brute force this but I had to :-(
00124       ++itr;
00125     }
00126   
00127   return pos; 
00128 }
00129 
00130 bool FileReaderSPNewFormat::isTrailer1(const unsigned short* buf)
00131 {
00132   bool result = false;
00133   result = (trailer[0] & buf[0]) == trailer[0];
00134   result = (trailer[1] & buf[1]) == trailer[1];
00135   result = (trailer[2] & buf[2]) == trailer[2];
00136   result = (trailer[3] & buf[3]) == trailer[3];
00137  
00138   return result;
00139 }
00140 
00141 bool FileReaderSPNewFormat::isTrailer2(const unsigned short* buf)
00142 {
00143   bool result = false;
00144   result = (trailer[4] & buf[0]) == trailer[4];
00145   result = (trailer[5] & buf[1]) == trailer[5];
00146   result = (trailer[6] & buf[2]) == trailer[6];
00147   result = (trailer[7] & buf[3]) == trailer[7];
00148   return result;
00149 }
00150 
00151 bool FileReaderSPNewFormat::hasTrailer(const unsigned short* buf, unsigned length)
00152 {
00153   return (isTrailer1(buf + length - 8) && isTrailer2(buf + length - 4));
00154 }

Generated on Tue Jun 9 17:39:23 2009 for CMSSW by  doxygen 1.5.4