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, const FEDReadoutMode mode) 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
86  FEDBufferPayload operator () (const FEDReadoutMode mode, const FEDStripData& data) const;
87  private:
88  //fill vector with channel data
89  void fillChannelBuffer(std::vector<uint8_t>* channelBuffer, const FEDReadoutMode mode,
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 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, 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  const uint16_t sourceID) const;
150  private:
151  //method to fill buffer at pointer from pre generated components (only the length and CRC will be changed)
152  //at least bufferSizeInBytes(feHeader,payload) must have already been allocated
153  static void fillBuffer(uint8_t* pointerToStartOfBuffer,
154  const FEDDAQHeader& daqHeader,
155  const FEDDAQTrailer& daqTrailer,
156  const TrackerSpecialHeader& tkSpecialHeader,
157  const FEDFEHeader& feHeader,
158  const FEDBufferPayload& payload);
159  //returns required size of buffer from given components
160  static size_t bufferSizeInBytes(const FEDFEHeader& feHeader,
161  const FEDBufferPayload& payload);
162  //used to store default values
166  std::auto_ptr<FEDFEHeader> defaultFEHeader_;
167  std::vector<bool> feUnitsEnabled_;
168  std::vector<bool> channelsEnabled_;
169  };
170 
171  //
172  // Inline function definitions
173  //
174 
175  //FEDStripData
176 
177  inline FEDStripData::FEDStripData(const std::vector<ChannelData>& data)
178  : data_(data)
179  { }
180 
181  //re-use non-const method
183  {
184  return const_cast<ChannelData&>(static_cast<const FEDStripData*>(this)->channel(internalFEDChannelNum));
185  }
186 
188  {
189  return channel(internalFEDChannelNum);
190  }
191 
193  {
194  return channel(internalFEDChannelNum);
195  }
196 
197  inline FEDStripData::ChannelData::ChannelData(bool dataIsAlreadyConvertedTo8Bit, const size_t numberOfSamples,
198  const std::pair<uint16_t,uint16_t> medians)
199  : medians_(medians),
200  data_(numberOfSamples,0),
201  dataIs8Bit_(dataIsAlreadyConvertedTo8Bit)
202  { }
203 
204  inline size_t FEDStripData::ChannelData::size() const
205  {
206  return data_.size();
207  }
208 
209  inline const uint16_t& FEDStripData::ChannelData::operator [] (const size_t sampleNumber) const
210  {
211  return data_[sampleNumber];
212  }
213 
214  //re-use const method
215  inline uint16_t& FEDStripData::ChannelData::operator [] (const size_t sampleNumber)
216  {
217  return const_cast<uint16_t&>(static_cast<const ChannelData&>(*this)[sampleNumber]);
218  }
219 
220  inline uint16_t FEDStripData::ChannelData::getSample(const uint16_t sampleNumber) const
221  {
222  //try {
223  // return data_.at(sampleNumber);
224  //} catch (const std::out_of_range&) {
225  // std::ostringstream ss;
226  // ss << "Sample index out of range. "
227  // << "Requesting sample " << sampleNumber
228  // << " when channel has only " << data_.size() << " samples.";
229  // throw cms::Exception("FEDBufferGenerator") << ss.str();
230  //}
231  return data_[sampleNumber];
232  }
233 
234  inline uint8_t FEDStripData::ChannelData::get8BitSample(const uint16_t sampleNumber, const FEDReadoutMode mode) const
235  {
236  uint16_t sample = getSample(sampleNumber);
237  // one start shifting the word
238  switch (mode) {
242  break;
245  sample = (sample>>1);
246  break;
249  sample = (sample>>2);
250  break;
251  default:
252  throw cms::Exception("FEDBufferGenerator") << "Invalid readout mode requested for 8-bit sample retrieval";
253  }
254  if (dataIs8Bit_) {
255  return (0xFF & sample);
256  }
257  else {
258  if (sample < 0xFE) return sample;
259  else if (sample == 0x3FF) return 0xFF;
260  else return 0xFE;
261  }
262  }
263 
264  inline uint16_t FEDStripData::ChannelData::get10BitSample(const uint16_t sampleNumber) const
265  {
266  if (dataIs8Bit_) {
267  return (0xFF & getSample(sampleNumber));
268  }
269  else {
270  const uint16_t sample = getSample(sampleNumber);
271  if (sample < 0x3FF) return sample;
272  else return 0x3FF;
273  }
274  }
275 
276  inline std::pair<uint16_t,uint16_t> FEDStripData::ChannelData::getMedians() const
277  {
278  return medians_;
279  }
280 
281  inline void FEDStripData::ChannelData::setMedians(const std::pair<uint16_t,uint16_t> values)
282  {
283  medians_ = values;
284  }
285 
286  //FEDBufferPayload
287 
288  inline size_t FEDBufferPayload::lengthInBytes() const
289  {
290  return data_.size();
291  }
292 
293  inline void FEDBufferPayload::appendToBuffer(size_t* pIndexInBuffer, const uint8_t value)
294  {
295  data_[((*pIndexInBuffer)++)^7] = value;
296  }
297 
298  inline void FEDBufferPayload::appendToBuffer(size_t* pIndexInBuffer, std::vector<uint8_t>::const_iterator start, std::vector<uint8_t>::const_iterator finish)
299  {
300  for (std::vector<uint8_t>::const_iterator iVal = start; iVal != finish; iVal++) {
301  appendToBuffer(pIndexInBuffer,*iVal);
302  }
303  }
304 
305  //FEDBufferPayloadCreator
306 
307  inline FEDBufferPayloadCreator::FEDBufferPayloadCreator(const std::vector<bool>& feUnitsEnabled, const std::vector<bool>& channelsEnabled)
308  : feUnitsEnabled_(feUnitsEnabled),
309  channelsEnabled_(channelsEnabled)
310  {}
311 
313  {
314  return createPayload(mode,data);
315  }
316 
317  //FEDBufferGenerator
318 
319  inline uint32_t FEDBufferGenerator::getL1ID() const
320  {
321  return defaultDAQHeader_.l1ID();
322  }
323 
324  inline uint16_t FEDBufferGenerator::getBXID() const
325  {
326  return defaultDAQHeader_.bxID();
327  }
328 
330  {
331  return defaultTrackerSpecialHeader_.readoutMode();
332  }
333 
335  {
336  return defaultTrackerSpecialHeader_.headerType();
337  }
338 
340  {
341  return defaultTrackerSpecialHeader_.bufferFormat();
342  }
343 
345  {
346  return defaultDAQHeader_.eventType();
347  }
348 
349  inline FEDBufferGenerator& FEDBufferGenerator::setL1ID(const uint32_t newL1ID)
350  {
351  defaultDAQHeader_.setL1ID(newL1ID);
352  return *this;
353  }
354 
355  inline FEDBufferGenerator& FEDBufferGenerator::setBXID(const uint16_t newBXID)
356  {
357  defaultDAQHeader_.setBXID(newBXID);
358  return *this;
359  }
360 
362  {
363  defaultTrackerSpecialHeader_.setReadoutMode(newReadoutMode);
364  return *this;
365  }
366 
368  {
369  defaultTrackerSpecialHeader_.setHeaderType(newHeaderType);
370  return *this;
371  }
372 
374  {
375  defaultTrackerSpecialHeader_.setBufferFormat(newBufferFormat);
376  return *this;
377  }
378 
380  {
381  defaultDAQHeader_.setEventType(newDAQEventType);
382  return *this;
383  }
384 
386  {
387  return defaultDAQHeader_;
388  }
389 
391  {
392  return defaultDAQTrailer_;
393  }
394 
396  {
397  return defaultTrackerSpecialHeader_;
398  }
399 
401  {
402  return *defaultFEHeader_;
403  }
404 
405  inline size_t FEDBufferGenerator::bufferSizeInBytes(const FEDFEHeader& feHeader,
406  const FEDBufferPayload& payload)
407  {
408  //FE header + payload + tracker special header + daq header + daq trailer
409  return feHeader.lengthInBytes()+payload.lengthInBytes()+8+8+8;
410  }
411 
412 }
413 
414 #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)
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
numberOfSamples
threshold for setting fine grain bit
FEDBufferFormat getBufferFormat() const
sistrip classes
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
FEDBufferPayloadCreator(const std::vector< bool > &enabledFEUnits, const std::vector< bool > &enabledChannels)
T * createPayload(const std::string &payloadTypeName)
Definition: Serialization.h:30
payload
payload postfix for testing
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)
FEDBufferPayload createPayload(const FEDReadoutMode mode, const FEDStripData &data) const
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
std::auto_ptr< FEDFEHeader > defaultFEHeader_
FEDBufferPayload operator()(const FEDReadoutMode mode, const FEDStripData &data) 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_
uint8_t get8BitSample(const uint16_t sampleNumber, const FEDReadoutMode mode) const
uint16_t & operator[](const size_t sampleNumber)