Back to home page

EIC code displayed by LXR

 
 

    


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

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 includes
0015 #include <DDG4/IoStreams.h>
0016 
0017 // ROOT include files
0018 #include <TFile.h>
0019 
0020 // C/C++ include files
0021 #include <sys/types.h>
0022 #include <sys/stat.h>
0023 #include <fcntl.h>
0024 #include <cstdio>
0025 
0026 namespace {
0027   /// Anonymous cast class to get access to protected members of TFile ;-)
0028   class MyTFile : public TFile  {
0029   private:
0030     /// Destructor (unused!)
0031     virtual ~MyTFile() {}
0032   public:
0033     /// Basic write call
0034     virtual Int_t SysWrite(Int_t fd, const void* buf, Int_t len)  override  { return TFile::SysWrite(fd, buf, len);  }
0035     /// Basic read call
0036     virtual Int_t SysRead(Int_t fd, void* buf, Int_t len)  override         { return TFile::SysRead(fd,buf,len);     }
0037     /// Basic seek call
0038     virtual Long64_t SysSeek(Int_t fd, Long64_t off, Int_t way)  override   { return TFile::SysSeek(fd, off, way);   }
0039   };
0040 }
0041 
0042 namespace dd4hep {
0043 
0044   /// Specialization for standard file descriptor files according to the posix standard
0045   template<> void dd4hep_file<int>::open(const char* path, BOOST_IOS::openmode mode)  {
0046     if ( m_handle ) ::close(m_handle);
0047     m_handle = ::open(path,mode);
0048   }
0049 
0050   /// Specialization for standard file descriptor files according to the posix standard
0051   template<> dd4hep_file<int>::dd4hep_file(handle_type fd, dd4hep_file_flags flags)
0052     : m_handle(fd), m_flag(flags)   {                   }
0053 
0054   /// Specialization for standard file descriptor files according to the posix standard
0055   template<> dd4hep_file<int>::dd4hep_file(const char* fn, BOOST_IOS::openmode mode)
0056     : m_handle(0), m_flag(close_handle) {open(fn,mode); }
0057 
0058   /// Specialization for standard file descriptor files according to the posix standard
0059   template<> std::streamsize dd4hep_file<int>::read(char_type* s, std::streamsize n)
0060   {      return ::read(m_handle,s,n);                   }
0061 
0062   /// Specialization for standard file descriptor files according to the posix standard
0063   template<> std::streamsize dd4hep_file<int>::write(const char_type* s, std::streamsize n)
0064   {      return ::write(m_handle,s,n);                  }
0065 
0066   /// Specialization for standard file descriptor files according to the posix standard
0067   template<> std::streampos dd4hep_file<int>::seek(stream_offset off, BOOST_IOS::seekdir way)
0068   {      return ::lseek(m_handle,off,way);              }
0069 
0070   /// Specialization for standard file descriptor files according to the posix standard
0071   template<> void dd4hep_file<int>::close()  {
0072     if ( m_handle ) ::close(m_handle);
0073     m_handle = 0;
0074   }
0075 
0076   /// Specialization for the usage of TFile structures
0077   template<> void dd4hep_file<TFile*>::open(const char* path, BOOST_IOS::openmode mode)  {
0078     if ( m_handle )   {
0079       m_handle->Close();
0080       delete m_handle;
0081     }
0082     std::string p = path;
0083     p += "?filetype=raw";
0084     if ( mode&BOOST_IOS::out )
0085       m_handle = TFile::Open(p.c_str(),"RECREATE","ROOT");
0086     else
0087       m_handle = TFile::Open(p.c_str());
0088     if ( m_handle->IsZombie() )   {
0089       delete m_handle;
0090       m_handle = 0;
0091       throw 1;
0092     }
0093   }
0094 #define _p(x) (reinterpret_cast<MyTFile*>(x))
0095 
0096   /// Specialization for the usage of TFile structures
0097   template<> dd4hep_file<TFile*>::dd4hep_file(handle_type fd, dd4hep_file_flags flags)
0098     : m_handle(fd), m_flag(flags)    {                                         }
0099 
0100   /// Specialization for the usage of TFile structures
0101   template<> dd4hep_file<TFile*>::dd4hep_file(const char* fname, BOOST_IOS::openmode mode)
0102     : m_handle(0), m_flag(close_handle)  { open(fname,mode);                   }
0103 
0104   /// Specialization for the usage of TFile structures
0105   template<> std::streamsize dd4hep_file<TFile*>::read(char_type* s, std::streamsize n)  {
0106     if ( m_handle )   {
0107       Long64_t nb1 = m_handle->GetBytesRead();
0108       Bool_t   res = _p(m_handle)->ReadBuffer(s,nb1,n);
0109       if ( res )  {
0110         Long64_t nb2 = m_handle->GetBytesRead();
0111         return nb2-nb1;
0112       }
0113     }
0114     return -1;
0115   }
0116 
0117   /// Specialization for the usage of TFile structures
0118   template<> std::streamsize dd4hep_file<TFile*>::write(const char_type* s, std::streamsize n)
0119   { return m_handle ? _p(m_handle)->SysWrite(m_handle->GetFd(),s,n) : -1;      }
0120 
0121   /// Specialization for the usage of TFile structures
0122   template<> std::streampos dd4hep_file<TFile*>::seek(stream_offset off, BOOST_IOS::seekdir way)
0123   { return m_handle ? _p(m_handle)->SysSeek(m_handle->GetFd(),off,way) : -1;   }
0124 
0125   /// Specialization for the usage of TFile structures
0126   template<> void dd4hep_file<TFile*>::close()
0127   { if ( m_handle )  { m_handle->Close(); delete m_handle; m_handle=0; }       }
0128 
0129 }