ATLAS Offline Software
AsgMetadataTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 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  incSvc->removeListener( this ); //removes entirely
83  }
84  }
85 #endif // XAOD_STANDALONE
86  }
87 
89 
90 #ifdef XAOD_STANDALONE
91  return &m_inputMetaStore;
92 #else // XAOD_STANDALONE
93  return m_inputMetaStore;
94 #endif // XAOD_STANDALONE
95  }
96 
98 
99 #ifdef XAOD_STANDALONE
100  return &m_outputMetaStore;
101 #else // XAOD_STANDALONE
102  return m_outputMetaStore;
103 #endif // XAOD_STANDALONE
104  }
105 
111 
112 #ifndef XAOD_STANDALONE
113  if (m_useIncidents) {
114  // Connect to the IncidentSvc:
115  ServiceHandle< IIncidentSvc > incSvc( "IncidentSvc", name() );
116  ATH_CHECK( incSvc.retrieve() );
117 
118  // Set up the right callbacks: don't rethrow exceptions, any failure and we should end
119  incSvc->addListener( this, IncidentType::BeginEvent, 0, false );
120  }
121  // Let the base class do its thing:
122  ATH_CHECK( AlgTool::sysInitialize() );
123 
124 #endif // not XAOD_STANDALONE
125 
126  // Return gracefully:
127  return StatusCode::SUCCESS;
128  }
129 
130  void AsgMetadataTool::handle( const Incident& inc ) {
131 
132  // Tell the user what's happening:
133  ATH_MSG_VERBOSE( "Callback received with incident: " << inc.type() );
134 
135  // Call the appropriate member function:
136  if( inc.type() == IncidentType::BeginInputFile ) {
137  m_beginInputFileCalled = true;
138  if( beginInputFile().isFailure() ) {
139  ATH_MSG_FATAL( "Failed to call beginInputFile()" );
140  throw std::runtime_error( "Couldn't call beginInputFile()" );
141  }
142  } else if( inc.type() == IncidentType::EndInputFile ) {
143  if( endInputFile().isFailure() ) {
144  ATH_MSG_FATAL( "Failed to call endInputFile()" );
145  throw std::runtime_error( "Couldn't call endInputFile()" );
146  }
147  } else if( inc.type() == IncidentType::BeginEvent ) {
148  // If the tool didn't catch the begin input file incident for the
149  // first input file of the job, then call the appropriate function
150  // now.
151  if( ! m_beginInputFileCalled ) {
152  m_beginInputFileCalled = true;
153  if( beginInputFile().isFailure() ) {
154  ATH_MSG_FATAL( "Failed to call beginInputFile()" );
155  throw std::runtime_error( "Couldn't call beginInputFile()" );
156  }
157  }
158  if( beginEvent().isFailure() ) {
159  ATH_MSG_FATAL( "Failed to call beginEvent()" );
160  throw std::runtime_error( "Couldn't call beginEvent()" );
161  }
162 
163  #ifdef XAOD_STANDALONE
164  } else if( inc.type() == IncidentType::MetaDataStop ) {
165  if( metaDataStop().isFailure() ) {
166  ATH_MSG_FATAL( "Failed to call metaDataStop()" );
167  throw std::runtime_error( "Couldn't call metaDataStop()" );
168  }
169 
170  #endif // XAOD_STANDALONE
171  } else {
172  ATH_MSG_WARNING( "Unknown incident type received in AsgMetaDataTool: " << inc.type() );
173  }
174 
175  return;
176  }
177 
181 
182  // Return gracefully:
183  return StatusCode::SUCCESS;
184  }
185 
189 
190  // Return gracefully:
191  return StatusCode::SUCCESS;
192  }
193 
197 
198  // Return gracefully:
199  return StatusCode::SUCCESS;
200  }
201 
205 
206  // Return gracefully:
207  return StatusCode::SUCCESS;
208  }
209 
210 } // 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:196
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:88
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:188
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:180
TEvent.h
asg::AsgMetadataTool::metaDataStop
virtual StatusCode metaDataStop()
Function called when the tool should write out its metadata.
Definition: AsgMetadataTool.cxx:204
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:221
asg::AsgMetadataTool::sysInitialize
virtual StatusCode sysInitialize()
Function initialising the tool in the correct way in Athena.
Definition: AsgMetadataTool.cxx:110
asg::AsgMetadataTool::~AsgMetadataTool
~AsgMetadataTool()
Destructor.
Definition: AsgMetadataTool.cxx:62
asg::SgTEventMeta
Wrapper class providing StoreGate-like access to metadata in ROOT.
Definition: SgTEventMeta.h:44
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:97
AsgMetadataTool.h
asg::AsgMetadataTool::handle
virtual void handle(const Incident &inc)
Function receiving incidents from IncidentSvc/TEvent.
Definition: AsgMetadataTool.cxx:130
xAOD::TEvent
Tool for accessing xAOD files outside of Athena.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:84
ServiceHandle< IIncidentSvc >
asg::AsgMetadataTool::m_useIncidents
bool m_useIncidents
Definition: AsgMetadataTool.h:128