The Gaudi Framework  master (41389df7)
Loading...
Searching...
No Matches
Gaudi::details::BranchWrapper Struct Reference

Encapsulates a branch within a ROOT TTree, managing the data and interaction with the TTree. More...

#include </builds/gaudi/Gaudi/GaudiUtils/include/Gaudi/details/BranchWrapper.h>

Collaboration diagram for Gaudi::details::BranchWrapper:

Public Member Functions

 BranchWrapper (const gsl::not_null< TTree * > tree, const std::string &className, const std::string &branchName, const std::string &location, const std::string &algName, unsigned int bufferSize=32000, unsigned int splitLevel=99)
 
void setDataPtr (void const *dataPtr)
 
void setBranchData (const gsl::not_null< DataObject * > pObj)
 
const std::string & getLocation () const
 
const std::string & getClassName () const
 
void setBufferSize (unsigned int size)
 
unsigned int computeOptimalBufferSize (unsigned int minBufferSize, unsigned int maxBufferSize, unsigned int approxEventsPerBasket, unsigned int splitLevel)
 compute optimal buffer size to fit given number of element per basket, respecting given min and max
 
void padEntries ()
 pad the number of item in the branch to the one of the Tree in which it leaves by adding empty entries
 

Private Attributes

void const * m_dataBuffer = nullptr
 
TBranch * m_branch = nullptr
 
std::string m_className
 
std::string m_location
 
void(* setBranchAddress )(gsl::not_null< TBranch * >, const void **)
 

Detailed Description

Encapsulates a branch within a ROOT TTree, managing the data and interaction with the TTree.

Definition at line 25 of file BranchWrapper.h.

Constructor & Destructor Documentation

◆ BranchWrapper()

Gaudi::details::BranchWrapper::BranchWrapper ( const gsl::not_null< TTree * > tree,
const std::string & className,
const std::string & branchName,
const std::string & location,
const std::string & algName,
unsigned int bufferSize = 32000,
unsigned int splitLevel = 99 )

Definition at line 37 of file BranchWrapper.cpp.

