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  G4int copyNumBase,
75  G4bool surfCheck)
76 {
77  struct _Wrap : public G4AssemblyVolume {
78  static void imprintsCountPlus(G4AssemblyVolume* p)
79  { _Wrap* w = (_Wrap*)p; w->ImprintsCountPlus(); }
80  };
81  std::string path;
82  TGeoVolume* vol = parent->GetVolume();
83  G4AssemblyVolume* par_ass = pParentAssembly->m_assembly;
84  Geant4GeometryInfo& info = cnv.data();
85  unsigned int numberOfDaughters = (copyNumBase == 0) ? pMotherLV->GetNoDaughters() : copyNumBase;
86 
87  // We start from the first available index
88  numberOfDaughters++;
89  _Wrap::imprintsCountPlus(par_ass);
90 
91  path = detail::tools::placementPath(chain);
92  printout(cnv.debugPlacements ? ALWAYS : DEBUG, "Geant4Converter",
93  "++ Assembly: %s", path.c_str());
94  std::vector<G4AssemblyTriplet>::iterator iter = par_ass->GetTripletsIterator();
95  for( unsigned int i = 0, n = par_ass->TotalTriplets(); i < n; i++, iter++ ) {
96  Chain new_chain = chain;
97  const auto& triplet = *iter;
98  const TGeoNode* node = pParentAssembly->m_entries[i];
99  Geant4AssemblyVolume* avol = pParentAssembly->m_places[i];
100 
101  new_chain.emplace_back(node);
102  path = detail::tools::placementPath(new_chain);
103  printout(cnv.debugPlacements ? ALWAYS : DEBUG, "Geant4Converter",
104  " Assembly: Entry: %s", path.c_str());
105 
106  G4Transform3D Ta( *(triplet.GetRotation()), triplet.GetTranslation() );
107  if ( triplet.IsReflection() ) {
108  Ta = Ta * G4ReflectZ3D();
109  }
110  G4Transform3D Tfinal = transformation * Ta;
111  if ( triplet.GetVolume() ) {
112  // Generate the unique name for the next PV instance
113  // The name has format:
114  //
115  // av_WWW_impr_XXX_YYY_ZZZ
116  // where the fields mean:
117  // WWW - assembly volume instance number
118  // XXX - assembly volume imprint number
119  // YYY - the name of a log. volume we want to make a placement of
120  // ZZZ - the log. volume index inside the assembly volume
121  //
122  std::stringstream pvName;
123  pvName << "AV_"
124  << m_assembly->GetAssemblyID()
125  << '!'
126  << parent->GetName()
127  << '#'
128  << parent->GetNumber()
129  << '!'
130  << node->GetName()
131  << '#'
132  << node->GetNumber()
133  << std::ends;
134  // Generate a new physical volume instance inside a mother
135  // (as we allow 3D transformation use G4ReflectionFactory to
136  // take into account eventual reflection)
137  //
138  G4PhysicalVolumesPair pvPlaced
139  = G4ReflectionFactory::Instance()->Place( Tfinal,
140  pvName.str().c_str(),
141  triplet.GetVolume(),
142  pMotherLV,
143  false,
144  numberOfDaughters + i,
145  surfCheck );
146 
147  info.g4VolumeImprints[vol].emplace_back(new_chain,pvPlaced.first);
148  printout(cnv.debugPlacements ? ALWAYS : DEBUG,
149  "Geant4Converter", "++ Place %svolume %s in assembly.",
150  triplet.IsReflection() ? "REFLECTED " : "", path.c_str());
151  printout(cnv.debugPlacements ? ALWAYS : DEBUG,
152  "Geant4Converter", " Assembly:Parent: %s %s %p G4:%s",
153  parent->GetName(), node->GetName(),
154  (void*)node, pvName.str().c_str());
155  if ( pvPlaced.second ) {
156  G4Exception("Geant4AssemblyVolume::imprint(..)", "GeomVol0003", FatalException,
157  "Fancy construct popping new mother from the stack!");
158  }
159  }
160  else if ( triplet.GetAssembly() ) {
161  // Place volumes in this assembly with composed transformation
162  imprint(cnv, parent, std::move(new_chain), avol, pMotherLV, Tfinal, i*100+copyNumBase, surfCheck );
163  }
164  else {
165  G4Exception("Geant4AssemblyVolume::imprint(..)", "GeomVol0003", FatalException,
166  "Triplet has no volume and no assembly");
167  }
168  }
169 }
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, G4int copyNumBase, 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