ATLAS Offline Software
Loading...
Searching...
No Matches
AsgMetadataTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5// System include(s):
6#include <stdexcept>
7
8// Local include(s):
10
11#ifndef XAOD_STANDALONE
12// Gaudi/Athena include(s):
13# include "GaudiKernel/Incident.h"
14# include "GaudiKernel/IIncidentSvc.h"
15#endif // not XAOD_STANDALONE
16
17#ifdef XAOD_STANDALONE
18// xAOD include(s):
21#endif // XAOD_STANDALONE
22
23namespace asg {
24
25#ifdef XAOD_STANDALONE
26 // To be able to refer to xAOD::IncidentType as IncidentType...
27 using namespace xAOD;
28#endif // XAOD_STANDALONE
29
31 : AsgTool( name ),
32#ifdef XAOD_STANDALONE
33 m_inputMetaStore( SgTEventMeta::InputStore ),
34 m_outputMetaStore( SgTEventMeta::OutputStore ),
35#else // XAOD_STANDALONE
36 m_inputMetaStore( "StoreGateSvc/InputMetaDataStore", name ),
37 m_outputMetaStore( "StoreGateSvc/MetaDataStore", name ),
38#endif // XAOD_STANDALONE
40 m_useIncidents (true)
41 {
42
43#ifdef XAOD_STANDALONE
44 // Try to access the current active TEvent:
46 xAOD::TEvent* event = dynamic_cast< xAOD::TEvent* >( vevent );
47 if( ! event ) {
48 ATH_MSG_WARNING( "Couldn't find active xAOD::TEvent object" );
49 ATH_MSG_WARNING( "Callbacks to the tool will not be available" );
50 return;
51 }
52 // Register the tool for callbacks:
53 if( event->addListener( this ).isFailure() ) {
54 ATH_MSG_ERROR( "Couldn't register the tool for xAOD callbacks" );
55 }
56#else // XAOD_STANDALONE
57 // Declare the interface implemented by this base class:
58 declareInterface< IIncidentListener >( this );
59#endif // XAOD_STANDALONE
60 }
61
63
64#ifdef XAOD_STANDALONE
65 // Try to access the active TEvent:
67 xAOD::TEvent* event = dynamic_cast< xAOD::TEvent* >( vevent );
68 // If we didn't succeed, fine. No need to complain about it...
69 if( event ) {
70 // But if there is a TEvent, then let's try to de-register from it
71 // properly.
72 if( event->removeListener( this ).isFailure() ) {
73 ATH_MSG_WARNING( "Active xAOD::TEvent didn't have a callback for "
74 "this tool" );
75 }
76 }
77#else
78 //if initialized, then we should remove listeners
79 if(FSMState() == Gaudi::StateMachine::INITIALIZED) {
80 ServiceHandle< IIncidentSvc > incSvc( "IncidentSvc", name() );
81 if( incSvc.retrieve().isSuccess() ) {
82 try {
83 incSvc->removeListener( this ); //removes entirely
84 }
85 catch (const GaudiException&) {
86 std::abort();
87 }
88 }
89 }
90#endif // XAOD_STANDALONE
91 }
92
94
95#ifdef XAOD_STANDALONE
96 return &m_inputMetaStore;
97#else // XAOD_STANDALONE
98 return m_inputMetaStore;
99#endif // XAOD_STANDALONE
100 }
101
103
104#ifdef XAOD_STANDALONE
105 return &m_outputMetaStore;
106#else // XAOD_STANDALONE
107 return m_outputMetaStore;
108#endif // XAOD_STANDALONE
109 }
110
116
117#ifndef XAOD_STANDALONE
118 if (m_useIncidents) {
119 // Connect to the IncidentSvc:
120 ServiceHandle< IIncidentSvc > incSvc( "IncidentSvc", name() );
121 ATH_CHECK( incSvc.retrieve() );
122
123 // Set up the right callbacks: don't rethrow exceptions, any failure and we should end
124 incSvc->addListener( this, IncidentType::BeginEvent, 0, false );
125 }
126 // Let the base class do its thing:
127 ATH_CHECK( AlgTool::sysInitialize() );
128
129#endif // not XAOD_STANDALONE
130
131 // Return gracefully:
132 return StatusCode::SUCCESS;
133 }
134
135 void AsgMetadataTool::handle( const Incident& inc ) {
136
137 // Tell the user what's happening:
138 ATH_MSG_VERBOSE( "Callback received with incident: " << inc.type() );
139
140 // Call the appropriate member function:
141 if( inc.type() == IncidentType::BeginInputFile ) {
143 if( beginInputFile().isFailure() ) {
144 ATH_MSG_FATAL( "Failed to call beginInputFile()" );
145 throw std::runtime_error( "Couldn't call beginInputFile()" );
146 }
147 } else if( inc.type() == IncidentType::EndInputFile ) {
148 if( endInputFile().isFailure() ) {
149 ATH_MSG_FATAL( "Failed to call endInputFile()" );
150 throw std::runtime_error( "Couldn't call endInputFile()" );
151 }
152 } else if( inc.type() == IncidentType::BeginEvent ) {
153 // If the tool didn't catch the begin input file incident for the
154 // first input file of the job, then call the appropriate function
155 // now.
156 if( ! m_beginInputFileCalled ) {
158 if( beginInputFile().isFailure() ) {
159 ATH_MSG_FATAL( "Failed to call beginInputFile()" );
160 throw std::runtime_error( "Couldn't call beginInputFile()" );
161 }
162 }
163 if( beginEvent().isFailure() ) {
164 ATH_MSG_FATAL( "Failed to call beginEvent()" );
165 throw std::runtime_error( "Couldn't call beginEvent()" );
166 }
167
168 #ifdef XAOD_STANDALONE
169 } else if( inc.type() == IncidentType::MetaDataStop ) {
170 if( metaDataStop().isFailure() ) {
171 ATH_MSG_FATAL( "Failed to call metaDataStop()" );
172 throw std::runtime_error( "Couldn't call metaDataStop()" );
173 }
174
175 #endif // XAOD_STANDALONE
176 } else {
177 ATH_MSG_WARNING( "Unknown incident type received in AsgMetaDataTool: " << inc.type() );
178 }
179
180 return;
181 }
182
186
187 // Return gracefully:
188 return StatusCode::SUCCESS;
189 }
190
194
195 // Return gracefully:
196 return StatusCode::SUCCESS;
197 }
198
202
203 // Return gracefully:
204 return StatusCode::SUCCESS;
205 }
206
210
211 // Return gracefully:
212 return StatusCode::SUCCESS;
213 }
214
215} // namespace asg
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
MetaStorePtr_t outputMetaStore() const
Accessor for the output metadata store.
virtual StatusCode beginInputFile()
Function called when a new input file is opened.
virtual StatusCode beginEvent()
Function called when a new events is loaded.
bool m_beginInputFileCalled
Flag helping to discover when the tool misses the opening of the first input file.
MetaStore_t m_outputMetaStore
Object accessing the output metadata store.
virtual void handle(const Incident &inc)
Function receiving incidents from IncidentSvc/TEvent.
const ServiceHandle< StoreGateSvc > & MetaStorePtr_t
Type of the metadata store pointer in standalone mode.
AsgMetadataTool(const std::string &name)
Normal ASG tool constructor with a name.
~AsgMetadataTool()
Destructor.
MetaStore_t m_inputMetaStore
Object accessing the input metadata store.
virtual StatusCode endInputFile()
Function called when the currently open input file got completely processed.
virtual StatusCode metaDataStop()
Function called when the tool should write out its metadata.
virtual StatusCode sysInitialize()
Function initialising the tool in the correct way in Athena.
MetaStorePtr_t inputMetaStore() const
Accessor for the input metadata store.
AsgTool(const std::string &name)
Constructor specifying the tool instance's name.
Definition AsgTool.cxx:58
Wrapper class providing StoreGate-like access to metadata in ROOT.
static TVirtualEvent * event()
Access the currently active TVirtualEvent object.
Tool for accessing xAOD files outside of Athena.
Base interface for getting objects out of the input file.
static const ::Int_t BeginEvent
A new event was just loaded.
Definition TIncident.h:29
static const ::Int_t EndInputFile
The processing of an input file has finished.
Definition TIncident.h:40
static const ::Int_t MetaDataStop
The metadata for the output file should be written out.
Definition TIncident.h:31
static const ::Int_t BeginInputFile
A new input file was just opened.
Definition TIncident.h:27