20 #include <DD4hep/detail/Handle.inl>
24 #include <TGeoMatrix.h>
25 #include <TGeoManager.h>
27 #ifdef __GNUC__ // Disable some diagnostics.
28 #pragma GCC diagnostic ignored "-Wunused-function"
33 using dd4hep::printout;
70 static bool s_GlobalDetectorAlignment_debug =
true;
73 TGeoPhysicalNode* node = a.
ptr();
75 TGeoMatrix* mm = node->GetNode()->GetMatrix();
78 printout( INFO,
"Alignment",
"DELTA matrix of %s", node->GetName() );
80 printout( INFO,
"Alignment",
"OLD matrix of %s", node->GetName() );
83 std::vector<dd4hep::PlacedVolume> places;
84 for(
int i = 0; i < node->GetLevel(); ++i )
85 places.emplace_back( node->GetNode(i+1) );
87 transform->MultiplyLeft(mm);
88 node->Align( transform, 0, check, overlap );
90 printout( INFO,
"Alignment",
"NEW matrix of %s", node->GetName() );
91 node->GetNode()->GetMatrix()->Print();
96 std::string path =
"/";
100 path += pv->GetName();
101 for(
int i = 0; i < node->GetLevel(); ++i ) {
103 pv = node->GetNode( i+1 );
104 if (
nullptr == pv->GetUserExtension() ) {
105 pv->SetUserExtension( places[i]->GetUserExtension() );
107 oldm = pv->GetMotherVolume();
110 path += pv->GetName();
111 if( oldm !=
v.ptr() ) {
112 printout(dd4hep::ALWAYS,
"GlobalAlignment",
114 "+++ Fix mother relationship after alignment: %s place: %p volume: %p mother volume: old: %p new: %p",
115 path.c_str(), (
void*)pv.
ptr(), (
void*)pv->GetVolume(), (
void*)oldm, (
void*)
v.ptr() );
118 pv->SetMotherVolume(
v.ptr() );
122 for(
int idau = 0; idau <
v->GetNdaughters(); ++idau ) {
123 TGeoNode* dau =
v->GetNode( idau );
124 if(
v.ptr() != dau->GetMotherVolume() ) {
125 dau->SetMotherVolume(
v );
127 oldm = dau->GetMotherVolume();
128 if( oldm !=
v.ptr() ) {
129 printout(dd4hep::ALWAYS,
"GlobalAlignment",
131 "+++ Fix mother relationship after alignment: %s/%s place: %p volume: %p "
132 "mother volume: old: %p --> new: %p",
133 path.c_str(), dau->GetName(), (
void*)dau, (
void*)dau->GetVolume(),
134 (
void*)oldm, (
void*)
v.ptr());
140 if (
nullptr ==
v->GetUserExtension() ) {
141 v->SetUserExtension( places[i].volume()->GetUserExtension() );
146 dd4hep::except(
"GlobalDetectorAlignment",
"Cannot align non existing physical node. [Invalid Handle]");
157 if ( h.isValid() && h->global.isValid() ) {
160 dd4hep::except(
"GlobalDetectorAlignment",
"Cannot access global alignment data. [Invalid Handle]");
171 dd4hep::detail::tools::PlacementPath::const_reverse_iterator j=nodes.rbegin();
172 dd4hep::detail::tools::ElementPath::const_reverse_iterator k=det_nodes.rbegin();
173 for(; j!=nodes.rend(); ++j, ++level) {
176 if ( ::strcmp((*j).ptr()->GetName(), (*k).placement().ptr()->GetName()) ) {
178 elements.emplace_back(level, *k);
204 return s_GlobalDetectorAlignment_debug;
209 bool tmp = s_GlobalDetectorAlignment_debug;
210 s_GlobalDetectorAlignment_debug = value;
221 return _alignment(*
this);
258 return _align(_alignment(*
this), matrix, chk, overlap);
284 if ( elt_path.empty() )
285 return _align(_alignment(*
this), matrix, chk, overlap);
287 return _align(_alignment(*
this), matrix, chk, overlap);
288 else if ( elt_path[0] ==
'/' ) {
291 return _align(a, matrix, chk, overlap);
295 return _align(a, matrix, chk, overlap);