Skip to content

Commit

Permalink
Fix to global alignment problem issued by Ben.
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusFrankATcernch committed Jul 1, 2024
1 parent 8a14d91 commit 9dab9c0
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 165 deletions.
6 changes: 3 additions & 3 deletions DDAlign/include/DDAlign/AlignmentTags.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#ifndef DDALIGN_ALIGNMENTTAGS_H
#define DDALIGN_ALIGNMENTTAGS_H

// Framework include files
#include "XML/XMLElements.h"
/// Framework include files
#include <XML/XMLElements.h>
#ifndef UNICODE
#define UNICODE(x) extern const ::dd4hep::xml::Tag_t Unicode_##x
#endif
Expand Down Expand Up @@ -45,7 +45,7 @@ namespace dd4hep {
}

#undef UNICODE // Do not miss this one!
#include "XML/XMLTags.h"
#include <XML/XMLTags.h>

#define _ALU(a) ::dd4hep::DDAlign::Unicode_##a

Expand Down
10 changes: 5 additions & 5 deletions DDAlign/include/DDAlign/AlignmentsCalib.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
#define DDALIGN_ALIGNMENTSCALIB_H

// Framework includes
#include "DD4hep/DetElement.h"
#include "DD4hep/Alignments.h"
#include "DD4hep/AlignmentData.h"
#include "DD4hep/ConditionsMap.h"
#include "DD4hep/AlignmentsCalculator.h"
#include <DD4hep/DetElement.h>
#include <DD4hep/Alignments.h>
#include <DD4hep/AlignmentData.h>
#include <DD4hep/ConditionsMap.h>
#include <DD4hep/AlignmentsCalculator.h>

// C/C++ include files
#include <set>
Expand Down
6 changes: 3 additions & 3 deletions DDAlign/include/DDAlign/GlobalAlignmentCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
#define DDALIGN_GLOBALALIGNMENTCACHE_H

// Framework include files
#include "DD4hep/ExtensionEntry.h"
#include "DD4hep/GlobalAlignment.h"
#include "DDAlign/GlobalAlignmentStack.h"
#include <DD4hep/ExtensionEntry.h>
#include <DD4hep/GlobalAlignment.h>
#include <DDAlign/GlobalAlignmentStack.h>

