CbmRoot
Loading...
Searching...
No Matches
CbmFsdGeoHandler.cxx
Go to the documentation of this file.
1/* Copyright (C) 2023 Physikalisches Institut, Eberhard Karls Universitaet Tuebingen, Tuebingen
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Lukas Chlad [committer] */
4
5#include "CbmFsdGeoHandler.h"
6
7#include "CbmFsdAddress.h" // for CbmFsdAddress
8#include "CbmFsdDetectorSpecs.h" // for CbmFsdModuleSpecs
9
10#include <Logger.h> // for LOG, Logger
11
12#include <TGeoBBox.h> // for TGeoBBox
13#include <TGeoManager.h> // for TGeoManager, gGeoManager
14#include <TGeoMatrix.h> // for TGeoMatrix
15#include <TGeoNode.h> // for TGeoIterator, TGeoNode
16#include <TGeoVolume.h> // for TGeoVolume
17#include <TVector3.h> // for TVector3
18#include <TVirtualMC.h> // for TVirtualMC, gMC
19
20#include <string> // for operator<, stoul
21#include <utility> // for pair
22
23#include <stddef.h> // for size_t
24
25using namespace std;
26
27// constructor with initialization of maps
29
31{
32 fUnitIdToSpecsMap.clear();
33 fModuleIdToSpecsMap.clear();
34
35 Int_t currentModuleId = -1;
36 Int_t currentUnitId = -1;
37
38 TGeoIterator geoIterator(gGeoManager->GetTopNode()->GetVolume());
39
40 TGeoNode* curNode;
41 TGeoCombiTrans* unitToGlobalMatrix = nullptr;
42 geoIterator.Reset(); // safety to reset to "cave" befor the loop starts
43 while ((curNode = geoIterator())) {
44 TString nodePath;
45 geoIterator.GetPath(nodePath);
46 if (!nodePath.Contains(fBranchStr)) {
47 geoIterator.Skip(); // skip current branch when it is not FSD => should speed up
48 continue; // don't do anything for this branch
49 }
50
51 TString nodeName(curNode->GetName());
52 if (nodeName.Contains(fUnitStr)) {
53 currentUnitId = curNode->GetNumber();
54 const TGeoMatrix* curUnitMatrix = geoIterator.GetCurrentMatrix();
55 unitToGlobalMatrix = new TGeoCombiTrans(*(curUnitMatrix));
56
57 CbmFsdUnitSpecs* unitSpecs = new CbmFsdUnitSpecs();
58 unitSpecs->fUnitId = currentUnitId;
59 unitSpecs->fUnitName = static_cast<TString>(curNode->GetVolume()->GetName());
60 unitSpecs->fNumModules = curNode->GetNdaughters();
61 fUnitIdToSpecsMap.insert(pair<Int_t, CbmFsdUnitSpecs*>(currentUnitId, unitSpecs));
62 }
63 if (nodeName.Contains(fModuleStr)) {
64 currentModuleId = curNode->GetNumber();
65
66
67 TGeoMatrix* moduleToUnitMatrix = curNode->GetMatrix();
68 TVector3 localModuleCoord(moduleToUnitMatrix->GetTranslation());
69 TVector3 globalModuleCoord;
70 if (!unitToGlobalMatrix) {
71 LOG(fatal) << "No parent (unit) matrix initialized!!";
72 }
73 unitToGlobalMatrix->LocalToMaster(&localModuleCoord[0], &globalModuleCoord[0]);
74
75 TGeoVolume* curScintillator = nullptr;
76 for (int idn = 0; idn < curNode->GetNdaughters(); idn++) {
77 TGeoNode* daughterNode = curNode->GetDaughter(idn);
78 if (TString(daughterNode->GetName()).Contains(fActiveMatStr)) {
79 curScintillator = daughterNode->GetVolume();
80 break;
81 }
82 }
83 const TGeoBBox* shape = (const TGeoBBox*) (curScintillator->GetShape());
84
85 CbmFsdModuleSpecs* moduleSpecs = new CbmFsdModuleSpecs();
86 moduleSpecs->fX = globalModuleCoord[0];
87 moduleSpecs->fY = globalModuleCoord[1];
88 moduleSpecs->fZ = globalModuleCoord[2];
89 moduleSpecs->dX = shape->GetDX();
90 moduleSpecs->dY = shape->GetDY();
91 moduleSpecs->dZ = shape->GetDZ();
92 moduleSpecs->fModuleId = currentModuleId;
93 moduleSpecs->fUnitId = currentUnitId;
94 fModuleIdToSpecsMap.insert(pair<Int_t, CbmFsdModuleSpecs*>(currentModuleId, moduleSpecs));
95 }
96 }
97
98 LOG(info) << "CbmFsdGeoHandler has initialized maps";
99 LOG(info) << "fUnitIdToSpecsMap.size() = " << fUnitIdToSpecsMap.size();
100 LOG(info) << "fModuleIdToSpecsMap.size() = " << fModuleIdToSpecsMap.size();
101}
102
104{
105 std::map<Int_t, CbmFsdModuleSpecs*>::iterator it;
106 it = fModuleIdToSpecsMap.find(modId);
107 if (it == fModuleIdToSpecsMap.end()) return nullptr;
108 return it->second;
109}
110
112{
113 std::map<Int_t, CbmFsdUnitSpecs*>::iterator it;
114 it = fUnitIdToSpecsMap.find(unitId);
115 if (it == fUnitIdToSpecsMap.end()) return nullptr;
116 return it->second;
117}
118
119int32_t CbmFsdGeoHandler::GetAddress(TString geoPath) const
120{
121 Int_t moduleID = GetCopyNumberByKey(geoPath, fModuleStr);
122 Int_t unitID = GetCopyNumberByKey(geoPath, fUnitStr);
123
124 return CbmFsdAddress::GetAddress(unitID, moduleID);
125}
126
127int32_t CbmFsdGeoHandler::GetCurrentAddress(TVirtualMC* vmc) const
128{
129 Int_t moduleID = -1;
130 Int_t unitID = -1;
131
132 Int_t upstreamOffset = 0;
133 while (((TString) vmc->CurrentVolOffName(upstreamOffset)).Length() > 0) {
134 if (((TString) vmc->CurrentVolOffName(upstreamOffset)).Contains(fUnitStr)) {
135 vmc->CurrentVolOffID(upstreamOffset, unitID);
136 }
137 if (((TString) vmc->CurrentVolOffName(upstreamOffset)).Contains(fModuleStr)) {
138 vmc->CurrentVolOffID(upstreamOffset, moduleID);
139 }
140 // if module and unit information was found one can
141 // exit the loop
142 if (moduleID > -1 && unitID > -1) { break; }
143 upstreamOffset++;
144 }
145
146 return CbmFsdAddress::GetAddress(unitID, moduleID);
147}
148
149int32_t CbmFsdGeoHandler::GetCurrentAddress(TGeoManager* geoMan) const
150{
151 Int_t moduleID = -1;
152 Int_t unitID = -1;
153
154 Int_t upstreamOffset = 0;
155 while (upstreamOffset <= geoMan->GetLevel()) {
156 TGeoNode* node = geoMan->GetMother(upstreamOffset);
157 if (((TString) node->GetVolume()->GetName()).Contains(fUnitStr)) unitID = node->GetNumber();
158 if (((TString) node->GetVolume()->GetName()).Contains(fModuleStr)) moduleID = node->GetNumber();
159 upstreamOffset++;
160 }
161
162 return CbmFsdAddress::GetAddress(unitID, moduleID);
163}
164
165Int_t CbmFsdGeoHandler::GetCopyNumberByKey(TString geoPath, TString key) const
166{
167 Int_t copyNum = -1;
168
169 // sanity checks
170 if (!geoPath.Contains(fBranchStr)) { LOG(warning) << __func__ << ": In geoPath " << fBranchStr << " was not found!"; }
171 else if (!geoPath.Contains(key)) {
172 LOG(warning) << __func__ << ": In geoPath " << key << " was not found!";
173 }
174 else {
175 Ssiz_t keyStart = geoPath.Index(key);
176 TString keyName = geoPath(keyStart, geoPath.Index("/", keyStart) - keyStart);
177 Ssiz_t copyNumStart = keyName.Last('_') + 1;
178 TString copyNumStr = keyName(copyNumStart, keyName.Length() - copyNumStart);
179 if (!copyNumStr.IsDigit()) {
180 LOG(warning) << __func__ << ": Expected numerical part from " << geoPath << " using key " << key << " is "
181 << copyNumStr << " which does not contain only digits!";
182 }
183 else {
184 copyNum = copyNumStr.Atoi();
185 }
186 }
187
188 return copyNum;
189}
Int_t GetCopyNumberByKey(TString geoPath, TString key) const
Helper function to extract copy number from geoPath using key word.
const TString fUnitStr
std::map< Int_t, CbmFsdModuleSpecs * > fModuleIdToSpecsMap
std::map< Int_t, CbmFsdUnitSpecs * > fUnitIdToSpecsMap
const TString fActiveMatStr
CbmFsdUnitSpecs * GetUnitSpecsById(Int_t id)
int32_t GetAddress(TString geoPath) const
Get the unique address from geometry path string.
const TString fBranchStr
CbmFsdModuleSpecs * GetModuleSpecsById(Int_t id)
int32_t GetCurrentAddress(TVirtualMC *vmc) const
Get the unique address from TVirtualMC.
const TString fModuleStr
int32_t GetAddress(uint32_t Unit=0, uint32_t Module=0, uint32_t PhotoDet=0, uint32_t Version=kCurrentVersion)
Construct address.
Hash for CbmL1LinkKey.