ATLAS Offline Software
RootBranchAddress.cxx
Go to the documentation of this file.
1 
3 /*
4  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
5 */
6 
7 // RootBranchAddress.cxx
8 // Implementation file for class Athena::RootBranchAddress
9 // Author: S.Binet<binet@cern.ch>
11 
12 // AthenaRootComps includes
13 #include "RootBranchAddress.h"
14 
15 // STL includes
16 
17 // fwk includes
18 #include "GaudiKernel/System.h"
19 
20 // ROOT includes
21 #include "TBranch.h"
22 #include "TTree.h"
23 #include "TLeaf.h"
24 
25 namespace Athena {
26 
28 // Public methods:
30 
31 // Constructors
33 
36  GenericAddress(),
37  m_ptr (),
38  m_branch (NULL)
39 {}
40 
43  GenericAddress(rhs),
44  m_ptr (rhs.m_ptr),
45  m_branch (rhs.m_branch)
46 {}
47 
51 {
52  if (this != &rhs) {
54  m_ptr = rhs.m_ptr;
55  m_branch = rhs.m_branch;
56  }
57  return *this;
58 }
59 
62  const CLID& clid,
63  const std::string& p1,
64  const std::string& p2,
65  unsigned long ip1,
66  unsigned long ip2) :
67  GenericAddress(svc, clid, p1, p2, ip1, ip2),
68  m_ptr (),
69  m_branch (NULL)
70 {
71  // std::cerr << "::RBA::+RBA... (this=" << this << ") br=["
72  // << this->par()[1] << "]\n";
73 }
74 
77 {
78  // std::cerr << "::RBA::~RBA... (this=" << this << ") br=["
79  // << this->par()[1] << "]\n";
80 }
81 
82 
84 // Const methods:
86 
88 // Non-const methods:
90 
91 void
93 {
94  const std::string& br_name = this->par()[1];
95  //const unsigned long* ipar = this->ipar();
96  TTree *tree = this->ttree();
97  Long64_t ientry = tree->GetReadEntry();
98 
99  // std::cerr << "::RBA::setBA(" << this << ") -- tree_nbr=" << tree_nbr << "|"
100  // << " ientry=" << ientry
101  // << "\tname=[" << br_name << "]"
102  // << " addr=" << m_ptr
103  // << "\n";
104  if (m_branch && m_ptr != 0) {
105  m_branch->GetEntry(ientry);
106  return;
107  }
108 
109  // activate branch...
110  tree->SetBranchStatus(br_name.c_str(), 1);
111 
112  // --- inspired from pyroot/src/Pythonize.cxx ---
113  // search for branch first (typical for objects)
114  TBranch *branch = tree->GetBranch(br_name.c_str());
115  if (!branch) {
116  //ATH_MSG_INFO("-- branch [" << br_name << "] =0 -> trying a subbranch");
117  // for benefit of naming of sub-branches,
118  // the actual name may have a trailing '.'
119  branch = tree->GetBranch((br_name+".").c_str());
120  }
121 
122  m_branch = branch;
123  if (branch) {
124  Long64_t bentry = branch->GetReadEntry();
125  if ( bentry != ientry || ientry < 0) {
126  // std::cerr << "::RBA:: -- ientry=" << ientry << " bentry="
127  // << bentry
128  // << "\tname=[" << br_name << "]"
129  // << "\n";
130  /*Long64_t nnn =*/ branch->GetEntry(ientry);
131  bentry = branch->GetReadEntry();
132  // std::cerr << "::RBA:: -- ientry=" << ientry << " bentry="
133  // << bentry
134  // << "\tname=[" << br_name << "]"
135  // << " --> " << nnn
136  // << "\n";
137 
138  }
139 
140  //ATH_MSG_INFO("-- branch [" << br_name << "] = OK");
141  // found a branched object, wrap its address for the object it represents
142  TClass *cls = TClass::GetClass(branch->GetClassName());
143  if (cls) {
144  if (!branch->GetAddress()) {
145  branch->GetEntry(ientry);
146  }
147  if (branch->GetAddress()) {
148  //ATH_MSG_INFO("-- branch [" << br_name << "] cls: [" << cls->GetName() << "]");
149  void* addr = *(char**)branch->GetAddress();
150  RootType t = RootType(*cls->GetTypeInfo());
151  if (t != rflx_type && !t.Id()) {
152  t = rflx_type;
153  }
154  m_type = std::move(t);
155  m_ptr = addr;
156  return;
157 
158  } else {
159  //ATH_MSG_INFO("-- branch [" << br_name << "] cls: [" << cls << "]");
160  }
161  } else {
162  //ATH_MSG_INFO("-- branch [" << br_name << "] cls: [" << cls << "]");
163  }
164 
165  }
166 
167  // if not, try a leaf
168  TLeaf *leaf = tree->GetLeaf(br_name.c_str());
169  //ATH_MSG_INFO("-- leaf [" << br_name << "] ??");
170  if (branch && !leaf) {
171  leaf = branch->GetLeaf(br_name.c_str());
172  if (!leaf) {
173  TObjArray *leaves = branch->GetListOfLeaves();
174  if (leaves->GetSize() && (leaves->First() == leaves->Last())) {
175  // i.e., if unambiguously only this one
176  leaf = (TLeaf*)leaves->At(0);
177  }
178  }
179  }
180 
181  if (leaf) {
182  //ATH_MSG_INFO("-- leaf [" << br_name << "] = OK");
183  // found a leaf, extract value and wrap
184  if ( 1 < leaf->GetLenStatic() || leaf->GetLeafCount() ) {
185  // array types --- FIXME
186  //ATH_MSG_INFO("-- leaf [" << br_name << "] => array...");
187  std::string tname = leaf->GetTypeName();
188  void *addr = (void*)leaf->GetValuePointer();
189  RootType t = RootType(tname+"*");
190  if (t != rflx_type && !t.Id()) {
191  t = rflx_type;
192  }
193  m_type = std::move(t);
194  m_ptr = &addr;
195  return;
196 
197  } else {
198  // value types
199  std::string tname = leaf->GetTypeName();
200  // std::cerr << "-- leaf [" << br_name << "] => value-type("
201  // << tname << ")...\n";
202  void *addr = (void*)leaf->GetValuePointer();
203  RootType t = RootType(tname);
204  if (t != rflx_type && !t.Id()) {
205  t = rflx_type;
206  }
207  m_type = std::move(t);
208  m_ptr = addr;
209  // std::cerr << "-->ptr: [" << m_type.Name() << "]\n";
210  }
211  }
212 
213 }
214 
216 TTree*
218 {
219  return reinterpret_cast<TTree*>(reinterpret_cast<unsigned long*>(this->ipar()[0]));
220 }
221 
223 // Protected methods:
225 
226 } //> end namespace Athena
Athena::RootBranchAddress
Definition: RootBranchAddress.h:33
Athena::RootBranchAddress::m_type
RootType m_type
the buffer for the TBranch
Definition: RootBranchAddress.h:88
Athena::RootBranchAddress::m_branch
TBranch * m_branch
the branch we are connected to.
Definition: RootBranchAddress.h:92
tree
TChain * tree
Definition: tile_monitor.h:30
TRTCalib_cfilter.p1
p1
Definition: TRTCalib_cfilter.py:130
Athena::RootBranchAddress::ttree
TTree * ttree()
the TTree whose branch we proxy
Definition: RootBranchAddress.cxx:217
CaloClusterListBadChannel.cls
cls
Definition: CaloClusterListBadChannel.py:8
columnar::operator=
AccessorTemplate & operator=(AccessorTemplate &&that)
Definition: VectorColumn.h:88
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
Athena::RootBranchAddress::RootBranchAddress
RootBranchAddress()
Default constructor:
Definition: RootBranchAddress.cxx:35
RootType
TTypeAdapter RootType
Definition: RootType.h:211
TRTCalib_cfilter.p2
p2
Definition: TRTCalib_cfilter.py:131
Athena
Some weak symbol referencing magic...
Definition: AthLegacySequence.h:21
Athena::RootBranchAddress::setBranchAddress
void setBranchAddress(const RootType &t)
setup the ROOT TTree internal address for the branch.
Definition: RootBranchAddress.cxx:92
Athena::RootBranchAddress::~RootBranchAddress
virtual ~RootBranchAddress()
Destructor:
Definition: RootBranchAddress.cxx:76
Handler::svc
AthROOTErrorHandlerSvc * svc
Definition: AthROOTErrorHandlerSvc.cxx:10
CLID
uint32_t CLID
The Class ID type.
Definition: Event/xAOD/xAODCore/xAODCore/ClassID_traits.h:47
createCoolChannelIdFile.par
par
Definition: createCoolChannelIdFile.py:28
RTTAlgmain.branch
branch
Definition: RTTAlgmain.py:61
RootBranchAddress.h
Athena::RootBranchAddress::operator=
RootBranchAddress & operator=(const RootBranchAddress &rhs)
Assignment operator:
Definition: RootBranchAddress.cxx:50
Athena::RootBranchAddress::m_ptr
void * m_ptr
Definition: RootBranchAddress.h:89
TScopeAdapter
Definition: RootType.h:119