CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SiStripFEDBufferGenerator.cc
Go to the documentation of this file.
3 #include <cstring>
4 #include <stdexcept>
5 
6 namespace sistrip {
7 
8  //FEDStripData
9 
10  FEDStripData::FEDStripData(bool dataIsAlreadyConvertedTo8Bit, const size_t samplesPerChannel)
11  : data_(FEDCH_PER_FED,ChannelData(dataIsAlreadyConvertedTo8Bit,samplesPerChannel))
12  {
13  if (samplesPerChannel > SCOPE_MODE_MAX_SCOPE_LENGTH) {
14  std::ostringstream ss;
15  ss << "Scope length " << samplesPerChannel << " is too long. "
16  << "Max scope length is " << SCOPE_MODE_MAX_SCOPE_LENGTH << ".";
17  throw cms::Exception("FEDBufferGenerator") << ss.str();
18  }
19  }
20 
22  {
23  try {
24  return data_.at(internalFEDChannelNum);
25  } catch (const std::out_of_range&) {
26  std::ostringstream ss;
27  ss << "Channel index out of range. (" << uint16_t(internalFEDChannelNum) << ") "
28  << "Index should be in internal numbering scheme (0-95). ";
29  throw cms::Exception("FEDBufferGenerator") << ss.str();
30  }
31  }
32 
33  void FEDStripData::ChannelData::setSample(const uint16_t sampleNumber, const uint16_t value)
34  {
35  if (value > 0x3FF) {
36  std::ostringstream ss;
37  ss << "Sample value (" << value << ") is too large. Maximum allowed is 1023. ";
38  throw cms::Exception("FEDBufferGenerator") << ss.str();
39  }
40  try {
41  data_.at(sampleNumber) = value;
42  } catch (const std::out_of_range&) {
43  std::ostringstream ss;
44  ss << "Sample index out of range. "
45  << "Requesting sample " << sampleNumber
46  << " when channel has only " << data_.size() << " samples.";
47  throw cms::Exception("FEDBufferGenerator") << ss.str();
48  }
49  }
50 
51  //FEDBufferPayload
52 
53  FEDBufferPayload::FEDBufferPayload(const std::vector< std::vector<uint8_t> >& channelBuffers)
54  {
55  //calculate size of buffer and allocate enough memory
56  uint32_t totalSize = 0;
57  for (uint8_t iFE = 0; iFE < FEUNITS_PER_FED; iFE++) {
58  for (uint8_t iCh = 0; iCh < FEDCH_PER_FEUNIT; iCh++) {
59  totalSize += channelBuffers[iFE*FEDCH_PER_FEUNIT+iCh].size();
60  }
61  //if it does not finish on a 64Bit word boundary then take into account padding
62  if (totalSize%8) {
63  totalSize = ((totalSize/8) + 1)*8;
64  }
65  }
66  data_.resize(totalSize);
67  size_t indexInBuffer = 0;
68  feLengths_.reserve(FEUNITS_PER_FED);
69  //copy channel data into buffer with padding and update lengths
70  for (uint8_t iFE = 0; iFE < FEUNITS_PER_FED; iFE++) {
71  const size_t lengthAtStartOfFEUnit = indexInBuffer;
72  //insert data for FE unit
73  for (uint8_t iCh = 0; iCh < FEDCH_PER_FEUNIT; iCh++) {
74  appendToBuffer(&indexInBuffer,channelBuffers[iFE*FEDCH_PER_FEUNIT+iCh].begin(),channelBuffers[iFE*FEDCH_PER_FEUNIT+iCh].end());
75  }
76  //store length
77  feLengths_.push_back(indexInBuffer-lengthAtStartOfFEUnit);
78  //add padding
79  while (indexInBuffer % 8) appendToBuffer(&indexInBuffer,0);
80  }
81  }
82 
83  const uint8_t* FEDBufferPayload::data() const
84  {
85  //vectors are guarenteed to be contiguous
86  if (lengthInBytes()) return &data_[0];
87  //return NULL if there is no data yet
88  else return NULL;
89  }
90 
91  uint16_t FEDBufferPayload::getFELength(const uint8_t internalFEUnitNum) const
92  {
93  try{
94  return feLengths_.at(internalFEUnitNum);
95  } catch (const std::out_of_range&) {
96  std::ostringstream ss;
97  ss << "Invalid FE unit number " << internalFEUnitNum << ". "
98  << "Number should be in internal numbering scheme (0-7). ";
99  throw cms::Exception("FEDBufferGenerator") << ss.str();
100  }
101  }
102 
104  {
105  std::vector< std::vector<uint8_t> > channelBuffers(FEDCH_PER_FED,std::vector<uint8_t>());
106  for (size_t iCh = 0; iCh < FEDCH_PER_FED; iCh++) {
107  if (!feUnitsEnabled_[iCh/FEDCH_PER_FEUNIT]) continue;
108  fillChannelBuffer(&channelBuffers[iCh],mode,data.channel(iCh),channelsEnabled_[iCh]);
109  }
110  return FEDBufferPayload(channelBuffers);
111  }
112 
113  void FEDBufferPayloadCreator::fillChannelBuffer(std::vector<uint8_t>* channelBuffer, const FEDReadoutMode mode,
114  const FEDStripData::ChannelData& data, const bool channelEnabled) const
115  {
116  switch (mode) {
117  case READOUT_MODE_SCOPE:
118  fillRawChannelBuffer(channelBuffer,PACKET_CODE_SCOPE,data,channelEnabled,false);
119  break;
121  fillRawChannelBuffer(channelBuffer,PACKET_CODE_VIRGIN_RAW,data,channelEnabled,true);
122  break;
124  fillRawChannelBuffer(channelBuffer,PACKET_CODE_PROC_RAW,data,channelEnabled,false);
125  break;
127  fillZeroSuppressedChannelBuffer(channelBuffer,data,channelEnabled);
128  break;
130  fillZeroSuppressedLiteChannelBuffer(channelBuffer,data,channelEnabled);
131  break;
132  default:
133  std::ostringstream ss;
134  ss << "Invalid readout mode " << mode;
135  throw cms::Exception("FEDBufferGenerator") << ss.str();
136  break;
137  }
138  }
139 
140  void FEDBufferPayloadCreator::fillRawChannelBuffer(std::vector<uint8_t>* channelBuffer,
141  const uint8_t packetCode,
143  const bool channelEnabled,
144  const bool reorderData) const
145  {
146  const uint16_t nSamples = data.size();
147  //2 bytes per sample + packet code + 2 bytes for length
148  const uint16_t channelLength = nSamples*2 + 3;
149  channelBuffer->reserve(channelLength);
150  //length (max length is 0xFFF)
151  channelBuffer->push_back( channelLength & 0xFF );
152  channelBuffer->push_back( (channelLength & 0xF00) >> 8 );
153  //packet code
154  channelBuffer->push_back(packetCode);
155  //channel samples
156  for (uint16_t sampleNumber = 0; sampleNumber < nSamples; sampleNumber++) {
157  const uint16_t sampleIndex = ( reorderData ? FEDStripOrdering::physicalOrderForStripInChannel(sampleNumber) : sampleNumber );
158  const uint16_t sampleValue = (channelEnabled ? data.getSample(sampleIndex) : 0);
159  channelBuffer->push_back(sampleValue & 0xFF);
160  channelBuffer->push_back((sampleValue & 0x300) >> 8);
161  }
162  }
163 
164  void FEDBufferPayloadCreator::fillZeroSuppressedChannelBuffer(std::vector<uint8_t>* channelBuffer,
166  const bool channelEnabled) const
167  {
168  channelBuffer->reserve(50);
169  //if channel is disabled then create empty channel header and return
170  if (!channelEnabled) {
171  //min length 7
172  channelBuffer->push_back(7);
173  channelBuffer->push_back(0);
174  //packet code
175  channelBuffer->push_back(PACKET_CODE_ZERO_SUPPRESSED);
176  //4 bytes of medians
177  channelBuffer->insert(channelBuffer->end(),4,0);
178  return;
179  }
180  //if channel is not empty
181  //add space for channel length
182  channelBuffer->push_back(0xFF); channelBuffer->push_back(0xFF);
183  //packet code
184  channelBuffer->push_back(PACKET_CODE_ZERO_SUPPRESSED);
185  //add medians
186  const std::pair<uint16_t,uint16_t> medians = data.getMedians();
187  channelBuffer->push_back(medians.first & 0xFF);
188  channelBuffer->push_back((medians.first & 0x300) >> 8);
189  channelBuffer->push_back(medians.second & 0xFF);
190  channelBuffer->push_back((medians.second & 0x300) >> 8);
191  //clusters
192  fillClusterData(channelBuffer,data);
193  //set length
194  const uint16_t length = channelBuffer->size();
195  (*channelBuffer)[0] = (length & 0xFF);
196  (*channelBuffer)[1] = ((length & 0x300) >> 8);
197  }
198 
199  void FEDBufferPayloadCreator::fillZeroSuppressedLiteChannelBuffer(std::vector<uint8_t>* channelBuffer,
201  const bool channelEnabled) const
202  {
203  channelBuffer->reserve(50);
204  //if channel is disabled then create empty channel header and return
205  if (!channelEnabled) {
206  //min length 2
207  channelBuffer->push_back(2);
208  channelBuffer->push_back(0);
209  return;
210  }
211  //if channel is not empty
212  //add space for channel length
213  channelBuffer->push_back(0xFF); channelBuffer->push_back(0xFF);
214  //clusters
215  fillClusterData(channelBuffer,data);
216  //set length
217  const uint16_t length = channelBuffer->size();
218  (*channelBuffer)[0] = (length & 0xFF);
219  (*channelBuffer)[1] = ((length & 0x300) >> 8);
220  }
221 
222  void FEDBufferPayloadCreator::fillClusterData(std::vector<uint8_t>* channelBuffer, const FEDStripData::ChannelData& data) const
223  {
224  uint16_t clusterSize = 0;
225  const uint16_t nSamples = data.size();
226  for( uint16_t strip = 0; strip < nSamples; ++strip) {
227  const uint8_t adc = data.get8BitSample(strip);
228 
229  if(adc) {
230  if( clusterSize==0 || strip == STRIPS_PER_APV ) {
231  if(clusterSize) {
232  *(channelBuffer->end() - clusterSize - 1) = clusterSize ;
233  clusterSize = 0;
234  }
235  channelBuffer->push_back(strip);
236  channelBuffer->push_back(0); //clustersize
237  }
238  channelBuffer->push_back(adc);
239  ++clusterSize;
240  }
241 
242  else if(clusterSize) {
243  *(channelBuffer->end() - clusterSize - 1) = clusterSize ;
244  clusterSize = 0;
245  }
246  }
247  if(clusterSize) *(channelBuffer->end() - clusterSize - 1) = clusterSize ;
248  }
249 
250  //FEDBufferGenerator
251 
252  FEDBufferGenerator::FEDBufferGenerator(const uint32_t l1ID, const uint16_t bxID,
253  const std::vector<bool>& feUnitsEnabled, const std::vector<bool>& channelsEnabled,
254  const FEDReadoutMode readoutMode, const FEDHeaderType headerType, const FEDBufferFormat bufferFormat,
255  const FEDDAQEventType evtType, const FEDDataType dataType)
256  : defaultDAQHeader_(l1ID,bxID,0,evtType),
257  defaultDAQTrailer_(0,0),
258  defaultTrackerSpecialHeader_(bufferFormat,readoutMode,headerType,dataType),
259  defaultFEHeader_(FEDFEHeader::newFEHeader(headerType)),
260  feUnitsEnabled_(feUnitsEnabled),
261  channelsEnabled_(channelsEnabled)
262  {
263  if (!defaultFEHeader_.get()) {
264  std::ostringstream ss;
265  ss << "Bad header format: " << headerType;
266  throw cms::Exception("FEDBufferGenerator") << ss.str();
267  }
268  }
269 
270  bool FEDBufferGenerator::getFEUnitEnabled(const uint8_t internalFEUnitNumber) const
271  {
272  try {
273  return feUnitsEnabled_.at(internalFEUnitNumber);
274  } catch (const std::out_of_range&) {
275  std::ostringstream ss;
276  ss << "Invalid FE unit number " << internalFEUnitNumber << ". Should be in internal numbering scheme (0-7)";
277  throw cms::Exception("FEDBufferGenerator") << ss.str();
278  }
279  }
280 
281  bool FEDBufferGenerator::getChannelEnabled(const uint8_t internalFEDChannelNumber) const
282  {
283  try {
284  return channelsEnabled_.at(internalFEDChannelNumber);
285  } catch (const std::out_of_range&) {
286 
287  std::ostringstream ss;
288  ss << "Invalid channel number " << internalFEDChannelNumber << ". "
289  << "Should be in internal numbering scheme (0-95)";
290  throw cms::Exception("FEDBufferGenerator") << ss.str();
291  }
292  }
293 
294  FEDBufferGenerator& FEDBufferGenerator::setFEUnitEnable(const uint8_t internalFEUnitNumber, const bool enabled)
295  {
296  try {
297  feUnitsEnabled_.at(internalFEUnitNumber) = enabled;
298  } catch (const std::out_of_range&) {
299  std::ostringstream ss;
300  ss << "Invalid FE unit number " << internalFEUnitNumber << ". "
301  << "Should be in internal numbering scheme (0-7)";
302  throw cms::Exception("FEDBufferGenerator") << ss.str();
303  }
304  return *this;
305  }
306 
307  FEDBufferGenerator& FEDBufferGenerator::setChannelEnable(const uint8_t internalFEDChannelNumber, const bool enabled)
308  {
309  try {
310  channelsEnabled_.at(internalFEDChannelNumber) = enabled;
311  } catch (const std::out_of_range&) {
312  std::ostringstream ss;
313  ss << "Invalid channel number " << internalFEDChannelNumber << ". "
314  <<"Should be in internal numbering scheme (0-95)";
315  throw cms::Exception("FEDBufferGenerator") << ss.str();
316  }
317  return *this;
318  }
319 
320  FEDBufferGenerator& FEDBufferGenerator::setFEUnitEnables(const std::vector<bool>& feUnitEnables)
321  {
322  if (feUnitEnables.size() != FEUNITS_PER_FED) {
323  std::ostringstream ss;
324  ss << "Setting FE enable vector with vector which is the wrong size. Size is " << feUnitEnables.size()
325  << " it must be " << FEUNITS_PER_FED << "." << std::endl;
326  throw cms::Exception("FEDBufferGenerator") << ss.str();
327  }
328  feUnitsEnabled_ = feUnitEnables;
329  return *this;
330  }
331 
332  FEDBufferGenerator& FEDBufferGenerator::setChannelEnables(const std::vector<bool>& channelEnables)
333  {
334  if (channelEnables.size() != FEDCH_PER_FED) {
335  std::ostringstream ss;
336  ss << "Setting FED channel enable vector with vector which is the wrong size. Size is " << channelEnables.size()
337  << " it must be " << FEDCH_PER_FED << "." << std::endl;
338  throw cms::Exception("FEDBufferGenerator") << ss.str();
339  }
340  channelsEnabled_ = channelEnables;
341  return *this;
342  }
343 
344  void FEDBufferGenerator::generateBuffer(FEDRawData* rawDataObject, const FEDStripData& data, const uint16_t sourceID) const
345  {
346  //deal with disabled FE units and channels properly (FE enables, status bits)
348  std::auto_ptr<FEDFEHeader> fedFeHeader(defaultFEHeader_->clone());
349  for (uint8_t iFE = 0; iFE < FEUNITS_PER_FED; iFE++) {
350  const bool enabled = feUnitsEnabled_[iFE];
351  tkSpecialHeader.setFEEnableForFEUnit(iFE,enabled);
352  if (!enabled) {
353  for (uint8_t iFEUnitChannel = 0; iFEUnitChannel < FEDCH_PER_FEUNIT; iFEUnitChannel++) {
354  fedFeHeader->setChannelStatus(iFE,iFEUnitChannel,FEDChannelStatus(0));
355  }
356  }
357  }
358  for (uint8_t iCh = 0; iCh < FEDCH_PER_FED; iCh++) {
359  if (!channelsEnabled_[iCh]) {
360  fedFeHeader->setChannelStatus(iCh,FEDChannelStatus(0));
361  }
362  }
363  //set the source ID
365  daqHeader.setSourceID(sourceID);
366  //build payload
368  const FEDBufferPayload payload = payloadPacker(getReadoutMode(),data);
369  //fill FE lengths
370  for (uint8_t iFE = 0; iFE < FEUNITS_PER_FED; iFE++) {
371  fedFeHeader->setFEUnitLength(iFE,payload.getFELength(iFE));
372  }
373  //resize buffer
374  rawDataObject->resize(bufferSizeInBytes(*fedFeHeader,payload));
375  //fill buffer
376  fillBuffer(rawDataObject->data(),daqHeader,defaultDAQTrailer_,tkSpecialHeader,*fedFeHeader,payload);
377  }
378 
379  void FEDBufferGenerator::fillBuffer(uint8_t* pointerToStartOfBuffer,
380  const FEDDAQHeader& daqHeader,
381  const FEDDAQTrailer& daqTrailer,
382  const TrackerSpecialHeader& tkSpecialHeader,
383  const FEDFEHeader& feHeader,
384  const FEDBufferPayload& payload)
385  {
386  //set the length in the DAQ trailer
387  const size_t lengthInBytes = bufferSizeInBytes(feHeader,payload);
388  FEDDAQTrailer updatedDAQTrailer(daqTrailer);
389  updatedDAQTrailer.setEventLengthIn64BitWords(lengthInBytes/8);
390  //copy pieces into buffer in order
391  uint8_t* bufferPointer = pointerToStartOfBuffer;
392  memcpy(bufferPointer,daqHeader.data(),8);
393  bufferPointer += 8;
394  memcpy(bufferPointer,tkSpecialHeader.data(),8);
395  bufferPointer += 8;
396  memcpy(bufferPointer,feHeader.data(),feHeader.lengthInBytes());
397  bufferPointer += feHeader.lengthInBytes();
398  memcpy(bufferPointer,payload.data(),payload.lengthInBytes());
399  bufferPointer += payload.lengthInBytes();
400  memcpy(bufferPointer,updatedDAQTrailer.data(),8);
401  //update CRC
402  const uint16_t crc = calculateFEDBufferCRC(pointerToStartOfBuffer,lengthInBytes);
403  updatedDAQTrailer.setCRC(crc);
404  memcpy(bufferPointer,updatedDAQTrailer.data(),8);
405  //word swap if necessary
406  if (tkSpecialHeader.wasSwapped()) {
407  for (size_t i = 0; i < 8; i++) {
408  bufferPointer[i] = bufferPointer[i^4];
409  }
410  }
411  }
412 
413 }
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)
virtual size_t lengthInBytes() const =0
void fillZeroSuppressedChannelBuffer(std::vector< uint8_t > *channelBuffer, const FEDStripData::ChannelData &data, const bool channelEnabled) const
void setSample(const uint16_t sampleNumber, const uint16_t adcValue)
int i
Definition: DBlmapReader.cc:9
void generateBuffer(FEDRawData *rawDataObject, const FEDStripData &data, const uint16_t sourceID) const
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)
void fillClusterData(std::vector< uint8_t > *channelBuffer, const FEDStripData::ChannelData &data) const
FEDStripData(const std::vector< ChannelData > &data)
uint8_t internalFEDChannelNum(const uint8_t internalFEUnitNum, const uint8_t internalFEUnitChannelNum)
#define NULL
Definition: scimark2.h:8
uint16_t calculateFEDBufferCRC(const uint8_t *buffer, const size_t lengthInBytes)
FEDReadoutMode getReadoutMode() const
const uint8_t * data() const
uint8_t get8BitSample(const uint16_t sampleNumber) const
const uint8_t * data() const
static const uint8_t PACKET_CODE_ZERO_SUPPRESSED
TrackerSpecialHeader & setFEEnableForFEUnit(const uint8_t internalFEUnitNum, const bool enabled)
FEDDAQTrailer & setEventLengthIn64BitWords(const uint32_t eventLengthIn64BitWords)
FEDBufferGenerator & setFEUnitEnables(const std::vector< bool > &feUnitsEnabled)
FEDDAQTrailer & setCRC(const uint16_t crc)
void resize(size_t newsize)
Definition: FEDRawData.cc:32
std::pair< uint16_t, uint16_t > getMedians() const
static const uint16_t FEUNITS_PER_FED
uint16_t getFELength(const uint8_t internalFEUnitNum) const
#define end
Definition: vmac.h:37
FEDBufferGenerator & setChannelEnable(const uint8_t internalFEDChannelNumber, const bool enabled)
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, const FEDDataType dataType=DATA_TYPE_FAKE)
FEDBufferPayload createPayload(const FEDReadoutMode mode, const FEDStripData &data) const
void fillZeroSuppressedLiteChannelBuffer(std::vector< uint8_t > *channelBuffer, const FEDStripData::ChannelData &data, const bool channelEnabled) const
static const uint8_t PACKET_CODE_PROC_RAW
ChannelData & channel(const uint8_t internalFEDChannelNum)
static const uint16_t FEDCH_PER_FEUNIT
uint16_t getSample(const uint16_t sampleNumber) const
std::auto_ptr< FEDFEHeader > defaultFEHeader_
FEDDAQHeader & setSourceID(const uint16_t sourceID)
FEDBufferGenerator & setFEUnitEnable(const uint8_t internalFEUnitNumber, const bool enabled)
virtual const uint8_t * data() const =0
bool getFEUnitEnabled(const uint8_t internalFEUnitNumber) const
#define begin
Definition: vmac.h:30
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
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.
Definition: FEDRawData.cc:28
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_
void fillChannelBuffer(std::vector< uint8_t > *channelBuffer, const FEDReadoutMode mode, const FEDStripData::ChannelData &data, const bool channelEnabled) const
bool getChannelEnabled(const uint8_t internalFEDChannelNumber) const
static const uint16_t SCOPE_MODE_MAX_SCOPE_LENGTH
TrackerSpecialHeader defaultTrackerSpecialHeader_
static const uint8_t PACKET_CODE_VIRGIN_RAW