DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Geant4AssemblyVolume.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 
15 #include <G4LogicalVolume.hh>
16 #include <G4VPhysicalVolume.hh>
17 #include <G4ReflectionFactory.hh>
18 
20 #include <sstream>
21 #include <string>
22 
24 #include <DD4hep/DetectorTools.h>
25 #include <DDG4/Geant4Converter.h>
28 
29 using namespace dd4hep::sim;
30 
34 }
35 
36 
39  // Do not delete the G4 assembly. This is done by Geant4!
40  m_assembly = nullptr;
41 }
42 
44 long Geant4AssemblyVolume::placeVolume(const TGeoNode* n,
45  G4LogicalVolume* pPlacedVolume,
46  G4Transform3D& transformation)
47 {
48  size_t id = m_assembly->TotalTriplets();
49  m_entries.emplace_back(n);
50  m_places.emplace_back(nullptr);
51  m_assembly->AddPlacedVolume(pPlacedVolume, transformation);
52  return (long)id;
53 }
54 
56 long Geant4AssemblyVolume::placeAssembly(const TGeoNode* n,
57  Geant4AssemblyVolume* pPlacedVolume,
58  G4Transform3D& transformation)
59 {
60  size_t id = m_assembly->TotalTriplets();
61  m_entries.emplace_back(n);
62  m_places.emplace_back(pPlacedVolume);
63  m_assembly->AddPlacedAssembly(pPlacedVolume->m_assembly, transformation);
64  return (long)id;
65 }
66 
69  const TGeoNode* parent,
70  Chain chain,
71  Geant4AssemblyVolume* pParentAssembly,
72  G4LogicalVolume* pMotherLV,
73  G4Transform3D& transformation,
74  G4bool surfCheck)
75 {
76  struct _Wrap : public G4AssemblyVolume {
77  static void imprintsCountPlus(G4AssemblyVolume* p)
78  { _Wrap* w = (_Wrap*)p; w->ImprintsCountPlus(); }
79  };
80  std::string path;
81  TGeoVolume* vol = parent->GetVolume();
82  G4AssemblyVolume* par_ass = pParentAssembly->m_assembly;
83  Geant4GeometryInfo& info = cnv.data();
84 
85  _Wrap::imprintsCountPlus(par_ass);
86 
87  path = detail::tools::placementPath(chain);
88  printout(cnv.debugPlacements ? ALWAYS : DEBUG, "Geant4Converter",
89  "++ Assembly: %s", path.c_str());
90  std::vector<G4AssemblyTriplet>::iterator iter = par_ass->GetTripletsIterator();
91  for( unsigned int i = 0, n = par_ass->TotalTriplets(); i < n; i++, iter++ ) {
92  Chain new_chain = chain;
93  const auto& triplet = *iter;
94  const TGeoNode* node = pParentAssembly->m_entries[i];
95  Geant4AssemblyVolume* avol = pParentAssembly->m_places[i];
96 
97  new_chain.emplace_back(node);
98  path = detail::tools::placementPath(new_chain);
99  printout(cnv.debugPlacements ? ALWAYS : DEBUG, "Geant4Converter",
100  " Assembly: Entry: %s", path.c_str());
101 
102  G4Transform3D Ta( *(triplet.GetRotation()), triplet.GetTranslation() );
103  if ( triplet.IsReflection() ) {
104  Ta = Ta * G4ReflectZ3D();
105  }
106  G4Transform3D Tfinal = transformation * Ta;
107  if ( triplet.GetVolume() ) {
108  // Generate the unique name for the next PV instance
109  // The name has format:
110  //
111  // av_WWW_impr_XXX_YYY_ZZZ
112  // where the fields mean:
113  // WWW - assembly volume instance number
114  // XXX - assembly volume imprint number
115  // YYY - the name of a log. volume we want to make a placement of
116  // ZZZ - the log. volume index inside the assembly volume
117  //
118  std::stringstream pvName;
119  pvName << "AV_"
120  << m_assembly->GetAssemblyID()
121  << '!'
122  << parent->GetName()
123  << '#'
124  << parent->GetNumber()
125  << '!'
126  << node->GetName()
127  << '#'
128  << node->GetNumber()
129  << std::ends;
130  // Generate a new physical volume instance inside a mother
131  // (as we allow 3D transformation use G4ReflectionFactory to
132  // take into account eventual reflection)
133  //
134  G4PhysicalVolumesPair pvPlaced
135  = G4ReflectionFactory::Instance()->Place( Tfinal,
136  pvName.str().c_str(),
137  triplet.GetVolume(),
138  pMotherLV,
139  false,
140  node->GetNumber(),
141  surfCheck );
142 
143  info.g4VolumeImprints[vol].emplace_back(new_chain,pvPlaced.first);
144  printout(cnv.debugPlacements ? ALWAYS : DEBUG,
145  "Geant4Converter", "++ Place %svolume %s in assembly.",
146  triplet.IsReflection() ? "REFLECTED " : "", path.c_str());
147  printout(cnv.debugPlacements ? ALWAYS : DEBUG,
148  "Geant4Converter", " Assembly:Parent: %s %s %p G4:%s",
149  parent->GetName(), node->GetName(),
150  (void*)node, pvName.str().c_str());
151  if ( pvPlaced.second ) {
152  G4Exception("Geant4AssemblyVolume::imprint(..)", "GeomVol0003", FatalException,
153  "Fancy construct popping new mother from the stack!");
154  }
155  }
156  else if ( triplet.GetAssembly() ) {
157  // Place volumes in this assembly with composed transformation
158  imprint(cnv, parent, std::move(new_chain), avol, pMotherLV, Tfinal, surfCheck );
159  }
160  else {
161  G4Exception("Geant4AssemblyVolume::imprint(..)", "GeomVol0003", FatalException,
162  "Triplet has no volume and no assembly");
163  }
164  }
165 }
dd4hep::sim::Geant4Converter
Geometry converter from dd4hep to Geant 4.
Definition: Geant4Converter.h:32
Geant4GeometryInfo.h
dd4hep::sim::Geant4AssemblyVolume::m_places
std::vector< Geant4AssemblyVolume * > m_places
Definition: Geant4AssemblyVolume.h:47
dd4hep::info
std::size_t info(const std::string &src, const std::string &msg)
Definition: RootDictionary.h:65
dd4hep::sim::Geant4AssemblyVolume::m_entries
std::vector< const TGeoNode * > m_entries
Definition: Geant4AssemblyVolume.h:46
dd4hep::sim::Geant4Mapping::data
Geant4GeometryInfo & data() const
Access to the data pointer.
Definition: Geant4Mapping.h:58
dd4hep::sim::Geant4AssemblyVolume
Hack! Wrapper around G4AssemblyVolume to access protected members.
Definition: Geant4AssemblyVolume.h:41
dd4hep::detail::tools::placementPath
std::string placementPath(DetElement element)
Assemble the placement path from a given detector element to the world volume.
Definition: DetectorTools.cpp:276
dd4hep::sim::Geant4AssemblyVolume::placeVolume
long placeVolume(const TGeoNode *n, G4LogicalVolume *pPlacedVolume, G4Transform3D &transformation)
Place logical daughter volume into the assembly.
Definition: Geant4AssemblyVolume.cpp:44
dd4hep::sim::Geant4AssemblyVolume::Chain
std::vector< const TGeoNode * > Chain
Definition: Geant4AssemblyVolume.h:45
dd4hep::sim::Geant4AssemblyVolume::imprint
void imprint(const Geant4Converter &cnv, const TGeoNode *n, Chain chain, Geant4AssemblyVolume *pAssembly, G4LogicalVolume *pMotherLV, G4Transform3D &transformation, G4bool surfCheck)
Expand all daughter placements and expand the contained assemblies to imprints.
Definition: Geant4AssemblyVolume.cpp:68
dd4hep::sim::Geant4Converter::debugPlacements
bool debugPlacements
Property: Flag to debug placements during conversion mechanism.
Definition: Geant4Converter.h:43
Geant4AssemblyVolume.h
DetectorTools.h
dd4hep::sim::Geant4AssemblyVolume::m_assembly
G4AssemblyVolume * m_assembly
Definition: Geant4AssemblyVolume.h:48
G4AssemblyVolume
Class of the Geant4 toolkit. See http://www-geant4.kek.jp/Reference.
Definition: Geant4Classes.h:7
dd4hep::sim
Namespace for the Geant4 based simulation part of the AIDA detector description toolkit.
Definition: Geant4Output2EDM4hep.cpp:49
dd4hep::sim::Geant4AssemblyVolume::~Geant4AssemblyVolume
virtual ~Geant4AssemblyVolume()
Default destructor.
Definition: Geant4AssemblyVolume.cpp:38
dd4hep::sim::Geant4GeometryInfo
Concreate class holding the relation information between geant4 objects and dd4hep objects.
Definition: Geant4GeometryInfo.h:93
dd4hep::sim::Geant4AssemblyVolume::placeAssembly
long placeAssembly(const TGeoNode *n, Geant4AssemblyVolume *pPlacedVolume, G4Transform3D &transformation)
Place daughter assembly into the assembly
Definition: Geant4AssemblyVolume.cpp:56
dd4hep::sim::Geant4AssemblyVolume::Geant4AssemblyVolume
Geant4AssemblyVolume()
Default constructor with initialization.
Definition: Geant4AssemblyVolume.cpp:32
Geant4Converter.h