| The Gaudi Framework v27r1

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