The Gaudi Framework  master (37c0b60a)
IncidentAsyncTestSvc.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 "IncidentAsyncTestSvc.h"
12 
15 #include <GaudiKernel/IService.h>
17 
18 #include <GaudiKernel/MsgStream.h>
19 
22 #include <GaudiKernel/MsgStream.h>
24 
26 
27 #define ON_DEBUG if ( msgLevel( MSG::DEBUG ) )
28 #define ON_VERBOSE if ( msgLevel( MSG::VERBOSE ) )
29 
30 #define DEBMSG ON_DEBUG debug()
31 #define VERMSG ON_VERBOSE verbose()
32 
34  auto sc = Service::initialize();
35  if ( sc.isFailure() ) return sc;
36  m_incSvc = service( "IncidentSvc", true );
37  if ( !m_incSvc ) throw GaudiException( "Cannot find IncidentSvc", name(), StatusCode::FAILURE );
38  m_msgSvc = msgSvc();
39  if ( m_incidentNames.empty() ) {
40  std::vector<std::string> incNames;
41  incNames.push_back( IncidentType::BeginEvent );
42  incNames.push_back( IncidentType::EndEvent );
43  m_incidentNames.setValue( incNames );
44  }
45  auto& incNames = m_incidentNames.value();
46  for ( auto& i : incNames ) { m_incSvc->addListener( this, i, m_prio ); }
47  return sc;
48 }
49 
51 
52 //=============================================================================
53 void IncidentAsyncTestSvc::handle( const Incident& incident ) {
54  if ( incident.type() == IncidentType::BeginEvent ) {
55  auto res = m_ctxData.insert(
56  std::make_pair( incident.context(), incident.context().evt() * m_eventMultiplier + m_fileOffset ) );
57  if ( !res.second ) {
58  warning() << " Context already exists for '" << incident.type() << "' event=" << incident.context().evt()
59  << endmsg;
60  }
61  } else if ( incident.type() == IncidentType::EndEvent ) {
62  {
63  auto g = std::scoped_lock{ m_eraseMutex };
64  auto res = m_ctxData.unsafe_erase( incident.context() );
65  if ( res == 0 ) {
66  warning() << " Context is missing for '" << incident.type() << "' event=" << incident.context().evt() << endmsg;
67  }
68  }
69  info() << " Cleaned up context store for event =" << incident.context().evt() << " for incident='"
70  << incident.type() << "'" << endmsg;
71  }
72  info() << " Handling incident '" << incident.type() << "' at ctx=" << incident.context() << endmsg;
73 }
74 
75 void IncidentAsyncTestSvc::getData( uint64_t* data, EventContext* ctx ) const {
76  if ( ctx ) {
77  debug() << "Asked for data with context " << *ctx << endmsg;
78  auto cit = m_ctxData.find( *ctx );
79  if ( cit == m_ctxData.end() ) {
80  fatal() << " data for event " << ctx->evt() << " is not initialized yet!. This shouldn't happen!" << endmsg;
81  return;
82  }
83  *data = cit->second;
84  } else {
85  const auto& ct = Gaudi::Hive::currentContext();
86  auto cit = m_ctxData.find( ct );
87  if ( cit == m_ctxData.end() ) {
88  fatal() << " data for event " << ct.evt() << " is not initialized yet!. This shouldn't happen!" << endmsg;
89  return;
90  }
91  *data = cit->second;
92  }
93 }
IncidentAsyncTestSvc::m_prio
Gaudi::Property< long > m_prio
Definition: IncidentAsyncTestSvc.h:46
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:118
IService.h
Gaudi::Hive::currentContext
GAUDI_API const EventContext & currentContext()
Definition: ThreadLocalContext.cpp:30
GaudiException.h
std::vector< std::string >
GaudiException
Definition: GaudiException.h:31
IncidentAsyncTestSvc::m_incSvc
SmartIF< IIncidentSvc > m_incSvc
Definition: IncidentAsyncTestSvc.h:48
IncidentAsyncTestSvc::m_msgSvc
SmartIF< IMessageSvc > m_msgSvc
Definition: IncidentAsyncTestSvc.h:47
IMessageSvc.h
Service::finalize
StatusCode finalize() override
Definition: Service.cpp:222
IncidentAsyncTestSvc::initialize
StatusCode initialize() override
Definition: IncidentAsyncTestSvc.cpp:33
AvalancheSchedulerErrorTest.msgSvc
msgSvc
Definition: AvalancheSchedulerErrorTest.py:80
Gaudi::Property::setValue
bool setValue(const ValueType &v)
Definition: Property.h:239
IIncidentSvc.h
std::vector::push_back
T push_back(T... args)
Incident::context
EventContext context() const
Access to the EventContext of the source of the incident.
Definition: Incident.h:60
GaudiPython.Pythonizations.ctx
ctx
Definition: Pythonizations.py:578
Service::name
const std::string & name() const override
Retrieve name of the service
Definition: Service.cpp:332
StatusCode
Definition: StatusCode.h:65
IncidentAsyncTestSvc.h
IncidentAsyncTestSvc::m_ctxData
tbb::concurrent_unordered_map< EventContext, uint64_t, EventContextHash, EventContextHash > m_ctxData
Definition: IncidentAsyncTestSvc.h:49
gaudirun.g
dictionary g
Definition: gaudirun.py:582
IncidentAsyncTestSvc::m_fileOffset
Gaudi::Property< uint64_t > m_fileOffset
Definition: IncidentAsyncTestSvc.h:43
IncidentAsyncTestSvc::m_incidentNames
Gaudi::Property< std::vector< std::string > > m_incidentNames
Definition: IncidentAsyncTestSvc.h:45
Gaudi::Property::value
const ValueType & value() const
Definition: Property.h:237
IncidentAsyncTestSvc::getData
void getData(uint64_t *data, EventContext *ctx=0) const override
Definition: IncidentAsyncTestSvc.cpp:75
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
IncidentAsyncTestSvc
Definition: IncidentAsyncTestSvc.h:29
IncidentAsyncTestSvc::m_eventMultiplier
Gaudi::Property< uint64_t > m_eventMultiplier
Definition: IncidentAsyncTestSvc.h:44
ThreadLocalContext.h
IncidentAsyncTestSvc::m_eraseMutex
std::mutex m_eraseMutex
Definition: IncidentAsyncTestSvc.h:50
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
IncidentAsyncTestSvc::finalize
StatusCode finalize() override
Definition: IncidentAsyncTestSvc.cpp:50
EventContext
Definition: EventContext.h:34
Incident::type
const std::string & type() const
Access to the incident type.
Definition: Incident.h:48
std::make_pair
T make_pair(T... args)
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
ISvcLocator.h
Incident
Definition: Incident.h:27
Service::service
StatusCode service(const std::string &name, const T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Service.h:89
EventContext::evt
ContextEvt_t evt() const
Definition: EventContext.h:50
IncidentAsyncTestSvc::handle
void handle(const Incident &incident) override
Reimplements from IIncidentListener.
Definition: IncidentAsyncTestSvc.cpp:53
MsgStream.h