The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
BranchWrapper.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3* *
4* This software is distributed under the terms of the Apache version 2 licence, *
5* copied verbatim in the file "LICENSE". *
6* *
7* In applying this licence, CERN does not waive the privileges and immunities *
8* granted to it by virtue of its status as an Intergovernmental Organization *
9* or submit itself to any jurisdiction. *
10\***********************************************************************************/
11#include <Gaudi/Algorithm.h>
13#include <TTree.h>
14#include <fmt/format.h>
15#include <map>
16#include <string>
17
18namespace {
19 // Mapping from C++ type names to ROOT branch types for branch creation
20 const std::map<std::string, std::string, std::less<>> typeMap = {
21 { "char", "B" }, { "unsigned char", "b" }, { "short", "S" }, { "unsigned short", "s" },
22 { "int", "I" }, { "unsigned int", "i" }, { "float", "F" }, { "double", "D" },
23 { "long long", "L" }, { "unsigned long long", "l" }, { "long", "G" }, { "unsigned long", "g" },
24 { "bool", "O" } };
25
26 // Helper function to find the ROOT branch type corresponding to a C++ type
27 std::optional<std::string> getLeafListForType( const std::string_view& typeName ) {
28 auto it = typeMap.find( typeName );
29 return ( it != typeMap.end() ) ? std::optional<std::string>{ it->second } : std::nullopt;
30 }
31} // namespace
32
33namespace Gaudi::details {
34
35 BranchWrapper::BranchWrapper( const gsl::not_null<TTree*> tree, const std::string& className,
36 const std::string& branchName, const std::string& location, const std::string& algName )
37 : m_className( className ), m_branchName( branchName ), m_location( location ), m_algName( algName ) {
38 auto leafListTag = getLeafListForType( m_className );
39 if ( leafListTag ) {
40 // Create a branch for fundamental types using the leaflist
41 m_branch = tree->Branch( m_branchName.c_str(), &m_dataBuffer,
42 ( fmt::format( "{}/{}", m_className, leafListTag.value() ) ).c_str() );
43 setBranchAddress = []( gsl::not_null<TBranch*> br, const void** wrappedDataPtr ) {
44 br->SetAddress( const_cast<void*>( *wrappedDataPtr ) );
45 };
46
47 } else if ( TClass::GetClass( m_className.c_str() ) ) {
48 // Create a branch for object types using the classname string
49 m_branch = tree->Branch( m_branchName.c_str(), m_className.c_str(), &m_dataBuffer );
50 setBranchAddress = []( gsl::not_null<TBranch*> br, const void** wrappedDataPtr ) {
51 br->SetAddress( wrappedDataPtr );
52 };
53
54 } else {
55 throw GaudiException( fmt::format( "Cannot create branch {} for unknown class: {}. Provide a dictionary please.",
58 }
59
60 if ( !m_branch ) {
61 throw GaudiException( fmt::format( "Failed to create branch {} for type {}.", m_branchName, m_className ),
63 }
64 }
65
66 // Set the data pointer for the branch from a given address
67 // Used by Gaudi::NTuple::Writer
68 void BranchWrapper::setDataPtr( void const* dataPtr ) {
69 m_dataBuffer = dataPtr;
71 }
72
73 // Set the data for the branch from a given DataObject
74 // Used by Gaudi::NTuple::GenericWriter
75 void BranchWrapper::setBranchData( const gsl::not_null<DataObject*> pObj ) {
76 auto baseWrapper = dynamic_cast<AnyDataWrapperBase*>( pObj.get() );
77 m_dataBuffer = baseWrapper ? baseWrapper->payload() : pObj.get();
79 }
80
81 const std::string& BranchWrapper::getLocation() const { return m_location; }
82
83 const std::string& BranchWrapper::getClassName() const { return m_className; }
84} // namespace Gaudi::details
Define general base for Gaudi exception.
constexpr static const auto FAILURE
Definition StatusCode.h:100
BranchWrapper(const gsl::not_null< TTree * > tree, const std::string &className, const std::string &branchName, const std::string &location, const std::string &algName)
void(* setBranchAddress)(gsl::not_null< TBranch * >, const void **)
const std::string & getClassName() const
void setDataPtr(void const *dataPtr)
const std::string & getLocation() const
void setBranchData(const gsl::not_null< DataObject * > pObj)