DD4hep - The AIDA detector description toolkit for high energy physics experiments
DD4hep  Rev:Unversioneddirectory
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GlobalAlignmentWriter.cpp
Go to the documentation of this file.
1 // $Id: $
2 //==========================================================================
3 // AIDA Detector description implementation for LCD
4 //--------------------------------------------------------------------------
5 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
6 // All rights reserved.
7 //
8 // For the licensing terms see $DD4hepINSTALL/LICENSE.
9 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
10 //
11 // Author : M.Frank
12 //
13 //==========================================================================
14 
15 // Framework includes
19 #include "DDAlign/AlignmentTags.h"
20 
21 #include "DD4hep/LCDD.h"
22 #include "DD4hep/Printout.h"
23 #include "DD4hep/MatrixHelpers.h"
25 #include "XML/DocumentHandler.h"
26 
27 #include "TGeoMatrix.h"
28 
29 // C/C++ include files
30 #include <stdexcept>
31 
34 using namespace DD4hep::Alignments;
35 using namespace DD4hep;
36 using namespace std;
37 
40  : m_lcdd(lcdd)
41 {
42  m_cache = lcdd.extension<Alignments::GlobalAlignmentCache>();
43  if ( m_cache ) m_cache->addRef();
44 }
45 
48  if ( m_cache ) m_cache->release();
49 }
50 
53  XML::Element e(0), placement(0), elt = XML::Element(doc,_ALU(detelement));
54  string path = element.placementPath();
55  GlobalAlignment a = element->global_alignment;
56  GlobalDetectorAlignment det(element);
57  const vector<GlobalAlignment>& va = det.volumeAlignments();
58 
59  elt.setAttr(_ALU(path),element.path());
60  if ( a.isValid() ) {
61  addNode(elt,a);
62  }
63  for(vector<GlobalAlignment>::const_iterator i=va.begin(); i!=va.end();++i) {
64  e = XML::Element(doc,_U(volume));
65  e.setAttr(_ALU(path),(*i)->GetName());
66  addNode(e,*i);
67  elt.append(e);
68  }
69  return elt;
70 }
71 
74  TGeoNode* n = a->GetNode();
75  TGeoHMatrix mat(a->GetOriginalMatrix()->Inverse());
76  mat.Multiply(n->GetMatrix());
77  const Double_t* t = mat.GetTranslation();
78  XML::Element placement = XML::Element(elt.document(),_U(comment));
79  placement.setAttr(_ALU(placement),a->GetName());
80  elt.append(placement);
81 
82  printout(INFO,"GlobalAlignmentWriter","Write Delta constants for %s",a->GetName());
83  //mat.Print();
84  if ( fabs(t[0]) > numeric_limits<double>::epsilon() ||
85  fabs(t[1]) > numeric_limits<double>::epsilon() ||
86  fabs(t[2]) > numeric_limits<double>::epsilon() ) {
87  XML::Element e = XML::Element(elt.document(),_U(position));
88  e.setAttr(_U(x),t[0]);
89  e.setAttr(_U(y),t[1]);
90  e.setAttr(_U(z),t[2]);
91  elt.append(e);
92  }
93  if ( mat.IsRotation() ) {
95  if ( fabs(rot.X()) > numeric_limits<double>::epsilon() ||
96  fabs(rot.Y()) > numeric_limits<double>::epsilon() ||
97  fabs(rot.Z()) > numeric_limits<double>::epsilon() ) {
98 
99  XML::Element e = XML::Element(elt.document(),_U(rotation));
100  // Don't know why the angles have the wrong sign....
101  rot *= -1;
102  e.setAttr(_U(x),rot.X());
103  e.setAttr(_U(y),rot.Y());
104  e.setAttr(_U(z),rot.Z());
105  elt.append(e);
106  }
107  }
108 }
109 
112  XML::Element elt(0);
113  if ( element.isValid() ) {
114  const DetElement::Children& c = element.children();
115  GlobalAlignment alignment = element->global_alignment;
116  if ( alignment.isValid() ) elt = createElement(doc,element);
117  for (DetElement::Children::const_iterator i = c.begin(); i != c.end(); ++i) {
118  XML::Element daughter = scan(doc, (*i).second);
119  if ( daughter ) {
120  (elt ? (elt) : (elt=createElement(doc,element))).append(daughter);
121  }
122  }
123  }
124  return elt;
125 }
126 
128 XML::Document GlobalAlignmentWriter::dump(DetElement top, bool enable_transactions) const {
129  const char comment[] = "\n"
130  " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
131  " ++++ DD4hep generated alignment file using the ++++\n"
132  " ++++ DD4hep Detector description XML generator. ++++\n"
133  " ++++ ++++\n"
134  " ++++ Parser:"
136  " ++++\n"
137  " ++++ ++++\n"
138  " ++++ M.Frank CERN/LHCb ++++\n"
139  " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n ";
141  XML::Document doc = docH.create("alignment", comment);
142  XML::Element elt(0), elements(0), root = doc.root();
143  root.append(elements = XML::Element(doc, _ALU(detelements)));
144  if ( enable_transactions ) root.append(XML::Element(doc,_ALU(open_transaction)));
145  if ( (elt=scan(doc,top)) ) elements.append(elt);
146  if ( enable_transactions ) root.append(XML::Element(doc,_ALU(close_transaction)));
147  return doc;
148 }
149 
151 long GlobalAlignmentWriter::write(XML::Document doc, const string& output) const {
153  return docH.output(doc, output);
154 }
Document create(const char *tag, const char *comment=0) const
Create new XML document by parsing empty xml buffer.
virtual int output(Document doc, const std::string &fname) const
Write xml document to output file (stdout if file name empty)
Position XYZAngles
Definition: MatrixHelpers.h:31
bool isValid() const
Check the validity of the object held by the handle.
Definition: Handle.h:124
Main handle class to hold a TGeo alignment object of type TGeoPhysicalNode.
GlobalDetectorAlignment. DetElement Handle supporting alignment operations.
virtual ~GlobalAlignmentWriter()
Standard destructor.
#define _U(a)
Definition: XMLTags.h:27
void append(Handle_t handle) const
Append a new element to the existing tree.
Definition: XMLElements.h:752
User abstraction class to manipulate XML elements within a document.
Definition: XMLElements.h:686
GlobalAlignmentWriter(LCDD &lcdd)
Initializing Constructor.
return e
Definition: Volumes.cpp:297
Handle_t root() const
Access the ROOT eleemnt of the DOM document.
XYZAngles _XYZangles(const double *matrix)
Convert a 3x3 rotation matrix to XYZAngles
DD4hep::Geometry::DetElement DetElement
#define XML_IMPLEMENTATION_TYPE
Definition: config.h:51
long write(XML::Document doc, const std::string &output) const
Write the XML document structure to a file.
XML::Element scan(XML::Document doc, DetElement element) const
Scan one DetElement structure and return an XML element containing the alignment in this subtree...
ROOT::Math::XYZVector Position
Definition: Objects.h:75
GlobalAlignmentCache * m_cache
Reference to the alignment cache.
int release()
Release object. If reference count goes to NULL, automatic deletion is triggered. ...
Document document() const
Access the hosting document handle of this DOM element.
Class caching all known alignment operations for one LCDD instance.
Attribute setAttr(const XmlChar *nam, const T &val) const
Set single attribute.
Definition: XMLElements.h:801
ROOT::Math::Translation3D Translation3D
Definition: Objects.h:112
#define _ALU(a)
Definition: AlignmentTags.h:52
Class supporting the basic functionality of an XML document.
Definition: XMLElements.h:617
void addNode(XML::Element elt, GlobalAlignment a) const
Add single alignment node to the XML document.
std::map< std::string, DetElement > Children
Definition: Detector.h:202
XML::Element createElement(XML::Document doc, DetElement element) const
Create the element corresponding to one single detector element without children. ...
int printout(PrintLevel severity, const char *src, const char *fmt,...)
Calls the display action with a given severity level.
Definition: Printout.cpp:111
Class supporting to read and parse XML documents.
std::vector< GlobalAlignment > & volumeAlignments()
Alignment entries for lower level volumes, which are NOT attached to daughter DetElements.
XML::Document dump(DetElement element, bool enable_transactions=false) const
Dump one full DetElement subtree into a newly created document.