| The Gaudi Framework v30r0 (c919700c)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

RootFileHandler.cpp
Go to the documentation of this file.
1 #include "GaudiKernel/IFileMgr.h"
2 #include "TFile.h"
3 #include "TROOT.h"
4 #include "TSSLSocket.h"
5 #include "TWebFile.h"
6
7 #include "GaudiKernel/MsgStream.h"
8 #include "RootFileHandler.h"
9 #include "boost/algorithm/string.hpp"
10
11 namespace ba = boost::algorithm;
12
13 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
14
15 RootFileHandler::RootFileHandler( IMessageSvc* msg, const std::string& p, const std::string& c )
16 : m_log( msg, "RootFileHandler" ), m_userProxy( p ), m_certDir( c )
17 {
18 // Protect against multiple instances of TROOT
19 if ( !gROOT ) {
20 static TROOT root( "root", "ROOT I/O" );
21 }
22 m_level = msg->outputLevel( "RootFileHandler" );
23 }
24
25 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
26
27 Io::open_t RootFileHandler::openRootFile( const std::string& n, const Io::IoFlags& f, const std::string& desc,
28 Io::Fd& fd, void*& ptr )
29 {
30
31 m_log.setLevel( m_level );
32
33 if ( m_log.level() <= MSG::DEBUG )
34 m_log << MSG::DEBUG << "openRootFile(\"" << n << "\"," << f << "," << desc << ")" << endmsg;
35
36 ptr = nullptr;
37 fd = -1;
38
39 std::string opt;
40
41 if ( f == Io::READ ) {
42 opt = "READ";
43 } else if ( f == ( Io::WRITE | Io::CREATE | Io::EXCL ) ) {
44 opt = "NEW";
45 } else if ( f == ( Io::WRITE | Io::CREATE ) ) {
46 opt = "RECREATE";
47 } else if ( ( f | Io::APPEND ) != 0 ) {
48 opt = "UPDATE";
49 } else {
50 m_log << MSG::ERROR << "Don't know how to handle IoFlag " << f << endmsg;
51 return 1;
52 }
53
54 std::unique_ptr<TFile> tf;
55
56 if ( ba::starts_with( n, "https://", ba::is_iequal{} ) || ba::starts_with( n, "http://", ba::is_iequal{} ) ) {
57
58 if ( !f.isRead() ) {
59 m_log << MSG::ERROR << "can only open web files in READ mode. "
60 << "requested mode is: " << f << endmsg;
61 return 1;
62 }
63
64 if ( !m_ssl_setup && ba::starts_with( n, "https://", ba::is_iequal{} ) ) {
65
66 if ( !setupSSL() ) {
67 m_log << MSG::ERROR << "Unable to setup TSSLSocket for ROOT TWebFile access over https" << endmsg;
68 }
69 }
70
71 try {
72 tf.reset( new TWebFile( n.c_str() ) );
73 } catch ( const std::exception& Exception ) {
74 m_log << MSG::ERROR << "exception caught while trying to open root"
75 << " file for reading: " << Exception.what() << std::endl
76 << " -> file probably corrupt." << endmsg;
77 return 1;
78 } catch ( ... ) {
79 m_log << MSG::ERROR << "Problems opening input file \"" << n << "\": probably corrupt" << endmsg;
80 return 1;
81 }
82
83 if ( tf && tf->IsZombie() ) {
84 m_log << MSG::ERROR << "Problems opening input file \"" << n << "\": file does not exist or in not accessible"
85 << endmsg;
86 tf.reset();
87 return 1;
88 }
89
90 } else {
91
92 try {
93 tf.reset( TFile::Open( n.c_str(), opt.c_str() ) );
94 } catch ( const std::exception& Exception ) {
95 m_log << MSG::ERROR << "exception caught while trying to open root"
96 << " file for reading: " << Exception.what() << std::endl
97 << " -> file probably corrupt." << endmsg;
98 return 1;
99 } catch ( ... ) {
100 m_log << MSG::ERROR << "Problems opening input file \"" << n << "\": probably corrupt" << endmsg;
101 return 1;
102 }
103 }
104
105 if ( !tf || !tf->IsOpen() ) {
106 m_log << MSG::ERROR << "Unable to open ROOT file \"" << n << "\" with options \"" << opt << "\"" << endmsg;
107
108 tf.reset();
109 return 1;
110 }
111
112 fd = tf->GetFd();
113
114 ptr = tf.release();
115
116 if ( m_log.level() <= MSG::DEBUG ) m_log << MSG::DEBUG << "opened TFile " << ptr << " Fd: " << fd << endmsg;
117
118 return 0;
119 }
120
121 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
122
123 Io::close_t RootFileHandler::closeRootFile( void* ptr )
124 {
125
126 if ( m_log.level() <= MSG::DEBUG ) m_log << MSG::DEBUG << "closeRootFile(ptr:" << ptr << ")" << endmsg;
127
128 if ( !ptr ) {
129 m_log << MSG::ERROR << "Unable to close file: ptr == 0" << endmsg;
130 return -1;
131 }
132
133 TFile* tf = static_cast<TFile*>( ptr );
134
135 try {
136 tf->Close();
137 } catch ( const std::exception& Exception ) {
138 m_log << MSG::ERROR << "exception caught while trying to close root"
139 << " file" << Exception.what() << endmsg;
140 return -1;
141 } catch ( ... ) {
142 m_log << MSG::ERROR << "Problems closing ROOT file \"" << tf->GetName() << "\"" << endmsg;
143 return -1;
144 }
145
146 return 0;
147 }
148
149 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
150
151 Io::reopen_t RootFileHandler::reopenRootFile( void*, const Io::IoFlags& )
152 {
153
154 m_log << MSG::ERROR << "reopen not implemented" << endmsg;
155 return -1;
156 }
157
158 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
159
160 bool RootFileHandler::setupSSL()
161 {
162
163 if ( m_log.level() <= MSG::DEBUG ) m_log << MSG::DEBUG << "setupSSL" << endmsg;
164
165 // don't set anything up
166 if ( m_userProxy == "NONE" || m_certDir == "NONE" ) {
167 m_ssl_setup = true;
168 return true;
169 }
170
171 // get stuff from $X509_USER_PROXY and $X509_CERT_DIR env vars
172 if ( m_userProxy == "X509" ) {
173 if ( !System::getEnv( "X509_USER_PROXY", m_userProxy ) ) {
174 m_log << MSG::ERROR << "env var X509_USER_PROXY not set" << endmsg;
175 return false;
176 }
177 }
178
179 if ( m_certDir == "X509" ) {
180 if ( !System::getEnv( "X509_CERT_DIR", m_certDir ) ) {
181 m_log << MSG::ERROR << "env var X509_CERT_DIR not set" << endmsg;
182 return false;
183 }
184 }
185
186 if ( m_log.level() <= MSG::DEBUG )
187 m_log << MSG::DEBUG << "userProxy: " << m_userProxy << " certDir: " << m_certDir << endmsg;
188
189 TSSLSocket::SetUpSSL( m_userProxy.c_str(), m_certDir.c_str(), m_userProxy.c_str(), m_userProxy.c_str() );
190
191 m_ssl_setup = true;
192
193 return true;
194 }
System::getEnv
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition: System.cpp:415
gaudirun.fd
fd
Definition: gaudirun.py:478
gaudirun.c
c
Definition: gaudirun.py:407
MsgStream.h
Io::reopen_t
int reopen_t
Definition: IFileMgr.h:251
Io::Fd
int Fd
Definition: IFileMgr.h:171
MSG::DEBUG
Definition: IMessageSvc.h:16
std::endl
T endl(T...args)
RootFileHandler::setupSSL
bool setupSSL()
Definition: RootFileHandler.cpp:160
RootFileHandler::openRootFile
Io::open_t openRootFile(const std::string &n, const Io::IoFlags &f, const std::string &desc, Io::Fd &fd, void *&ptr)
Definition: RootFileHandler.cpp:27
Io::open_t
int open_t
Definition: IFileMgr.h:249
Io::EXCL
Definition: IFileMgr.h:32
RootFileHandler::closeRootFile
Io::close_t closeRootFile(void *ptr)
Definition: RootFileHandler.cpp:123
std::unique_ptr::release
T release(T...args)
RootFileHandler::reopenRootFile
Io::reopen_t reopenRootFile(void *, const Io::IoFlags &)
Definition: RootFileHandler.cpp:151
std::string
STL class.
RootFileHandler::m_userProxy
std::string m_userProxy
Definition: RootFileHandler.h:35
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:228
RootFileHandler::m_certDir
std::string m_certDir
Definition: RootFileHandler.h:35
std::exception::what
T what(T...args)
RootFileHandler::m_ssl_setup
bool m_ssl_setup
Definition: RootFileHandler.h:36
Io::WRITE
Definition: IFileMgr.h:28
Io::IoFlags
Definition: IFileMgr.h:40
IMessageSvc
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:38
Io::READ
Definition: IFileMgr.h:27
RootFileHandler::m_level
int m_level
Definition: RootFileHandler.h:33
std::unique_ptr::reset
T reset(T...args)
std::exception
STL class.
Io::APPEND
Definition: IFileMgr.h:35
std::unique_ptr< TFile >
Io::CREATE
Definition: IFileMgr.h:31
std::string::c_str
T c_str(T...args)
MsgStream::setLevel
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:102
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:8
Io::IoFlags::isRead
bool isRead() const
Definition: IFileMgr.h:56
MSG::ERROR
Definition: IMessageSvc.h:16
gaudiComponentHelp.root
root
Definition: gaudiComponentHelp.py:18
IFileMgr.h
MsgStream::level
MSG::Level level() const
Retrieve output level.
Definition: MsgStream.h:108
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
Io::close_t
int close_t
Definition: IFileMgr.h:250
RootFileHandler.h
RootFileHandler::RootFileHandler
RootFileHandler(IMessageSvc *, const std::string &userProxy, const std::string &certDir)
Definition: RootFileHandler.cpp:15
RootFileHandler::m_log
MsgStream m_log
Definition: RootFileHandler.h:32
IMessageSvc::outputLevel
virtual int outputLevel() const =0
Retrieve the current output level threshold.