The Gaudi Framework  master (37c0b60a)
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 
18 namespace {
19  // Mapping from C++ type names to ROOT branch types for branch creation
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 
33 namespace 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 
82 
84 } // namespace Gaudi::details
std::string
STL class.
Gaudi::details::BranchWrapper::m_algName
std::string m_algName
Definition: BranchWrapper.h:33
Gaudi::details::BranchWrapper::m_location
std::string m_location
Definition: BranchWrapper.h:32
Gaudi::details::BranchWrapper::getClassName
const std::string & getClassName() const
Definition: BranchWrapper.cpp:83
std::map::find
T find(T... args)
GaudiException
Definition: GaudiException.h:31
Gaudi::details::BranchWrapper::m_branch
TBranch * m_branch
Definition: BranchWrapper.h:29
Gaudi::details::BranchWrapper::setBranchData
void setBranchData(const gsl::not_null< DataObject * > pObj)
Definition: BranchWrapper.cpp:75
AnyDataWrapperBase
Definition: AnyDataWrapper.h:30
Gaudi::details::BranchWrapper::m_branchName
std::string m_branchName
Definition: BranchWrapper.h:31
Gaudi::details::BranchWrapper::m_className
std::string m_className
Definition: BranchWrapper.h:30
Gaudi::details::BranchWrapper::getLocation
const std::string & getLocation() const
Definition: BranchWrapper.cpp:81
Gaudi::details::BranchWrapper::m_dataBuffer
void const * m_dataBuffer
Definition: BranchWrapper.h:28
std::string::c_str
T c_str(T... args)
Algorithm.h
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
std::map
STL class.
Gaudi::details::BranchWrapper::setDataPtr
void setDataPtr(void const *dataPtr)
Definition: BranchWrapper.cpp:68
GaudiDict::typeName
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:31
BranchWrapper.h
Gaudi::details::BranchWrapper::BranchWrapper
BranchWrapper(const gsl::not_null< TTree * > tree, const std::string &className, const std::string &branchName, const std::string &location, const std::string &algName)
Definition: BranchWrapper.cpp:35
std::map::end
T end(T... args)
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
Gaudi::details::BranchWrapper::setBranchAddress
void(* setBranchAddress)(gsl::not_null< TBranch * >, const void **)
Definition: BranchWrapper.h:34
Gaudi::details
Definition: Algorithm.h:16