DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Geant4Helpers.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 
14 // Framework include files
15 #include <DD4hep/Printout.h>
16 #include <DD4hep/DD4hepUnits.h>
17 #include <DDG4/Geant4Helpers.h>
18 
19 #include <CLHEP/Units/SystemOfUnits.h>
20 
21 // ROOT include files
22 #include <TGeoMatrix.h>
23 
24 namespace {
25  static constexpr const double CM_2_MM = (CLHEP::centimeter/dd4hep::centimeter);
26 
28  struct MyTransform3D : public G4Transform3D {
29 #if 0
30  MyTransform3D(double XX, double XY, double XZ, double DX,
31  double YX, double YY, double YZ, double DY,
32  double ZX, double ZY, double ZZ, double DZ)
33  : G4Transform3D(XX, XY, XZ, DX, YX, YY, YZ, DY, ZX, ZY, ZZ, DZ) {
34  }
35 #endif
36  MyTransform3D(const double* t, const double* r)
37  : G4Transform3D(r[0],r[1],r[2],t[0]*CM_2_MM,r[3],r[4],r[5],t[1]*CM_2_MM,r[6],r[7],r[8],t[2]*CM_2_MM) {
38  }
39  MyTransform3D(const double* t)
40  : G4Transform3D(1.0, 0.0, 0.0, t[0]*CM_2_MM, 0.0, 1.0, 0.0, t[1]*CM_2_MM, 0.0, 0.0, 1.0, t[2]*CM_2_MM) {
41  }
42  MyTransform3D(Transform3D&& copy) : Transform3D(std::move(copy)) {}
43  };
45  class MyG4RotationMatrix : public G4RotationMatrix {
46  public:
47  MyG4RotationMatrix() : G4RotationMatrix() {}
48  MyG4RotationMatrix(const double* r) : G4RotationMatrix(r[0],r[1],r[2],r[3],r[4],r[5],r[6],r[7],r[8]) {
49  }
50  };
51 }
52 
53 G4RotationMatrix dd4hep::sim::g4Rotation(const TGeoMatrix* matrix) {
54  return matrix->IsRotation() ? MyG4RotationMatrix(matrix->GetRotationMatrix()) : MyG4RotationMatrix();
55 }
56 
57 G4RotationMatrix dd4hep::sim::g4Rotation(const TGeoMatrix& matrix) {
58  return g4Rotation(&matrix);
59 }
60 
61 G4RotationMatrix dd4hep::sim::g4Rotation(const TGeoRotation* matrix) {
62  return matrix->IsRotation() ? MyG4RotationMatrix(matrix->GetRotationMatrix()) : MyG4RotationMatrix();
63 }
64 
65 G4RotationMatrix dd4hep::sim::g4Rotation(const TGeoRotation& matrix) {
66  return g4Rotation(&matrix);
67 }
68 
69 G4RotationMatrix dd4hep::sim::g4Rotation(const Rotation3D& rot) {
70  double r[9];
71  rot.GetComponents(r, r+9);
72  return MyG4RotationMatrix(r);
73 }
74 
75 G4RotationMatrix dd4hep::sim::g4Rotation(const RotationZYX& rot) {
76  return g4Rotation(Rotation3D(rot));
77 }
78 
79 G4Transform3D dd4hep::sim::g4Transform(const double translation[]) {
80  return MyTransform3D(translation);
81 }
82 
83 G4Transform3D dd4hep::sim::g4Transform(const double* translation, const double* rotation) {
84  return MyTransform3D(translation, rotation);
85 }
86 
87 void dd4hep::sim::g4Transform(const double translation[], G4Transform3D& transform) {
88  transform = MyTransform3D(translation);
89 }
90 
91 void dd4hep::sim::g4Transform(const double* translation, const double* rotation, G4Transform3D& transform) {
92  transform = MyTransform3D(translation, rotation);
93 }
94 
95 G4Transform3D dd4hep::sim::g4Transform(const TGeoMatrix& matrix) {
96  return g4Transform(&matrix);
97 }
98 
99 G4Transform3D dd4hep::sim::g4Transform(const TGeoMatrix* matrix) {
100  return matrix->IsRotation()
101  ? g4Transform(matrix->GetTranslation(), matrix->GetRotationMatrix())
102  : g4Transform(matrix->GetTranslation());
103 }
104 
105 void dd4hep::sim::g4Transform(const TGeoMatrix* matrix, G4Transform3D& transform) {
106  matrix->IsRotation()
107  ? g4Transform(matrix->GetTranslation(), matrix->GetRotationMatrix(), transform)
108  : g4Transform(matrix->GetTranslation(), transform);
109 }
110 
111 void dd4hep::sim::g4Transform(const TGeoMatrix& matrix, G4Transform3D& transform) {
112  g4Transform(&matrix, transform);
113 }
114 
115 G4Transform3D dd4hep::sim::g4Transform(const TGeoTranslation& translation, const TGeoRotation& rotation) {
116  return g4Transform(&translation, &rotation);
117 }
118 
119 G4Transform3D dd4hep::sim::g4Transform(const TGeoTranslation* translation, const TGeoRotation* rotation) {
120  return rotation->IsRotation()
121  ? g4Transform(translation->GetTranslation(), rotation->GetRotationMatrix())
122  : g4Transform(translation->GetTranslation());
123 }
124 
125 G4Transform3D dd4hep::sim::g4Transform(const Position& pos, const Rotation3D& rot) {
126  double r[9], t[3] = {pos.X(), pos.Y(), pos.Z()};
127  rot.GetComponents(r, r+9);
128  return g4Transform(t, r);
129 }
130 
131 void dd4hep::sim::g4Transform(const Position& pos, const Rotation3D& rot, G4Transform3D& transform) {
132  double r[9], t[3] = {pos.X(), pos.Y(), pos.Z()};
133  rot.GetComponents(r, r+9);
134  g4Transform(t, r, transform);
135 }
136 
137 G4Transform3D dd4hep::sim::g4Transform(const Position& pos, const RotationZYX& rot) {
138  return g4Transform(pos, Rotation3D(rot));
139 }
140 
141 G4Transform3D dd4hep::sim::g4Transform(const Transform3D& matrix) {
142  Position pos;
143  Rotation3D rot;
144  matrix.GetDecomposition(rot, pos);
145  return g4Transform(pos, rot);
146 }
147 
148 void dd4hep::sim::g4Transform(const Transform3D& matrix, G4Transform3D& transform) {
149  Position pos;
150  Rotation3D rot;
151  matrix.GetDecomposition(rot, pos);
152  g4Transform(pos, rot, transform);
153 }
154 
156 G4Transform3D
157 dd4hep::sim::generate_placements(const G4Transform3D& start,
158  const G4Transform3D& delta,
159  std::size_t count,
160  const std::function<void(const G4Transform3D& delta)>& callback)
161 {
162  G4Transform3D transform(start);
163  for( std::size_t i = 0; i < count; ++i ) {
164  callback(transform);
165  transform = transform * delta;
166  }
167  return transform;
168 }
169 
171 G4Transform3D
172 dd4hep::sim::generate_placements(const G4Transform3D& start,
173  const G4Transform3D& delta1,
174  std::size_t count1,
175  const G4Transform3D& delta2,
176  std::size_t count2,
177  const std::function<void(const G4Transform3D& delta)>& callback)
178 {
179  G4Transform3D transform2 = start;
180  for(std::size_t j = 0; j < count2; ++j) {
181  G4Transform3D transform1 = transform2;
182  generate_placements(transform1, delta1, count1, callback);
183  transform2 = transform2 * delta2;
184  }
185  return transform2;
186 }
187 
189 G4Transform3D
190 dd4hep::sim::generate_placements(const G4Transform3D& start,
191  const G4Transform3D& delta1,
192  std::size_t count1,
193  const G4Transform3D& delta2,
194  std::size_t count2,
195  const G4Transform3D& delta3,
196  std::size_t count3,
197  const std::function<void(const G4Transform3D& delta)>& callback)
198 {
199  G4Transform3D transform3 = start;
200  for(std::size_t k = 0; k < count3; ++k) {
201  G4Transform3D transform2 = transform3;
202  generate_placements(transform2, delta1, count1, delta2, count2, callback);
203  transform3 = transform3 * delta3;
204  }
205  return transform3;
206 }
207 
208 std::pair<double, EAxis> dd4hep::sim::extract_axis(const Transform3D& trafo) {
209  constexpr double eps = 2e-14;
210  G4Transform3D tr = g4Transform(trafo);
211  if ( fabs(tr.xx()) > (1e0-eps) &&
212  fabs(tr.yy()) > (1e0-eps) &&
213  fabs(tr.zz()) > (1e0-eps) ) {
214  if ( fabs(tr.dx()) > eps )
215  return { tr.dx(), kYAxis };
216  else if ( fabs(tr.dy()) > eps )
217  return { tr.dy(), kYAxis };
218  else if ( fabs(tr.dz()) > eps )
219  return { tr.dz(), kZAxis };
220  }
221  else if ( tr.getTranslation().mag() > eps && fabs(tr.dz()) < eps ) {
222  return { tr.getTranslation().rho(), kRho };
223  }
224  else if ( fabs(tr.dz()) < eps ) {
225  return { tr.getRotation().phi(), kPhi }; // Is this correct ?
226  }
227  except("Geant4Converter","Invalid volume parametrization matrix. Unknown Axis!");
228  return { 0e0, kUndefined };
229 }
dd4hep::sim::g4Rotation
G4RotationMatrix g4Rotation(const TGeoMatrix *matrix)
Extract G4 rotation matrix from ROOT matrices.
Definition: Geant4Helpers.cpp:53
CM_2_MM
#define CM_2_MM
Definition: ParticleActors.cpp:37
delta
const Delta * delta
Definition: AlignmentsCalculator.cpp:67
dd4hep::Rotation3D
ROOT::Math::Rotation3D Rotation3D
Definition: Objects.h:113
dd4hep::sim::g4Transform
void g4Transform(const double *translation, const double *rotation, G4Transform3D &transform)
These conversions automatically apply the conversion from CM to MM!
Definition: Geant4Helpers.cpp:91
dd4hep::sim::extract_axis
std::pair< double, EAxis > extract_axis(const Transform3D &trafo)
Definition: Geant4Helpers.cpp:208
Geant4Helpers.h
dd4hep::Transform3D
ROOT::Math::Transform3D Transform3D
Definition: Objects.h:117
dd4hep::Position
ROOT::Math::XYZVector Position
Definition: Objects.h:81
std
Definition: Plugins.h:30
dd4hep::sim::generate_placements
G4Transform3D generate_placements(const G4Transform3D &start, const G4Transform3D &delta, std::size_t count, const std::function< void(const G4Transform3D &delta)> &callback)
Generate parameterised placements in 1 dimension according to transformation delta.
Definition: Geant4Helpers.cpp:157
dd4hep::detail::tools::copy
void copy(Alignment from, Alignment to)
Copy alignment object from source object.
Definition: AlignmentTools.cpp:43
dd4hep::RotationZYX
ROOT::Math::RotationZYX RotationZYX
Definition: Objects.h:105
DD4hepUnits.h
Printout.h