Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:14:53

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 // DDDB is a detector description convention developed by the LHCb experiment.
0015 // For further information concerning the DTD, please see:
0016 // http://lhcb-comp.web.cern.ch/lhcb-comp/Frameworks/DetDesc/Documents/lhcbDtd.pdf
0017 //
0018 //==========================================================================
0019 
0020 // Framework includes
0021 #include "DDDB/DDDBReader.h"
0022 #include "DD4hep/IOV.h"
0023 
0024 /// Namespace for the AIDA detector description toolkit
0025 namespace dd4hep {
0026 
0027   /// Namespace of the DDDB conversion stuff
0028   namespace DDDB  {
0029 
0030     /// Class supporting the interface of the LHCb conditions database to dd4hep
0031     /**
0032      *
0033      *  \author   M.Frank
0034      *  \version  1.0
0035      *  \ingroup DD4HEP_XML
0036      */
0037     class DDDBFileReader : public DDDBReader   {
0038       long m_validityLowerLimit = 0;
0039       long m_validityUpperLimit = IOV::MAX_KEY;
0040 
0041     public:
0042       /// Standard constructor
0043       DDDBFileReader();
0044       /// Default destructor
0045       virtual ~DDDBFileReader()  {}
0046       /// Read raw XML object from the database / file
0047       virtual int getObject(const std::string& system_id, UserContext* ctxt, std::string& data)  override;
0048       /// Inform reader about a locally (e.g. by XercesC) handled source load
0049       virtual void parserLoaded(const std::string& system_id)  override;
0050       /// Inform reader about a locally (e.g. by XercesC) handled source load
0051       virtual void parserLoaded(const std::string& system_id, UserContext* ctxt)  override;
0052       /// Resolve a given URI to a string containing the data
0053       virtual bool load(const std::string& system_id, std::string& buffer)  override;
0054       /// Resolve a given URI to a string containing the data
0055       virtual bool load(const std::string& system_id, UserContext* ctxt, std::string& buffer)  override;
0056 
0057     };
0058   }    /* End namespace DDDB            */
0059 }      /* End namespace dd4hep          */
0060 
0061 
0062 //==========================================================================
0063 // Framework includes
0064 #include "DD4hep/Factories.h"
0065 #include "DD4hep/Printout.h"
0066 #include "DD4hep/Detector.h"
0067 
0068 // C/C++ include files
0069 #include <sys/types.h>
0070 #include <sys/stat.h>
0071 #include <unistd.h>
0072 #include <fcntl.h>
0073 
0074 /// Standard constructor
0075 dd4hep::DDDB::DDDBFileReader::DDDBFileReader()
0076   : DDDBReader()
0077 {
0078   declareProperty("ValidityLower",     m_context.valid_since);
0079   declareProperty("ValidityUpper",     m_context.valid_until);
0080   declareProperty("ValidityLowerLimit",m_validityLowerLimit);
0081   declareProperty("ValidityUpperLimit",m_validityUpperLimit);
0082 }
0083 
0084 int dd4hep::DDDB::DDDBFileReader::getObject(const std::string& system_id,
0085                                             UserContext*       /* ctxt */,
0086                                             std::string&       buffer)
0087 {
0088   std::string path = system_id;
0089   int fid  = ::open(path.c_str(), O_RDONLY);
0090 
0091   if ( fid == -1 )   {
0092     std::string p = m_directory+system_id;
0093     if ( p.substr(0,7) == "file://" ) path = p.substr(6);
0094     else if ( p.substr(0,5) == "file:" ) path = p.substr(5);
0095     else path = p;
0096     fid  = ::open(path.c_str(), O_RDONLY);
0097   }
0098   if ( fid != -1 )   {
0099     struct stat buff;
0100     if ( 0 == ::fstat(fid, &buff) )  {
0101       int done = 0, len = buff.st_size;
0102       char* b  = new char[len+1];
0103       b[0] = 0;
0104       while ( done<len )  {
0105         int sc = ::read(fid, b+done, buff.st_size-done);
0106         if ( sc >= 0 ) { done += sc; continue; }
0107         break;
0108       }
0109       ::close(fid);
0110       b[done] = 0;
0111       buffer = b;
0112       delete [] b;
0113       if ( done>=len ) {
0114         return 1;
0115       }
0116       return 0;
0117     }
0118     ::close(fid);
0119   }
0120   return 0;
0121 }
0122 
0123 /// Resolve a given URI to a string containing the data
0124 bool dd4hep::DDDB::DDDBFileReader::load(const std::string& system_id, std::string& buffer)   {
0125   return xml::UriReader::load(system_id, buffer);
0126 }
0127 
0128 /// Resolve a given URI to a string containing the data
0129 bool dd4hep::DDDB::DDDBFileReader::load(const std::string& system_id,
0130                                         UserContext*  ctxt,
0131                                         std::string& buffer)
0132 {
0133   return DDDBReader::load(system_id, ctxt, buffer);
0134 }
0135 
0136 /// Inform reader about a locally (e.g. by XercesC) handled source load
0137 void dd4hep::DDDB::DDDBFileReader::parserLoaded(const std::string& system_id)  {
0138   DDDBReader::parserLoaded(system_id);
0139 }
0140 
0141 /// Inform reader about a locally (e.g. by XercesC) handled source load
0142 void dd4hep::DDDB::DDDBFileReader::parserLoaded(const std::string& /* system_id */, UserContext* ctxt)  {
0143   DDDBReaderContext *ct = (DDDBReaderContext *)ctxt;
0144   // Adjust IOV period according to setup (files have no IOV)
0145   ct->valid_since = m_context.valid_since;
0146   ct->valid_until = m_context.valid_until;
0147   // Check lower bound
0148   if (ct->valid_since < m_validityLowerLimit)
0149     ct->valid_since = m_validityLowerLimit;
0150   else if (ct->valid_since > m_validityUpperLimit)
0151     ct->valid_since = m_validityUpperLimit;
0152   // Check upper bound
0153   if (ct->valid_until < m_validityLowerLimit)
0154     ct->valid_until = m_validityLowerLimit;
0155   else if (ct->valid_until > m_validityUpperLimit)
0156     ct->valid_until = m_validityUpperLimit;
0157   //DDDBReader::parserLoaded(system_id, ctxt);
0158 }
0159 
0160 namespace {
0161   void* create_dddb_xml_file_reader(const char* /* arg */) {
0162     return new dd4hep::DDDB::DDDBFileReader();
0163   }
0164 }
0165 DECLARE_CONSTRUCTOR(DDDB_FileReader,create_dddb_xml_file_reader)