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  ANA_CHECK( m_event.record( const_cast< void* >( obj ), item.typeName,
83  item.name, m_basketSize, m_splitLevel ) );
84  }
85 
86  // Write the event.
87  if( m_event.fill() <= 0 ) {
88  ATH_MSG_FATAL( "There was an error writing out the event" );
89  return StatusCode::FAILURE;
90  }
91 
92  // Return gracefully.
93  return StatusCode::SUCCESS;
94  }
95 
97 
98  // Access the file of the output stream.
99  TFile* ofile = wk()->getOutputFile( m_outputStreamName );
100  if( ! ofile ) {
101  ATH_MSG_FATAL( "Couldn't access output file for stream \""
102  << m_outputStreamName << "\"" );
103  return StatusCode::FAILURE;
104  }
105 
106  // Finish writing to this output file.
108 
109  // Return gracefully.
110  return StatusCode::SUCCESS;
111  }
112 
114 
115  // Loop over all of the declared items.
116  for( const std::string& stringItem : m_itemList ) {
117 
118  // Interpret the item string.
119  static const std::regex itemRegex( "([^#]+)#([^\\.]+\\.?)(.*)" );
120  std::smatch itemMatch;
121  if( ! std::regex_match( stringItem, itemMatch, itemRegex ) ) {
122  ATH_MSG_FATAL( "Item \"" << stringItem
123  << "\" is not of the form: \"Type#Name\"" );
124  return StatusCode::FAILURE;
125  }
126  ATH_MSG_VERBOSE( "Found item: " << itemMatch[ 1 ] << "#"
127  << itemMatch[ 2 ] << itemMatch[ 3 ] );
128 
129  // Consider all systematics. Not usin CP::SysListHandle::foreach, to
130  // be able to exit the for-loop early if necessary.
131  auto sysVector = m_systematicsList.systematicsVector();
132  for( const auto& sys : sysVector ) {
133 
134  // Event store key for the object under consideration.
135  std::string key;
137 
138  // Whether or not the object will be available, as long as
139  // variable selection rules were set up for it, let xAOD::TEvent
140  // know about them.
141  if( itemMatch[ 3 ] != "" ) {
142  ATH_MSG_DEBUG( "Calling setAuxItemList( \"" << key << "\""
143  << ", \"" << itemMatch[ 3 ]
144  << "\" )" );
145  m_event.setAuxItemList( key, itemMatch[ 3 ] );
146  }
147 
148  // Construct an Item object.
149  Item item;
150  item.name = key;
151  TClass* cl = TClass::GetClass( itemMatch[ 1 ].str().c_str() );
152  if( ! cl ) {
153  ATH_MSG_FATAL( "Type \"" << itemMatch[ 1 ] << "\" not found" );
154  return StatusCode::FAILURE;
155  }
156  item.type = cl->GetTypeInfo();
157  if( ! item.type ) {
158  ATH_MSG_FATAL( "No compiled dictionary found for \""
159  << itemMatch[ 1 ] << "\"" );
160  return StatusCode::FAILURE;
161  }
162  item.typeName = SG::normalizedTypeinfoName( *( item.type ) );
163 
164  // Check if the item is available.
165  static const bool SILENT = true;
166  static const bool METADATA = false;
167  xAOD::TEvent* event = evtStore()->event();
168  if( event->getOutputObject( item.name, *( item.type ), METADATA ) ||
169  event->getInputObject( item.name, *( item.type ), SILENT,
170  METADATA ) ) {
171  m_writtenItemList.push_back( item );
172  ATH_MSG_DEBUG( "Scheduling \"" << itemMatch[ 1 ] << "#"
173  << key << "\" for writing" );
174  }
175 
176  // If there was no %SYS% pattern in the object name, stop the loop
177  // over the systematics now.
178  if( key == itemMatch[ 2 ] ) {
179  break;
180  }
181  }
182  }
183 
184  // Return gracefully.
185  return StatusCode::SUCCESS;
186  }
187 
188 } // namespace CP
xAOD::TEvent::finishWritingTo
StatusCode finishWritingTo(::TFile *file)
Finish writing to an output file.
Definition: Control/xAODRootAccess/Root/TEvent.cxx:735
CP::xAODWriterAlg::setup
StatusCode setup()
Function setting up the algorithm while processing the first event.
Definition: xAODWriterAlg.cxx:113
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
CP::xAODWriterAlg::m_basketSize
Gaudi::Property< int > m_basketSize
(Starter) Basket size for the created branches
Definition: xAODWriterAlg.h:61
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:48
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::xAODWriterAlg::m_splitLevel
Gaudi::Property< int > m_splitLevel
Split level for the created branches.
Definition: xAODWriterAlg.h:63
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
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::record
StatusCode record(T *obj, const std::string &key, ::Int_t basketSize=32000, ::Int_t splitLevel=0)
Add an output object to the event.
item
Definition: ItemListSvc.h:43
xAOD::TEvent::fill
::Int_t fill()
Function filling one event into the output tree.
Definition: Control/xAODRootAccess/Root/TEvent.cxx:1509
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
python.CaloScaleNoiseConfig.str
str
Definition: CaloScaleNoiseConfig.py:78
Worker.h
CP::xAODWriterAlg::execute
StatusCode execute() override
Function executing the algorithm.
Definition: xAODWriterAlg.cxx:48
xAOD::TEvent::writeTo
StatusCode writeTo(::TFile *file, Int_t autoFlush=200, const char *treeName=EVENT_TREE_NAME)
Connect the object to an output file.
Definition: Control/xAODRootAccess/Root/TEvent.cxx:693
xAOD::TEvent::setAuxItemList
void setAuxItemList(const std::string &containerKey, const std::string &itemList)
Configure which dynamic variables to write out for a given store.
Definition: Control/xAODRootAccess/Root/TEvent.cxx:883
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:26
CP::xAODWriterAlg::finalize
StatusCode finalize() override
Function finalising the algorithm.
Definition: xAODWriterAlg.cxx:96
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:84
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37