CMS 3D CMS Logo

SiStripFEDBufferGenerator.h
Go to the documentation of this file.
1 #ifndef EventFilter_SiStripRawToDigi_SiStripFEDBufferGenerator_H
2 #define EventFilter_SiStripRawToDigi_SiStripFEDBufferGenerator_H
3 
4 #include "boost/cstdint.hpp"
7 #include <vector>
8 #include <list>
9 #include <utility>
10 #include <memory>
11 
12 namespace sistrip {
13 
14  //
15  // Class definitions
16  //
17 
19  {
20  public:
21  //class used to represent channel data
22  class ChannelData {
23  public:
24  ChannelData(bool dataIsAlreadyConvertedTo8Bit, const size_t numberOfSamples,
25  const std::pair<uint16_t,uint16_t> medians = std::make_pair<uint16_t>(0,0));
26  //number of samples
27  size_t size() const;
28  //get common mode medians for first and second APV
29  std::pair<uint16_t,uint16_t> getMedians() const;
30  //set common mode medians for first and second APV
31  void setMedians(const std::pair<uint16_t,uint16_t> values);
32  //get the 10bit value to be used for raw modes
33  uint16_t getSample(const uint16_t sampleNumber) const;
34  //get the 8 bit value to be used for ZS modes, converting it as the FED does if specified in constructor
35  uint8_t get8BitSample(const uint16_t sampleNumber, uint16_t nBotBitsToDrop) const;
36  uint16_t get10BitSample(const uint16_t sampleNumber) const;
37  void setSample(const uint16_t sampleNumber, const uint16_t adcValue);
38  //setting value directly is equivalent to get and set Sample but without length check
39  uint16_t& operator [] (const size_t sampleNumber);
40  const uint16_t& operator [] (const size_t sampleNumber) const;
41  private:
42  std::pair<uint16_t,uint16_t> medians_;
43  std::vector<uint16_t> data_;
45  };
46 
47  FEDStripData(const std::vector<ChannelData>& data);
48  //specify whether the data is already in the 8bit ZS format (only affects what is returned by ChannelData::get8BitSample() if the value if >253)
49  //if the data is for scope mode then specify the scope length
50  FEDStripData(bool dataIsAlreadyConvertedTo8Bit = true, const size_t samplesPerChannel = STRIPS_PER_FEDCH);
51  //access to elements
53  const ChannelData& operator [] (const uint8_t internalFEDChannelNum) const;
55  const ChannelData& channel(const uint8_t internalFEDChannelNum) const;
56  private:
57  std::vector<ChannelData> data_;
58  };
59 
61  {
62  public:
63  FEDBufferPayload(const std::vector< std::vector<uint8_t> >& channelBuffers);
64  //size of payload in bytes
65  size_t lengthInBytes() const;
66  //returns NULL if payload size is 0, otherwise return a pointer to the payload buffer
67  const uint8_t* data() const;
68  //size of FE unit payload
69  uint16_t getFELength(const uint8_t internalFEUnitNum) const;
70  private:
71  void appendToBuffer(size_t* pIndexInBuffer, const uint8_t value);
72  void appendToBuffer(size_t* pIndexInBuffer, std::vector<uint8_t>::const_iterator start, std::vector<uint8_t>::const_iterator finish);
73  std::vector<uint8_t> data_;
74  std::vector<uint16_t> feLengths_;
75  };
76 
78  {
79  public:
80  //specify which FE units and channels should have data generated for them
81  //If an FE unit is disabled then the channel is as well. The whole FE payload will be missing.
82  //If a channel is disabled then it is considered to have all zeros in the data but will be present in the data
83  FEDBufferPayloadCreator(const std::vector<bool>& enabledFEUnits, const std::vector<bool>& enabledChannels);
84  //create the payload for a particular mode
85  FEDBufferPayload createPayload(FEDReadoutMode mode, uint8_t packetCode, const FEDStripData& data) const;
86  FEDBufferPayload operator () (FEDReadoutMode mode, uint8_t packetCode, const FEDStripData& data) const;
87  private:
88  //fill vector with channel data
89  void fillChannelBuffer(std::vector<uint8_t>* channelBuffer, FEDReadoutMode mode, uint8_t packetCode,
90  const FEDStripData::ChannelData& data, const bool channelEnabled) const;
91  //fill the vector with channel data for raw mode
92  void fillRawChannelBuffer(std::vector<uint8_t>* channelBuffer, const uint8_t packetCode,
93  const FEDStripData::ChannelData& data, const bool channelEnabled, const bool reorderData) const;
94  //fill the vector with channel data for zero suppressed modes
95  void fillZeroSuppressedChannelBuffer(std::vector<uint8_t>* channelBuffer, const uint8_t packetCode, const FEDStripData::ChannelData& data, const bool channelEnabled) const;
96  void fillZeroSuppressedLiteChannelBuffer(std::vector<uint8_t>* channelBuffer, const FEDStripData::ChannelData& data, const bool channelEnabled, const FEDReadoutMode mode) const;
97  void fillPreMixRawChannelBuffer(std::vector<uint8_t>* channelBuffer, const FEDStripData::ChannelData& data, const bool channelEnabled) const;
98  //add the ZS cluster data for the channel to the end of the vector
99  void fillClusterData(std::vector<uint8_t>* channelBuffer, uint8_t packetCode, const FEDStripData::ChannelData& data, const FEDReadoutMode mode) const;
100  void fillClusterDataPreMixMode(std::vector<uint8_t>* channelBuffer, const FEDStripData::ChannelData& data) const;
101  std::vector<bool> feUnitsEnabled_;
102  std::vector<bool> channelsEnabled_;
103  };
104 
106  {
107  public:
108  //constructor in which you can specify the defaults for some parameters
109  FEDBufferGenerator(const uint32_t l1ID = 0,
110  const uint16_t bxID = 0,
111  const std::vector<bool>& feUnitsEnabled = std::vector<bool>(FEUNITS_PER_FED,true),
112  const std::vector<bool>& channelsEnabled = std::vector<bool>(FEDCH_PER_FED,true),
113  const FEDReadoutMode readoutMode = READOUT_MODE_ZERO_SUPPRESSED,
114  const FEDHeaderType headerType = HEADER_TYPE_FULL_DEBUG,
115  const FEDBufferFormat bufferFormat = BUFFER_FORMAT_OLD_SLINK,
116  const FEDDAQEventType evtType = DAQ_EVENT_TYPE_PHYSICS);
117  //methods to get and set the defaults
118  uint32_t getL1ID() const;
119  uint16_t getBXID() const;
120  FEDReadoutMode getReadoutMode() const;
121  FEDHeaderType getHeaderType() const;
122  FEDBufferFormat getBufferFormat() const;
123  FEDDAQEventType getDAQEventType() const;
124  FEDBufferGenerator& setL1ID(const uint32_t newL1ID);
125  FEDBufferGenerator& setBXID(const uint16_t newBXID);
126  FEDBufferGenerator& setReadoutMode(const FEDReadoutMode newReadoutMode);
127  FEDBufferGenerator& setHeaderType(const FEDHeaderType newHeaderType);
128  FEDBufferGenerator& setBufferFormat(const FEDBufferFormat newBufferFormat);
129  FEDBufferGenerator& setDAQEventType(const FEDDAQEventType newDAQEventType);
130  //disabled FE units produce no data at all
131  //disabled channels have headers but data is all zeros (raw modes) or have no clusters (ZS)
132  bool getFEUnitEnabled(const uint8_t internalFEUnitNumber) const;
133  bool getChannelEnabled(const uint8_t internalFEDChannelNumber) const;
134  FEDBufferGenerator& setFEUnitEnable(const uint8_t internalFEUnitNumber, const bool enabled);
135  FEDBufferGenerator& setChannelEnable(const uint8_t internalFEDChannelNumber, const bool enabled);
136  FEDBufferGenerator& setFEUnitEnables(const std::vector<bool>& feUnitsEnabled);
137  FEDBufferGenerator& setChannelEnables(const std::vector<bool>& channelsEnabled);
138  //make finer changes to defaults for parts of buffer
139  //setting source ID in DAQ header and length and CRC in DAQ trailer has no effect since they are set when buffer is built
140  FEDDAQHeader& daqHeader();
141  FEDDAQTrailer& daqTrailer();
142  TrackerSpecialHeader& trackerSpecialHeader();
143  FEDFEHeader& feHeader();
144  //method to generate buffer
145  //unspecified parameters use defaults set by constructor or setters
146  //FEDRawData object will be resized to fit buffer and filled
147  void generateBuffer(FEDRawData* rawDataObject,
148  const FEDStripData& data,
149  uint16_t sourceID,
150  uint8_t packetCode) const;
151  private:
152  //method to fill buffer at pointer from pre generated components (only the length and CRC will be changed)
153  //at least bufferSizeInBytes(feHeader,payload) must have already been allocated
154  static void fillBuffer(uint8_t* pointerToStartOfBuffer,
155  const FEDDAQHeader& daqHeader,
156  const FEDDAQTrailer& daqTrailer,
157  const TrackerSpecialHeader& tkSpecialHeader,
158  const FEDFEHeader& feHeader,
159  const FEDBufferPayload& payload);
160  //returns required size of buffer from given components
161  static size_t bufferSizeInBytes(const FEDFEHeader& feHeader,
162  const FEDBufferPayload& payload);
163  //used to store default values
167  std::unique_ptr<FEDFEHeader> defaultFEHeader_;
168  std::vector<bool> feUnitsEnabled_;
169  std::vector<bool> channelsEnabled_;
170  };
171 
172  //
173  // Inline function definitions
174  //
175 
176  //FEDStripData
177 
178  inline FEDStripData::FEDStripData(const std::vector<ChannelData>& data)
179  : data_(data)
180  { }
181 
182  //re-use non-const method
184  {
185  return const_cast<ChannelData&>(static_cast<const FEDStripData*>(this)->channel(internalFEDChannelNum));
186  }
187 
189  {
190  return channel(internalFEDChannelNum);
191  }
192 
194  {
195  return channel(internalFEDChannelNum);
196  }
197 
198  inline FEDStripData::ChannelData::ChannelData(bool dataIsAlreadyConvertedTo8Bit, const size_t numberOfSamples,
199  const std::pair<uint16_t,uint16_t> medians)
200  : medians_(medians),
201  data_(numberOfSamples,0),
202  dataIs8Bit_(dataIsAlreadyConvertedTo8Bit)
203  { }
204 
205  inline size_t FEDStripData::ChannelData::size() const
206  {
207  return data_.size();
208  }
209 
210  inline const uint16_t& FEDStripData::ChannelData::operator [] (const size_t sampleNumber) const
211  {
212  return data_[sampleNumber];
213  }
214 
215  //re-use const method
216  inline uint16_t& FEDStripData::ChannelData::operator [] (const size_t sampleNumber)
217  {
218  return const_cast<uint16_t&>(static_cast<const ChannelData&>(*this)[sampleNumber]);
219  }
220 
221  inline uint16_t FEDStripData::ChannelData::getSample(const uint16_t sampleNumber) const
222  {
223  //try {
224  // return data_.at(sampleNumber);
225  //} catch (const std::out_of_range&) {
226  // std::ostringstream ss;
227  // ss << "Sample index out of range. "
228  // << "Requesting sample " << sampleNumber
229  // << " when channel has only " << data_.size() << " samples.";
230  // throw cms::Exception("FEDBufferGenerator") << ss.str();
231  //}
232  return data_[sampleNumber];
233  }
234 
235  inline uint8_t FEDStripData::ChannelData::get8BitSample(const uint16_t sampleNumber, uint16_t nBotBitsToDrop) const
236  {
237  uint16_t sample = getSample(sampleNumber) >> nBotBitsToDrop;
238  if (dataIs8Bit_) {
239  return (0xFF & sample);
240  }
241  else {
242  if (sample < 0xFE) return sample;
243  else if (sample == 0x3FF) return 0xFF;
244  else return 0xFE;
245  }
246  }
247 
248  inline uint16_t FEDStripData::ChannelData::get10BitSample(const uint16_t sampleNumber) const
249  {
250  if (dataIs8Bit_) {
251  return (0xFF & getSample(sampleNumber));
252  }
253  else {
254  const uint16_t sample = getSample(sampleNumber);
255  if (sample < 0x3FF) return sample;
256  else return 0x3FF;
257  }
258  }
259 
260  inline std::pair<uint16_t,uint16_t> FEDStripData::ChannelData::getMedians() const
261  {
262  return medians_;
263  }
264 
265  inline void FEDStripData::ChannelData::setMedians(const std::pair<uint16_t,uint16_t> values)
266  {
267  medians_ = values;
268  }
269 
270  //FEDBufferPayload
271 
272  inline size_t FEDBufferPayload::lengthInBytes() const
273  {
274  return data_.size();
275  }
276 
277  inline void FEDBufferPayload::appendToBuffer(size_t* pIndexInBuffer, const uint8_t value)
278  {
279  data_[((*pIndexInBuffer)++)^7] = value;
280  }
281 
282  inline void FEDBufferPayload::appendToBuffer(size_t* pIndexInBuffer, std::vector<uint8_t>::const_iterator start, std::vector<uint8_t>::const_iterator finish)
283  {
284  for (std::vector<uint8_t>::const_iterator iVal = start; iVal != finish; iVal++) {
285  appendToBuffer(pIndexInBuffer,*iVal);
286  }
287  }
288 
289  //FEDBufferPayloadCreator
290 
291  inline FEDBufferPayloadCreator::FEDBufferPayloadCreator(const std::vector<bool>& feUnitsEnabled, const std::vector<bool>& channelsEnabled)
292  : feUnitsEnabled_(feUnitsEnabled),
293  channelsEnabled_(channelsEnabled)
294  {}
295 
297  {
298  return createPayload(mode, packetCode, data);
299  }
300 
301  //FEDBufferGenerator
302 
303  inline uint32_t FEDBufferGenerator::getL1ID() const
304  {
305  return defaultDAQHeader_.l1ID();
306  }
307 
308  inline uint16_t FEDBufferGenerator::getBXID() const
309  {
310  return defaultDAQHeader_.bxID();
311  }
312 
314  {
315  return defaultTrackerSpecialHeader_.readoutMode();
316  }
317 
319  {
320  return defaultTrackerSpecialHeader_.headerType();
321  }
322 
324  {
325  return defaultTrackerSpecialHeader_.bufferFormat();
326  }
327 
329  {
330  return defaultDAQHeader_.eventType();
331  }
332 
333  inline FEDBufferGenerator& FEDBufferGenerator::setL1ID(const uint32_t newL1ID)
334  {
335  defaultDAQHeader_.setL1ID(newL1ID);
336  return *this;
337  }
338 
339  inline FEDBufferGenerator& FEDBufferGenerator::setBXID(const uint16_t newBXID)
340  {
341  defaultDAQHeader_.setBXID(newBXID);
342  return *this;
343  }
344 
346  {
347  defaultTrackerSpecialHeader_.setReadoutMode(newReadoutMode);
348  return *this;
349  }
350 
352  {
353  defaultTrackerSpecialHeader_.setHeaderType(newHeaderType);
354  return *this;
355  }
356 
358  {
359  defaultTrackerSpecialHeader_.setBufferFormat(newBufferFormat);
360  return *this;
361  }
362 
364  {
365  defaultDAQHeader_.setEventType(newDAQEventType);
366  return *this;
367  }
368 
370  {
371  return defaultDAQHeader_;
372  }
373 
375  {
376  return defaultDAQTrailer_;
377  }
378 
380  {
381  return defaultTrackerSpecialHeader_;
382  }
383 
385  {
386  return *defaultFEHeader_;
387  }
388 
389  inline size_t FEDBufferGenerator::bufferSizeInBytes(const FEDFEHeader& feHeader,
390  const FEDBufferPayload& payload)
391  {
392  //FE header + payload + tracker special header + daq header + daq trailer
393  return feHeader.lengthInBytes()+payload.lengthInBytes()+8+8+8;
394  }
395 
396 }
397 
398 #endif //ndef EventFilter_SiStripRawToDigi_FEDBufferGenerator_H
Definition: start.py:1
void setSample(const uint16_t sampleNumber, const uint16_t adcValue)
void setMedians(const std::pair< uint16_t, uint16_t > values)
FEDBufferGenerator & setReadoutMode(const FEDReadoutMode newReadoutMode)
FEDBufferPayload operator()(FEDReadoutMode mode, uint8_t packetCode, const FEDStripData &data) const
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)
TrackerSpecialHeader & trackerSpecialHeader()
FEDBufferGenerator & setBufferFormat(const FEDBufferFormat newBufferFormat)
FEDReadoutMode getReadoutMode() const
FEDBufferFormat getBufferFormat() const
sistrip classes
std::unique_ptr< FEDFEHeader > defaultFEHeader_
ChannelData(bool dataIsAlreadyConvertedTo8Bit, const size_t numberOfSamples, const std::pair< uint16_t, uint16_t > medians=std::make_pair< uint16_t >(0, 0))
uint16_t get10BitSample(const uint16_t sampleNumber) const
uint8_t get8BitSample(const uint16_t sampleNumber, uint16_t nBotBitsToDrop) const
FEDBufferPayloadCreator(const std::vector< bool > &enabledFEUnits, const std::vector< bool > &enabledChannels)
T * createPayload(const std::string &payloadTypeName)
Definition: Serialization.h:30
std::pair< uint16_t, uint16_t > getMedians() const
static const uint16_t FEUNITS_PER_FED
Definition: value.py:1
void appendToBuffer(size_t *pIndexInBuffer, const uint8_t value)
FEDBufferGenerator & setDAQEventType(const FEDDAQEventType newDAQEventType)
static const uint16_t STRIPS_PER_FEDCH
FEDBufferGenerator & setHeaderType(const FEDHeaderType newHeaderType)
ChannelData & channel(const uint8_t internalFEDChannelNum)
ChannelData & operator[](const uint8_t internalFEDChannelNum)
FEDBufferGenerator & setL1ID(const uint32_t newL1ID)
uint16_t getSample(const uint16_t sampleNumber) const
virtual size_t lengthInBytes() const =0
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
std::vector< uint16_t > feLengths_
static const uint16_t FEDCH_PER_FED
std::vector< ChannelData > data_
std::pair< uint16_t, uint16_t > medians_
FEDDAQEventType getDAQEventType() const
FEDBufferGenerator & setBXID(const uint16_t newBXID)
TrackerSpecialHeader defaultTrackerSpecialHeader_
FEDBufferPayload createPayload(FEDReadoutMode mode, uint8_t packetCode, const FEDStripData &data) const
uint16_t & operator[](const size_t sampleNumber)