CMS 3D CMS Logo

SiStripDigiToRaw.cc
Go to the documentation of this file.
1 
2 #include "SiStripDigiToRaw.h"
13 #include <iostream>
14 #include <iomanip>
15 #include <sstream>
16 #include <boost/format.hpp>
17 
18 namespace sistrip {
19 
20  // -----------------------------------------------------------------------------
22  DigiToRaw::DigiToRaw(FEDReadoutMode mode, uint8_t packetCode, bool useFedKey)
23  : mode_(mode),
24  packetCode_(packetCode),
25  useFedKey_(useFedKey),
26  bufferGenerator_(),
27  warnings_("DigiToRaw", "[sistrip::DigiToRaw::createFedBuffers_]", edm::isDebugEnabled()) {
28  if (edm::isDebugEnabled()) {
29  LogDebug("DigiToRaw") << "[sistrip::DigiToRaw::DigiToRaw]"
30  << " Constructing object...";
31  }
33  }
34 
35  // -----------------------------------------------------------------------------
38  if (edm::isDebugEnabled()) {
39  LogDebug("DigiToRaw") << "[sistrip::DigiToRaw::~DigiToRaw]"
40  << " Destructing object...";
41  }
42  }
43 
44  // -----------------------------------------------------------------------------
56  std::unique_ptr<FEDRawDataCollection>& buffers) {
57  createFedBuffers_(event, cabling, collection, buffers, true);
58  }
59 
63  std::unique_ptr<FEDRawDataCollection>& buffers) {
64  createFedBuffers_(event, cabling, collection, buffers, false);
65  }
66 
67  //with copy of headers from initial raw data collection (raw->digi->raw)
72  std::unique_ptr<FEDRawDataCollection>& buffers) {
73  createFedBuffers_(event, cabling, rawbuffers, collection, buffers, true);
74  }
75 
80  std::unique_ptr<FEDRawDataCollection>& buffers) {
81  createFedBuffers_(event, cabling, rawbuffers, collection, buffers, false);
82  }
83 
84  template <class Digi_t>
88  std::unique_ptr<FEDRawDataCollection>& buffers,
89  bool zeroSuppressed) {
91  //CAMM initialise to some dummy empty buffers??? Or OK like this ?
92  createFedBuffers_(event, cabling, rawbuffers, collection, buffers, zeroSuppressed);
93  }
94 
95  template <class Digi_t>
100  std::unique_ptr<FEDRawDataCollection>& buffers,
101  bool zeroSuppressed) {
102  const bool dataIsAlready8BitTruncated =
103  zeroSuppressed &&
104  (!(
105  //for special mode premix raw, data is zero-suppressed but not converted to 8 bit
107  // the same goes for 10bit ZS modes
111  try {
112  //set the L1ID to use in the buffers
113  bufferGenerator_.setL1ID(0xFFFFFF & event.id().event());
114  auto fed_ids = cabling->fedIds();
115 
116  //copy header if valid rawbuffers handle
117  if (rawbuffers.isValid()) {
118  if (edm::isDebugEnabled()) {
119  edm::LogWarning("DigiToRaw") << "[sistrip::DigiToRaw::createFedBuffers_]"
120  << " Valid raw buffers, getting headers from them..."
121  << " Number of feds: " << fed_ids.size() << " between " << *(fed_ids.begin())
122  << " and " << *(fed_ids.end());
123  }
124 
125  const FEDRawDataCollection& rawDataCollection = *rawbuffers;
126 
127  for (auto ifed = fed_ids.begin(); ifed != fed_ids.end(); ++ifed) {
128  const FEDRawData& rawfedData = rawDataCollection.FEDData(*ifed);
129 
130  if (edm::isDebugEnabled()) {
131  edm::LogWarning("DigiToRaw") << "[sistrip::DigiToRaw::createFedBuffers_]"
132  << "Fed " << *ifed << " : size of buffer = " << rawfedData.size();
133  }
134 
135  //need to construct full object to copy full header
136  std::unique_ptr<const sistrip::FEDBuffer> fedbuffer;
137  if (rawfedData.size() == 0) {
138  warnings_.add("Invalid raw data for FED, skipping", (boost::format("id %1%") % *ifed).str());
139  } else {
140  try {
141  fedbuffer.reset(new sistrip::FEDBuffer(rawfedData.data(), rawfedData.size(), true));
142  } catch (const cms::Exception& e) {
143  edm::LogWarning("DigiToRaw") << "[sistrip::DigiToRaw::createFedBuffers_]"
144  << " Could not construct FEDBuffer for FED " << *ifed << std::endl;
145  }
146 
147  if (fedbuffer->headerType() == sistrip::HEADER_TYPE_INVALID) {
148  warnings_.add("Invalid header type for FED, skipping", (boost::format("id %1%") % *ifed).str());
149  continue;
150  }
151 
152  if (edm::isDebugEnabled()) {
153  edm::LogWarning("DigiToRaw") << "[sistrip::DigiToRaw::createFedBuffers_]"
154  << " Original header type: " << fedbuffer->headerType();
155  }
156 
158  //fedbuffer->headerType());
159  bufferGenerator_.daqHeader() = fedbuffer->daqHeader();
160  bufferGenerator_.daqTrailer() = fedbuffer->daqTrailer();
161 
162  bufferGenerator_.trackerSpecialHeader() = fedbuffer->trackerSpecialHeader();
164 
165  if (edm::isDebugEnabled()) {
166  std::ostringstream debugStream;
167  if (ifed == fed_ids.begin()) {
169  std::cout << std::endl;
170  }
172  edm::LogWarning("DigiToRaw")
173  << "[sistrip::DigiToRaw::createFedBuffers_]"
174  << " Tracker special header apveAddress: "
175  << static_cast<int>(bufferGenerator_.trackerSpecialHeader().apveAddress())
176  << " - event type = " << bufferGenerator_.getDAQEventType()
177  << " - buffer format = " << bufferGenerator_.getBufferFormat() << "\n"
178  << " - SpecialHeader bufferformat " << bufferGenerator_.trackerSpecialHeader().bufferFormat()
179  << " - headertype " << bufferGenerator_.trackerSpecialHeader().headerType() << " - readoutmode "
180  << bufferGenerator_.trackerSpecialHeader().readoutMode() << " - apvaddrregister " << std::hex
182  << " - feenabledregister " << std::hex
184  << " - feoverflowregister " << std::hex
186  << " - statusregister " << bufferGenerator_.trackerSpecialHeader().fedStatusRegister() << "\n"
187  << " SpecialHeader: " << debugStream.str();
188  }
189 
190  std::unique_ptr<FEDFEHeader> tempFEHeader(fedbuffer->feHeader()->clone());
191  FEDFullDebugHeader* fedFeHeader = dynamic_cast<FEDFullDebugHeader*>(tempFEHeader.get());
192  if (edm::isDebugEnabled()) {
193  std::ostringstream debugStream;
194  if (ifed == fed_ids.begin()) {
195  std::cout << "FEHeader before transfer: " << std::endl;
196  fedFeHeader->print(std::cout);
197  std::cout << std::endl;
198  }
199  fedFeHeader->print(debugStream);
200  edm::LogWarning("DigiToRaw") << "[sistrip::DigiToRaw::createFedBuffers_]"
201  << " length of original feHeader: " << fedFeHeader->lengthInBytes() << "\n"
202  << debugStream.str();
203  }
204  //status registers
205  (bufferGenerator_.feHeader()).setBEStatusRegister(fedFeHeader->beStatusRegister());
206  (bufferGenerator_.feHeader()).setDAQRegister2(fedFeHeader->daqRegister2());
207  (bufferGenerator_.feHeader()).setDAQRegister(fedFeHeader->daqRegister());
208  for (uint8_t iFE = 1; iFE < 6; iFE++) {
210  .set32BitReservedRegister(iFE, fedFeHeader->get32BitWordFrom(fedFeHeader->feWord(iFE) + 10));
211  }
212 
213  std::vector<bool> feEnabledVec;
214  feEnabledVec.resize(FEUNITS_PER_FED, true);
215  for (uint8_t iFE = 0; iFE < FEUNITS_PER_FED; iFE++) {
216  feEnabledVec[iFE] = fedbuffer->trackerSpecialHeader().feEnabled(iFE);
217  (bufferGenerator_.feHeader()).setFEUnitMajorityAddress(iFE, fedFeHeader->feUnitMajorityAddress(iFE));
218  for (uint8_t iFEUnitChannel = 0; iFEUnitChannel < FEDCH_PER_FEUNIT; iFEUnitChannel++) {
220  .setChannelStatus(iFE, iFEUnitChannel, fedFeHeader->getChannelStatus(iFE, iFEUnitChannel));
221  } //loop on channels
222  } //loop on fe units
223  bufferGenerator_.setFEUnitEnables(feEnabledVec);
224 
225  if (edm::isDebugEnabled()) {
226  std::ostringstream debugStream;
227  if (ifed == fed_ids.begin()) {
228  std::cout << "\nFEHeader after transfer: " << std::endl;
230  std::cout << std::endl;
231  }
232  bufferGenerator_.feHeader().print(debugStream);
233  edm::LogWarning("DigiToRaw")
234  << "[sistrip::DigiToRaw::createFedBuffers_]"
235  << " length of feHeader: " << bufferGenerator_.feHeader().lengthInBytes() << "\n"
236  << debugStream.str();
237  }
238  auto conns = cabling->fedConnections(*ifed);
239 
240  FEDStripData fedData(dataIsAlready8BitTruncated);
241 
242  for (auto iconn = conns.begin(); iconn != conns.end(); iconn++) {
243  // Determine FED key from cabling
244  uint32_t fed_key = ((iconn->fedId() & sistrip::invalid_) << 16) | (iconn->fedCh() & sistrip::invalid_);
245 
246  // Determine whether DetId or FED key should be used to index digi containers
247  uint32_t key = (useFedKey_ || mode_ == READOUT_MODE_SCOPE) ? fed_key : iconn->detId();
248 
249  // Check key is non-zero and valid
250  if (!key || (key == sistrip::invalid32_)) {
251  continue;
252  }
253 
254  // Determine APV pair number (needed only when using DetId)
255  uint16_t ipair = (useFedKey_ || mode_ == READOUT_MODE_SCOPE) ? 0 : iconn->apvPairNumber();
256 
257  FEDStripData::ChannelData& chanData = fedData[iconn->fedCh()];
258 
259  // Find digis for DetID in collection
260  if (!collection.isValid()) {
261  if (edm::isDebugEnabled()) {
262  edm::LogWarning("DigiToRaw") << "[DigiToRaw::createFedBuffers] "
263  << "digis collection is not valid...";
264  }
265  break;
266  }
267  typename std::vector<edm::DetSet<Digi_t> >::const_iterator digis = collection->find(key);
268  if (digis == collection->end()) {
269  continue;
270  }
271 
272  typename edm::DetSet<Digi_t>::const_iterator idigi, digis_begin(digis->data.begin());
273  for (idigi = digis_begin; idigi != digis->data.end(); idigi++) {
274  if (STRIP(idigi, digis_begin) < ipair * 256 || STRIP(idigi, digis_begin) > ipair * 256 + 255) {
275  continue;
276  }
277  const unsigned short strip = STRIP(idigi, digis_begin) % 256;
278 
279  if (strip >= STRIPS_PER_FEDCH) {
280  if (edm::isDebugEnabled()) {
281  std::stringstream ss;
282  ss << "[sistrip::DigiToRaw::createFedBuffers]"
283  << " strip >= strips_per_fedCh";
284  edm::LogWarning("DigiToRaw") << ss.str();
285  }
286  continue;
287  }
288 
289  // check if value already exists
290  if (edm::isDebugEnabled()) {
291  const uint16_t value = 0; //chanData[strip];
292  if (value && value != (*idigi).adc()) {
293  std::stringstream ss;
294  ss << "[sistrip::DigiToRaw::createFedBuffers]"
295  << " Incompatible ADC values in buffer!"
296  << " FedId/FedCh: " << *ifed << "/" << iconn->fedCh()
297  << " DetStrip: " << STRIP(idigi, digis_begin) << " FedChStrip: " << strip
298  << " AdcValue: " << (*idigi).adc() << " RawData[" << strip << "]: " << value;
299  edm::LogWarning("DigiToRaw") << ss.str();
300  }
301  }
302 
303  // Add digi to buffer
304  chanData[strip] = (*idigi).adc();
305  }
306  }
307  // if ((*idigi).strip() >= (ipair+1)*256) break;
308 
309  if (edm::isDebugEnabled()) {
310  edm::LogWarning("DigiToRaw") << "DigiToRaw::createFedBuffers] "
311  << "Almost at the end...";
312  }
313  //create the buffer
314  FEDRawData& fedrawdata = buffers->FEDData(*ifed);
315  bufferGenerator_.generateBuffer(&fedrawdata, fedData, *ifed, packetCode_);
316 
317  if (edm::isDebugEnabled()) {
318  std::ostringstream debugStream;
319  bufferGenerator_.feHeader().print(debugStream);
320  edm::LogWarning("DigiToRaw")
321  << "[sistrip::DigiToRaw::createFedBuffers_]"
322  << " length of final feHeader: " << bufferGenerator_.feHeader().lengthInBytes() << "\n"
323  << debugStream.str();
324  }
325  }
326  } //loop on fedids
327  if (edm::isDebugEnabled()) {
328  edm::LogWarning("DigiToRaw") << "[sistrip::DigiToRaw::createFedBuffers_]"
329  << "end of first loop on feds";
330  }
331 
332  } //end of workflow for copying header, below is workflow without copying header
333  else {
334  if (edm::isDebugEnabled()) {
335  edm::LogWarning("DigiToRaw") << "[sistrip::DigiToRaw::createFedBuffers_]"
336  << "Now getting the digis..."
337  << " Number of feds: " << fed_ids.size() << " between " << *(fed_ids.begin())
338  << " and " << *(fed_ids.end());
339  }
340 
341  for (auto ifed = fed_ids.begin(); ifed != fed_ids.end(); ++ifed) {
342  auto conns = cabling->fedConnections(*ifed);
343 
344  FEDStripData fedData(dataIsAlready8BitTruncated);
345 
346  for (auto iconn = conns.begin(); iconn != conns.end(); iconn++) {
347  // Determine FED key from cabling
348  uint32_t fed_key = ((iconn->fedId() & sistrip::invalid_) << 16) | (iconn->fedCh() & sistrip::invalid_);
349 
350  // Determine whether DetId or FED key should be used to index digi containers
351  uint32_t key = (useFedKey_ || mode_ == READOUT_MODE_SCOPE) ? fed_key : iconn->detId();
352 
353  // Check key is non-zero and valid
354  if (!key || (key == sistrip::invalid32_)) {
355  continue;
356  }
357 
358  // Determine APV pair number (needed only when using DetId)
359  uint16_t ipair = (useFedKey_ || mode_ == READOUT_MODE_SCOPE) ? 0 : iconn->apvPairNumber();
360 
361  FEDStripData::ChannelData& chanData = fedData[iconn->fedCh()];
362 
363  // Find digis for DetID in collection
364  if (!collection.isValid()) {
365  if (edm::isDebugEnabled()) {
366  edm::LogWarning("DigiToRaw") << "[DigiToRaw::createFedBuffers] "
367  << "digis collection is not valid...";
368  }
369  break;
370  }
371  typename std::vector<edm::DetSet<Digi_t> >::const_iterator digis = collection->find(key);
372  if (digis == collection->end()) {
373  continue;
374  }
375 
376  typename edm::DetSet<Digi_t>::const_iterator idigi, digis_begin(digis->data.begin());
377  for (idigi = digis_begin; idigi != digis->data.end(); idigi++) {
378  if (STRIP(idigi, digis_begin) < ipair * 256 || STRIP(idigi, digis_begin) > ipair * 256 + 255) {
379  continue;
380  }
381  const unsigned short strip = STRIP(idigi, digis_begin) % 256;
382 
383  if (strip >= STRIPS_PER_FEDCH) {
384  if (edm::isDebugEnabled()) {
385  std::stringstream ss;
386  ss << "[sistrip::DigiToRaw::createFedBuffers]"
387  << " strip >= strips_per_fedCh";
388  edm::LogWarning("DigiToRaw") << ss.str();
389  }
390  continue;
391  }
392 
393  // check if value already exists
394  if (edm::isDebugEnabled()) {
395  const uint16_t value = 0; //chanData[strip];
396  if (value && value != (*idigi).adc()) {
397  std::stringstream ss;
398  ss << "[sistrip::DigiToRaw::createFedBuffers]"
399  << " Incompatible ADC values in buffer!"
400  << " FedId/FedCh: " << *ifed << "/" << iconn->fedCh()
401  << " DetStrip: " << STRIP(idigi, digis_begin) << " FedChStrip: " << strip
402  << " AdcValue: " << (*idigi).adc() << " RawData[" << strip << "]: " << value;
403  edm::LogWarning("DigiToRaw") << ss.str();
404  }
405  }
406 
407  // Add digi to buffer
408  chanData[strip] = (*idigi).adc();
409  }
410  }
411  // if ((*idigi).strip() >= (ipair+1)*256) break;
412 
413  if (edm::isDebugEnabled()) {
414  edm::LogWarning("DigiToRaw") << "DigiToRaw::createFedBuffers] "
415  << "Almost at the end...";
416  }
417  //create the buffer
418  FEDRawData& fedrawdata = buffers->FEDData(*ifed);
419  bufferGenerator_.generateBuffer(&fedrawdata, fedData, *ifed, packetCode_);
420 
421  if (edm::isDebugEnabled()) {
422  std::ostringstream debugStream;
423  bufferGenerator_.feHeader().print(debugStream);
424  edm::LogWarning("DigiToRaw") << "[sistrip::DigiToRaw::createFedBuffers_]"
425  << " length of final feHeader: " << bufferGenerator_.feHeader().lengthInBytes()
426  << "\n"
427  << debugStream.str();
428  }
429  } //loop on feds
430  } //end if-else for copying header
431  } //try
432  catch (const std::exception& e) {
433  if (edm::isDebugEnabled()) {
434  edm::LogWarning("DigiToRaw") << "DigiToRaw::createFedBuffers] "
435  << "Exception caught : " << e.what();
436  }
437  }
438  }
439 
442  return it->strip();
443  }
446  return it - begin;
447  }
448 
449 } // namespace sistrip
#define LogDebug(id)
EventNumber_t event() const
Definition: EventID.h:40
bool isDebugEnabled()
DigiToRaw(FEDReadoutMode mode, uint8_t packetCode, bool use_fed_key)
FEDBufferGenerator & setReadoutMode(const FEDReadoutMode newReadoutMode)
static const uint32_t invalid32_
Definition: Constants.h:15
FEDStatusRegister fedStatusRegister() const
virtual void print(std::ostream &os) const =0
WarningSummary warnings_
TrackerSpecialHeader & trackerSpecialHeader()
FEDBufferFormat getBufferFormat() const
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:45
sistrip classes
static const uint8_t PACKET_CODE_ZERO_SUPPRESSED10
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
FEDBufferGenerator & setFEUnitEnables(const std::vector< bool > &feUnitsEnabled)
static const uint16_t FEUNITS_PER_FED
FedsConstIterRange fedIds() const
void generateBuffer(FEDRawData *rawDataObject, const FEDStripData &data, uint16_t sourceID, uint8_t packetCode) const
Definition: value.py:1
void createFedBuffers(edm::Event &, edm::ESHandle< SiStripFedCabling > &cabling, edm::Handle< edm::DetSetVector< SiStripDigi > > &digis, std::unique_ptr< FEDRawDataCollection > &buffers)
bool isValid() const
Definition: HandleBase.h:70
void add(const std::string &message, const std::string &details="")
FEDReadoutMode mode_
void print(std::ostream &os) const
static const uint16_t STRIPS_PER_FEDCH
FEDBufferGenerator & setHeaderType(const FEDHeaderType newHeaderType)
FEDBufferGenerator & setL1ID(const uint32_t newL1ID)
static const uint16_t FEDCH_PER_FEUNIT
static const uint16_t invalid_
Definition: Constants.h:16
ConnsConstIterRange fedConnections(uint16_t fed_id) const
virtual size_t lengthInBytes() const =0
edm::EventID id() const
Definition: EventBase.h:59
collection_type data
Definition: DetSet.h:81
#define begin
Definition: vmac.h:32
HLT enums.
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:24
uint16_t STRIP(const edm::DetSet< SiStripDigi >::const_iterator &it, const edm::DetSet< SiStripDigi >::const_iterator &begin) const
FEDDAQEventType getDAQEventType() const
collection_type::const_iterator const_iterator
Definition: DetSet.h:32
#define str(s)
FEDBufferGenerator bufferGenerator_
void createFedBuffers_(edm::Event &, edm::ESHandle< SiStripFedCabling > &cabling, edm::Handle< edm::DetSetVector< Digi_t > > &digis, std::unique_ptr< FEDRawDataCollection > &buffers, bool zeroSuppressed)
Definition: event.py:1