Skip to content

Commit

Permalink
add protections in tof-compressor
Browse files Browse the repository at this point in the history
  • Loading branch information
noferini committed Oct 15, 2023
1 parent 99c9b85 commit e3df5c9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 14 deletions.
28 changes: 23 additions & 5 deletions Detectors/TOF/compression/include/TOFCompression/Compressor.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Compressor
inline bool run()
{
rewind();
mEncoderPointerMax = reinterpret_cast<uint32_t*>(mEncoderBuffer + mEncoderBufferSize);
mEncoderPointerMax = reinterpret_cast<uint32_t*>(mEncoderBuffer + mEncoderBufferSizeInt);
if (mDecoderCONET) {
mDecoderPointerMax = reinterpret_cast<const uint32_t*>(mDecoderBuffer + mDecoderBufferSize);
while (mDecoderPointer < mDecoderPointerMax) {
Expand Down Expand Up @@ -90,10 +90,20 @@ class Compressor
void setDecoderBuffer(const char* val) { mDecoderBuffer = val; };
void setEncoderBuffer(char* val) { mEncoderBuffer = val; };
void setDecoderBufferSize(long val) { mDecoderBufferSize = val; };
void setEncoderBufferSize(long val) { mEncoderBufferSize = val; };
void setEncoderBufferSize(long val)
{
mEncoderBufferSize = val;
mEncoderBufferSizeInt = mEncoderBufferSize / 4;
};

inline uint32_t getDecoderByteCounter() const { return reinterpret_cast<const char*>(mDecoderPointer) - mDecoderBuffer; };
inline uint32_t getEncoderByteCounter() const { return reinterpret_cast<char*>(mEncoderPointer) - mEncoderBuffer; };
inline uint32_t getEncoderByteCounter() const
{
if (reinterpret_cast<char*>(mEncoderPointer) < mEncoderBuffer) {
return 0;
}
return reinterpret_cast<char*>(mEncoderPointer) - mEncoderBuffer;
};

// benchmarks
double mIntegratedBytes = 0.;
Expand Down Expand Up @@ -140,13 +150,21 @@ class Compressor

/** encoder private functions and data members **/

void encoderSpider(int itrm);
int encoderSpider(int itrm);
inline void encoderRewind() { mEncoderPointer = reinterpret_cast<uint32_t*>(mEncoderBuffer); };
inline void encoderNext() { mEncoderPointer++; };
inline int encoderNext()
{
if (mEncoderPointer + 1 >= mEncoderPointerMax) {
return 1;
};
mEncoderPointer++;
return 0;
};

std::ofstream mEncoderFile;
char* mEncoderBuffer = nullptr;
long mEncoderBufferSize;
long mEncoderBufferSizeInt;
uint32_t* mEncoderPointer = nullptr;
uint32_t* mEncoderPointerMax = nullptr;
uint32_t* mEncoderPointerStart = nullptr;
Expand Down
44 changes: 35 additions & 9 deletions Detectors/TOF/compression/src/Compressor.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ bool Compressor<RDH, verbose, paranoid>::processHBF()
if (mEncoderPointer + mDecoderRDH->headerSize >= mEncoderPointerMax) {
LOG(error) << "link = " << rdh->feeId << ": beyond the buffer size mEncoderPointer+mDecoderRDH->headerSize = " << mEncoderPointer + mDecoderRDH->headerSize << " >= "
<< "mEncoderPointerMax = " << mEncoderPointerMax;
encoderRewind();
return true;
}
std::memcpy(mEncoderPointer, mDecoderRDH, mDecoderRDH->headerSize);
Expand Down Expand Up @@ -423,7 +424,10 @@ bool Compressor<RDH, verbose, paranoid>::processDRM()
auto slotPartMask = crateHeader->slotPartMask;
printf("%s %08x Crate header (drmID=%d, bunchID=%d, slotPartMask=0x%x) %s \n", colorGreen, *mEncoderPointer, drmID, bunchID, slotPartMask, colorReset);
}
encoderNext();
if (encoderNext()) {
encoderRewind();
return true;
}

/** encode Crate Orbit **/
*mEncoderPointer = *mDecoderSummary.tofOrbit;
Expand All @@ -432,7 +436,10 @@ bool Compressor<RDH, verbose, paranoid>::processDRM()
auto orbitID = crateOrbit->orbitID;
printf("%s %08x Crate orbit (orbitID=%u) %s \n", colorGreen, *mEncoderPointer, orbitID, colorReset);
}
encoderNext();
if (encoderNext()) {
encoderRewind();
return true;
}

/** loop over DRM payload **/
int nsteps = 0;
Expand Down Expand Up @@ -493,7 +500,10 @@ bool Compressor<RDH, verbose, paranoid>::processDRM()
auto NumberOfErrors = CrateTrailer->numberOfErrors;
printf("%s %08x Crate trailer (EventCounter=%d, NumberOfDiagnostics=%d, NumberOfErrors=%d) %s \n", colorGreen, *mEncoderPointer, EventCounter, NumberOfDiagnostics, NumberOfErrors, colorReset);
}
encoderNext();
if (encoderNext()) {
encoderRewind();
return true;
}

