forked from mcoquet642/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeometryManager.cxx
More file actions
503 lines (450 loc) · 16.3 KB
/
GeometryManager.cxx
File metadata and controls
503 lines (450 loc) · 16.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
// 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.
/// \file GeometryManager.cxx
/// \brief Implementation of the GeometryManager class
#include <FairLogger.h> // for LOG
#include <TCollection.h> // for TIter
#include <TFile.h>
#include <TGeoMatrix.h> // for TGeoHMatrix
#include <TGeoNode.h> // for TGeoNode
#include <TGeoPhysicalNode.h> // for TGeoPhysicalNode, TGeoPNEntry
#include <string>
#include <cassert>
#include <cstddef> // for NULL
#include <numeric>
#include "DetectorsBase/GeometryManager.h"
#include "DetectorsCommonDataFormats/AlignParam.h"
#include "DetectorsCommonDataFormats/NameConf.h"
#include "DetectorsBase/Aligner.h"
using namespace o2::detectors;
using namespace o2::base;
/// Implementation of GeometryManager, the geometry manager class which interfaces to TGeo and
/// the look-up table mapping unique volume indices to symbolic volume names. For that, it
/// collects several static methods
std::mutex GeometryManager::sTGMutex;
//______________________________________________________________________
Bool_t GeometryManager::getOriginalMatrix(const char* symname, TGeoHMatrix& m)
{
m.Clear();
if (!gGeoManager || !gGeoManager->IsClosed()) {
LOG(ERROR) << "No active geometry or geometry not yet closed!";
;
return kFALSE;
}
std::lock_guard<std::mutex> guard(sTGMutex);
if (!gGeoManager->GetListOfPhysicalNodes()) {
LOG(WARNING) << "gGeoManager doesn't contain any aligned nodes!";
if (!gGeoManager->cd(symname)) {
LOG(ERROR) << "Volume path " << symname << " not valid!";
return kFALSE;
} else {
m = *gGeoManager->GetCurrentMatrix();
return kTRUE;
}
}
TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
const char* path = nullptr;
if (pne) {
m = *pne->GetGlobalOrig();
return kTRUE;
} else {
LOG(WARNING) << "The symbolic volume name " << symname
<< "does not correspond to a physical entry. Using it as a volume path!";
path = symname;
}
return getOriginalMatrixFromPath(path, m);
}
//______________________________________________________________________
Bool_t GeometryManager::getOriginalMatrixFromPath(const char* path, TGeoHMatrix& m)
{
m.Clear();
if (!gGeoManager || !gGeoManager->IsClosed()) {
LOG(ERROR) << "Can't get the original global matrix! gGeoManager doesn't exist or it is still opened!";
return kFALSE;
}
std::lock_guard<std::mutex> guard(sTGMutex);
if (!gGeoManager->CheckPath(path)) {
LOG(ERROR) << "Volume path " << path << " not valid!";
return kFALSE;
}
TIter next(gGeoManager->GetListOfPhysicalNodes());
gGeoManager->cd(path);
while (gGeoManager->GetLevel()) {
TGeoPhysicalNode* physNode = nullptr;
next.Reset();
TGeoNode* node = gGeoManager->GetCurrentNode();
while ((physNode = (TGeoPhysicalNode*)next())) {
if (physNode->GetNode() == node) {
break;
}
}
TGeoMatrix* lm = nullptr;
if (physNode) {
lm = physNode->GetOriginalMatrix();
if (!lm) {
lm = node->GetMatrix();
}
} else {
lm = node->GetMatrix();
}
m.MultiplyLeft(lm);
gGeoManager->CdUp();
}
return kTRUE;
}
//______________________________________________________________________
TGeoHMatrix* GeometryManager::getMatrix(TGeoPNEntry* pne)
{
// Get the global transformation matrix for a given PNEntry
// by quering the TGeoManager
if (!gGeoManager || !gGeoManager->IsClosed()) {
LOG(ERROR) << "Can't get the global matrix! gGeoManager doesn't exist or it is still opened!";
return nullptr;
}
// if matrix already known --> return it
TGeoPhysicalNode* pnode = pne->GetPhysicalNode();
if (pnode) {
return pnode->GetMatrix();
}
// otherwise calculate it from title and attach via TGeoPhysicalNode
pne->SetPhysicalNode(new TGeoPhysicalNode(pne->GetTitle()));
return pne->GetPhysicalNode()->GetMatrix();
}
//______________________________________________________________________
TGeoHMatrix* GeometryManager::getMatrix(const char* symname)
{
// Get the global transformation matrix for a given alignable volume
// identified by its symbolic name 'symname' by quering the TGeoManager
if (!gGeoManager || !gGeoManager->IsClosed()) {
LOG(ERROR) << "No active geometry or geometry not yet closed!";
return nullptr;
}
TGeoPNEntry* pne = gGeoManager->GetAlignableEntry(symname);
if (!pne) {
return nullptr;
}
return getMatrix(pne);
}
//______________________________________________________________________
const char* GeometryManager::getSymbolicName(DetID detid, int sensid)
{
/**
* Get symoblic name of sensitive volume sensid of detector detid
**/
int id = getSensID(detid, sensid);
TGeoPNEntry* pne = gGeoManager->GetAlignableEntryByUID(id);
if (!pne) {
LOG(ERROR) << "Failed to find alignable entry with index " << id << ": Det" << detid << " Sens.Vol:" << sensid << ") !";
return nullptr;
}
return pne->GetName();
}
TGeoPNEntry* GeometryManager::getPNEntry(DetID detid, Int_t sensid)
{
/**
* Get PN Entry of sensitive volume sensid of detector detid
**/
int id = getSensID(detid, sensid);
TGeoPNEntry* pne = gGeoManager->GetAlignableEntryByUID(id);
if (!pne) {
LOG(ERROR) << "The sens.vol " << sensid << " of det " << detid << " does not correspond to a physical entry!";
}
return pne;
}
//______________________________________________________________________
TGeoHMatrix* GeometryManager::getMatrix(DetID detid, Int_t sensid)
{
/**
* Get position matrix (aligned) of sensitive volume sensid of detector detid. Slow
**/
static TGeoHMatrix matTmp;
TGeoPNEntry* pne = getPNEntry(detid, sensid);
if (!pne) {
return nullptr;
}
TGeoPhysicalNode* pnode = pne->GetPhysicalNode();
if (pnode) {
return pnode->GetMatrix();
}
const char* path = pne->GetTitle();
gGeoManager->PushPath(); // Preserve the modeler state.
if (!gGeoManager->cd(path)) {
gGeoManager->PopPath();
LOG(ERROR) << "Volume path " << path << " not valid!";
return nullptr;
}
matTmp = *gGeoManager->GetCurrentMatrix();
gGeoManager->PopPath();
return &matTmp;
}
//______________________________________________________________________
Bool_t GeometryManager::getOriginalMatrix(DetID detid, int sensid, TGeoHMatrix& m)
{
/**
* Get position matrix (original) of sensitive volume sensid of detector detid. Slow
**/
m.Clear();
const char* symname = getSymbolicName(detid, sensid);
if (!symname) {
return kFALSE;
}
return getOriginalMatrix(symname, m);
}
//______________________________________________________________________
bool GeometryManager::applyAlignment(const std::vector<const std::vector<o2::detectors::AlignParam>*> algPars)
{
/// misalign geometry with alignment objects from the array, optionaly check overlaps
for (auto dv : algPars) {
if (dv && !applyAlignment(*dv)) {
return false;
}
}
return true;
}
//______________________________________________________________________
bool GeometryManager::applyAlignment(const std::vector<o2::detectors::AlignParam>& algPars)
{
/// misalign geometry with alignment objects from the array, optionaly check overlaps
int nvols = algPars.size();
std::vector<int> ord(nvols);
std::iota(std::begin(ord), std::end(ord), 0); // sort to apply alignment in correct hierarchy
std::sort(std::begin(ord), std::end(ord), [&algPars](int a, int b) { return algPars[a].getLevel() > algPars[b].getLevel(); });
bool res = true;
for (int i = 0; i < nvols; i++) {
if (!algPars[ord[i]].applyToGeometry()) {
res = false;
LOG(ERROR) << "Error applying alignment object for volume" << algPars[ord[i]].getSymName();
}
}
return res;
}
// ================= methods for nested MatBudgetExt class ================
//______________________________________________________________________
void GeometryManager::MatBudgetExt::normalize(double step)
{
double nrm = 1. / step;
meanRho *= nrm;
meanA *= nrm;
meanZ *= nrm;
meanZ2A *= nrm;
if (nrm > 0.) {
length = step;
}
}
//______________________________________________________________________
void GeometryManager::accountMaterial(const TGeoMaterial* material, GeometryManager::MatBudgetExt& bd)
{
bd.meanRho = material->GetDensity();
bd.meanX2X0 = material->GetRadLen();
bd.meanA = material->GetA();
bd.meanZ = material->GetZ();
if (material->IsMixture()) {
TGeoMixture* mixture = (TGeoMixture*)material;
Double_t norm = 0.;
bd.meanZ2A = 0.;
for (Int_t iel = 0; iel < mixture->GetNelements(); iel++) {
norm += mixture->GetWmixt()[iel];
bd.meanZ2A += mixture->GetZmixt()[iel] * mixture->GetWmixt()[iel] / mixture->GetAmixt()[iel];
}
bd.meanZ2A /= norm;
} else {
bd.meanZ2A = bd.meanZ / bd.meanA;
}
}
//_____________________________________________________________________________________
GeometryManager::MatBudgetExt GeometryManager::meanMaterialBudgetExt(float x0, float y0, float z0, float x1, float y1, float z1)
{
//
// TODO? It seems there is no real nead for extended material budget, consider eliminating it
//
// Calculate mean material budget and material properties (extended version) between
// the points "0" and "1".
//
// see MatBudgetExt data members for provided information
//
// Origin: Marian Ivanov, Marian.Ivanov@cern.ch
//
// Corrections and improvements by
// Andrea Dainese, Andrea.Dainese@lnl.infn.it,
// Andrei Gheata, Andrei.Gheata@cern.ch
//
// Ported to O2: ruben.shahoyan@cern.ch
//
double length, startD[3] = {x0, y0, z0};
double dir[3] = {x1 - x0, y1 - y0, z1 - z0};
if ((length = dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]) < TGeoShape::Tolerance() * TGeoShape::Tolerance()) {
return MatBudgetExt(); // return empty struct
}
length = TMath::Sqrt(length);
double invlen = 1. / length;
for (int i = 3; i--;) {
dir[i] *= invlen;
}
std::lock_guard<std::mutex> guard(sTGMutex);
// Initialize start point and direction
TGeoNode* currentnode = gGeoManager->InitTrack(startD, dir);
if (!currentnode) {
LOG(ERROR) << "start point out of geometry: " << x0 << ':' << y0 << ':' << z0;
return MatBudgetExt(); // return empty struct
}
MatBudgetExt budTotal, budStep;
accountMaterial(currentnode->GetVolume()->GetMedium()->GetMaterial(), budStep);
budStep.length = length;
// Locate next boundary within length without computing safety.
// Propagate either with length (if no boundary found) or just cross boundary
gGeoManager->FindNextBoundaryAndStep(length, kFALSE);
Double_t stepTot = 0.0; // Step made
Double_t step = gGeoManager->GetStep();
// If no boundary within proposed length, return current step data
if (!gGeoManager->IsOnBoundary()) {
budStep.meanX2X0 = budStep.length / budStep.meanX2X0;
return MatBudgetExt(budStep);
}
// Try to cross the boundary and see what is next
Int_t nzero = 0;
while (length > TGeoShape::Tolerance()) {
if (step < 2. * TGeoShape::Tolerance()) {
nzero++;
} else {
nzero = 0;
}
if (nzero > 3) {
// This means navigation has problems on one boundary
// Try to cross by making a small step
const double* curPos = gGeoManager->GetCurrentPoint();
LOG(warning) << "Cannot cross boundary at (" << curPos[0] << ',' << curPos[1] << ',' << curPos[2] << ')';
budTotal.normalize(stepTot);
budTotal.nCross = -1; // flag failed navigation
return MatBudgetExt(budTotal);
}
stepTot += step;
budTotal.meanRho += step * budStep.meanRho;
budTotal.meanX2X0 += step / budStep.meanX2X0;
budTotal.meanA += step * budStep.meanA;
budTotal.meanZ += step * budStep.meanZ;
budTotal.meanZ2A += step * budStep.meanZ2A;
budTotal.nCross++;
if (step >= length) {
break;
}
currentnode = gGeoManager->GetCurrentNode();
if (!currentnode) {
break;
}
length -= step;
accountMaterial(currentnode->GetVolume()->GetMedium()->GetMaterial(), budStep);
gGeoManager->FindNextBoundaryAndStep(length, kFALSE);
step = gGeoManager->GetStep();
}
budTotal.normalize(stepTot);
return MatBudgetExt(budTotal);
}
//_____________________________________________________________________________________
o2::base::MatBudget GeometryManager::meanMaterialBudget(float x0, float y0, float z0, float x1, float y1, float z1)
{
//
// Calculate mean material budget and material properties between
// the points "0" and "1".
//
// see MatBudget data members for provided information
//
// Origin: Marian Ivanov, Marian.Ivanov@cern.ch
//
// Corrections and improvements by
// Andrea Dainese, Andrea.Dainese@lnl.infn.it,
// Andrei Gheata, Andrei.Gheata@cern.ch
//
// Ported to O2: ruben.shahoyan@cern.ch
//
double length, startD[3] = {x0, y0, z0};
double dir[3] = {x1 - x0, y1 - y0, z1 - z0};
if ((length = dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]) < TGeoShape::Tolerance() * TGeoShape::Tolerance()) {
return o2::base::MatBudget(); // return empty struct
}
length = TMath::Sqrt(length);
double invlen = 1. / length;
for (int i = 3; i--;) {
dir[i] *= invlen;
}
std::lock_guard<std::mutex> guard(sTGMutex);
// Initialize start point and direction
TGeoNode* currentnode = gGeoManager->InitTrack(startD, dir);
if (!currentnode) {
LOG(ERROR) << "start point out of geometry: " << x0 << ':' << y0 << ':' << z0;
return o2::base::MatBudget(); // return empty struct
}
o2::base::MatBudget budTotal, budStep;
accountMaterial(currentnode->GetVolume()->GetMedium()->GetMaterial(), budStep);
budStep.length = length;
// Locate next boundary within length without computing safety.
// Propagate either with length (if no boundary found) or just cross boundary
gGeoManager->FindNextBoundaryAndStep(length, kFALSE);
Double_t stepTot = 0.0; // Step made
Double_t step = gGeoManager->GetStep();
// If no boundary within proposed length, return current step data
if (!gGeoManager->IsOnBoundary()) {
budStep.meanX2X0 = budStep.length / budStep.meanX2X0;
return o2::base::MatBudget(budStep);
}
// Try to cross the boundary and see what is next
Int_t nzero = 0;
while (length > TGeoShape::Tolerance()) {
if (step < 2. * TGeoShape::Tolerance()) {
nzero++;
} else {
nzero = 0;
}
if (nzero > 3) {
// This means navigation has problems on one boundary
// Try to cross by making a small step
const double* curPos = gGeoManager->GetCurrentPoint();
LOG(warning) << "Cannot cross boundary at (" << curPos[0] << ',' << curPos[1] << ',' << curPos[2] << ')';
budTotal.meanRho /= stepTot;
budTotal.length = stepTot;
return o2::base::MatBudget(budTotal);
}
stepTot += step;
budTotal.meanRho += step * budStep.meanRho;
budTotal.meanX2X0 += step / budStep.meanX2X0;
if (step >= length) {
break;
}
currentnode = gGeoManager->GetCurrentNode();
if (!currentnode) {
break;
}
length -= step;
accountMaterial(currentnode->GetVolume()->GetMedium()->GetMaterial(), budStep);
gGeoManager->FindNextBoundaryAndStep(length, kFALSE);
step = gGeoManager->GetStep();
}
budTotal.meanRho /= stepTot;
budTotal.length = stepTot;
return o2::base::MatBudget(budTotal);
}
//_________________________________
void GeometryManager::loadGeometry(std::string_view geomFileName, bool applyMisalignment)
{
///< load geometry from file
std::string fname = o2::base::NameConf::getGeomFileName(geomFileName);
LOG(INFO) << "Loading geometry " << o2::base::NameConf::GEOMOBJECTNAME << " from " << fname;
TFile flGeom(fname.data());
if (flGeom.IsZombie()) {
LOG(FATAL) << "Failed to open file " << fname;
}
if (!flGeom.Get(std::string(o2::base::NameConf::GEOMOBJECTNAME).c_str())) {
LOG(FATAL) << "Did not find geometry named " << o2::base::NameConf::GEOMOBJECTNAME;
}
if (applyMisalignment) {
auto& aligner = Aligner::Instance();
aligner.applyAlignment();
}
}