The Gaudi Framework  master (37c0b60a)
FetchLeavesFromFile.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2023 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 \***********************************************************************************/
12 #include <GaudiKernel/DataObject.h>
17 #include <GaudiKernel/IRegistry.h>
18 #include <GaudiKernel/SmartIF.h>
19 #include <fmt/format.h>
20 
21 namespace Gaudi {
22  namespace Hive {
23  class FetchLeavesFromFile final : public Gaudi::Functional::Producer<IDataStoreLeaves::LeavesList()> {
24  public:
26  : Producer( name, pSvcLocator, KeyValue( "InputFileLeavesLocation", "/Event/InputFileLeaves" ) ) {}
27 
28  StatusCode initialize() override;
29 
31 
32  StatusCode finalize() override {
34  return Producer::finalize();
35  }
36 
37  // Scan the data service starting from the node specified as \b Root.
39 
40  private:
41  Gaudi::Property<std::string> m_dataSvcName{ this, "DataService", "EventDataSvc",
42  "Name of the data service to use" };
43 
44  Gaudi::Property<std::string> m_rootNode{ this, "Root", "", "Path to the element from which to start the scan" };
45 
46  // Pointer to the IDataManagerSvc interface of the data service.
48  };
49 
50  // implementation
51 
53  return Producer::initialize().andThen( [&]() {
54  m_dataMgrSvc = serviceLocator()->service( m_dataSvcName );
55  if ( !m_dataMgrSvc ) {
56  error() << "Cannot get IDataManagerSvc " << m_dataSvcName << endmsg;
57  return StatusCode::FAILURE;
58  }
59  return StatusCode::SUCCESS;
60  } );
61  }
62 
64  // we have to make sure that the node we start the traversal from is loaded
65  {
66  DataObject* obj = nullptr;
67  evtSvc()
68  ->retrieveObject( m_rootNode, obj )
69  .orThrow( fmt::format( "failed to retrieve {} from {}", m_rootNode.value(), m_dataSvcName.value() ),
70  name() );
71  }
72  // result
74  // used to test for nodes with same origin
75  std::string origin;
76  // we do not get info from exceptions in the data store agent, so we record the exception message, if any
77  std::string failure_msg;
79  ->traverseSubTree( m_rootNode,
80  [&]( IRegistry* reg, int ) {
81  if ( reg->address() && reg->dataSvc() ) { // we consider only objects that come from a
82  // file
83  if ( origin.empty() ) {
84  // this is the first node we encounter, so we record where it comes from
85  origin = reg->address()->par()[0];
86  }
87  // if the current object comes from the same file as the first entry...
88  if ( origin == reg->address()->par()[0] ) {
89  // ... make sure the object has been loaded...
90  DataObject* obj = reg->object();
91  if ( !obj )
92  reg->dataSvc()
93  ->retrieveObject( reg->identifier(), obj )
94  .orElse( [&]() {
95  failure_msg = fmt::format( "failed to retrieve {} from {}",
96  reg->identifier(), m_dataSvcName.value() );
97  // we do not really care about the exception we throw because traverseSubTree
98  // will just use it to abort the traversal
99  throw GaudiException( failure_msg, name(), StatusCode::FAILURE );
100  } )
101  .ignore();
102  // ... and add it to the list
103  all_leaves.push_back( obj );
104  if ( msgLevel( MSG::VERBOSE ) )
105  verbose() << "::i_collectLeaves added " << reg->identifier() << endmsg;
106  return true; // we can continue the recursion
107  }
108  }
109  // if we reach this point the object was not interesting, so no need to recurse further
110  return false;
111  } )
112  .orThrow( failure_msg, name() );
113  return all_leaves;
114  }
115 
116  } // namespace Hive
117 } // namespace Gaudi
118 
Gaudi::Hive::FetchLeavesFromFile::m_dataSvcName
Gaudi::Property< std::string > m_dataSvcName
Definition: FetchLeavesFromFile.cpp:41
IDataStoreLeaves.h
std::string
STL class.
IOpaqueAddress::par
virtual const std::string * par() const =0
Retrieve String parameters.
StatusCode::andThen
StatusCode andThen(F &&f, ARGS &&... args) const
Chain code blocks making the execution conditional a success result.
Definition: StatusCode.h:163
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
ISvcLocator
Definition: ISvcLocator.h:46
Gaudi::Hive::FetchLeavesFromFile::FetchLeavesFromFile
FetchLeavesFromFile(const std::string &name, ISvcLocator *pSvcLocator)
Definition: FetchLeavesFromFile.cpp:25
IRegistry
Definition: IRegistry.h:32
IDataProviderSvc.h
std::vector::push_back
T push_back(T... args)
SmartIF.h
Gaudi::Hive::FetchLeavesFromFile::i_collectLeaves
IDataStoreLeaves::LeavesList i_collectLeaves() const
Definition: FetchLeavesFromFile.cpp:63
StatusCode
Definition: StatusCode.h:65
Gaudi::Functional::Producer
details::Producer< Signature, Traits_, details::isLegacy< Traits_ > > Producer
Definition: Producer.h:37
IOpaqueAddress.h
Producer.h
Gaudi::Property::value
const ValueType & value() const
Definition: Property.h:237
Gaudi::Hive::FetchLeavesFromFile::finalize
StatusCode finalize() override
Definition: FetchLeavesFromFile.cpp:32
SmartIF< IDataManagerSvc >
Gaudi::Functional::details::Producer
Definition: Producer.h:22
genconfuser.verbose
verbose
Definition: genconfuser.py:28
Gaudi::Hive::FetchLeavesFromFile
Definition: FetchLeavesFromFile.cpp:23
Gaudi::Hive::FetchLeavesFromFile::m_rootNode
Gaudi::Property< std::string > m_rootNode
Definition: FetchLeavesFromFile.cpp:44
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
IRegistry.h
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition: __init__.py:1
Gaudi::Hive::FetchLeavesFromFile::operator()
IDataStoreLeaves::LeavesList operator()() const override
Definition: FetchLeavesFromFile.cpp:30
IRegistry::address
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
MSG::VERBOSE
@ VERBOSE
Definition: IMessageSvc.h:25
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
DataObject.h
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
IRegistry::identifier
virtual const id_type & identifier() const =0
Full identifier (or key)
DataObject
Definition: DataObject.h:36
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
Gaudi::Hive::FetchLeavesFromFile::initialize
StatusCode initialize() override
Definition: FetchLeavesFromFile.cpp:52
IRegistry::dataSvc
virtual IDataProviderSvc * dataSvc() const =0
Retrieve pointer to Transient Store.
Gaudi::Property< std::string >
IDataManagerSvc.h
Gaudi::Hive::FetchLeavesFromFile::m_dataMgrSvc
SmartIF< IDataManagerSvc > m_dataMgrSvc
Definition: FetchLeavesFromFile.cpp:47