The Gaudi Framework  v29r0 (ff2e7097)
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 class IDataManagerSvc;
9 class IDataProviderSvc;
10 class IRegistry;
27 class DataSvcFileEntriesTool : public extends<AlgTool, IDataStoreLeaves, IIncidentListener>
28 {
29 public:
31  using extends::extends;
32 
34  ~DataSvcFileEntriesTool() override = default;
35 
37  StatusCode initialize() override;
38 
40  StatusCode finalize() override;
41 
46  const LeavesList& leaves() const override;
47 
51  void handle( const Incident& incident ) override;
52 
53 private:
54  Gaudi::Property<std::string> m_dataSvcName{this, "DataService", "EventDataSvc", "Name of the data service to use"};
55  Gaudi::Property<std::string> m_rootNode{this, "Root", "", "Path to the element from which to start the scan"};
57  this, "ScanOnBeginEvent", false,
58  "If the scan has to be started during the BeginEvent incident (true) or on demand (false, default)"};
60  this, "IgnoreOriginChange", false,
61  "Disable the detection of the change in the origin of object between the BeginEvent and the scan"};
62 
69 
71  LeavesList m_leaves;
72 
74  void i_collectLeaves();
76  void i_collectLeaves( IRegistry* reg );
77 
81 
87 };
88 
89 // ========== implementation
90 #include "GaudiKernel/DataObject.h"
96 #include "GaudiKernel/IRegistry.h"
97 
99 
101 {
103  if ( sc.isFailure() ) return sc;
104 
105  // Retrieve the pointer to the needed services.
106 
107  m_incidentSvc = serviceLocator()->service( "IncidentSvc" );
108  if ( !m_incidentSvc ) {
109  error() << "Cannot get IncidentSvc" << endmsg;
110  return StatusCode::FAILURE;
111  }
112 
114  if ( !m_dataSvc || !m_dataMgrSvc ) {
115  error() << "Cannot get IDataProviderSvc+IDataManagerSvc " << m_dataSvcName << endmsg;
116  return StatusCode::FAILURE;
117  }
118 
119  // Register ourself to the incident service as listener for BeginEvent
120  m_incidentSvc->addListener( this, IncidentType::BeginEvent );
121 
122  // If the Root node is not specified, take the name from the data service itself.
123  if ( m_rootNode.empty() ) {
125  }
126 
127  // Clear the cache (in case the instance is re-initilized).
128  m_leaves.clear();
129  return StatusCode::SUCCESS;
130 }
131 
133 {
134  // unregister from the incident service
135  if ( m_incidentSvc ) {
136  m_incidentSvc->removeListener( this, IncidentType::BeginEvent );
137  }
138  // Release the services
141  m_dataSvc.reset();
142 
143  return AlgTool::finalize();
144 }
145 
147 {
148  // Get the file id of the root node at every event
149  IOpaqueAddress* addr = i_getRootNode()->address();
150  if ( addr )
151  m_initialBase = addr->par()[0];
152  else
153  m_initialBase.clear(); // use empty file id if there is no address
154 
155  m_leaves.clear();
156  if ( m_scanOnBeginEvent ) {
157  verbose() << "::handle scanning on " << incident.type() << endmsg;
158  i_collectLeaves();
159  }
160 }
161 
163 {
164  if ( m_leaves.empty() ) {
165  const_cast<DataSvcFileEntriesTool*>( this )->i_collectLeaves();
166  }
167  return m_leaves;
168 }
169 
171 {
172  DataObject* obj = nullptr;
174  if ( sc.isFailure() ) {
175  throw GaudiException( "Cannot get " + m_rootNode + " from " + m_dataSvcName, name(), StatusCode::FAILURE );
176  }
177  return obj->registry();
178 }
179 
181 
184 {
185  // I do not put sanity checks on the pointers because I know how I'm calling the function
186  IOpaqueAddress* addr = reg->address();
187  if ( addr ) { // we consider only objects that are in a file
188  if ( msgLevel( MSG::VERBOSE ) ) verbose() << "::i_collectLeaves added " << reg->identifier() << endmsg;
189  m_leaves.push_back( reg->object() ); // add this object
190  // Origin of the current object
191  const std::string& base = addr->par()[0];
192  // Compare with the origin seen during BeginEvent
193  if ( !m_ignoreOriginChange && ( m_initialBase != base ) )
194  throw GaudiException( "Origin of data has changed ('" + m_initialBase + "' != '" + base +
195  "'), probably OutputStream was called before "
196  "InputCopyStream: check options",
198 
199  std::vector<IRegistry*> lfs; // leaves of the current object
200  StatusCode sc = m_dataMgrSvc->objectLeaves( reg, lfs );
201  if ( sc.isSuccess() ) {
202  for ( const auto& i : lfs ) {
203  // Continue if the leaf has the same database as the parent
204  if ( i->address() && i->address()->par()[0] == base ) {
205  DataObject* obj = nullptr;
206  sc = m_dataSvc->retrieveObject( reg, i->name(), obj );
207  if ( sc.isSuccess() ) {
208  i_collectLeaves( i );
209  } else {
210  throw GaudiException( "Cannot get " + i->identifier() + " from " + m_dataSvcName, name(),
212  }
213  }
214  }
215  }
216  }
217 }
218 
virtual const std::string & rootName() const =0
Get Name of root Event.
Define general base for Gaudi exception.
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: AlgTool.cpp:84
const std::string & type() const
Access to the incident type.
Definition: Incident.h:41
Implementation of property with value of concrete type.
Definition: Property.h:319
StatusCode initialize() override
Definition: AlgTool.cpp:241
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
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
Test for a status code of FAILURE.
Definition: StatusCode.h:86
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...
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:33
SmartIF< IDataManagerSvc > m_dataMgrSvc
Pointer to the IDataManagerSvc interface of the data service.
STL class.
StatusCode finalize() override
Definition: AlgTool.cpp:311
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:79
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:28
~DataSvcFileEntriesTool() override=default
Destructor.
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
T clear(T...args)
StatusCode initialize() override
Initialize the tool.
virtual DataObject * object() const =0
Retrieve object behind the link.
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 ...
const std::string & name() const override
Retrieve full identifying name of the concrete tool object.
Definition: AlgTool.cpp:63
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.
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:92
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.
MSG::Level msgLevel() const
get the output level from the embedded MsgStream
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:29
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:209
void handle(const Incident &incident) override
Call-back function for the BeginEvent incident.
The interface implemented by the IncidentSvc service.
Definition: IIncidentSvc.h:23
virtual StatusCode retrieveObject(IRegistry *pDirectory, const std::string &path, DataObject *&pObject)=0
Retrieve object identified by its directory entry.