Repository navigation
Expand file tree
/
Copy pathGeometryTGeo.cxx
More file actions
369 lines (307 loc) · 11.3 KB
/
Copy pathGeometryTGeo.cxx
File metadata and controls
369 lines (307 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#include <IOTOFBase/GeometryTGeo.h>
#include <IOTOFBase/IOTOFBaseParam.h>
#include <TGeoManager.h>
#include <TMath.h>
namespace o2
{
namespace iotof
{
std::unique_ptr<o2::iotof::GeometryTGeo> GeometryTGeo::sInstance;
// Common i/oTOF
std::string GeometryTGeo::sIOTOFVolumeName = "IOTOFV";
// Inner TOF
std::string GeometryTGeo::sITOFLayerName = "ITOFLayer";
std::string GeometryTGeo::sITOFStaveName = "ITOFStave";
std::string GeometryTGeo::sITOFModuleName = "ITOFModule";
std::string GeometryTGeo::sITOFChipName = "ITOFChip";
std::string GeometryTGeo::sITOFSensorName = "ITOFSensor";
// Outer TOF
std::string GeometryTGeo::sOTOFLayerName = "OTOFLayer";
std::string GeometryTGeo::sOTOFStaveName = "OTOFStave";
std::string GeometryTGeo::sOTOFModuleName = "OTOFModule";
std::string GeometryTGeo::sOTOFChipName = "OTOFChip";
std::string GeometryTGeo::sOTOFSensorName = "OTOFSensor";
// Forward TOF
std::string GeometryTGeo::sFTOFLayerName = "FTOFLayer";
std::string GeometryTGeo::sFTOFChipName = "FTOFChip";
std::string GeometryTGeo::sFTOFSensorName = "FTOFSensor";
// Backward TOF
std::string GeometryTGeo::sBTOFLayerName = "BTOFLayer";
std::string GeometryTGeo::sBTOFChipName = "BTOFChip";
std::string GeometryTGeo::sBTOFSensorName = "BTOFSensor";
GeometryTGeo::GeometryTGeo(bool build, int loadTrans) : DetMatrixCache()
{
if (sInstance) {
LOGP(fatal, "Invalid use of public constructor: o2::iotof::GeometryTGeo instance exists");
}
if (build) {
Build(loadTrans);
}
}
int GeometryTGeo::extractNumberOfStavesIOTOF(int lay) const
{
int numberOfStaves{0};
std::string layName = lay == 0 ? GeometryTGeo::getITOFLayerPattern() : GeometryTGeo::getOTOFLayerPattern();
TGeoVolume* layV = gGeoManager->GetVolume(layName.c_str());
if (layV == nullptr) {
LOG(fatal) << "Can't find volume " << layName;
return -1;
}
TObjArray* nodes = layV->GetNodes();
int nNodes = nodes->GetEntriesFast();
for (int j{0}; j < nNodes; ++j) {
if (strstr(nodes->At(j)->GetName(), lay == 0 ? GeometryTGeo::getITOFStavePattern() : GeometryTGeo::getOTOFStavePattern()) != nullptr) {
numberOfStaves++;
}
}
return numberOfStaves;
}
int GeometryTGeo::extractNumberOfModulesIOTOF(int lay) const
{
int numberOfModules{0};
std::string staveName = lay == 0 ? GeometryTGeo::getITOFStavePattern() : GeometryTGeo::getOTOFStavePattern();
TGeoVolume* staveV = gGeoManager->GetVolume(staveName.c_str());
if (staveV == nullptr) {
LOG(fatal) << "Can't find volume " << staveName;
return -1;
}
TObjArray* nodes = staveV->GetNodes();
int nNodes = nodes->GetEntriesFast();
for (int j{0}; j < nNodes; ++j) {
if (strstr(nodes->At(j)->GetName(), lay == 0 ? GeometryTGeo::getITOFModulePattern() : GeometryTGeo::getOTOFModulePattern()) != nullptr) {
numberOfModules++;
}
}
return numberOfModules;
}
int GeometryTGeo::extractNumberOfChipsPerModuleIOTOF(int lay) const
{
int numberOfChips{0};
std::string moduleName = lay == 0 ? GeometryTGeo::getITOFModulePattern() : GeometryTGeo::getOTOFModulePattern();
TGeoVolume* moduleV = gGeoManager->GetVolume(moduleName.c_str());
if (moduleV == nullptr) {
LOG(fatal) << "Can't find volume " << moduleName;
return -1;
}
TObjArray* nodes = moduleV->GetNodes();
int nNodes = nodes->GetEntriesFast();
for (int j{0}; j < nNodes; ++j) {
if (strstr(nodes->At(j)->GetName(), lay == 0 ? GeometryTGeo::getITOFChipPattern() : GeometryTGeo::getOTOFChipPattern()) != nullptr) {
numberOfChips++;
}
}
return numberOfChips;
}
int GeometryTGeo::extractNumberOfChipsFTOF() const
{
return 0;
}
int GeometryTGeo::extractNumberOfChipsBTOF() const
{
return 0;
}
int GeometryTGeo::getIOTOFFirstChipIndex(int lay) const
{
return lay == 0 ? 0 : mLastChipIndex[0] + 1;
}
int GeometryTGeo::getIOTOFLayer(int index) const
{
if (index < 0 || index > mLastChipIndex[1]) {
LOG(fatal) << "Invalid chip index " << index;
return -1;
}
return index > mLastChipIndex[0] ? 1 : 0;
}
int GeometryTGeo::getIOTOFChipIndex(int lay, int sta, int mod, int chip) const
{
return getIOTOFFirstChipIndex(lay) + (sta - 1) * mNumberOfChipsPerStaveIOTOF[lay] + (mod - 1) * mNumberOfChipsPerModuleIOTOF[lay] + (chip - 1);
}
bool GeometryTGeo::getIOTOFChipId(int index, int& lay, int& sta, int& mod, int& chip) const
{
lay = getIOTOFLayer(index);
index -= getIOTOFFirstChipIndex(lay);
sta = mNumberOfStavesIOTOF[lay] > 0 ? index / mNumberOfChipsPerStaveIOTOF[lay] : -1;
index %= mNumberOfChipsPerStaveIOTOF[lay];
mod = mNumberOfModulesIOTOF[lay] > 0 ? index / mNumberOfChipsPerModuleIOTOF[lay] : -1;
chip = index % mNumberOfChipsPerModuleIOTOF[lay];
return true;
}
TString GeometryTGeo::getMatrixPath(int index) const
{
int lay, sta, mod, chip;
getIOTOFChipId(index, lay, sta, mod, chip);
TString path = Form("/cave_1/barrel_1/%s_2/", GeometryTGeo::getIOTOFVolPattern());
sta += 1;
mod += 1;
chip += 1;
if (lay == 0) {
path += Form("%s_1/", GeometryTGeo::getITOFLayerPattern());
if (mNumberOfStavesIOTOF[lay] > 0)
path += Form("%s_%d/", GeometryTGeo::getITOFStavePattern(), sta);
if (mNumberOfModulesIOTOF[lay] > 0)
path += Form("%s_%d/", GeometryTGeo::getITOFModulePattern(), mod);
if (mNumberOfChipsPerModuleIOTOF[lay] > 0)
path += Form("%s_%d/%s_1", GeometryTGeo::getITOFChipPattern(), chip, GeometryTGeo::getITOFSensorPattern());
} else {
path += Form("%s_1/", GeometryTGeo::getOTOFLayerPattern());
if (mNumberOfStavesIOTOF[lay] > 0)
path += Form("%s_%d/", GeometryTGeo::getOTOFStavePattern(), sta);
if (mNumberOfModulesIOTOF[lay] > 0)
path += Form("%s_%d/", GeometryTGeo::getOTOFModulePattern(), mod);
if (mNumberOfChipsPerModuleIOTOF[lay] > 0)
path += Form("%s_%d/%s_1", GeometryTGeo::getOTOFChipPattern(), chip, GeometryTGeo::getOTOFSensorPattern());
}
return path;
}
TGeoHMatrix* GeometryTGeo::extractMatrixSensor(int index) const
{
auto path = getMatrixPath(index);
static TGeoHMatrix matTmp;
gGeoManager->PushPath();
if (!gGeoManager->cd(path.Data())) {
gGeoManager->PopPath();
LOG(error) << "Error in cd-ing to " << path.Data();
return nullptr;
}
matTmp = *gGeoManager->GetCurrentMatrix();
// LOG(info) << "Path = " << path.Data();
// Restore the modeler state
gGeoManager->PopPath();
// account for the difference between physical sensitive layer (where charge collection is simulated) and effective sensor thicknesses
// TODO: apply translation by the effective sensor thickness, not yet done (see ITS)
return &matTmp;
}
void GeometryTGeo::Build(int loadTrans)
{
if (isBuilt()) {
LOGP(warning, "Already built");
return; // already initialized
}
if (!gGeoManager) {
LOGP(fatal, "Geometry is not loaded");
}
auto& iotofPars = IOTOFBaseParam::Instance();
if (!iotofPars.segmentedInnerTOF && !iotofPars.segmentedOuterTOF) {
return;
}
// Inner/outer TOF
for (int j{0}; j < 2; ++j) {
mNumberOfStavesIOTOF[j] = extractNumberOfStavesIOTOF(j);
mNumberOfModulesIOTOF[j] = extractNumberOfModulesIOTOF(j);
mNumberOfChipsPerModuleIOTOF[j] = extractNumberOfChipsPerModuleIOTOF(j);
}
// Forward TOF
mNumberOfChipsFTOF = extractNumberOfChipsFTOF();
// Backward TOF
mNumberOfChipsBTOF = extractNumberOfChipsBTOF();
int numberOfChips{0};
for (int j{0}; j < 2; ++j) {
mNumberOfChipsPerStaveIOTOF[j] = mNumberOfModulesIOTOF[j] * mNumberOfChipsPerModuleIOTOF[j];
mNumberOfChipsIOTOF[j] = mNumberOfStavesIOTOF[j] * mNumberOfChipsPerStaveIOTOF[j];
numberOfChips += mNumberOfChipsIOTOF[j];
mLastChipIndex[j] = numberOfChips - 1;
}
LOG(info) << "numberOfChipsITOF = " << mNumberOfChipsIOTOF[0] << ", numberOfChipsOTOF = " << mNumberOfChipsIOTOF[1] << ", numberOfChips = " << numberOfChips << ", mNumberOfChipesPerStaveITOF" << mNumberOfChipsPerStaveIOTOF[0];
setSize(numberOfChips);
defineSensors();
fillTrackingFramesCache();
fillMatrixCache(loadTrans);
// fillMatrixCache(o2::math_utils::bit2Mask(o2::math_utils::TransformType::L2G));
}
void GeometryTGeo::defineSensors()
{
for (int i = 0; i < mSize; i++) {
sensors.push_back(i);
}
}
void GeometryTGeo::fillTrackingFramesCache()
{
// fill for every sensor of IOTOF its tracking frame parameters
if (!isTrackingFrameCached() && !sensors.empty()) {
size_t newSize = sensors.size();
mCacheRefX.resize(newSize);
mCacheRefAlpha.resize(newSize);
for (int i = 0; i < newSize; i++) {
int sensorId = sensors[i];
extractSensorXAlpha(sensorId, mCacheRefX[i], mCacheRefAlpha[i]);
}
}
}
void GeometryTGeo::fillMatrixCache(int mask)
{
if (mSize < 1) {
LOG(warning) << "The method Build was not called yet";
Build(mask);
return;
}
LOG(debug) << "Filling matrix cache for " << getName() << " with mask " << mask;
if ((mask & o2::math_utils::bit2Mask(o2::math_utils::TransformType::L2G)) && !getCacheL2G().isFilled()) {
// Matrices for Local (Sensor!!! rather than the full chip) to Global frame transformation
LOG(info) << "Loading " << getName() << " L2G matrices from TGeo; there are " << mSize << " matrices";
auto& cacheL2G = getCacheL2G();
cacheL2G.setSize(mSize);
for (int i = 0; i < mSize; i++) {
TGeoHMatrix* hm = extractMatrixSensor(i);
cacheL2G.setMatrix(o2::math_utils::Transform3D(*hm), i);
}
}
// build T2L matrices for IOTOF
if ((mask & o2::math_utils::bit2Mask(o2::math_utils::TransformType::T2L)) && !getCacheT2L().isFilled()) {
LOGP(info, "Loading {} T2L matrices from TGeo for IOTOF", getName());
if (sensors.size()) {
int m_Size = sensors.size();
auto& cacheT2L = getCacheT2L();
cacheT2L.setSize(m_Size);
for (int i = 0; i < m_Size; i++) {
int sensorID = sensors[i];
TGeoHMatrix& hm = createT2LMatrix(sensorID);
cacheT2L.setMatrix(Mat3D(hm), i);
}
}
}
}
void GeometryTGeo::extractSensorXAlpha(int chipID, float& x, float& alp)
{
double locA[3] = {-100., 0., 0.}, locB[3] = {100., 0., 0.}, gloA[3], gloB[3];
double xp{0}, yp{0};
const TGeoHMatrix* matL2G = extractMatrixSensor(chipID);
matL2G->LocalToMaster(locA, gloA);
matL2G->LocalToMaster(locB, gloB);
double dx = gloB[0] - gloA[0], dy = gloB[1] - gloA[1];
double t = (gloB[0] * dx + gloB[1] * dy) / (dx * dx + dy * dy);
xp = gloB[0] - dx * t;
yp = gloB[1] - dy * t;
alp = std::atan2(yp, xp);
x = std::hypot(xp, yp);
o2::math_utils::bringTo02Pi(alp);
}
TGeoHMatrix& GeometryTGeo::createT2LMatrix(int chipID)
{
static TGeoHMatrix t2l;
t2l.Clear();
float alpha = getSensorRefAlpha(chipID);
t2l.RotateZ(alpha * TMath::RadToDeg());
const TGeoHMatrix* matL2G = extractMatrixSensor(chipID);
const TGeoHMatrix& matL2Gi = matL2G->Inverse();
t2l.MultiplyLeft(&matL2Gi);
return t2l;
}
GeometryTGeo* GeometryTGeo::Instance()
{
if (!sInstance) {
sInstance = std::unique_ptr<GeometryTGeo>(new GeometryTGeo(true, 0));
}
return sInstance.get();
}
} // namespace iotof
} // namespace o2