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