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