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

Wrapper around a given NTuple. More...

#include <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::BranchWrapper > m_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 44 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 98 of file Writer.h.

99 {
100 ( ..., m_branchWrappers.emplace_back(
102 branchNames[Is], "", algName ) );
103 }
Wrapper around a given NTuple.
Definition Writer.h:44
std::vector< Gaudi::details::BranchWrapper > m_branchWrappers
Definition Writer.h:93

◆ 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 69 of file Writer.h.

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

◆ 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 47 of file Writer.h.

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

◆ writeTree()

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

Definition at line 81 of file Writer.h.

81 {
82 // Change to the proper directory in the ROOT file
83 file.cd( m_dir.c_str() );
84 if ( m_tree->Write() <= 0 ) {
85 throw GaudiException( "Failed to write TTree to ROOT file.", algName, StatusCode::FAILURE );
86 }
87 m_tree = nullptr;
88 }
GaudiException(std::string Message, std::string Tag, StatusCode Code)
Constructor (1).

Member Data Documentation

◆ m_branchWrappers

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

Definition at line 93 of file Writer.h.

◆ m_dir

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

Definition at line 92 of file Writer.h.

◆ m_mtx

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

Definition at line 94 of file Writer.h.

◆ m_tree

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

Definition at line 91 of file Writer.h.


The documentation for this class was generated from the following file:
  • GaudiUtils/include/Gaudi/NTuple/Writer.h