forked from philiprodrigues/felix-long-readout-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPdspChannelMapService.cpp
More file actions
executable file
·455 lines (386 loc) · 15.3 KB
/
PdspChannelMapService.cpp
File metadata and controls
executable file
·455 lines (386 loc) · 15.3 KB
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
///////////////////////////////////////////////////////////////////////////////////////////////////
// Class: PdspChannelMapService
// Module type: service
// File: PdspChannelMapService.h
// Author: Jingbo Wang (jiowang@ucdavis.edu), February 2018
//
// Implementation of hardware-offline channel mapping reading from a file.
///////////////////////////////////////////////////////////////////////////////////////////////////
#include "PdspChannelMapService.h"
#include "FelixFormat.hh" // For dune::FelixFrame
#include <stdexcept>
#include <sstream>
namespace
{
const char* felix_channelmap_text=
#include "protoDUNETPCChannelMap_FELIX_v4.txt"
;
const char* rce_channelmap_text=
#include "protoDUNETPCChannelMap_RCE_v4.txt"
;
}
// Bad channel value
unsigned int bad() {
unsigned int val = std::numeric_limits<unsigned int>::max();
return val;
}
PdspChannelMapService::PdspChannelMapService() {
fBadCrateNumberWarningsIssued = 0;
fBadSlotNumberWarningsIssued = 0;
fBadFiberNumberWarningsIssued = 0;
fSSPBadChannelNumberWarningsIssued = 0;
fASICWarningsIssued = 0;
fASICChanWarningsIssued = 0;
std::istringstream inFile(rce_channelmap_text);
// if(inFile.bad() || inFile.fail() || !inFile.is_open()){
// throw std::runtime_error(std::string("Bad file ")+std::string(rcename));
// }
std::string line;
while (std::getline(inFile,line)) {
unsigned int crateNo, slotNo, fiberNo, FEMBChannel, StreamChannel, slotID, fiberID, chipNo, chipChannel, asicNo, asicChannel, planeType, offlineChannel;
std::stringstream linestream(line);
linestream >> crateNo >> slotNo >> fiberNo>> FEMBChannel >> StreamChannel >> slotID >> fiberID >> chipNo >> chipChannel >> asicNo >> asicChannel >> planeType >> offlineChannel;
// fill lookup tables. Throw an exception if any number is out of expected bounds.
// checking for negative values produces compiler warnings as these are unsigned ints
if (offlineChannel >= fNChans)
{
throw std::logic_error("Ununderstood Offline Channel");
}
if (crateNo >= fNCrates)
{
throw std::logic_error("Ununderstood Crate Number");
}
if (slotNo >= fNSlots)
{
throw std::logic_error("Ununderstood Slot Number");
}
if (fiberNo >= fNFibers)
{
throw std::logic_error("Ununderstood Fiber Number");
}
if (StreamChannel >= fNFEMBChans)
{
throw std::logic_error("Ununderstood FEMB (Stream) Channel Number");
}
farrayCsfcToOffline[crateNo][slotNo][fiberNo][StreamChannel] = offlineChannel;
fvAPAMap[offlineChannel] = crateNo;
fvWIBMap[offlineChannel] = slotNo;
fvFEMBMap[offlineChannel] = fiberNo;
fvFEMBChannelMap[offlineChannel] = FEMBChannel;
fvStreamChannelMap[offlineChannel] = StreamChannel;
fvSlotIdMap[offlineChannel] = slotID;
fvFiberIdMap[offlineChannel] = fiberID;
fvChipMap[offlineChannel] = chipNo;
fvChipChannelMap[offlineChannel] = chipChannel;
fvASICMap[offlineChannel] = asicNo;
fvASICChannelMap[offlineChannel] = asicChannel;
fvPlaneMap[offlineChannel] = planeType;
}
//inFile.close();
std::istringstream FELIXinFile(felix_channelmap_text);
// if(FELIXinFile.bad() || FELIXinFile.fail() || !FELIXinFile.is_open()){
// throw std::runtime_error(std::string("Bad file ")+std::string(felixname));
// }
while (std::getline(FELIXinFile,line)) {
unsigned int crateNo, slotNo, fiberNo, FEMBChannel, StreamChannel, slotID, fiberID, chipNo, chipChannel, asicNo, asicChannel, planeType, offlineChannel;
std::stringstream linestream(line);
linestream >> crateNo >> slotNo >> fiberNo>> FEMBChannel >> StreamChannel >> slotID >> fiberID >> chipNo >> chipChannel >> asicNo >> asicChannel >> planeType >> offlineChannel;
// fill lookup tables. Throw an exception if any number is out of expected bounds.
// checking for negative values produces compiler warnings as these are unsigned ints
if (offlineChannel >= fNChans)
{
throw std::logic_error("Ununderstood Offline Channel");
}
if (crateNo >= fNCrates)
{
throw std::logic_error("Ununderstood Crate Number");
}
if (slotNo >= fNSlots)
{
throw std::logic_error("Ununderstood Slot Number");
}
if (fiberNo >= fNFibers)
{
throw std::logic_error("Ununderstood Fiber Number");
}
if (StreamChannel >= fNFEMBChans)
{
throw std::logic_error("Ununderstood FEMB (Stream) Channel Number");
}
fFELIXarrayCsfcToOffline[crateNo][slotNo][fiberNo][StreamChannel] = offlineChannel;
fFELIXvAPAMap[offlineChannel] = crateNo;
fFELIXvWIBMap[offlineChannel] = slotNo;
fFELIXvFEMBMap[offlineChannel] = fiberNo;
fFELIXvFEMBChannelMap[offlineChannel] = FEMBChannel;
fFELIXvStreamChannelMap[offlineChannel] = StreamChannel;
fFELIXvSlotIdMap[offlineChannel] = slotID;
fFELIXvFiberIdMap[offlineChannel] = fiberID;
fFELIXvChipMap[offlineChannel] = chipNo;
fFELIXvChipChannelMap[offlineChannel] = chipChannel;
fFELIXvASICMap[offlineChannel] = asicNo;
fFELIXvASICChannelMap[offlineChannel] = asicChannel;
fFELIXvPlaneMap[offlineChannel] = planeType;
}
//inFile.close();
// APA numbering -- hardcoded here.
// Installation numbering:
// APA5 APA6 APA4
// beam -->
// APA3 APA2 APA1
//
// The Offline numbering:
// APA1 APA3 APA5
// beam -->
// APA0 APA2 APA4
//
fvInstalledAPA[0] = 3;
fvInstalledAPA[1] = 5;
fvInstalledAPA[2] = 2;
fvInstalledAPA[3] = 6;
fvInstalledAPA[4] = 1;
fvInstalledAPA[5] = 4;
// and the inverse map -- shifted by 1 -- the above list must start counting at 1.
for (size_t i=0; i<6; ++i)
{
fvTPCSet_VsInstalledAPA[fvInstalledAPA[i]-1] = i;
}
}
// assumes crate goes from 1-6, in "installed crate ordering"
// assumes slot goes from 0-5.
// assumes fiber goes from 1-4.
// These conventions are observed in Run 2973, a cryo commissioning run.
unsigned int PdspChannelMapService::GetOfflineNumberFromDetectorElements(unsigned int crate, unsigned int slot, unsigned int fiber, unsigned int streamchannel, FelixOrRCE frswitch) {
unsigned int offlineChannel=0;
unsigned int lcrate = crate;
unsigned int lslot = slot;
unsigned int lfiber = fiber;
if (crate > fNCrates || crate == 0)
{
if ( count_bits(fBadCrateNumberWarningsIssued) == 1)
{
std::cout << "PdspChannelMapService: Bad Crate Number, expecting a number between 1 and 6. Falling back to 1. Ununderstood crate number=" << crate << std::endl;
}
fBadCrateNumberWarningsIssued++;
lcrate = 1;
}
if (slot >= fNSlots)
{
if (count_bits(fBadSlotNumberWarningsIssued) == 1)
{
std::cout << "PdspChannelMapService: Bad slot number, using slot number zero as a fallback. Ununderstood slot number: " << slot << std::endl;
}
fBadSlotNumberWarningsIssued++;
lslot = 0;
}
if (fiber > fNFibers || fiber == 0)
{
if (count_bits(fBadFiberNumberWarningsIssued)==1)
{
std::cout << "PdspChannelMapService: Bad fiber number, falling back to 1. Ununderstood fiber number: " << fiber;
}
fBadFiberNumberWarningsIssued++;
lfiber = 1;
}
if (streamchannel >= fNFEMBChans)
{
std::cout << streamchannel << " >= " << fNFEMBChans << std::endl;
throw std::logic_error("Ununderstood Stream (FEMB) chan");
}
if (frswitch == kRCE)
{
offlineChannel = farrayCsfcToOffline[fvTPCSet_VsInstalledAPA[lcrate-1]][lslot][lfiber-1][streamchannel];
}
else
{
offlineChannel = fFELIXarrayCsfcToOffline[fvTPCSet_VsInstalledAPA[lcrate-1]][lslot][lfiber-1][streamchannel];
}
return offlineChannel;
}
// does not depend on FELIX or RCE -- offline channels should always map to the same APA/crate regardless of RCE or FELIX
unsigned int PdspChannelMapService::APAFromOfflineChannel(unsigned int offlineChannel) const {
check_offline_channel(offlineChannel);
return fvAPAMap[offlineChannel];
// return fFELIXvAPAMap[offlineChannel]; // -- FELIX one -- should be the same
}
unsigned int PdspChannelMapService::InstalledAPAFromOfflineChannel(unsigned int offlineChannel) const {
check_offline_channel(offlineChannel);
unsigned int offlineAPA = fvAPAMap[offlineChannel];
if (offlineAPA > 5)
{
throw std::logic_error("Offline APA Number out of range");
}
return fvInstalledAPA[fvAPAMap[offlineChannel]];
}
// does not depend on FELIX or RCE
unsigned int PdspChannelMapService::WIBFromOfflineChannel(unsigned int offlineChannel) const {
check_offline_channel(offlineChannel);
return fvWIBMap[offlineChannel];
// return fFELIXvWIBMap[offlineChannel]; // -- FELIX one -- should be the same
}
// does not depend on FELIX or RCE
unsigned int PdspChannelMapService::FEMBFromOfflineChannel(unsigned int offlineChannel) const {
check_offline_channel(offlineChannel);
return fvFEMBMap[offlineChannel]+1;
//return fFELIXvFEMBMap[offlineChannel];
}
// does not depend on FELIX or RCE
unsigned int PdspChannelMapService::FEMBChannelFromOfflineChannel(unsigned int offlineChannel) const {
check_offline_channel(offlineChannel);
return fvFEMBChannelMap[offlineChannel];
// return fFELIXvFEMBChannelMap[offlineChannel]; // -- FELIX one -- should be the same
}
// this one does depend on FELIX or RCE
unsigned int PdspChannelMapService::StreamChannelFromOfflineChannel(unsigned int offlineChannel, FelixOrRCE frswitch) const {
check_offline_channel(offlineChannel);
if (frswitch == kRCE)
{
return fvStreamChannelMap[offlineChannel];
}
else
{
return fFELIXvStreamChannelMap[offlineChannel];
}
}
// does not depend on FELIX or RCE
unsigned int PdspChannelMapService::SlotIdFromOfflineChannel(unsigned int offlineChannel) const {
check_offline_channel(offlineChannel);
return fvSlotIdMap[offlineChannel];
// return fFELIXvSlotIdMap[offlineChannel]; // -- FELIX one -- should be the same
}
// may potentially depend on FELIX or RCE, but if fibers are switched between the WIB and the FELIX or RCE,
// we can fix this in the channel map but report with this method the fiber coming out of the WIB, not the
// one going in to the FELIX or RCE
unsigned int PdspChannelMapService::FiberIdFromOfflineChannel(unsigned int offlineChannel) const {
check_offline_channel(offlineChannel);
return fvFiberIdMap[offlineChannel];
}
// does not depend on FELIX or RCE
unsigned int PdspChannelMapService::ChipFromOfflineChannel(unsigned int offlineChannel) const {
check_offline_channel(offlineChannel);
return fvChipMap[offlineChannel];
}
unsigned int PdspChannelMapService::AsicFromOfflineChannel(unsigned int offlineChannel) const {
check_offline_channel(offlineChannel);
return fvChipMap[offlineChannel];
}
unsigned int PdspChannelMapService::ChipChannelFromOfflineChannel(unsigned int offlineChannel) const {
check_offline_channel(offlineChannel);
return fvChipChannelMap[offlineChannel];
}
// from David Adams -- use the chip channel instead of the asic channel
unsigned int PdspChannelMapService::AsicChannelFromOfflineChannel(unsigned int offlineChannel) const {
check_offline_channel(offlineChannel);
return fvChipChannelMap[offlineChannel];
}
// really shouldn't be using this as it doesn't mean asic
unsigned int PdspChannelMapService::ASICFromOfflineChannel(unsigned int offlineChannel) {
if (count_bits(fASICWarningsIssued) == 1)
{
std::cout << "PdspChannelMapService: Deprecated call to ASICFromOfflineChannel. Use AsicLinkFromOfflineChannel" << std::endl;
}
fASICWarningsIssued++;
check_offline_channel(offlineChannel);
return fvASICMap[offlineChannel];
}
unsigned int PdspChannelMapService::AsicLinkFromOfflineChannel(unsigned int offlineChannel) const {
check_offline_channel(offlineChannel);
return fvASICMap[offlineChannel];
}
unsigned int PdspChannelMapService::ASICChannelFromOfflineChannel(unsigned int offlineChannel) {
if (count_bits(fASICChanWarningsIssued) == 1)
{
std::cout << "PdspChannelMapService: Deprecated call to ASICChannelFromOfflineChannel. Not a meaningful number -- channels are grouped by 16's not 8's" << std::endl;
}
fASICChanWarningsIssued++;
check_offline_channel(offlineChannel);
return fvASICChannelMap[offlineChannel];
}
unsigned int PdspChannelMapService::PlaneFromOfflineChannel(unsigned int offlineChannel) const {
check_offline_channel(offlineChannel);
return fvPlaneMap[offlineChannel];
}
size_t PdspChannelMapService::count_bits(size_t i)
{
size_t result=0;
size_t s = sizeof(size_t)*8;
for (size_t j=0; j<s; ++j)
{
if (i & 1) ++result;
i >>= 1;
}
return result;
}
unsigned int PdspChannelMapService::SSPOfflineChannelFromOnlineChannel(unsigned int onlineChannel)
{
unsigned int lchannel = onlineChannel;
if (onlineChannel > fNSSPChans)
{
if (count_bits(fSSPBadChannelNumberWarningsIssued)==1)
{
std::cout << "PdspChannelMapService: Online Channel Number too high, using zero as a fallback: " << onlineChannel << std::endl;
}
fSSPBadChannelNumberWarningsIssued++;
lchannel = 0;
}
return farraySSPOnlineToOffline[lchannel];
}
unsigned int PdspChannelMapService::SSPOnlineChannelFromOfflineChannel(unsigned int offlineChannel) const
{
SSP_check_offline_channel(offlineChannel);
return farraySSPOfflineToOnline[offlineChannel];
}
unsigned int PdspChannelMapService::SSPAPAFromOfflineChannel(unsigned int offlineChannel) const
{
SSP_check_offline_channel(offlineChannel);
return fvSSPAPAMap[offlineChannel];
}
unsigned int PdspChannelMapService::SSPWithinAPAFromOfflineChannel(unsigned int offlineChannel) const
{
SSP_check_offline_channel(offlineChannel);
return fvSSPWithinAPAMap[offlineChannel];
}
unsigned int PdspChannelMapService::SSPGlobalFromOfflineChannel(unsigned int offlineChannel) const
{
SSP_check_offline_channel(offlineChannel);
return fvSSPGlobalMap[offlineChannel];
}
unsigned int PdspChannelMapService::SSPChanWithinSSPFromOfflineChannel(unsigned int offlineChannel) const
{
SSP_check_offline_channel(offlineChannel);
return fvSSPChanWithinSSPMap[offlineChannel];
}
unsigned int PdspChannelMapService::OpDetNoFromOfflineChannel(unsigned int offlineChannel) const
{
SSP_check_offline_channel(offlineChannel);
return fvOpDetNoMap[offlineChannel];
}
unsigned int PdspChannelMapService::getOfflineChannel(int crate, int slot, int fiber, unsigned int ch)
{
// I don't know why we need this pre-mangling: I just copied it from the raw decoder code
unsigned int fiberloc = 0;
if (fiber == 1){
fiberloc = 1;
}
else if(fiber == 2){
fiberloc = 3;
}
else{
std::cout << " Fiber number " << (int) fiber << " is expected to be 1 or 2 -- revisit logic" << std::endl;
fiberloc = 1;
}
unsigned int chloc = ch;
if (chloc > 127){
chloc -= 128;
fiberloc++;
}
unsigned int crateloc = crate;
unsigned int offline = GetOfflineNumberFromDetectorElements(crateloc, slot, fiberloc, chloc, PdspChannelMapService::kFELIX);
return offline;
}
unsigned int PdspChannelMapService::getOfflineChannel(const dune::FelixFrame* frame, unsigned int ch)
{
int crate = frame->crate_no();
int slot = frame->slot_no();
int fiber = frame->fiber_no();
return getOfflineChannel(crate, slot, fiber, ch);
}