ATLAS Offline Software
Loading...
Searching...
No Matches
RootSvc.cxx
Go to the documentation of this file.
1
2
3/*
4 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
5*/
6
7// RootSvc.cxx
8// Implementation file for class Athena::RootSvc
9// Author: Peter van Gemmeren <gemmeren@anl.gov>
11
12// AthenaRootComps includes
13#include "RootSvc.h"
14#include "RootConnection.h"
15
16// POOL/APR includes for Catalog
18
19// fwk includes
21
24
25namespace Athena {
26
27RootSvc::RootSvc(const std::string& name, ISvcLocator* pSvcLocator) :
28 base_class(name, pSvcLocator),
29 m_catalog(0),
30 m_conns(),
31 m_wconn(0),
32 m_dictSvc("AthDictLoaderSvc", name) {
33}
34
36 for (ConnMap_t::iterator itr = m_conns.begin(), iend = m_conns.end(); itr != iend; ++itr) {
37 delete itr->second; itr->second = 0;
38 }
39 m_conns.clear();
40}
41
42StatusCode RootSvc::initialize() {
43 ATH_MSG_INFO("Initializing " << name());
44 if (!::AthService::initialize().isSuccess()) {
45 ATH_MSG_FATAL("Cannot initialize ConversionSvc base class.");
46 return StatusCode::FAILURE;
47 }
49 try {
50 m_catalog->setWriteCatalog("xmlcatalog_file:RootFileCatalog.xml"); // FIXME: Make config
51 m_catalog->start();
52 } catch (std::exception& e) {
53 ATH_MSG_FATAL ("Set up Catalog - caught exception: " << e.what());
54 return StatusCode::FAILURE;
55 }
56 ATH_CHECK(m_dictSvc.retrieve());
57 return StatusCode::SUCCESS;
58}
59
60StatusCode RootSvc::finalize() {
61 for (ConnMap_t::const_iterator itr = m_conns.begin(), iend = m_conns.end(); itr != iend; ++itr) {
62 if (!itr->second->disconnect().isSuccess()) {
63 ATH_MSG_WARNING("Cannot disconnect file = " << itr->first.toString());
64 }
65 }
66 if (m_catalog != 0) {
67 m_catalog->commit();
68 delete m_catalog; m_catalog = 0;
69 }
70 return ::AthService::finalize();
71}
72
74RootType RootSvc::getType(const std::type_info& type) const {
75 return m_dictSvc->load_type(type);
76}
77
79void* RootSvc::readObject(const Token& /*token*/, void*& /*pObj*/) {
80 return 0;
81}
82
84const Token* RootSvc::writeObject(const Placement& placement, const RootType& type, const void* pObj) {
85 ATH_MSG_VERBOSE("RootSvc::writeObject pObj = " << pObj);
86 if (m_wconn == 0) {
87 ATH_MSG_ERROR("Cannot write without RootConnection for placement " << placement.containerName());
88 return 0;
89 }
90 if (!m_wconn->setContainer(placement.containerName(), type.Name()).isSuccess()) {
91 ATH_MSG_ERROR("Cannot set container [" << placement.containerName() << "]");
92 return 0;
93 }
94 unsigned long ientry = 0;
95 if (!m_wconn->write(pObj, ientry).isSuccess()) {
96 ATH_MSG_ERROR("Cannot write Object to placement [" << placement.containerName() << "]");
97 return 0;
98 }
99 return new Token();
100}
101
104 void* pObj = type.Construct();
105 return pObj;
106}
107
109void RootSvc::destructObject(const RootType& /*type*/, void* /*pObj*/) const {
110}
111
113StatusCode RootSvc::open(const std::string& fname, const std::string& /*mode*/) {
114// Catalog to get fid...
115 Guid fid = Guid::null();
116 if (m_catalog != 0) {
117 std::string fidString, ftype;
118 m_catalog->lookupFileByPFN(fname, fidString, ftype);
119 if( fidString.empty() ) {
120 m_catalog->registerPFN(fname, "ROOT_All", fidString);
121 }
122 fid.fromString(fidString);
123 }
124 Athena::RootConnection* conn = 0;
125 ConnMap_t::const_iterator fitr = m_conns.find(fid);
126 if (fitr == m_conns.end()) {
127 conn = new Athena::RootConnection(this, fname);
128 m_conns.insert(std::make_pair(fid, conn));
129 } else {
130 conn = fitr->second;
131 }
132 if (conn == 0) {
133 ATH_MSG_ERROR("Cannot get RootConnection for file " << fid.toString());
134 return StatusCode::FAILURE;
135 }
136 return StatusCode::SUCCESS;
137}
138
140StatusCode RootSvc::connect(const std::string& fname) {
141 ATH_MSG_VERBOSE("connect(" << fname << ")...");
142 Athena::RootConnection* conn = this->connection(fname);
143 if (conn == 0) {
144 ATH_MSG_ERROR("No open RootConnection for file " << fname);
145 return StatusCode::FAILURE;
146 }
147 if (!conn->connectWrite("recreate").isSuccess()) {
148 ATH_MSG_ERROR("Cannot connect to file " << fname);
149 return StatusCode::FAILURE;
150 }
151 m_wconn = conn;
152 return StatusCode::SUCCESS;
153}
154
156 ATH_MSG_VERBOSE("RootSvc::commitOutput");
157 if (m_wconn == 0) {
158 ATH_MSG_ERROR("Cannot commit without RootConnection.");
159 return StatusCode::FAILURE;
160 }
161 if (!m_wconn->commit().isSuccess()) {
162 ATH_MSG_ERROR("Cannot commit RootConnection.");
163 return StatusCode::FAILURE;
164 }
165 return StatusCode::SUCCESS;
166}
167
169StatusCode RootSvc::disconnect(const std::string& fname) {
170 ATH_MSG_VERBOSE("disconnect(" << fname << ")...");
171 Athena::RootConnection* conn = this->connection(fname);
172 if (conn == 0) {
173 ATH_MSG_ERROR("No open RootConnection for file " << fname);
174 return StatusCode::FAILURE;
175 }
176 if (!conn->disconnect().isSuccess()) {
177 ATH_MSG_ERROR("Cannot disconnect to file " << fname);
178 return StatusCode::FAILURE;
179 }
180 if (m_wconn == conn) {
181 m_wconn = 0;
182 }
183 return StatusCode::SUCCESS;
184}
185
188Athena::RootConnection* RootSvc::connection(const std::string& fname) {
189// Catalog to get fid...
190 Guid fid = Guid::null();
191 if (m_catalog != 0) {
192 std::string fidString, ftype;
193 m_catalog->lookupFileByPFN(fname, fidString, ftype);
194 if( fidString.empty() ) {
195 m_catalog->registerPFN(fname, "ROOT_All", fidString);
196 }
197 fid.fromString(fidString);
198 }
199 Athena::RootConnection* conn = 0;
200 ConnMap_t::const_iterator fitr = m_conns.find(fid);
201 if (fitr != m_conns.end()) {
202 conn = fitr->second;
203 }
204 return conn;
205}
206
207} //> namespace Athena
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
This file contains the class definition for the Placement class (migrated from POOL).
This file contains the class definition for the Athena::RootConnection class.
This file contains the class definition for the RootSvc class.
TTypeAdapter RootType
Definition RootType.h:211
This file contains the class definition for the Token class (migrated from POOL).
This class provides the implementation of Athena::RootConnection class, similar to Gaudi IDataConnect...
StatusCode initialize()
Gaudi Service Interface method implementations:
Definition RootSvc.cxx:42
StatusCode open(const std::string &fname, const std::string &mode)
Open the file fname with open mode mode
Definition RootSvc.cxx:113
ConnMap_t m_conns
Map of file name keys and connection values.
Definition RootSvc.h:96
RootType getType(const std::type_info &type) const
Load the type (dictionary) from Root.
Definition RootSvc.cxx:74
void destructObject(const RootType &type, void *pObj) const
Destruct a given object of type RootType.
Definition RootSvc.cxx:109
StatusCode disconnect(const std::string &fname)
Disconnect the file fname from the service.
Definition RootSvc.cxx:169
void * readObject(const Token &token, void *&pObj)
Read object from Root.
Definition RootSvc.cxx:79
const Token * writeObject(const Placement &placement, const RootType &type, const void *pObj)
Write object of a given class to Root.
Definition RootSvc.cxx:84
pool::IFileCatalog * m_catalog
Definition RootSvc.h:92
StatusCode connect(const std::string &fname)
Connect the file fname to the service.
Definition RootSvc.cxx:140
Athena::RootConnection * m_wconn
Definition RootSvc.h:97
StatusCode commitOutput()
Commit data and flush buffer.
Definition RootSvc.cxx:155
Athena::RootConnection * connection(const std::string &fname)
Get the RootConnection associated with file fname
Definition RootSvc.cxx:188
void * createObject(const RootType &type) const
Create an object of a given RootType.
Definition RootSvc.cxx:103
RootSvc()
Default constructor:
ServiceHandle< ::IDictLoaderSvc > m_dictSvc
ServiceHandle to the dictionary service.
Definition RootSvc.h:100
virtual ~RootSvc()
Destructor.
Definition RootSvc.cxx:35
StatusCode finalize()
Definition RootSvc.cxx:60
This class provides a encapsulation of a GUID/UUID/CLSID/IID data structure (128 bit number).
Definition Guid.h:25
constexpr void fromString(std::string_view s)
Automatic conversion from string representation.
Definition Guid.h:143
static const Guid & null() noexcept
NULL-Guid: static class method.
Definition Guid.cxx:14
constexpr void toString(std::span< char, StrLen > buf, bool uppercase=true) const noexcept
Automatic conversion to string representation.
This class holds all the necessary information to guide the writing of an object in a physical place.
Definition Placement.h:19
const std::string & containerName() const
Access container name.
Definition Placement.h:32
This class provides a token that identifies in a unique way objects on the persistent storage.
Definition Token.h:21
Some weak symbol referencing magic... These are declared in AthenaKernel/getMessageSvc....