15 std::ostringstream ss;
16 ss <<
"Scope length " << samplesPerChannel <<
" is too long. " 25 return data_.at(internalFEDChannelNum);
26 }
catch (
const std::out_of_range&) {
27 std::ostringstream ss;
28 ss <<
"Channel index out of range. (" << uint16_t(internalFEDChannelNum) <<
") " 29 <<
"Index should be in internal numbering scheme (0-95). ";
37 std::ostringstream ss;
38 ss <<
"Sample value (" << value <<
") is too large. Maximum allowed is 1023. ";
43 }
catch (
const std::out_of_range&) {
44 std::ostringstream ss;
45 ss <<
"Sample index out of range. " 46 <<
"Requesting sample " << sampleNumber
47 <<
" when channel has only " <<
data_.size() <<
" samples.";
57 uint32_t totalSize = 0;
60 totalSize += channelBuffers[iFE*FEDCH_PER_FEUNIT+iCh].size();
64 totalSize = ((totalSize/8) + 1)*8;
67 data_.resize(totalSize);
68 size_t indexInBuffer = 0;
69 feLengths_.reserve(FEUNITS_PER_FED);
72 const size_t lengthAtStartOfFEUnit = indexInBuffer;
75 appendToBuffer(&indexInBuffer,channelBuffers[iFE*FEDCH_PER_FEUNIT+iCh].
begin(),channelBuffers[iFE*FEDCH_PER_FEUNIT+iCh].
end());
78 feLengths_.push_back(indexInBuffer-lengthAtStartOfFEUnit);
80 while (indexInBuffer % 8) appendToBuffer(&indexInBuffer,0);
87 if (lengthInBytes())
return &
data_[0];
95 return feLengths_.at(internalFEUnitNum);
96 }
catch (
const std::out_of_range&) {
97 std::ostringstream ss;
98 ss <<
"Invalid FE unit number " << internalFEUnitNum <<
". " 99 <<
"Number should be in internal numbering scheme (0-7). ";
106 std::vector< std::vector<uint8_t> > channelBuffers(
FEDCH_PER_FED,std::vector<uint8_t>());
109 fillChannelBuffer(&channelBuffers[iCh], mode, packetCode, data.
channel(iCh), channelsEnabled_[iCh]);
121 switch (packetCode) {
141 fillZeroSuppressedChannelBuffer(channelBuffer,packetCode,data,channelEnabled);
151 fillZeroSuppressedLiteChannelBuffer(channelBuffer,data,channelEnabled,mode);
154 fillPreMixRawChannelBuffer(channelBuffer,data,channelEnabled);
157 std::ostringstream ss;
158 ss <<
"Invalid readout mode " <<
mode;
165 const uint8_t packetCode,
167 const bool channelEnabled,
168 const bool reorderData)
const 171 uint16_t channelLength = 0;
172 switch (packetCode) {
174 channelLength = nSamples*2 + 3;
177 channelLength = std::ceil(nSamples*1.25) + 3;
181 channelLength = nSamples*1 + 3;
184 channelBuffer->reserve(channelLength);
186 channelBuffer->push_back( channelLength & 0xFF );
187 channelBuffer->push_back( (channelLength & 0xF00) >> 8 );
189 channelBuffer->push_back(packetCode);
191 uint16_t sampleValue_pre = 0;
192 for (uint16_t sampleNumber = 0; sampleNumber <
nSamples; sampleNumber++) {
194 const uint16_t sampleValue = (channelEnabled ? data.
getSample(sampleIndex) : 0);
195 switch (packetCode) {
197 channelBuffer->push_back(sampleValue & 0xFF);
198 channelBuffer->push_back((sampleValue & 0x300) >> 8);
201 if (sampleNumber%4==0) {
202 channelBuffer->push_back((sampleValue & 0x3FC) >> 2);
204 else if (sampleNumber%4==1) {
205 channelBuffer->push_back(((sampleValue_pre & 0x3) << 6) | ((sampleValue & 0x3F0) >> 4));
207 else if (sampleNumber%4==2) {
208 channelBuffer->push_back(((sampleValue_pre & 0xF) << 4) | ((sampleValue & 0x3C0) >> 6));
210 else if (sampleNumber%4==3) {
211 channelBuffer->push_back(((sampleValue_pre & 0x3F) << 2) | ((sampleValue & 0x300)>>8));
212 channelBuffer->push_back(sampleValue & 0xFF);
214 sampleValue_pre = sampleValue;
217 channelBuffer->push_back((sampleValue & 0x3FC) >> 2);
220 channelBuffer->push_back((sampleValue & 0x1FE) >> 1);
227 const uint8_t packetCode,
229 const bool channelEnabled)
const 231 channelBuffer->reserve(50);
233 if (!channelEnabled) {
235 channelBuffer->push_back(7);
236 channelBuffer->push_back(0);
238 channelBuffer->push_back(packetCode);
240 channelBuffer->insert(channelBuffer->end(),4,0);
245 channelBuffer->push_back(0xFF);
246 channelBuffer->push_back(0xFF);
248 channelBuffer->push_back(packetCode);
250 const std::pair<uint16_t,uint16_t> medians = data.
getMedians();
251 channelBuffer->push_back(medians.first & 0xFF);
252 channelBuffer->push_back((medians.first & 0x300) >> 8);
253 channelBuffer->push_back(medians.second & 0xFF);
254 channelBuffer->push_back((medians.second & 0x300) >> 8);
258 const uint16_t length = channelBuffer->size();
259 (*channelBuffer)[0] = (length & 0xFF);
260 (*channelBuffer)[1] = ((length & 0x300) >> 8);
265 const bool channelEnabled,
268 channelBuffer->reserve(50);
270 if (!channelEnabled) {
272 channelBuffer->push_back(2);
273 channelBuffer->push_back(0);
278 channelBuffer->push_back(0xFF);
279 channelBuffer->push_back(0xFF);
281 fillClusterData(channelBuffer, 0, data, mode);
283 const uint16_t length = channelBuffer->size();
284 (*channelBuffer)[0] = (length & 0xFF);
285 (*channelBuffer)[1] = ((length & 0x300) >> 8);
290 const bool channelEnabled)
const 292 channelBuffer->reserve(50);
294 if (!channelEnabled) {
296 channelBuffer->push_back(7);
297 channelBuffer->push_back(0);
301 channelBuffer->insert(channelBuffer->end(),4,0);
306 channelBuffer->push_back(0xFF); channelBuffer->push_back(0xFF);
310 const std::pair<uint16_t,uint16_t> medians = data.
getMedians();
311 channelBuffer->push_back(medians.first & 0xFF);
312 channelBuffer->push_back((medians.first & 0x300) >> 8);
313 channelBuffer->push_back(medians.second & 0xFF);
314 channelBuffer->push_back((medians.second & 0x300) >> 8);
316 fillClusterDataPreMixMode(channelBuffer,data);
318 const uint16_t length = channelBuffer->size();
319 (*channelBuffer)[0] = (length & 0xFF);
320 (*channelBuffer)[1] = ((length & 0x300) >> 8);
348 uint16_t clusterSize = 0;
349 std::size_t size_pos = 0;
350 uint16_t adc_pre = 0;
357 if ( is10Bit && (clusterSize%4) ) { channelBuffer->push_back(adc_pre); }
358 (*channelBuffer)[size_pos] = clusterSize;
362 channelBuffer->push_back(
strip);
363 size_pos = channelBuffer->size();
364 channelBuffer->push_back(0);
367 channelBuffer->push_back(adc & 0xFF);
369 if (clusterSize%4==0) {
370 channelBuffer->push_back((adc & 0x3FC) >> 2);
371 adc_pre = ((adc & 0x3) << 6);
372 }
else if (clusterSize%4==1) {
373 channelBuffer->push_back(adc_pre | ((adc & 0x3F0) >> 4));
374 adc_pre = ((adc & 0xF) << 4);
375 }
else if (clusterSize%4==2) {
376 channelBuffer->push_back(adc_pre | ((adc & 0x3C0) >> 6));
377 adc_pre = ((adc & 0x3F) << 2);
378 }
else if (clusterSize%4==3) {
379 channelBuffer->push_back(adc_pre | ((adc & 0x300) >> 8));
380 channelBuffer->push_back(adc & 0xFF);
386 else if (clusterSize) {
387 if ( is10Bit && (clusterSize%4) ) { channelBuffer->push_back(adc_pre); }
388 (*channelBuffer)[size_pos] = clusterSize;
393 (*channelBuffer)[size_pos] = clusterSize;
394 if ( is10Bit && (clusterSize%4) ) { channelBuffer->push_back(adc_pre); }
400 uint16_t clusterSize = 0;
408 *(channelBuffer->end() - 2*clusterSize - 1) = clusterSize ;
411 channelBuffer->push_back(
strip);
412 channelBuffer->push_back(0);
414 channelBuffer->push_back(adc & 0xFF);
415 channelBuffer->push_back((adc & 0x0300) >> 8);
420 else if(clusterSize) {
421 *(channelBuffer->end() - 2*clusterSize - 1) = clusterSize ;
426 *(channelBuffer->end() - 2*clusterSize - 1) = clusterSize ;
433 const std::vector<bool>& feUnitsEnabled,
const std::vector<bool>& channelsEnabled,
436 : defaultDAQHeader_(l1ID,bxID,0,evtType),
437 defaultDAQTrailer_(0,0),
438 defaultTrackerSpecialHeader_(bufferFormat,readoutMode,headerType),
439 defaultFEHeader_(
FEDFEHeader::newFEHeader(headerType)),
440 feUnitsEnabled_(feUnitsEnabled),
441 channelsEnabled_(channelsEnabled)
444 std::ostringstream ss;
445 ss <<
"Bad header format: " << headerType;
454 }
catch (
const std::out_of_range&) {
455 std::ostringstream ss;
456 ss <<
"Invalid FE unit number " << internalFEUnitNumber <<
". Should be in internal numbering scheme (0-7)";
465 }
catch (
const std::out_of_range&) {
467 std::ostringstream ss;
468 ss <<
"Invalid channel number " << internalFEDChannelNumber <<
". " 469 <<
"Should be in internal numbering scheme (0-95)";
478 }
catch (
const std::out_of_range&) {
479 std::ostringstream ss;
480 ss <<
"Invalid FE unit number " << internalFEUnitNumber <<
". " 481 <<
"Should be in internal numbering scheme (0-7)";
491 }
catch (
const std::out_of_range&) {
492 std::ostringstream ss;
493 ss <<
"Invalid channel number " << internalFEDChannelNumber <<
". " 494 <<
"Should be in internal numbering scheme (0-95)";
503 std::ostringstream ss;
504 ss <<
"Setting FE enable vector with vector which is the wrong size. Size is " << feUnitEnables.size()
515 std::ostringstream ss;
516 ss <<
"Setting FED channel enable vector with vector which is the wrong size. Size is " << channelEnables.size()
533 for (uint8_t iFEUnitChannel = 0; iFEUnitChannel <
FEDCH_PER_FEUNIT; iFEUnitChannel++) {
551 fedFeHeader->setFEUnitLength(iFE,payload.
getFELength(iFE));
571 uint8_t* bufferPointer = pointerToStartOfBuffer;
572 memcpy(bufferPointer,daqHeader.
data(),8);
574 memcpy(bufferPointer,tkSpecialHeader.
data(),8);
580 memcpy(bufferPointer,updatedDAQTrailer.
data(),8);
583 updatedDAQTrailer.
setCRC(crc);
584 memcpy(bufferPointer,updatedDAQTrailer.
data(),8);
587 for (
size_t i = 0;
i < 8;
i++) {
588 bufferPointer[
i] = bufferPointer[
i^4];
int adc(sample_type sample)
get the ADC sample (12 bits)
static uint8_t physicalOrderForStripInChannel(const uint8_t readoutOrderStripIndexInChannel)
FEDBufferGenerator & setChannelEnables(const std::vector< bool > &channelsEnabled)
void fillClusterDataPreMixMode(std::vector< uint8_t > *channelBuffer, const FEDStripData::ChannelData &data) const
FEDDAQHeader defaultDAQHeader_
static const uint8_t PACKET_CODE_ZERO_SUPPRESSED8_BOTBOT
void setSample(const uint16_t sampleNumber, const uint16_t adcValue)
static const uint8_t PACKET_CODE_SCOPE
FEDBufferPayload(const std::vector< std::vector< uint8_t > > &channelBuffers)
static size_t bufferSizeInBytes(const FEDFEHeader &feHeader, const FEDBufferPayload &payload)
FEDStripData(const std::vector< ChannelData > &data)
uint8_t internalFEDChannelNum(const uint8_t internalFEUnitNum, const uint8_t internalFEUnitChannelNum)
void fillClusterData(std::vector< uint8_t > *channelBuffer, uint8_t packetCode, const FEDStripData::ChannelData &data, const FEDReadoutMode mode) const
uint16_t calculateFEDBufferCRC(const uint8_t *buffer, const size_t lengthInBytes)
FEDReadoutMode getReadoutMode() const
void fillZeroSuppressedChannelBuffer(std::vector< uint8_t > *channelBuffer, const uint8_t packetCode, const FEDStripData::ChannelData &data, const bool channelEnabled) const
std::unique_ptr< FEDFEHeader > defaultFEHeader_
uint16_t get10BitSample(const uint16_t sampleNumber) const
FEDDAQTrailer & daqTrailer()
uint8_t get8BitSample(const uint16_t sampleNumber, uint16_t nBotBitsToDrop) const
static const uint8_t PACKET_CODE_ZERO_SUPPRESSED8_TOPBOT
static const uint8_t PACKET_CODE_ZERO_SUPPRESSED10
std::vector< bool > channelsEnabled_
static const uint8_t PACKET_CODE_VIRGIN_RAW10
const uint8_t * data() const
static const uint8_t PACKET_CODE_ZERO_SUPPRESSED
FEDDAQTrailer & setEventLengthIn64BitWords(const uint32_t eventLengthIn64BitWords)
FEDBufferGenerator & setFEUnitEnables(const std::vector< bool > &feUnitsEnabled)
FEDDAQTrailer & setCRC(const uint16_t crc)
void resize(size_t newsize)
std::pair< uint16_t, uint16_t > getMedians() const
static const uint16_t FEUNITS_PER_FED
void fillZeroSuppressedLiteChannelBuffer(std::vector< uint8_t > *channelBuffer, const FEDStripData::ChannelData &data, const bool channelEnabled, const FEDReadoutMode mode) const
const uint8_t * data() const
FEDDAQTrailer defaultDAQTrailer_
uint16_t getFELength(const uint8_t internalFEUnitNum) const
void generateBuffer(FEDRawData *rawDataObject, const FEDStripData &data, uint16_t sourceID, uint8_t packetCode) const
FEDBufferGenerator & setChannelEnable(const uint8_t internalFEDChannelNumber, const bool enabled)
constexpr size_t nSamples
static const uint8_t PACKET_CODE_VIRGIN_RAW8_TOPBOT
void fillChannelBuffer(std::vector< uint8_t > *channelBuffer, FEDReadoutMode mode, uint8_t packetCode, const FEDStripData::ChannelData &data, const bool channelEnabled) const
void fillRawChannelBuffer(std::vector< uint8_t > *channelBuffer, const uint8_t packetCode, const FEDStripData::ChannelData &data, const bool channelEnabled, const bool reorderData) const
FEDBufferGenerator(const uint32_t l1ID=0, const uint16_t bxID=0, const std::vector< bool > &feUnitsEnabled=std::vector< bool >(FEUNITS_PER_FED, true), const std::vector< bool > &channelsEnabled=std::vector< bool >(FEDCH_PER_FED, true), const FEDReadoutMode readoutMode=READOUT_MODE_ZERO_SUPPRESSED, const FEDHeaderType headerType=HEADER_TYPE_FULL_DEBUG, const FEDBufferFormat bufferFormat=BUFFER_FORMAT_OLD_SLINK, const FEDDAQEventType evtType=DAQ_EVENT_TYPE_PHYSICS)
static const uint8_t PACKET_CODE_PROC_RAW
ChannelData & channel(const uint8_t internalFEDChannelNum)
static const uint8_t PACKET_CODE_VIRGIN_RAW8_BOTBOT
static const uint16_t FEDCH_PER_FEUNIT
uint16_t getSample(const uint16_t sampleNumber) const
void fillPreMixRawChannelBuffer(std::vector< uint8_t > *channelBuffer, const FEDStripData::ChannelData &data, const bool channelEnabled) const
FEDBufferGenerator & setFEUnitEnable(const uint8_t internalFEUnitNumber, const bool enabled)
bool getFEUnitEnabled(const uint8_t internalFEUnitNumber) const
char data[epos_bytes_allocation]
std::vector< bool > feUnitsEnabled_
static const uint16_t FEDCH_PER_FED
static const uint16_t STRIPS_PER_APV
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
static void fillBuffer(uint8_t *pointerToStartOfBuffer, const FEDDAQHeader &daqHeader, const FEDDAQTrailer &daqTrailer, const TrackerSpecialHeader &tkSpecialHeader, const FEDFEHeader &feHeader, const FEDBufferPayload &payload)
std::vector< ChannelData > data_
size_t lengthInBytes() const
bool getChannelEnabled(const uint8_t internalFEDChannelNumber) const
static const uint16_t SCOPE_MODE_MAX_SCOPE_LENGTH
TrackerSpecialHeader defaultTrackerSpecialHeader_
FEDBufferPayload createPayload(FEDReadoutMode mode, uint8_t packetCode, const FEDStripData &data) const
FEDDAQHeader & daqHeader()
static const uint8_t PACKET_CODE_VIRGIN_RAW