DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
GlobalAlignmentWriter.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 includes
18 #include <DDAlign/AlignmentTags.h>
19 
20 #include <DD4hep/Detector.h>
21 #include <DD4hep/Printout.h>
22 #include <DD4hep/MatrixHelpers.h>
24 #include <XML/DocumentHandler.h>
25 
26 #include <TGeoMatrix.h>
27 
28 // C/C++ include files
29 #include <stdexcept>
30 
31 using namespace dd4hep::align;
32 namespace xml = dd4hep::xml;
33 
35 GlobalAlignmentWriter::GlobalAlignmentWriter(Detector& description) : m_detDesc(description)
36 {
38  if ( m_cache ) m_cache->addRef();
39 }
40 
43  if ( m_cache ) m_cache->release();
44 }
45 
48  xml::Element e(0), placement(0), elt = xml::Element(doc,_ALU(detelement));
49  std::string path = element.placementPath();
50  GlobalAlignment a = element->global_alignment;
52 
53  elt.setAttr(_ALU(path),element.path());
54  if ( a.isValid() ) {
55  addNode(elt,a);
56  }
57  const std::vector<GlobalAlignment>& vol_alignments = det.volumeAlignments();
58  for(const auto& alignment : vol_alignments ) {
59  e = xml::Element(doc,_U(volume));
60  e.setAttr(_ALU(path), alignment->GetName());
61  addNode(e, alignment);
62  elt.append(e);
63  }
64  return elt;
65 }
66 
69  TGeoNode* n = a->GetNode();
70  TGeoHMatrix mat(a->GetOriginalMatrix()->Inverse());
71  mat.Multiply(n->GetMatrix());
72  const Double_t* t = mat.GetTranslation();
73  xml::Element placement = xml::Element(elt.document(),_U(comment));
74  placement.setAttr(_ALU(placement),a->GetName());
75  elt.append(placement);
76 
77  printout(INFO,"GlobalAlignmentWriter","Write Delta constants for %s",a->GetName());
78  //mat.Print();
79  if ( fabs(t[0]) > std::numeric_limits<double>::epsilon() ||
80  fabs(t[1]) > std::numeric_limits<double>::epsilon() ||
81  fabs(t[2]) > std::numeric_limits<double>::epsilon() ) {
82  xml::Element e = xml::Element(elt.document(),_U(position));
83  e.setAttr(_U(x),_toString(t[0]/dd4hep::mm,"%f*mm"));
84  e.setAttr(_U(y),_toString(t[1]/dd4hep::mm,"%f*mm"));
85  e.setAttr(_U(z),_toString(t[2]/dd4hep::mm,"%f*mm"));
86  elt.append(e);
87  }
88  if ( mat.IsRotation() ) {
90  if ( fabs(rot.X()) > std::numeric_limits<double>::epsilon() ||
91  fabs(rot.Y()) > std::numeric_limits<double>::epsilon() ||
92  fabs(rot.Z()) > std::numeric_limits<double>::epsilon() ) {
93 
94  xml::Element e = xml::Element(elt.document(),_U(rotation));
95  // Don't know why the angles have the wrong sign....
96  rot *= -1;
97  e.setAttr(_U(x),_toString(rot.X()/dd4hep::rad,"%f*rad"));
98  e.setAttr(_U(y),_toString(rot.Y()/dd4hep::rad,"%f*rad"));
99  e.setAttr(_U(z),_toString(rot.Z()/dd4hep::rad,"%f*rad"));
100  elt.append(e);
101  }
102  }
103 }
104 
107  xml::Element elt(0);
108  if ( element.isValid() ) {
109  const DetElement::Children& c = element.children();
110  GlobalAlignment alignment = element->global_alignment;
111  if ( alignment.isValid() ) elt = createElement(doc,element);
112  for (DetElement::Children::const_iterator i = c.begin(); i != c.end(); ++i) {
113  xml::Element daughter = scan(doc, (*i).second);
114  if ( daughter ) {
115  (elt ? (elt) : (elt=createElement(doc,element))).append(daughter);
116  }
117  }
118  }
119  return elt;
120 }
121 
123 xml::Document GlobalAlignmentWriter::dump(DetElement top, bool enable_transactions) const {
125  xml::Document doc = docH.create("alignment", docH.defaultComment());
126  xml::Element elt(0), elements(0), root = doc.root();
127  root.append(elements = xml::Element(doc, _ALU(detelements)));
128  if ( enable_transactions ) root.append(xml::Element(doc,_ALU(open_transaction)));
129  if ( (elt=scan(doc,top)) ) elements.append(elt);
130  if ( enable_transactions ) root.append(xml::Element(doc,_ALU(close_transaction)));
131  return doc;
132 }
133 
135 long GlobalAlignmentWriter::write(xml::Document doc, const std::string& output) const {
137  return docH.output(doc, output);
138 }
dd4hep::DetElement::children
const Children & children() const
Access to the list of children.
Definition: DetElement.cpp:207
dd4hep::DetElement::path
const std::string & path() const
Path of the detector element (not necessarily identical to placement path!)
Definition: DetElement.cpp:158
dd4hep::xml::DocumentHandler::create
Document create(const char *tag, const char *comment=0) const
Create new XML document by parsing empty xml buffer.
Definition: DocumentHandler.cpp:681
dd4hep::xml::Element::document
Document document() const
Access the hosting document handle of this DOM element.
Definition: XMLElements.cpp:1089
dd4hep::align::GlobalDetectorAlignment
GlobalDetectorAlignment. DetElement Handle supporting alignment operations.
Definition: GlobalDetectorAlignment.h:35
dd4hep::xml::Element::append
void append(Handle_t handle) const
Append a new element to the existing tree.
Definition: XMLElements.h:839
dd4hep::align::GlobalAlignment
Main handle class to hold a TGeo alignment object of type TGeoPhysicalNode.
Definition: GlobalAlignment.h:38
dd4hep::align::GlobalAlignmentWriter::m_cache
GlobalAlignmentCache * m_cache
Reference to the alignment cache.
Definition: GlobalAlignmentWriter.h:41
MatrixHelpers.h
Detector.h
dd4hep::XYZAngles
ROOT::Math::XYZVector XYZAngles
Definition: Objects.h:83
dd4hep::align::GlobalAlignmentCache::release
int release()
Release object. If reference count goes to NULL, automatic deletion is triggered.
Definition: GlobalAlignmentCache.cpp:63
dd4hep::align::GlobalAlignmentWriter::write
long write(xml::Document doc, const std::string &output) const
Write the XML document structure to a file.
Definition: GlobalAlignmentWriter.cpp:135
dd4hep::align::GlobalAlignmentCache::addRef
int addRef()
Add reference count.
Definition: GlobalAlignmentCache.cpp:58
dd4hep::Handle::isValid
bool isValid() const
Check the validity of the object held by the handle.
Definition: Handle.h:128
DetectorInterna.h
dd4hep::_toString
std::string _toString(bool value)
String conversions: boolean value to string.
Definition: Handle.cpp:332
_ALU
#define _ALU(a)
Definition: AlignmentTags.h:50
epsilon
const double epsilon
Definition: test_cellid_position_converter.cpp:41
DocumentHandler.h
GlobalAlignmentCache.h
dd4hep::DetElement
Handle class describing a detector element.
Definition: DetElement.h:188
dd4hep::align::GlobalAlignmentWriter::dump
xml::Document dump(DetElement element, bool enable_transactions=false) const
Dump one full DetElement subtree into a newly created document.
Definition: GlobalAlignmentWriter.cpp:123
GlobalAlignmentWriter.h
dd4hep::xml
Namespace for the AIDA detector description toolkit supporting XML utilities.
Definition: ConditionsTags.h:27
_U
#define _U(a)
Definition: Tags.h:23
dd4hep::DetElement::placementPath
const std::string & placementPath() const
Access to the full path to the placed object.
Definition: DetElement.cpp:85
dd4hep::xml::DocumentHandler::defaultComment
static std::string defaultComment()
Default comment string.
Definition: DocumentHandler.cpp:630
dd4hep::detail::matrix::_xyzAngles
XYZAngles _xyzAngles(const double *matrix)
Convert a 3x3 rotation matrix to XYZAngles.
Definition: MatrixHelpers.cpp:195
dd4hep::xml::Document
Class supporting the basic functionality of an XML document.
Definition: XMLElements.h:697
dd4hep::align::GlobalAlignmentCache
Class caching all known alignment operations for one Detector instance.
Definition: GlobalAlignmentCache.h:41
dd4hep::xml::DocumentHandler::output
virtual int output(Document doc, const std::string &fname) const
Write xml document to output file (stdout if file name empty)
Definition: DocumentHandler.cpp:396
dd4hep::DetElement::Children
std::map< std::string, DetElement > Children
Definition: DetElement.h:206
dd4hep::xml::DocumentHandler
Class supporting to read and parse XML documents.
Definition: DocumentHandler.h:39
dd4hep::xml::Element
User abstraction class to manipulate XML elements within a document.
Definition: XMLElements.h:769
dd4hep::Detector::extension
IFACE * extension(bool alert=true) const
Access extension element by the type.
Definition: Detector.h:342
dd4hep::align::GlobalAlignmentWriter::~GlobalAlignmentWriter
virtual ~GlobalAlignmentWriter()
Standard destructor.
Definition: GlobalAlignmentWriter.cpp:42
dd4hep::xml::Element::setAttr
Attribute setAttr(const XmlChar *nam, const T &val) const
Set single attribute.
Definition: XMLElements.h:900
det
DetElement::Object * det
Definition: AlignmentsCalculator.cpp:66
dd4hep::align::GlobalAlignmentWriter::GlobalAlignmentWriter
GlobalAlignmentWriter(Detector &description)
Initializing Constructor.
Definition: GlobalAlignmentWriter.cpp:35
dd4hep::Detector
The main interface to the dd4hep detector description package.
Definition: Detector.h:90
AlignmentTags.h
dd4hep::align::GlobalAlignmentWriter::scan
xml::Element scan(xml::Document doc, DetElement element) const
Scan one DetElement structure and return an XML element containing the alignment in this subtree.
Definition: GlobalAlignmentWriter.cpp:106
GlobalDetectorAlignment.h
dd4hep::align::GlobalAlignmentWriter::createElement
xml::Element createElement(xml::Document doc, DetElement element) const
Create the element corresponding to one single detector element without children.
Definition: GlobalAlignmentWriter.cpp:47
dd4hep::align::GlobalAlignmentWriter::addNode
void addNode(xml::Element elt, GlobalAlignment a) const
Add single alignment node to the XML document.
Definition: GlobalAlignmentWriter.cpp:68
Printout.h
dd4hep::align
Namespace for implementation details of the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:31
dd4hep::xml::Document::root
Handle_t root() const
Access the ROOT eleemnt of the DOM document.
Definition: XMLElements.cpp:1040