Jpp  master_rocky-37-gf0c5bc59d
the software that should make you happy
Public Member Functions | Protected Attributes | List of all members
JROOT::JTreeBranchAddresses< T > Class Template Reference

Class responsible to setup TTree branch addresses for reading objects of type T. More...

#include <JTreeBranchAddresses.hh>

Inheritance diagram for JROOT::JTreeBranchAddresses< T >:
JROOT::JRootAddress< T > JROOT::JTreeReader< T > JROOT::JTreeReaderObjectIterator< T > JROOT::JTreeReaderAccessibleObjectIterator< T > JROOT::JRootFileReader< T, true >

Public Member Functions

void setBranchAddress (TTree &tree)
 
void release ()
 Release memory. More...
 
T * getAddress () const
 Get address. More...
 

Protected Attributes

T * address
 

Detailed Description

template<typename T>
class JROOT::JTreeBranchAddresses< T >

Class responsible to setup TTree branch addresses for reading objects of type T.

Definition at line 39 of file JTreeBranchAddresses.hh.

Member Function Documentation

◆ setBranchAddress()

template<typename T >
void JROOT::JTreeBranchAddresses< T >::setBranchAddress ( TTree &  tree)
inline

Definition at line 53 of file JTreeBranchAddresses.hh.

54  {
55  TObjArray* branches = tree.GetListOfBranches();
56  // single branch
57  if (branches->GetEntries() == 1) {
58  tree.SetBranchAddress(branches->First()->GetName(), &this->address);
59  return;
60  }
61 
62  // if more than one branch, check those are the data members of T
63  TClass* t_class = TClass::GetClass<T>();
64  if (t_class == nullptr) {
65  THROW(JException, "Could not get class " << typeid(T).name());
66  }
67 
68  auto branch_names = to_vector(tree.GetListOfBranches());
69  auto all_member_names = to_vector(t_class->GetListOfRealData());
70 
71  std::vector<std::string> member_names;
72 
73  // remove TObject specific members
74  std::copy_if(all_member_names.begin(), all_member_names.end(), std::back_inserter(member_names),
75  [](const std::string& member_name)
76  { return !JRootClass::is_tobject_member(member_name.c_str()); });
77 
78  std::sort(branch_names.begin(), branch_names.end());
79  std::sort(member_names.begin(), member_names.end());
80 
81  // all members must have a corresponding branch
82  // (but more branches are allowed, we just don't handle them here)
83  if (std::includes(branch_names.begin(), branch_names.end(), member_names.begin(),
84  member_names.end())) {
85 
86  this->address = new T;
87 
88  auto* base = reinterpret_cast<uint8_t*>(this->address);
89 
90  for (const auto& member_name : member_names) {
91  auto* member = static_cast<TDataMember*>(t_class->GetDataMember(member_name.c_str()));
92  tree.SetBranchAddress(member_name.c_str(),
93  reinterpret_cast<void*>(base + member->GetOffset()));
94  }
95  return;
96  }
97 
98  // if we cannot configure the tree, try to give some relevant information
99  // to the user who will have to deal with the exception...
100  THROW(JException, "Members of type " << typeid(T).name() << "\n"
101  << JEEP::JEEPZ() << member_names << "\nBranches in tree\n"
102  << JEEP::JEEPZ() << branch_names);
103  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
std::vector< std::string > to_vector(TCollection *col)
Auxiliary data structure for streaming of STL containers.

◆ release()

template<class T >
void JROOT::JRootAddress< T >::release ( )
inlineinherited

Release memory.

Definition at line 46 of file JRootAddress.hh.

47  {
48  if (address != NULL) {
49 
50  delete address;
51 
52  address = NULL;
53  }
54  }

◆ getAddress()

template<class T >
T* JROOT::JRootAddress< T >::getAddress ( ) const
inlineinherited

Get address.

Returns
address

Definition at line 62 of file JRootAddress.hh.

63  {
64  return address;
65  }

Member Data Documentation

◆ address

template<class T >
T* JROOT::JRootAddress< T >::address
protectedinherited

Definition at line 68 of file JRootAddress.hh.


The documentation for this class was generated from the following file: