DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
ConditionsRepositoryParser.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/XMLParsers.h>
18 #include <XML/XML.h>
19 #include <DD4hep/Path.h>
20 #include <DD4hep/Printout.h>
21 #include <DD4hep/DetectorTools.h>
22 #include <DD4hep/AlignmentData.h>
26 
27 #include <DDCond/ConditionsTags.h>
29 
30 // C/C++ include files
31 #include <stdexcept>
32 #include <climits>
33 
35 namespace dd4hep {
36 
38  namespace {
40  class iov;
41  class iov_type;
42  class manager;
43  class repository;
44  class detelement;
45  class conditions;
46  class arbitrary;
47 
49  class value;
50  class pressure;
51  class temperature;
52  class mapping;
53  class sequence;
54  class alignment;
55  }
57  template <> void Converter<iov>::operator()(xml_h seq) const;
58  template <> void Converter<iov_type>::operator()(xml_h seq) const;
59  template <> void Converter<repository>::operator()(xml_h seq) const;
60  template <> void Converter<manager>::operator()(xml_h seq) const;
61  template <> void Converter<value>::operator()(xml_h e) const;
62  template <> void Converter<pressure>::operator()(xml_h e) const;
63  template <> void Converter<temperature>::operator()(xml_h e) const;
64  template <> void Converter<sequence>::operator()(xml_h e) const;
65  template <> void Converter<mapping>::operator()(xml_h e) const;
66  template <> void Converter<alignment>::operator()(xml_h e) const;
67  template <> void Converter<conditions>::operator()(xml_h seq) const;
68  template <> void Converter<arbitrary>::operator()(xml_h seq) const;
69 }
70 
71 using namespace dd4hep::cond;
72 
74 namespace {
75 
77  static dd4hep::PrintLevel s_parseLevel = dd4hep::DEBUG;
78 
80 
85  struct ConversionArg {
86  dd4hep::DetElement detector;
87  ConditionsPool* pool;
88  ConditionsManager manager;
89  ConversionArg() = delete;
90  ConversionArg(const ConversionArg&) = delete;
91  ConversionArg(dd4hep::DetElement det, ConditionsManager m) : detector(det), pool(0), manager(m)
92  { }
93  ConversionArg& operator=(const ConversionArg&) = delete;
94  };
95 
97 
102  struct CurrentDetector {
103  dd4hep::DetElement detector;
104  ConversionArg* arg;
105  CurrentDetector(ConversionArg* a) : arg(a) {
106  detector = arg->detector;
107  }
108  ~CurrentDetector() {
109  arg->detector = detector;
110  }
111  void set(const std::string& path) {
112  if ( !path.empty() ) {
113  arg->detector = dd4hep::detail::tools::findDaughterElement(detector,path);
114  }
115  }
116  };
118 
123  struct CurrentPool {
124  ConditionsPool* pool;
125  ConversionArg* arg;
126  CurrentPool(ConversionArg* a) : arg(a) {
127  pool = arg->pool;
128  }
129  ~CurrentPool() {
130  arg->pool = pool;
131  }
132  void set(ConditionsPool* p) {
133  arg->pool = p;
134  }
135  };
136 
138 
143  dd4hep::Condition create_condition(dd4hep::DetElement det, xml_h e) {
144  xml_dim_t elt(e);
145  std::string tag = elt.tag();
146  std::string typ = elt.hasAttr(_U(type)) ? elt.typeStr() : tag;
147  std::string nam = elt.hasAttr(_U(name)) ? elt.nameStr() : tag;
148  std::string add = xml_handler_t::system_path(e);
149  std::string cond_nam = det.path()+"#"+nam;
150  dd4hep::Condition cond(cond_nam, typ);
152  dd4hep::printout(s_parseLevel,"XMLConditions","++ Processing condition tag:%s name:%s type:%s [%s] hash:%016X det:%p",
153  tag.c_str(), cond.name(), typ.c_str(),
154  dd4hep::Path(add).filename().c_str(), cond.key(), det.ptr());
155 #if !defined(DD4HEP_MINIMAL_CONDITIONS)
156  cond->address = add;
157  cond->value = "";
158  cond->validity = "";
159  if ( elt.hasAttr(_U(comment)) ) {
160  cond->comment = elt.attr<std::string>(_U(comment));
161  }
162 #endif
163  //ConditionsKeyAssign(det).addKey(cond.name());
164  return cond;
165  }
166 
168 
173  template <typename BINDER> dd4hep::Condition bind_condition(const BINDER& bnd,
175  xml_h e,
176  const std::string& type="")
177  {
178  xml_dim_t elt(e);
179  std::string typ = type.empty() ? elt.typeStr() : type;
180  std::string val = elt.hasAttr(_U(value)) ? elt.valueStr() : elt.text();
181  dd4hep::Condition con = create_condition(det, e);
182  std::string unit = elt.hasAttr(_U(unit)) ? elt.attr<std::string>(_U(unit)) : std::string("");
183  if ( !unit.empty() ) val += "*"+unit;
184  con->value = val;
185  dd4hep::detail::OpaqueDataBinder::bind(bnd, con, typ, val);
186  return con;
187  }
188 }
189 
191 namespace dd4hep {
192 
194 
199  template <> void Converter<iov_type>::operator()(xml_h element) const {
200  xml_dim_t e = element;
201  std::string nam = e.nameStr();
202  std::size_t id = e.id() >= 0 ? e.id() : INT_MAX;
203  ConversionArg* arg = _param<ConversionArg>();
204  dd4hep::printout(s_parseLevel,"XMLConditions","++ Registering IOV type: [%d]: %s",int(id),nam.c_str());
205  const IOVType* iov_type = arg->manager.registerIOVType(id,nam).second;
206  if ( !iov_type ) {
207  except("XMLConditions","Failed to register iov type: [%d]: %s",int(id),nam.c_str());
208  }
209  }
210 
212 
217  template <> void Converter<iov>::operator()(xml_h element) const {
218  xml_dim_t e = element;
219  std::string val = e.attr<std::string>(_UC(validity));
220  ConversionArg* arg = _param<ConversionArg>();
221  CurrentPool pool(arg);
222 
223  pool.set(arg->manager.registerIOV(val));
224  if ( e.hasAttr(_U(ref)) ) {
225  std::string ref = e.attr<std::string>(_U(ref));
226  dd4hep::printout(s_parseLevel,"XMLConditions","++ Reading IOV file: %s -> %s",val.c_str(),ref.c_str());
227  xml::DocumentHolder doc(xml::DocumentHandler().load(element, element.attr_value(_U(ref))));
228  Converter<conditions>(description,param,optional)(doc.root());
229  return;
230  }
231  xml_coll_t(e,_UC(detelement)).for_each(Converter<arbitrary>(description,param,optional));
232  }
233 
235 
240  template <> void Converter<manager>::operator()(xml_h element) const {
241  ConversionArg* arg = _param<ConversionArg>();
242  if ( element.hasAttr(_U(ref)) ) {
243  xml::DocumentHolder doc(xml::DocumentHandler().load(element, element.attr_value(_U(ref))));
244  Converter<arbitrary>(description,param,optional)(doc.root());
245  }
246  for( xml_coll_t c(element,_UC(property)); c; ++c) {
247  xml_dim_t d = c;
248  std::string nam = d.nameStr();
249  std::string val = d.valueStr();
250  try {
251  printout(s_parseLevel,"XMLConditions","++ Setup conditions Manager[%s] = %s",
252  nam.c_str(),val.c_str());
253  arg->manager[nam].str(val);
254  }
255  catch(const std::exception& e) {
256  printout(ERROR,"XMLConditions","++ FAILED: conditions Manager[%s] = %s [%s]",
257  nam.c_str(),val.c_str(),e.what());
258  }
259  }
260  arg->manager.initialize();
261  printout(s_parseLevel,"XMLConditions","++ Conditions Manager successfully initialized.");
262  }
263 
265 
270  template <> void Converter<value>::operator()(xml_h e) const {
271  ConversionArg* arg = _param<ConversionArg>();
272  dd4hep::Condition con = bind_condition(detail::ValueBinder(), arg->detector, e);
273  arg->manager.registerUnlocked(*arg->pool, con);
274  }
275 
277 
284  template <> void Converter<pressure>::operator()(xml_h e) const {
285  ConversionArg* arg = _param<ConversionArg>();
286  dd4hep::Condition con = bind_condition(detail::ValueBinder(), arg->detector, e, "double");
288  arg->manager.registerUnlocked(*arg->pool, con);
289  }
290 
292 
299  template <> void Converter<temperature>::operator()(xml_h e) const {
300  ConversionArg* arg = _param<ConversionArg>();
301  dd4hep::Condition con = bind_condition(detail::ValueBinder(), arg->detector, e, "double");
303  arg->manager.registerUnlocked(*arg->pool, con);
304  }
305 
307 
312  template <> void Converter<sequence>::operator()(xml_h e) const {
313  ConversionArg* arg = _param<ConversionArg>();
314  dd4hep::Condition con = create_condition(arg->detector, e);
315  xml::parse_sequence(e, con->data);
316  arg->manager.registerUnlocked(*arg->pool, con);
317  }
318 
320 
325  template <> void Converter<mapping>::operator()(xml_h e) const {
326  ConversionArg* arg = _param<ConversionArg>();
327  dd4hep::Condition con = create_condition(arg->detector, e);
328  xml::parse_mapping(e, con->data);
329  arg->manager.registerUnlocked(*arg->pool, con);
330  }
331 
333 
338  template <> void Converter<alignment>::operator()(xml_h e) const {
339  xml_h child_rot, child_pos, child_piv;
340  ConversionArg* arg = _param<ConversionArg>();
341  dd4hep::Condition con = create_condition(arg->detector, e);
342  //Delta& del = con.bind<Delta>();
343  xml::parse_delta(e, con->data);
345  arg->manager.registerUnlocked(*arg->pool, con);
346  }
347 
349 
354  template <> void Converter<detelement>::operator()(xml_h e) const {
355  xml_comp_t elt(e);
356  ConversionArg* arg = _param<ConversionArg>();
357  CurrentDetector detector(arg);
358  if ( elt.hasAttr(_U(path)) ) {
359  detector.set(e.attr<std::string>(_U(path)));
360  printout(s_parseLevel,"XMLConditions","++ Processing condition for:%s",
361  arg->detector.path().c_str());
362  }
363  if ( elt.hasAttr(_U(ref)) ) {
364  xml::DocumentHolder doc(xml::DocumentHandler().load(e, e.attr_value(_U(ref))));
365  (*this)(doc.root());
366  }
367  xml_coll_t(e,_U(value)).for_each(Converter<value>(description,param,optional));
368  xml_coll_t(e,_UC(mapping)).for_each(Converter<mapping>(description,param,optional));
369  xml_coll_t(e,_UC(sequence)).for_each(Converter<sequence>(description,param,optional));
370  xml_coll_t(e,_UC(pressure)).for_each(Converter<pressure>(description,param,optional));
371  xml_coll_t(e,_UC(alignment_delta)).for_each(Converter<alignment>(description,param,optional));
372  xml_coll_t(e,_UC(temperature)).for_each(Converter<temperature>(description,param,optional));
373  xml_coll_t(e,_UC(detelement)).for_each(Converter<detelement>(description,param,optional));
374  }
375 
377 
382  template <> void Converter<repository>::operator()(xml_h element) const {
383  xml_coll_t(element,_UC(manager)).for_each(Converter<manager>(description,param,optional));
384  xml_coll_t(element,_UC(iov_type)).for_each(Converter<iov_type>(description,param,optional));
385  xml_coll_t(element,_UC(iov)).for_each(Converter<iov>(description,param,optional));
386  }
387 
389 
394  template <> void Converter<arbitrary>::operator()(xml_h e) const {
395  xml_comp_t elt(e);
396  std::string tag = elt.tag();
397  if ( tag == "repository" )
398  Converter<repository>(description,param,optional)(e);
399  else if ( tag == "manager" )
400  Converter<manager>(description,param,optional)(e);
401  else if ( tag == "conditions" )
402  Converter<conditions>(description,param,optional)(e);
403  else if ( tag == "detelement" )
404  Converter<detelement>(description,param,optional)(e);
405  else if ( tag == "iov_type" )
406  Converter<iov_type>(description,param,optional)(e);
407  else if ( tag == "iov" ) // Processing repository file
408  Converter<iov>(description,param,optional)(e);
409  else
410  except("XMLConditions",
411  "++ Failed to handle unknown tag: %s",tag.c_str());
412  }
413 
415 
420  template <> void Converter<conditions>::operator()(xml_h e) const {
421  xml_coll_t(e,_U(star)).for_each(Converter<arbitrary>(description,param,optional));
422  }
423 }
424 
426 
431 static long setup_repository_loglevel(dd4hep::Detector& /* description */, int argc, char** argv) {
432  if ( argc == 1 ) {
433  s_parseLevel = dd4hep::printLevel(argv[0]);
434  return 1;
435  }
436  dd4hep::except("ConditionsXMLRepositoryPrintLevel","++ Invalid plugin arguments: %s",
437  dd4hep::arguments(argc,argv).c_str());
438  return 0;
439 }
440 DECLARE_APPLY(DD4hep_ConditionsXMLRepositoryPrintLevel,setup_repository_loglevel)
441 
442 #include <DD4hep/DD4hepUI.h>
443 
449 static long setup_repository_Conditions(dd4hep::Detector& description, int argc, char** argv) {
450  if ( argc == 1 ) {
451  dd4hep::detail::DD4hepUI ui(description);
452  std::string fname(argv[0]);
453  ConditionsManager mgr(ui.conditionsMgr());
454  ConversionArg arg(description.world(), mgr);
455  xml_doc_holder_t doc(xml_handler_t().load(fname));
456  (dd4hep::Converter<dd4hep::conditions>(description,&arg))(doc.root());
457  return 1;
458  }
459  dd4hep::except("XML_DOC_READER","Invalid number of arguments to interprete conditions: %d != %d.",argc,1);
460  return 0;
461 }
462 DECLARE_APPLY(DD4hep_ConditionsXMLRepositoryParser,setup_repository_Conditions)
dd4hep::xml::Collection_t
Class to support the access to collections of XmlNodes (or XmlElements)
Definition: XMLElements.h:636
dd4hep::Condition::value
const std::string & value() const
Access the value field of the condition as a string.
Definition: Conditions.cpp:150
dd4hep::ConditionKey::hashCode
static Condition::key_type hashCode(DetElement detector, const char *value)
Hash code generation from input string.
Definition: Conditions.cpp:273
dd4hep::Detector::world
virtual DetElement world() const =0
Return reference to the top-most (world) detector element.
cond
AlignmentCondition::Object * cond
Definition: AlignmentsCalculator.cpp:68
xml_handler_t
dd4hep::xml::DocumentHandler xml_handler_t
Definition: XML.h:37
Path.h
ConditionsInterna.h
Detector.h
dd4hep::exception
void exception(const std::string &src, const std::string &msg)
Definition: RootDictionary.h:69
dd4hep::xml::parse_sequence
void parse_sequence(Handle_t e, OpaqueDataBlock &block)
Converts linear STL containers from their string representation.
Definition: XMLParsers.cpp:125
DECLARE_APPLY
#define DECLARE_APPLY(name, func)
Definition: Factories.h:281
dd4hep::xml::Handle_t
Class to easily access the properties of single XmlElements.
Definition: XMLElements.h:380
ConditionsManager.h
dd4hep::Condition::PRESSURE
@ PRESSURE
Definition: Conditions.h:84
dd4hep::Path::filename
Path filename() const
The file name of the path.
Definition: Path.cpp:211
dd4hep::xml::parse_delta
void parse_delta(Handle_t e, OpaqueDataBlock &block)
Parse delta into an opaque data block.
Definition: XMLParsers.cpp:99
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
_UC
#define _UC(x)
Definition: ConditionsTags.h:63
dd4hep::Condition
Main condition object handle.
Definition: Conditions.h:51
dd4hep::cond
Namespace for implementation details of the AIDA detector description toolkit.
Definition: ConditionsCleanup.h:23
dd4hep::Condition::TEMPERATURE
@ TEMPERATURE
Definition: Conditions.h:82
dd4hep::DetElement
Handle class describing a detector element.
Definition: DetElement.h:188
dd4hep::Condition::ALIGNMENT_DELTA
@ ALIGNMENT_DELTA
Definition: Conditions.h:86
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::cond::ConditionsPool
Class implementing the conditions collection for a given IOV type.
Definition: ConditionsPool.h:54
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
dd4hep::detail::OpaqueDataBinder::bind
static bool bind(const BINDER &b, OBJECT &object, const std::string &typ, const std::string &val)
Binding function for scalar items. See the implementation function for the concrete instantiations.
dd4hep::cond::ConditionsManager
Manager class for condition handles.
Definition: ConditionsManager.h:46
dd4hep::Condition::setFlag
void setFlag(mask_type option)
Flag operations: Set a conditons flag.
Definition: Conditions.cpp:191
DetFactoryHelper.h
dd4hep::detail::DD4hepUI
ROOT interactive UI for dd4hep applications.
Definition: DD4hepUI.h:33
XML.h
dd4hep::xml::DocumentHandler
Class supporting to read and parse XML documents.
Definition: DocumentHandler.h:39
Conversions.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::xml::parse_mapping
void parse_mapping(Handle_t e, OpaqueDataBlock &block)
Converts opaque maps to OpaqueDataBlock objects.
Definition: XMLParsers.cpp:105
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
dd4hep::xml::DocumentHandler::system_path
static std::string system_path(Handle_t base)
System ID of a given XML entity.
Definition: DocumentHandler.cpp:277
ConditionsTags.h
dd4hep::xml::Handle_t::attr
T attr(const Attribute a) const
Access typed attribute value by the XmlAttr.
Definition: XMLElements.h:454
Printout.h
OpaqueDataBinder.h
xml_dim_t
dd4hep::xml::Dimension xml_dim_t
Definition: XML.h:31
AlignmentData.h
dd4hep::Path
Path handling class.
Definition: Path.h:45
dd4hep::Condition::data
OpaqueDataBlock & data() const
Access the opaque data block.
Definition: Conditions.cpp:129