ATLAS Offline Software
RootSvc.cxx
Go to the documentation of this file.
1 
3 /*
4  Copyright (C) 2002-2024 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 
25 namespace Athena {
26 
27 RootSvc::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 
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->connect();
52  m_catalog->start();
53  } catch (std::exception& e) {
54  ATH_MSG_FATAL ("Set up Catalog - caught exception: " << e.what());
55  return StatusCode::FAILURE;
56  }
57  ATH_CHECK(m_dictSvc.retrieve());
58  return StatusCode::SUCCESS;
59 }
60 
62  for (ConnMap_t::const_iterator itr = m_conns.begin(), iend = m_conns.end(); itr != iend; ++itr) {
63  if (!itr->second->disconnect().isSuccess()) {
64  ATH_MSG_WARNING("Cannot disconnect file = " << itr->first.toString());
65  }
66  }
67  if (m_catalog != 0) {
68  m_catalog->commit();
69  delete m_catalog; m_catalog = 0;
70  }
72 }
73 
75 RootType RootSvc::getType(const std::type_info& type) const {
76  return m_dictSvc->load_type(type);
77 }
78 
80 void* RootSvc::readObject(const Token& /*token*/, void*& /*pObj*/) {
81  return 0;
82 }
83 
85 const Token* RootSvc::writeObject(const Placement& placement, const RootType& type, const void* pObj) {
86  ATH_MSG_VERBOSE("RootSvc::writeObject pObj = " << pObj);
87  if (m_wconn == 0) {
88  ATH_MSG_ERROR("Cannot write without RootConnection for placement " << placement.containerName());
89  return 0;
90  }
91  if (!m_wconn->setContainer(placement.containerName(), type.Name()).isSuccess()) {
92  ATH_MSG_ERROR("Cannot set container [" << placement.containerName() << "]");
93  return 0;
94  }
95  unsigned long ientry = 0;
96  if (!m_wconn->write(pObj, ientry).isSuccess()) {
97  ATH_MSG_ERROR("Cannot write Object to placement [" << placement.containerName() << "]");
98  return 0;
99  }
100  return new Token();
101 }
102 
104 void* RootSvc::createObject(const RootType& type) const {
105  void* pObj = type.Construct();
106  return pObj;
107 }
108 
110 void RootSvc::destructObject(const RootType& /*type*/, void* /*pObj*/) const {
111 }
112 
114 StatusCode RootSvc::open(const std::string& fname, const std::string& /*mode*/) {
115 // Catalog to get fid...
116  Guid fid = Guid::null();
117  if (m_catalog != 0) {
118  std::string fidString, ftype;
119  m_catalog->lookupFileByPFN(fname, fidString, ftype);
120  if( fidString.empty() ) {
121  m_catalog->registerPFN(fname, "ROOT_All", fidString);
122  }
123  fid.fromString(fidString);
124  }
126  ConnMap_t::const_iterator fitr = m_conns.find(fid);
127  if (fitr == m_conns.end()) {
128  conn = new Athena::RootConnection(this, fname);
129  m_conns.insert(std::make_pair(fid, conn));
130  } else {
131  conn = fitr->second;
132  }
133  if (conn == 0) {
134  ATH_MSG_ERROR("Cannot get RootConnection for file " << fid.toString());
135  return StatusCode::FAILURE;
136  }
137  return StatusCode::SUCCESS;
138 }
139 
141 StatusCode RootSvc::connect(const std::string& fname) {
142  ATH_MSG_VERBOSE("connect(" << fname << ")...");
143  Athena::RootConnection* conn = this->connection(fname);
144  if (conn == 0) {
145  ATH_MSG_ERROR("No open RootConnection for file " << fname);
146  return StatusCode::FAILURE;
147  }
148  if (!conn->connectWrite("recreate").isSuccess()) {
149  ATH_MSG_ERROR("Cannot connect to file " << fname);
150  return StatusCode::FAILURE;
151  }
152  m_wconn = conn;
153  return StatusCode::SUCCESS;
154 }
155 
157  ATH_MSG_VERBOSE("RootSvc::commitOutput");
158  if (m_wconn == 0) {
159  ATH_MSG_ERROR("Cannot commit without RootConnection.");
160  return StatusCode::FAILURE;
161  }
162  if (!m_wconn->commit().isSuccess()) {
163  ATH_MSG_ERROR("Cannot commit RootConnection.");
164  return StatusCode::FAILURE;
165  }
166  return StatusCode::SUCCESS;
167 }
168 
170 StatusCode RootSvc::disconnect(const std::string& fname) {
171  ATH_MSG_VERBOSE("disconnect(" << fname << ")...");
172  Athena::RootConnection* conn = this->connection(fname);
173  if (conn == 0) {
174  ATH_MSG_ERROR("No open RootConnection for file " << fname);
175  return StatusCode::FAILURE;
176  }
177  if (!conn->disconnect().isSuccess()) {
178  ATH_MSG_ERROR("Cannot disconnect to file " << fname);
179  return StatusCode::FAILURE;
180  }
181  if (m_wconn == conn) {
182  m_wconn = 0;
183  }
184  return StatusCode::SUCCESS;
185 }
186 
190 // Catalog to get fid...
191  Guid fid = Guid::null();
192  if (m_catalog != 0) {
193  std::string fidString, ftype;
194  m_catalog->lookupFileByPFN(fname, fidString, ftype);
195  if( fidString.empty() ) {
196  m_catalog->registerPFN(fname, "ROOT_All", fidString);
197  }
198  fid.fromString(fidString);
199  }
201  ConnMap_t::const_iterator fitr = m_conns.find(fid);
202  if (fitr != m_conns.end()) {
203  conn = fitr->second;
204  }
205  return conn;
206 }
207 
208 } //> namespace Athena
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
Athena::RootConnection
This class provides the implementation of Athena::RootConnection class, similar to Gaudi IDataConnect...
Definition: RootConnection.h:36
Guid::null
static const Guid & null()
NULL-Guid: static class method.
Definition: Guid.cxx:18
python.tests.PyTestsLib.finalize
def finalize(self)
_info( "content of StoreGate..." ) self.sg.dump()
Definition: PyTestsLib.py:50
Athena::RootSvc::initialize
StatusCode initialize()
Gaudi Service Interface method implementations:
Definition: RootSvc.cxx:42
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
Placement
This class holds all the necessary information to guide the writing of an object in a physical place.
Definition: Placement.h:19
checkCorrelInHIST.conn
conn
Definition: checkCorrelInHIST.py:25
Athena::RootSvc::m_dictSvc
ServiceHandle< ::IDictLoaderSvc > m_dictSvc
ServiceHandle to the dictionary service.
Definition: RootSvc.h:100
pool::IFileCatalog::lookupFileByPFN
void lookupFileByPFN(const std::string &pfn, std::string &fid, std::string &tech) const
Get FID and filetype for a given PFN.
Placement::containerName
const std::string & containerName() const
Access container name.
Definition: Placement.h:32
Athena::RootSvc::m_catalog
pool::IFileCatalog * m_catalog
Definition: RootSvc.h:92
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
Athena::RootSvc::m_conns
ConnMap_t m_conns
Map of file name keys and connection values.
Definition: RootSvc.h:96
pool::IFileCatalog::registerPFN
void registerPFN(const std::string &pfn, const std::string &ftype, std::string &fid)
Register PFN, assign new FID if not given.
Athena::RootSvc::~RootSvc
virtual ~RootSvc()
Destructor.
Definition: RootSvc.cxx:35
initialize
void initialize()
Definition: run_EoverP.cxx:894
Athena::RootConnection::commit
StatusCode commit()
Commit data stream to ROOT.
Definition: RootConnection.cxx:96
IFileCatalog.h
Guid::toString
const std::string toString() const
Automatic conversion to string representation.
Definition: Guid.cxx:58
pool::IFileCatalog::commit
void commit()
Save catalog to file.
Definition: IFileCatalog.h:49
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
Athena::RootSvc::getType
RootType getType(const std::type_info &type) const
Load the type (dictionary) from Root.
Definition: RootSvc.cxx:75
Token
This class provides a token that identifies in a unique way objects on the persistent storage.
Definition: Token.h:21
Athena::RootConnection::write
StatusCode write(const void *data, unsigned long &len)
Write root byte buffer to output stream.
Definition: RootConnection.cxx:130
pool::IFileCatalog
Definition: IFileCatalog.h:23
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
Athena::RootSvc::connect
StatusCode connect(const std::string &fname)
Connect the file fname to the service.
Definition: RootSvc.cxx:141
Athena::RootSvc::disconnect
StatusCode disconnect(const std::string &fname)
Disconnect the file fname from the service.
Definition: RootSvc.cxx:170
Athena
Some weak symbol referencing magic...
Definition: AthLegacySequence.h:21
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
Athena::RootSvc::m_wconn
Athena::RootConnection * m_wconn
Definition: RootSvc.h:97
pool::IFileCatalog::start
void start()
redirect to init() for Gaudi FC
Definition: IFileCatalog.h:45
calibdata.exception
exception
Definition: calibdata.py:496
RootSvc.h
This file contains the class definition for the RootSvc class.
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
pool::IFileCatalog::setWriteCatalog
void setWriteCatalog(const std::string &connect)
Access to the (first) writable file catalog.
Athena::RootSvc::writeObject
const Token * writeObject(const Placement &placement, const RootType &type, const void *pObj)
Write object of a given class to Root.
Definition: RootSvc.cxx:85
Athena::RootSvc::connection
Athena::RootConnection * connection(const std::string &fname)
Get the RootConnection associated with file fname
Definition: RootSvc.cxx:189
pool::IFileCatalog::connect
void connect()
Definition: IFileCatalog.h:39
Athena::RootSvc::finalize
StatusCode finalize()
Definition: RootSvc.cxx:61
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
Athena::RootSvc::open
StatusCode open(const std::string &fname, const std::string &mode)
Open the file fname with open mode mode
Definition: RootSvc.cxx:114
Athena::RootSvc::commitOutput
StatusCode commitOutput()
Commit data and flush buffer.
Definition: RootSvc.cxx:156
python.AthDsoLogger.fname
string fname
Definition: AthDsoLogger.py:67
Guid
This class provides a encapsulation of a GUID/UUID/CLSID/IID data structure (128 bit number).
Definition: Guid.h:20
Athena::RootSvc::destructObject
void destructObject(const RootType &type, void *pObj) const
Destruct a given object of type RootType.
Definition: RootSvc.cxx:110
IDictLoaderSvc.h
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
Athena::RootSvc::readObject
void * readObject(const Token &token, void *&pObj)
Read object from Root.
Definition: RootSvc.cxx:80
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
Guid::fromString
const Guid & fromString(const std::string &s)
Automatic conversion from string representation.
Definition: Guid.cxx:65
RootConnection.h
This file contains the class definition for the Athena::RootConnection class.
Athena::RootConnection::setContainer
StatusCode setContainer(const std::string &container, const std::string &type)
Set the container name and type, creating TTree and TBranch as needed.
Definition: RootConnection.cxx:148
Athena::RootSvc::createObject
void * createObject(const RootType &type) const
Create an object of a given RootType.
Definition: RootSvc.cxx:104
Placement.h
This file contains the class definition for the Placement class (migrated from POOL).
Token.h
This file contains the class definition for the Token class (migrated from POOL).
Athena::RootSvc::RootSvc
RootSvc()
Default constructor:
TScopeAdapter
Definition: RootType.h:119