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// fwk includes
18
21
22namespace Athena {
23
24RootSvc::RootSvc(const std::string& name, ISvcLocator* pSvcLocator) :
25 base_class(name, pSvcLocator),
26 m_gCatalogMgr(Gaudi::svcLocator()->service<Gaudi::IFileCatalogMgr>( "Gaudi::MultiFileCatalog" )),
28 m_conns(),
29 m_wconn(0),
30 m_dictSvc("AthDictLoaderSvc", name) {
31}
32
34 for (ConnMap_t::iterator itr = m_conns.begin(), iend = m_conns.end(); itr != iend; ++itr) {
35 delete itr->second; itr->second = 0;
36 }
37 m_conns.clear();
38}
39
40StatusCode RootSvc::initialize() {
41 ATH_MSG_INFO("Initializing " << name());
42 if (!::AthService::initialize().isSuccess()) {
43 ATH_MSG_FATAL("Cannot initialize ConversionSvc base class.");
44 return StatusCode::FAILURE;
45 }
46 try {
47 m_gCatalogMgr->addCatalog("xmlcatalog_file:RootFileCatalog.xml");
48 m_gCatalogMgr->setWriteCatalog(m_gCatalogMgr->findCatalog("file:RootFileCatalog.xml", true));
49 m_gCatalog->init();
50 } catch (std::exception& e) {
51 ATH_MSG_FATAL ("Set up Catalog - caught exception: " << e.what());
52 return StatusCode::FAILURE;
53 }
54 ATH_CHECK(m_dictSvc.retrieve());
55 return StatusCode::SUCCESS;
56}
57
58StatusCode RootSvc::finalize() {
59 for (ConnMap_t::const_iterator itr = m_conns.begin(), iend = m_conns.end(); itr != iend; ++itr) {
60 if (!itr->second->disconnect().isSuccess()) {
61 ATH_MSG_WARNING("Cannot disconnect file = " << itr->first.toString());
62 }
63 }
64 m_gCatalog->commit();
65 return ::AthService::finalize();
66}
67
69RootType RootSvc::getType(const std::type_info& type) const {
70 return m_dictSvc->load_type(type);
71}
72
74void* RootSvc::readObject(const Token& /*token*/, void*& /*pObj*/) {
75 return 0;
76}
77
79const Token* RootSvc::writeObject(const Placement& placement, const RootType& type, const void* pObj) {
80 ATH_MSG_VERBOSE("RootSvc::writeObject pObj = " << pObj);
81 if (m_wconn == 0) {
82 ATH_MSG_ERROR("Cannot write without RootConnection for placement " << placement.containerName());
83 return 0;
84 }
85 if (!m_wconn->setContainer(placement.containerName(), type.Name()).isSuccess()) {
86 ATH_MSG_ERROR("Cannot set container [" << placement.containerName() << "]");
87 return 0;
88 }
89 unsigned long ientry = 0;
90 if (!m_wconn->write(pObj, ientry).isSuccess()) {
91 ATH_MSG_ERROR("Cannot write Object to placement [" << placement.containerName() << "]");
92 return 0;
93 }
94 return new Token();
95}
96
98void* RootSvc::createObject(const RootType& type) const {
99 void* pObj = type.Construct();
100 return pObj;
101}
102
104void RootSvc::destructObject(const RootType& /*type*/, void* /*pObj*/) const {
105}
106
108StatusCode RootSvc::open(const std::string& fname, const std::string& /*mode*/) {
109// Catalog to get fid...
110 Guid fid = Guid::null();
111 std::string fidString;
112 fidString = m_gCatalog->lookupPFN(fname);
113 if( fidString.empty() ) {
114 fidString = m_gCatalog->createFID();
115 m_gCatalog->registerPFN(fidString, fname, "ROOT_All");
116 }
117 fid.fromString(fidString);
118 Athena::RootConnection* conn = 0;
119 ConnMap_t::const_iterator fitr = m_conns.find(fid);
120 if (fitr == m_conns.end()) {
121 conn = new Athena::RootConnection(this, fname);
122 m_conns.insert(std::make_pair(fid, conn));
123 } else {
124 conn = fitr->second;
125 }
126 if (conn == 0) {
127 ATH_MSG_ERROR("Cannot get RootConnection for file " << fid.toString());
128 return StatusCode::FAILURE;
129 }
130 return StatusCode::SUCCESS;
131}
132
134StatusCode RootSvc::connect(const std::string& fname) {
135 ATH_MSG_VERBOSE("connect(" << fname << ")...");
136 Athena::RootConnection* conn = this->connection(fname);
137 if (conn == 0) {
138 ATH_MSG_ERROR("No open RootConnection for file " << fname);
139 return StatusCode::FAILURE;
140 }
141 if (!conn->connectWrite("recreate").isSuccess()) {
142 ATH_MSG_ERROR("Cannot connect to file " << fname);
143 return StatusCode::FAILURE;
144 }
145 m_wconn = conn;
146 return StatusCode::SUCCESS;
147}
148
150 ATH_MSG_VERBOSE("RootSvc::commitOutput");
151 if (m_wconn == 0) {
152 ATH_MSG_ERROR("Cannot commit without RootConnection.");
153 return StatusCode::FAILURE;
154 }
155 if (!m_wconn->commit().isSuccess()) {
156 ATH_MSG_ERROR("Cannot commit RootConnection.");
157 return StatusCode::FAILURE;
158 }
159 return StatusCode::SUCCESS;
160}
161
163StatusCode RootSvc::disconnect(const std::string& fname) {
164 ATH_MSG_VERBOSE("disconnect(" << fname << ")...");
165 Athena::RootConnection* conn = this->connection(fname);
166 if (conn == 0) {
167 ATH_MSG_ERROR("No open RootConnection for file " << fname);
168 return StatusCode::FAILURE;
169 }
170 if (!conn->disconnect().isSuccess()) {
171 ATH_MSG_ERROR("Cannot disconnect to file " << fname);
172 return StatusCode::FAILURE;
173 }
174 if (m_wconn == conn) {
175 m_wconn = 0;
176 }
177 return StatusCode::SUCCESS;
178}
179
182Athena::RootConnection* RootSvc::connection(const std::string& fname) {
183// Catalog to get fid...
184 Guid fid = Guid::null();
185 std::string fidString;
186 fidString = m_gCatalog->lookupPFN(fname);
187 if( fidString.empty() ) {
188 fidString = m_gCatalog->createFID();
189 m_gCatalog->registerPFN(fidString, fname, "ROOT_All");
190 }
191 fid.fromString(fidString);
192 Athena::RootConnection* conn = 0;
193 ConnMap_t::const_iterator fitr = m_conns.find(fid);
194 if (fitr != m_conns.end()) {
195 conn = fitr->second;
196 }
197 return conn;
198}
199
200} //> 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...
SmartIF< Gaudi::IFileCatalog > m_gCatalog
Definition RootSvc.h:96
StatusCode initialize()
Gaudi Service Interface method implementations:
Definition RootSvc.cxx:40
StatusCode open(const std::string &fname, const std::string &mode)
Open the file fname with open mode mode.
Definition RootSvc.cxx:108
ConnMap_t m_conns
Map of file name keys and connection values.
Definition RootSvc.h:100
RootType getType(const std::type_info &type) const
Load the type (dictionary) from Root.
Definition RootSvc.cxx:69
void destructObject(const RootType &type, void *pObj) const
Destruct a given object of type RootType.
Definition RootSvc.cxx:104
StatusCode disconnect(const std::string &fname)
Disconnect the file fname from the service.
Definition RootSvc.cxx:163
void * readObject(const Token &token, void *&pObj)
Read object from Root.
Definition RootSvc.cxx:74
const Token * writeObject(const Placement &placement, const RootType &type, const void *pObj)
Write object of a given class to Root.
Definition RootSvc.cxx:79
StatusCode connect(const std::string &fname)
Connect the file fname to the service.
Definition RootSvc.cxx:134
Athena::RootConnection * m_wconn
Definition RootSvc.h:101
StatusCode commitOutput()
Commit data and flush buffer.
Definition RootSvc.cxx:149
Athena::RootConnection * connection(const std::string &fname)
Get the RootConnection associated with file fname.
Definition RootSvc.cxx:182
void * createObject(const RootType &type) const
Create an object of a given RootType.
Definition RootSvc.cxx:98
RootSvc()
Default constructor:
ServiceHandle< ::IDictLoaderSvc > m_dictSvc
ServiceHandle to the dictionary service.
Definition RootSvc.h:104
virtual ~RootSvc()
Destructor.
Definition RootSvc.cxx:33
SmartIF< Gaudi::IFileCatalogMgr > m_gCatalogMgr
Definition RootSvc.h:95
StatusCode finalize()
Definition RootSvc.cxx:58
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:20
const std::string & containerName() const
Access container name.
Definition Placement.h:33
This class provides a token that identifies in a unique way objects on the persistent storage.
Definition Token.h:22
Some weak symbol referencing magic... These are declared in AthenaKernel/getMessageSvc....
Definition AthDsoUtils.h:10
=============================================================================