DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
GlobalAlignmentOperators.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/Detector.h>
16 #include <DD4hep/Printout.h>
20 
21 // C/C++ include files
22 #include <stdexcept>
23 
24 using namespace dd4hep::align;
25 
27  if ( !cache.insert(alignment) ) {
28  // Error
29  }
30 }
31 
32 void GlobalAlignmentSelector::operator()(Entries::value_type e) const {
33  TGeoPhysicalNode* pn = 0;
34  nodes.emplace(e->path,std::make_pair(pn,e));
35 }
36 
37 void GlobalAlignmentSelector::operator()(const Cache::value_type& entry) const {
38  TGeoPhysicalNode* pn = entry.second;
39  for(Entries::const_iterator j=entries.begin(); j != entries.end(); ++j) {
40  Entries::value_type e = (*j);
42  const char* p = pn->GetName();
43  bool reset_children = GlobalAlignmentStack::resetChildren(*e);
44  if ( reset_children && ::strstr(p,e->path.c_str()) == p ) {
45  nodes.emplace(p,std::make_pair(pn,e));
46  break;
47  }
48  else if ( e->path == p ) {
49  nodes.emplace(p,std::make_pair(pn,e));
50  break;
51  }
52  }
53  }
54 }
55 
57  printout(ALWAYS,"GlobalAlignmentCache","++++++++++++++++++++++++ Summary ++++++++++++++++++++++++");
58 }
59 
60 template <> void GlobalAlignmentActor<DDAlign_standard_operations::node_print>::operator()(Nodes::value_type& n) const {
61  TGeoPhysicalNode* p = n.second.first;
62  const Entry& e = *n.second.second;
63  printout(ALWAYS,"GlobalAlignmentCache","Need to reset entry:%s - %s [needsReset:%s, hasMatrix:%s]",
64  p->GetName(), e.path.c_str(),
67 }
68 
69 template <> void GlobalAlignmentActor<DDAlign_standard_operations::node_delete>::operator()(Nodes::value_type& n) const {
70  delete n.second.second;
71  n.second.second = 0;
72 }
73 
74 template <> void GlobalAlignmentActor<DDAlign_standard_operations::node_reset>::operator()(Nodes::value_type& n) const {
75  TGeoPhysicalNode* p = n.second.first;
76  std::string np;
77  if ( p->IsAligned() ) {
78  for (Int_t i=0, nLvl=p->GetLevel(); i<=nLvl; i++) {
79  TGeoNode* node = p->GetNode(i);
80  TGeoMatrix* mat = node->GetMatrix(); // Node's relative matrix
81  np += std::string("/")+node->GetName();
82  if ( !mat->IsIdentity() && i > 0 ) { // Ignore the 'world', is identity anyhow
83  GlobalAlignment a = cache.get(np);
84  if ( a.isValid() ) {
85  printout(ALWAYS,"GlobalAlignmentActor<reset>","Correct path:%s leaf:%s",p->GetName(),np.c_str());
86  TGeoHMatrix* glob = p->GetMatrix(i-1);
87  if ( a.isValid() && i!=nLvl ) {
88  *mat = *(a->GetOriginalMatrix());
89  }
90  else if ( i==nLvl ) {
91  TGeoHMatrix* hm = dynamic_cast<TGeoHMatrix*>(mat);
92  TGeoMatrix* org = p->GetOriginalMatrix();
93  if ( hm && org ) {
94  hm->SetTranslation(org->GetTranslation());
95  hm->SetRotation(org->GetRotationMatrix());
96  }
97  else {
98  printout(ALWAYS,"GlobalAlignmentActor<reset>",
99  "Invalid operation: %p %p", (void*)hm, (void*)org);
100  }
101  }
102  *glob *= *mat;
103  }
104  }
105  }
106  }
107 }
108 
109 template <> void GlobalAlignmentActor<DDAlign_standard_operations::node_align>::operator()(Nodes::value_type& n) const {
110  Entry& e = *n.second.second;
111  bool overlap = GlobalAlignmentStack::overlapDefined(e);
112  DetElement det = e.detector;
113 
114  if ( !det->global_alignment.isValid() && !GlobalAlignmentStack::hasMatrix(e) ) {
115  printout(WARNING,"GlobalAlignmentActor","++++ SKIP Alignment %s DE:%s Valid:%s Matrix:%s",
116  e.path.c_str(),det.placementPath().c_str(),
117  yes_no(det->global_alignment.isValid()), yes_no(GlobalAlignmentStack::hasMatrix(e)));
118  return;
119  }
121  printout(INFO,"GlobalAlignmentActor","++++ %s DE:%s Matrix:%s",
122  e.path.c_str(),det.placementPath().c_str(),yes_no(GlobalAlignmentStack::hasMatrix(e)));
123  }
124  // Need to care about optional arguments 'check_overlaps' and 'overlap'
126  GlobalAlignment align;
127  Transform3D trafo;
128  const Delta& delta = e.delta;
129  bool no_vol = e.path == det.placementPath();
130  double ovl_precision = e.overlap;
131 
133  trafo = Transform3D(Translation3D(delta.translation)*delta.pivot*delta.rotation*(delta.pivot.Inverse()));
134  else if ( delta.checkFlag(Delta::HAVE_ROTATION|Delta::HAVE_TRANSLATION) )
135  trafo = Transform3D(delta.rotation,delta.translation);
136  else if ( delta.checkFlag(Delta::HAVE_ROTATION|Delta::HAVE_PIVOT) )
137  trafo = Transform3D(delta.pivot*delta.rotation*(delta.pivot.Inverse()));
138  else if ( delta.checkFlag(Delta::HAVE_ROTATION) )
139  trafo = Transform3D(delta.rotation);
140  else if ( delta.checkFlag(Delta::HAVE_TRANSLATION) )
141  trafo = Transform3D(delta.translation);
142 
143  if ( GlobalAlignmentStack::checkOverlap(e) && overlap )
144  align = no_vol ? ad.align(trafo,ovl_precision,e.overlap) : ad.align(e.path,trafo,ovl_precision,e.overlap);
146  align = no_vol ? ad.align(trafo,ovl_precision) : ad.align(e.path,trafo,ovl_precision);
147  else
148  align = no_vol ? ad.align(trafo) : ad.align(e.path,trafo);
149 
150  if ( align.isValid() ) {
151  insert(align);
152  return;
153  }
154  except("GlobalAlignmentActor","Failed to apply alignment for "+e.path);
155 }
156 
157 #if 0
158 void alignment_reset_dbg(const string& path, const GlobalAlignment& a) {
159  TGeoPhysicalNode* n = a.ptr();
160  cout << " +++++++++++++++++++++++++++++++ " << path << endl;
161  cout << " +++++ Misaligned physical node: " << endl;
162  n->Print();
163  string np;
164  if ( n->IsAligned() ) {
165  for (Int_t i=0; i<=n->GetLevel(); i++) {
166  TGeoMatrix* mat = n->GetNode(i)->GetMatrix();
167  np += "/";
168  np += n->GetNode(i)->GetName();
169  if ( mat->IsIdentity() ) continue;
170  if ( i == 0 ) continue;
171 
172  TGeoHMatrix* glob = n->GetMatrix(i-1);
173  NodeMap::const_iterator j=original_matrices.find(np);
174  if ( j != original_matrices.end() && i!=n->GetLevel() ) {
175  cout << " +++++ Patch Level: " << i << np << endl;
176  *mat = *((*j).second);
177  }
178  else {
179  if ( i==n->GetLevel() ) {
180  cout << " +++++ Level: " << i << np << " --- Original matrix: " << endl;
181  n->GetOriginalMatrix()->Print();
182  cout << " +++++ Level: " << i << np << " --- Local matrix: " << endl;
183  mat->Print();
184  TGeoHMatrix* hm = dynamic_cast<TGeoHMatrix*>(mat);
185  hm->SetTranslation(n->GetOriginalMatrix()->GetTranslation());
186  hm->SetRotation(n->GetOriginalMatrix()->GetRotationMatrix());
187  cout << " +++++ Level: " << i << np << " --- New local matrix" << endl;
188  mat->Print();
189  }
190  else {
191  cout << " +++++ Level: " << i << np << " --- Keep matrix " << endl;
192  mat->Print();
193  }
194  }
195  cout << " +++++ Level: " << i << np << " --- Global matrix: " << endl;
196  glob->Print();
197  *glob *= *mat;
198  cout << " +++++ Level: " << i << np << " --- New global matrix: " << endl;
199  glob->Print();
200  }
201  }
202  cout << "\n\n\n +++++ physical node (full): " << np << endl;
203  n->Print();
204  cout << " +++++ physical node (global): " << np << endl;
205  n->GetMatrix()->Print();
206 }
207 #endif
dd4hep::align::GlobalAlignmentSelector::operator()
void operator()(const GlobalAlignmentCache::Cache::value_type &e) const
Function callback for cache entries.
dd4hep::Delta::HAVE_TRANSLATION
@ HAVE_TRANSLATION
Definition: AlignmentData.h:48
dd4hep::align::GlobalAlignmentStack::StackEntry::detector
DetElement detector
Reference to the detector element.
Definition: GlobalAlignmentStack.h:55
dd4hep::align::GlobalAlignmentStack::hasMatrix
static bool hasMatrix(const StackEntry &e)
Check if this alignment entry has a non unitary transformation matrix.
Definition: GlobalAlignmentStack.h:127
dd4hep::align::GlobalDetectorAlignment
GlobalDetectorAlignment. DetElement Handle supporting alignment operations.
Definition: GlobalDetectorAlignment.h:35
dd4hep::align::GlobalDetectorAlignment::debug
static bool debug()
Access debugging flag.
Definition: GlobalDetectorAlignment.cpp:192
dd4hep::align::GlobalAlignment
Main handle class to hold a TGeo alignment object of type TGeoPhysicalNode.
Definition: GlobalAlignment.h:38
Detector.h
dd4hep::Delta::HAVE_PIVOT
@ HAVE_PIVOT
Definition: AlignmentData.h:50
GlobalAlignmentOperators.h
dd4hep::Delta::HAVE_ROTATION
@ HAVE_ROTATION
Definition: AlignmentData.h:49
delta
const Delta * delta
Definition: AlignmentsCalculator.cpp:67
dd4hep::Handle::isValid
bool isValid() const
Check the validity of the object held by the handle.
Definition: Handle.h:128
DetectorInterna.h
dd4hep::align::GlobalAlignmentOperator::insert
void insert(GlobalAlignment alignment) const
Insert alignment entry.
Definition: GlobalAlignmentOperators.cpp:26
dd4hep::align::GlobalAlignmentStack::StackEntry::overlap
double overlap
Parameter for overlap checking.
Definition: GlobalAlignmentStack.h:61
dd4hep::Translation3D
ROOT::Math::Translation3D Translation3D
Definition: Objects.h:119
dd4hep::align::GlobalAlignmentStack::StackEntry::delta
Delta delta
Delta transformation to be applied.
Definition: GlobalAlignmentStack.h:57
dd4hep::align::GlobalAlignmentSelector::entries
const Entries & entries
Definition: GlobalAlignmentOperators.h:57
dd4hep::align::GlobalAlignmentStack::checkOverlap
static bool checkOverlap(const StackEntry &e)
Check if the overlap flag checking is enabled.
Definition: GlobalAlignmentStack.h:123
dd4hep::Transform3D
ROOT::Math::Transform3D Transform3D
Definition: Objects.h:117
dd4hep::align::GlobalAlignmentStack::needsReset
static bool needsReset(const StackEntry &e)
Check flag if the node location should be reset.
Definition: GlobalAlignmentStack.h:129
dd4hep::align::GlobalAlignmentStack::overlapDefined
static bool overlapDefined(const StackEntry &e)
Check if the overlap flag checking is enabled.
Definition: GlobalAlignmentStack.h:121
dd4hep::align::GlobalAlignmentOperator::nodes
Nodes & nodes
Definition: GlobalAlignmentOperators.h:40
dd4hep::Handle::ptr
T * ptr() const
Access to the held object.
Definition: Handle.h:153
det
DetElement::Object * det
Definition: AlignmentsCalculator.cpp:66
dd4hep::align::GlobalAlignmentActor::init
void init()
Definition: GlobalAlignmentOperators.h:87
dd4hep::align::GlobalAlignmentStack::StackEntry::path
std::string path
Path to the misaligned volume.
Definition: GlobalAlignmentStack.h:59
GlobalDetectorAlignment.h
dd4hep::align::GlobalAlignmentCache::insert
bool insert(GlobalAlignment alignment)
Add a new entry to the cache. The key is the placement path.
Definition: GlobalAlignmentCache.cpp:90
dd4hep::align::GlobalAlignmentStack::StackEntry
Stack entry definition.
Definition: GlobalAlignmentStack.h:52
Printout.h
dd4hep::align
Namespace for implementation details of the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:31
dd4hep::align::GlobalAlignmentStack::resetChildren
static bool resetChildren(const StackEntry &e)
Check flag if the node location and all children should be reset.
Definition: GlobalAlignmentStack.h:131
dd4hep::align::GlobalAlignmentOperator::cache
GlobalAlignmentCache & cache
Definition: GlobalAlignmentOperators.h:39
dd4hep::align::GlobalAlignmentActor::operator()
void operator()(Nodes::value_type &e) const
Function callback for entries.