File indexing completed on 2025-01-30 09:16:25
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <DD4hep/Detector.h>
0016 #include <DD4hep/Printout.h>
0017 #include <DD4hep/detail/DetectorInterna.h>
0018 #include <DDAlign/GlobalAlignmentOperators.h>
0019 #include <DDAlign/GlobalDetectorAlignment.h>
0020
0021
0022 #include <stdexcept>
0023
0024 using namespace dd4hep::align;
0025
0026 void GlobalAlignmentOperator::insert(GlobalAlignment alignment) const {
0027 if ( !cache.insert(alignment) ) {
0028
0029 }
0030 }
0031
0032 void GlobalAlignmentSelector::operator()(Entries::value_type e) const {
0033 TGeoPhysicalNode* pn = 0;
0034 nodes.emplace(e->path,std::make_pair(pn,e));
0035 }
0036
0037 void GlobalAlignmentSelector::operator()(const Cache::value_type& entry) const {
0038 TGeoPhysicalNode* pn = entry.second;
0039 for(Entries::const_iterator j=entries.begin(); j != entries.end(); ++j) {
0040 Entries::value_type e = (*j);
0041 if ( GlobalAlignmentStack::needsReset(*e) || GlobalAlignmentStack::hasMatrix(*e) ) {
0042 const char* p = pn->GetName();
0043 bool reset_children = GlobalAlignmentStack::resetChildren(*e);
0044 if ( reset_children && ::strstr(p,e->path.c_str()) == p ) {
0045 nodes.emplace(p,std::make_pair(pn,e));
0046 break;
0047 }
0048 else if ( e->path == p ) {
0049 nodes.emplace(p,std::make_pair(pn,e));
0050 break;
0051 }
0052 }
0053 }
0054 }
0055
0056 template <> void GlobalAlignmentActor<DDAlign_standard_operations::node_print>::init() {
0057 printout(ALWAYS,"GlobalAlignmentCache","++++++++++++++++++++++++ Summary ++++++++++++++++++++++++");
0058 }
0059
0060 template <> void GlobalAlignmentActor<DDAlign_standard_operations::node_print>::operator()(Nodes::value_type& n) const {
0061 TGeoPhysicalNode* p = n.second.first;
0062 const Entry& e = *n.second.second;
0063 printout(ALWAYS,"GlobalAlignmentCache","Need to reset entry:%s - %s [needsReset:%s, hasMatrix:%s]",
0064 p->GetName(), e.path.c_str(),
0065 yes_no(GlobalAlignmentStack::needsReset(e)),
0066 yes_no(GlobalAlignmentStack::hasMatrix(e)));
0067 }
0068
0069 template <> void GlobalAlignmentActor<DDAlign_standard_operations::node_delete>::operator()(Nodes::value_type& n) const {
0070 delete n.second.second;
0071 n.second.second = 0;
0072 }
0073
0074 template <> void GlobalAlignmentActor<DDAlign_standard_operations::node_reset>::operator()(Nodes::value_type& n) const {
0075 TGeoPhysicalNode* p = n.second.first;
0076 std::string np;
0077 if ( p->IsAligned() ) {
0078 for (Int_t i=0, nLvl=p->GetLevel(); i<=nLvl; i++) {
0079 TGeoNode* node = p->GetNode(i);
0080 TGeoMatrix* mat = node->GetMatrix();
0081 np += std::string("/")+node->GetName();
0082 if ( !mat->IsIdentity() && i > 0 ) {
0083 GlobalAlignment a = cache.get(np);
0084 if ( a.isValid() ) {
0085 printout(ALWAYS,"GlobalAlignmentActor<reset>","Correct path:%s leaf:%s",p->GetName(),np.c_str());
0086 TGeoHMatrix* glob = p->GetMatrix(i-1);
0087 if ( a.isValid() && i!=nLvl ) {
0088 *mat = *(a->GetOriginalMatrix());
0089 }
0090 else if ( i==nLvl ) {
0091 TGeoHMatrix* hm = dynamic_cast<TGeoHMatrix*>(mat);
0092 TGeoMatrix* org = p->GetOriginalMatrix();
0093 if ( hm && org ) {
0094 hm->SetTranslation(org->GetTranslation());
0095 hm->SetRotation(org->GetRotationMatrix());
0096 }
0097 else {
0098 printout(ALWAYS,"GlobalAlignmentActor<reset>",
0099 "Invalid operation: %p %p", (void*)hm, (void*)org);
0100 }
0101 }
0102 *glob *= *mat;
0103 }
0104 }
0105 }
0106 }
0107 }
0108
0109 template <> void GlobalAlignmentActor<DDAlign_standard_operations::node_align>::operator()(Nodes::value_type& n) const {
0110 Entry& e = *n.second.second;
0111 bool overlap = GlobalAlignmentStack::overlapDefined(e);
0112 DetElement det = e.detector;
0113
0114 if ( !det->global_alignment.isValid() && !GlobalAlignmentStack::hasMatrix(e) ) {
0115 printout(WARNING,"GlobalAlignmentActor","++++ SKIP Alignment %s DE:%s Valid:%s Matrix:%s",
0116 e.path.c_str(),det.placementPath().c_str(),
0117 yes_no(det->global_alignment.isValid()), yes_no(GlobalAlignmentStack::hasMatrix(e)));
0118 return;
0119 }
0120 if ( GlobalDetectorAlignment::debug() ) {
0121 printout(INFO,"GlobalAlignmentActor","++++ %s DE:%s Matrix:%s",
0122 e.path.c_str(),det.placementPath().c_str(),yes_no(GlobalAlignmentStack::hasMatrix(e)));
0123 }
0124
0125 GlobalDetectorAlignment ad(det);
0126 GlobalAlignment align;
0127 Transform3D trafo;
0128 const Delta& delta = e.delta;
0129 bool no_vol = e.path == det.placementPath();
0130 double ovl_precision = e.overlap;
0131
0132 if ( delta.checkFlag(Delta::HAVE_ROTATION|Delta::HAVE_PIVOT|Delta::HAVE_TRANSLATION) )
0133 trafo = Transform3D(Translation3D(delta.translation)*delta.pivot*delta.rotation*(delta.pivot.Inverse()));
0134 else if ( delta.checkFlag(Delta::HAVE_ROTATION|Delta::HAVE_TRANSLATION) )
0135 trafo = Transform3D(delta.rotation,delta.translation);
0136 else if ( delta.checkFlag(Delta::HAVE_ROTATION|Delta::HAVE_PIVOT) )
0137 trafo = Transform3D(delta.pivot*delta.rotation*(delta.pivot.Inverse()));
0138 else if ( delta.checkFlag(Delta::HAVE_ROTATION) )
0139 trafo = Transform3D(delta.rotation);
0140 else if ( delta.checkFlag(Delta::HAVE_TRANSLATION) )
0141 trafo = Transform3D(delta.translation);
0142
0143 if ( GlobalAlignmentStack::checkOverlap(e) && overlap )
0144 align = no_vol ? ad.align(trafo,ovl_precision,e.overlap) : ad.align(e.path,trafo,ovl_precision,e.overlap);
0145 else if ( GlobalAlignmentStack::checkOverlap(e) )
0146 align = no_vol ? ad.align(trafo,ovl_precision) : ad.align(e.path,trafo,ovl_precision);
0147 else
0148 align = no_vol ? ad.align(trafo) : ad.align(e.path,trafo);
0149
0150 if ( align.isValid() ) {
0151 insert(align);
0152 return;
0153 }
0154 except("GlobalAlignmentActor","Failed to apply alignment for "+e.path);
0155 }
0156
0157 #if 0
0158 void alignment_reset_dbg(const string& path, const GlobalAlignment& a) {
0159 TGeoPhysicalNode* n = a.ptr();
0160 cout << " +++++++++++++++++++++++++++++++ " << path << endl;
0161 cout << " +++++ Misaligned physical node: " << endl;
0162 n->Print();
0163 string np;
0164 if ( n->IsAligned() ) {
0165 for (Int_t i=0; i<=n->GetLevel(); i++) {
0166 TGeoMatrix* mat = n->GetNode(i)->GetMatrix();
0167 np += "/";
0168 np += n->GetNode(i)->GetName();
0169 if ( mat->IsIdentity() ) continue;
0170 if ( i == 0 ) continue;
0171
0172 TGeoHMatrix* glob = n->GetMatrix(i-1);
0173 NodeMap::const_iterator j=original_matrices.find(np);
0174 if ( j != original_matrices.end() && i!=n->GetLevel() ) {
0175 cout << " +++++ Patch Level: " << i << np << endl;
0176 *mat = *((*j).second);
0177 }
0178 else {
0179 if ( i==n->GetLevel() ) {
0180 cout << " +++++ Level: " << i << np << " --- Original matrix: " << endl;
0181 n->GetOriginalMatrix()->Print();
0182 cout << " +++++ Level: " << i << np << " --- Local matrix: " << endl;
0183 mat->Print();
0184 TGeoHMatrix* hm = dynamic_cast<TGeoHMatrix*>(mat);
0185 hm->SetTranslation(n->GetOriginalMatrix()->GetTranslation());
0186 hm->SetRotation(n->GetOriginalMatrix()->GetRotationMatrix());
0187 cout << " +++++ Level: " << i << np << " --- New local matrix" << endl;
0188 mat->Print();
0189 }
0190 else {
0191 cout << " +++++ Level: " << i << np << " --- Keep matrix " << endl;
0192 mat->Print();
0193 }
0194 }
0195 cout << " +++++ Level: " << i << np << " --- Global matrix: " << endl;
0196 glob->Print();
0197 *glob *= *mat;
0198 cout << " +++++ Level: " << i << np << " --- New global matrix: " << endl;
0199 glob->Print();
0200 }
0201 }
0202 cout << "\n\n\n +++++ physical node (full): " << np << endl;
0203 n->Print();
0204 cout << " +++++ physical node (global): " << np << endl;
0205 n->GetMatrix()->Print();
0206 }
0207 #endif