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  // -----------------------------------------------------------------------------
23  uint8_t packetCode,
24  bool useFedKey ) :
25  mode_(mode),
26  packetCode_(packetCode),
27  useFedKey_(useFedKey),
28  bufferGenerator_(),
29  warnings_("DigiToRaw", "[sistrip::DigiToRaw::createFedBuffers_]", edm::isDebugEnabled())
30  {
31  if ( edm::isDebugEnabled() ) {
32  LogDebug("DigiToRaw")
33  << "[sistrip::DigiToRaw::DigiToRaw]"
34  << " Constructing object...";
35  }
37  }
38 
39  // -----------------------------------------------------------------------------
42  if ( edm::isDebugEnabled() ) {
43  LogDebug("DigiToRaw")
44  << "[sistrip::DigiToRaw::~DigiToRaw]"
45  << " Destructing object...";
46  }
47  }
48 
49  // -----------------------------------------------------------------------------
61  std::unique_ptr<FEDRawDataCollection>& buffers ) {
62  createFedBuffers_(event, cabling, collection, buffers, true);
63  }
64 
68  std::unique_ptr<FEDRawDataCollection>& buffers ) {
69  createFedBuffers_(event, cabling, collection, buffers, false);
70  }
71 
72  //with copy of headers from initial raw data collection (raw->digi->raw)
77  std::unique_ptr<FEDRawDataCollection>& buffers ) {
78  createFedBuffers_(event, cabling, rawbuffers, collection, buffers, true);
79  }
80 
85  std::unique_ptr<FEDRawDataCollection>& buffers ) {
86  createFedBuffers_(event, cabling, rawbuffers, collection, buffers, false);
87  }
88 
89 
90 
91 
92  template<class Digi_t>
96  std::unique_ptr<FEDRawDataCollection>& buffers,
97  bool zeroSuppressed) {
98 
100  //CAMM initialise to some dummy empty buffers??? Or OK like this ?
101  createFedBuffers_(event,cabling,rawbuffers,collection,buffers,zeroSuppressed);
102 
103  }
104 
105  template<class Digi_t>
110  std::unique_ptr<FEDRawDataCollection>& buffers,
111  bool zeroSuppressed) {
112  const bool dataIsAlready8BitTruncated = zeroSuppressed && ( ! (
113  //for special mode premix raw, data is zero-suppressed but not converted to 8 bit
115  // the same goes for 10bit ZS modes
119  ) );
120  try {
121 
122  //set the L1ID to use in the buffers
123  bufferGenerator_.setL1ID(0xFFFFFF & event.id().event());
124  auto fed_ids = cabling->fedIds();
125 
126  //copy header if valid rawbuffers handle
127  if (rawbuffers.isValid()){
128  if ( edm::isDebugEnabled() ) {
129  edm::LogWarning("DigiToRaw")
130  << "[sistrip::DigiToRaw::createFedBuffers_]"
131  << " Valid raw buffers, getting headers from them..."
132  << " Number of feds: " << fed_ids.size() << " between "
133  << *(fed_ids.begin()) << " and " << *(fed_ids.end());
134  }
135 
136  const FEDRawDataCollection& rawDataCollection = *rawbuffers;
137 
138  for ( auto ifed = fed_ids.begin(); ifed != fed_ids.end(); ++ifed ) {
139  const FEDRawData& rawfedData = rawDataCollection.FEDData(*ifed);
140 
141  if ( edm::isDebugEnabled() ) {
142  edm::LogWarning("DigiToRaw")
143  << "[sistrip::DigiToRaw::createFedBuffers_]"
144  << "Fed " << *ifed << " : size of buffer = " << rawfedData.size();
145  }
146 
147  //need to construct full object to copy full header
148  std::unique_ptr<const sistrip::FEDBuffer> fedbuffer;
149  if ( rawfedData.size() == 0 ) {
150  warnings_.add("Invalid raw data for FED, skipping", (boost::format("id %1%") % *ifed).str());
151  } else {
152  try {
153  fedbuffer.reset(new sistrip::FEDBuffer(rawfedData.data(),rawfedData.size(),true));
154  } catch (const cms::Exception& e) {
155  edm::LogWarning("DigiToRaw") << "[sistrip::DigiToRaw::createFedBuffers_]"
156  << " Could not construct FEDBuffer for FED " << *ifed
157  << std::endl;
158  }
159 
160  if ( fedbuffer->headerType() == sistrip::HEADER_TYPE_INVALID ) {
161  warnings_.add("Invalid header type for FED, skipping", (boost::format("id %1%") % *ifed).str());
162  continue;
163  }
164 
165  if ( edm::isDebugEnabled() ) {
166  edm::LogWarning("DigiToRaw")
167  << "[sistrip::DigiToRaw::createFedBuffers_]"
168  << " Original header type: " << fedbuffer->headerType();
169  }
170 
172  //fedbuffer->headerType());
173  bufferGenerator_.daqHeader() = fedbuffer->daqHeader();
174  bufferGenerator_.daqTrailer() = fedbuffer->daqTrailer();
175 
176  bufferGenerator_.trackerSpecialHeader() = fedbuffer->trackerSpecialHeader();
178 
179  if ( edm::isDebugEnabled() ) {
180  std::ostringstream debugStream;
181  if(ifed==fed_ids.begin()){ bufferGenerator_.trackerSpecialHeader().print(std::cout); std::cout << std::endl;}
183  edm::LogWarning("DigiToRaw")
184  << "[sistrip::DigiToRaw::createFedBuffers_]"
185  << " Tracker special header apveAddress: " << static_cast<int>(bufferGenerator_.trackerSpecialHeader().apveAddress())
186  << " - event type = " << bufferGenerator_.getDAQEventType()
187  << " - buffer format = " << bufferGenerator_.getBufferFormat() << "\n"
188  << " - SpecialHeader bufferformat " << bufferGenerator_.trackerSpecialHeader().bufferFormat()
189  << " - headertype " << bufferGenerator_.trackerSpecialHeader().headerType()
190  << " - readoutmode " << bufferGenerator_.trackerSpecialHeader().readoutMode()
191  << " - apvaddrregister " << std::hex << static_cast<int>(bufferGenerator_.trackerSpecialHeader().apvAddressErrorRegister())
192  << " - feenabledregister " << std::hex << static_cast<int>(bufferGenerator_.trackerSpecialHeader().feEnableRegister())
193  << " - feoverflowregister " << std::hex << static_cast<int>(bufferGenerator_.trackerSpecialHeader().feOverflowRegister())
194  << " - statusregister " << bufferGenerator_.trackerSpecialHeader().fedStatusRegister() << "\n"
195  << " SpecialHeader: " << debugStream.str();
196  }
197 
198  std::unique_ptr<FEDFEHeader> tempFEHeader(fedbuffer->feHeader()->clone());
199  FEDFullDebugHeader* fedFeHeader = dynamic_cast<FEDFullDebugHeader*>(tempFEHeader.get());
200  if ( edm::isDebugEnabled() ) {
201  std::ostringstream debugStream;
202  if(ifed==fed_ids.begin()){ std::cout << "FEHeader before transfer: " << std::endl;fedFeHeader->print(std::cout);std::cout <<std::endl;}
203  fedFeHeader->print(debugStream);
204  edm::LogWarning("DigiToRaw")
205  << "[sistrip::DigiToRaw::createFedBuffers_]"
206  << " length of original feHeader: " << fedFeHeader->lengthInBytes() << "\n"
207  << debugStream.str();
208  }
209  //status registers
210  (bufferGenerator_.feHeader()).setBEStatusRegister(fedFeHeader->beStatusRegister());
211  (bufferGenerator_.feHeader()).setDAQRegister2(fedFeHeader->daqRegister2());
212  (bufferGenerator_.feHeader()).setDAQRegister(fedFeHeader->daqRegister());
213  for(uint8_t iFE=1; iFE<6; iFE++) {
214  (bufferGenerator_.feHeader()).set32BitReservedRegister(iFE,fedFeHeader->get32BitWordFrom(fedFeHeader->feWord(iFE)+10));
215  }
216 
217  std::vector<bool> feEnabledVec;
218  feEnabledVec.resize(FEUNITS_PER_FED,true);
219  for (uint8_t iFE = 0; iFE < FEUNITS_PER_FED; iFE++) {
220  feEnabledVec[iFE]=fedbuffer->trackerSpecialHeader().feEnabled(iFE);
221  (bufferGenerator_.feHeader()).setFEUnitMajorityAddress(iFE,fedFeHeader->feUnitMajorityAddress(iFE));
222  for (uint8_t iFEUnitChannel = 0; iFEUnitChannel < FEDCH_PER_FEUNIT; iFEUnitChannel++) {
223  (bufferGenerator_.feHeader()).setChannelStatus(iFE,iFEUnitChannel,fedFeHeader->getChannelStatus(iFE,iFEUnitChannel));
224  }//loop on channels
225  }//loop on fe units
226  bufferGenerator_.setFEUnitEnables(feEnabledVec);
227 
228  if ( edm::isDebugEnabled() ) {
229  std::ostringstream debugStream;
230  if(ifed==fed_ids.begin()){ std::cout << "\nFEHeader after transfer: " << std::endl;bufferGenerator_.feHeader().print(std::cout); std::cout << std::endl;}
231  bufferGenerator_.feHeader().print(debugStream);
232  edm::LogWarning("DigiToRaw")
233  << "[sistrip::DigiToRaw::createFedBuffers_]"
234  << " length of feHeader: " << bufferGenerator_.feHeader().lengthInBytes() << "\n"
235  << debugStream.str();
236  }
237  auto conns = cabling->fedConnections(*ifed);
238 
239  FEDStripData fedData(dataIsAlready8BitTruncated);
240 
241 
242  for (auto iconn = conns.begin() ; iconn != conns.end(); iconn++ ) {
243 
244  // Determine FED key from cabling
245  uint32_t fed_key = ( ( iconn->fedId() & sistrip::invalid_ ) << 16 ) | ( iconn->fedCh() & sistrip::invalid_ );
246 
247  // Determine whether DetId or FED key should be used to index digi containers
248  uint32_t key = ( useFedKey_ || mode_ == READOUT_MODE_SCOPE ) ? fed_key : iconn->detId();
249 
250  // Check key is non-zero and valid
251  if ( !key || ( key == sistrip::invalid32_ ) ) { continue; }
252 
253  // Determine APV pair number (needed only when using DetId)
254  uint16_t ipair = ( useFedKey_ || mode_ == READOUT_MODE_SCOPE ) ? 0 : iconn->apvPairNumber();
255 
256  FEDStripData::ChannelData& chanData = fedData[iconn->fedCh()];
257 
258  // Find digis for DetID in collection
259  if (!collection.isValid()){
260  if ( edm::isDebugEnabled() ) {
261  edm::LogWarning("DigiToRaw")
262  << "[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()) { continue; }
269 
270  typename edm::DetSet<Digi_t>::const_iterator idigi, digis_begin(digis->data.begin());
271  for ( idigi = digis_begin; idigi != digis->data.end(); idigi++ ) {
272 
273  if ( STRIP(idigi, digis_begin) < ipair*256 ||
274  STRIP(idigi, digis_begin) > ipair*256+255 ) { continue; }
275  const unsigned short strip = STRIP(idigi, digis_begin) % 256;
276 
277  if ( strip >= STRIPS_PER_FEDCH ) {
278  if ( edm::isDebugEnabled() ) {
279  std::stringstream ss;
280  ss << "[sistrip::DigiToRaw::createFedBuffers]"
281  << " strip >= strips_per_fedCh";
282  edm::LogWarning("DigiToRaw") << ss.str();
283  }
284  continue;
285  }
286 
287  // check if value already exists
288  if ( edm::isDebugEnabled() ) {
289  const uint16_t value = 0;//chanData[strip];
290  if ( value && value != (*idigi).adc() ) {
291  std::stringstream ss;
292  ss << "[sistrip::DigiToRaw::createFedBuffers]"
293  << " Incompatible ADC values in buffer!"
294  << " FedId/FedCh: " << *ifed << "/" << iconn->fedCh()
295  << " DetStrip: " << STRIP(idigi, digis_begin)
296  << " FedChStrip: " << strip
297  << " AdcValue: " << (*idigi).adc()
298  << " 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")
311  << "DigiToRaw::createFedBuffers] "
312  << "Almost at the end...";
313  }
314  //create the buffer
315  FEDRawData& fedrawdata = buffers->FEDData( *ifed );
316  bufferGenerator_.generateBuffer(&fedrawdata, fedData, *ifed, packetCode_);
317 
318  if ( edm::isDebugEnabled() ) {
319  std::ostringstream debugStream;
320  bufferGenerator_.feHeader().print(debugStream);
321  edm::LogWarning("DigiToRaw")
322  << "[sistrip::DigiToRaw::createFedBuffers_]"
323  << " length of final feHeader: " << bufferGenerator_.feHeader().lengthInBytes() << "\n"
324  << debugStream.str();
325  }
326  }
327  }//loop on fedids
328  if ( edm::isDebugEnabled() ) {
329  edm::LogWarning("DigiToRaw")
330  << "[sistrip::DigiToRaw::createFedBuffers_]"
331  << "end of first loop on feds";
332  }
333 
334  }//end of workflow for copying header, below is workflow without copying header
335  else{
336  if ( edm::isDebugEnabled() ) {
337  edm::LogWarning("DigiToRaw")
338  << "[sistrip::DigiToRaw::createFedBuffers_]"
339  << "Now getting the digis..."
340  << " Number of feds: " << fed_ids.size() << " between "
341  << *(fed_ids.begin()) << " and " << *(fed_ids.end());
342  }
343 
344  for ( auto ifed = fed_ids.begin(); ifed != fed_ids.end(); ++ifed ) {
345 
346  auto conns = cabling->fedConnections(*ifed);
347 
348  FEDStripData fedData(dataIsAlready8BitTruncated);
349 
350 
351  for (auto iconn = conns.begin() ; iconn != conns.end(); iconn++ ) {
352 
353  // Determine FED key from cabling
354  uint32_t fed_key = ( ( iconn->fedId() & sistrip::invalid_ ) << 16 ) | ( iconn->fedCh() & sistrip::invalid_ );
355 
356  // Determine whether DetId or FED key should be used to index digi containers
357  uint32_t key = ( useFedKey_ || mode_ == READOUT_MODE_SCOPE ) ? fed_key : iconn->detId();
358 
359  // Check key is non-zero and valid
360  if ( !key || ( key == sistrip::invalid32_ ) ) { continue; }
361 
362  // Determine APV pair number (needed only when using DetId)
363  uint16_t ipair = ( useFedKey_ || mode_ == READOUT_MODE_SCOPE ) ? 0 : iconn->apvPairNumber();
364 
365  FEDStripData::ChannelData& chanData = fedData[iconn->fedCh()];
366 
367  // Find digis for DetID in collection
368  if (!collection.isValid()){
369  if ( edm::isDebugEnabled() ) {
370  edm::LogWarning("DigiToRaw")
371  << "[DigiToRaw::createFedBuffers] "
372  << "digis collection is not valid...";
373  }
374  break;
375  }
376  typename std::vector< edm::DetSet<Digi_t> >::const_iterator digis = collection->find( key );
377  if (digis == collection->end()) { continue; }
378 
379  typename edm::DetSet<Digi_t>::const_iterator idigi, digis_begin(digis->data.begin());
380  for ( idigi = digis_begin; idigi != digis->data.end(); idigi++ ) {
381 
382  if ( STRIP(idigi, digis_begin) < ipair*256 ||
383  STRIP(idigi, digis_begin) > ipair*256+255 ) { continue; }
384  const unsigned short strip = STRIP(idigi, digis_begin) % 256;
385 
386  if ( strip >= STRIPS_PER_FEDCH ) {
387  if ( edm::isDebugEnabled() ) {
388  std::stringstream ss;
389  ss << "[sistrip::DigiToRaw::createFedBuffers]"
390  << " strip >= strips_per_fedCh";
391  edm::LogWarning("DigiToRaw") << ss.str();
392  }
393  continue;
394  }
395 
396  // check if value already exists
397  if ( edm::isDebugEnabled() ) {
398  const uint16_t value = 0;//chanData[strip];
399  if ( value && value != (*idigi).adc() ) {
400  std::stringstream ss;
401  ss << "[sistrip::DigiToRaw::createFedBuffers]"
402  << " Incompatible ADC values in buffer!"
403  << " FedId/FedCh: " << *ifed << "/" << iconn->fedCh()
404  << " DetStrip: " << STRIP(idigi, digis_begin)
405  << " FedChStrip: " << strip
406  << " AdcValue: " << (*idigi).adc()
407  << " RawData[" << strip << "]: " << value;
408  edm::LogWarning("DigiToRaw") << ss.str();
409  }
410  }
411 
412  // Add digi to buffer
413  chanData[strip] = (*idigi).adc();
414  }
415  }
416  // if ((*idigi).strip() >= (ipair+1)*256) break;
417 
418  if ( edm::isDebugEnabled() ) {
419  edm::LogWarning("DigiToRaw")
420  << "DigiToRaw::createFedBuffers] "
421  << "Almost at the end...";
422  }
423  //create the buffer
424  FEDRawData& fedrawdata = buffers->FEDData( *ifed );
425  bufferGenerator_.generateBuffer(&fedrawdata, fedData, *ifed, packetCode_);
426 
427  if ( edm::isDebugEnabled() ) {
428  std::ostringstream debugStream;
429  bufferGenerator_.feHeader().print(debugStream);
430  edm::LogWarning("DigiToRaw")
431  << "[sistrip::DigiToRaw::createFedBuffers_]"
432  << " length of final feHeader: " << bufferGenerator_.feHeader().lengthInBytes() << "\n"
433  << debugStream.str();
434  }
435  }//loop on feds
436  }//end if-else for copying header
437  }//try
438  catch (const std::exception& e) {
439  if ( edm::isDebugEnabled() ) {
440  edm::LogWarning("DigiToRaw")
441  << "DigiToRaw::createFedBuffers] "
442  << "Exception caught : " << e.what();
443  }
444  }
445 
446  }
447 
450 
451 }
452 
#define LogDebug(id)
EventNumber_t event() const
Definition: EventID.h:41
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:47
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
format
Some error handling for the usage.
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:74
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:80
#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:28
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:33
#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