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 ( edm::isDebugEnabled() ) {
161  edm::LogWarning("DigiToRaw")
162  << "[sistrip::DigiToRaw::createFedBuffers_]"
163  << " Original header type: " << fedbuffer->headerType();
164  }
165 
167  //fedbuffer->headerType());
168  bufferGenerator_.daqHeader() = fedbuffer->daqHeader();
169  bufferGenerator_.daqTrailer() = fedbuffer->daqTrailer();
170 
171  bufferGenerator_.trackerSpecialHeader() = fedbuffer->trackerSpecialHeader();
173 
174  if ( edm::isDebugEnabled() ) {
175  std::ostringstream debugStream;
176  if(ifed==fed_ids.begin()){ bufferGenerator_.trackerSpecialHeader().print(std::cout); std::cout << std::endl;}
178  edm::LogWarning("DigiToRaw")
179  << "[sistrip::DigiToRaw::createFedBuffers_]"
180  << " Tracker special header apveAddress: " << static_cast<int>(bufferGenerator_.trackerSpecialHeader().apveAddress())
181  << " - event type = " << bufferGenerator_.getDAQEventType()
182  << " - buffer format = " << bufferGenerator_.getBufferFormat() << "\n"
183  << " - SpecialHeader bufferformat " << bufferGenerator_.trackerSpecialHeader().bufferFormat()
184  << " - headertype " << bufferGenerator_.trackerSpecialHeader().headerType()
185  << " - readoutmode " << bufferGenerator_.trackerSpecialHeader().readoutMode()
186  << " - apvaddrregister " << std::hex << static_cast<int>(bufferGenerator_.trackerSpecialHeader().apvAddressErrorRegister())
187  << " - feenabledregister " << std::hex << static_cast<int>(bufferGenerator_.trackerSpecialHeader().feEnableRegister())
188  << " - feoverflowregister " << std::hex << static_cast<int>(bufferGenerator_.trackerSpecialHeader().feOverflowRegister())
189  << " - statusregister " << bufferGenerator_.trackerSpecialHeader().fedStatusRegister() << "\n"
190  << " SpecialHeader: " << debugStream.str();
191  }
192 
193  std::unique_ptr<FEDFEHeader> tempFEHeader(fedbuffer->feHeader()->clone());
194  FEDFullDebugHeader* fedFeHeader = dynamic_cast<FEDFullDebugHeader*>(tempFEHeader.get());
195  if ( edm::isDebugEnabled() ) {
196  std::ostringstream debugStream;
197  if(ifed==fed_ids.begin()){ std::cout << "FEHeader before transfer: " << std::endl;fedFeHeader->print(std::cout);std::cout <<std::endl;}
198  fedFeHeader->print(debugStream);
199  edm::LogWarning("DigiToRaw")
200  << "[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++) {
209  (bufferGenerator_.feHeader()).set32BitReservedRegister(iFE,fedFeHeader->get32BitWordFrom(fedFeHeader->feWord(iFE)+10));
210  }
211 
212  std::vector<bool> feEnabledVec;
213  feEnabledVec.resize(FEUNITS_PER_FED,true);
214  for (uint8_t iFE = 0; iFE < FEUNITS_PER_FED; iFE++) {
215  feEnabledVec[iFE]=fedbuffer->trackerSpecialHeader().feEnabled(iFE);
216  (bufferGenerator_.feHeader()).setFEUnitMajorityAddress(iFE,fedFeHeader->feUnitMajorityAddress(iFE));
217  for (uint8_t iFEUnitChannel = 0; iFEUnitChannel < FEDCH_PER_FEUNIT; iFEUnitChannel++) {
218  (bufferGenerator_.feHeader()).setChannelStatus(iFE,iFEUnitChannel,fedFeHeader->getChannelStatus(iFE,iFEUnitChannel));
219  }//loop on channels
220  }//loop on fe units
221  bufferGenerator_.setFEUnitEnables(feEnabledVec);
222 
223  if ( edm::isDebugEnabled() ) {
224  std::ostringstream debugStream;
225  if(ifed==fed_ids.begin()){ std::cout << "\nFEHeader after transfer: " << std::endl;bufferGenerator_.feHeader().print(std::cout); std::cout << std::endl;}
226  bufferGenerator_.feHeader().print(debugStream);
227  edm::LogWarning("DigiToRaw")
228  << "[sistrip::DigiToRaw::createFedBuffers_]"
229  << " length of feHeader: " << bufferGenerator_.feHeader().lengthInBytes() << "\n"
230  << debugStream.str();
231  }
232  auto conns = cabling->fedConnections(*ifed);
233 
234  FEDStripData fedData(dataIsAlready8BitTruncated);
235 
236 
237  for (auto iconn = conns.begin() ; iconn != conns.end(); iconn++ ) {
238 
239  // Determine FED key from cabling
240  uint32_t fed_key = ( ( iconn->fedId() & sistrip::invalid_ ) << 16 ) | ( iconn->fedCh() & sistrip::invalid_ );
241 
242  // Determine whether DetId or FED key should be used to index digi containers
243  uint32_t key = ( useFedKey_ || mode_ == READOUT_MODE_SCOPE ) ? fed_key : iconn->detId();
244 
245  // Check key is non-zero and valid
246  if ( !key || ( key == sistrip::invalid32_ ) ) { continue; }
247 
248  // Determine APV pair number (needed only when using DetId)
249  uint16_t ipair = ( useFedKey_ || mode_ == READOUT_MODE_SCOPE ) ? 0 : iconn->apvPairNumber();
250 
251  FEDStripData::ChannelData& chanData = fedData[iconn->fedCh()];
252 
253  // Find digis for DetID in collection
254  if (!collection.isValid()){
255  if ( edm::isDebugEnabled() ) {
256  edm::LogWarning("DigiToRaw")
257  << "[DigiToRaw::createFedBuffers] "
258  << "digis collection is not valid...";
259  }
260  break;
261  }
262  typename std::vector< edm::DetSet<Digi_t> >::const_iterator digis = collection->find( key );
263  if (digis == collection->end()) { continue; }
264 
265  typename edm::DetSet<Digi_t>::const_iterator idigi, digis_begin(digis->data.begin());
266  for ( idigi = digis_begin; idigi != digis->data.end(); idigi++ ) {
267 
268  if ( STRIP(idigi, digis_begin) < ipair*256 ||
269  STRIP(idigi, digis_begin) > ipair*256+255 ) { continue; }
270  const unsigned short strip = STRIP(idigi, digis_begin) % 256;
271 
272  if ( strip >= STRIPS_PER_FEDCH ) {
273  if ( edm::isDebugEnabled() ) {
274  std::stringstream ss;
275  ss << "[sistrip::DigiToRaw::createFedBuffers]"
276  << " strip >= strips_per_fedCh";
277  edm::LogWarning("DigiToRaw") << ss.str();
278  }
279  continue;
280  }
281 
282  // check if value already exists
283  if ( edm::isDebugEnabled() ) {
284  const uint16_t value = 0;//chanData[strip];
285  if ( value && value != (*idigi).adc() ) {
286  std::stringstream ss;
287  ss << "[sistrip::DigiToRaw::createFedBuffers]"
288  << " Incompatible ADC values in buffer!"
289  << " FedId/FedCh: " << *ifed << "/" << iconn->fedCh()
290  << " DetStrip: " << STRIP(idigi, digis_begin)
291  << " FedChStrip: " << strip
292  << " AdcValue: " << (*idigi).adc()
293  << " RawData[" << strip << "]: " << value;
294  edm::LogWarning("DigiToRaw") << ss.str();
295  }
296  }
297 
298  // Add digi to buffer
299  chanData[strip] = (*idigi).adc();
300  }
301  }
302  // if ((*idigi).strip() >= (ipair+1)*256) break;
303 
304  if ( edm::isDebugEnabled() ) {
305  edm::LogWarning("DigiToRaw")
306  << "DigiToRaw::createFedBuffers] "
307  << "Almost at the end...";
308  }
309  //create the buffer
310  FEDRawData& fedrawdata = buffers->FEDData( *ifed );
311  bufferGenerator_.generateBuffer(&fedrawdata, fedData, *ifed, packetCode_);
312 
313  if ( edm::isDebugEnabled() ) {
314  std::ostringstream debugStream;
315  bufferGenerator_.feHeader().print(debugStream);
316  edm::LogWarning("DigiToRaw")
317  << "[sistrip::DigiToRaw::createFedBuffers_]"
318  << " length of final feHeader: " << bufferGenerator_.feHeader().lengthInBytes() << "\n"
319  << debugStream.str();
320  }
321  }
322  }//loop on fedids
323  if ( edm::isDebugEnabled() ) {
324  edm::LogWarning("DigiToRaw")
325  << "[sistrip::DigiToRaw::createFedBuffers_]"
326  << "end of first loop on feds";
327  }
328 
329  }//end of workflow for copying header, below is workflow without copying header
330  else{
331  if ( edm::isDebugEnabled() ) {
332  edm::LogWarning("DigiToRaw")
333  << "[sistrip::DigiToRaw::createFedBuffers_]"
334  << "Now getting the digis..."
335  << " Number of feds: " << fed_ids.size() << " between "
336  << *(fed_ids.begin()) << " and " << *(fed_ids.end());
337  }
338 
339  for ( auto ifed = fed_ids.begin(); ifed != fed_ids.end(); ++ifed ) {
340 
341  auto conns = cabling->fedConnections(*ifed);
342 
343  FEDStripData fedData(dataIsAlready8BitTruncated);
344 
345 
346  for (auto iconn = conns.begin() ; iconn != conns.end(); iconn++ ) {
347 
348  // Determine FED key from cabling
349  uint32_t fed_key = ( ( iconn->fedId() & sistrip::invalid_ ) << 16 ) | ( iconn->fedCh() & sistrip::invalid_ );
350 
351  // Determine whether DetId or FED key should be used to index digi containers
352  uint32_t key = ( useFedKey_ || mode_ == READOUT_MODE_SCOPE ) ? fed_key : iconn->detId();
353 
354  // Check key is non-zero and valid
355  if ( !key || ( key == sistrip::invalid32_ ) ) { continue; }
356 
357  // Determine APV pair number (needed only when using DetId)
358  uint16_t ipair = ( useFedKey_ || mode_ == READOUT_MODE_SCOPE ) ? 0 : iconn->apvPairNumber();
359 
360  FEDStripData::ChannelData& chanData = fedData[iconn->fedCh()];
361 
362  // Find digis for DetID in collection
363  if (!collection.isValid()){
364  if ( edm::isDebugEnabled() ) {
365  edm::LogWarning("DigiToRaw")
366  << "[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()) { continue; }
373 
374  typename edm::DetSet<Digi_t>::const_iterator idigi, digis_begin(digis->data.begin());
375  for ( idigi = digis_begin; idigi != digis->data.end(); idigi++ ) {
376 
377  if ( STRIP(idigi, digis_begin) < ipair*256 ||
378  STRIP(idigi, digis_begin) > ipair*256+255 ) { continue; }
379  const unsigned short strip = STRIP(idigi, digis_begin) % 256;
380 
381  if ( strip >= STRIPS_PER_FEDCH ) {
382  if ( edm::isDebugEnabled() ) {
383  std::stringstream ss;
384  ss << "[sistrip::DigiToRaw::createFedBuffers]"
385  << " strip >= strips_per_fedCh";
386  edm::LogWarning("DigiToRaw") << ss.str();
387  }
388  continue;
389  }
390 
391  // check if value already exists
392  if ( edm::isDebugEnabled() ) {
393  const uint16_t value = 0;//chanData[strip];
394  if ( value && value != (*idigi).adc() ) {
395  std::stringstream ss;
396  ss << "[sistrip::DigiToRaw::createFedBuffers]"
397  << " Incompatible ADC values in buffer!"
398  << " FedId/FedCh: " << *ifed << "/" << iconn->fedCh()
399  << " DetStrip: " << STRIP(idigi, digis_begin)
400  << " FedChStrip: " << strip
401  << " AdcValue: " << (*idigi).adc()
402  << " 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")
415  << "DigiToRaw::createFedBuffers] "
416  << "Almost at the end...";
417  }
418  //create the buffer
419  FEDRawData& fedrawdata = buffers->FEDData( *ifed );
420  bufferGenerator_.generateBuffer(&fedrawdata, fedData, *ifed, packetCode_);
421 
422  if ( edm::isDebugEnabled() ) {
423  std::ostringstream debugStream;
424  bufferGenerator_.feHeader().print(debugStream);
425  edm::LogWarning("DigiToRaw")
426  << "[sistrip::DigiToRaw::createFedBuffers_]"
427  << " length of final feHeader: " << bufferGenerator_.feHeader().lengthInBytes() << "\n"
428  << debugStream.str();
429  }
430  }//loop on feds
431  }//end if-else for copying header
432  }//try
433  catch (const std::exception& e) {
434  if ( edm::isDebugEnabled() ) {
435  edm::LogWarning("DigiToRaw")
436  << "DigiToRaw::createFedBuffers] "
437  << "Exception caught : " << e.what();
438  }
439  }
440 
441  }
442 
445 
446 }
447 
#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:60
collection_type data
Definition: DetSet.h:78
#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