The Gaudi Framework  v33r0 (d5ea422b)
DataSvcFileEntriesTool.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 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 
17 class IIncidentSvc;
18 struct IDataManagerSvc;
19 class IDataProviderSvc;
20 class IRegistry;
37 class DataSvcFileEntriesTool : public extends<AlgTool, IDataStoreLeaves, IIncidentListener> {
38 public:
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 
59 private:
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 
93 };
94 
95 // ========== implementation
96 #include "GaudiKernel/DataObject.h"
102 #include "GaudiKernel/IRegistry.h"
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
141  m_dataSvc.reset();
142 
143  return AlgTool::finalize();
144 }
145 
146 void DataSvcFileEntriesTool::handle( const Incident& incident ) {
147  // Get the file id of the root node at every event
148  IOpaqueAddress* addr = i_getRootNode()->address();
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;
157  i_collectLeaves();
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 
virtual const std::string * par() const =0
Retrieve String parameters.
const LeavesList & leaves() const override
Return the list of collected objects.
virtual StatusCode objectLeaves(const DataObject *pObject, std::vector< IRegistry * > &refLeaves)=0
Explore the object store: retrieve all leaves attached to the object The object is identified by its ...
Define general base for Gaudi exception.
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:82
Implementation of property with value of concrete type.
Definition: Property.h:370
StatusCode initialize() override
Definition: AlgTool.cpp:211
const std::string & type() const
Access to the incident type.
Definition: Incident.h:48
constexpr static const auto SUCCESS
Definition: StatusCode.h:96
virtual const name_type & name() const =0
Name of the directory (or key)
Gaudi::Property< std::string > m_rootNode
Data provider interface definition.
SmartIF< IIncidentSvc > m_incidentSvc
Pointer to the incident service.
SmartIF< IDataProviderSvc > m_dataSvc
Pointer to the IDataProviderSvc interface of the data service.
std::string m_initialBase
File ID of the Root node.
Tool to scan a transient store branch that collects all the objects that belong to the same source (f...
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
SmartIF< IDataManagerSvc > m_dataMgrSvc
Pointer to the IDataManagerSvc interface of the data service.
STL class.
#define DECLARE_COMPONENT(type)
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: AlgTool.cpp:89
StatusCode finalize() override
Definition: AlgTool.cpp:278
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:86
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
StatusCode finalize() override
Finalize the tool.
void i_collectLeaves()
Scan the data service starting from the node specified as Root.
Gaudi::Property< bool > m_ignoreOriginChange
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
Gaudi::Property< std::string > m_dataSvcName
Gaudi::Property< bool > m_scanOnBeginEvent
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:32
T clear(T... args)
bool isSuccess() const
Definition: StatusCode.h:361
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
virtual DataObject * object() const =0
Retrieve object behind the link.
StatusCode initialize() override
Initialize the tool.
virtual StatusCode retrieveObject(IRegistry *pDirectory, std::string_view path, DataObject *&pObject)=0
Retrieve object identified by its directory entry.
Base class used to extend a class implementing other interfaces.
Definition: extends.h:20
Base class for all Incidents (computing events).
Definition: Incident.h:27
virtual void addListener(IIncidentListener *lis, const std::string &type="", long priority=0, bool rethrow=false, bool singleShot=false)=0
Add listener.
constexpr static const auto FAILURE
Definition: StatusCode.h:97
virtual const id_type & identifier() const =0
Full identifier (or key)
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:96
virtual void removeListener(IIncidentListener *lis, const std::string &type="")=0
Remove listener.
bool isFailure() const
Definition: StatusCode.h:141
Opaque address interface definition.
LeavesList m_leaves
Internal cache for the list of objects found during the scan.
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:40
const std::string & name() const override
Retrieve full identifying name of the concrete tool object.
Definition: AlgTool.cpp:68
virtual const std::string & rootName() const =0
Get Name of root Event.
IRegistry * i_getRootNode()
Return the pointer to the IRegistry object associated to the node specified as Root.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
void handle(const Incident &incident) override
Call-back function for the BeginEvent incident.
The interface implemented by the IncidentSvc service.
Definition: IIncidentSvc.h:33