DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
DetectorLoad.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/DetectorLoad.h>
16 #include <DD4hep/Detector.h>
17 #include <DD4hep/Printout.h>
18 #include <DD4hep/Plugins.h>
19 #include <XML/XMLElements.h>
20 #include <XML/DocumentHandler.h>
21 
22 // C/C++ include files
23 #include <stdexcept>
24 
25 #ifndef __TIXML__
26 #include <xercesc/dom/DOMException.hpp>
27 namespace dd4hep {
28  namespace xml {
29  typedef xercesc::DOMException XmlException;
30  }
31 }
32 #endif
33 
34 using namespace dd4hep;
35 
37 DetectorLoad::DetectorLoad(Detector* description) : m_detDesc(description) {
38 }
39 
41 DetectorLoad::DetectorLoad(Detector& description) : m_detDesc(&description) {
42 }
43 
46 }
47 
49 void DetectorLoad::processXML(const std::string& xmlfile, xml::UriReader* entity_resolver) {
50  try {
51  xml::DocumentHolder doc(xml::DocumentHandler().load(xmlfile,entity_resolver));
52  if ( doc ) {
53  xml::Handle_t handle = doc.root();
54  if ( handle ) {
55  processXMLElement(xmlfile,handle);
56  return;
57  }
58  }
59  throw std::runtime_error("dd4hep: Failed to parse the XML file " + xmlfile + " [Invalid XML ROOT handle]");
60  }
61  catch (const xml::XmlException& e) {
62  throw std::runtime_error(xml::_toString(e.msg) + "\ndd4hep: XML-DOM Exception while parsing " + xmlfile);
63  }
64  catch (const std::exception& e) {
65  throw std::runtime_error(std::string(e.what()) + "\ndd4hep: while parsing " + xmlfile);
66  }
67  catch (...) {
68  throw std::runtime_error("dd4hep: UNKNOWN exception while parsing " + xmlfile);
69  }
70 }
71 
73 void DetectorLoad::processXML(const xml::Handle_t& base, const std::string& xmlfile, xml::UriReader* entity_resolver) {
74  try {
75  xml::Strng_t xml(xmlfile);
76  xml::DocumentHolder doc(xml::DocumentHandler().load(base,xml,entity_resolver));
77  if ( doc ) {
78  xml::Handle_t handle = doc.root();
79  if ( handle ) {
80  processXMLElement(xmlfile,handle);
81  return;
82  }
83  }
84  throw std::runtime_error("dd4hep: Failed to parse the XML file " + xmlfile + " [Invalid XML ROOT handle]");
85  }
86  catch (const xml::XmlException& e) {
87  throw std::runtime_error(xml::_toString(e.msg) + "\ndd4hep: XML-DOM Exception while parsing " + xmlfile);
88  }
89  catch (const std::exception& e) {
90  throw std::runtime_error(std::string(e.what()) + "\ndd4hep: while parsing " + xmlfile);
91  }
92  catch (...) {
93  throw std::runtime_error("dd4hep: UNKNOWN exception while parsing " + xmlfile);
94  }
95 }
96 
98 void DetectorLoad::processXMLString(const char* xmldata) {
99  processXMLString(xmldata, 0);
100 }
101 
103 void DetectorLoad::processXMLString(const char* xmldata, xml::UriReader* entity_resolver) {
104  try {
105  if ( xmldata) {
106  xml::DocumentHolder doc(xml::DocumentHandler().parse(xmldata,::strlen(xmldata),"In-Memory",entity_resolver));
107  if ( doc ) {
108  xml::Handle_t handle = doc.root();
109  if ( handle ) {
110  processXMLElement("In-Memory-XML",handle);
111  return;
112  }
113  }
114  }
115  throw std::runtime_error("DetectorLoad::processXMLString: Invalid XML In-memory source [NULL]");
116  }
117  catch (const xml::XmlException& e) {
118  throw std::runtime_error(xml::_toString(e.msg) + "\ndd4hep: XML-DOM Exception while parsing XML in-memory string.");
119  }
120  catch (const std::exception& e) {
121  throw std::runtime_error(std::string(e.what()) + "\ndd4hep: while parsing XML in-memory string.");
122  }
123  catch (...) {
124  throw std::runtime_error("dd4hep: UNKNOWN exception while parsing XML in-memory string.");
125  }
126 }
127 
129 void DetectorLoad::processXMLElement(const std::string& xmlfile, const xml::Handle_t& xml_root) {
130  if ( xml_root.ptr() ) {
131  std::string tag = xml_root.tag();
132  std::string type = tag + "_XML_reader";
133  xml::Handle_t handle = xml_root;
134  long result = PluginService::Create<long>(type, m_detDesc, &handle);
135  if (0 == result) {
136  PluginDebug dbg;
137  result = PluginService::Create<long>(type, m_detDesc, &handle);
138  if ( 0 == result ) {
139  throw std::runtime_error("dd4hep: Failed to locate plugin to interprete files of type"
140  " \"" + tag + "\" - no factory:" + type + ". " + dbg.missingFactory(type));
141  }
142  }
143  result = *(long*) result;
144  if (result != 1) {
145  throw std::runtime_error("dd4hep: Failed to parse the XML file " + xmlfile + " with the plugin " + type);
146  }
147  return;
148  }
149  throw std::runtime_error("dd4hep: Failed to parse the XML file " + xmlfile + " [Invalid XML ROOT handle]");
150 }
151 
154  if ( xml_root.ptr() ) {
155  std::string tag = xml_root.tag();
156  std::string type = tag + "_XML_reader";
157  xml::Handle_t handle = xml_root;
158  long result = PluginService::Create<long>(type, m_detDesc, &handle);
159  if (0 == result) {
160  PluginDebug dbg;
161  result = PluginService::Create<long>(type, m_detDesc, &handle);
162  if ( 0 == result ) {
163  throw std::runtime_error("dd4hep: Failed to locate plugin to interprete files of type"
164  " \"" + tag + "\" - no factory:"
165  + type + ". " + dbg.missingFactory(type));
166  }
167  }
168  result = *(long*) result;
169  if (result != 1) {
170  throw std::runtime_error("dd4hep: Failed to parse the XML element with tag "
171  + tag + " with the plugin " + type);
172  }
173  return;
174  }
175  throw std::runtime_error("dd4hep: Failed to parse the XML file [Invalid XML ROOT handle]");
176 }
dd4hep::xml::Handle_t::tag
std::string tag() const
Text access to the element's tag.
Definition: XMLElements.h:414
dd4hep::xml::XmlException
xercesc::DOMException XmlException
Definition: DetectorImp.cpp:53
Detector.h
dd4hep::exception
void exception(const std::string &src, const std::string &msg)
Definition: RootDictionary.h:69
dd4hep::DetectorLoad::m_detDesc
Detector * m_detDesc
Reference to the Detector instance.
Definition: DetectorLoad.h:50
dd4hep::DetectorLoad::~DetectorLoad
virtual ~DetectorLoad()
Default destructor.
Definition: DetectorLoad.cpp:45
dd4hep::xml::Handle_t
Class to easily access the properties of single XmlElements.
Definition: XMLElements.h:380
dd4hep::DetectorLoad::processXMLElement
virtual void processXMLElement(const std::string &msg_source, const xml::Handle_t &root)
Process a given DOM (sub-) tree.
Definition: DetectorLoad.cpp:129
dd4hep::xml::parse
void parse(Handle_t e, RotationZYX &rot)
Convert rotation XML objects to dd4hep::RotationZYX.
Definition: XMLParsers.cpp:48
DocumentHandler.h
dd4hep::DetectorLoad::processXMLString
virtual void processXMLString(const char *xmldata)
Process XML unit and adopt all data from source string in momory.
Definition: DetectorLoad.cpp:98
dd4hep::xml
Namespace for the AIDA detector description toolkit supporting XML utilities.
Definition: ConditionsTags.h:27
Plugins.h
dd4hep::PluginDebug
Helper to debug plugin manager calls.
Definition: Plugins.h:74
dd4hep::xml::_toString
std::string _toString(const Attribute attr)
Convert xml attribute to STL string.
Definition: XMLElements.cpp:234
dd4hep::DetectorLoad::DetectorLoad
DetectorLoad()=delete
No defautl constructor.
dd4hep::xml::Strng_t
Helper class to encapsulate a unicode string.
Definition: XMLElements.h:170
dd4hep::PluginDebug::missingFactory
std::string missingFactory(const std::string &name) const
Helper to check factory existence.
Definition: Plugins.cpp:147
dd4hep::xml::DocumentHolder
Class supporting the basic functionality of an XML document including ownership.
Definition: XMLElements.h:741
dd4hep::xml::UriReader
Class supporting to read data given a URI.
Definition: UriReader.h:35
dd4hep::xml::DocumentHandler
Class supporting to read and parse XML documents.
Definition: DocumentHandler.h:39
XMLElements.h
DetectorLoad.h
dd4hep::DetectorBuildType
DetectorBuildType
Detector description build types.
Definition: BuildType.h:34
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::Detector
The main interface to the dd4hep detector description package.
Definition: Detector.h:90
Printout.h
dd4hep::DetectorLoad::processXML
virtual void processXML(const std::string &fname, xml::UriReader *entity_resolver=0)
Process XML unit and adopt all data from source structure.
Definition: DetectorLoad.cpp:49
dd4hep::xml::Handle_t::ptr
Elt_t ptr() const
Direct access to the XmlElement by function.
Definition: XMLElements.h:401
handle
void handle(const O *o, const C &c, F pmf)
Definition: LCDDConverter.cpp:1105
dd4hep::xml::Document::root
Handle_t root() const
Access the ROOT eleemnt of the DOM document.
Definition: XMLElements.cpp:1040