ATLAS Offline Software
RootBranchAddress.cxx
Go to the documentation of this file.
1 
3 /*
4  Copyright (C) 2002-2017 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 //#include "GaudiKernel/ClassID.h"
20 
21 // ROOT includes
22 #include "TBranch.h"
23 #include "TTree.h"
24 #include "TLeaf.h"
25 
26 namespace Athena {
27 
29 // Public methods:
31 
32 // Constructors
34 
37  GenericAddress(),
38  m_ptr (),
39  m_branch (NULL)
40 {}
41 
44  GenericAddress(rhs),
45  m_ptr (rhs.m_ptr),
46  m_branch (rhs.m_branch)
47 {}
48 
52 {
53  if (this != &rhs) {
54  GenericAddress::operator=(rhs);
55  m_ptr = rhs.m_ptr;
56  m_branch = rhs.m_branch;
57  }
58  return *this;
59 }
60 
63  const CLID& clid,
64  const std::string& p1,
65  const std::string& p2,
66  unsigned long ip1,
67  unsigned long ip2) :
68  GenericAddress(svc, clid, p1, p2, ip1, ip2),
69  m_ptr (),
70  m_branch (NULL)
71 {
72  // std::cerr << "::RBA::+RBA... (this=" << this << ") br=["
73  // << this->par()[1] << "]\n";
74 }
75 
78 {
79  // std::cerr << "::RBA::~RBA... (this=" << this << ") br=["
80  // << this->par()[1] << "]\n";
81 }
82 
83 
85 // Const methods:
87 
89 // Non-const methods:
91 
92 void
94 {
95  const std::string& br_name = this->par()[1];
96  //const unsigned long* ipar = this->ipar();
97  TTree *tree = this->ttree();
98  Long64_t ientry = tree->GetReadEntry();
99 
100  // std::cerr << "::RBA::setBA(" << this << ") -- tree_nbr=" << tree_nbr << "|"
101  // << " ientry=" << ientry
102  // << "\tname=[" << br_name << "]"
103  // << " addr=" << m_ptr
104  // << "\n";
105  if (m_branch && m_ptr != 0) {
106  m_branch->GetEntry(ientry);
107  return;
108  }
109 
110  // activate branch...
111  tree->SetBranchStatus(br_name.c_str(), 1);
112 
113  // --- inspired from pyroot/src/Pythonize.cxx ---
114  // search for branch first (typical for objects)
115  TBranch *branch = tree->GetBranch(br_name.c_str());
116  if (!branch) {
117  //ATH_MSG_INFO("-- branch [" << br_name << "] =0 -> trying a subbranch");
118  // for benefit of naming of sub-branches,
119  // the actual name may have a trailing '.'
120  branch = tree->GetBranch((br_name+".").c_str());
121  }
122 
123  m_branch = branch;
124  if (branch) {
125  Long64_t bentry = branch->GetReadEntry();
126  if ( bentry != ientry || ientry < 0) {
127  // std::cerr << "::RBA:: -- ientry=" << ientry << " bentry="
128  // << bentry
129  // << "\tname=[" << br_name << "]"
130  // << "\n";
131  /*Long64_t nnn =*/ branch->GetEntry(ientry);
132  bentry = branch->GetReadEntry();
133  // std::cerr << "::RBA:: -- ientry=" << ientry << " bentry="
134  // << bentry
135  // << "\tname=[" << br_name << "]"
136  // << " --> " << nnn
137  // << "\n";
138 
139  }
140 
141  //ATH_MSG_INFO("-- branch [" << br_name << "] = OK");
142  // found a branched object, wrap its address for the object it represents
143  TClass *cls = TClass::GetClass(branch->GetClassName());
144  if (cls) {
145  if (!branch->GetAddress()) {
146  branch->GetEntry(ientry);
147  }
148  if (branch->GetAddress()) {
149  //ATH_MSG_INFO("-- branch [" << br_name << "] cls: [" << cls->GetName() << "]");
150  void* addr = *(char**)branch->GetAddress();
151  RootType t = RootType(*cls->GetTypeInfo());
152  if (t != rflx_type && !t.Id()) {
153  t = rflx_type;
154  }
155  m_type = t;
156  m_ptr = addr;
157  return;
158 
159  } else {
160  //ATH_MSG_INFO("-- branch [" << br_name << "] cls: [" << cls << "]");
161  }
162  } else {
163  //ATH_MSG_INFO("-- branch [" << br_name << "] cls: [" << cls << "]");
164  }
165 
166  }
167 
168  // if not, try a leaf
169  TLeaf *leaf = tree->GetLeaf(br_name.c_str());
170  //ATH_MSG_INFO("-- leaf [" << br_name << "] ??");
171  if (branch && !leaf) {
172  leaf = branch->GetLeaf(br_name.c_str());
173  if (!leaf) {
174  TObjArray *leaves = branch->GetListOfLeaves();
175  if (leaves->GetSize() && (leaves->First() == leaves->Last())) {
176  // i.e., if unambiguously only this one
177  leaf = (TLeaf*)leaves->At(0);
178  }
179  }
180  }
181 
182  if (leaf) {
183  //ATH_MSG_INFO("-- leaf [" << br_name << "] = OK");
184  // found a leaf, extract value and wrap
185  if ( 1 < leaf->GetLenStatic() || leaf->GetLeafCount() ) {
186  // array types --- FIXME
187  //ATH_MSG_INFO("-- leaf [" << br_name << "] => array...");
188  std::string tname = leaf->GetTypeName();
189  void *addr = (void*)leaf->GetValuePointer();
190  RootType t = RootType(tname+"*");
191  if (t != rflx_type && !t.Id()) {
192  t = rflx_type;
193  }
194  m_type = t;
195  m_ptr = &addr;
196  return;
197 
198  } else {
199  // value types
200  std::string tname = leaf->GetTypeName();
201  // std::cerr << "-- leaf [" << br_name << "] => value-type("
202  // << tname << ")...\n";
203  void *addr = (void*)leaf->GetValuePointer();
204  RootType t = RootType(tname);
205  if (t != rflx_type && !t.Id()) {
206  t = rflx_type;
207  }
208  m_type = t;
209  m_ptr = addr;
210  // std::cerr << "-->ptr: [" << m_type.Name() << "]\n";
211  }
212  }
213 
214 }
215 
217 TTree*
219 {
220  return reinterpret_cast<TTree*>(reinterpret_cast<unsigned long*>(this->ipar()[0]));
221 }
222 
224 // Protected methods:
226 
227 } //> 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:218
CaloClusterListBadChannel.cls
cls
Definition: CaloClusterListBadChannel.py:8
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
Athena::RootBranchAddress::RootBranchAddress
RootBranchAddress()
Default constructor:
Definition: RootBranchAddress.cxx:36
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:93
Athena::RootBranchAddress::~RootBranchAddress
virtual ~RootBranchAddress()
Destructor:
Definition: RootBranchAddress.cxx:77
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:29
RTTAlgmain.branch
branch
Definition: RTTAlgmain.py:61
RootBranchAddress.h
Athena::RootBranchAddress::operator=
RootBranchAddress & operator=(const RootBranchAddress &rhs)
Assignment operator:
Definition: RootBranchAddress.cxx:51
Athena::RootBranchAddress::m_ptr
void * m_ptr
Definition: RootBranchAddress.h:89
TScopeAdapter
Definition: RootType.h:119