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  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;
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;
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,
118  //methods to get and set the defaults
119  uint32_t getL1ID() const;
120  uint16_t getBXID() const;
125  FEDDataType getFEDDataType() const;
126  FEDBufferGenerator& setL1ID(const uint32_t newL1ID);
127  FEDBufferGenerator& setBXID(const uint16_t newBXID);
128  FEDBufferGenerator& setReadoutMode(const FEDReadoutMode newReadoutMode);
129  FEDBufferGenerator& setHeaderType(const FEDHeaderType newHeaderType);
130  FEDBufferGenerator& setBufferFormat(const FEDBufferFormat newBufferFormat);
131  FEDBufferGenerator& setDAQEventType(const FEDDAQEventType newDAQEventType);
132  FEDBufferGenerator& setFEDDataType(const FEDDataType newFEDDataType);
133  //disabled FE units produce no data at all
134  //disabled channels have headers but data is all zeros (raw modes) or have no clusters (ZS)
135  bool getFEUnitEnabled(const uint8_t internalFEUnitNumber) const;
136  bool getChannelEnabled(const uint8_t internalFEDChannelNumber) const;
137  FEDBufferGenerator& setFEUnitEnable(const uint8_t internalFEUnitNumber, const bool enabled);
138  FEDBufferGenerator& setChannelEnable(const uint8_t internalFEDChannelNumber, const bool enabled);
139  FEDBufferGenerator& setFEUnitEnables(const std::vector<bool>& feUnitsEnabled);
140  FEDBufferGenerator& setChannelEnables(const std::vector<bool>& channelsEnabled);
141  //make finer changes to defaults for parts of buffer
142  //setting source ID in DAQ header and length and CRC in DAQ trailer has no effect since they are set when buffer is built
147  //method to generate buffer
148  //unspecified parameters use defaults set by constructor or setters
149  //FEDRawData object will be resized to fit buffer and filled
150  void generateBuffer(FEDRawData* rawDataObject,
151  const FEDStripData& data,
152  const uint16_t sourceID) const;
153  private:
154  //method to fill buffer at pointer from pre generated components (only the length and CRC will be changed)
155  //at least bufferSizeInBytes(feHeader,payload) must have already been allocated
156  static void fillBuffer(uint8_t* pointerToStartOfBuffer,
157  const FEDDAQHeader& daqHeader,
158  const FEDDAQTrailer& daqTrailer,
159  const TrackerSpecialHeader& tkSpecialHeader,
160  const FEDFEHeader& feHeader,
161  const FEDBufferPayload& payload);
162  //returns required size of buffer from given components
163  static size_t bufferSizeInBytes(const FEDFEHeader& feHeader,
164  const FEDBufferPayload& payload);
165  //used to store default values
169  std::auto_ptr<FEDFEHeader> defaultFEHeader_;
170  std::vector<bool> feUnitsEnabled_;
171  std::vector<bool> channelsEnabled_;
172  };
173 
174  //
175  // Inline function definitions
176  //
177 
178  //FEDStripData
179 
180  inline FEDStripData::FEDStripData(const std::vector<ChannelData>& data)
181  : data_(data)
182  { }
183 
184  //re-use non-const method
186  {
187  return const_cast<ChannelData&>(static_cast<const FEDStripData*>(this)->channel(internalFEDChannelNum));
188  }
189 
191  {
192  return channel(internalFEDChannelNum);
193  }
194 
196  {
197  return channel(internalFEDChannelNum);
198  }
199 
200  inline FEDStripData::ChannelData::ChannelData(bool dataIsAlreadyConvertedTo8Bit, const size_t numberOfSamples,
201  const std::pair<uint16_t,uint16_t> medians)
202  : medians_(medians),
203  data_(numberOfSamples,0),
204  dataIs8Bit_(dataIsAlreadyConvertedTo8Bit)
205  { }
206 
207  inline size_t FEDStripData::ChannelData::size() const
208  {
209  return data_.size();
210  }
211 
212  inline const uint16_t& FEDStripData::ChannelData::operator [] (const size_t sampleNumber) const
213  {
214  return data_[sampleNumber];
215  }
216 
217  //re-use const method
218  inline uint16_t& FEDStripData::ChannelData::operator [] (const size_t sampleNumber)
219  {
220  return const_cast<uint16_t&>(static_cast<const ChannelData&>(*this)[sampleNumber]);
221  }
222 
223  inline uint16_t FEDStripData::ChannelData::getSample(const uint16_t sampleNumber) const
224  {
225  //try {
226  // return data_.at(sampleNumber);
227  //} catch (const std::out_of_range&) {
228  // std::ostringstream ss;
229  // ss << "Sample index out of range. "
230  // << "Requesting sample " << sampleNumber
231  // << " when channel has only " << data_.size() << " samples.";
232  // throw cms::Exception("FEDBufferGenerator") << ss.str();
233  //}
234  return data_[sampleNumber];
235  }
236 
237  inline uint8_t FEDStripData::ChannelData::get8BitSample(const uint16_t sampleNumber) const
238  {
239  if (dataIs8Bit_) return (0xFF & getSample(sampleNumber));
240  else {
241  const uint16_t sample = getSample(sampleNumber);
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,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  {
316  }
317 
319  {
321  }
322 
324  {
326  }
327 
329  {
330  return defaultDAQHeader_.eventType();
331  }
332 
334  {
336  }
337 
338  inline FEDBufferGenerator& FEDBufferGenerator::setL1ID(const uint32_t newL1ID)
339  {
340  defaultDAQHeader_.setL1ID(newL1ID);
341  return *this;
342  }
343 
344  inline FEDBufferGenerator& FEDBufferGenerator::setBXID(const uint16_t newBXID)
345  {
346  defaultDAQHeader_.setBXID(newBXID);
347  return *this;
348  }
349 
351  {
353  return *this;
354  }
355 
357  {
359  return *this;
360  }
361 
363  {
365  return *this;
366  }
367 
369  {
370  defaultDAQHeader_.setEventType(newDAQEventType);
371  return *this;
372  }
373 
375  {
377  return *this;
378  }
379 
381  {
382  return defaultDAQHeader_;
383  }
384 
386  {
387  return defaultDAQTrailer_;
388  }
389 
391  {
393  }
394 
396  {
397  return *defaultFEHeader_;
398  }
399 
400  inline size_t FEDBufferGenerator::bufferSizeInBytes(const FEDFEHeader& feHeader,
401  const FEDBufferPayload& payload)
402  {
403  //FE header + payload + tracker special header + daq header + daq trailer
404  return feHeader.lengthInBytes()+payload.lengthInBytes()+8+8+8;
405  }
406 
407 }
408 
409 #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 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)
tuple start
Check for commandline option errors.
Definition: dqm_diff.py:58
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)
uint16_t get10BitSample(const uint16_t sampleNumber) const
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)
void fillPreMixRawChannelBuffer(std::vector< uint8_t > *channelBuffer, const FEDStripData::ChannelData &data, const bool channelEnabled) const
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)