/** encode Diagnostic Words **/
for (int iword = 0; iword < mCheckerSummary.nDiagnosticWords; ++iword) {
Expand All @@ -505,7 +515,10 @@ bool Compressor<RDH, verbose, paranoid>::processDRM()
auto faultBits = Diagnostic->faultBits;
printf("%s %08x Diagnostic (slotId=%d, faultBits=0x%x) %s \n", colorGreen, *mEncoderPointer, slotId, faultBits, colorReset);
}
encoderNext();
if (encoderNext()) {
encoderRewind();
return true;
}
}

/** encode TDC errors **/
Expand All @@ -525,7 +538,10 @@ bool Compressor<RDH, verbose, paranoid>::processDRM()
auto tdcID = Error->tdcID;
printf("%s %08x Error (slotId=%d, chain=%d, tdcId=%d, errorFlags=0x%x) %s \n", colorGreen, *mEncoderPointer, slotID, chain, tdcID, errorFlags, colorReset);
}
encoderNext();
if (encoderNext()) {
encoderRewind();
return true;
}
}
#endif
mDecoderSummary.trmErrors[itrm][ichain] = 0;
Expand Down Expand Up @@ -692,7 +708,10 @@ bool Compressor<RDH, verbose, paranoid>::processTRM()

/** encoder Spider **/
if (mDecoderSummary.hasHits[itrm][0] || mDecoderSummary.hasHits[itrm][1]) {
encoderSpider(itrm);
if (encoderSpider(itrm)) {
encoderRewind();
return true;
}
}

/** success **/
Expand Down Expand Up @@ -833,7 +852,7 @@ bool Compressor<RDH, verbose, paranoid>::decoderParanoid()
}

template <typename RDH, bool verbose, bool paranoid>
void Compressor<RDH, verbose, paranoid>::encoderSpider(int itrm)
int Compressor<RDH, verbose, paranoid>::encoderSpider(int itrm)
{
/** encoder spider **/

Expand Down Expand Up @@ -924,7 +943,10 @@ void Compressor<RDH, verbose, paranoid>::encoderSpider(int itrm)
auto TRMID = FrameHeader->trmID;
printf("%s %08x Frame header (TRMID=%d, FrameID=%d, NumberOfHits=%d) %s \n", colorGreen, *mEncoderPointer, TRMID, FrameID, NumberOfHits, colorReset);
}
encoderNext();
if (encoderNext()) {
encoderRewind();
return true;
}

// packed hits
for (int ihit = 0; ihit < mSpiderSummary.nFramePackedHits[iframe]; ++ihit) {
Expand All @@ -938,11 +960,15 @@ void Compressor<RDH, verbose, paranoid>::encoderSpider(int itrm)
auto TOT = PackedHit->tot;
printf("%s %08x Packed hit (Chain=%d, TDCID=%d, Channel=%d, Time=%d, TOT=%d) %s \n", colorGreen, *mEncoderPointer, Chain, TDCID, Channel, Time, TOT, colorReset);
}
encoderNext();
if (encoderNext()) {
encoderRewind();
return true;
}
}

mSpiderSummary.nFramePackedHits[iframe] = 0;
}
return 0;
}

template <typename RDH, bool verbose, bool paranoid>
Expand Down

0 comments on commit e3df5c9

Please sign in to comment.