/data3/calcul/jacquem/working_dir/Micromegas/micromegasFrameWork/src/parser/DifReader.cpp

Go to the documentation of this file.
00001 /* @version $Revision: 1491 $ * @modifiedby $Author: jacquem $ * @lastmodified $Date: 2012-02-10 15:58:39 +0100 (Fri, 10 Feb 2012) $ */
00002 
00003 ////////////////////////////////////////////////////////////////////////
00004 // MICROMEGAS
00005 //
00006 //       EVENT RECONSTRUCTION SOFTWARE FOR DIRAC BOARDS
00007 //
00008 ////////////////////////////////////////////////////////////////////////
00009 // v1 23.10.2009
00010 ////////////////////////////////////////////////////////////////////////
00011 // timestamp (v1) :
00012 //   t(hit) = t(lecture) - t2 + t3
00013 //   l(lecture) = t0 + (t1 - t10)
00014 //   avec
00015 //     t0 = timestamp unix (32 bits, avec ou sans millisecondes)
00016 //     t1 = BcId absolu (bcId_Abs, 48 bits)
00017 //     t10 = premiere lecture de t1
00018 //     t2 = BcId pour la DIF (bcId_HRHit, 24 bits)
00019 //     t3 = BCId pour la HR (bcId_Hit, 24 bits en CODE GRAY)
00020 ////////////////////////////////////////////////////////////////////////
00021 #include "parser/DifReader.hh"
00022 #include "parser/CrcCheck.hh"
00023 
00024 
00025 #include "tools/Log.hh"
00026 #include "tools/MicroException.hh"
00027 
00028 #include <stdio.h>
00029 #include <errno.h>
00030 #include <stdlib.h>
00031 #include <string.h>
00032 #include <string>
00033 #include <sstream>
00034 #include <iostream>
00035 #include <iomanip>
00036 
00037 using namespace std;
00038 
00039 //-------------------------------------------------------------------
00040 DifReader::DifReader(Run& aRun, FILE *aFile, ui32 firstEventId) : AcquisitionParser(aRun, aFile, firstEventId)
00041 {
00042   fileError = 0;
00043   displayData = (FILELog::ReportingLevel() >= logDEBUG) ? 1 : 0;
00044   level = 0;
00045   line = 0;
00046   bitBuffer = (unsigned char *)NULL;
00047 }; // DifReader
00048 
00049 //-------------------------------------------------------------------
00050 unsigned int DifReader::getBinData(const int nBytes) {
00051   unsigned int result = 0;
00052   fileError = 0;
00053   for (int byte = 0; byte < nBytes; ++byte) {
00054     unsigned char data = 0;
00055     if (fread((void *)&data, sizeof(unsigned char), 1, inputFile) != 1) {
00056       //FILE_LOG(logERROR) << "getBinData : error " << errno << endl;
00057       fileError = EOF;
00058       return(0);
00059     }
00060     result = (result << 8) + data;
00061   }
00062 
00063   putData(result, nBytes);
00064   return(result);
00065 }; // DifReader.getBinData
00066 
00067 // Binary data for Dirac
00068 //-------------------------------------------------------------------
00069 unsigned char *DifReader::getBinData(const int nBytes, unsigned char *buf) {
00070   unsigned int result = 0;
00071   fileError = 0;
00072   if (fread(buf, sizeof(unsigned char), nBytes, inputFile) != nBytes) {
00073     *buf = 0;
00074     FILE_LOG(logERROR) << "getBinData : error " << errno << endl;
00075     fileError = EOF;
00076     return(0);
00077   }
00078   buf[nBytes] = 0;
00079   putBinData(buf, nBytes);
00080   return(buf);
00081 }; // DifReader.getBinData
00082 
00083 //-------------------------------------------------------------------
00084 unsigned int DifReader::getData(const int nBytes) {
00085   unsigned int result = 0;
00086   fileError = 0;
00087   for (int byte = 0; byte < nBytes; ++byte) {
00088     int data = 0;
00089     if (fscanf(inputFile, "%2x", &data) != 1) {
00090       //FILE_LOG(logERROR) << "getData : error " << errno << endl;
00091       fileError = EOF;
00092       return 0;
00093     }
00094     if ( computeCrc ) {
00095       crc->compute(data) ; 
00096     }
00097     result = (result << 8) + data;
00098   }
00099 
00100   putData(result, nBytes);
00101   return(result);
00102 } // DifReader.getData
00103 
00104 //-------------------------------------------------------------------
00105 unsigned char *DifReader::getData(const int nBytes, unsigned char *buf) {
00106   unsigned char *bufStr = buf;
00107   fileError = 0;
00108   for (int byte = 0; byte < nBytes; ++byte) {
00109     int data = 0;
00110     if (fscanf(inputFile, "%2x", &data) != 1) {
00111       FILE_LOG(logERROR) << "getData : error " << errno << endl;
00112       *bufStr = 0;
00113       fileError = EOF;
00114       return(0);
00115     }
00116     *bufStr++ = data;
00117 
00118       if ( computeCrc ) {
00119       crc->compute(data) ;
00120     }
00121 
00122   }
00123   *bufStr = 0;
00124   putBinData(buf, nBytes);
00125   return(buf);
00126 } // DifReader.getData
00127 // ----------------------------------------------------------------
00128 // read one whole buffer from file
00129 unsigned char *DifReader::getBitData() {
00130   unsigned char *res;
00131   res = (bitBinaryFile) ? getBinData(bitBufCount, bitBuffer)
00132                         : getData(bitBufCount, bitBuffer);
00133   display();
00134   return(res);
00135 } // DifReader.getBitData
00136 
00137 // ----------------------------------------------------------------
00138 /* open the device to read the bit stream from it */
00139 unsigned char *DifReader::initGetBits(const int bufLen, const bool binaryFile) {
00140   bitBuffer = (unsigned char*)malloc(bufLen + 1);
00141   if (!bitBuffer) {
00142     return 0;
00143   }
00144   bitIdx = 8;
00145   bitBufCount = bufLen;
00146   bitByteIdx = 0;
00147   bitBinaryFile = binaryFile;
00148   if (!getBitData()) {
00149     FILE_LOG(logERROR) << "InitGetBits : error " << errno << endl;
00150     return 0;
00151   }
00152   return bitBuffer;
00153 } // DifReader.initGetBits
00154 
00155 // ----------------------------------------------------------------
00156 /* close the device containing the bit stream after a read process*/
00157 void DifReader::finishGetBits() {
00158   bitBufCount = 0;
00159   if (bitBuffer)
00160     free(bitBuffer);
00161   bitBuffer = NULL;
00162 } // DifReader.finishGetBits
00163 
00164 //-------------------------------------------------------------------
00165 static const int masks[8]={0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80};
00166 /* read 1 bit from the bit stream */
00167 unsigned int DifReader::getBit() {
00168   unsigned int bit = (bitBuffer[bitByteIdx] & masks[bitIdx - 1]) >> (bitIdx - 1);
00169   bitIdx--;
00170   if (!bitIdx) {
00171     bitIdx = 8;
00172     bitByteIdx++;
00173     if (bitByteIdx == bitBufCount) {
00174 //      getBitData();
00175       bitByteIdx = 0;
00176     }
00177   }
00178   return bit;
00179 } // DifReader.getBit
00180 
00181 //-------------------------------------------------------------------
00182 /*read N bits from the bit stream */
00183 unsigned int DifReader::getBits(const int N) {
00184   unsigned int val = 0;
00185   int i = N;
00186   unsigned int j;
00187 
00188   while (i) {
00189     j = (bitBuffer[bitByteIdx] & masks[bitIdx - 1]) >> (bitIdx - 1);
00190     bitIdx--;
00191     if (!bitIdx) {
00192       bitIdx = 8;
00193       bitByteIdx++;
00194       if (bitByteIdx == bitBufCount) {
00195 //        FILE_LOG(logERROR) << "getBits : refill buffer i = " << i << ", bitByteIdx = " << bitByteIdx << ", bitBufCount = " << bitBufCount << endl;
00196 //        getBitData();
00197         bitByteIdx = 0;
00198       }
00199     }
00200     val = (val << 1) | j;
00201     i--;
00202   }
00203   return val;
00204 } // DifReader.getBits
00205 
00206 //-------------------------------------------------------------------
00207 /*this function seeks for a byte aligned sync word (max 32 bits) in the bit stream and
00208   places the bit stream pointer right after the sync.
00209   This function returns 1 if the sync was found otherwise it returns 0  */
00210 int DifReader::seekSyncBits(unsigned int sync, const int N) {
00211   unsigned int val, val1;
00212   unsigned int maxi = 0xFFFFFFFF;
00213 
00214   while (bitIdx != 8) {
00215     val = getBit();
00216 //    if () // error case
00217 //      return 0;
00218   }
00219 
00220   val = getBits(N);
00221 //  if () // error case
00222 //    return 0;
00223 
00224   while ((val & maxi) != sync) {
00225     val <<= 8;
00226     val1 = getBits(8);
00227 //    if () // error case
00228 //      return 0;
00229     val |= val1;
00230   }
00231   return 1;
00232 } // DifReader.seekSyncBits
00233 
00234 //-------------------------------------------------------------------
00235 int DifReader::getBitBufferData(void *buffer, const int len) {
00236   if (len < bitByteIdx) {
00237     memcpy(buffer, bitBuffer, len);
00238     bitByteIdx -= len;
00239     memcpy(bitBuffer, bitBuffer + len, bitByteIdx);
00240     return len;
00241   }
00242   else {
00243     int lg = bitByteIdx;
00244     memcpy(buffer, bitBuffer, bitByteIdx);
00245     bitByteIdx = 0;
00246     return lg;
00247   }
00248 } // DifReader.getBitBufferData
00249 
00250 //-------------------------------------------------------------------
00251 void DifReader::putData(const unsigned int result, const int nBytes) {
00252   if (!displayData)
00253     return;
00254   dataBuf << hex << setw(nBytes * 2) << setfill('0') << result;
00255   if (nBytes > 1)
00256     dataBuf << " ";
00257 //  display();
00258 } // DifReader.putData
00259 
00260 //-------------------------------------------------------------------
00261 void DifReader::putData(const unsigned char *buf, const int nBytes) {
00262   if (!displayData)
00263     return;
00264   if (nBytes > 4)
00265     dataBuf << "("<< dec << nBytes << "):";
00266   dataBuf << buf;
00267   if (nBytes > 1)
00268     dataBuf << " ";
00269 //  display();
00270 } // DifReader.putData
00271 
00272 //-------------------------------------------------------------------
00273 void DifReader::putBinData(const unsigned char *buf, const int nBytes) {
00274   if (!displayData)
00275     return;
00276   if (nBytes > 4)
00277     dataBuf << "(" << dec << nBytes << "):";
00278   for (int byte = 0; byte < nBytes; byte++) {
00279     dataBuf << hex << setw(2) << setfill('0') << (int)(*buf);
00280     buf++;
00281   }
00282   if (nBytes > 1)
00283     dataBuf << " ";
00284 //  display();
00285 } // DifReader.putBinData
00286 
00287 //-------------------------------------------------------------------
00288 void DifReader::display(const int inc) {
00289   line++;
00290   if (inc < 0) {
00291     level += inc;
00292     if (level < 0) level = 0;
00293   }
00294   if (dataBuf.str().length() != 0) {
00295     FILE_LOG(logDEBUG) << "----display-----: " <<  setfill(' ') << setw(level * 2) << "" << dataBuf.str() << endl;
00296   }
00297   if (inc > 0)
00298     level += inc;
00299   dataBuf.str("");
00300 } // DifReader.display
00301 
00302 //-------------------------------------------------------------------
00303 const i64 DifReader::getbcIdAbsOrg(const i32 difId, const i64 bcIdAbs) {
00304   BcIdDifAbsOrgMap_t::iterator it = bcIdDifAbsOrgMap.find(difId);
00305   if (it == bcIdDifAbsOrgMap.end()) {
00306     FILE_LOG(logDEBUG)   << "    set bc origin (dif 0x" << hex << difId << dec << ")=" << bcIdAbs << endl;
00307     bcIdDifAbsOrgMap[difId] = bcIdAbs;
00308     return(bcIdAbs);
00309   }
00310   return(it->second);
00311 } // DifReader.getbcIdAbsOrg
00312 

Generated on Mon Jan 7 13:15:22 2013 for MicromegasFramework by  doxygen 1.4.7