DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
GlobalDetectorAlignment.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
16 #include <DD4hep/DetectorTools.h>
17 #include <DD4hep/InstanceCount.h>
18 #include <DD4hep/MatrixHelpers.h>
19 #include <DD4hep/Printout.h>
20 #include <DD4hep/detail/Handle.inl>
22 
23 // ROOT include files
24 #include <TGeoMatrix.h>
25 #include <TGeoManager.h>
26 
27 #ifdef __GNUC__ // Disable some diagnostics.
28 #pragma GCC diagnostic ignored "-Wunused-function"
29 #endif
30 
31 using LevelElements = std::vector<std::pair<int,dd4hep::DetElement> >;
32 using namespace dd4hep::align;
33 using dd4hep::printout;
34 using dd4hep::INFO;
35 
37 namespace dd4hep {
38 
40  namespace align {
41 
43 
49  public:
51  std::vector<GlobalAlignment> volume_alignments;
52 
53  public:
54  GlobalAlignmentData(const std::string& path)
55  : NamedObject(path, "global-alignment")
56  {
57  this->global = GlobalAlignment( path );
58  }
60  detail::destroyHandle( this->global );
61  }
62  };
63  } /* End namespace Aligments */
64 } /* End namespace dd4hep */
65 
67 
68 namespace {
69 
70  static bool s_GlobalDetectorAlignment_debug = true;
71 
72  GlobalAlignment _align(const GlobalAlignment& a, TGeoHMatrix* transform, bool check, double overlap) {
73  TGeoPhysicalNode* node = a.ptr();
74  if ( node ) {
75  TGeoMatrix* mm = node->GetNode()->GetMatrix();
76  bool dbg = GlobalDetectorAlignment::debug();
77  if ( dbg ) {
78  printout(INFO, "Alignment", "DELTA matrix of %s", node->GetName());
79  transform->Print();
80  printout(INFO, "Alignment", "OLD matrix of %s", node->GetName());
81  mm->Print();
82  }
83  std::vector<dd4hep::PlacedVolume> places;
84  for( int i = 0; i < node->GetLevel(); ++i )
85  places.emplace_back(node->GetNode(i+1));
86 
87  transform->MultiplyLeft(mm); // orig * delta
88  node->Align(transform, 0, check, overlap);
89  if ( dbg ) {
90  printout(INFO, "Alignment", "NEW matrix of %s", node->GetName());
91  node->GetNode()->GetMatrix()->Print();
92  }
93 
94  for( int i = 0; i < node->GetLevel(); ++i ) {
95  //const char *tag = " ";
96  dd4hep::PlacedVolume p = node->GetNode(i+1);
97  if ( nullptr == p->GetUserExtension() ) {
98  //tag = "SET ";
99  p->SetUserExtension(places[i]->GetUserExtension());
100  }
101 #if 0
102  printout(INFO, "Alignment", "_align(places): %s Path[%d]: %-24s %p <-> %p %s",
103  tag, i, p.name(), p.ptr(), places[i].ptr(), places[i].name());
104  tag = " ";
105 #endif
106  dd4hep::Volume v = p->GetVolume();
107  if ( nullptr == v->GetUserExtension() ) {
108  //tag = "SET ";
109  v->SetUserExtension(places[i].volume()->GetUserExtension());
110  }
111 #if 0
112  printout(INFO, "Alignment", "_align(volumes): %s Path[%d]: %-24s %p <-> %p %s",
113  tag, i, v.name(), v.ptr(), places[i].volume().ptr(), places[i].volume().name());
114  p.access();
115  v.access();
116  places[i].access();
117  places[i].volume().access();
118 #endif
119  }
120 
121  /*
122  printout(INFO, "Alignment", "Apply new relative matrix mother to daughter:");
123  transform->Print();
124  transform->MultiplyLeft(mm); // orig * delta
125  printout(INFO, "Alignment", "With deltas %s ....", n->GetName());
126  transform->Print();
127  n->Align(transform, 0, check, overlap);
128 
129  Position local, global = a.toGlobal(local);
130  cout << "Local:" << local << " Global: " << global
131  << " and back:" << a.globalToLocal(global) << endl;
132  */
133  return GlobalAlignment(node);
134  }
135  dd4hep::except("GlobalDetectorAlignment", "Cannot align non existing physical node. [Invalid Handle]");
136  return { };
137  }
138 
139  GlobalAlignment _alignment(const GlobalDetectorAlignment& det) {
140  dd4hep::DetElement::Object& e = det._data();
141  if ( !e.global_alignment.isValid() ) {
142  std::string path = dd4hep::detail::tools::placementPath(det);
144  }
146  if ( h.isValid() && h->global.isValid() ) {
147  return h->global;
148  }
149  dd4hep::except("GlobalDetectorAlignment", "Cannot access global alignment data. [Invalid Handle]");
150  return { };
151  }
152 
153  void _dumpParentElements(GlobalDetectorAlignment& det, LevelElements& elements) {
154  int level = 0;
160  dd4hep::detail::tools::PlacementPath::const_reverse_iterator j=nodes.rbegin();
161  dd4hep::detail::tools::ElementPath::const_reverse_iterator k=det_nodes.rbegin();
162  for(; j!=nodes.rend(); ++j, ++level) {
163  //cout << "(" << level << ") " << (void*)((*j).ptr())
164  // << " " << string((*j)->GetName()) << " ";
165  if ( ::strcmp((*j).ptr()->GetName(), (*k).placement().ptr()->GetName()) ) {
166  //cout << "[DE]";
167  elements.emplace_back(level, *k);
168  ++k;
169  }
170  else {
171  //elements.emplace_back(level,DetElement());
172  }
173  //std::cout << " ";
174  }
175  //std::cout << std::endl;
176  }
177 }
178 
181  : DetElement(std::move(e))
182 {
183 }
184 
187  : DetElement(std::move(e))
188 {
189 }
190 
193  return s_GlobalDetectorAlignment_debug;
194 }
195 
198  bool tmp = s_GlobalDetectorAlignment_debug;
199  s_GlobalDetectorAlignment_debug = value;
200  return tmp;
201 }
202 
204 void GlobalDetectorAlignment::collectNodes(std::vector<PlacedVolume>& nodes) {
205  detail::tools::placementPath(*this, nodes);
206 }
207 
210  return _alignment(*this);
211 }
212 
214 std::vector<GlobalAlignment>& GlobalDetectorAlignment::volumeAlignments() {
215  Handle<GlobalAlignmentData> h(_data().global_alignment);
216  return h->volume_alignments;
217 }
218 
220 const std::vector<GlobalAlignment>& GlobalDetectorAlignment::volumeAlignments() const {
221  Handle<GlobalAlignmentData> h(_data().global_alignment);
222  return h->volume_alignments;
223 }
224 
226 GlobalAlignment GlobalDetectorAlignment::align(const Position& pos, bool chk, double overlap) {
227  return align(detail::matrix::_transform(pos), chk, overlap);
228 }
229 
231 GlobalAlignment GlobalDetectorAlignment::align(const RotationZYX& rot, bool chk, double overlap) {
232  return align(detail::matrix::_transform(rot), chk, overlap);
233 }
234 
236 GlobalAlignment GlobalDetectorAlignment::align(const Position& pos, const RotationZYX& rot, bool chk, double overlap) {
237  return align(detail::matrix::_transform(pos, rot), chk, overlap);
238 }
239 
241 GlobalAlignment GlobalDetectorAlignment::align(const Transform3D& transform, bool chk, double overlap) {
242  return align(detail::matrix::_transform(transform), chk, overlap);
243 }
244 
246 GlobalAlignment GlobalDetectorAlignment::align(TGeoHMatrix* matrix, bool chk, double overlap) {
247  return _align(_alignment(*this), matrix, chk, overlap);
248 }
249 
251 GlobalAlignment GlobalDetectorAlignment::align(const std::string& elt_path, const Position& pos, bool chk, double overlap) {
252  return align(elt_path,detail::matrix::_transform(pos), chk, overlap);
253 }
254 
256 GlobalAlignment GlobalDetectorAlignment::align(const std::string& elt_path, const RotationZYX& rot, bool chk, double overlap) {
257  return align(elt_path,detail::matrix::_transform(rot), chk, overlap);
258 }
259 
262 GlobalDetectorAlignment::align(const std::string& elt_path, const Position& pos, const RotationZYX& rot, bool chk, double overlap) {
263  return align(elt_path,detail::matrix::_transform(pos, rot), chk, overlap);
264 }
265 
267 GlobalAlignment GlobalDetectorAlignment::align(const std::string& elt_path, const Transform3D& transform, bool chk, double overlap) {
268  return align(elt_path,detail::matrix::_transform(transform), chk, overlap);
269 }
270 
272 GlobalAlignment GlobalDetectorAlignment::align(const std::string& elt_path, TGeoHMatrix* matrix, bool chk, double overlap) {
273  if ( elt_path.empty() )
274  return _align(_alignment(*this), matrix, chk, overlap);
275  else if ( elt_path == placementPath() )
276  return _align(_alignment(*this), matrix, chk, overlap);
277  else if ( elt_path[0] == '/' ) {
278  GlobalAlignment a(elt_path);
279  volumeAlignments().emplace_back(a);
280  return _align(a, matrix, chk, overlap);
281  }
282  GlobalAlignment a(placementPath() + '/' + elt_path);
283  volumeAlignments().emplace_back(a);
284  return _align(a, matrix, chk, overlap);
285 }
dd4hep::align::GlobalAlignmentData::global
GlobalAlignment global
Definition: GlobalDetectorAlignment.cpp:50
dd4hep::align::GlobalDetectorAlignment::volumeAlignments
std::vector< GlobalAlignment > & volumeAlignments()
Alignment entries for lower level volumes, which are NOT attached to daughter DetElements.
Definition: GlobalDetectorAlignment.cpp:214
dd4hep::align::GlobalDetectorAlignment
GlobalDetectorAlignment. DetElement Handle supporting alignment operations.
Definition: GlobalDetectorAlignment.h:35
dd4hep::detail::destroyHandle
void destroyHandle(T &handle)
Helper to delete objects from heap and reset the handle.
Definition: Handle.h:185
dd4hep::detail::tools::ElementPath
std::vector< DetElement > ElementPath
Definition: DetectorTools.h:38
dd4hep::align::GlobalDetectorAlignment::debug
static bool debug()
Access debugging flag.
Definition: GlobalDetectorAlignment.cpp:192
v
View * v
Definition: MultiView.cpp:28
dd4hep::align::GlobalAlignment
Main handle class to hold a TGeo alignment object of type TGeoPhysicalNode.
Definition: GlobalAlignment.h:38
LevelElements
std::vector< std::pair< int, dd4hep::DetElement > > LevelElements
Definition: GlobalDetectorAlignment.cpp:31
MatrixHelpers.h
dd4hep::align::GlobalDetectorAlignment::alignment
GlobalAlignment alignment() const
Access to the alignment block.
Definition: GlobalDetectorAlignment.cpp:209
dd4hep::PlacedVolume
Handle class holding a placed volume (also called physical volume)
Definition: Volumes.h:163
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::Handle::isValid
bool isValid() const
Check the validity of the object held by the handle.
Definition: Handle.h:128
dd4hep::Handle
Handle: a templated class like a shared pointer, which allows specialized access to tgeometry objects...
Definition: Handle.h:84
DetectorInterna.h
dd4hep::detail::matrix::_transform
TGeoHMatrix * _transform(const Transform3D &trans)
Convert a Transform3D object to a newly created TGeoHMatrix.
Definition: MatrixHelpers.cpp:140
dd4hep::Handle::name
const char * name() const
Access the object name (or "" if not supported by the object)
dd4hep::DetElementObject
Data class with properties of a detector element.
Definition: DetectorInterna.h:81
dd4hep::align::GlobalAlignmentData
Global alignment data container.
Definition: GlobalDetectorAlignment.cpp:48
dd4hep::align::GlobalAlignmentData::volume_alignments
std::vector< GlobalAlignment > volume_alignments
Definition: GlobalDetectorAlignment.cpp:51
dd4hep::DetElement
Handle class describing a detector element.
Definition: DetElement.h:188
dd4hep::Volume
Handle class holding a placed volume (also called physical volume)
Definition: Volumes.h:370
dd4hep::align::GlobalDetectorAlignment::align
GlobalAlignment align(const Position &pos, bool check=false, double overlap=0.001)
Align the PhysicalNode of the placement of the detector element (translation only)
Definition: GlobalDetectorAlignment.cpp:226
DetectorTools.h
dd4hep::DetElement::_data
Object & _data() const
Additional data accessor.
Definition: DetElement.h:317
dd4hep::DetElementObject::global_alignment
Ref_t global_alignment
Global alignment data.
Definition: DetectorInterna.h:142
dd4hep::DetElement::placementPath
const std::string & placementPath() const
Access to the full path to the placed object.
Definition: DetElement.cpp:85
dd4hep::Transform3D
ROOT::Math::Transform3D Transform3D
Definition: Objects.h:117
dd4hep::Position
ROOT::Math::XYZVector Position
Definition: Objects.h:81
dd4hep::Ref_t
Handle< NamedObject > Ref_t
Default Ref_t definition describing named objects.
Definition: Handle.h:182
dd4hep::detail::tools::elementPath
std::string elementPath(DetElement element)
Assemble the path of a particular detector element.
Definition: DetectorTools.cpp:207
dd4hep::Handle::access
T * access() const
Checked object access. Throws invalid handle runtime exception if invalid handle.
std
Definition: Plugins.h:30
dd4hep::Handle::ptr
T * ptr() const
Access to the held object.
Definition: Handle.h:153
dd4hep::align::GlobalDetectorAlignment::GlobalDetectorAlignment
GlobalDetectorAlignment(DetElement e)
Initializing constructor.
Definition: GlobalDetectorAlignment.cpp:180
dd4hep::align::GlobalAlignmentData::GlobalAlignmentData
GlobalAlignmentData(const std::string &path)
Definition: GlobalDetectorAlignment.cpp:54
dd4hep::align::GlobalDetectorAlignment::collectNodes
void collectNodes(std::vector< PlacedVolume > &nodes)
Collect all placements from the detector element up to the world volume.
Definition: GlobalDetectorAlignment.cpp:204
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::NamedObject
Implementation of a named object.
Definition: NamedObject.h:30
det
DetElement::Object * det
Definition: AlignmentsCalculator.cpp:66
dd4hep::detail::tools::PlacementPath
std::vector< PlacedVolume > PlacementPath
Definition: DetectorTools.h:39
dd4hep::RotationZYX
ROOT::Math::RotationZYX RotationZYX
Definition: Objects.h:105
InstanceCount.h
GlobalDetectorAlignment.h
dd4hep::align::GlobalAlignmentData::~GlobalAlignmentData
virtual ~GlobalAlignmentData()
Definition: GlobalDetectorAlignment.cpp:59
Printout.h
dd4hep::align
Namespace for implementation details of the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:31
DD4HEP_INSTANTIATE_HANDLE_NAMED
DD4HEP_INSTANTIATE_HANDLE_NAMED(GlobalAlignmentData)