The Gaudi Framework  master (37c0b60a)
AnyDataAlgorithm.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 #include <GaudiKernel/Algorithm.h>
14 #include <string>
15 #include <vector>
16 //-----------------------------------------------------------------------------
17 // Implementation file for class : AnyData{Get,Put}Algorithm
18 //
19 // 2016-05-26 : Roel Aaij
20 //-----------------------------------------------------------------------------
28  Gaudi::Property<std::string> m_loc{ this, "Location", "Test" };
29  DataObjectWriteHandle<std::vector<int>> m_ids{ this, "Output", "/Event/Test/Ids" };
31 
32 public:
33  AnyDataPutAlgorithm( const std::string& name, ISvcLocator* pSvcLocator ) : Algorithm( name, pSvcLocator ) {
34  for ( int i = 0; i < 100; i++ ) { m_id_vec.emplace_back( "/Event/Test/Ids" + std::to_string( i ), this ); }
35  }
36 
37  StatusCode execute() override {
38  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Execute" << endmsg;
39 
40  auto i = std::make_unique<AnyDataWrapper<int>>( 0 );
41  auto j = std::make_unique<AnyDataWrapper<std::vector<int>>>( std::vector<int>{ 0, 1, 2, 3 } );
42 
43  eventSvc()
44  ->registerObject( m_loc.value() + "/One", i.release() )
45  .orThrow( "failed to register " + m_loc.value() + "/One" );
46  eventSvc()
47  ->registerObject( m_loc.value() + "/Two", j.release() )
48  .orThrow( "failed to register " + m_loc.value() + "/Two" );
49 
50  m_ids.put( std::vector<int>( { 42, 84 } ) );
51 
52  for ( int i = 0; i < 100; ++i ) m_id_vec[i].put( std::move( i ) );
53 
54  return StatusCode::SUCCESS;
55  }
56 };
57 
58 // Declaration of the Algorithm Factory
66 template <class T>
69 
70  DataObjectReadHandle<std::vector<int>> m_ids{ this, "Input", "/Event/Test/Ids" };
71 
72 public:
74 
75  StatusCode execute() override {
76  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Execute" << endmsg;
77 
78  DataObject* tmp = nullptr;
79  eventSvc()->retrieveObject( m_location, tmp ).ignore();
80  auto base = dynamic_cast<AnyDataWrapperBase*>( tmp );
81  if ( base ) {
82  info() << "Got base from " << m_location.value() << endmsg;
83  } else {
84  error() << "failed to get base from " << m_location.value() << endmsg;
85  return StatusCode::FAILURE;
86  }
87  const auto i = dynamic_cast<const AnyDataWrapper<T>*>( base );
88  if ( i ) {
89  info() << "Got " << System::typeinfoName( typeid( T ) ) << " from " << m_location.value() << ": " << i->getData()
90  << endmsg;
91  }
92 
93  const auto& ids = m_ids.get();
94  info() << "AnyDataHandler holds:";
95  for ( const auto& i : *ids ) info() << i << " ";
96  info() << endmsg;
97  return StatusCode::SUCCESS;
98  }
99 };
100 
101 // Declaration of the Algorithm Factory
102 DECLARE_COMPONENT_WITH_ID( AnyDataGetAlgorithm<int>, "AnyDataGetAlgorithm_Int" )
103 DECLARE_COMPONENT_WITH_ID( AnyDataGetAlgorithm<std::vector<int>>, "AnyDataGetAlgorithm_VectorInt" )
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:25
DataObjectWriteHandle
Definition: DataObjectHandle.h:474
AnyDataGetAlgorithm::m_ids
DataObjectReadHandle< std::vector< int > > m_ids
Definition: AnyDataAlgorithm.cpp:70
std::string
STL class.
AnyDataWrapper.h
std::move
T move(T... args)
Gaudi::Algorithm::name
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:526
Gaudi::Algorithm::eventSvc
SmartIF< IDataProviderSvc > & eventSvc() const
The standard event data service.
Definition: Algorithm.cpp:559
std::vector
STL class.
ISvcLocator
Definition: ISvcLocator.h:46
AnyDataPutAlgorithm::m_ids
DataObjectWriteHandle< std::vector< int > > m_ids
Definition: AnyDataAlgorithm.cpp:29
Algorithm
Alias for backward compatibility.
Definition: Algorithm.h:58
Algorithm.h
AnyDataPutAlgorithm
Definition: AnyDataAlgorithm.cpp:27
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:315
AnyDataPutAlgorithm::AnyDataPutAlgorithm
AnyDataPutAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Definition: AnyDataAlgorithm.cpp:33
CommonMessaging< implements< IAlgorithm, IDataHandleHolder, IProperty, IStateful > >::msgLevel
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
Definition: CommonMessaging.h:148
AnyDataWrapperBase
Definition: AnyDataWrapper.h:30
DECLARE_COMPONENT_WITH_ID
#define DECLARE_COMPONENT_WITH_ID(type, id)
Definition: PluginServiceV1.h:47
DataObjectHandle::get
T * get() const
Retrieve object from transient data store.
Definition: DataObjectHandle.h:117
StatusCode
Definition: StatusCode.h:65
ProduceConsume.j
j
Definition: ProduceConsume.py:104
DataObjectReadHandle
Definition: DataObjectHandle.h:452
Gaudi::Property::value
const ValueType & value() const
Definition: Property.h:237
std::to_string
T to_string(T... args)
AnyDataGetAlgorithm::execute
StatusCode execute() override
Definition: AnyDataAlgorithm.cpp:75
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
AnyDataWrapper
Definition: AnyDataWrapper.h:55
Containers::vector
struct GAUDI_API vector
Parametrisation class for vector-like implementation.
Definition: KeyedObjectManager.h:39
std::vector::emplace_back
T emplace_back(T... args)
DataObjectHandle::put
T * put(std::unique_ptr< T > object) const
Register object in transient store.
Definition: DataObjectHandle.h:180
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
AnyDataPutAlgorithm::m_id_vec
std::vector< DataObjectWriteHandle< int > > m_id_vec
Definition: AnyDataAlgorithm.cpp:30
std
STL namespace.
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
AnyDataPutAlgorithm::execute
StatusCode execute() override
Definition: AnyDataAlgorithm.cpp:37
DataObject
Definition: DataObject.h:36
AnyDataPutAlgorithm::m_loc
Gaudi::Property< std::string > m_loc
Definition: AnyDataAlgorithm.cpp:28
AnyDataGetAlgorithm::m_location
Gaudi::Property< std::string > m_location
Definition: AnyDataAlgorithm.cpp:68
check_ParticleID.base
base
Definition: check_ParticleID.py:24
Gaudi::Algorithm::Algorithm
Algorithm(std::string name, ISvcLocator *svcloc, std::string version=PACKAGE_VERSION)
Constructor.
Definition: Algorithm.h:101
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
DataObjectHandle.h
Gaudi::Property< std::string >
Gaudi::Functional::details::put
auto put(const DataObjectHandle< Out1 > &out_handle, Out2 &&out)
Definition: details.h:168
AnyDataGetAlgorithm
Definition: AnyDataAlgorithm.cpp:67