The Gaudi Framework  v38r3 (c3fc9673)
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, const void** wrappedDataPtr ) {
45  br->SetAddress( const_cast<void*>( *wrappedDataPtr ) );
46  };
47 
48  } else if ( TClass::GetClass( m_className.c_str() ) ) {
49  // Create a branch for object types using the classname string
50  m_branch = tree->Branch( m_branchName.c_str(), m_className.c_str(), &m_dataBuffer );
51  setBranchAddress = []( gsl::not_null<TBranch*> br, const void** wrappedDataPtr ) {
52  br->SetAddress( wrappedDataPtr );
53  };
54 
55  } else {
56  throw GaudiException( fmt::format( "Cannot create branch {} for unknown class: {}. Provide a dictionary please.",
59  }
60 
61  if ( !m_branch ) {
62  throw GaudiException( fmt::format( "Failed to create branch {} for type {}.", m_branchName, m_className ),
64  }
65  }
66 
67  // Set the data pointer for the branch from a given address
68  // Used by Gaudi::NTuple::Writer
69  void BranchWrapper::setDataPtr( void const* dataPtr ) {
70  m_dataBuffer = dataPtr;
72  }
73 
74  // Set the data for the branch from a given DataObject
75  // Used by Gaudi::NTuple::GenericWriter
76  void BranchWrapper::setBranchData( const gsl::not_null<DataObject*> pObj ) {
77  baseWrapper = dynamic_cast<AnyDataWrapperBase*>( pObj.get() );
78  if ( !baseWrapper ) {
79  throw GaudiException( "Failed to cast DataObject to AnyDataWrapperBase type.", m_algRef.name(),
81  }
82 
85  }
86 
88 
90 } // namespace Gaudi::details
std::string
STL class.
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:89
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:76
Gaudi::details::BranchWrapper::getLocation
std::string getLocation() const
Definition: BranchWrapper.cpp:87
AnyDataWrapperBase
Definition: AnyDataWrapper.h:30
Gaudi::details::BranchWrapper::m_branchName
std::string m_branchName
Definition: BranchWrapper.h:32
Gaudi::details::BranchWrapper::m_className
std::string m_className
Definition: BranchWrapper.h:31
Gaudi::details::BranchWrapper::m_dataBuffer
void const * m_dataBuffer
Definition: BranchWrapper.h:28
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.
AnyDataWrapperBase::payload
virtual Ptr payload() const =0
Gaudi::details::BranchWrapper::setDataPtr
void setDataPtr(void const *dataPtr)
Definition: BranchWrapper.cpp:69
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::BranchWrapper::setBranchAddress
void(* setBranchAddress)(gsl::not_null< TBranch * >, const void **)
Definition: BranchWrapper.h:35
Gaudi::details
Definition: Algorithm.h:16