The Gaudi Framework  v38r2 (5b3c9e4d)
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  auto getLeafListForType( const std::string_view& typeName ) {
28  auto it = typeMap.find( typeName );
29  return ( it != typeMap.end() ) ? it->second : std::string{};
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,
37  const Gaudi::Algorithm& algRef )
38  : m_className( className ), m_branchName( branchName ), m_location( location ), m_algRef( algRef ) {
39  auto leafListTag = getLeafListForType( m_className );
40  if ( !leafListTag.empty() ) {
41  // Create a branch for fundamental types using the leaflist
42  m_branch = tree->Branch( m_branchName.c_str(), &m_dataBuffer,
43  ( fmt::format( "{}/{}", m_className, leafListTag ) ).c_str() );
44  setBranchAddress = []( gsl::not_null<TBranch*> br, void** wrappedDataPtr ) { br->SetAddress( *wrappedDataPtr ); };
45 
46  } else if ( TClass::GetClass( m_className.c_str() ) ) {
47  // Create a branch for object types using the classname string
48  m_branch = tree->Branch( m_branchName.c_str(), m_className.c_str(), &m_dataBuffer );
49  setBranchAddress = []( gsl::not_null<TBranch*> br, void** wrappedDataPtr ) { br->SetAddress( wrappedDataPtr ); };
50 
51  } else {
52  throw GaudiException( fmt::format( "Cannot create branch {} for unknown class: {}. Provide a dictionary please.",
55  }
56 
57  if ( !m_branch ) {
58  throw GaudiException( fmt::format( "Failed to create branch {} for type {}.", m_branchName, m_className ),
60  }
61  }
62 
63  // Set the data pointer for the branch from a given address
64  // Used by Gaudi::NTuple::Writer
65  void BranchWrapper::setDataPtr( void* dataPtr ) {
66  m_dataBuffer = dataPtr;
68  }
69 
70  // Set the data for the branch from a given DataObject
71  // Used by Gaudi::NTuple::GenericWriter
72  void BranchWrapper::setBranchData( const gsl::not_null<DataObject*> pObj ) {
73  baseWrapper = dynamic_cast<AnyDataWrapperBase*>( pObj.get() );
74  if ( !baseWrapper ) {
75  throw GaudiException( "Failed to cast DataObject to AnyDataWrapperBase type.", m_algRef.name(),
77  }
78 
81  }
82 
84 
86 } // namespace Gaudi::details
Gaudi::details::BranchWrapper::setDataPtr
void setDataPtr(void *dataPtr)
Definition: BranchWrapper.cpp:65
std::string
STL class.
AnyDataWrapperBase::voidp
virtual void * voidp()=0
Gaudi::details::BranchWrapper::m_location
std::string m_location
Definition: BranchWrapper.h:33
GaudiAlg.HistoUtils.location
location
Definition: HistoUtils.py:964
Gaudi::Algorithm::name
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:528
std::map::find
T find(T... args)
GaudiException
Definition: GaudiException.h:31
Gaudi::details::BranchWrapper::baseWrapper
AnyDataWrapperBase * baseWrapper
Definition: BranchWrapper.h:29
Gaudi::details::BranchWrapper::getClassName
std::string getClassName() const
Definition: BranchWrapper.cpp:85
Gaudi::details::BranchWrapper::m_branch
TBranch * m_branch
Definition: BranchWrapper.h:30
Gaudi::details::BranchWrapper::setBranchData
void setBranchData(const gsl::not_null< DataObject * > pObj)
Definition: BranchWrapper.cpp:72
Gaudi::details::BranchWrapper::getLocation
std::string getLocation() const
Definition: BranchWrapper.cpp:83
Gaudi::details::BranchWrapper::m_dataBuffer
void * m_dataBuffer
Definition: BranchWrapper.h:28
AnyDataWrapperBase
Definition: AnyDataWrapper.h:29
Gaudi::details::BranchWrapper::m_branchName
std::string m_branchName
Definition: BranchWrapper.h:32
Gaudi::details::BranchWrapper::setBranchAddress
void(* setBranchAddress)(gsl::not_null< TBranch * >, void **)
Definition: BranchWrapper.h:35
Gaudi::details::BranchWrapper::m_className
std::string m_className
Definition: BranchWrapper.h:31
Gaudi::Algorithm
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:90
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::m_algRef
const Gaudi::Algorithm & m_algRef
Definition: BranchWrapper.h:34
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 Gaudi::Algorithm &algRef)
Definition: BranchWrapper.cpp:35
std::map::end
T end(T... args)
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
Gaudi::details
Definition: Algorithm.h:16