CMS 3D CMS Logo

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