2#include "Tracking/geo/TrackingGeometry.h"
4#include "G4RunManager.hh"
5#include "G4UIsession.hh"
6#include "G4strstreambuf.hh"
7#include "Tracking/geo/GeoUtils.h"
20 G4UIsession* SessionStart() {
return nullptr; }
21 G4int ReceiveG4cout(
const G4String&) {
return 0; }
22 G4int ReceiveG4cerr(
const G4String&) {
return 0; }
26 const Acts::GeometryContext& gctx,
27 const std::string& gdml,
bool debug)
34 double rotationAngle = M_PI * 0.5;
42 Acts::Vector3 xPos1(cos(rotationAngle), 0., sin(rotationAngle));
43 Acts::Vector3 yPos1(0., 1., 0.);
44 Acts::Vector3 zPos1(-sin(rotationAngle), 0., cos(rotationAngle));
46 y_rot_.col(0) = xPos1;
47 y_rot_.col(1) = yPos1;
48 y_rot_.col(2) = zPos1;
51 Acts::Vector3 xPos2(1., 0., 0.);
52 Acts::Vector3 yPos2(0., cos(rotationAngle), sin(rotationAngle));
53 Acts::Vector3 zPos2(0., -sin(rotationAngle), cos(rotationAngle));
55 x_rot_.col(0) = xPos2;
56 x_rot_.col(1) = yPos2;
57 x_rot_.col(2) = zPos2;
70 std::unique_ptr<SilentG4> silence;
71 if (G4RunManager::GetRunManager() ==
nullptr) {
73 silence = std::make_unique<SilentG4>();
76 G4coutbuf.SetDestination(silence.get());
77 G4cerrbuf.SetDestination(silence.get());
84 parser.Read(gdml_,
false);
86 fWorldPhysVol_ = parser.GetWorldVolume();
92 G4coutbuf.SetDestination(
nullptr);
93 G4cerrbuf.SetDestination(
nullptr);
97G4VPhysicalVolume* TrackingGeometry::findDaughterByName(G4VPhysicalVolume* pvol,
99 G4LogicalVolume* lvol = pvol->GetLogicalVolume();
100 for (G4int i = 0; i < lvol->GetNoDaughters(); i++) {
101 G4VPhysicalVolume* fDaughterPhysVol = lvol->GetDaughter(i);
102 std::string dName = fDaughterPhysVol->GetName();
103 if (dName.find(name) != std::string::npos)
return fDaughterPhysVol;
110void TrackingGeometry::getAllDaughters(G4VPhysicalVolume* pvol) {
111 G4LogicalVolume* lvol = pvol->GetLogicalVolume();
114 std::cout <<
"Checking daughters of ::" << pvol->GetName() << std::endl;
116 for (G4int i = 0; i < lvol->GetNoDaughters(); i++) {
117 G4VPhysicalVolume* fDaughterPhysVol = lvol->GetDaughter(i);
120 std::cout <<
"name::" << fDaughterPhysVol->GetName() << std::endl;
121 std::cout <<
"pos::" << fDaughterPhysVol->GetTranslation() << std::endl;
122 std::cout <<
"n_dau::"
123 << fDaughterPhysVol->GetLogicalVolume()->GetNoDaughters()
125 std::cout <<
"replica::" << fDaughterPhysVol->IsReplicated() << std::endl;
126 std::cout <<
"copyNR::" << fDaughterPhysVol->GetCopyNo() << std::endl;
128 getAllDaughters(fDaughterPhysVol);
147void TrackingGeometry::dumpGeometry(
const std::string& outputDir,
148 const Acts::GeometryContext& gctx)
const {
149 if (!tGeometry_)
return;
152 std::cout << __PRETTY_FUNCTION__ << std::endl;
154 for (
auto const& surfaceId : layer_surface_map_) {
155 std::cout <<
" " << surfaceId.first << std::endl;
156 std::cout <<
" Check the surface" << std::endl;
158 surfaceId.second->toStream(gctx);
159 std::cout <<
" GeometryID::" << surfaceId.second->geometryId()
161 std::cout <<
" GeometryID::" << surfaceId.second->geometryId().value()
167 boost::filesystem::create_directory(outputDir);
169 double outputScalor = 1.0;
170 size_t outputPrecision = 6;
172 Acts::ObjVisualization3D objVis(outputPrecision, outputScalor);
173 Acts::ViewConfig containerView = Acts::ViewConfig({220, 220, 220});
174 Acts::ViewConfig volumeView = Acts::ViewConfig({220, 220, 0});
175 Acts::ViewConfig sensitiveView = Acts::ViewConfig({0, 180, 240});
176 Acts::ViewConfig passiveView = Acts::ViewConfig({240, 280, 0});
177 Acts::ViewConfig gridView = Acts::ViewConfig({220, 0, 0});
179 Acts::GeometryView3D::drawTrackingVolume(
180 objVis, *(tGeometry_->highestTrackingVolume()), gctx, containerView,
181 volumeView, passiveView, sensitiveView, gridView,
true,
"",
".");
185Acts::Transform3 TrackingGeometry::GetTransform(
const G4VPhysicalVolume& phex,
186 bool toTrackingFrame)
const {
187 Acts::Vector3 pos(phex.GetTranslation().x(), phex.GetTranslation().y(),
188 phex.GetTranslation().z());
190 Acts::RotationMatrix3 rotation;
191 ConvertG4Rot(phex.GetRotation(), rotation);
194 if (toTrackingFrame) {
195 pos(0) = phex.GetTranslation().z();
196 pos(1) = phex.GetTranslation().x();
197 pos(2) = phex.GetTranslation().y();
198 rotation = x_rot_ * y_rot_ * rotation;
201 Acts::Translation3 translation(pos);
203 Acts::Transform3 transform(translation * rotation);
210Acts::Transform3 TrackingGeometry::toTracker(
211 const Acts::Transform3& trans)
const {
212 Acts::Vector3 pos{trans.translation()(2), trans.translation()(0),
213 trans.translation()(1)};
215 Acts::RotationMatrix3 rotation = trans.rotation();
216 rotation = x_rot_ * y_rot_ * rotation;
218 Acts::Translation3 translation(pos);
219 Acts::Transform3 transform(translation * rotation);
225void TrackingGeometry::ConvertG4Rot(
const G4RotationMatrix* g4rot,
226 Acts::RotationMatrix3& rot)
const {
230 rot = Acts::RotationMatrix3::Identity();
233 rot(0, 0) = g4rot->xx();
234 rot(0, 1) = g4rot->xy();
235 rot(0, 2) = g4rot->xz();
237 rot(1, 0) = g4rot->yx();
238 rot(1, 1) = g4rot->yy();
239 rot(1, 2) = g4rot->yz();
241 rot(2, 0) = g4rot->zx();
242 rot(2, 1) = g4rot->zy();
243 rot(2, 2) = g4rot->zz();
249Acts::Vector3 TrackingGeometry::ConvertG4Pos(
const G4ThreeVector& g4pos)
const {
250 Acts::Vector3 trans{g4pos.x(), g4pos.y(), g4pos.z()};
253 std::cout << std::endl;
254 std::cout <<
"g4pos::" << g4pos << std::endl;
255 std::cout << trans << std::endl;
261void TrackingGeometry::getSurfaces(
262 std::vector<const Acts::Surface*>& surfaces)
const {
264 throw std::runtime_error(
"TrackingGeometry::getSurfaces tGeometry is null");
266 const Acts::TrackingVolume* tVolume = tGeometry_->highestTrackingVolume();
267 if (tVolume->confinedVolumes()) {
268 for (
auto volume : tVolume->confinedVolumes()->arrayObjects()) {
269 if (volume->confinedLayers()) {
270 for (
const auto& layer : volume->confinedLayers()->arrayObjects()) {
271 if (layer->layerType() == Acts::navigation)
continue;
272 for (
auto surface : layer->surfaceArray()->surfaces()) {
274 surfaces.push_back(surface);
284void TrackingGeometry::makeLayerSurfacesMap() {
285 std::vector<const Acts::Surface*> surfaces;
286 getSurfaces(surfaces);
288 for (
auto& surface : surfaces) {
293 unsigned int volumeId = surface->geometryId().volume();
294 unsigned int layerId = (surface->geometryId().layer() /
297 unsigned int sensorId =
298 surface->geometryId().sensitive() -
303 std::cout <<
"VolumeID " << volumeId <<
" LayerId " << layerId
304 <<
" sensorId " << sensorId << std::endl;
307 unsigned int surfaceId = volumeId * 1000 + layerId * 100 + sensorId;
309 layer_surface_map_[surfaceId] = surface;
This class throws away all of the messages from Geant4.
TrackingGeometry(const std::string &name, const Acts::GeometryContext &gctx, const std::string &gdml, bool debug)
All classes in the ldmx-sw project use this namespace.