CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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;
36  void setSample(const uint16_t sampleNumber, const uint16_t adcValue);
37  //setting value directly is equivalent to get and set Sample but without length check
38  uint16_t& operator [] (const size_t sampleNumber);
39  const uint16_t& operator [] (const size_t sampleNumber) const;
40  private:
41  std::pair<uint16_t,uint16_t> medians_;
42  std::vector<uint16_t> data_;
44  };
45 
46  FEDStripData(const std::vector<ChannelData>& data);
47  //specify whether the data is already in the 8bit ZS format (only affects what is returned by ChannelData::get8BitSample() if the value if >253)
48  //if the data is for scope mode then specify the scope length
49  FEDStripData(bool dataIsAlreadyConvertedTo8Bit = true, const size_t samplesPerChannel = STRIPS_PER_FEDCH);
50  //access to elements
52  const ChannelData& operator [] (const uint8_t internalFEDChannelNum) const;
54  const ChannelData& channel(const uint8_t internalFEDChannelNum) const;
55  private:
56  std::vector<ChannelData> data_;
57  };
58 
60  {
61  public:
62  FEDBufferPayload(const std::vector< std::vector<uint8_t> >& channelBuffers);
63  //size of payload in bytes
64  size_t lengthInBytes() const;
65  //returns NULL if payload size is 0, otherwise return a pointer to the payload buffer
66  const uint8_t* data() const;
67  //size of FE unit payload
68  uint16_t getFELength(const uint8_t internalFEUnitNum) const;
69  private:
70  void appendToBuffer(size_t* pIndexInBuffer, const uint8_t value);
71  void appendToBuffer(size_t* pIndexInBuffer, std::vector<uint8_t>::const_iterator start, std::vector<uint8_t>::const_iterator finish);
72  std::vector<uint8_t> data_;
73  std::vector<uint16_t> feLengths_;
74  };
75 
77  {
78  public:
79  //specify which FE units and channels should have data generated for them
80  //If an FE unit is disabled then the channel is as well. The whole FE payload will be missing.
81  //If a channel is disabled then it is considered to have all zeros in the data but will be present in the data
82  FEDBufferPayloadCreator(const std::vector<bool>& enabledFEUnits, const std::vector<bool>& enabledChannels);
83  //create the payload for a particular mode
85  FEDBufferPayload operator () (const FEDReadoutMode mode, const FEDStripData& data) const;
86  private:
87  //fill vector with channel data
88  void fillChannelBuffer(std::vector<uint8_t>* channelBuffer, const FEDReadoutMode mode,
89  const FEDStripData::ChannelData& data, const bool channelEnabled) const;
90  //fill the vector with channel data for raw mode
91  void fillRawChannelBuffer(std::vector<uint8_t>* channelBuffer, const uint8_t packetCode,
92  const FEDStripData::ChannelData& data, const bool channelEnabled, const bool reorderData) const;
93  //fill the vector with channel data for zero suppressed modes
94  void fillZeroSuppressedChannelBuffer(std::vector<uint8_t>* channelBuffer, const FEDStripData::ChannelData& data, const bool channelEnabled) const;
95  void fillZeroSuppressedLiteChannelBuffer(std::vector<uint8_t>* channelBuffer, const FEDStripData::ChannelData& data, const bool channelEnabled) const;
96  //add the ZS cluster data for the channel to the end of the vector
97  void fillClusterData(std::vector<uint8_t>* channelBuffer, const FEDStripData::ChannelData& data) const;
98  std::vector<bool> feUnitsEnabled_;
99  std::vector<bool> channelsEnabled_;
100  };
101 
103  {
104  public:
105  //constructor in which you can specify the defaults for some parameters
106  FEDBufferGenerator(const uint32_t l1ID = 0,
107  const uint16_t bxID = 0,
108  const std::vector<bool>& feUnitsEnabled = std::vector<bool>(FEUNITS_PER_FED,true),
109  const std::vector<bool>& channelsEnabled = std::vector<bool>(FEDCH_PER_FED,true),
110  const FEDReadoutMode readoutMode = READOUT_MODE_ZERO_SUPPRESSED,
111  const FEDHeaderType headerType = HEADER_TYPE_FULL_DEBUG,
112  const FEDBufferFormat bufferFormat = BUFFER_FORMAT_OLD_SLINK,
113  const FEDDAQEventType evtType = DAQ_EVENT_TYPE_PHYSICS,
115  //methods to get and set the defaults
116  uint32_t getL1ID() const;
117  uint16_t getBXID() const;
122  FEDDataType getFEDDataType() const;
123  FEDBufferGenerator& setL1ID(const uint32_t newL1ID);
124  FEDBufferGenerator& setBXID(const uint16_t newBXID);
125  FEDBufferGenerator& setReadoutMode(const FEDReadoutMode newReadoutMode);
126  FEDBufferGenerator& setHeaderType(const FEDHeaderType newHeaderType);
127  FEDBufferGenerator& setBufferFormat(const FEDBufferFormat newBufferFormat);
128  FEDBufferGenerator& setDAQEventType(const FEDDAQEventType newDAQEventType);
129  FEDBufferGenerator& setFEDDataType(const FEDDataType newFEDDataType);
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
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
235  {
236  if (dataIs8Bit_) return (0xFF & getSample(sampleNumber));
237  else {
238  const uint16_t sample = getSample(sampleNumber);
239  if (sample < 0xFE) return sample;
240  else if (sample == 0x3FF) return 0xFF;
241  else return 0xFE;
242  }
243  }
244 
245  inline std::pair<uint16_t,uint16_t> FEDStripData::ChannelData::getMedians() const
246  {
247  return medians_;
248  }
249 
250  inline void FEDStripData::ChannelData::setMedians(const std::pair<uint16_t,uint16_t> values)
251  {
252  medians_ = values;
253  }
254 
255  //FEDBufferPayload
256 
257  inline size_t FEDBufferPayload::lengthInBytes() const
258  {
259  return data_.size();
260  }
261 
262  inline void FEDBufferPayload::appendToBuffer(size_t* pIndexInBuffer, const uint8_t value)
263  {
264  data_[((*pIndexInBuffer)++)^7] = value;
265  }
266 
267  inline void FEDBufferPayload::appendToBuffer(size_t* pIndexInBuffer, std::vector<uint8_t>::const_iterator start, std::vector<uint8_t>::const_iterator finish)
268  {
269  for (std::vector<uint8_t>::const_iterator iVal = start; iVal != finish; iVal++) {
270  appendToBuffer(pIndexInBuffer,*iVal);
271  }
272  }
273 
274  //FEDBufferPayloadCreator
275 
276  inline FEDBufferPayloadCreator::FEDBufferPayloadCreator(const std::vector<bool>& feUnitsEnabled, const std::vector<bool>& channelsEnabled)
277  : feUnitsEnabled_(feUnitsEnabled),
278  channelsEnabled_(channelsEnabled)
279  {}
280 
282  {
283  return createPayload(mode,data);
284  }
285 
286  //FEDBufferGenerator
287 
288  inline uint32_t FEDBufferGenerator::getL1ID() const
289  {
290  return defaultDAQHeader_.l1ID();
291  }
292 
293  inline uint16_t FEDBufferGenerator::getBXID() const
294  {
295  return defaultDAQHeader_.bxID();
296  }
297 
299  {
301  }
302 
304  {
306  }
307 
309  {
311  }
312 
314  {
315  return defaultDAQHeader_.eventType();
316  }
317 
319  {
321  }
322 
323  inline FEDBufferGenerator& FEDBufferGenerator::setL1ID(const uint32_t newL1ID)
324  {
325  defaultDAQHeader_.setL1ID(newL1ID);
326  return *this;
327  }
328 
329  inline FEDBufferGenerator& FEDBufferGenerator::setBXID(const uint16_t newBXID)
330  {
331  defaultDAQHeader_.setBXID(newBXID);
332  return *this;
333  }
334 
336  {
338  return *this;
339  }
340 
342  {
344  return *this;
345  }
346 
348  {
350  return *this;
351  }
352 
354  {
355  defaultDAQHeader_.setEventType(newDAQEventType);
356  return *this;
357  }
358 
360  {
362  return *this;
363  }
364 
366  {
367  return defaultDAQHeader_;
368  }
369 
371  {
372  return defaultDAQTrailer_;
373  }
374 
376  {
378  }
379 
381  {
382  return *defaultFEHeader_;
383  }
384 
385  inline size_t FEDBufferGenerator::bufferSizeInBytes(const FEDFEHeader& feHeader,
386  const FEDBufferPayload& payload)
387  {
388  //FE header + payload + tracker special header + daq header + daq trailer
389  return feHeader.lengthInBytes()+payload.lengthInBytes()+8+8+8;
390  }
391 
392 }
393 
394 #endif //ndef EventFilter_SiStripRawToDigi_FEDBufferGenerator_H
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)
TrackerSpecialHeader & setDataType(const FEDDataType dataType)
void generateBuffer(FEDRawData *rawDataObject, const FEDStripData &data, const uint16_t sourceID) const
void setMedians(const std::pair< uint16_t, uint16_t > values)
FEDBufferGenerator & setReadoutMode(const FEDReadoutMode newReadoutMode)
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)
TrackerSpecialHeader & setBufferFormat(const FEDBufferFormat newBufferFormat)
TrackerSpecialHeader & trackerSpecialHeader()
FEDBufferGenerator & setBufferFormat(const FEDBufferFormat newBufferFormat)
FEDReadoutMode getReadoutMode() const
FEDBufferFormat getBufferFormat() const
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)
FEDBufferPayloadCreator(const std::vector< bool > &enabledFEUnits, const std::vector< bool > &enabledChannels)
uint8_t get8BitSample(const uint16_t sampleNumber) const
FEDBufferGenerator & setFEUnitEnables(const std::vector< bool > &feUnitsEnabled)
FEDBufferGenerator & setFEDDataType(const FEDDataType newFEDDataType)
std::pair< uint16_t, uint16_t > getMedians() const
static const uint16_t FEUNITS_PER_FED
uint16_t getFELength(const uint8_t internalFEUnitNum) 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 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 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)
std::auto_ptr< FEDFEHeader > defaultFEHeader_
FEDDAQHeader & setL1ID(const uint32_t l1ID)
int mode
Definition: AMPTWrapper.h:139
FEDBufferPayload operator()(const FEDReadoutMode mode, const FEDStripData &data) const
FEDBufferGenerator & setFEUnitEnable(const uint8_t internalFEUnitNumber, const bool enabled)
bool getFEUnitEnabled(const uint8_t internalFEUnitNumber) const
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_
void fillChannelBuffer(std::vector< uint8_t > *channelBuffer, const FEDReadoutMode mode, const FEDStripData::ChannelData &data, const bool channelEnabled) const
std::pair< uint16_t, uint16_t > medians_
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
uint16_t & operator[](const size_t sampleNumber)
TrackerSpecialHeader & setReadoutMode(const FEDReadoutMode readoutMode)