ATLAS Offline Software
Loading...
Searching...
No Matches
TrigT1NSWSimExtras.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4#ifndef TRIGT1NSWSIMEXTRAS_H
5#define TRIGT1NSWSIMEXTRAS_H
6
7#include <algorithm>
8#include <array>
9#include <cstdint>
10#include <unordered_map>
11#include <iomanip>
12#include <cmath>
13#include <set>
14#include <sstream>
15#include <string>
16#include <string_view>
17#include <vector>
19
27
28namespace NSWL1 {
29 // Space
30 const std::string SPACE{" "};
31
32 // Underscore
33 const std::string UNDERSCORE{"_"};
34
35 // Comma
36 const std::string COMMA{","};
37
38 // Semicolon
39 const std::string SEMICOLON{";"};
40
41 // VHDL comment
42 const std::string VHDLCOMMENT{"--"};
43
44 // Wheels
45 const std::vector<std::string> WHEELS{"A", "C"};
46
47 // Number of layers/sectors per wheel
48 constexpr uint32_t NLAYERS{8};
49 constexpr uint32_t NSECTORS{16};
50
51 // Sector name
52 inline std::string sectorName(const std::string& wheel, const uint32_t sec) {
53 std::stringstream ss;
54 ss << wheel;
55 ss << std::setfill('0') << std::setw(2) << (sec + 1);
56 return ss.str();
57 }
58
59 // Switch to int phiID
60 inline int getSignedPhiID(const uint32_t phiid) {
61 // 1 bit of sign (0 = positive) followed by 5 bits of phiid
62 constexpr size_t nbitsPhi{5};
63 constexpr size_t mask{(1 << nbitsPhi) - 1};
64 return std::pow(-1, phiid >> nbitsPhi) * (phiid & mask);
65 }
66
67 // Pad specific conventions
68 namespace PAD {
69 // Large - Small
70 const std::string LARGE{"large"};
71 const std::string SMALL{"small"};
72
73 // Allowed layers
74 constexpr std::array<uint32_t, NLAYERS> LAYERS{0, 1, 2, 3, 4, 5, 6, 7};
75
76 // Number of sectors
77 constexpr std::array<uint32_t, NSECTORS> SECTORS{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
78
79 // Unpacking pad patterns from vhd file (start)
80 const std::string PATTERN_TAG{"pad_trigger_pattern_array :="};
81
82 // Unpacking pad patterns from vhd file (our signal that all the useful blocks are done)
83 const std::string PATTERN_END{"bandid_tds_numbers"};
84
85 // Expected length of 1 line of pattern info: pfeb0, chan0, ..., pfeb7, chan7, phiid
86 constexpr size_t PATTERNLEN{17};
87
88 // Source ID from wheel + sector
89 inline uint32_t wheelSectorToSourceID(const char wheel, const uint32_t sector) {
90 uint32_t prefix = 0x6d0020;
91 if(wheel == 'C') prefix += (1<<16);
92 return prefix+sector;
93 }
94
95 // Calculate pFEB
96 inline uint32_t getpFEBAthena(const int gas_gap, const int multiplet, const int stationEta) {
97 const auto layer = (multiplet-1)*4 + (gas_gap-1);
98 uint32_t pfeb = (std::abs(stationEta)-1)*8 + layer;
99 return pfeb;
100 }
101
102 inline std::pair<int,int> getNrowsNcols(const int pfeb, const bool isLarge) {
103 if (isLarge) {
104 if(pfeb == 0) return std::make_pair(17, 6);
105 if(pfeb == 1) return std::make_pair(17, 6);
106 if(pfeb == 2) return std::make_pair(16, 7);
107 if(pfeb == 3) return std::make_pair(16, 7);
108 if(pfeb == 4) return std::make_pair(16, 6);
109 if(pfeb == 5) return std::make_pair(16, 6);
110 if(pfeb == 6) return std::make_pair(16, 6);
111 if(pfeb == 7) return std::make_pair(16, 6);
112 if(pfeb == 8) return std::make_pair(14, 4);
113 if(pfeb == 9) return std::make_pair(14, 4);
114 if(pfeb == 10) return std::make_pair(15, 5);
115 if(pfeb == 11) return std::make_pair(15, 5);
116 if(pfeb == 12) return std::make_pair(14, 4);
117 if(pfeb == 13) return std::make_pair(14, 4);
118 if(pfeb == 14) return std::make_pair(14, 4);
119 if(pfeb == 15) return std::make_pair(14, 4);
120 if(pfeb == 16) return std::make_pair(15, 4);
121 if(pfeb == 17) return std::make_pair(15, 4);
122 if(pfeb == 18) return std::make_pair(14, 5);
123 if(pfeb == 19) return std::make_pair(14, 5);
124 if(pfeb == 20) return std::make_pair(13, 4);
125 if(pfeb == 21) return std::make_pair(13, 4);
126 if(pfeb == 22) return std::make_pair(14, 4);
127 if(pfeb == 23) return std::make_pair(14, 4);
128 }
129 else {
130 if(pfeb == 0) return std::make_pair(17, 4);
131 if(pfeb == 1) return std::make_pair(17, 4);
132 if(pfeb == 2) return std::make_pair(18, 4);
133 if(pfeb == 3) return std::make_pair(18, 4);
134 if(pfeb == 4) return std::make_pair(17, 4);
135 if(pfeb == 5) return std::make_pair(17, 4);
136 if(pfeb == 6) return std::make_pair(17, 3);
137 if(pfeb == 7) return std::make_pair(17, 3);
138 if(pfeb == 8) return std::make_pair(16, 3);
139 if(pfeb == 9) return std::make_pair(16, 3);
140 if(pfeb == 10) return std::make_pair(15, 3);
141 if(pfeb == 11) return std::make_pair(15, 3);
142 if(pfeb == 12) return std::make_pair(15, 2);
143 if(pfeb == 13) return std::make_pair(15, 2);
144 if(pfeb == 14) return std::make_pair(15, 3);
145 if(pfeb == 15) return std::make_pair(15, 3);
146 if(pfeb == 16) return std::make_pair(13, 3);
147 if(pfeb == 17) return std::make_pair(13, 3);
148 if(pfeb == 18) return std::make_pair(14, 3);
149 if(pfeb == 19) return std::make_pair(14, 3);
150 if(pfeb == 20) return std::make_pair(12, 2);
151 if(pfeb == 21) return std::make_pair(12, 2);
152 if(pfeb == 22) return std::make_pair(13, 3);
153 if(pfeb == 23) return std::make_pair(13, 3);
154 }
155 return std::make_pair(0,0);
156 }
157
158 inline std::pair<int,int> getRowColAthena(const int athenaCh) {
159 const int arow = (athenaCh-1)%18;
160 const int acol = int((athenaCh-1)/18);
161 return std::make_pair(arow, acol);
162 }
163
164 inline uint32_t getPadchAthena(const int athCh, const int pfeb, const int sector, int gasGap) {
165 bool isLarge = (sector % 2 == 0);
166 const auto [Nrows, Ncols] = getNrowsNcols(pfeb, isLarge);
167 const auto [arow, acol] = getRowColAthena(athCh);
168 //row and column counting all begin at 0
169 const int ABrow = Nrows -1 -arow;
170 const int ABcol = ((gasGap-1)%2 ==0) ? (Ncols-1)-acol : acol;
171 return ABrow*Ncols + ABcol;
172 }
173
174 // Dummy bandid
175 constexpr uint32_t DUMMY_BANDID{0xff};
176
178 constexpr std::array<uint32_t,8> DUMMYPADS{104,105,110,111,112,113,114,115};
179
180 // Pad trigger ID
181 constexpr uint32_t PAD_TRIGGER_ROB{0x20};
182
183 // Pad trigger N(BC) in readout
184 constexpr uint32_t PAD_TRIGGER_READOUT_NBC{7};
185
186 // Wheels
187 constexpr uint32_t MUON_STGC_ENDCAP_A_SIDE{0x6D};
188 constexpr uint32_t MUON_STGC_ENDCAP_C_SIDE{0x6E};
189
190 // Number of pfeb per radius
191 constexpr uint32_t NPFEB_PER_RADIUS{NLAYERS};
192
193 // Unpacking a line of pattern info
194 constexpr size_t I_PFEB0 = 0;
195 constexpr size_t I_PADCHAN0 = 1;
196 constexpr size_t I_PFEB1 = 2;
197 constexpr size_t I_PADCHAN1 = 3;
198 constexpr size_t I_PFEB2 = 4;
199 constexpr size_t I_PADCHAN2 = 5;
200 constexpr size_t I_PFEB3 = 6;
201 constexpr size_t I_PADCHAN3 = 7;
202 constexpr size_t I_PFEB4 = 8;
203 constexpr size_t I_PADCHAN4 = 9;
204 constexpr size_t I_PFEB5 = 10;
205 constexpr size_t I_PADCHAN5 = 11;
206 constexpr size_t I_PFEB6 = 12;
207 constexpr size_t I_PADCHAN6 = 13;
208 constexpr size_t I_PFEB7 = 14;
209 constexpr size_t I_PADCHAN7 = 15;
210 constexpr size_t I_PHIID = 16;
211
212 /*
213 * The PriorityEncoder deals with the cases where we have more than one trigger segment in the same BC.
214 * The stripTDS can only deal with one request per BC, so if two triggers have bandIDs that require strips from the same stripTDS, we can only send one of the triggers.
215 * The trigger with highest BandID is given priority.
216 * This function defines for each BandID a range of forbidden bandIDs. It returns the lower bound of the forbidden range.
217 *
218 * Ex: {94,84} means if we have a trigger with bandID 94, the forbidden range is 84-94.
219 * Any additional trigger with BandID in that range will be ignored.
220 *
221 * So it would be something like map<triggered bandID, lower bound of the forbidden BandID range>
222 */
223 inline uint32_t priorityEncoderL(uint32_t bandid) {
224 static const std::unordered_map<uint32_t,uint32_t> bandids = {
225 {94,84}, {93,84}, {92,84}, {91,84}, {90,82},
226 {89,80}, {88,80}, {87,77}, {86,76}, {85,76}, {84,74}, {83,74}, {82,72}, {81,72}, {80,70},
227 {79,70}, {78,70}, {77,67}, {76,66}, {75,66}, {74,66}, {73,62}, {72,62}, {71,62}, {70,62},
228 {69,62}, {68,62}, {67,62}, {66,62}, {65,54}, {64,54}, {63,54}, {62,54}, {61,51}, {60,51},
229 {59,51}, {58,51}, {57,51}, {56,46}, {55,45}, {54,44}, {53,44}, {52,43}, {51,41}, {50,41},
230 {49,41}, {48,41}, {47,41}, {46,36}, {45,35}, {44,35}, {43,33}, {42,32}, {41,32}, {40,32},
231 {39,32}, {38,32}, {37,32}, {36,32}, {35,24}, {34,24}, {33,24}, {32,22}, {31,22}, {30,22},
232 {29,22}, {28,22}, {27,22}, {26,22}, {25,22}, {24,14}, {23,14}, {22,12}, {21,12}, {20,12},
233 {19,12}, {18,12}, {17,12}, {16,12}, {15,12}, {14, 6}, {13, 6}, {12, 6}, {11, 6}, {10, 6},
234 { 9, 6}, { 8, 6}, { 7, 6}, { 6, 6}};
235 auto it = bandids.find(bandid);
236 if(it == bandids.end()) return bandid;
237 else return it->second;
238 }
239 inline uint32_t priorityEncoderS(uint32_t bandid) {
240 static const std::unordered_map<uint32_t,uint32_t> bandids = {
241 {94,85}, {93,85}, {92,85}, {91,80}, {90,80},
242 {89,80}, {88,80}, {87,80}, {86,76}, {85,74}, {84,74}, {83,74}, {82,74}, {81,69}, {80,69},
243 {79,69}, {78,69}, {77,69}, {76,64}, {75,64}, {74,64}, {73,64}, {72,64}, {71,64}, {70,64},
244 {69,64}, {68,57}, {67,57}, {66,57}, {65,57}, {64,53}, {63,53}, {62,53}, {61,53}, {60,53},
245 {59,53}, {58,47}, {57,46}, {56,46}, {55,44}, {54,44}, {53,43}, {52,43}, {51,43}, {50,43},
246 {49,43}, {48,37}, {47,37}, {46,37}, {45,37}, {44,33}, {43,33}, {42,33}, {41,33}, {40,33},
247 {39,33}, {38,33}, {37,33}, {36,25}, {35,25}, {34,25}, {33,22}, {32,22}, {31,22}, {30,22},
248 {29,22}, {28,22}, {27,22}, {26,22}, {25,14}, {24,14}, {23,12}, {22,12}, {21,12}, {20,12},
249 {19,12}, {18,12}, {17,12}, {16,12}, {15,12}, {14, 6}, {13, 6}, {12, 5}, {11, 5}, {10, 5},
250 { 9, 5}, { 8, 5}, { 7, 5}, { 6, 2}, { 5, 2}, { 4, 2}, { 3, 2}, { 2, 2}};
251 auto it = bandids.find(bandid);
252 if(it == bandids.end()) return bandid;
253 else return it->second;
254 }
255
256 inline bool isDummyPad(const uint32_t padchan) {
257 return std::count(DUMMYPADS.begin(), DUMMYPADS.end(), padchan);
258 }
259
260 // Helper functions: contains
261 inline bool contains(const std::string& str, const std::string& substr) {
262 return str.find(substr) != std::string::npos;
263 }
264 inline bool contains(const std::vector<std::string>& cont, const std::string& val) {
265 return std::count(cont.begin(), cont.end(), val);
266 }
267 inline bool contains(const std::vector<uint32_t>& cont, const uint32_t val) {
268 return std::count(cont.begin(), cont.end(), val);
269 }
270 inline bool contains(const std::set<uint32_t>& cont, const uint32_t val) {
271 return cont.contains(val);
272 }
273
274 // Helper functions: replacement
275 inline std::string replace(std::string subject, std::string_view search, std::string_view replacement) {
276 if (search.empty()) return subject;
277
278 for (std::size_t pos = 0; (pos = subject.find(search, pos)) != std::string::npos;
279 pos += replacement.size()) {
280 subject.replace(pos, search.size(), replacement);
281 }
282 return subject;
283 }
284
285 /* Helper function: parsing line
286 * - take a line like this and return the BandID as an integer, i.e.: "constant bandid_91_small_patterns : pad_trigger_pattern_array := ("
287 * - Retrieve first object: bandid_91_small_patterns
288 * - Split the object in its components by underscore: 'constant bandid', '91', 'small', 'patterns',...
289 * - Now save the number: 91
290 */
291 inline uint32_t parseLineForBandid(const std::string& line) {
292 const auto & word_split = CxxUtils::tokenize(line, UNDERSCORE);
293 return static_cast<uint32_t>(std::stoul(word_split.at(1)));
294 }
295 }
296
297 // MM specific conventions
298 namespace MM {
299 // Wheels
300 constexpr uint32_t MUON_MM_ENDCAP_A_SIDE{0x6B};
301 constexpr uint32_t MUON_MM_ENDCAP_C_SIDE{0x6C};
302 }
303
304 // Find side by source ID
305 inline bool isA(const uint32_t sourceid) {
306 return ( ((sourceid >> 16) & 0xff) == PAD::MUON_STGC_ENDCAP_A_SIDE || ((sourceid >> 16) & 0xff) == MM::MUON_MM_ENDCAP_A_SIDE );
307 }
308}
309#endif
static Double_t ss
void search(TDirectory *td, const std::string &s, std::string cwd, node *n)
recursive directory search for TH1 and TH2 and TProfiles
Definition hcg.cxx:743
std::vector< std::string > tokenize(std::string_view the_str, std::string_view delimiters)
Splits the string into smaller substrings.
constexpr uint32_t MUON_MM_ENDCAP_A_SIDE
constexpr uint32_t MUON_MM_ENDCAP_C_SIDE
constexpr size_t I_PADCHAN1
const std::string SMALL
constexpr size_t I_PFEB7
uint32_t priorityEncoderS(uint32_t bandid)
std::string replace(std::string subject, std::string_view search, std::string_view replacement)
constexpr size_t I_PADCHAN0
constexpr size_t I_PADCHAN2
constexpr size_t I_PADCHAN7
constexpr std::array< uint32_t, 8 > DUMMYPADS
Dummy pads.
constexpr uint32_t NPFEB_PER_RADIUS
constexpr size_t I_PHIID
constexpr std::array< uint32_t, NLAYERS > LAYERS
constexpr size_t I_PFEB2
uint32_t priorityEncoderL(uint32_t bandid)
const std::string PATTERN_END
constexpr uint32_t PAD_TRIGGER_READOUT_NBC
uint32_t getPadchAthena(const int athCh, const int pfeb, const int sector, int gasGap)
bool contains(const std::string &str, const std::string &substr)
constexpr size_t I_PADCHAN6
bool isDummyPad(const uint32_t padchan)
constexpr size_t I_PFEB3
constexpr size_t I_PADCHAN3
constexpr size_t I_PFEB5
std::pair< int, int > getRowColAthena(const int athenaCh)
constexpr size_t I_PADCHAN4
const std::string PATTERN_TAG
const std::string LARGE
constexpr size_t I_PFEB1
uint32_t parseLineForBandid(const std::string &line)
uint32_t wheelSectorToSourceID(const char wheel, const uint32_t sector)
constexpr std::array< uint32_t, NSECTORS > SECTORS
constexpr size_t PATTERNLEN
constexpr uint32_t MUON_STGC_ENDCAP_A_SIDE
constexpr size_t I_PFEB0
constexpr size_t I_PFEB6
constexpr size_t I_PFEB4
constexpr uint32_t PAD_TRIGGER_ROB
uint32_t getpFEBAthena(const int gas_gap, const int multiplet, const int stationEta)
constexpr uint32_t DUMMY_BANDID
constexpr size_t I_PADCHAN5
std::pair< int, int > getNrowsNcols(const int pfeb, const bool isLarge)
constexpr uint32_t MUON_STGC_ENDCAP_C_SIDE
PadEmulatorCoincidences.
bool isA(const uint32_t sourceid)
const std::vector< std::string > WHEELS
const std::string COMMA
std::string sectorName(const std::string &wheel, const uint32_t sec)
const std::string SEMICOLON
constexpr uint32_t NLAYERS
const std::string UNDERSCORE
int getSignedPhiID(const uint32_t phiid)
constexpr uint32_t NSECTORS
const std::string SPACE
const std::string VHDLCOMMENT