ATLAS Offline Software
Loading...
Searching...
No Matches
RPCPatternMatching.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
5
10
11#include <fstream>
12
13namespace L1Muon {
14
16 ATH_MSG_INFO("Initializing " << name() << "...");
17
18 ATH_CHECK(m_keyRpcRdo.initialize());
19 ATH_CHECK(m_cablingKey.initialize());
20 ATH_CHECK(m_idHelperSvc.retrieve());
21 ATH_CHECK(m_geoCtxKey.initialize());
22 ATH_CHECK(detStore()->retrieve(m_detMgr));
23
25 ATH_CHECK(m_outputCandKey.initialize());
26
27 // Initialize patterns
28 if (!initializePatterns()) {
29 ATH_MSG_FATAL("Failed to initialize patterns.");
30 return StatusCode::FAILURE;
31 }
32
33 return StatusCode::SUCCESS;
34}
35
36StatusCode RPCPatternMatching::execute(const EventContext& ctx) const {
37 ATH_MSG_DEBUG("Executing " << name() << "...");
38
39 // Retrieve the input containers
40 const xAOD::NRPCRDOContainer* rdoContainer{nullptr};
41 ATH_CHECK(SG::get(rdoContainer, m_keyRpcRdo, ctx));
42
43 const Muon::RpcCablingMap* cablingMap{nullptr};
44 ATH_CHECK(SG::get(cablingMap, m_cablingKey, ctx));
45
46 // Get ActsGeometryContext
47 const ActsTrk::GeometryContext* gctxHandle{};
48 ATH_CHECK(SG::get(gctxHandle, m_geoCtxKey, ctx));
49 const ActsTrk::GeometryContext& geoCtx = *gctxHandle;
50
53 ATH_CHECK(outputCandHandle.record(std::make_unique<xAOD::L1RPCCandDataContainer>(), std::make_unique<xAOD::L1RPCCandDataAuxContainer>()));
54 auto outputCands = outputCandHandle.ptr();
55
56 // Get strip IDs for PatternMatching from SDOs/RDOs
57 std::vector<uint32_t> stripIDs;
58 std::map<uint32_t, uint32_t> strip_coordinates;
59 std::map<uint32_t, uint32_t> strip_etabin;
60
61 // sorting RDOs by offline Identifier to easily access both strip sides --> we can compute phi coordinate for BI strips
62 std::map<Identifier, RdoPairs, Muon::IdentifierByDetElSorter> sortedRdos{Muon::IdentifierByDetElSorter{m_idHelperSvc.get()}};
63 std::map<Identifier, Muon::RpcCablingData> sortedCabling;
64 for (const xAOD::NRPCRDO* rdo : *rdoContainer){
65 /* cabling data conversion */
66 Muon::RpcCablingData cabling{};
67 cabling.subDetector = rdo->subdetector();
68 cabling.boardSector = rdo->boardsector();
69 cabling.board = rdo->board();
70 cabling.channelId = rdo->channel();
71
72 if (!cablingMap->getOfflineId(cabling, msgStream())){
73 ATH_MSG_FATAL("Cabling retrieval failed "<<cabling);
74 return StatusCode::FAILURE;
75 }
76
77 Identifier offId{};
78 if (!cablingMap->convert(cabling, offId)){
79 ATH_MSG_FATAL("Cabling conversion failed "<<cabling);
80 return StatusCode::FAILURE;
81 }
82
83 const bool stripSide = cabling.stripSide();
84 sortedRdos[offId][stripSide] = rdo;
85 sortedCabling[offId] = cabling;
86 }
87
88 for (const auto& [offId, rdoPairs] : sortedRdos) {
89 Muon::RpcCablingData cabling = sortedCabling[offId];
90 getStripIdforAlg(rdoPairs, stripIDs, strip_coordinates, strip_etabin, cabling, offId, geoCtx);
91 }
92
93 // check for the required strip coincidence within each RPC layer
94 if (!checkStripCoincidence(stripIDs)) {
95 ATH_MSG_DEBUG("No required strip coincidence found, unable to form a valid muon candidate.");
96 return StatusCode::SUCCESS;
97 }
98 // use BI strip coordinates as seed for pattern matching
99 // to avoid multiple matches for the same eta bin, we store the already matched eta bins
100 // in uint16_t identifiers with the following structure:
101 // bits 0-4: eta bin (0-21)
102 // bits 5-10: sector (0-63)
103 std::map<uint16_t, std::vector<uint32_t>> usedEtaBins; // to store already matched eta bins
104 std::map<uint16_t, std::vector<RPCMuonPattern>> matchedEtaPatterns; // to store matched eta patterns
105 std::map<uint16_t, std::vector<RPCMuonPattern>> matchedPhiPatterns; // to store matched phi patterns
106 std::map<uint16_t, std::vector<RPCMuonPattern>> matchedPatterns; // to store matched eta+phi patterns --> muon candidates
107
108 // Check eta_bin from BI strips only to reduce combinatorics
109 // First check if any BI strip is present, if not use BM1 strips as seed (if present)
110 bool hasBIStrip = false;
111 for (auto strip: stripIDs) {
113 hasBIStrip = true;
114 break;
115 }
116 }
117
118 for (auto strip: stripIDs) {
119 bool isBIorBM1 = (RPCSLStripConverter::getStation(strip) == SLRPCStation::BI_BOR_BOM && RPCSLStripConverter::getMeasuresPhi(strip) == 0); // check if the strip is in the BI layer and is an eta strip
120 if (!hasBIStrip) {
121 isBIorBM1 = (RPCSLStripConverter::getStation(strip) == SLRPCStation::BM1 && RPCSLStripConverter::getMeasuresPhi(strip) == 0); // if no BI strip, check for BM1 eta strips
122 }
123 if (isBIorBM1) {
124 ATH_MSG_DEBUG("Using BI strip ID: " << std::hex << strip << " with coord: " << std::dec << strip_etabin[strip] << " as seed for pattern matching");
125 // Perform pattern matching using this BI strip as seed
126 // We get the eta bin from the strip coordinates and the trigger sector from the strip ID
127 uint16_t triggerSector = RPCSLStripConverter::getTriggerSector(strip); // extract trigger sector from strip ID
128 uint32_t etaBin = strip_etabin[strip];
129
130 std::vector<uint32_t>& binsInSector = usedEtaBins[triggerSector];
131
132 if (Acts::rangeContainsValue(binsInSector, etaBin)) {
133 ATH_MSG_DEBUG("Eta bin " << etaBin << " in sector " << triggerSector << " already matched, skipping to avoid duplicates.");
134 continue;
135 }
136
137 auto it_sector = m_etaPatterns.find(triggerSector);
138 if (it_sector == m_etaPatterns.end()) {
139 ATH_MSG_WARNING("No eta patterns found for sector: " << triggerSector);
140 continue;
141 }
142
143 auto it_etabin = it_sector->second.find(static_cast<uint16_t>(etaBin));
144 if (it_etabin == it_sector->second.end()) {
145 ATH_MSG_WARNING("No eta patterns found for sector: " << triggerSector << " and eta bin: " << etaBin);
146 continue;
147 }
148
149 for (const auto& pattern : it_etabin->second) {
150 matchPattern(pattern, stripIDs, matchedEtaPatterns[triggerSector], RPCSLStripConverter::getTriggerSector(strip));
151 }
152
153 if (matchedEtaPatterns[triggerSector].empty()) {
154 ATH_MSG_DEBUG("No eta patterns matched for sector: " << triggerSector << " and eta bin: " << etaBin);
155 continue;
156 } else {
157 ATH_MSG_DEBUG("Matched " << matchedEtaPatterns[triggerSector].size() << " eta patterns for sector: " << triggerSector << " and eta bin: " << etaBin);
158 }
159
160 it_sector = m_phiPatterns.find(triggerSector);
161 if (it_sector == m_phiPatterns.end()) {
162 ATH_MSG_WARNING("No phi patterns found for sector: " << triggerSector);
163 continue;
164 }
165
166 it_etabin = it_sector->second.find(static_cast<uint16_t>(etaBin));
167 if (it_etabin == it_sector->second.end()) {
168 ATH_MSG_WARNING("No phi patterns found for sector: " << triggerSector << " and eta bin: " << etaBin);
169 continue;
170 }
171
172 for (const auto& pattern : it_etabin->second) {
173 matchPattern(pattern, stripIDs, matchedPhiPatterns[triggerSector], RPCSLStripConverter::getTriggerSector(strip));
174 }
175
176 if (matchedPhiPatterns[triggerSector].empty()) {
177 ATH_MSG_DEBUG("No phi patterns matched for sector: " << triggerSector << " and eta bin: " << etaBin);
178 continue;
179 } else {
180 ATH_MSG_DEBUG("Matched " << matchedPhiPatterns[triggerSector].size() << " phi patterns for sector: " << triggerSector << " and eta bin: " << etaBin);
181 }
182
183 for (const auto& etaPattern : matchedEtaPatterns[triggerSector]) {
184 for (const auto& phiPattern : matchedPhiPatterns[triggerSector]) {
185 matchEtaAndPhiPatterns(etaPattern, phiPattern, matchedPatterns[triggerSector]);
186 }
187 }
188 usedEtaBins[triggerSector].push_back(etaBin); // mark this eta bin in this sector as matched
189 }
190 }
191
192 // For now, we directly create final candidates
193 if (!createCandidate(matchedPatterns, *outputCands)) {
194 ATH_MSG_WARNING("Failed to create candidates from matched patterns.");
195 return StatusCode::SUCCESS; // continue even if candidate creation fails
196 }
197 return StatusCode::SUCCESS;
198}
199
200void RPCPatternMatching::matchEtaAndPhiPatterns(const RPCMuonPattern& etaPattern, const RPCMuonPattern& phiPattern, std::vector<RPCMuonPattern>& matchedPatterns) const {
201 // This function implements the logic to match a given eta and phi pattern together to form a muon candidate
202 // The matching logic can be based on the compatibility of the eta and phi patterns (e.g. same sector, compatible coordinates, etc.)
203 // For simplicity, we will just check if they belong to the same sector and if they have compatible eta bins
204
205 if (etaPattern.getTriggerSector() != phiPattern.getTriggerSector()) {
206 return; // patterns must be in the same sector to be matched
207 }
208
209 if (etaPattern.getEtaBin() != phiPattern.getEtaBin()) {
210 return; // patterns must have the same eta bin to be matched
211 }
212
213 // combine the eta and phi patterns infos
214 uint32_t combinedInfos = etaPattern.getInfos() | phiPattern.getInfos();
215
216 // create a new pattern with the combined infos and add it to the matched patterns
217 matchedPatterns.push_back(RPCMuonPattern(etaPattern, combinedInfos));
218
219}
220StatusCode RPCPatternMatching::loadPatternFile(const std::string& filePath, const std::string& patternType, PatternContainer& patternContainer) {
221 std::ifstream file(filePath, std::ios::binary);
222 if (!file) {
223 ATH_MSG_ERROR("Failed to open " << patternType << " pattern file: " << filePath);
224 return StatusCode::FAILURE;
225 }
226
227 // Read global triggerSector (sector ID)
228 uint16_t sector_id;
229 file.read(reinterpret_cast<char*>(&sector_id), sizeof(sector_id));
230 if (!file) {
231 ATH_MSG_ERROR("Failed to read sector ID from " << patternType << " pattern file: " << filePath);
232 return StatusCode::FAILURE;
233 }
234 ATH_MSG_INFO("Reading " << patternType << " patterns for sector ID: " << sector_id);
235
236 // Read patterns until EOF
237 while (true) {
238 uint32_t etaBin, numPatterns;
239 file.read(reinterpret_cast<char*>(&etaBin), sizeof(etaBin));
240 if (!file) break; // EOF
241 file.read(reinterpret_cast<char*>(&numPatterns), sizeof(numPatterns));
242 if (!file) break; // EOF
243
244 // Apply eta-specific assertions
245 if (patternType == "eta") {
246 assert(etaBin < 22); // assuming a maximum of 22 eta bins (0-21)
247 assert(numPatterns < 20000); // assuming a maximum of 20000 patterns per eta bin
248 }
249
250 std::vector<RPCMuonPattern> patterns;
251 ATH_MSG_DEBUG("Reading " << numPatterns << " patterns for eta bin: " << etaBin);
252 patterns.reserve(numPatterns);
253
254 for (uint32_t i = 0; i < numPatterns; i++) {
255 uint32_t bibm1, bm2bo, coordbibm1, coordbm2bo, infos, separator;
256 file.read(reinterpret_cast<char*>(&bibm1), sizeof(bibm1));
257 file.read(reinterpret_cast<char*>(&bm2bo), sizeof(bm2bo));
258 file.read(reinterpret_cast<char*>(&coordbibm1), sizeof(coordbibm1));
259 file.read(reinterpret_cast<char*>(&coordbm2bo), sizeof(coordbm2bo));
260 file.read(reinterpret_cast<char*>(&infos), sizeof(infos));
261 file.read(reinterpret_cast<char*>(&separator), sizeof(separator));
262
263 if (!file) {
264 ATH_MSG_ERROR("Incomplete pattern data in " << patternType << " file.");
265 return StatusCode::FAILURE;
266 }
267
268 if (separator != 0xFFFFFFFF) {
269 ATH_MSG_ERROR("Invalid separator " << std::hex << separator << " found in " << patternType << " patterns");
270 return StatusCode::FAILURE;
271 }
272
273 if (!patternSanityCheck(RPCMuonPattern(sector_id, bibm1, bm2bo, coordbibm1, coordbm2bo, infos))) {
274 ATH_MSG_WARNING("Pattern sanity check failed for " << patternType << " pattern with bibm1: " << std::hex << bibm1 << std::dec
275 << ", bm2bo: " << std::hex << bm2bo << std::dec
276 << ", coordbibm1: " << coordbibm1
277 << ", coordbm2bo: " << coordbm2bo
278 << ", infos: " << infos
279 << " in sector " << sector_id
280 << " and eta bin " << etaBin << ". Skipping this pattern.");
281 continue;
282 }
283
284 // Construct RPCMuonPattern
285 patterns.emplace_back(sector_id, bibm1, bm2bo, coordbibm1, coordbm2bo, infos);
286 }
287
288 patternContainer[static_cast<uint16_t>(sector_id)][static_cast<uint16_t>(etaBin)] = std::move(patterns);
289
290 auto& loadedPatterns = patternContainer[static_cast<uint16_t>(sector_id)][static_cast<uint16_t>(etaBin)];
291 ATH_MSG_DEBUG("Loaded " << loadedPatterns.size() << " / " << numPatterns << " " << patternType << " patterns for sector: " << sector_id << " and eta bin: " << etaBin);
292
293 if (!loadedPatterns.empty()) {
294 ATH_MSG_DEBUG("First pattern details - bibm1: " << std::hex << loadedPatterns.front().getBIBM1() << std::dec
295 << ", bm2bo: " << std::hex << loadedPatterns.front().getBM2BO() << std::dec
296 << ", coordbibm1: " << loadedPatterns.front().getCoordBIBM1()
297 << ", coordbm2bo: " << loadedPatterns.front().getCoordBM2BO()
298 << ", infos: " << loadedPatterns.front().getInfos());
299
300 ATH_MSG_DEBUG("Last pattern details - bibm1: " << std::hex << loadedPatterns.back().getBIBM1() << std::dec
301 << ", bm2bo: " << std::hex << loadedPatterns.back().getBM2BO() << std::dec
302 << ", coordbibm1: " << loadedPatterns.back().getCoordBIBM1()
303 << ", coordbm2bo: " << loadedPatterns.back().getCoordBM2BO()
304 << ", infos: " << loadedPatterns.back().getInfos());
305 } else {
306 ATH_MSG_WARNING("Pattern vector is empty! Skipping details printout.");
307 }
308 }
309
310 return StatusCode::SUCCESS;
311}
312
314 ATH_MSG_INFO("Initializing patterns...");
315
316 for (int sec = 0; sec < 64; ++sec) { // 64 trigger sectors
317 std::string etaFileName = std::format("dev/L1MuonS1RPCPattern/v1/L1MuonRPCPattern_Eta_Sector_{:}.bin", sec);
318 std::string phiFileName = std::format("dev/L1MuonS1RPCPattern/v1/L1MuonRPCPattern_Phi_Sector_{:}.bin", sec);
319 std::string etaFilePath = PathResolverFindCalibFile(etaFileName);
320 std::string phiFilePath = PathResolverFindCalibFile(phiFileName);
321
322 if (etaFilePath.empty() || phiFilePath.empty()) {
323 ATH_MSG_ERROR("Could not locate pattern files for sector " << sec);
324 return StatusCode::FAILURE;
325 }
326
327 if (loadPatternFile(etaFilePath, "eta", m_etaPatterns).isFailure()) return StatusCode::FAILURE;
328 if (loadPatternFile(phiFilePath, "phi", m_phiPatterns).isFailure()) return StatusCode::FAILURE;
329 }
330
331 ATH_MSG_INFO("Pattern initialization complete.");
332 return StatusCode::SUCCESS;
333}
334
335int RPCPatternMatching::getVirtualBIPhiStripNumber(float loc_pos_along_BIstrip, const MuonGMR4::RpcReadoutElement& readoutElement) const {
336 return RPCPatternUtils::getVirtualBIPhiStripNumber(loc_pos_along_BIstrip, readoutElement);
337}
338
339uint32_t RPCPatternMatching::getVirtualBIPhiStripCoordinate(const Amg::Vector3D& stripPos, float loc_pos_along_BIstrip) const {
340 return RPCPatternUtils::getVirtualBIPhiStripCoordinate(stripPos, loc_pos_along_BIstrip);
341}
342
343uint32_t RPCPatternMatching::getSLStripId(bool side, int sector, int doubletPhi, Muon::MuonStationIndex::StIndex RPClayer, int doubletR, int stationEta, int doubletZ, int stripNumber, bool measuresPhi) const {
344 return RPCPatternUtils::getSLStripId(side, sector, doubletPhi,
345 RPClayer, doubletR, stationEta,
346 doubletZ, stripNumber, measuresPhi);
347}
348
352
353void RPCPatternMatching::getStripIdforAlg(const RdoPairs& rdo_pair, std::vector<uint32_t>& stripIDs, std::map<uint32_t, uint32_t>& strip_coordinates, std::map<uint32_t, uint32_t>& strip_etabin,
354 const Muon::RpcCablingData& cabling, const Identifier& offId, const ActsTrk::GeometryContext& geoCtx) const {
355 return RPCPatternUtils::getStripIdforAlg(rdo_pair, stripIDs, strip_coordinates, strip_etabin, cabling, offId, geoCtx, m_idHelperSvc.get(), m_detMgr);
356}
357
358void RPCPatternMatching::matchPattern(const RPCMuonPattern& pattern, const std::vector<uint32_t>& stripIDs,
359 std::vector<RPCMuonPattern>& matchedPatterns, const uint16_t& triggerSector) const {
360 // This function implements the logic to match a given pattern against the provided strip IDs
361 bool match = false;
362 auto stripBI = pattern.getStripBI() | static_cast<uint32_t>(triggerSector) << 16;
363 auto stripBM1 = pattern.getStripBM1() | static_cast<uint32_t>(triggerSector) << 16;
364 auto stripBM2 = pattern.getStripBM2() | static_cast<uint32_t>(triggerSector) << 16;
365 auto stripBO = pattern.getStripBO() | static_cast<uint32_t>(triggerSector) << 16;
366 auto priority = pattern.getPriority();
367
368 // different case depending on the pattern priority
369 if (priority == 5) { //BI-BM2-BO
370 match = Acts::rangeContainsValue(stripIDs, stripBI) &&
371 Acts::rangeContainsValue(stripIDs, stripBM2) &&
372 Acts::rangeContainsValue(stripIDs, stripBO);
373 } else if (priority == 4) { //BI-BM1-BO
374 match = Acts::rangeContainsValue(stripIDs, stripBI) &&
375 Acts::rangeContainsValue(stripIDs, stripBM1) &&
376 Acts::rangeContainsValue(stripIDs, stripBO);
377 } else if (priority == 3) { //BM1-BM2-BO
378 match = Acts::rangeContainsValue(stripIDs, stripBM1) &&
379 Acts::rangeContainsValue(stripIDs, stripBM2) &&
380 Acts::rangeContainsValue(stripIDs, stripBO);
381 } else if (priority == 2) { //BI-BM1-BM2
382 match = Acts::rangeContainsValue(stripIDs, stripBI) &&
383 Acts::rangeContainsValue(stripIDs, stripBM1) &&
384 Acts::rangeContainsValue(stripIDs, stripBM2);
385 } else if (priority == 1) { //BI-BO
386 match = Acts::rangeContainsValue(stripIDs, stripBI) &&
387 Acts::rangeContainsValue(stripIDs, stripBO);
388 } else {
389 ATH_MSG_WARNING("Unknown pattern priority: " << priority);
390 }
391
392 if (match) {
393 matchedPatterns.push_back(pattern);
394 ATH_MSG_DEBUG("Pattern matched.");
395 // pattern.print();
396 }
397}
398
399bool RPCPatternMatching::checkStripCoincidence(std::vector<uint32_t>& stripIDs) const {
400 // This function checks for the required strip coincidence within each RPC layer
401 // (2/3 majority logic for BI and 1/2 logic for BM and BO) and verifies that there
402 // is a valid muon candidate to process.
403
404 // Loop over all possible trigger sectors (0-63) to check for coincidences
405 bool validCandidate = false;
406 std::vector<uint32_t> stripIDsAfterStripCoincidence;
407
408 std::sort(stripIDs.begin(), stripIDs.end());
409 for (uint16_t triggerSector = 0; triggerSector < 64; triggerSector++) {
410 std::vector<uint32_t> stripIDsInSector;
411 for (const auto& stripID : stripIDs) {
412 if (RPCSLStripConverter::getTriggerSector(stripID) == triggerSector) {
413 stripIDsInSector.push_back(stripID);
414 }
415 }
416
417 if (stripIDsInSector.empty()) continue; // No strips for this triggerSector, skip to the next one
418
419 // Check for coincidences in the current sector
420 bool hasBI[2] = {false, false}; // [0] = measuresEta, [1] = measuresPhi
421 bool hasBM1[2] = {false, false}; // [0] = measuresEta, [1] = measuresPhi
422 bool hasBM2[2] = {false, false}; // [0] = measuresEta, [1] = measuresPhi
423 bool hasBO[2] = {false, false}; // [0] = measuresEta, [1] = measuresPhi
424
425 std::sort(stripIDsInSector.begin(), stripIDsInSector.end());
426 std::vector<uint32_t> stripID_unique = stripIDsInSector; // create a copy of the original vector to keep only unique strip IDs that pass the coincidence requirement
427 stripID_unique.erase(std::unique(stripID_unique.begin(), stripID_unique.end()), stripID_unique.end());
428
429 for (int i = 0; i < int(stripID_unique.size()); ++i) {
431 uint32_t current_strip = stripID_unique[i];
432 uint32_t next_strip = current_strip + 0x2;
433 uint32_t prev_strip = current_strip - 0x2;
434 int count = std::count(stripIDsInSector.begin(), stripIDsInSector.end(), current_strip) +
435 std::count(stripIDsInSector.begin(), stripIDsInSector.end(), next_strip) +
436 std::count(stripIDsInSector.begin(), stripIDsInSector.end(), prev_strip);
437 if (count < 2) {
438 stripID_unique.erase(std::remove(stripID_unique.begin(), stripID_unique.end(), stripID_unique[i]), stripID_unique.end());
439 i--; // Adjust index after erasing
440 }
441 else {
442 if (!hasBI[stripID_unique[i]& 0x1]) {
443 hasBI[stripID_unique[i]& 0x1] = true;
444 }
445 }
446 }
447 else if (RPCSLStripConverter::getStation(stripID_unique[i]) == SLRPCStation::BM1) {
448 hasBM1[stripID_unique[i] & 0x1] = true;
449 } else if (RPCSLStripConverter::getStation(stripID_unique[i]) == SLRPCStation::BM2) {
450 hasBM2[stripID_unique[i] & 0x1] = true;
451 } else if (RPCSLStripConverter::getStation(stripID_unique[i]) == SLRPCStation::BO) {
452 // BORBOM are treated as BI strips
453 // if (some condition){
454 // // Do something
455 // } else {
456 hasBO[stripID_unique[i] & 0x1] = true;
457 // }
458 }
459 }
460 stripIDsAfterStripCoincidence.insert(stripIDsAfterStripCoincidence.end(), stripID_unique.begin(), stripID_unique.end());
461
462 bool valid_eta_cand = (hasBI[0] + hasBM1[0] + hasBM2[0] + hasBO[0] > 2) || (hasBI[0] + hasBO[0] == 2);
463 bool valid_phi_cand = (hasBI[1] + hasBM1[1] + hasBM2[1] + hasBO[1] > 2) || (hasBI[1] + hasBO[1] == 2);
464 if (valid_eta_cand && valid_phi_cand) {
465 validCandidate = true;
466 }
467 }
468
469 stripIDs = std::move(stripIDsAfterStripCoincidence); // Update the original stripIDs vector with the filtered results
470 return validCandidate;
471 }
472
473StatusCode RPCPatternMatching::createCandidate(const std::map<uint16_t, std::vector<RPCMuonPattern>>& matchedPatterns, xAOD::L1RPCCandDataContainer& outputCand) const {
474 // This function creates the final muon candidates from the matched patterns
475 // and stores them in the output container
476 static const std::map<int, int> th_map = {{4, 0}, {5, 1}, {6, 2}, {7, 3}, {8, 4}, {9, 5}, {10, 6}, \
477 {12, 7}, {15, 8}, {18, 9}, {20, 10}, {25, 11}, {30, 12}, \
478 {40, 13}, {80, 14}, {100, 15}};
479
480 // Provide up to two candidates per sector
481 for (const auto & [sector, patterns] : matchedPatterns){
482 int cand_count = 0;
483 //Select one pattern per eta bin (the one with the highest priority and then highest pt)
484 std::map<uint32_t, RPCMuonPattern> best_patterns;
485 getBestCandidatesPerEtaBin(patterns, best_patterns);
486
487 for (const auto& [etaBin, pattern] : best_patterns) {
488 if (cand_count >=2) {
489 ATH_MSG_DEBUG("Maximum number of candidates reached for sector " << sector << ", skipping remaining patterns.");
490 break; // limit to 2 candidates per sector
491 }
492 cand_count++;
493 uint16_t subdetectorId = pattern.getTriggerSector() > 31 ? 0x65 : 0x66; // 0x65 for positive eta, 0x66 for negative eta
494 uint16_t sectorId = pattern.getTriggerSector();
495 uint16_t bcTag = 0; // placeholder, to be set properly
496
497
498 xAOD::L1RPCCandData* newCand = outputCand.push_back(std::make_unique<xAOD::L1RPCCandData>());
499
500 newCand->initialize(subdetectorId, sectorId, bcTag);
501 newCand->setL1Eta(pattern.getEtaFloat());
502 newCand->setL1Phi(pattern.getPhiFloat());
503 newCand->setL1Pt(pattern.getPtFloat());
504 newCand->setL1Threshold(pattern.getMaxThreshold());
505 newCand->setL1CandCharge(pattern.getCharge());
506 newCand->setCoinType(pattern.getPriority());
507
508 std::array<float, 4> zPos{
509 pattern.getCoordBIFloat(),
510 pattern.getCoordBM1Float(),
511 pattern.getCoordBM2Float(),
512 pattern.getCoordBOFloat()};
513
514 newCand->setL1ZPos(zPos);
515
516 }
517 }
518
519 return StatusCode::SUCCESS;
520}
521
522void RPCPatternMatching::getBestCandidatesPerEtaBin(const std::vector<RPCMuonPattern>& patterns,
523 std::map<uint32_t, RPCMuonPattern>& best_patterns) const {
524 // This function selects the best candidate per eta bin based on priority and pt
525 // TO DO: review the selection criteria if SL firmware logic is different
526 // 1) Group patterns by eta bin
527 std::map<uint32_t, std::vector<RPCMuonPattern>> patterns_per_eta;
528 for (const auto& pattern : patterns) {
529 uint32_t etaBin = pattern.getEtaBin();
530 patterns_per_eta[etaBin].push_back(pattern);
531 }
532 // 2) Sort patterns within each eta bin by priority and pt
533 for (auto& [etaBin, eta_patterns] : patterns_per_eta) {
534 std::sort(eta_patterns.begin(), eta_patterns.end(),
535 [](const RPCMuonPattern& a, const RPCMuonPattern& b) {
536 if (a.getPriority() != b.getPriority()) {
537 return a.getPriority() > b.getPriority(); // higher priority first
538 }
539 return a.getPt() > b.getPt(); // higher pt first
540 });
541 // 3) Select the best pattern for this eta bin
542 best_patterns.emplace(etaBin, eta_patterns.front());
543 }
544}
545
547 // This function checks the sanity of the pattern data read from the files
548 // For example, it can check that the strip IDs are within expected ranges,
549 // that the priority is valid, etc.
550
551 if (pattern.getPriority() < 1 || pattern.getPriority() > 5) {
552 ATH_MSG_DEBUG("Invalid pattern priority: " << pattern.getPriority());
553 return false;
554 }
555 if (pattern.getStripBI() > 0x4000) {
556 ATH_MSG_DEBUG("Invalid BI strip ID: " << std::hex << pattern.getStripBI() << std::dec);
557 return false;
558 }
559 if (pattern.getStripBM1() > 0x8000 || pattern.getStripBM1() < 0x4000) {
560 ATH_MSG_DEBUG("Invalid BM1 strip ID: " << std::hex << pattern.getStripBM1() << std::dec);
561 return false;
562 }
563 if (pattern.getStripBM2() > 0xC000 || pattern.getStripBM2() < 0x8000) {
564 ATH_MSG_DEBUG("Invalid BM2 strip ID: " << std::hex << pattern.getStripBM2() << std::dec);
565 return false;
566 }
567 if (pattern.getStripBO() > 0x10000 || pattern.getStripBO() < 0xC000) {
568 ATH_MSG_DEBUG("Invalid BO strip ID: " << std::hex << pattern.getStripBO() << std::dec);
569 return false;
570 }
571 if (pattern.getEtaBin() > 22) {
572 ATH_MSG_DEBUG("Invalid eta bin: " << pattern.getEtaBin());
573 return false;
574 }
575
576 return true;
577}
578
579} // namespace L1Muon
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_ERROR(x,...)
#define ATH_MSG_WARNING(x,...)
#define ATH_MSG_INFO(x,...)
#define ATH_MSG_FATAL(x,...)
static Double_t a
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
size_t size() const
Number of registered mappings.
unsigned bcTag(unsigned bcBitMap)
static const Attributes_t empty
const ServiceHandle< StoreGateSvc > & detStore() const
value_type push_back(value_type pElem)
Add an element to the end of the collection.
uint16_t getTriggerSector() const
uint32_t getEtaBin() const
uint32_t getInfos() const
bool checkStripCoincidence(std::vector< uint32_t > &stripIDs) const
uint32_t getVirtualBIPhiStripCoordinate(const Amg::Vector3D &stripPos, float loc_pos_along_BIstrip) const
SG::ReadHandleKey< ActsTrk::GeometryContext > m_geoCtxKey
StatusCode createCandidate(const std::map< uint16_t, std::vector< RPCMuonPattern > > &matchedPatterns, xAOD::L1RPCCandDataContainer &outputCandHandle) const
virtual StatusCode initialize() override
uint32_t getBMBOPhiStripCoordinate(const Amg::Vector3D &stripPos) const
SG::WriteHandleKey< xAOD::L1RPCCandDataContainer > m_outputCandKey
Output Trigger candidates.
void getStripIdforAlg(const RdoPairs &rdoPairs, std::vector< uint32_t > &stripIDs, std::map< uint32_t, uint32_t > &strip_coordinates, std::map< uint32_t, uint32_t > &strip_etabin, const Muon::RpcCablingData &cabling, const Identifier &offId, const ActsTrk::GeometryContext &geoCtx) const
void matchPattern(const RPCMuonPattern &pattern, const std::vector< uint32_t > &stripIDs, std::vector< RPCMuonPattern > &matchedPatterns, const uint16_t &sector_header) const
void matchEtaAndPhiPatterns(const RPCMuonPattern &etaPattern, const RPCMuonPattern &phiPattern, std::vector< RPCMuonPattern > &matchedPatterns) const
std::array< const xAOD::NRPCRDO *, 2 > RdoPairs
ServiceHandle< Muon::IMuonIdHelperSvc > m_idHelperSvc
RPC Id helper.
int getVirtualBIPhiStripNumber(float loc_pos_along_BIstrip, const MuonGMR4::RpcReadoutElement &readoutElement) const
StatusCode loadPatternFile(const std::string &filePath, const std::string &patternType, PatternContainer &patternContainer)
virtual StatusCode execute(const EventContext &ctx) const override
const MuonGMR4::MuonDetectorManager * m_detMgr
uint32_t getSLStripId(bool side, int sector, int doubletPhi, Muon::MuonStationIndex::StIndex RPClayer, int doubletR, int stationEta, int doubletZ, int stripNumber, bool measuresPhi) const
SG::ReadCondHandleKey< Muon::RpcCablingMap > m_cablingKey
RPC cabling map.
std::map< uint16_t, std::map< uint16_t, std::vector< RPCMuonPattern > > > PatternContainer
void getBestCandidatesPerEtaBin(const std::vector< RPCMuonPattern > &matchedPatterns, std::map< uint32_t, RPCMuonPattern > &bestCandidates) const
bool patternSanityCheck(const RPCMuonPattern &pattern) const
SG::ReadHandleKey< xAOD::NRPCRDOContainer > m_keyRpcRdo
RPC Rdo.
static SLRPCStation getStation(uint32_t packed)
static uint8_t getMeasuresPhi(uint32_t packed)
static uint16_t getTriggerSector(uint32_t packed)
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
pointer_type ptr()
Dereference the pointer.
void setL1Pt(float pt)
Set the candidate pT.
void initialize(uint16_t subdetectorId, uint16_t sectorId, uint16_t bcTag)
Initialize candidate with basic properties.
void setCoinType(uint8_t coinType)
Set the coincidence type.
void setL1Eta(float eta)
Set the eta.
void setL1Threshold(uint8_t threshold)
Set the threshold.
void setL1Phi(float phi)
Set the phi.
void setL1CandCharge(uint8_t candCharge)
Set the candidate charge.
void setL1ZPos(std::array< float, 4 > &zPos)
Set the z positions of the RPC sector logic.
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:148
bool match(std::string s1, std::string s2)
match the individual directories of two strings
Definition hcg.cxx:359
std::vector< std::string > patterns
Definition listroot.cxx:187
Eigen::Matrix< double, 3, 1 > Vector3D
uint32_t getBMBOPhiStripCoordinate(const Amg::Vector3D &stripPos)
void getStripIdforAlg(const RdoPairs &rdo_pair, std::vector< uint32_t > &stripIDs, std::map< uint32_t, uint32_t > &strip_coordinates, std::map< uint32_t, uint32_t > &strip_etabin, const Muon::RpcCablingData &cabling, const Identifier &offId, const ActsTrk::GeometryContext &geoCtx, const Muon::IMuonIdHelperSvc *idHelperSvc, const MuonGMR4::MuonDetectorManager *detMgr)
uint32_t getSLStripId(bool side, int sector, int doubletPhi, Muon::MuonStationIndex::StIndex RPClayer, int doubletR, int stationEta, int doubletZ, int stripNumber, bool measuresPhi)
uint32_t getVirtualBIPhiStripCoordinate(const Amg::Vector3D &stripPos, float loc_pos_along_BIstrip)
int getVirtualBIPhiStripNumber(float loc_pos_along_BIstrip, const MuonGMR4::RpcReadoutElement &readoutElement)
StIndex
enum to classify the different station layers in the muon spectrometer
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.
DataModel_detail::iterator< DVL > unique(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of unique for DataVector/List.
DataModel_detail::iterator< DVL > remove(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end, const T &value)
Specialization of remove for DataVector/List.
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.
NRPCRDO_v1 NRPCRDO
Define the version of the NRPC RDO class.
Definition NRPCRDO.h:13
NRPCRDOContainer_v1 NRPCRDOContainer
Define the version of the NRPC RDO container.
L1RPCCandData_v1 L1RPCCandData
L1RPCCandDataContainer_v1 L1RPCCandDataContainer
Helper struct that is parsed to the cabling map to translate between the offline & online Identifiers...
TFile * file