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