ATLAS Offline Software
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):
20 # include "xAODRootAccess/TEvent.h"
21 #endif // XAOD_STANDALONE
22 
23 namespace 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 
30  AsgMetadataTool::AsgMetadataTool( const std::string& name )
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
39  m_beginInputFileCalled( false ),
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 ) {
142  m_beginInputFileCalled = true;
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 ) {
157  m_beginInputFileCalled = true;
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
xAOD::TVirtualEvent
Base interface for getting objects out of the input file.
Definition: TVirtualEvent.h:32
asg::AsgTool
Base class for the dual-use tool implementation classes.
Definition: AsgTool.h:47
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
asg::AsgMetadataTool::beginEvent
virtual StatusCode beginEvent()
Function called when a new events is loaded.
Definition: AsgMetadataTool.cxx:201
asg::AsgMetadataTool::AsgMetadataTool
AsgMetadataTool(const std::string &name)
Normal ASG tool constructor with a name.
Definition: AsgMetadataTool.cxx:30
asg
Definition: DataHandleTestTool.h:28
asg::AsgMetadataTool::inputMetaStore
MetaStorePtr_t inputMetaStore() const
Accessor for the input metadata store.
Definition: AsgMetadataTool.cxx:93
xAOD
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.
Definition: ICaloAffectedTool.h:24
asg::AsgMetadataTool::m_beginInputFileCalled
bool m_beginInputFileCalled
Flag helping to discover when the tool misses the opening of the first input file.
Definition: AsgMetadataTool.h:126
asg::AsgMetadataTool::endInputFile
virtual StatusCode endInputFile()
Function called when the currently open input file got completely processed.
Definition: AsgMetadataTool.cxx:193
asg::AsgMetadataTool::m_outputMetaStore
MetaStore_t m_outputMetaStore
Object accessing the output metadata store.
Definition: AsgMetadataTool.h:121
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
xAOD::TActiveEvent::event
static TVirtualEvent * event()
Access the currently active TVirtualEvent object.
Definition: TActiveEvent.cxx:16
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
asg::AsgMetadataTool::beginInputFile
virtual StatusCode beginInputFile()
Function called when a new input file is opened.
Definition: AsgMetadataTool.cxx:185
TEvent.h
asg::AsgMetadataTool::metaDataStop
virtual StatusCode metaDataStop()
Function called when the tool should write out its metadata.
Definition: AsgMetadataTool.cxx:209
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
TActiveEvent.h
asg::AsgMetadataTool::m_inputMetaStore
MetaStore_t m_inputMetaStore
Object accessing the input metadata store.
Definition: AsgMetadataTool.h:119
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
asg::AsgMetadataTool::sysInitialize
virtual StatusCode sysInitialize()
Function initialising the tool in the correct way in Athena.
Definition: AsgMetadataTool.cxx:115
asg::AsgMetadataTool::~AsgMetadataTool
~AsgMetadataTool()
Destructor.
Definition: AsgMetadataTool.cxx:62
asg::SgTEventMeta
Wrapper class providing StoreGate-like access to metadata in ROOT.
Definition: SgTEventMeta.h:45
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
asg::AsgMetadataTool::outputMetaStore
MetaStorePtr_t outputMetaStore() const
Accessor for the output metadata store.
Definition: AsgMetadataTool.cxx:102
AsgMetadataTool.h
asg::AsgMetadataTool::handle
virtual void handle(const Incident &inc)
Function receiving incidents from IncidentSvc/TEvent.
Definition: AsgMetadataTool.cxx:135
xAOD::TEvent
Tool for accessing xAOD files outside of Athena.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:85
ServiceHandle< IIncidentSvc >
asg::AsgMetadataTool::m_useIncidents
bool m_useIncidents
Definition: AsgMetadataTool.h:128