DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
ConditionsParser.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 <XML/Conversions.h>
17 #include <XML/XMLElements.h>
18 #include <XML/DocumentHandler.h>
19 #include <DD4hep/Printout.h>
20 #include <DD4hep/DetectorTools.h>
22 
23 #include <DDCond/ConditionsTags.h>
24 #include <DDCond/ConditionsEntry.h>
27 
29 namespace dd4hep {
30 
31  namespace {
33  class include;
34  class arbitrary;
35  class conditions;
36 
37  class iov;
38  class iov_type;
39  class manager;
40  class repository;
41  class detelement;
42  class align_conditions;
43  class align_arbitrary;
45  class value;
46  class mapping;
47  class sequence;
48  class alignment;
49  class position;
50  class rotation;
51  class pivot;
52  }
53  template <> void Converter<include>::operator()(xml_h seq) const;
54  template <> void Converter<arbitrary>::operator()(xml_h seq) const;
55  template <> void Converter<conditions>::operator()(xml_h seq) const;
56 }
57 
58 using namespace dd4hep::cond;
59 
61 namespace dd4hep {
62 
64  struct ConversionArg {
68  : detector(det), stack(stk)
69  {
70  }
71  };
72 
74  std::string _getValidity(xml_h elt) {
75  if ( !elt.ptr() )
76  return "Infinite";
77  else if ( !elt.hasAttr(_UC(validity)) )
78  return _getValidity(elt.parent());
79  return elt.attr<std::string>(_UC(validity));
80  }
81 
83  DetElement _getDetector(void* param, xml_h e) {
84  ConversionArg* arg = static_cast<ConversionArg*>(param);
85  DetElement detector = arg ? arg->detector : DetElement();
86  std::string subpath = e.hasAttr(_U(path)) ? e.attr<std::string>(_U(path)) : std::string();
87  return subpath.empty() ? detector : detail::tools::findDaughterElement(detector,subpath);
88  }
89 
91  Entry* _createStackEntry(void* param, xml_h element) {
92  xml_comp_t e(element);
93  DetElement elt = _getDetector(param, element);
94  std::string name = e.hasAttr(_U(name)) ? e.nameStr() : e.tag();
95  return new Entry(elt,name,e.tag(),_getValidity(element),detail::hash32(name));
96  }
97 
98 
130  template <> void Converter<arbitrary>::operator()(xml_h e) const {
131  xml_comp_t elt(e);
132  std::string tag = elt.tag();
133  ConversionArg* arg = _param<ConversionArg>();
134  if ( !arg )
135  except("ConditionsParser","++ Invalid parser argument [Internal Error]");
136  else if ( tag == "conditions" )
137  Converter<conditions>(description,param,optional)(e);
138  else if ( arg->stack && tag == "detelement" )
139  Converter<conditions>(description,param,optional)(e);
140  else if ( tag == "open_transaction" )
141  return;
142  else if ( tag == "close_transaction" )
143  return;
144  else if ( tag == "include" )
145  Converter<include>(description,param,optional)(e);
146  else if ( tag == "detelements" )
147  xml_coll_t(e,_U(star)).for_each(Converter<conditions>(description,param,optional));
148  else if ( tag == "subdetectors" )
149  xml_coll_t(e,_U(star)).for_each(Converter<conditions>(description,param,optional));
150  else if ( tag == "alignment" ) {
151  dd4hep_ptr<Entry> val(_createStackEntry(param,e));
152  val->value = elt.attr<std::string>(_U(ref));
153  if ( !arg->stack )
154  except("ConditionsParser","Non-existing Conditions stack:%s %d",__FILE__, __LINE__);
155  else
156  arg->stack->emplace_back(val.release());
157  }
158  else {
159  dd4hep_ptr<Entry> val(_createStackEntry(param,e));
160  val->value = elt.hasAttr(_U(value)) ? elt.valueStr() : e.text();
161  if ( !arg->stack )
162  except("ConditionsParser","Non-existing Conditions stack:%s %d",__FILE__, __LINE__);
163  else
164  arg->stack->emplace_back(val.release());
165  }
166  }
167 
174  template <> void Converter<include>::operator()(xml_h element) const {
175  xml::DocumentHolder doc(xml::DocumentHandler().load(element, element.attr_value(_U(ref))));
176  xml_coll_t(doc.root(),_U(star)).for_each(Converter<arbitrary>(description,param,optional));
177  }
178 
194  template <> void Converter<conditions>::operator()(xml_h e) const {
195  ConversionArg* arg = _param<ConversionArg>();
196  DetElement elt = arg->detector;
197  arg->detector = _getDetector(param,e);
198  xml_coll_t(e,_U(star)).for_each(Converter<arbitrary>(description,param,optional));
199  arg->detector = elt;
200  }
201 }
202 
209 static void* setup_global_Conditions(dd4hep::Detector& description, int argc, char** argv) {
210  if ( argc == 2 ) {
211  xml_h e = xml_h::Elt_t(argv[0]);
212  ConditionsStack* stack = (ConditionsStack*)argv[1];
213  dd4hep::ConversionArg args(description.world(), stack);
214  (dd4hep::Converter<dd4hep::conditions>(description,&args))(e);
215  return &description;
216  }
217  dd4hep::except("XML_DOC_READER","Invalid number of arguments to interprete conditions: %d != %d.",argc,2);
218  return 0;
219 }
220 DECLARE_DD4HEP_CONSTRUCTOR(XMLConditionsParser,setup_global_Conditions)
dd4hep::Detector::world
virtual DetElement world() const =0
Return reference to the top-most (world) detector element.
dd4hep::_getDetector
DetElement _getDetector(void *param, xml_h e)
Helper: Extract the required detector element from the parsing information.
Definition: ConditionsParser.cpp:83
Detector.h
Entry
GlobalAlignmentStack::StackEntry Entry
Definition: GlobalAlignmentCache.cpp:25
dd4hep::xml::Handle_t::parent
Handle_t parent() const
Access the element's parent element.
Definition: XMLElements.cpp:663
ConditionsDataLoader.h
dd4hep::ConversionArg
Helper structure for data conversion.
Definition: ConditionsParser.cpp:64
dd4hep::xml::Handle_t
Class to easily access the properties of single XmlElements.
Definition: XMLElements.h:380
ConditionsManager.h
dd4hep::_createStackEntry
Entry * _createStackEntry(void *param, xml_h element)
Helper: Extract the string value from the xml element.
Definition: ConditionsParser.cpp:91
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
xml_comp_t
dd4hep::xml::Component xml_comp_t
Definition: XML.h:33
DocumentHandler.h
ConditionsEntry.h
_UC
#define _UC(x)
Definition: ConditionsTags.h:63
dd4hep::cond
Namespace for implementation details of the AIDA detector description toolkit.
Definition: ConditionsCleanup.h:23
dd4hep::DetElement
Handle class describing a detector element.
Definition: DetElement.h:188
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
DetectorTools.h
_U
#define _U(a)
Definition: Tags.h:23
DECLARE_DD4HEP_CONSTRUCTOR
#define DECLARE_DD4HEP_CONSTRUCTOR(name, func)
Definition: Factories.h:291
dd4hep::_getValidity
std::string _getValidity(xml_h elt)
Helper: Extract the validity from the xml element.
Definition: ConditionsParser.cpp:74
dd4hep::ConversionArg::detector
DetElement detector
Definition: ConditionsParser.cpp:65
dd4hep::xml::Handle_t::Elt_t
XmlElement * Elt_t
Definition: XMLElements.h:383
dd4hep::xml::DocumentHolder
Class supporting the basic functionality of an XML document including ownership.
Definition: XMLElements.h:741
dd4hep::cond::ConditionsStack
std::list< Entry * > ConditionsStack
Definition: ConditionsDataLoader.h:34
DetFactoryHelper.h
dd4hep::xml::DocumentHandler
Class supporting to read and parse XML documents.
Definition: DocumentHandler.h:39
Conversions.h
XMLElements.h
mapping
ConditionsMap & mapping
Definition: AlignmentsCalculator.cpp:82
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
det
DetElement::Object * det
Definition: AlignmentsCalculator.cpp:66
dd4hep::Detector
The main interface to the dd4hep detector description package.
Definition: Detector.h:90
dd4hep::xml::Handle_t::hasAttr
bool hasAttr(const XmlChar *t) const
Check for the existence of a named attribute.
Definition: XMLElements.cpp:673
ConditionsTags.h
dd4hep::ConversionArg::ConversionArg
ConversionArg(DetElement det, ConditionsStack *stk)
Definition: ConditionsParser.cpp:67
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::xml::Handle_t::ptr
Elt_t ptr() const
Direct access to the XmlElement by function.
Definition: XMLElements.h:401
dd4hep::ConversionArg::stack
ConditionsStack * stack
Definition: ConditionsParser.cpp:66
dd4hep::xml::Handle_t::text
std::string text() const
Text access to the element's text.
Definition: XMLElements.h:418
dd4hep::cond::Entry
The intermediate conditions data used to populate the DetElement conditions.
Definition: ConditionsEntry.h:36