EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TGeoParser.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TGeoParser.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2020 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
10 
13 
14 #include <iostream>
15 
16 #include "TGeoBBox.h"
17 #include "TGeoNode.h"
18 #include "TGeoVolume.h"
19 
22  const TGeoMatrix& gmatrix) {
23  // Volume is present
24  if (state.volume != nullptr) {
25  std::string volumeName = state.volume->GetName();
26  // If you are on branch, you stay on branch
27  state.onBranch =
28  state.onBranch or
29  TGeoPrimitivesHelper::match(options.volumeNames, volumeName.c_str());
30  // Loop over the daughters and collect them
31  auto daugthers = state.volume->GetNodes();
32  // Daughter node iteration
33  TIter iObj(daugthers);
34  while (TObject* obj = iObj()) {
35  TGeoNode* node = dynamic_cast<TGeoNode*>(obj);
36  if (node != nullptr) {
37  state.volume = nullptr;
38  state.node = node;
39  select(state, options, gmatrix);
40  }
41  }
42  } else if (state.node != nullptr) {
43  // The node name for checking
44  std::string nodeName = state.node->GetName();
45  std::string nodeVolName = state.node->GetVolume()->GetName();
46  // Get the matrix of the current node for positioning
47  const TGeoMatrix* nmatrix = state.node->GetMatrix();
48  TGeoHMatrix transform = TGeoCombiTrans(gmatrix) * TGeoCombiTrans(*nmatrix);
49  std::string suffix = "_transform";
50  transform.SetName((nodeName + suffix).c_str());
51  // Check if you had found the target node
52  if (state.onBranch and
53  TGeoPrimitivesHelper::match(options.targetNames, nodeVolName.c_str())) {
54  // Get the placement and orientation in respect to its mother
55  const Double_t* rotation = transform.GetRotationMatrix();
56  const Double_t* translation = transform.GetTranslation();
57 
58  // Create a eigen transform
59  Vector3D t(options.unit * translation[0], options.unit * translation[1],
60  options.unit * translation[2]);
61  Vector3D cx(rotation[0], rotation[3], rotation[6]);
62  Vector3D cy(rotation[1], rotation[4], rotation[7]);
63  Vector3D cz(rotation[2], rotation[5], rotation[8]);
64  auto etrf = TGeoPrimitivesHelper::makeTransform(cx, cy, cz, t);
65 
66  bool accept = true;
67  if (not options.parseRanges.empty()) {
68  auto shape =
69  dynamic_cast<TGeoBBox*>(state.node->GetVolume()->GetShape());
70  // It uses the bounding box of TGeoBBox
71  // @TODO this should be replace by a proper TGeo to Acts::VolumeBounds
72  // and vertices converision which would make a more appropriate parsomg
73  double dx = options.unit * shape->GetDX();
74  double dy = options.unit * shape->GetDY();
75  double dz = options.unit * shape->GetDZ();
76  for (auto x : std::vector<double>{-dx, dx}) {
77  for (auto y : std::vector<double>{-dy, dy}) {
78  for (auto z : std::vector<double>{-dz, dz}) {
79  Vector3D edge = etrf * Vector3D(x, y, z);
80  for (auto& check : options.parseRanges) {
81  double val = VectorHelpers::cast(edge, check.first);
82  if (val < check.second.first or val > check.second.second) {
83  accept = false;
84  break;
85  }
86  }
87  }
88  }
89  }
90  }
91  if (accept) {
92  state.selectedNodes.push_back(
93  {state.node, std::make_unique<TGeoHMatrix>(transform)});
94  }
95  state.node = nullptr;
96  } else {
97  // If it's not accepted, get the associated volume
98  state.volume = state.node->GetVolume();
99  state.node = nullptr;
100  // Set one further down
101  select(state, options, transform);
102  }
103  }
104  return;
105 }