The Gaudi Framework  master (48d4dc99)
Loading...
Searching...
No Matches
Gaudi::NTuple::Wrapper< COLUMNS > Class Template Reference

Wrapper around a given NTuple. More...

#include </builds/gaudi/Gaudi/GaudiUtils/include/Gaudi/NTuple/Writer.h>

Collaboration diagram for Gaudi::NTuple::Wrapper< COLUMNS >:

Public Member Functions

void initTree (TFile &file, std::string const &algName, std::string name, std::array< std::string, sizeof...(COLUMNS)> const &branchNames)
 
void fillTree (std::tuple< COLUMNS... > const &data) const
 fills the tree with given data this is safe to be called in the threaded context
 
void writeTree (TFile &file, std::string const &algName)
 

Private Member Functions

template<std::size_t... Is>
void createBranches (std::index_sequence< Is... > const, std::string const &algName, std::array< std::string, sizeof...(COLUMNS)> const &branchNames) const
 

Private Attributes

TTree * m_tree { nullptr }
 
std::string m_dir {}
 
std::vector< Gaudi::details::BranchWrapperm_branchWrappers {}
 
std::mutex m_mtx
 

Detailed Description

template<typename... COLUMNS>
class Gaudi::NTuple::Wrapper< COLUMNS >

Wrapper around a given NTuple.

Provides the functionality for initializing branches in a ROOT TTree, filling the tree, and writing it to a given file.

An algorithm wanting to write NTuple would typically declare one or several Wrappers as its member and call initTree at initialization, fillTree during processing (that one is thread safe) and finally writeTree at finalization initTree and writeTree are not thread safe

Template Parameters
COLUMNSVariadic template parameters representing data types in the NTuple

Definition at line 48 of file Writer.h.

Member Function Documentation

◆ createBranches()

template<typename... COLUMNS>
template<std::size_t... Is>
void Gaudi::NTuple::Wrapper< COLUMNS >::createBranches ( std::index_sequence< Is... > const ,
std::string const & algName,
std::array< std::string, sizeof...(COLUMNS)> const & branchNames ) const
inlineprivate

Definition at line 102 of file Writer.h.

103 {
104 ( ..., m_branchWrappers.emplace_back(
106 branchNames[Is], "", algName ) );
107 }
Wrapper around a given NTuple.
Definition Writer.h:48
std::vector< Gaudi::details::BranchWrapper > m_branchWrappers
Definition Writer.h:97

◆ fillTree()

template<typename... COLUMNS>
void Gaudi::NTuple::Wrapper< COLUMNS >::fillTree ( std::tuple< COLUMNS... > const & data) const
inline

fills the tree with given data this is safe to be called in the threaded context

Definition at line 73 of file Writer.h.

73 {
76 [&]( const auto&... elems ) {
77 size_t index = 0;
78 ( ..., m_branchWrappers[index++].setDataPtr( const_cast<void*>( static_cast<const void*>( &elems ) ) ) );
79 },
80 data );
81 m_tree->Fill();
82 }
std::mutex m_mtx
Definition Writer.h:98

◆ initTree()

template<typename... COLUMNS>
void Gaudi::NTuple::Wrapper< COLUMNS >::initTree ( TFile & file,
std::string const & algName,
std::string name,
std::array< std::string, sizeof...(COLUMNS)> const & branchNames )
inline

Definition at line 51 of file Writer.h.

52 {
53 // handle the case where the name given includes '/', then it's actually a directory and a name
54 auto pos = name.rfind( '/' );
55 if ( pos != std::string::npos ) {
56 m_dir = name.substr( 0, pos );
57 name = name.substr( pos + 1 );
58 }
59 // create directory if needed
60 if ( m_dir != "" ) file.mkdir( m_dir.c_str(), "", true );
61 // Needed for ROOT to write data of the TTree in the right file
62 file.cd( m_dir.c_str() );
63 // create TTree and branches
64 m_tree = std::make_unique<TTree>( name.c_str(), "Tree of Writer Algorithm" ).release();
65 m_branchWrappers.reserve( m_branchWrappers.size() + sizeof...( COLUMNS ) );
67 }
void createBranches(std::index_sequence< Is... > const, std::string const &algName, std::array< std::string, sizeof...(COLUMNS)> const &branchNames) const
Definition Writer.h:102
std::string m_dir
Definition Writer.h:96

◆ writeTree()

template<typename... COLUMNS>
void Gaudi::NTuple::Wrapper< COLUMNS >::writeTree ( TFile & file,
std::string const & algName )
inline

Definition at line 85 of file Writer.h.

85 {
86 // Change to the proper directory in the ROOT file
87 file.cd( m_dir.c_str() );
88 if ( m_tree->Write() <= 0 ) {
89 throw GaudiException( "Failed to write TTree to ROOT file.", algName, StatusCode::FAILURE );
90 }
91 m_tree = nullptr;
92 }

Member Data Documentation

◆ m_branchWrappers

template<typename... COLUMNS>
std::vector<Gaudi::details::BranchWrapper> Gaudi::NTuple::Wrapper< COLUMNS >::m_branchWrappers {}
mutableprivate

Definition at line 97 of file Writer.h.

97{}; // Container for BranchWrapper objects

◆ m_dir

template<typename... COLUMNS>
std::string Gaudi::NTuple::Wrapper< COLUMNS >::m_dir {}
private

Definition at line 96 of file Writer.h.

96{}; // Directory where to create m_tree

◆ m_mtx

template<typename... COLUMNS>
std::mutex Gaudi::NTuple::Wrapper< COLUMNS >::m_mtx
mutableprivate

Definition at line 98 of file Writer.h.

◆ m_tree

template<typename... COLUMNS>
TTree* Gaudi::NTuple::Wrapper< COLUMNS >::m_tree { nullptr }
private

Definition at line 95 of file Writer.h.

95{ nullptr }; // Pointer to the TTree being written to

The documentation for this class was generated from the following file: