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