ATLAS Offline Software
xAODWriterAlg.cxx
Go to the documentation of this file.
1 //
2 // Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 //
4 
5 // System include(s):
6 #include <regex>
7 
8 // ROOT include(s):
9 #include <TClass.h>
10 #include <TFile.h>
11 
12 // Core include(s):
14 #include "EventLoop/Worker.h"
15 
16 // Local include(s):
18 
19 namespace CP {
20 
22 
23  // Make sure that the xAOD::TEvent object managed by EventLoop is the
24  // "active" one.
25  evtStore()->event()->setActive();
26 
27  // Set up the systematics list.
29 
30  // Access the file of the output stream.
31  TFile* ofile = wk()->getOutputFile( m_outputStreamName );
32  if( ! ofile ) {
33  ATH_MSG_FATAL( "Couldn't access output file for stream \""
34  << m_outputStreamName << "\"" );
35  return StatusCode::FAILURE;
36  }
37 
38  // Write to this output file.
40 
41  // Reset the internal flag(s).
42  m_itemListInitialized = false;
43 
44  // Return gracefully.
45  return StatusCode::SUCCESS;
46  }
47 
49 
50  // If this is the first event, figure out which objects can actually be
51  // written out.
52  if( ! m_itemListInitialized ) {
53  ANA_CHECK( setup() );
54  m_itemListInitialized = true;
55  }
56 
57  // Write all objects to the output file.
58  xAOD::TEvent* event = evtStore()->event();
59  for( const Item& item : m_writtenItemList ) {
60 
61  // Get the object. See the description in @c xAOD::TEvent::retrieve
62  // (the const version) for an explanation of this implementation.
63  static const bool SILENT = true;
64  static const bool METADATA = false;
65  const void* obj = event->getOutputObject( item.name, *( item.type ),
66  METADATA );
67  if( ! obj ) {
68  obj = event->getInputObject( item.name, *( item.type ), SILENT,
69  METADATA );
70  } else {
71  event->getInputObject( item.name, *( item.type ), SILENT,
72  METADATA );
73  }
74 
75  // Check that we succeeded.
76  if( ! obj ) {
77  ATH_MSG_FATAL( "Couldn't retrieve object \"" << item.name << "\"" );
78  return StatusCode::FAILURE;
79  }
80 
81  // Record it to the output for the current event.
82  static constexpr bool OVERWRITE = false;
83  static constexpr bool IS_OWNER = true;
84  ANA_CHECK( m_event.record( const_cast< void* >( obj ), item.typeName,
85  item.name, OVERWRITE, METADATA, IS_OWNER ) );
86  }
87 
88  // Write the event.
89  if( m_event.fill() <= 0 ) {
90  ATH_MSG_FATAL( "There was an error writing out the event" );
91  return StatusCode::FAILURE;
92  }
93 
94  // Return gracefully.
95  return StatusCode::SUCCESS;
96  }
97 
99 
100  // Access the file of the output stream.
101  TFile* ofile = wk()->getOutputFile( m_outputStreamName );
102  if( ! ofile ) {
103  ATH_MSG_FATAL( "Couldn't access output file for stream \""
104  << m_outputStreamName << "\"" );
105  return StatusCode::FAILURE;
106  }
107 
108  // Finish writing to this output file.
110 
111  // Return gracefully.
112  return StatusCode::SUCCESS;
113  }
114 
116 
117  // Loop over all of the declared items.
118  for( const std::string& stringItem : m_itemList ) {
119 
120  // Interpret the item string.
121  static const std::regex itemRegex( "([^#]+)#([^\\.]+\\.?)(.*)" );
122  std::smatch itemMatch;
123  if( ! std::regex_match( stringItem, itemMatch, itemRegex ) ) {
124  ATH_MSG_FATAL( "Item \"" << stringItem
125  << "\" is not of the form: \"Type#Name\"" );
126  return StatusCode::FAILURE;
127  }
128  ATH_MSG_VERBOSE( "Found item: " << itemMatch[ 1 ] << "#"
129  << itemMatch[ 2 ] << itemMatch[ 3 ] );
130 
131  // Consider all systematics. Not usin CP::SysListHandle::foreach, to
132  // be able to exit the for-loop early if necessary.
133  auto sysVector = m_systematicsList.systematicsVector();
134  for( const auto& sys : sysVector ) {
135 
136  // Event store key for the object under consideration.
137  std::string key;
139 
140  // Whether or not the object will be available, as long as
141  // variable selection rules were set up for it, let xAOD::TEvent
142  // know about them.
143  if( itemMatch[ 3 ] != "" ) {
144  ATH_MSG_DEBUG( "Calling setAuxItemList( \"" << key << "\""
145  << ", \"" << itemMatch[ 3 ]
146  << "\" )" );
147  m_event.setAuxItemList( key, itemMatch[ 3 ] );
148  }
149 
150  // Construct an Item object.
151  Item item;
152  item.name = key;
153  TClass* cl = TClass::GetClass( itemMatch[ 1 ].str().c_str() );
154  if( ! cl ) {
155  ATH_MSG_FATAL( "Type \"" << itemMatch[ 1 ] << "\" not found" );
156  return StatusCode::FAILURE;
157  }
158  item.type = cl->GetTypeInfo();
159  if( ! item.type ) {
160  ATH_MSG_FATAL( "No compiled dictionary found for \""
161  << itemMatch[ 1 ] << "\"" );
162  return StatusCode::FAILURE;
163  }
164  item.typeName = SG::normalizedTypeinfoName( *( item.type ) );
165 
166  // Check if the item is available.
167  static const bool SILENT = true;
168  static const bool METADATA = false;
169  xAOD::TEvent* event = evtStore()->event();
170  if( event->getOutputObject( item.name, *( item.type ), METADATA ) ||
171  event->getInputObject( item.name, *( item.type ), SILENT,
172  METADATA ) ) {
173  m_writtenItemList.push_back( item );
174  ATH_MSG_DEBUG( "Scheduling \"" << itemMatch[ 1 ] << "#"
175  << key << "\" for writing" );
176  }
177 
178  // If there was no %SYS% pattern in the object name, stop the loop
179  // over the systematics now.
180  if( key == itemMatch[ 2 ] ) {
181  break;
182  }
183  }
184  }
185 
186  // Return gracefully.
187  return StatusCode::SUCCESS;
188  }
189 
190 } // namespace CP
xAOD::TEvent::finishWritingTo
StatusCode finishWritingTo(::TFile *file)
Finish writing to an output file.
Definition: Control/xAODRootAccess/Root/TEvent.cxx:447
CP::xAODWriterAlg::setup
StatusCode setup()
Function setting up the algorithm while processing the first event.
Definition: xAODWriterAlg.cxx:115
CP::xAODWriterAlg::initialize
StatusCode initialize() override
Function initialising the algorithm.
Definition: xAODWriterAlg.cxx:21
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
CP::xAODWriterAlg::m_writtenItemList
std::vector< Item > m_writtenItemList
Item list being written after the first event.
Definition: xAODWriterAlg.h:83
xAOD::TEvent::record
StatusCode record(void *obj, const std::string &typeName, const std::string &key, bool overwrite, bool metadata, bool isOwner) override
Record an object into a connected output file.
Definition: Control/xAODRootAccess/Root/TEvent.cxx:1638
CP::OutOfValidityAction::SILENT
@ SILENT
don't print anything and return success
SG::normalizedTypeinfoName
std::string normalizedTypeinfoName(const std::type_info &info)
Convert a type_info to a normalized string representation (matching the names used in the root dictio...
Definition: normalizedTypeinfoName.cxx:120
validation.ofile
ofile
Definition: validation.py:96
CP::xAODWriterAlg::m_itemListInitialized
bool m_itemListInitialized
Internal flag.
Definition: xAODWriterAlg.h:81
CP::xAODWriterAlg::m_event
xAOD::TEvent m_event
Object to write the output file with.
Definition: xAODWriterAlg.h:78
ANA_CHECK
#define ANA_CHECK(EXP)
check whether the given expression was successful
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:324
CP::SysListHandle::systematicsVector
const std::vector< CP::SystematicSet > & systematicsVector() const
the list of systematics to loop over
Definition: SysListHandle.cxx:96
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
CP
Select isolated Photons, Electrons and Muons.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:25
mapkey::sys
@ sys
Definition: TElectronEfficiencyCorrectionTool.cxx:42
PrepareReferenceFile.regex
regex
Definition: PrepareReferenceFile.py:43
PyPoolBrowser.item
item
Definition: PyPoolBrowser.py:129
AthCommonDataStore< AthCommonMsg< Algorithm > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
CP::SysListHandle::initialize
::StatusCode initialize()
intialize this property
Definition: SysListHandle.cxx:69
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
CP::xAODWriterAlg::Item
Helper struct.
Definition: xAODWriterAlg.h:68
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
python.CaloAddPedShiftConfig.str
str
Definition: CaloAddPedShiftConfig.py:42
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
CP::xAODWriterAlg::m_outputStreamName
Gaudi::Property< std::string > m_outputStreamName
Name of the output stream to write to.
Definition: xAODWriterAlg.h:57
normalizedTypeinfoName.h
Convert a type_info to a normalized string representation (matching the names used in the root dictio...
xAODWriterAlg.h
xAOD::TEvent::fill
::Int_t fill()
Function filling one event into the output tree.
Definition: Control/xAODRootAccess/Root/TEvent.cxx:814
CP::ISystematicsSvc::makeSystematicsName
virtual StatusCode makeSystematicsName(std::string &result, const std::string &name, const CP::SystematicSet &sys) const =0
make the name for the given systematics
CP::xAODWriterAlg::m_systematicsList
SysListHandle m_systematicsList
The systematic list to consider during execution.
Definition: xAODWriterAlg.h:86
Worker.h
CP::xAODWriterAlg::execute
StatusCode execute() override
Function executing the algorithm.
Definition: xAODWriterAlg.cxx:48
xAOD::Event::setAuxItemList
void setAuxItemList(const std::string &containerKey, const std::string &itemList)
Configure which dynamic variables to write out for a given store.
Definition: EventCore.cxx:71
CP::xAODWriterAlg::m_itemList
Gaudi::Property< std::vector< std::string > > m_itemList
Item list to write to the output file.
Definition: xAODWriterAlg.h:59
python.PyAthena.obj
obj
Definition: PyAthena.py:132
dq_make_web_display.cl
cl
print [x.__class__ for x in toList(dqregion.getSubRegions()) ]
Definition: dq_make_web_display.py:25
CP::xAODWriterAlg::finalize
StatusCode finalize() override
Function finalising the algorithm.
Definition: xAODWriterAlg.cxx:98
CP::SysListHandle::service
const ISystematicsSvc & service() const
the service we use
xAOD::TEvent
Tool for accessing xAOD files outside of Athena.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:57
xAOD::TEvent::writeTo
StatusCode writeTo(::TFile *file, int autoFlush=200, std::string_view treeName=EVENT_TREE_NAME)
Connect the object to an output file.
Definition: Control/xAODRootAccess/Root/TEvent.cxx:408
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37