CMS 3D CMS Logo

DTuROSRawToDigi.cc
Go to the documentation of this file.
1 //-------------------------------------------------
2 //
3 // Class: DTuROSRawToDigi
4 //
5 // L1 DT uROS Raw-to-Digi
6 //
7 //
8 //
9 // Author :
10 // C. Heidemann - RWTH Aachen
11 // J. Troconiz - UAM
12 //
13 //
14 //--------------------------------------------------
15 
17 
21 
26 
27 #include <iostream>
28 
29 
31 
32  produces<DTDigiCollection>();
33  produces<std::vector<DTuROSFEDData>>();
34 
35  DTuROSInputTag_ = pset.getParameter<edm::InputTag>("inputLabel");
36 
37  debug_ = pset.getUntrackedParameter<bool>("debug", false);
38 
40  feds_.push_back(i);
41 
42  nfeds_ = feds_.size();
43 
44  Raw_token = consumes<FEDRawDataCollection>(DTuROSInputTag_);
45 
46 }
47 
48 
50 
51 
53 
54  DTDigiCollection digis;
55  std::vector<DTuROSFEDData> words;
56 
57  if (!fillRawData(e, c, digis, words)) return;
58 
59  auto uROSDTDigi_product = std::make_unique<DTDigiCollection>(digis);
60  auto uROSDTWord_product = std::make_unique<std::vector<DTuROSFEDData>>(words);
61 
62  e.put(std::move(uROSDTDigi_product));
63  e.put(std::move(uROSDTWord_product));
64 
65 }
66 
67 
69  DTDigiCollection& digis, std::vector<DTuROSFEDData>& words) {
70 
72  e.getByToken(Raw_token, data);
73 
75  c.get<DTReadOutMappingRcd>().get( mapping );
76 
77  for (int w_i = 0; w_i < nfeds_; ++w_i) {
78  DTuROSFEDData fwords;
79  process(feds_[w_i], data, mapping, digis, fwords);
80  words.push_back(fwords);
81  }
82 
83  return true;
84 }
85 
86 
87 void DTuROSRawToDigi::process(int DTuROSFED,
90  DTDigiCollection& digis,
91  DTuROSFEDData& fwords) {
92 
93 
94  // Container
95  std::vector<long> DTuROSWordContainer;
96 
97 
98  // Header constituents
99  int BOEevTy, DTuROSId;
100 
101 
102  // Trailer constituents
103  int chkEOE, evtLgth, CRC;
104 
105 
106  // Hit counter
107  std::map<uint32_t, int> hitOrder;
108 
109 
110  //--> Header - line 1
111 
112 
113  FEDRawData dturosdata = data->FEDData(DTuROSFED);
114  if ( dturosdata.size() == 0 ) return;
115 
116 
117  lineFED=dturosdata.data();
118  long dataWord = 0;
119  int lines = 0; // counting including header
120  readline(lines, dataWord);
121  const int posBOEevTy = 60 ; // positions 60 -> 63
122  const int posDTuROSId = 8 ; // positions 8 -> 19
123  const int posNslost = 52 ; // positions 52 -> 55
124  const int posSlotFED = 16 ; // positions 16 -> 19
125 
126  BOEevTy = ( dataWord >> posBOEevTy ) & 0xF;
127  DTuROSId = ( dataWord >> posDTuROSId ) & 0xFFF;
128 
129 
130  if ( (BOEevTy != 0x5) || (DTuROSId != DTuROSFED) ) {
131  if ( debug_ ) edm::LogWarning("dturos_unpacker") << "Not a DTuROS FED " << DTuROSFED << " or header " << std::hex << dataWord;
132  return;
133  }
134 
135 
136  fwords.setfed(DTuROSId);
137  fwords.setheader1(dataWord);
138 
139 
140  int newCRC = 0xFFFF;
141  dt_crc::calcCRC(dataWord, newCRC);
142 
143  int crate = DTuROSId;
144 
145 
146  //--> Header - line 2
147 
148 
149  readline(lines, dataWord);
150  dt_crc::calcCRC(dataWord, newCRC);
151 
152  int nslots = ( dataWord >> posNslost ) & 0xF;
153 
154 
155  fwords.setheader2(dataWord);
156  fwords.setnslots(nslots);
157 
158 
159  //--> AMC - line 3 to 2+nslots
160  std::map<int, int> slot_size;
161  for (int j = 0; j < nslots; ++j) {
162 
163  readline(lines, dataWord);
164  dt_crc::calcCRC(dataWord, newCRC);
165 
166  int slot = ( dataWord >> posSlotFED ) & 0xF;
167 
168  if ( (slot < 1) || (slot > 12) ) {
169  if ( debug_ ) edm::LogWarning("dturos_unpacker") << "AMCnumber " << std::dec << slot << " out of range (1-12)";
170  return;
171  }
172 
173 
174  slot_size[slot] = ( dataWord >> 32 ) & 0xFFFFFF; // positions 32 -> 55
175 
176 
177  fwords.setslotsize(slot, slot_size[slot]);
178 
179  }
180 
181  //--> DTuROS data
182 
183 
184  std::map<int,int>::iterator sziterator = slot_size.begin();
185  std::map<int,int>::iterator szitend = slot_size.end();
186  for (; sziterator != szitend; ++sziterator) {
187  for (int k=0; k<sziterator->second; ++k) {
188 
189  readline(lines, dataWord);
190  dt_crc::calcCRC(dataWord, newCRC);
191 
192  DTuROSWordContainer.push_back(dataWord);
193 
194  }
195  }
196 
197 
198  //--> Trailer - line 1
199 
200 
201  readline(lines, dataWord);
202  dt_crc::calcCRC(dataWord, newCRC);
203 
204  //--> Trailer - line 2
205 
206 
207  readline(lines, dataWord);
208 
209  const int posEOE = 60; // positions 60 -> 63
210  const int posEvtLenght = 32; // positions 33 ->55
211  const int posCRC = 16; // positions 16 ->31
212  chkEOE = ( dataWord >> posEOE ) & 0xF;
213 
214 
215  if ( chkEOE != 0xA ) {
216  if ( debug_ ) edm::LogWarning("dturos_unpacker") << "Trailer " << std::hex << dataWord << " does not start with 0xA";
217  return;
218  }
219 
220 
221  evtLgth = ( dataWord >> posEvtLenght ) & 0xFFFFFF;
222  CRC = ( dataWord >> posCRC ) & 0xFFFF;
223 
224 
225  dt_crc::calcCRC(dataWord & 0xFFFFFFFF0000FFFF, newCRC);
226 
227  if ( newCRC != CRC ) {
228  if ( debug_ ) edm::LogWarning("dturos_unpacker")
229  << "Calculated CRC " << std::hex << newCRC << " differs from CRC in trailer " << std::hex << CRC;
230  return;
231  }
232 
233 
234  if ( lines != evtLgth ) {
235  if ( debug_ ) edm::LogWarning("dturos_unpacker")
236  << "Number of words read != event length " << std::dec << lines << " " << evtLgth;
237  return;
238  }
239 
240 
241  fwords.settrailer(dataWord);
242  fwords.setevtlgth(evtLgth);
243 
244 
245  //--> analyze event
246 
247 
248  std::vector<long>::iterator DTuROSiterator = DTuROSWordContainer.begin();
249  std::vector<long>::iterator DTuROSitend = DTuROSWordContainer.end();
250 
251  const int posSlot = 56; // positions 56 -> 59
252 
253  for (; DTuROSiterator != DTuROSitend; ++DTuROSiterator) {
254 
255  DTuROSROSData rwords;
256 
257 
258  dataWord = (*DTuROSiterator); // Header AMC 1
259 
260 
261  int slot = ( dataWord >> posSlot ) & 0xF;
262 
263 
264  if ( (slot < 1) || (slot > 12) ) {
265  if ( debug_ ) edm::LogWarning("dturos_unpacker") << "Slot " << std::dec << slot << " out of range (1-12) in crate " << crate;
266  break;
267  }
268 
269 
270  rwords.setslot(slot);
271  rwords.setheader1(dataWord);
272 
273 
274  ++DTuROSiterator;
275  dataWord = (*DTuROSiterator); // Header AMC 2
276 
277 
278  rwords.setheader2(dataWord);
279  int slotMap = dataWord & 0xF;
280  if (slotMap == 0) slotMap=slot;
281  const int posSel1 = 60 ; // positions 60 -> 6
282  const int posSel2 = 28 ; // position 28
283  const int posTDCTime = 32 ; // positions 32 -> 45
284  const int posTDCChannel = 46 ; // positions 46 -> 50
285  const int posTDCId = 51 ; // positions 51 -> 52
286  const int posLink = 53 ; // positions 53 -> 59
287  const int posTDCChannelSel2Null = 14 ; // positions 14 -> 18
288  const int posTDCIdSel2Null = 19 ; // positions 19 -> 20
289  const int posLinkSel2Null = 21 ; // positions 21 -> 27
290  const int posErrorSel3 = 32 ; // positions 32 -> 60
291 
292  for (int k=2; k<slot_size[slot]-1; ++k) {
293 
294 
295  ++DTuROSiterator;
296  dataWord = (*DTuROSiterator);
297  int selector = ( dataWord >> posSel1 ) & 0xF;
298  int selector2 = ( dataWord >> posSel2 ) & 0x1;
299 
300  if ( selector == 4 ) { // OK word
301 
302  if ( rwords.getokword1() ){ rwords.setokword2(dataWord); }
303  else { rwords.setokword1(dataWord); }
304 
305  }
306  else if ( selector >= 8 && selector <= 13 ) { // OK xword
307 
308  rwords.setokxword(selector-8, dataWord);
309 
310  }
311  else if ( selector == 15 ) { // extra word
312 
313  rwords.setexword(dataWord);
314 
315  }
316  else {
317 
318  if ( selector == 2 ) { // TDC word
319 
320  int tdcTime = ( dataWord >> posTDCTime ) & 0x3FFF;
321  int tdcChannel = ( dataWord >> posTDCChannel ) & 0x1F;
322  int tdcId = ( dataWord >> posTDCId ) & 0x3;
323  int link = ( dataWord >> posLink ) & 0x7F;
324 
325  int dummy = 0;
326 
327  bool tenDDU = ! mapping->readOutToGeometry(779, 7, 1, 1, 1,
328  dummy, dummy, dummy,
329  dummy, dummy, dummy);
330 
331  int dduId = theDDU(crate, slotMap, link, tenDDU);
332  int rosId = theROS(slotMap, link);
333  int robId = theROB(slotMap, link);
334 
335 
336  DTROChainCoding channelIndex(dduId, rosId, robId, tdcId, tdcChannel);
337  if (hitOrder.find(channelIndex.getCode()) == hitOrder.end()) hitOrder[channelIndex.getCode()] = 0;
338  else hitOrder[channelIndex.getCode()]++;
339 
340 
341  int wheelId, stationId, sectorId, slId,layerId, cellId;
342  if ( ! mapping->readOutToGeometry(dduId, rosId, robId, tdcId, tdcChannel,
343  wheelId, stationId, sectorId, slId, layerId, cellId)) {
344 
345  DTWireId detId = DTWireId(wheelId, stationId, sectorId, slId, layerId, cellId);
346  int wire = detId.wire();
347 
348  DTDigi digi(wire, tdcTime, hitOrder[channelIndex.getCode()]);
349  digis.insertDigi(detId.layerId(),digi);
350 
351  }
352 
353  }
354  else if ( selector == 3 ) { // error word
355 
356  if ( debug_ ) edm::LogWarning("dturos_unpacker") << "Error word [" << std::dec << k << "] : " << std::hex << dataWord
357  << std::dec << " in slot " << slot << " in crate " << crate;
358 
359  int error = ( dataWord >> posErrorSel3 ) & 0x1FFFFFFF;
360  rwords.seterror(error);
361 
362  }
363 
364 
365  if ((dataWord & 0x1FFFFFFF) == 0x1FFFFFFF) continue;
366 
367  if ( selector2 == 0 ) { // TDC word
368 
369  int tdcTime = ( dataWord ) & 0x3FFF; // positions 0 -> 13
370  int tdcChannel = ( dataWord >> posTDCChannelSel2Null ) & 0x1F;
371  int tdcId = ( dataWord >> posTDCIdSel2Null ) & 0x3;
372  int link = ( dataWord >> posLinkSel2Null ) & 0x7F; // positions 21 -> 27
373 
374 
375  if (tdcTime == 16383) continue;
376 
377  int dummy = 0;
378 
379  bool tenDDU = ! mapping->readOutToGeometry(779, 7, 1, 1, 1,
380  dummy, dummy, dummy,
381  dummy, dummy, dummy);
382 
383  int dduId = theDDU(crate, slotMap, link, tenDDU);
384  int rosId = theROS(slotMap, link);
385  int robId = theROB(slotMap, link);
386 
387 
388  DTROChainCoding channelIndex(dduId, rosId, robId, tdcId, tdcChannel);
389  if (hitOrder.find(channelIndex.getCode()) == hitOrder.end()) hitOrder[channelIndex.getCode()] = 0;
390  else hitOrder[channelIndex.getCode()]++;
391 
392 
393  int wheelId, stationId, sectorId, slId,layerId, cellId;
394  if ( ! mapping->readOutToGeometry(dduId, rosId, robId, tdcId, tdcChannel,
395  wheelId, stationId, sectorId, slId, layerId, cellId)) {
396 
397  DTWireId detId = DTWireId(wheelId, stationId, sectorId, slId, layerId, cellId);
398  int wire = detId.wire();
399 
400  DTDigi digi(wire, tdcTime, hitOrder[channelIndex.getCode()]);
401  digis.insertDigi(detId.layerId(),digi);
402 
403  }
404 
405  }
406  else if ( selector2 == 1 ) { // error word
407 
408  if ( debug_ ) edm::LogWarning("dturos_unpacker") << "Error word [" << std::dec << k << "] : " << std::hex << dataWord
409  << std::dec << " in slot " << slot << " in crate " << crate;
410 
411  int error = ( dataWord ) & 0x1FFFFFFF; // positions 0 -> 28
412  rwords.seterror(error);
413 
414  }
415 
416  }
417 
418  }
419 
420 
421  ++DTuROSiterator;
422  dataWord = (*DTuROSiterator); // Trailer AMC
423 
424 
425  rwords.settrailer(dataWord);
426  fwords.setuROS(slot, rwords);
427 
428  } // end for-loop container content
429 
430 
431  return;
432 }
433 
434 
435 int DTuROSRawToDigi::theDDU(int crate, int slot, int link, bool tenDDU) {
436 
437  int ros = theROS(slot,link);
438 
439  int ddu = 772;
440 
441  //if (crate == 1368) { ddu = 775; }
443 
444  if (crate == FEDNumbering::MINDTUROSFEDID) {
445  if (slot < 7)
446  ddu = 770;
447  else
448  ddu = 771;
449  }
450 
451  if (crate == (FEDNumbering::MINDTUROSFEDID+1)) { ddu = 772; }
452 
453  if (crate == FEDNumbering::MAXDTUROSFEDID) {
454  if (slot < 7)
455  ddu = 773;
456  else
457  ddu = 774;
458  }
459 
460  if (ros > 6 && tenDDU && ddu < 775)
461  ddu += 5;
462 
463  return ddu;
464 }
465 
466 
467 int DTuROSRawToDigi::theROS(int slot, int link) {
468 
469  if (slot%6 == 5) return link+1;
470 
471  int ros = (link/24) + 3*(slot%6) - 2;
472  return ros;
473 }
474 
475 
476 int DTuROSRawToDigi::theROB(int slot, int link) {
477 
478  if (slot%6 == 5) return 23;
479 
480  int rob = link%24;
481  if (rob < 15) return rob;
482  if (rob == 15) return 24;
483  return rob-1;
484 }
485 
486 
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
void setuROS(int slot, DTuROSROSData rwords)
int theROS(int slot, int link)
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:137
void settrailer(long dword)
void process(int DTuROSFED, edm::Handle< FEDRawDataCollection > data, edm::ESHandle< DTReadOutMapping > mapping, DTDigiCollection &digis, DTuROSFEDData &fwords)
void setfed(int fed)
~DTuROSRawToDigi() override
Destructor.
int theROB(int slot, int link)
void settrailer(long dword)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
std::vector< int > feds_
int readOutToGeometry(int dduId, int rosId, int robId, int tdcId, int channelId, DTWireId &wireId) const
transform identifiers
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
void setheader1(long dword)
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:47
void setokxword(int i, long okxword)
void seterror(int error)
void setslotsize(int slot, int size)
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
void setexword(long exword)
unsigned char * lineFED
uint32_t getCode() const
Getters ///////////////////////.
void readline(int &lines, long &dataWord)
int theDDU(int crate, int slot, int link, bool tenDDU)
void setokword1(long okword)
void produce(edm::Event &e, const edm::EventSetup &c) override
Produce digis out of raw data.
long getokword1() const
void setheader2(long dword)
Definition: DTDigi.h:17
bool fillRawData(edm::Event &e, const edm::EventSetup &c, DTDigiCollection &digis, std::vector< DTuROSFEDData > &words)
Generate and fill FED raw data for a full event.
void setheader1(long dword)
int k[5][pyjets_maxn]
int wire() const
Return the wire number.
Definition: DTWireId.h:56
edm::InputTag DTuROSInputTag_
DTuROSRawToDigi(const edm::ParameterSet &pset)
Constructor.
void setnslots(int nslots)
void setslot(int slot)
edm::EDGetTokenT< FEDRawDataCollection > Raw_token
void setheader2(long dword)
DTLayerId layerId() const
Return the corresponding LayerId.
Definition: DTWireId.h:62
void calcCRC(long, int &)
Definition: DTCRC.cc:3
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
T get() const
Definition: EventSetup.h:62
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:28
void setokword2(long okword)
void setevtlgth(int evtLgth)
def move(src, dest)
Definition: eostools.py:511