Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:13:54

0001 //==========================================================================
0002 //  AIDA Detector description implementation 
0003 //--------------------------------------------------------------------------
0004 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0005 // All rights reserved.
0006 //
0007 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0008 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0009 //
0010 // Author     : M.Frank
0011 //
0012 //==========================================================================
0013 
0014 // Framework include files
0015 #include <DD4hep/DetectorLoad.h>
0016 #include <DD4hep/Detector.h>
0017 #include <DD4hep/Printout.h>
0018 #include <DD4hep/Plugins.h>
0019 #include <XML/XMLElements.h>
0020 #include <XML/DocumentHandler.h>
0021 
0022 // C/C++ include files
0023 #include <stdexcept>
0024 
0025 #ifndef __TIXML__
0026 #include <xercesc/dom/DOMException.hpp>
0027 namespace dd4hep {
0028   namespace xml {
0029     typedef xercesc::DOMException XmlException;
0030   }
0031 }
0032 #endif
0033 
0034 using namespace dd4hep;
0035 
0036 /// Default constructor (protected, for sub-classes)
0037 DetectorLoad::DetectorLoad(Detector* description) : m_detDesc(description)  {
0038 }
0039 
0040 /// Default constructor (public, if used as a handle)
0041 DetectorLoad::DetectorLoad(Detector& description) : m_detDesc(&description)  {
0042 }
0043 
0044 /// Standard destructor
0045 DetectorLoad::~DetectorLoad() {
0046 }
0047 
0048 /// Process XML unit and adopt all data from source structure.
0049 void DetectorLoad::processXML(const std::string& xmlfile, xml::UriReader* entity_resolver) {
0050   try {
0051     xml::DocumentHolder doc(xml::DocumentHandler().load(xmlfile,entity_resolver));
0052     if ( doc )   {
0053       xml::Handle_t handle = doc.root();
0054       if ( handle )   {
0055         processXMLElement(xmlfile,handle);
0056         return;
0057       }
0058     }
0059     throw std::runtime_error("dd4hep: Failed to parse the XML file " + xmlfile + " [Invalid XML ROOT handle]");
0060   }
0061   catch (const xml::XmlException& e) {
0062     throw std::runtime_error(xml::_toString(e.msg) + "\ndd4hep: XML-DOM Exception while parsing " + xmlfile);
0063   }
0064   catch (const std::exception& e) {
0065     throw std::runtime_error(std::string(e.what()) + "\ndd4hep: while parsing " + xmlfile);
0066   }
0067   catch (...) {
0068     throw std::runtime_error("dd4hep: UNKNOWN exception while parsing " + xmlfile);
0069   }
0070 }
0071 
0072 /// Process XML unit and adopt all data from source structure.
0073 void DetectorLoad::processXML(const xml::Handle_t& base, const std::string& xmlfile, xml::UriReader* entity_resolver) {
0074   try {
0075     xml::Strng_t xml(xmlfile);
0076     xml::DocumentHolder doc(xml::DocumentHandler().load(base,xml,entity_resolver));
0077     if ( doc )   {
0078       xml::Handle_t handle = doc.root();
0079       if ( handle )   {
0080         processXMLElement(xmlfile,handle);
0081         return;
0082       }
0083     }
0084     throw std::runtime_error("dd4hep: Failed to parse the XML file " + xmlfile + " [Invalid XML ROOT handle]");
0085   }
0086   catch (const xml::XmlException& e) {
0087     throw std::runtime_error(xml::_toString(e.msg) + "\ndd4hep: XML-DOM Exception while parsing " + xmlfile);
0088   }
0089   catch (const std::exception& e) {
0090     throw std::runtime_error(std::string(e.what()) + "\ndd4hep: while parsing " + xmlfile);
0091   }
0092   catch (...) {
0093     throw std::runtime_error("dd4hep: UNKNOWN exception while parsing " + xmlfile);
0094   }
0095 }
0096 
0097 /// Process XML unit and adopt all data from source string in momory.
0098 void DetectorLoad::processXMLString(const char* xmldata)   {
0099   processXMLString(xmldata, 0);
0100 }
0101 
0102 /// Process XML unit and adopt all data from source string in momory. Subsequent parsers may use the entity resolver.
0103 void DetectorLoad::processXMLString(const char* xmldata, xml::UriReader* entity_resolver) {
0104   try {
0105     if ( xmldata)   {
0106       xml::DocumentHolder doc(xml::DocumentHandler().parse(xmldata,::strlen(xmldata),"In-Memory",entity_resolver));
0107       if ( doc )   {
0108         xml::Handle_t handle = doc.root();
0109         if ( handle )   {
0110           processXMLElement("In-Memory-XML",handle);
0111           return;
0112         }
0113       }
0114     }
0115     throw std::runtime_error("DetectorLoad::processXMLString: Invalid XML In-memory source [NULL]");
0116   }
0117   catch (const xml::XmlException& e) {
0118     throw std::runtime_error(xml::_toString(e.msg) + "\ndd4hep: XML-DOM Exception while parsing XML in-memory string.");
0119   }
0120   catch (const std::exception& e) {
0121     throw std::runtime_error(std::string(e.what()) + "\ndd4hep: while parsing XML in-memory string.");
0122   }
0123   catch (...) {
0124     throw std::runtime_error("dd4hep: UNKNOWN exception while parsing XML in-memory string.");
0125   }
0126 }
0127 
0128 /// Process a given DOM (sub-) tree
0129 void DetectorLoad::processXMLElement(const std::string& xmlfile, const xml::Handle_t& xml_root) {
0130   if ( xml_root.ptr() )   {
0131     std::string tag = xml_root.tag();
0132     std::string type = tag + "_XML_reader";
0133     xml::Handle_t handle = xml_root;
0134     long result = PluginService::Create<long>(type, m_detDesc, &handle);
0135     if (0 == result) {
0136       PluginDebug dbg;
0137       result = PluginService::Create<long>(type, m_detDesc, &handle);
0138       if ( 0 == result )  {
0139         throw std::runtime_error("dd4hep: Failed to locate plugin to interprete files of type"
0140                                  " \"" + tag + "\" - no factory:" + type + ". " + dbg.missingFactory(type));
0141       }
0142     }
0143     result = *(long*) result;
0144     if (result != 1) {
0145       throw std::runtime_error("dd4hep: Failed to parse the XML file " + xmlfile + " with the plugin " + type);
0146     }
0147     return;
0148   }
0149   throw std::runtime_error("dd4hep: Failed to parse the XML file " + xmlfile + " [Invalid XML ROOT handle]");
0150 }
0151 
0152 /// Process a given DOM (sub-) tree
0153 void DetectorLoad::processXMLElement(const xml::Handle_t& xml_root, DetectorBuildType /* type */) {
0154   if ( xml_root.ptr() )   {
0155     std::string tag = xml_root.tag();
0156     std::string type = tag + "_XML_reader";
0157     xml::Handle_t handle = xml_root;
0158     long result = PluginService::Create<long>(type, m_detDesc, &handle);
0159     if (0 == result) {
0160       PluginDebug dbg;
0161       result = PluginService::Create<long>(type, m_detDesc, &handle);
0162       if ( 0 == result )  {
0163         throw std::runtime_error("dd4hep: Failed to locate plugin to interprete files of type"
0164                                  " \"" + tag + "\" - no factory:" 
0165                                  + type + ". " + dbg.missingFactory(type));
0166       }
0167     }
0168     result = *(long*) result;
0169     if (result != 1)   {
0170       throw std::runtime_error("dd4hep: Failed to parse the XML element with tag " 
0171                                + tag + " with the plugin " + type);
0172     }
0173     return;
0174   }
0175   throw std::runtime_error("dd4hep: Failed to parse the XML file [Invalid XML ROOT handle]");
0176 }