ATLAS Offline Software
Loading...
Searching...
No Matches
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
19namespace 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.
28 ATH_CHECK( m_systematicsList.initialize() );
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.
39 ANA_CHECK( m_event.writeTo( ofile ) );
40
41 // Reset the internal flag(s).
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() );
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.
109 ANA_CHECK( m_event.finishWritingTo( ofile ) );
110
111 // Return gracefully.
112 return StatusCode::SUCCESS;
113 }
114
115 StatusCode xAODWriterAlg::setup() {
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;
138 ANA_CHECK (m_systematicsList.service().makeSystematicsName( key, itemMatch[ 2 ], sys ));
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
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_FATAL(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_DEBUG(x)
#define ANA_CHECK(EXP)
check whether the given expression was successful
bool m_itemListInitialized
Internal flag.
StatusCode setup()
Function setting up the algorithm while processing the first event.
StatusCode finalize() override
Function finalising the algorithm.
std::vector< Item > m_writtenItemList
Item list being written after the first event.
xAOD::TEvent m_event
Object to write the output file with.
StatusCode execute() override
Function executing the algorithm.
Gaudi::Property< std::vector< std::string > > m_itemList
Item list to write to the output file.
SysListHandle m_systematicsList
The systematic list to consider during execution.
Gaudi::Property< std::string > m_outputStreamName
Name of the output stream to write to.
StatusCode initialize() override
Function initialising the algorithm.
Tool for accessing xAOD files outside of Athena.
Select isolated Photons, Electrons and Muons.
@ SILENT
don't print anything and return success
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...
Convert a type_info to a normalized string representation (matching the names used in the root dictio...