The Gaudi Framework  master (3c045658)
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"
14#include <TFile.h>
15#include <TROOT.h>
16
17/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
18
19RootFileHandler::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
27Io::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
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
113
114 m_log << MSG::ERROR << "reopen not implemented" << endmsg;
115 return -1;
116}
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 &)
RootFileHandler(IMessageSvc *)
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)
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