The Gaudi Framework  master (37c0b60a)
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 
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 
DataSvcFileEntriesTool::leaves
const LeavesList & leaves() const override
Return the list of collected objects.
Definition: DataSvcFileEntriesTool.cpp:161
IDataStoreLeaves.h
DataSvcFileEntriesTool::m_incidentSvc
SmartIF< IIncidentSvc > m_incidentSvc
Pointer to the incident service.
Definition: DataSvcFileEntriesTool.cpp:70
std::string
STL class.
DataStoreItem.h
IOpaqueAddress::par
virtual const std::string * par() const =0
Retrieve String parameters.
IDataManagerSvc
Definition: IDataManagerSvc.h:55
DataSvcFileEntriesTool::handle
void handle(const Incident &incident) override
Call-back function for the BeginEvent incident.
Definition: DataSvcFileEntriesTool.cpp:146
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
GaudiException.h
IOpaqueAddress
Definition: IOpaqueAddress.h:33
std::vector< DataObject * >
SmartIF::reset
void reset(TYPE *ptr=nullptr)
Set the internal pointer to the passed one disposing of the old one.
Definition: SmartIF.h:96
GaudiException
Definition: GaudiException.h:31
DataSvcFileEntriesTool::m_dataMgrSvc
SmartIF< IDataManagerSvc > m_dataMgrSvc
Pointer to the IDataManagerSvc interface of the data service.
Definition: DataSvcFileEntriesTool.cpp:72
IRegistry
Definition: IRegistry.h:32
DataSvcFileEntriesTool::m_leaves
LeavesList m_leaves
Internal cache for the list of objects found during the scan.
Definition: DataSvcFileEntriesTool.cpp:77
AlgTool::initialize
StatusCode initialize() override
Definition: AlgTool.cpp:199
CommonMessaging< implements< IAlgTool, IDataHandleHolder, IProperty, IStateful > >::msgLevel
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
Definition: CommonMessaging.h:148
IDataProviderSvc.h
AlgTool::name
const std::string & name() const override
Retrieve full identifying name of the concrete tool object.
Definition: AlgTool.cpp:67
std::string::clear
T clear(T... args)
IIncidentSvc.h
SmartIF.h
IRegistry::name
virtual const name_type & name() const =0
Name of the directory (or key)
DataSvcFileEntriesTool
Tool to scan a transient store branch that collects all the objects that belong to the same source (f...
Definition: DataSvcFileEntriesTool.cpp:37
AlgTool::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: AlgTool.cpp:88
StatusCode
Definition: StatusCode.h:65
IOpaqueAddress.h
DataSvcFileEntriesTool::m_dataSvc
SmartIF< IDataProviderSvc > m_dataSvc
Pointer to the IDataProviderSvc interface of the data service.
Definition: DataSvcFileEntriesTool.cpp:74
DataSvcFileEntriesTool::m_dataSvcName
Gaudi::Property< std::string > m_dataSvcName
Definition: DataSvcFileEntriesTool.cpp:60
Gaudi::Property::value
const ValueType & value() const
Definition: Property.h:237
SmartIF< IIncidentSvc >
genconfuser.verbose
verbose
Definition: genconfuser.py:28
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
extends
Base class used to extend a class implementing other interfaces.
Definition: extends.h:20
DataSvcFileEntriesTool::initialize
StatusCode initialize() override
Initialize the tool.
Definition: DataSvcFileEntriesTool.cpp:106
IRegistry.h
DataSvcFileEntriesTool::finalize
StatusCode finalize() override
Finalize the tool.
Definition: DataSvcFileEntriesTool.cpp:135
DataSvcFileEntriesTool::m_ignoreOriginChange
Gaudi::Property< bool > m_ignoreOriginChange
Definition: DataSvcFileEntriesTool.cpp:65
IRegistry::address
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
MSG::VERBOSE
@ VERBOSE
Definition: IMessageSvc.h:25
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
DataSvcFileEntriesTool::m_scanOnBeginEvent
Gaudi::Property< bool > m_scanOnBeginEvent
Definition: DataSvcFileEntriesTool.cpp:62
DataObject.h
IIncidentListener.h
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
IRegistry::object
virtual DataObject * object() const =0
Retrieve object behind the link.
IRegistry::identifier
virtual const id_type & identifier() const =0
Full identifier (or key)
DataObject
Definition: DataObject.h:36
DataSvcFileEntriesTool::m_rootNode
Gaudi::Property< std::string > m_rootNode
Definition: DataSvcFileEntriesTool.cpp:61
AlgTool.h
Incident::type
const std::string & type() const
Access to the incident type.
Definition: Incident.h:48
check_ParticleID.base
base
Definition: check_ParticleID.py:24
IDataProviderSvc
Definition: IDataProviderSvc.h:53
DataSvcFileEntriesTool::i_collectLeaves
void i_collectLeaves()
Scan the data service starting from the node specified as Root.
Definition: DataSvcFileEntriesTool.cpp:175
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
DataSvcFileEntriesTool::m_initialBase
std::string m_initialBase
File ID of the Root node.
Definition: DataSvcFileEntriesTool.cpp:92
IIncidentSvc
Definition: IIncidentSvc.h:33
Incident
Definition: Incident.h:27
DataObject::registry
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:78
Gaudi::Property< std::string >
IDataManagerSvc.h
DataSvcFileEntriesTool::i_getRootNode
IRegistry * i_getRootNode()
Return the pointer to the IRegistry object associated to the node specified as Root.
Definition: DataSvcFileEntriesTool.cpp:166
AlgTool::finalize
StatusCode finalize() override
Definition: AlgTool.cpp:266