The Gaudi Framework  v33r0 (d5ea422b)
HiveTestAlgorithm.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 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/MsgStream.h"
14 
15 #include <atomic>
16 
17 DECLARE_COMPONENT( HiveTestAlgorithm )
18 
19 using namespace std;
20 
21 class MyObject : public DataObject {
22  int m_data;
25 
26 public:
27  MyObject( int d ) : m_data( d ) { c_instances++; }
28  MyObject( const MyObject& o ) : DataObject(), m_data( o.m_data ) { c_instances++; }
29  ~MyObject() { d_instances++; }
30  int getData() { return m_data; }
31  static void dump() { cout << "MyObject (C/D): " << c_instances << "/" << d_instances << endl; }
32 };
33 
36 
38  info() << ":HiveTestAlgorithm::initialize " << endmsg;
39 
40  int i = 0;
41  for ( auto k : m_inputs ) {
42  debug() << "adding input key " << k << endmsg;
43  m_inputHandles.push_back( new DataObjectHandle<DataObject>( k, Gaudi::DataHandle::Reader, this ) );
44  declareProperty( "dummy_in_" + std::to_string( i ), *( m_inputHandles.back() ) );
45  i++;
46  }
47 
48  i = 0;
49  for ( auto k : m_outputs ) {
50  debug() << "adding output key " << k << endmsg;
51  m_outputHandles.push_back( new DataObjectHandle<DataObject>( k, Gaudi::DataHandle::Writer, this ) );
52  declareProperty( "dummy_out_" + std::to_string( i ), *( m_outputHandles.back() ) );
53  i++;
54  }
55 
56  return StatusCode::SUCCESS;
57 }
58 
60  ++m_total;
62 
63  info() << ":HiveTestAlgorithm::getting inputs... " << evt << endmsg;
64 
65  for ( auto& handle : m_inputHandles ) {
66  auto obj = dynamic_cast<MyObject*>( handle->get() );
67  if ( !obj ) {
68  fatal() << "Unable to dcast inputHandles object" << endmsg;
69  return StatusCode::FAILURE;
70  }
71  info() << "Got data with value " << obj->getData() << endmsg;
72  }
73 
74  info() << ":HiveTestAlgorithm::registering outputs... " << evt << endmsg;
75 
76  for ( auto& outputHandle : m_outputHandles ) { outputHandle->put( new MyObject( 1000 + evt ) ); }
77 
78  return StatusCode::SUCCESS;
79 }
80 
82  info() << name() << ":HiveTestAlgorithm::finalize - total events: " << m_total << endmsg;
84  return StatusCode::SUCCESS;
85 }
86 
88 
static atomic< int > c_instances
MyObject(const MyObject &o)
ContextEvt_t evt() const
Definition: EventContext.h:50
T to_string(T... args)
T endl(T... args)
virtual const std::vector< std::string > get_outputs()
constexpr static const auto SUCCESS
Definition: StatusCode.h:96
STL namespace.
static void dump()
static atomic< int > d_instances
#define DECLARE_COMPONENT(type)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
GAUDI_API const EventContext & currentContext()
StatusCode initialize() override
virtual const std::vector< std::string > get_inputs()
constexpr static const auto FAILURE
Definition: StatusCode.h:97
StatusCode execute() override
StatusCode finalize() override
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:40
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202