ATLAS Offline Software
Loading...
Searching...
No Matches
RNtupleTreeMakerAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5// Local include(s):
7
8// ROOT include(s):
9#include <TFile.h>
10
11// Gaudi/EventLoop include(s):
12#ifdef XAOD_STANDALONE
13#include "EventLoop/Worker.h"
14#endif
15
16namespace CP {
17
19 if( m_branches.empty() ) {
20 ATH_MSG_ERROR( "No branches set up for writing" );
21 return StatusCode::FAILURE;
22 }
23 ATH_CHECK( m_systematicsService.retrieve() );
24 m_isInitialized = false;
25 return StatusCode::SUCCESS;
26 }
27
28 StatusCode RNtupleTreeMakerAlg::execute(const EventContext& /*ctx*/) {
29 if( ! m_isInitialized ) {
30 m_model = ROOT::RNTupleModel::Create();
31 if ( !m_model ) {
32 ATH_MSG_ERROR( "Failed to create RNTupleModel" );
33 return StatusCode::FAILURE;
34 }
35
37
38 TFile* outputFile = nullptr;
39#ifdef XAOD_STANDALONE
40 if( wk() ) {
41 outputFile = wk()->getOutputFile( m_outputStreamName.value() );
42 } else {
43 ATH_MSG_ERROR( "Worker not available in standalone mode" );
44 return StatusCode::FAILURE;
45 }
46#else
47 // naive implementation for AthAnalysis, I don't see any Ath Svc offer getting the output stream easily
48 m_outputFile.reset( TFile::Open( m_outputStreamName.value().c_str(), "UPDATE" ) );
49 if (m_outputFile && m_outputFile->IsZombie()) m_outputFile.reset();
50 outputFile = m_outputFile.get();
51#endif
52
53 if( !outputFile ) {
54 ATH_MSG_ERROR( "Could not retrieve file for stream: " << m_outputStreamName.value() );
55 return StatusCode::FAILURE;
56 }
57
58 try {
59 m_writer = ROOT::RNTupleWriter::Append( std::move(m_model), m_modelName.value(), *outputFile );
60 } catch( const std::exception& e ) {
61 ATH_MSG_ERROR( "Failed to create RNTupleWriter: " << e.what() );
62 return StatusCode::FAILURE;
63 }
64
65 m_isInitialized = true;
66 }
67
68 ATH_CHECK( m_processorList.process( *(evtStore()) ) );
69
70 if ( m_writer ) {
71 m_writer->Fill();
72 }
73
74 return StatusCode::SUCCESS;
75 }
76
78 m_writer.reset();
79#ifndef XAOD_STANDALONE
80 if (m_outputFile) {
81 m_outputFile->Close();
82 m_outputFile.reset();
83 }
84#endif
85 return StatusCode::SUCCESS;
86 }
87
89 std::unordered_set<std::string> nonContainerSet( m_nonContainers.begin(), m_nonContainers.end() );
90 ATH_CHECK( m_processorList.setupTree( m_branches, std::move(nonContainerSet), *m_systematicsService, *m_model ) );
91 return StatusCode::SUCCESS;
92 }
93
94
95
96 // Removed old ContainerProcessor/ElementProcessor implementations as they are now in FieldHelpers
97} // namespace CP
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x,...)
ServiceHandle< StoreGateSvc > & evtStore()
Gaudi::Property< std::vector< std::string > > m_nonContainers
Gaudi::Property< std::vector< std::string > > m_branches
Gaudi::Property< std::string > m_outputStreamName
StatusCode setupTree()
Function setting up the internal data structures on the first event.
Gaudi::Property< std::string > m_modelName
CP::RNtupleFieldHelpers::ProcessorList m_processorList
std::unique_ptr< ROOT::RNTupleWriter > m_writer
virtual StatusCode initialize() override
Function executed during algorithm initialization.
ServiceHandle< ISystematicsSvc > m_systematicsService
Service handle for systematics.
virtual StatusCode finalize() override
Function executed during algo finalization.
std::unique_ptr< ROOT::RNTupleModel > m_model
std::unique_ptr< TFile > m_outputFile
virtual::StatusCode execute()
execute this algorithm
Select isolated Photons, Electrons and Muons.