Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
DataSvcFileEntriesTool.cpp
Go to the documentation of this file.
1 // ========== header
2 #include "GaudiKernel/AlgTool.h"
5 #include "GaudiKernel/SmartIF.h"
6 
7 class IIncidentSvc;
8 struct IDataManagerSvc;
9 class IDataProviderSvc;
10 class IRegistry;
27 class DataSvcFileEntriesTool : public extends<AlgTool, IDataStoreLeaves, IIncidentListener> {
28 public:
30  using extends::extends;
31 
33  StatusCode initialize() override;
34 
36  StatusCode finalize() override;
37 
42  const LeavesList& leaves() const override;
43 
47  void handle( const Incident& incident ) override;
48 
49 private:
50  Gaudi::Property<std::string> m_dataSvcName{this, "DataService", "EventDataSvc", "Name of the data service to use"};
51  Gaudi::Property<std::string> m_rootNode{this, "Root", "", "Path to the element from which to start the scan"};
53  this, "ScanOnBeginEvent", false,
54  "If the scan has to be started during the BeginEvent incident (true) or on demand (false, default)"};
56  this, "IgnoreOriginChange", false,
57  "Disable the detection of the change in the origin of object between the BeginEvent and the scan"};
58 
65 
67  LeavesList m_leaves;
68 
70  void i_collectLeaves();
72  void i_collectLeaves( IRegistry* reg );
73 
77 
83 };
84 
85 // ========== implementation
86 #include "GaudiKernel/DataObject.h"
92 #include "GaudiKernel/IRegistry.h"
93 
95 
98  if ( sc.isFailure() ) return sc;
99 
100  // Retrieve the pointer to the needed services.
101 
102  m_incidentSvc = serviceLocator()->service( "IncidentSvc" );
103  if ( !m_incidentSvc ) {
104  error() << "Cannot get IncidentSvc" << endmsg;
105  return StatusCode::FAILURE;
106  }
107 
109  if ( !m_dataSvc || !m_dataMgrSvc ) {
110  error() << "Cannot get IDataProviderSvc+IDataManagerSvc " << m_dataSvcName << endmsg;
111  return StatusCode::FAILURE;
112  }
113 
114  // Register ourself to the incident service as listener for BeginEvent
115  m_incidentSvc->addListener( this, IncidentType::BeginEvent );
116 
117  // If the Root node is not specified, take the name from the data service itself.
118  if ( m_rootNode.empty() ) { m_rootNode = m_dataMgrSvc->rootName(); }
119 
120  // Clear the cache (in case the instance is re-initilized).
121  m_leaves.clear();
122  return StatusCode::SUCCESS;
123 }
124 
126  // unregister from the incident service
127  if ( m_incidentSvc ) { m_incidentSvc->removeListener( this, IncidentType::BeginEvent ); }
128  // Release the services
131  m_dataSvc.reset();
132 
133  return AlgTool::finalize();
134 }
135 
136 void DataSvcFileEntriesTool::handle( const Incident& incident ) {
137  // Get the file id of the root node at every event
138  IOpaqueAddress* addr = i_getRootNode()->address();
139  if ( addr )
140  m_initialBase = addr->par()[0];
141  else
142  m_initialBase.clear(); // use empty file id if there is no address
143 
144  m_leaves.clear();
145  if ( m_scanOnBeginEvent ) {
146  verbose() << "::handle scanning on " << incident.type() << endmsg;
147  i_collectLeaves();
148  }
149 }
150 
152  if ( m_leaves.empty() ) { const_cast<DataSvcFileEntriesTool*>( this )->i_collectLeaves(); }
153  return m_leaves;
154 }
155 
157  DataObject* obj = nullptr;
158  StatusCode sc = m_dataSvc->retrieveObject( m_rootNode.value(), obj );
159  if ( sc.isFailure() ) {
160  throw GaudiException( "Cannot get " + m_rootNode + " from " + m_dataSvcName, name(), StatusCode::FAILURE );
161  }
162  return obj->registry();
163 }
164 
166 
169  // I do not put sanity checks on the pointers because I know how I'm calling the function
170  IOpaqueAddress* addr = reg->address();
171  if ( addr ) { // we consider only objects that are in a file
172  if ( msgLevel( MSG::VERBOSE ) ) verbose() << "::i_collectLeaves added " << reg->identifier() << endmsg;
173  m_leaves.push_back( reg->object() ); // add this object
174  // Origin of the current object
175  const std::string& base = addr->par()[0];
176  // Compare with the origin seen during BeginEvent
177  if ( !m_ignoreOriginChange && ( m_initialBase != base ) )
178  throw GaudiException( "Origin of data has changed ('" + m_initialBase + "' != '" + base +
179  "'), probably OutputStream was called before "
180  "InputCopyStream: check options",
182 
183  std::vector<IRegistry*> lfs; // leaves of the current object
184  StatusCode sc = m_dataMgrSvc->objectLeaves( reg, lfs );
185  if ( sc.isSuccess() ) {
186  for ( const auto& i : lfs ) {
187  // Continue if the leaf has the same database as the parent
188  if ( i->address() && i->address()->par()[0] == base ) {
189  DataObject* obj = nullptr;
190  sc = m_dataSvc->retrieveObject( reg, i->name(), obj );
191  if ( sc.isSuccess() ) {
192  i_collectLeaves( i );
193  } else {
194  throw GaudiException( "Cannot get " + i->identifier() + " from " + m_dataSvcName, name(),
196  }
197  }
198  }
199  }
200  }
201 }
202 
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.
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: AlgTool.cpp:79
const std::string & type() const
Access to the incident type.
Definition: Incident.h:38
Implementation of property with value of concrete type.
Definition: Property.h:352
StatusCode initialize() override
Definition: AlgTool.cpp:201
bool isSuccess() const
Definition: StatusCode.h:267
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
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.
virtual const std::string * par() const =0
Retrieve String parameters.
SmartIF< IDataProviderSvc > m_dataSvc
Pointer to the IDataProviderSvc interface of the data service.
std::string m_initialBase
File ID of the Root node.
bool isFailure() const
Definition: StatusCode.h:130
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:72
Tool to scan a transient store branch that collects all the objects that belong to the same source (f...
SmartIF< IDataManagerSvc > m_dataMgrSvc
Pointer to the IDataManagerSvc interface of the data service.
STL class.
#define DECLARE_COMPONENT(type)
StatusCode finalize() override
Definition: AlgTool.cpp:268
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:76
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.
virtual const id_type & identifier() const =0
Full identifier (or key)
Gaudi::Property< bool > m_ignoreOriginChange
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
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:22
virtual StatusCode retrieveObject(IRegistry *pDirectory, boost::string_ref path, DataObject *&pObject)=0
Retrieve object identified by its directory entry.
T clear(T...args)
virtual const std::string & rootName() const =0
Get Name of root Event.
StatusCode initialize() override
Initialize the tool.
virtual DataObject * object() const =0
Retrieve object behind the link.
const std::string & name() const override
Retrieve full identifying name of the concrete tool object.
Definition: AlgTool.cpp:58
Base class used to extend a class implementing other interfaces.
Definition: extends.h:10
Base class for all Incidents (computing events).
Definition: Incident.h:17
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:86
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:86
const LeavesList & leaves() const override
Return the list of collected objects.
virtual void removeListener(IIncidentListener *lis, const std::string &type="")=0
Remove listener.
Opaque address interface definition.
LeavesList m_leaves
Internal cache for the list of objects found during the scan.
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
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:192
void handle(const Incident &incident) override
Call-back function for the BeginEvent incident.
The interface implemented by the IncidentSvc service.
Definition: IIncidentSvc.h:23
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)