The Gaudi Framework
master (9f3f8895)
Toggle main menu visibility
Loading...
Searching...
No Matches
RootFileHandler.cpp
Go to the documentation of this file.
1
/***********************************************************************************\
2
* (c) Copyright 1998-2026 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
\***********************************************************************************/
11
#include "
RootFileHandler.h
"
12
#include <
GaudiKernel/IFileMgr.h
>
13
#include <
GaudiKernel/MsgStream.h
>
14
#include <TFile.h>
15
#include <TROOT.h>
16
17
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
18
19
RootFileHandler::RootFileHandler
(
IMessageSvc
* msg ) :
m_log
( msg,
"RootFileHandler"
) {
20
// Protect against multiple instances of TROOT
21
if
( !gROOT ) {
static
TROOT root(
"root"
,
"ROOT I/O"
); }
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
m_log
.setLevel(
m_level
);
31
32
if
(
m_log
.level() <=
MSG::DEBUG
)
33
m_log
<<
MSG::DEBUG
<<
"openRootFile(\""
<< n <<
"\","
<< f <<
","
<< desc <<
")"
<<
endmsg
;
34
35
ptr =
nullptr
;
36
fd = -1;
37
38
std::string opt;
39
40
if
( f ==
Io::READ
) {
41
opt =
"READ"
;
42
}
else
if
( f == (
Io::WRITE
|
Io::CREATE
|
Io::EXCL
) ) {
43
opt =
"NEW"
;
44
}
else
if
( f == (
Io::WRITE
|
Io::CREATE
) ) {
45
opt =
"RECREATE"
;
46
}
else
if
( f |
Io::APPEND
) {
47
opt =
"UPDATE"
;
48
}
else
{
49
m_log
<<
MSG::ERROR
<<
"Don't know how to handle IoFlag "
<< f <<
endmsg
;
50
return
1;
51
}
52
53
std::unique_ptr<TFile> tf;
54
55
try
{
56
tf.reset( TFile::Open( n.c_str(), opt.c_str() ) );
57
}
catch
(
const
std::exception& Exception ) {
58
m_log
<<
MSG::ERROR
<<
"exception caught while trying to open root"
59
<<
" file for reading: "
<< Exception.what() << std::endl
60
<<
" -> file probably corrupt."
<<
endmsg
;
61
return
1;
62
}
catch
( ... ) {
63
m_log
<<
MSG::ERROR
<<
"Problems opening input file \""
<< n <<
"\": probably corrupt"
<<
endmsg
;
64
return
1;
65
}
66
67
if
( !tf || !tf->IsOpen() ) {
68
m_log
<<
MSG::ERROR
<<
"Unable to open ROOT file \""
<< n <<
"\" with options \""
<< opt <<
"\""
<<
endmsg
;
69
70
tf.reset();
71
return
1;
72
}
73
74
fd = tf->GetFd();
75
76
ptr = tf.release();
77
78
if
(
m_log
.level() <=
MSG::DEBUG
)
m_log
<<
MSG::DEBUG
<<
"opened TFile "
<< ptr <<
" Fd: "
<< fd <<
endmsg
;
79
80
return
0;
81
}
82
83
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
84
85
Io::close_t
RootFileHandler::closeRootFile
(
void
* ptr ) {
86
87
if
(
m_log
.level() <=
MSG::DEBUG
)
m_log
<<
MSG::DEBUG
<<
"closeRootFile(ptr:"
<< ptr <<
")"
<<
endmsg
;
88
89
if
( !ptr ) {
90
m_log
<<
MSG::ERROR
<<
"Unable to close file: ptr == 0"
<<
endmsg
;
91
return
-1;
92
}
93
94
TFile* tf =
static_cast<
TFile*
>
( ptr );
95
96
try
{
97
tf->Close();
98
}
catch
(
const
std::exception& Exception ) {
99
m_log
<<
MSG::ERROR
<<
"exception caught while trying to close root"
100
<<
" file"
<< Exception.what() <<
endmsg
;
101
return
-1;
102
}
catch
( ... ) {
103
m_log
<<
MSG::ERROR
<<
"Problems closing ROOT file \""
<< tf->GetName() <<
"\""
<<
endmsg
;
104
return
-1;
105
}
106
107
return
0;
108
}
109
110
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
111
112
Io::reopen_t
RootFileHandler::reopenRootFile
(
void
*,
const
Io::IoFlags
& ) {
113
114
m_log
<<
MSG::ERROR
<<
"reopen not implemented"
<<
endmsg
;
115
return
-1;
116
}
IFileMgr.h
MsgStream.h
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition
MsgStream.h:198
RootFileHandler.h
IMessageSvc
The IMessage is the interface implemented by the message service.
Definition
IMessageSvc.h:34
Io::IoFlags
Definition
IFileMgr.h:43
RootFileHandler::reopenRootFile
Io::reopen_t reopenRootFile(void *, const Io::IoFlags &)
Definition
RootFileHandler.cpp:112
RootFileHandler::RootFileHandler
RootFileHandler(IMessageSvc *)
Definition
RootFileHandler.cpp:19
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
RootFileHandler::closeRootFile
Io::close_t closeRootFile(void *ptr)
Definition
RootFileHandler.cpp:85
RootFileHandler::m_level
int m_level
Definition
RootFileHandler.h:39
RootFileHandler::m_log
MsgStream m_log
Definition
RootFileHandler.h:38
Io::reopen_t
int reopen_t
Definition
IFileMgr.h:232
Io::READ
@ READ
Definition
IFileMgr.h:30
Io::CREATE
@ CREATE
Definition
IFileMgr.h:34
Io::WRITE
@ WRITE
Definition
IFileMgr.h:31
Io::EXCL
@ EXCL
Definition
IFileMgr.h:35
Io::APPEND
@ APPEND
Definition
IFileMgr.h:38
Io::Fd
int Fd
Definition
IFileMgr.h:158
Io::close_t
int close_t
Definition
IFileMgr.h:231
Io::open_t
int open_t
Definition
IFileMgr.h:230
MSG::DEBUG
@ DEBUG
Definition
IMessageSvc.h:22
MSG::ERROR
@ ERROR
Definition
IMessageSvc.h:22
GaudiSvc
src
FileMgr
RootFileHandler.cpp
Generated on
for The Gaudi Framework by
1.17.0