The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
RootFileHandler.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3* *
4* This software is distributed under the terms of the Apache version 2 licence, *
5* copied verbatim in the file "LICENSE". *
6* *
7* In applying this licence, CERN does not waive the privileges and immunities *
8* granted to it by virtue of its status as an Intergovernmental Organization *
9* or submit itself to any jurisdiction. *
10\***********************************************************************************/
12#include <TFile.h>
13#include <TROOT.h>
14#include <TSSLSocket.h>
15#include <TWebFile.h>
16
17#include "RootFileHandler.h"
19#include <boost/algorithm/string.hpp>
20
21namespace ba = boost::algorithm;
22
23/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24
25RootFileHandler::RootFileHandler( IMessageSvc* msg, const std::string& p, const std::string& c )
26 : m_log( msg, "RootFileHandler" ), m_userProxy( p ), m_certDir( c ) {
27 // Protect against multiple instances of TROOT
28 if ( !gROOT ) { static TROOT root( "root", "ROOT I/O" ); }
29 m_level = msg->outputLevel( "RootFileHandler" );
30}
31
32/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
33
34Io::open_t RootFileHandler::openRootFile( const std::string& n, const Io::IoFlags& f, const std::string& desc,
35 Io::Fd& fd, void*& ptr ) {
36
37 m_log.setLevel( m_level );
38
39 if ( m_log.level() <= MSG::DEBUG )
40 m_log << MSG::DEBUG << "openRootFile(\"" << n << "\"," << f << "," << desc << ")" << endmsg;
41
42 ptr = nullptr;
43 fd = -1;
44
45 std::string opt;
46
47 if ( f == Io::READ ) {
48 opt = "READ";
49 } else if ( f == ( Io::WRITE | Io::CREATE | Io::EXCL ) ) {
50 opt = "NEW";
51 } else if ( f == ( Io::WRITE | Io::CREATE ) ) {
52 opt = "RECREATE";
53 } else if ( f | Io::APPEND ) {
54 opt = "UPDATE";
55 } else {
56 m_log << MSG::ERROR << "Don't know how to handle IoFlag " << f << endmsg;
57 return 1;
58 }
59
60 std::unique_ptr<TFile> tf;
61
62 try {
63 tf.reset( TFile::Open( n.c_str(), opt.c_str() ) );
64 } catch ( const std::exception& Exception ) {
65 m_log << MSG::ERROR << "exception caught while trying to open root"
66 << " file for reading: " << Exception.what() << std::endl
67 << " -> file probably corrupt." << endmsg;
68 return 1;
69 } catch ( ... ) {
70 m_log << MSG::ERROR << "Problems opening input file \"" << n << "\": probably corrupt" << endmsg;
71 return 1;
72 }
73
74 if ( !tf || !tf->IsOpen() ) {
75 m_log << MSG::ERROR << "Unable to open ROOT file \"" << n << "\" with options \"" << opt << "\"" << endmsg;
76
77 tf.reset();
78 return 1;
79 }
80
81 fd = tf->GetFd();
82
83 ptr = tf.release();
84
85 if ( m_log.level() <= MSG::DEBUG ) m_log << MSG::DEBUG << "opened TFile " << ptr << " Fd: " << fd << endmsg;
86
87 return 0;
88}
89
90//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
91
93
94 if ( m_log.level() <= MSG::DEBUG ) m_log << MSG::DEBUG << "closeRootFile(ptr:" << ptr << ")" << endmsg;
95
96 if ( !ptr ) {
97 m_log << MSG::ERROR << "Unable to close file: ptr == 0" << endmsg;
98 return -1;
99 }
100
101 TFile* tf = static_cast<TFile*>( ptr );
102
103 try {
104 tf->Close();
105 } catch ( const std::exception& Exception ) {
106 m_log << MSG::ERROR << "exception caught while trying to close root"
107 << " file" << Exception.what() << endmsg;
108 return -1;
109 } catch ( ... ) {
110 m_log << MSG::ERROR << "Problems closing ROOT file \"" << tf->GetName() << "\"" << endmsg;
111 return -1;
112 }
113
114 return 0;
115}
116
117//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
118
120
121 m_log << MSG::ERROR << "reopen not implemented" << endmsg;
122 return -1;
123}
124
125//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
126
128
129 if ( m_log.level() <= MSG::DEBUG ) m_log << MSG::DEBUG << "setupSSL" << endmsg;
130
131 // don't set anything up
132 if ( m_userProxy == "NONE" || m_certDir == "NONE" ) {
133 m_ssl_setup = true;
134 return true;
135 }
136
137 // get stuff from $X509_USER_PROXY and $X509_CERT_DIR env vars
138 if ( m_userProxy == "X509" ) {
139 if ( !System::getEnv( "X509_USER_PROXY", m_userProxy ) ) {
140 m_log << MSG::ERROR << "env var X509_USER_PROXY not set" << endmsg;
141 return false;
142 }
143 }
144
145 if ( m_certDir == "X509" ) {
146 if ( !System::getEnv( "X509_CERT_DIR", m_certDir ) ) {
147 m_log << MSG::ERROR << "env var X509_CERT_DIR not set" << endmsg;
148 return false;
149 }
150 }
151
152 if ( m_log.level() <= MSG::DEBUG )
153 m_log << MSG::DEBUG << "userProxy: " << m_userProxy << " certDir: " << m_certDir << endmsg;
154
155 TSSLSocket::SetUpSSL( m_userProxy.c_str(), m_certDir.c_str(), m_userProxy.c_str(), m_userProxy.c_str() );
156
157 m_ssl_setup = true;
158
159 return true;
160}
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
The IMessage is the interface implemented by the message service.
Definition IMessageSvc.h:34
Io::reopen_t reopenRootFile(void *, const Io::IoFlags &)
Io::open_t openRootFile(const std::string &n, const Io::IoFlags &f, const std::string &desc, Io::Fd &fd, void *&ptr)
Io::close_t closeRootFile(void *ptr)
std::string m_certDir
RootFileHandler(IMessageSvc *, const std::string &userProxy, const std::string &certDir)
std::string m_userProxy
int reopen_t
Definition IFileMgr.h:232
@ READ
Definition IFileMgr.h:30
@ CREATE
Definition IFileMgr.h:34
@ WRITE
Definition IFileMgr.h:31
@ EXCL
Definition IFileMgr.h:35
@ APPEND
Definition IFileMgr.h:38
int Fd
Definition IFileMgr.h:158
int close_t
Definition IFileMgr.h:231
int open_t
Definition IFileMgr.h:230
@ DEBUG
Definition IMessageSvc.h:22
@ ERROR
Definition IMessageSvc.h:22
GAUDI_API std::string getEnv(const char *var)
get a particular environment variable (returning "UNKNOWN" if not set)
Definition System.cpp:329