The Gaudi Framework  master (b9786168)
Loading...
Searching...
No Matches
DataSvcFileEntriesTool.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2024 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// ========== header
12#include <GaudiKernel/AlgTool.h>
15#include <GaudiKernel/SmartIF.h>
16
17class IIncidentSvc;
18struct IDataManagerSvc;
20class IRegistry;
37class DataSvcFileEntriesTool : public extends<AlgTool, IDataStoreLeaves, IIncidentListener> {
38public:
40 using extends::extends;
41
43 StatusCode initialize() override;
44
46 StatusCode finalize() override;
47
52 const LeavesList& leaves() const override;
53
57 void handle( const Incident& incident ) override;
58
59private:
60 Gaudi::Property<std::string> m_dataSvcName{ this, "DataService", "EventDataSvc", "Name of the data service to use" };
61 Gaudi::Property<std::string> m_rootNode{ this, "Root", "", "Path to the element from which to start the scan" };
63 this, "ScanOnBeginEvent", false,
64 "If the scan has to be started during the BeginEvent incident (true) or on demand (false, default)" };
66 this, "IgnoreOriginChange", false,
67 "Disable the detection of the change in the origin of object between the BeginEvent and the scan" };
68
75
77 LeavesList m_leaves;
78
80 void i_collectLeaves();
82 void i_collectLeaves( IRegistry* reg );
83
87
92 std::string m_initialBase;
93};
94
95// ========== implementation
103
105
108 if ( sc.isFailure() ) return sc;
109
110 // Retrieve the pointer to the needed services.
111
112 m_incidentSvc = serviceLocator()->service( "IncidentSvc" );
113 if ( !m_incidentSvc ) {
114 error() << "Cannot get IncidentSvc" << endmsg;
115 return StatusCode::FAILURE;
116 }
117
119 if ( !m_dataSvc || !m_dataMgrSvc ) {
120 error() << "Cannot get IDataProviderSvc+IDataManagerSvc " << m_dataSvcName << endmsg;
121 return StatusCode::FAILURE;
122 }
123
124 // Register ourself to the incident service as listener for BeginEvent
125 m_incidentSvc->addListener( this, IncidentType::BeginEvent );
126
127 // If the Root node is not specified, take the name from the data service itself.
128 if ( m_rootNode.empty() ) { m_rootNode = m_dataMgrSvc->rootName(); }
129
130 // Clear the cache (in case the instance is re-initilized).
131 m_leaves.clear();
132 return StatusCode::SUCCESS;
133}
134
136 // unregister from the incident service
137 if ( m_incidentSvc ) { m_incidentSvc->removeListener( this, IncidentType::BeginEvent ); }
138 // Release the services
139 m_incidentSvc.reset();
140 m_dataMgrSvc.reset();
141 m_dataSvc.reset();
142
143 return AlgTool::finalize();
144}
145
147 // Get the file id of the root node at every event
149 if ( addr )
150 m_initialBase = addr->par()[0];
151 else
152 m_initialBase.clear(); // use empty file id if there is no address
153
154 m_leaves.clear();
155 if ( m_scanOnBeginEvent ) {
156 verbose() << "::handle scanning on " << incident.type() << endmsg;
158 }
159}
160
162 if ( m_leaves.empty() ) { const_cast<DataSvcFileEntriesTool*>( this )->i_collectLeaves(); }
163 return m_leaves;
164}
165
167 DataObject* obj = nullptr;
168 StatusCode sc = m_dataSvc->retrieveObject( m_rootNode.value(), obj );
169 if ( sc.isFailure() ) {
170 throw GaudiException( "Cannot get " + m_rootNode + " from " + m_dataSvcName, name(), StatusCode::FAILURE );
171 }
172 return obj->registry();
173}
174
176
179 // I do not put sanity checks on the pointers because I know how I'm calling the function
180 IOpaqueAddress* addr = reg->address();
181 if ( addr ) { // we consider only objects that are in a file
182 if ( msgLevel( MSG::VERBOSE ) ) verbose() << "::i_collectLeaves added " << reg->identifier() << endmsg;
183 m_leaves.push_back( reg->object() ); // add this object
184 // Origin of the current object
185 const std::string& base = addr->par()[0];
186 // Compare with the origin seen during BeginEvent
187 if ( !m_ignoreOriginChange && ( m_initialBase != base ) )
188 throw GaudiException( "Origin of data has changed ('" + m_initialBase + "' != '" + base +
189 "'), probably OutputStream was called before "
190 "InputCopyStream: check options",
192
193 std::vector<IRegistry*> lfs; // leaves of the current object
194 StatusCode sc = m_dataMgrSvc->objectLeaves( reg, lfs );
195 if ( sc.isSuccess() ) {
196 for ( const auto& i : lfs ) {
197 // Continue if the leaf has the same database as the parent
198 if ( i->address() && i->address()->par()[0] == base ) {
199 DataObject* obj = nullptr;
200 sc = m_dataSvc->retrieveObject( reg, i->name(), obj );
201 if ( sc.isSuccess() ) {
202 i_collectLeaves( i );
203 } else {
204 throw GaudiException( "Cannot get " + i->identifier() + " from " + m_dataSvcName, name(),
206 }
207 }
208 }
209 }
210 }
211}
212
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
#define DECLARE_COMPONENT(type)
StatusCode initialize() override
Definition AlgTool.cpp:158
const std::string & name() const override
Retrieve full identifying name of the concrete tool object.
Definition AlgTool.cpp:57
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition AlgTool.cpp:63
StatusCode finalize() override
Definition AlgTool.cpp:210
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
A DataObject is the base class of any identifiable object on any data store.
Definition DataObject.h:37
IRegistry * registry() const
Get pointer to Registry.
Definition DataObject.h:79
Tool to scan a transient store branch that collects all the objects that belong to the same source (f...
Gaudi::Property< std::string > m_dataSvcName
StatusCode initialize() override
Initialize the tool.
Gaudi::Property< bool > m_scanOnBeginEvent
std::string m_initialBase
File ID of the Root node.
Gaudi::Property< bool > m_ignoreOriginChange
void i_collectLeaves()
Scan the data service starting from the node specified as Root.
Gaudi::Property< std::string > m_rootNode
StatusCode finalize() override
Finalize the tool.
LeavesList m_leaves
Internal cache for the list of objects found during the scan.
void handle(const Incident &incident) override
Call-back function for the BeginEvent incident.
const LeavesList & leaves() const override
Return the list of collected objects.
SmartIF< IIncidentSvc > m_incidentSvc
Pointer to the incident service.
SmartIF< IDataManagerSvc > m_dataMgrSvc
Pointer to the IDataManagerSvc interface of the data service.
SmartIF< IDataProviderSvc > m_dataSvc
Pointer to the IDataProviderSvc interface of the data service.
IRegistry * i_getRootNode()
Return the pointer to the IRegistry object associated to the node specified as Root.
Implementation of property with value of concrete type.
Definition PropertyFwd.h:27
Define general base for Gaudi exception.
Data provider interface definition.
std::vector< DataObject * > LeavesList
Returned type.
The interface implemented by the IncidentSvc service.
Opaque address interface definition.
virtual const std::string * par() const =0
Retrieve String parameters.
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition IRegistry.h:29
virtual const name_type & name() const =0
Name of the directory (or key)
virtual const id_type & identifier() const =0
Full identifier (or key)
virtual DataObject * object() const =0
Retrieve object behind the link.
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
virtual SmartIF< IService > & service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true)=0
Returns a smart pointer to a service.
Base class for all Incidents (computing events).
Definition Incident.h:24
const std::string & type() const
Access to the incident type.
Definition Incident.h:43
Small smart pointer class with automatic reference counting for IInterface.
Definition SmartIF.h:28
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
bool isFailure() const
Definition StatusCode.h:129
bool isSuccess() const
Definition StatusCode.h:314
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
Base class used to extend a class implementing other interfaces.
Definition extends.h:19
@ VERBOSE
Definition IMessageSvc.h:22