40 : m_className( className ), m_location( location ) {
41 auto leafListTag = getLeafListForType( m_className );
42 if ( leafListTag ) {
43 // Create a branch for fundamental types using the leaflist
44 m_branch = tree->Branch( branchName.c_str(), &m_dataBuffer,
45 ( std::format( "{}/{}", m_className, leafListTag.value() ) ).c_str(), bufferSize );
46 setBranchAddress = []( gsl::not_null<TBranch*> br, const void** wrappedDataPtr ) {
47 br->SetAddress( const_cast<void*>( *wrappedDataPtr ) );
48 };
49
50 } else if ( TClass::GetClass( m_className.c_str() ) ) {
51 // Create a branch for object types using the classname string
52 m_branch = tree->Branch( branchName.c_str(), m_className.c_str(), &m_dataBuffer, bufferSize, splitLevel );
53 setBranchAddress = []( gsl::not_null<TBranch*> br, const void** wrappedDataPtr ) {
54 br->SetAddress( wrappedDataPtr );
55 };
56
57 } else {
58 throw GaudiException( std::format( "Cannot create branch {} for unknown class: {}. Provide a dictionary please.",
59 branchName, m_className ),
60 algName, StatusCode::FAILURE );
61 }
62
63 if ( !m_branch ) {
64 throw GaudiException( std::format( "Failed to create branch {} for type {}.", branchName, m_className ), algName,
66 }
67 }
constexpr static const auto FAILURE
Definition StatusCode.h:100
void(* setBranchAddress)(gsl::not_null< TBranch * >, const void **)

Member Function Documentation

◆ computeOptimalBufferSize()

unsigned int Gaudi::details::BranchWrapper::computeOptimalBufferSize ( unsigned int minBufferSize,
unsigned int maxBufferSize,
unsigned int approxEventsPerBasket,
unsigned int splitLevel )

compute optimal buffer size to fit given number of element per basket, respecting given min and max

Definition at line 84 of file BranchWrapper.cpp.

85 {
86 // simply try to write one item in a dummey branch of a dummy tree im memory and measure size
87 auto dummy_file = std::make_unique<TMemFile>( "dummy.root", "CREATE" );
88 auto dummy_tree = std::make_unique<TTree>( "DummyTree", "DummyTree", splitLevel, dummy_file->GetDirectory( "/" ) );
89 auto leafListTag = getLeafListForType( m_className );
90 TBranch* dummy_branch{ nullptr };
91 if ( leafListTag ) {
92 // Create a branch for fundamental types using the leaflist
93 dummy_branch =
94 dummy_tree->Branch( "DummyBranch", &m_dataBuffer,
95 ( fmt::format( "{}/{}", m_className, leafListTag.value() ) ).c_str(), minBufferSize );
96 } else if ( TClass::GetClass( m_className.c_str() ) ) {
97 // Create a branch for object types using the classname string
98 dummy_branch = dummy_tree->Branch( "DummyBranch", m_className.c_str(), &m_dataBuffer, minBufferSize, splitLevel );
99 } // no else as unknown className would have raised an exception at constructor level
100 int nWritten = dummy_branch->Fill();
101 if ( nWritten >= 0 ) {
102 unsigned int newBasketSize = nWritten * approxEventsPerBasket;
103 // Ensure that newBasketSize doesn't wrap around
104 if ( std::numeric_limits<Int_t>::max() / approxEventsPerBasket < (unsigned int)nWritten ) {
105 newBasketSize = std::numeric_limits<Int_t>::max();
106 }
107 return std::min( maxBufferSize, std::max( minBufferSize, newBasketSize ) );
108 }
109 return minBufferSize;
110 }

◆ getClassName()

const std::string & Gaudi::details::BranchWrapper::getClassName ( ) const
inline

Definition at line 44 of file BranchWrapper.h.

44{ return m_className; }

◆ getLocation()

const std::string & Gaudi::details::BranchWrapper::getLocation ( ) const
inline

Definition at line 43 of file BranchWrapper.h.

43{ return m_location; }

◆ padEntries()

void Gaudi::details::BranchWrapper::padEntries ( )

pad the number of item in the branch to the one of the Tree in which it leaves by adding empty entries

Definition at line 112 of file BranchWrapper.cpp.

112 {
113 if ( !m_branch ) return;
114 auto nEvents = m_branch->GetTree()->GetEntries();
115 auto nEntries = m_branch->GetEntries();
116 if ( nEntries < nEvents ) {
117 m_branch->SetAddress( nullptr );
118 for ( auto i = nEntries; i < nEvents; i++ ) { m_branch->Fill(); }
119 }
120 }

◆ setBranchData()

void Gaudi::details::BranchWrapper::setBranchData ( const gsl::not_null< DataObject * > pObj)

Definition at line 78 of file BranchWrapper.cpp.

78 {
79 auto baseWrapper = dynamic_cast<AnyDataWrapperBase*>( pObj.get() );
80 m_dataBuffer = baseWrapper ? baseWrapper->payload() : pObj.get();
82 }

◆ setBufferSize()

void Gaudi::details::BranchWrapper::setBufferSize ( unsigned int size)
inline

Definition at line 45 of file BranchWrapper.h.

45{ m_branch->SetBasketSize( size ); }

◆ setDataPtr()

void Gaudi::details::BranchWrapper::setDataPtr ( void const * dataPtr)

Definition at line 71 of file BranchWrapper.cpp.

71 {
72 m_dataBuffer = dataPtr;
74 }

Member Data Documentation

◆ m_branch

TBranch* Gaudi::details::BranchWrapper::m_branch = nullptr
private

Definition at line 29 of file BranchWrapper.h.

◆ m_className

std::string Gaudi::details::BranchWrapper::m_className
private

Definition at line 30 of file BranchWrapper.h.

◆ m_dataBuffer

void const* Gaudi::details::BranchWrapper::m_dataBuffer = nullptr
private

Definition at line 28 of file BranchWrapper.h.

◆ m_location

std::string Gaudi::details::BranchWrapper::m_location
private

Definition at line 31 of file BranchWrapper.h.

◆ setBranchAddress

void(* Gaudi::details::BranchWrapper::setBranchAddress) (gsl::not_null< TBranch * >, const void **)
private

Definition at line 32 of file BranchWrapper.h.


The documentation for this struct was generated from the following files: