00001 #ifndef EventFilter_SiStripRawToDigi_SiStripFEDBufferGenerator_H
00002 #define EventFilter_SiStripRawToDigi_SiStripFEDBufferGenerator_H
00003
00004 #include "boost/cstdint.hpp"
00005 #include "EventFilter/SiStripRawToDigi/interface/SiStripFEDBufferComponents.h"
00006 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
00007 #include <vector>
00008 #include <list>
00009 #include <utility>
00010 #include <memory>
00011
00012 namespace sistrip {
00013
00014
00015
00016
00017
00018 class FEDStripData
00019 {
00020 public:
00021
00022 class ChannelData {
00023 public:
00024 ChannelData(bool dataIsAlreadyConvertedTo8Bit, const size_t numberOfSamples,
00025 const std::pair<uint16_t,uint16_t> medians = std::make_pair<uint16_t>(0,0));
00026
00027 size_t size() const;
00028
00029 std::pair<uint16_t,uint16_t> getMedians() const;
00030
00031 void setMedians(const std::pair<uint16_t,uint16_t> values);
00032
00033 uint16_t getSample(const uint16_t sampleNumber) const;
00034
00035 uint8_t get8BitSample(const uint16_t sampleNumber) const;
00036 void setSample(const uint16_t sampleNumber, const uint16_t adcValue);
00037
00038 uint16_t& operator [] (const size_t sampleNumber);
00039 const uint16_t& operator [] (const size_t sampleNumber) const;
00040 private:
00041 std::pair<uint16_t,uint16_t> medians_;
00042 std::vector<uint16_t> data_;
00043 bool dataIs8Bit_;
00044 };
00045
00046 FEDStripData(const std::vector<ChannelData>& data);
00047
00048
00049 FEDStripData(bool dataIsAlreadyConvertedTo8Bit = true, const size_t samplesPerChannel = STRIPS_PER_FEDCH);
00050
00051 ChannelData& operator [] (const uint8_t internalFEDChannelNum);
00052 const ChannelData& operator [] (const uint8_t internalFEDChannelNum) const;
00053 ChannelData& channel(const uint8_t internalFEDChannelNum);
00054 const ChannelData& channel(const uint8_t internalFEDChannelNum) const;
00055 private:
00056 std::vector<ChannelData> data_;
00057 };
00058
00059 class FEDBufferPayload
00060 {
00061 public:
00062 FEDBufferPayload(const std::vector< std::vector<uint8_t> >& channelBuffers);
00063
00064 size_t lengthInBytes() const;
00065
00066 const uint8_t* data() const;
00067
00068 uint16_t getFELength(const uint8_t internalFEUnitNum) const;
00069 private:
00070 void appendToBuffer(size_t* pIndexInBuffer, const uint8_t value);
00071 void appendToBuffer(size_t* pIndexInBuffer, std::vector<uint8_t>::const_iterator start, std::vector<uint8_t>::const_iterator finish);
00072 std::vector<uint8_t> data_;
00073 std::vector<uint16_t> feLengths_;
00074 };
00075
00076 class FEDBufferPayloadCreator
00077 {
00078 public:
00079
00080
00081
00082 FEDBufferPayloadCreator(const std::vector<bool>& enabledFEUnits, const std::vector<bool>& enabledChannels);
00083
00084 FEDBufferPayload createPayload(const FEDReadoutMode mode, const FEDStripData& data) const;
00085 FEDBufferPayload operator () (const FEDReadoutMode mode, const FEDStripData& data) const;
00086 private:
00087
00088 void fillChannelBuffer(std::vector<uint8_t>* channelBuffer, const FEDReadoutMode mode,
00089 const FEDStripData::ChannelData& data, const bool channelEnabled) const;
00090
00091 void fillRawChannelBuffer(std::vector<uint8_t>* channelBuffer, const uint8_t packetCode,
00092 const FEDStripData::ChannelData& data, const bool channelEnabled, const bool reorderData) const;
00093
00094 void fillZeroSuppressedChannelBuffer(std::vector<uint8_t>* channelBuffer, const FEDStripData::ChannelData& data, const bool channelEnabled) const;
00095 void fillZeroSuppressedLiteChannelBuffer(std::vector<uint8_t>* channelBuffer, const FEDStripData::ChannelData& data, const bool channelEnabled) const;
00096
00097 void fillClusterData(std::vector<uint8_t>* channelBuffer, const FEDStripData::ChannelData& data) const;
00098 std::vector<bool> feUnitsEnabled_;
00099 std::vector<bool> channelsEnabled_;
00100 };
00101
00102 class FEDBufferGenerator
00103 {
00104 public:
00105
00106 FEDBufferGenerator(const uint32_t l1ID = 0,
00107 const uint16_t bxID = 0,
00108 const std::vector<bool>& feUnitsEnabled = std::vector<bool>(FEUNITS_PER_FED,true),
00109 const std::vector<bool>& channelsEnabled = std::vector<bool>(FEDCH_PER_FED,true),
00110 const FEDReadoutMode readoutMode = READOUT_MODE_ZERO_SUPPRESSED,
00111 const FEDHeaderType headerType = HEADER_TYPE_FULL_DEBUG,
00112 const FEDBufferFormat bufferFormat = BUFFER_FORMAT_OLD_SLINK,
00113 const FEDDAQEventType evtType = DAQ_EVENT_TYPE_PHYSICS,
00114 const FEDDataType dataType = DATA_TYPE_FAKE);
00115
00116 uint32_t getL1ID() const;
00117 uint16_t getBXID() const;
00118 FEDReadoutMode getReadoutMode() const;
00119 FEDHeaderType getHeaderType() const;
00120 FEDBufferFormat getBufferFormat() const;
00121 FEDDAQEventType getDAQEventType() const;
00122 FEDDataType getFEDDataType() const;
00123 FEDBufferGenerator& setL1ID(const uint32_t newL1ID);
00124 FEDBufferGenerator& setBXID(const uint16_t newBXID);
00125 FEDBufferGenerator& setReadoutMode(const FEDReadoutMode newReadoutMode);
00126 FEDBufferGenerator& setHeaderType(const FEDHeaderType newHeaderType);
00127 FEDBufferGenerator& setBufferFormat(const FEDBufferFormat newBufferFormat);
00128 FEDBufferGenerator& setDAQEventType(const FEDDAQEventType newDAQEventType);
00129 FEDBufferGenerator& setFEDDataType(const FEDDataType newFEDDataType);
00130
00131
00132 bool getFEUnitEnabled(const uint8_t internalFEUnitNumber) const;
00133 bool getChannelEnabled(const uint8_t internalFEDChannelNumber) const;
00134 FEDBufferGenerator& setFEUnitEnable(const uint8_t internalFEUnitNumber, const bool enabled);
00135 FEDBufferGenerator& setChannelEnable(const uint8_t internalFEDChannelNumber, const bool enabled);
00136 FEDBufferGenerator& setFEUnitEnables(const std::vector<bool>& feUnitsEnabled);
00137 FEDBufferGenerator& setChannelEnables(const std::vector<bool>& channelsEnabled);
00138
00139
00140 FEDDAQHeader& daqHeader();
00141 FEDDAQTrailer& daqTrailer();
00142 TrackerSpecialHeader& trackerSpecialHeader();
00143 FEDFEHeader& feHeader();
00144
00145
00146
00147 void generateBuffer(FEDRawData* rawDataObject,
00148 const FEDStripData& data,
00149 const uint16_t sourceID) const;
00150 private:
00151
00152
00153 static void fillBuffer(uint8_t* pointerToStartOfBuffer,
00154 const FEDDAQHeader& daqHeader,
00155 const FEDDAQTrailer& daqTrailer,
00156 const TrackerSpecialHeader& tkSpecialHeader,
00157 const FEDFEHeader& feHeader,
00158 const FEDBufferPayload& payload);
00159
00160 static size_t bufferSizeInBytes(const FEDFEHeader& feHeader,
00161 const FEDBufferPayload& payload);
00162
00163 FEDDAQHeader defaultDAQHeader_;
00164 FEDDAQTrailer defaultDAQTrailer_;
00165 TrackerSpecialHeader defaultTrackerSpecialHeader_;
00166 std::auto_ptr<FEDFEHeader> defaultFEHeader_;
00167 std::vector<bool> feUnitsEnabled_;
00168 std::vector<bool> channelsEnabled_;
00169 };
00170
00171
00172
00173
00174
00175
00176
00177 inline FEDStripData::FEDStripData(const std::vector<ChannelData>& data)
00178 : data_(data)
00179 { }
00180
00181
00182 inline FEDStripData::ChannelData& FEDStripData::channel(const uint8_t internalFEDChannelNum)
00183 {
00184 return const_cast<ChannelData&>(static_cast<const FEDStripData*>(this)->channel(internalFEDChannelNum));
00185 }
00186
00187 inline FEDStripData::ChannelData& FEDStripData:: operator [] (const uint8_t internalFEDChannelNum)
00188 {
00189 return channel(internalFEDChannelNum);
00190 }
00191
00192 inline const FEDStripData::ChannelData& FEDStripData:: operator [] (const uint8_t internalFEDChannelNum) const
00193 {
00194 return channel(internalFEDChannelNum);
00195 }
00196
00197 inline FEDStripData::ChannelData::ChannelData(bool dataIsAlreadyConvertedTo8Bit, const size_t numberOfSamples,
00198 const std::pair<uint16_t,uint16_t> medians)
00199 : medians_(medians),
00200 data_(numberOfSamples,0),
00201 dataIs8Bit_(dataIsAlreadyConvertedTo8Bit)
00202 { }
00203
00204 inline size_t FEDStripData::ChannelData::size() const
00205 {
00206 return data_.size();
00207 }
00208
00209 inline const uint16_t& FEDStripData::ChannelData::operator [] (const size_t sampleNumber) const
00210 {
00211 return data_[sampleNumber];
00212 }
00213
00214
00215 inline uint16_t& FEDStripData::ChannelData::operator [] (const size_t sampleNumber)
00216 {
00217 return const_cast<uint16_t&>(static_cast<const ChannelData&>(*this)[sampleNumber]);
00218 }
00219
00220 inline uint16_t FEDStripData::ChannelData::getSample(const uint16_t sampleNumber) const
00221 {
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231 return data_[sampleNumber];
00232 }
00233
00234 inline uint8_t FEDStripData::ChannelData::get8BitSample(const uint16_t sampleNumber) const
00235 {
00236 if (dataIs8Bit_) return (0xFF & getSample(sampleNumber));
00237 else {
00238 const uint16_t sample = getSample(sampleNumber);
00239 if (sample < 0xFE) return sample;
00240 else if (sample == 0x3FF) return 0xFF;
00241 else return 0xFE;
00242 }
00243 }
00244
00245 inline std::pair<uint16_t,uint16_t> FEDStripData::ChannelData::getMedians() const
00246 {
00247 return medians_;
00248 }
00249
00250 inline void FEDStripData::ChannelData::setMedians(const std::pair<uint16_t,uint16_t> values)
00251 {
00252 medians_ = values;
00253 }
00254
00255
00256
00257 inline size_t FEDBufferPayload::lengthInBytes() const
00258 {
00259 return data_.size();
00260 }
00261
00262 inline void FEDBufferPayload::appendToBuffer(size_t* pIndexInBuffer, const uint8_t value)
00263 {
00264 data_[((*pIndexInBuffer)++)^7] = value;
00265 }
00266
00267 inline void FEDBufferPayload::appendToBuffer(size_t* pIndexInBuffer, std::vector<uint8_t>::const_iterator start, std::vector<uint8_t>::const_iterator finish)
00268 {
00269 for (std::vector<uint8_t>::const_iterator iVal = start; iVal != finish; iVal++) {
00270 appendToBuffer(pIndexInBuffer,*iVal);
00271 }
00272 }
00273
00274
00275
00276 inline FEDBufferPayloadCreator::FEDBufferPayloadCreator(const std::vector<bool>& feUnitsEnabled, const std::vector<bool>& channelsEnabled)
00277 : feUnitsEnabled_(feUnitsEnabled),
00278 channelsEnabled_(channelsEnabled)
00279 {}
00280
00281 inline FEDBufferPayload FEDBufferPayloadCreator::operator () (const FEDReadoutMode mode, const FEDStripData& data) const
00282 {
00283 return createPayload(mode,data);
00284 }
00285
00286
00287
00288 inline uint32_t FEDBufferGenerator::getL1ID() const
00289 {
00290 return defaultDAQHeader_.l1ID();
00291 }
00292
00293 inline uint16_t FEDBufferGenerator::getBXID() const
00294 {
00295 return defaultDAQHeader_.bxID();
00296 }
00297
00298 inline FEDReadoutMode FEDBufferGenerator::getReadoutMode() const
00299 {
00300 return defaultTrackerSpecialHeader_.readoutMode();
00301 }
00302
00303 inline FEDHeaderType FEDBufferGenerator::getHeaderType() const
00304 {
00305 return defaultTrackerSpecialHeader_.headerType();
00306 }
00307
00308 inline FEDBufferFormat FEDBufferGenerator::getBufferFormat() const
00309 {
00310 return defaultTrackerSpecialHeader_.bufferFormat();
00311 }
00312
00313 inline FEDDAQEventType FEDBufferGenerator::getDAQEventType() const
00314 {
00315 return defaultDAQHeader_.eventType();
00316 }
00317
00318 inline FEDDataType FEDBufferGenerator::getFEDDataType() const
00319 {
00320 return defaultTrackerSpecialHeader_.dataType();
00321 }
00322
00323 inline FEDBufferGenerator& FEDBufferGenerator::setL1ID(const uint32_t newL1ID)
00324 {
00325 defaultDAQHeader_.setL1ID(newL1ID);
00326 return *this;
00327 }
00328
00329 inline FEDBufferGenerator& FEDBufferGenerator::setBXID(const uint16_t newBXID)
00330 {
00331 defaultDAQHeader_.setBXID(newBXID);
00332 return *this;
00333 }
00334
00335 inline FEDBufferGenerator& FEDBufferGenerator::setReadoutMode(const FEDReadoutMode newReadoutMode)
00336 {
00337 defaultTrackerSpecialHeader_.setReadoutMode(newReadoutMode);
00338 return *this;
00339 }
00340
00341 inline FEDBufferGenerator& FEDBufferGenerator::setHeaderType(const FEDHeaderType newHeaderType)
00342 {
00343 defaultTrackerSpecialHeader_.setHeaderType(newHeaderType);
00344 return *this;
00345 }
00346
00347 inline FEDBufferGenerator& FEDBufferGenerator::setBufferFormat(const FEDBufferFormat newBufferFormat)
00348 {
00349 defaultTrackerSpecialHeader_.setBufferFormat(newBufferFormat);
00350 return *this;
00351 }
00352
00353 inline FEDBufferGenerator& FEDBufferGenerator::setDAQEventType(const FEDDAQEventType newDAQEventType)
00354 {
00355 defaultDAQHeader_.setEventType(newDAQEventType);
00356 return *this;
00357 }
00358
00359 inline FEDBufferGenerator& FEDBufferGenerator::setFEDDataType(const FEDDataType newFEDDataType)
00360 {
00361 defaultTrackerSpecialHeader_.setDataType(newFEDDataType);
00362 return *this;
00363 }
00364
00365 inline FEDDAQHeader& FEDBufferGenerator::daqHeader()
00366 {
00367 return defaultDAQHeader_;
00368 }
00369
00370 inline FEDDAQTrailer& FEDBufferGenerator::daqTrailer()
00371 {
00372 return defaultDAQTrailer_;
00373 }
00374
00375 inline TrackerSpecialHeader& FEDBufferGenerator::trackerSpecialHeader()
00376 {
00377 return defaultTrackerSpecialHeader_;
00378 }
00379
00380 inline FEDFEHeader& FEDBufferGenerator::feHeader()
00381 {
00382 return *defaultFEHeader_;
00383 }
00384
00385 inline size_t FEDBufferGenerator::bufferSizeInBytes(const FEDFEHeader& feHeader,
00386 const FEDBufferPayload& payload)
00387 {
00388
00389 return feHeader.lengthInBytes()+payload.lengthInBytes()+8+8+8;
00390 }
00391
00392 }
00393
00394 #endif //ndef EventFilter_SiStripRawToDigi_FEDBufferGenerator_H