DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
GlobalAlignmentParser.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/Path.h>
17 #include <DD4hep/Mutex.h>
18 #include <DD4hep/Printout.h>
19 #include <XML/Conversions.h>
20 #include <XML/XMLParsers.h>
21 #include <XML/DocumentHandler.h>
22 #include <DD4hep/DetectorTools.h>
24 
25 #include <DDAlign/AlignmentTags.h>
29 
30 // C/C++ include files
31 #include <stdexcept>
32 
33 namespace dd4hep {
34 
35  namespace {
36 
38  class alignment;
39  class detelement;
40  class include_file;
41  class volume;
42  class rotation;
43  class position;
44  class pivot;
45  class alignment_delta;
46  class debug;
47  }
48 
50  template <> void Converter<debug>::operator()(xml_h seq) const;
51  template <> void Converter<volume>::operator()(xml_h seq) const;
52  template <> void Converter<alignment>::operator()(xml_h seq) const;
53  template <> void Converter<detelement>::operator()(xml_h seq) const;
54  template <> void Converter<include_file>::operator()(xml_h seq) const;
55 
56  static long setup_Alignment(Detector& description, const xml_h& e);
57 }
58 
59 using namespace dd4hep::align;
61 
62 
69 template <> void dd4hep::Converter<dd4hep::debug>::operator()(xml_h e) const {
70  bool value = e.attr<bool>(_U(value));
72 }
73 
89 template <> void dd4hep::Converter<dd4hep::volume>::operator()(xml_h e) const {
90  Delta val;
91  GlobalAlignmentStack* stack = _option<GlobalAlignmentStack>();
92  std::pair<DetElement,std::string>* elt = _param<std::pair<DetElement,std::string> >();
93  std::string subpath = e.attr<std::string>(_ALU(path));
94  bool reset = e.hasAttr(_ALU(reset)) ? e.attr<bool>(_ALU(reset)) : true;
95  bool reset_dau = e.hasAttr(_ALU(reset_children)) ? e.attr<bool>(_ALU(reset_children)) : true;
96  bool check = e.hasAttr(_ALU(check_overlaps));
97  bool check_val = check ? e.attr<bool>(_ALU(check_overlaps)) : false;
98  bool overlap = e.hasAttr(_ALU(overlap));
99  double ovl = overlap ? e.attr<double>(_ALU(overlap)) : 0.001;
100  std::string elt_place = elt->first.placementPath();
101  std::string placement = subpath[0]=='/' ? subpath : elt_place + "/" + subpath;
102 
103  printout(INFO,"Alignment<vol>"," path:%s placement:%s reset:%s children:%s",
104  subpath.c_str(), placement.c_str(), yes_no(reset), yes_no(reset_dau));
105 
106  xml::parse(e,val);
107  if ( val.flags ) val.flags |= GlobalAlignmentStack::MATRIX_DEFINED;
108  if ( overlap ) val.flags |= GlobalAlignmentStack::OVERLAP_DEFINED;
109  if ( reset ) val.flags |= GlobalAlignmentStack::RESET_VALUE;
110  if ( reset_dau ) val.flags |= GlobalAlignmentStack::RESET_CHILDREN;
111  if ( check ) val.flags |= GlobalAlignmentStack::CHECKOVL_DEFINED;
112  if ( check_val ) val.flags |= GlobalAlignmentStack::CHECKOVL_VALUE;
113 
114  dd4hep_ptr<StackEntry> entry(new StackEntry(elt->first,placement,val,ovl));
115  stack->insert(entry);
116  std::pair<DetElement,std::string> vol_param(elt->first,subpath);
117  xml_coll_t(e,_U(volume)).for_each(Converter<volume>(description,&vol_param));
118 }
119 
136 template <> void dd4hep::Converter<dd4hep::detelement>::operator()(xml_h e) const {
137  DetElement det(_param<DetElement::Object>());
138  GlobalAlignmentStack* stack = _option<GlobalAlignmentStack>();
139  std::string path = e.attr<std::string>(_ALU(path));
140  bool check = e.hasAttr(_ALU(check_overlaps));
141  bool check_val = check ? e.attr<bool>(_ALU(check_overlaps)) : false;
142  bool reset = e.hasAttr(_ALU(reset)) ? e.attr<bool>(_ALU(reset)) : false;
143  bool reset_dau = e.hasAttr(_ALU(reset_children)) ? e.attr<bool>(_ALU(reset_children)) : false;
144  bool overlap = e.hasAttr(_ALU(overlap));
145  double ovl = overlap ? e.attr<double>(_ALU(overlap)) : 0.001;
146  DetElement elt = detail::tools::findDaughterElement(det,path);
147  std::string placement = elt.isValid() ? elt.placementPath() : std::string("-----");
148 
149  if ( !elt.isValid() ) {
150  except("GlocalAlignmentParser",
151  "dd4hep: DetElement %s has no child: %s [No such child]", det.path().c_str(), path.c_str());
152  }
153 
154  Delta delta;
155  xml::parse(e, delta);
156  if ( delta.flags ) {
158  reset = reset_dau = true;
159  }
160  if ( overlap ) delta.flags |= GlobalAlignmentStack::OVERLAP_DEFINED;
161  if ( check ) delta.flags |= GlobalAlignmentStack::CHECKOVL_DEFINED;
162  if ( reset ) delta.flags |= GlobalAlignmentStack::RESET_VALUE;
163  if ( reset_dau ) delta.flags |= GlobalAlignmentStack::RESET_CHILDREN;
164  if ( check_val ) delta.flags |= GlobalAlignmentStack::CHECKOVL_VALUE;
165 
166  printout(INFO,
167  "Alignment<det>","path:%s [%s] placement:%s matrix:%s reset:%s children:%s",
168  path.c_str(),
169  elt.isValid() ? elt.path().c_str() : "-----",
170  placement.c_str(),
171  yes_no(delta.checkFlag(GlobalAlignmentStack::MATRIX_DEFINED)),
172  yes_no(reset), yes_no(reset_dau));
173 
174  dd4hep_ptr<StackEntry> entry(new StackEntry(elt,placement,delta,ovl));
175  stack->insert(entry);
176 
177  std::pair<DetElement, std::string> vol_param(elt,"");
178  xml_coll_t(e,_U(volume)).for_each(Converter<volume>(description,&vol_param,optional));
179  xml_coll_t(e,_ALU(detelement)).for_each(Converter<detelement>(description,elt.ptr(),optional));
180  xml_coll_t(e,_U(include)).for_each(Converter<include_file>(description,elt.ptr(),optional));
181 }
182 
194 template <> void dd4hep::Converter<dd4hep::include_file>::operator()(xml_h element) const {
195  xml::DocumentHolder doc(xml::DocumentHandler().load(element, element.attr_value(_U(ref))));
196  xml_h node = doc.root();
197  std::string tag = node.tag();
198  if ( tag == "alignment" )
199  Converter<alignment>(description,param,optional)(node);
200  else if ( tag == "global_alignment" )
201  setup_Alignment(description, node);
202  else if ( tag == "detelement" )
203  Converter<detelement>(description,param,optional)(node);
204  else if ( tag == "subdetectors" || tag == "detelements" )
205  xml_coll_t(node,_ALU(detelements)).for_each(Converter<detelement>(description,param,optional));
206  else
207  except("GlocalAlignmentParser", "Undefined tag name in XML structure: %s XML parsing abandoned.", tag.c_str());
208 }
209 
222 template <> void dd4hep::Converter<dd4hep::alignment>::operator()(xml_h e) const {
225  xml_coll_t(e,_ALU(debug)).for_each(Converter<debug>(description,param,optional));
226  xml_coll_t(e,_ALU(detelement)).for_each(Converter<detelement>(description,param,optional));
227  xml_coll_t(e,_ALU(detelements)).for_each(_ALU(detelement),Converter<detelement>(description,param,optional));
228  xml_coll_t(e,_ALU(subdetectors)).for_each(_ALU(detelement),Converter<detelement>(description,param,optional));
229  xml_coll_t(e,_U(include)).for_each(Converter<include_file>(description,param,optional));
230 }
231 
238 long dd4hep::setup_Alignment(dd4hep::Detector& description, const xml_h& e) {
239  static dd4hep::dd4hep_mutex_t s_mutex;
240  dd4hep::dd4hep_lock_t lock(s_mutex);
241  bool open_trans = e.hasChild(_ALU(open_transaction));
242  bool close_trans = e.hasChild(_ALU(close_transaction));
243 
246  if ( open_trans ) {
248  except("GlobalAlignment","Request to open a second alignment transaction stack -- not allowed!");
249  }
251  }
252  if ( !GlobalAlignmentStack::exists() ) {
253  printout(ERROR,"GlobalAlignment",
254  "Request process global alignments without cache.");
255  printout(ERROR,"GlobalAlignment",
256  "Call plugin DD4hep_GlobalAlignmentInstall first OR add XML tag <open_transaction/>");
257  except("GlobalAlignment","Request process global alignments without cache.");
258  }
260  (dd4hep::Converter<dd4hep::alignment>(description,description.world().ptr(),&stack))(e);
261  if ( close_trans ) {
262  cache->commit(stack);
264  }
266  xml_elt_t elt(e);
267  Path uri = elt.document().uri();
268  printout(INFO,"GlobalAlignment","+++ Successfully parsed XML: %s",
269  uri.filename().c_str());
270  }
271  return 1;
272 }
273 DECLARE_XML_DOC_READER(global_alignment,setup_Alignment)
274 
275 
281 static long install_Alignment(dd4hep::Detector& description, int, char**) {
282  GlobalAlignmentCache::install(description);
283  return 1;
284 }
285 DECLARE_APPLY(DD4hep_GlobalAlignmentInstall,install_Alignment)
dd4hep::align::GlobalAlignmentStack::get
static GlobalAlignmentStack & get()
Static client accessor.
Definition: GlobalAlignmentStack.cpp:95
dd4hep::Detector::world
virtual DetElement world() const =0
Return reference to the top-most (world) detector element.
dd4hep::xml::Handle_t::tag
std::string tag() const
Text access to the element's tag.
Definition: XMLElements.h:414
dd4hep::align::GlobalAlignmentStack::CHECKOVL_DEFINED
@ CHECKOVL_DEFINED
Definition: GlobalAlignmentStack.h:39
dd4hep::align::GlobalAlignmentStack
Alignment Stack object definition.
Definition: GlobalAlignmentStack.h:34
dd4hep::align::GlobalDetectorAlignment::debug
static bool debug()
Access debugging flag.
Definition: GlobalDetectorAlignment.cpp:192
Path.h
Detector.h
dd4hep::align::GlobalAlignmentStack::create
static void create()
Create an alignment stack instance. The creation of a second instance will be refused.
Definition: GlobalAlignmentStack.cpp:102
dd4hep::debug
std::size_t debug(const std::string &src, const std::string &msg)
Definition: RootDictionary.h:64
delta
const Delta * delta
Definition: AlignmentsCalculator.cpp:67
dd4hep::align::GlobalAlignmentStack::insert
bool insert(const std::string &full_path, dd4hep_ptr< StackEntry > &new_entry)
Add a new entry to the cache. The key is the placement path.
Definition: GlobalAlignmentStack.cpp:124
DECLARE_APPLY
#define DECLARE_APPLY(name, func)
Definition: Factories.h:281
_ALU
#define _ALU(a)
Definition: AlignmentTags.h:50
dd4hep::dd4hep_mutex_t
Do-nothing compatibility std::unique_ptr emulation for cxx-98.
Definition: Mutex.h:30
dd4hep::xml::Handle_t
Class to easily access the properties of single XmlElements.
Definition: XMLElements.h:380
dd4hep::Path::filename
Path filename() const
The file name of the path.
Definition: Path.cpp:211
dd4hep::detail::tools::findDaughterElement
DetElement findDaughterElement(DetElement parent, const std::string &subpath)
Find DetElement as child of a parent by its relative or absolute path.
Definition: DetectorTools.cpp:219
xml_coll_t
dd4hep::xml::Collection_t xml_coll_t
Definition: ConditionsRepository.cpp:35
dd4hep::xml::parse
void parse(Handle_t e, RotationZYX &rot)
Convert rotation XML objects to dd4hep::RotationZYX.
Definition: XMLParsers.cpp:48
DocumentHandler.h
GlobalAlignmentCache.h
dd4hep::xml::Handle_t::hasChild
bool hasChild(const XmlChar *tag) const
Check the existence of a child with a given tag name.
Definition: XMLElements.cpp:764
dd4hep::dd4hep_lock_t
Do-nothing compatibility std::unique_ptr emulation for cxx-98.
Definition: Mutex.h:43
dd4hep::align::GlobalAlignmentStack::CHECKOVL_VALUE
@ CHECKOVL_VALUE
Definition: GlobalAlignmentStack.h:40
dd4hep::xml::Handle_t::attr_value
const XmlChar * attr_value(const Attribute attr) const
Access attribute value by the attribute (throws exception if not present)
Definition: XMLElements.cpp:864
dd4hep::xml::Collection_t::for_each
void for_each(T oper) const
Loop processor using function object.
Definition: XMLElements.h:667
XMLParsers.h
dd4hep::align::GlobalAlignmentCache::commit
void commit(GlobalAlignmentStack &stack)
Close existing transaction stack and apply all alignments.
Definition: GlobalAlignmentCache.cpp:167
DetectorTools.h
_U
#define _U(a)
Definition: Tags.h:23
dd4hep::xml::DocumentHolder
Class supporting the basic functionality of an XML document including ownership.
Definition: XMLElements.h:741
DECLARE_XML_DOC_READER
#define DECLARE_XML_DOC_READER(name, func)
Definition: Factories.h:316
dd4hep::align::GlobalAlignmentStack::RESET_CHILDREN
@ RESET_CHILDREN
Definition: GlobalAlignmentStack.h:42
StackEntry
GlobalAlignmentStack::StackEntry StackEntry
Definition: GlobalAlignmentParser.cpp:60
dd4hep::align::GlobalAlignmentCache
Class caching all known alignment operations for one Detector instance.
Definition: GlobalAlignmentCache.h:41
Mutex.h
DetFactoryHelper.h
GlobalAlignmentStack.h
dd4hep::align::GlobalAlignmentStack::MATRIX_DEFINED
@ MATRIX_DEFINED
Definition: GlobalAlignmentStack.h:38
dd4hep::xml::DocumentHandler
Class supporting to read and parse XML documents.
Definition: DocumentHandler.h:39
dd4hep::Handle::ptr
T * ptr() const
Access to the held object.
Definition: Handle.h:153
Conversions.h
dd4hep::xml::Element
User abstraction class to manipulate XML elements within a document.
Definition: XMLElements.h:769
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
det
DetElement::Object * det
Definition: AlignmentsCalculator.cpp:66
dd4hep::align::GlobalAlignmentStack::RESET_VALUE
@ RESET_VALUE
Definition: GlobalAlignmentStack.h:41
dd4hep::Detector
The main interface to the dd4hep detector description package.
Definition: Detector.h:90
AlignmentTags.h
dd4hep::xml::Handle_t::hasAttr
bool hasAttr(const XmlChar *t) const
Check for the existence of a named attribute.
Definition: XMLElements.cpp:673
dd4hep::align::GlobalAlignmentStack::exists
static bool exists()
Check existence of alignment stack.
Definition: GlobalAlignmentStack.cpp:110
GlobalDetectorAlignment.h
dd4hep::align::GlobalAlignmentStack::OVERLAP_DEFINED
@ OVERLAP_DEFINED
Definition: GlobalAlignmentStack.h:37
dd4hep::align::GlobalAlignmentStack::StackEntry
Stack entry definition.
Definition: GlobalAlignmentStack.h:52
dd4hep::xml::Handle_t::attr
T attr(const Attribute a) const
Access typed attribute value by the XmlAttr.
Definition: XMLElements.h:454
Printout.h
dd4hep::align::GlobalAlignmentStack::release
void release()
Clear data content and remove the slignment stack.
Definition: GlobalAlignmentStack.cpp:115
dd4hep::align
Namespace for implementation details of the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:31
dd4hep::Path
Path handling class.
Definition: Path.h:45
dd4hep::align::GlobalAlignmentCache::install
static GlobalAlignmentCache * install(Detector &description)
Create and install a new instance tree.
Definition: GlobalAlignmentCache.cpp:72