/// Namespace for the AIDA detector description toolkit
namespace dd4hep {
Expand Down
6 changes: 3 additions & 3 deletions DDAlign/include/DDAlign/GlobalAlignmentOperators.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
#define DDALIGN_GLOBALALIGNMENTOPERATORS_H

// Framework include files
#include "DD4hep/Alignments.h"
#include "DD4hep/GlobalAlignment.h"
#include "DDAlign/GlobalAlignmentCache.h"
#include <DD4hep/Alignments.h>
#include <DD4hep/GlobalAlignment.h>
#include <DDAlign/GlobalAlignmentCache.h>

/// Namespace for the AIDA detector description toolkit
namespace dd4hep {
Expand Down
7 changes: 3 additions & 4 deletions DDAlign/include/DDAlign/GlobalAlignmentStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
#define DDALIGN_GLOBALALIGNMENTSTACK_H

// Framework include files
#include "DD4hep/Alignments.h"
#include "DD4hep/AlignmentData.h"
//#include "DD4hep/Objects.h"
#include "DD4hep/Memory.h"
#include <DD4hep/Alignments.h>
#include <DD4hep/AlignmentData.h>
#include <DD4hep/Memory.h>


/// Namespace for the AIDA detector description toolkit
Expand Down
6 changes: 3 additions & 3 deletions DDAlign/include/DDAlign/GlobalAlignmentWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
#define DDALIGN_GLOBALALIGNMENTWRITER_H

// Framework include files
#include "XML/XMLElements.h"
#include "DD4hep/DetElement.h"
#include "DD4hep/GlobalAlignment.h"
#include <XML/XMLElements.h>
#include <DD4hep/DetElement.h>
#include <DD4hep/GlobalAlignment.h>

/// Namespace for the AIDA detector description toolkit
namespace dd4hep {
Expand Down
4 changes: 2 additions & 2 deletions DDAlign/include/DDAlign/GlobalDetectorAlignment.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#define DDALIGN_GLOBALDETECTORALIGNMENT_H

// Framework include files
#include "DD4hep/DetElement.h"
#include "DD4hep/GlobalAlignment.h"
#include <DD4hep/DetElement.h>
#include <DD4hep/GlobalAlignment.h>

// Forward declarations
class TGeoHMatrix;
Expand Down
128 changes: 82 additions & 46 deletions DDAlign/src/GlobalDetectorAlignment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@
#include <TGeoMatrix.h>
#include <TGeoManager.h>


#ifdef __GNUC__ // Disable some diagnostics.
#pragma GCC diagnostic ignored "-Wunused-function"
#endif

using LevelElements = std::vector<std::pair<int,dd4hep::DetElement> >;
using namespace dd4hep::align;
using dd4hep::printout;
using dd4hep::INFO;

/// Namespace for the AIDA detector description toolkit
namespace dd4hep {

Expand All @@ -43,63 +47,95 @@ namespace dd4hep {
*/
class GlobalAlignmentData : public NamedObject {
public:
GlobalAlignment global;
GlobalAlignment global;
std::vector<GlobalAlignment> volume_alignments;

public:
GlobalAlignmentData(const std::string& path) : NamedObject(path,"global-alignment") {
global = GlobalAlignment(path);
GlobalAlignmentData(const std::string& path)
: NamedObject(path, "global-alignment")
{
this->global = GlobalAlignment( path );
}
virtual ~GlobalAlignmentData() {
detail::destroyHandle (global);
virtual ~GlobalAlignmentData() {
detail::destroyHandle( this->global );
}
};
} /* End namespace Aligments */
} /* End namespace dd4hep */

using namespace dd4hep::align;
typedef std::vector<std::pair<int,dd4hep::DetElement> > LevelElements;
} /* End namespace Aligments */
} /* End namespace dd4hep */

DD4HEP_INSTANTIATE_HANDLE_NAMED(GlobalAlignmentData);

namespace {

static bool s_GlobalDetectorAlignment_debug = false;
static bool s_GlobalDetectorAlignment_debug = true;

GlobalAlignment _align(const GlobalAlignment& a, TGeoHMatrix* transform, bool check, double overlap) {
TGeoPhysicalNode* n = a.ptr();
if ( n ) {
TGeoMatrix* mm = n->GetNode()->GetMatrix();
TGeoPhysicalNode* node = a.ptr();
if ( node ) {
TGeoMatrix* mm = node->GetNode()->GetMatrix();
bool dbg = GlobalDetectorAlignment::debug();
if ( dbg ) {
printout(dd4hep::INFO,"Alignment","DELTA matrix of %s", n->GetName());
printout(INFO, "Alignment", "DELTA matrix of %s", node->GetName());
transform->Print();
dd4hep::printout(dd4hep::INFO,"Alignment","OLD matrix of %s", n->GetName());
printout(INFO, "Alignment", "OLD matrix of %s", node->GetName());
mm->Print();
}
std::vector<dd4hep::PlacedVolume> places;
for( int i = 0; i < node->GetLevel(); ++i )
places.emplace_back(node->GetNode(i+1));

transform->MultiplyLeft(mm); // orig * delta
n->Align(transform, 0, check, overlap);
if ( dbg ) {
dd4hep::printout(dd4hep::INFO,"Alignment","NEW matrix of %s", n->GetName());
n->GetNode()->GetMatrix()->Print();
node->Align(transform, 0, check, overlap);
if ( dbg ) {
printout(INFO, "Alignment", "NEW matrix of %s", node->GetName());
node->GetNode()->GetMatrix()->Print();
}

for( int i = 0; i < node->GetLevel(); ++i ) {
//const char *tag = " ";
dd4hep::PlacedVolume p = node->GetNode(i+1);
if ( nullptr == p->GetUserExtension() ) {
//tag = "SET ";
p->SetUserExtension(places[i]->GetUserExtension());
}
#if 0
printout(INFO, "Alignment", "_align(places): %s Path[%d]: %-24s %p <-> %p %s",
tag, i, p.name(), p.ptr(), places[i].ptr(), places[i].name());
tag = " ";
#endif
dd4hep::Volume v = p->GetVolume();
if ( nullptr == v->GetUserExtension() ) {
//tag = "SET ";
v->SetUserExtension(places[i].volume()->GetUserExtension());
}
#if 0
printout(INFO, "Alignment", "_align(volumes): %s Path[%d]: %-24s %p <-> %p %s",
tag, i, v.name(), v.ptr(), places[i].volume().ptr(), places[i].volume().name());
p.access();
v.access();
places[i].access();
places[i].volume().access();
#endif
}

/*
printout(dd4hep::INFO,"Alignment","Apply new relative matrix mother to daughter:");
printout(INFO, "Alignment", "Apply new relative matrix mother to daughter:");
transform->Print();
transform->MultiplyLeft(mm); // orig * delta
printout(dd4hep::INFO,"Alignment","With deltas %s ....", n->GetName());
printout(INFO, "Alignment", "With deltas %s ....", n->GetName());
transform->Print();
n->Align(transform, 0, check, overlap);
Position local, global = a.toGlobal(local);
cout << "Local:" << local << " Global: " << global
<< " and back:" << a.globalToLocal(global) << endl;
*/
return GlobalAlignment(n);
return GlobalAlignment(node);
}
dd4hep::except("GlobalDetectorAlignment", "Cannot align non existing physical node. [Invalid Handle]");
return { };
}

GlobalAlignment _alignment(const GlobalDetectorAlignment& det) {
dd4hep::DetElement::Object& e = det._data();
if ( !e.global_alignment.isValid() ) {
Expand All @@ -118,17 +154,17 @@ namespace {
int level = 0;
dd4hep::detail::tools::PlacementPath nodes;
dd4hep::detail::tools::ElementPath det_nodes;
dd4hep::detail::tools::placementPath(det,nodes);
dd4hep::detail::tools::elementPath(det,det_nodes);
dd4hep::detail::tools::placementPath(det, nodes);
dd4hep::detail::tools::elementPath(det, det_nodes);
/// std::cout << "Placement path:";
dd4hep::detail::tools::PlacementPath::const_reverse_iterator j=nodes.rbegin();
dd4hep::detail::tools::ElementPath::const_reverse_iterator k=det_nodes.rbegin();
for(; j!=nodes.rend(); ++j, ++level) {
//cout << "(" << level << ") " << (void*)((*j).ptr())
// << " " << string((*j)->GetName()) << " ";
if ( ::strcmp((*j).ptr()->GetName(),(*k).placement().ptr()->GetName()) ) {
if ( ::strcmp((*j).ptr()->GetName(), (*k).placement().ptr()->GetName()) ) {
//cout << "[DE]";
elements.emplace_back(level,*k);
elements.emplace_back(level, *k);
++k;
}
else {
Expand Down Expand Up @@ -166,7 +202,7 @@ bool GlobalDetectorAlignment::debug(bool value) {

/// Collect all placements from the detector element up to the world volume
void GlobalDetectorAlignment::collectNodes(std::vector<PlacedVolume>& nodes) {
detail::tools::placementPath(*this,nodes);
detail::tools::placementPath(*this, nodes);
}

/// Access to the alignment block
Expand All @@ -188,62 +224,62 @@ const std::vector<GlobalAlignment>& GlobalDetectorAlignment::volumeAlignments()

/// Align the PhysicalNode of the placement of the detector element (translation only)
GlobalAlignment GlobalDetectorAlignment::align(const Position& pos, bool chk, double overlap) {
return align(detail::matrix::_transform(pos),chk,overlap);
return align(detail::matrix::_transform(pos), chk, overlap);
}

/// Align the PhysicalNode of the placement of the detector element (rotation only)
GlobalAlignment GlobalDetectorAlignment::align(const RotationZYX& rot, bool chk, double overlap) {
return align(detail::matrix::_transform(rot),chk,overlap);
return align(detail::matrix::_transform(rot), chk, overlap);
}

/// Align the PhysicalNode of the placement of the detector element (translation + rotation)
GlobalAlignment GlobalDetectorAlignment::align(const Position& pos, const RotationZYX& rot, bool chk, double overlap) {
return align(detail::matrix::_transform(pos,rot),chk,overlap);
return align(detail::matrix::_transform(pos, rot), chk, overlap);
}

/// Align the physical node according to a generic Transform3D
GlobalAlignment GlobalDetectorAlignment::align(const Transform3D& transform, bool chk, double overlap) {
return align(detail::matrix::_transform(transform),chk,overlap);
return align(detail::matrix::_transform(transform), chk, overlap);
}

/// Align the physical node according to a generic TGeo matrix
GlobalAlignment GlobalDetectorAlignment::align(TGeoHMatrix* matrix, bool chk, double overlap) {
return _align(_alignment(*this),matrix,chk,overlap);
return _align(_alignment(*this), matrix, chk, overlap);
}

/// Align the PhysicalNode of the placement of the detector element (translation only)
GlobalAlignment GlobalDetectorAlignment::align(const std::string& elt_path, const Position& pos, bool chk, double overlap) {
return align(elt_path,detail::matrix::_transform(pos),chk,overlap);
return align(elt_path,detail::matrix::_transform(pos), chk, overlap);
}

/// Align the PhysicalNode of the placement of the detector element (rotation only)
GlobalAlignment GlobalDetectorAlignment::align(const std::string& elt_path, const RotationZYX& rot, bool chk, double overlap) {
return align(elt_path,detail::matrix::_transform(rot),chk,overlap);
return align(elt_path,detail::matrix::_transform(rot), chk, overlap);
}

/// Align the PhysicalNode of the placement of the detector element (translation + rotation)
GlobalAlignment
GlobalDetectorAlignment::align(const std::string& elt_path, const Position& pos, const RotationZYX& rot, bool chk, double overlap) {
return align(elt_path,detail::matrix::_transform(pos,rot),chk,overlap);
return align(elt_path,detail::matrix::_transform(pos, rot), chk, overlap);
}

/// Align the physical node according to a generic Transform3D
GlobalAlignment GlobalDetectorAlignment::align(const std::string& elt_path, const Transform3D& transform, bool chk, double overlap) {
return align(elt_path,detail::matrix::_transform(transform),chk,overlap);
return align(elt_path,detail::matrix::_transform(transform), chk, overlap);
}

/// Align the physical node according to a generic TGeo matrix
GlobalAlignment GlobalDetectorAlignment::align(const std::string& elt_path, TGeoHMatrix* matrix, bool chk, double overlap) {
if ( elt_path.empty() )
return _align(_alignment(*this),matrix,chk,overlap);
return _align(_alignment(*this), matrix, chk, overlap);
else if ( elt_path == placementPath() )
return _align(_alignment(*this),matrix,chk,overlap);
return _align(_alignment(*this), matrix, chk, overlap);
else if ( elt_path[0] == '/' ) {
GlobalAlignment a(elt_path);
volumeAlignments().emplace_back(a);
return _align(a,matrix,chk,overlap);
return _align(a, matrix, chk, overlap);
}
GlobalAlignment a(placementPath()+'/'+elt_path);
GlobalAlignment a(placementPath() + '/' + elt_path);
volumeAlignments().emplace_back(a);
return _align(a,matrix,chk,overlap);
return _align(a, matrix, chk, overlap);
}
Loading

0 comments on commit 9dab9c0

Please sign in to comment.