CbmRoot
Loading...
Searching...
No Matches
CbmGeoNodeMapBuilder.cxx
Go to the documentation of this file.
1/* Copyright (C) 2025 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Sergei Zharko [committer] */
4
9
11
12#include "CbmGeoHelper.h"
13#include "CbmMvdAddress.h"
14#include "CbmMvdDetector.h"
15#include "CbmMvdSensor.h"
16#include "CbmStsSetup.h"
17#include "CbmTofAddress.h"
18#include "CbmTrdAddress.h"
19
20#include <FairRuntimeDb.h>
21
22#include <TGeoManager.h>
23#include <TGeoPhysicalNode.h>
24
25using cbm::GeoNodeMap;
27
28// A tag for convenient ECbmModuleId dispatching in lambdas
29template<ECbmModuleId ModuleId>
30using Tag = std::integral_constant<ECbmModuleId, ModuleId>;
31
32// ---------------------------------------------------------------------------------------------------------------------
33//
34template<>
35std::unordered_map<uint32_t, std::string> GeoNodeMapBuilder::Add<ECbmModuleId::kMvd>()
36{
37 std::unordered_map<uint32_t, std::string> res;
38 const auto& mvdSensorMap = CbmMvdDetector::Instance()->GetSensorMap();
39 for (auto [address, pSensor] : mvdSensorMap) {
40 res[address] = pSensor->GetNodeName().Data();
41 }
42 return res;
43}
44
45// ---------------------------------------------------------------------------------------------------------------------
46//
47template<>
48std::unordered_map<uint32_t, std::string> GeoNodeMapBuilder::Add<ECbmModuleId::kSts>()
49{
50 std::unordered_map<uint32_t, std::string> res;
51 const auto* pSetup = CbmStsSetup::Instance();
52 for (int iSensor = 0; iSensor < pSetup->GetNofSensors(); ++iSensor) {
53 const auto* pSensor = pSetup->GetSensor(iSensor);
54 assert(pSensor);
55 const auto* pNode = pSensor->GetNode();
56 assert(pNode);
57 std::string path = pNode->GetName();
58 if (path.compare(0, 4, "cave") == 0) { // The path is global but does not start with '/'
59 path = "/" + path;
60 }
61 uint32_t address = pSensor->GetAddress();
62 res[address] = path;
63 }
64 return res;
65}
66
67// ---------------------------------------------------------------------------------------------------------------------
68//
69template<>
70std::unordered_map<uint32_t, std::string> GeoNodeMapBuilder::Add<ECbmModuleId::kTof>()
71{
72 std::unordered_map<uint32_t, std::string> res;
73
74 assert(gGeoManager);
75 auto vRpcPaths{GeoHelper::CollectNodes("tof", std::regex{"counter"}, "", gGeoManager->GetTopNode())};
76 std::regex rpcPattern{"module_(\\d+)_(\\d+)/gas_box_(\\d+)/counter_(\\d+)"};
77
78 for (const auto& rpcPath : vRpcPaths) {
79 std::smatch match;
80 std::string line{rpcPath.Data()};
81 if (std::regex_search(line, match, rpcPattern)) {
82 int smTypeId{std::stoi(match[1])};
83 int smId{std::stoi(match[2])};
84 int rpcId{std::stoi(match[4])};
85 uint32_t address{CbmTofAddress::GetUniqueAddress(smId, rpcId, 0, 0, smTypeId, 0)};
86 res[address] = std::string(rpcPath.Data());
87 }
88 }
89
90 return res;
91}
92
93// ---------------------------------------------------------------------------------------------------------------------
94//
95template<>
96std::unordered_map<uint32_t, std::string> GeoNodeMapBuilder::Add<ECbmModuleId::kTrd>()
97{
98 std::unordered_map<uint32_t, std::string> res;
99
100 assert(gGeoManager);
101 auto vRpcPaths{GeoHelper::CollectNodes("tof", std::regex{"counter"}, "", gGeoManager->GetTopNode())};
102 std::regex rpcPattern{"layer(\\d+)_(\\d+)/module(\\d+)_(\\d+)"};
103
104 for (const auto& rpcPath : vRpcPaths) {
105 std::smatch match;
106 std::string line{rpcPath.Data()};
107 if (std::regex_search(line, match, rpcPattern)) {
108 int layerId{std::stoi(match[1])};
109 int moduleId{std::stoi(match[3])};
110 uint32_t address{CbmTrdAddress::GetAddress(layerId, moduleId, 0, 0, 0)};
111 res[address] = std::string(rpcPath.Data());
112 }
113 }
114
115 return res;
116}
117
118// ---------------------------------------------------------------------------------------------------------------------
119//
121try {
122 std::unordered_map<uint32_t, std::string> collectedNodes;
123
124 // Using tag dispatching before C++20
125 auto AddNodes = [&](auto aTag) -> void {
126 constexpr ECbmModuleId ModuleId = decltype(aTag)::value;
127 if (fDetToBuildNodes.find(ModuleId) != fDetToBuildNodes.end()) {
128 LOG(info) << "GeoNodeMapBuilder: building node map for " << ModuleId;
129 collectedNodes.merge(Add<ModuleId>());
130 }
131 };
132
133 AddNodes(Tag<ECbmModuleId::kMvd>{});
134 AddNodes(Tag<ECbmModuleId::kSts>{});
135 AddNodes(Tag<ECbmModuleId::kMuch>{});
136 AddNodes(Tag<ECbmModuleId::kTrd>{});
137 AddNodes(Tag<ECbmModuleId::kTof>{});
138
139 return GeoNodeMap{std::move(collectedNodes)};
140}
141catch (const std::exception& err) {
142 LOG(fatal) << "cbm::GeoNodeMapBuilder: failed building the map. Reason: " << err.what();
143 __builtin_unreachable(); // No return is reachable
144}
ECbmModuleId
Enumerator for module Identifiers.
Definition CbmDefs.h:45
A multi-purpose class to access the geometrical information stored in a TGeoManager (source)
std::integral_constant< ECbmModuleId, ModuleId > Tag
A builder for the cbm::GeoNodeMap class (source)
Helper class to convert unique channel ID back and forth.
std::map< uint32_t, CbmMvdSensor * > & GetSensorMap()
static CbmMvdDetector * Instance()
static CbmStsSetup * Instance()
static uint32_t GetUniqueAddress(uint32_t Sm, uint32_t Rpc, uint32_t Channel, uint32_t Side=0, uint32_t SmType=0, uint32_t RpcType=0)
static uint32_t GetAddress(int32_t layerId, int32_t moduleId, int32_t sectorId, int32_t rowId, int32_t columnId)
Return address from system ID, layer, module, sector, column and row IDs.
static std::vector< TString > CollectNodes(const TString &detector, const std::regex &component, const TString &path, TGeoNode *pNode)
Collects paths of the components.
static std::unordered_map< uint32_t, std::string > Add()
Adds nodes for a given detector ID.
GeoNodeMap Build() const
Builds the geo-node map.
A builder for GeoNodeMap in CBM.
static std::unordered_map< uint32_t, std::string > Add()
Adds nodes for a given detector ID.
std::unordered_set< ECbmModuleId > fDetToBuildNodes
A list of detectors requested to build nodes.
A class to keep paths to nodes vs. hardware addresses for different detectors.