g4tools  5.4.0
tree_manip
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_rroot_tree_manip
5 #define tools_rroot_tree_manip
6 
7 #include "file"
8 #include "tree"
9 #include "stl_vector"
10 
11 namespace tools {
12 namespace rroot {
13 
14 inline tree* find_tree(file& a_file,ifac& a_fac,const std::string& a_name,bool a_map_objs = false) {
15  std::ostream& out = a_file.out();
16  //out << "tools::rroot::find_tree : name : " << file << std::endl;
17 
18  key* key = a_file.dir().find_key(a_name);
19  if(!key) {
20  out << "tools::rroot::find_tree :"
21  << " key " << sout(a_name) << " not found."
22  << std::endl;
23  return 0;
24  }
25 
26  unsigned int sz;
27  char* buf = key->get_object_buffer(a_file,sz); //we don't have ownership of buf.
28  if(!buf) {
29  out << "tools::rroot::find_tree :"
30  << " can't get data buffer of "
31  << sout(key->object_name()) << "."
32  << std::endl;
33  return 0;
34  }
35 
36  //out << "tools::rroot::find_tree :"
37  // << " get data buffer size " << sz << "."
38  // << std::endl;
39 
40  if(key->object_class()!=TTree_cls()) {
41  out << "tools::rroot::find_tree :"
42  << " key object class "
43  << sout(key->object_class())
44  << " is not a TTree."
45  << std::endl;
46  return 0;
47  }
48 
49  buffer b(out,a_file.byte_swap(),sz,buf,key->key_length(),false);
50  b.set_map_objs(a_map_objs);
51 
52  tree* _tree = new tree(a_file,a_fac);
53  if(!_tree->stream(b)) {
54  out << "tools::rroot::find_tree :"
55  << " TTree streaming failed"
56  << std::endl;
57  delete _tree;
58  return 0;
59  }
60 
61  return _tree;
62 }
63 
64 inline branch_element* find_be(tree& a_tree,const std::string& a_name){
65  std::ostream& out = a_tree.file().out();
66 
67  branch* _branch = a_tree.find_branch(a_name,true);
68  if(!_branch) {
69  out << "tools::rroot::find_be :"
70  << " branch not found."
71  << std::endl;
72  return 0;
73  }
74 
75  branch_element* be = id_cast<branch,branch_element>(*_branch);
76  if(!be) {
77  out << "tools::rroot::find_be :"
78  << " branch not a branch_element."
79  << std::endl;
80  return 0;
81  }
82 
83  return be;
84 }
85 
86 template <class T>
87 inline stl_vector<T>* find_vec(ifile& a_file,branch_element& a_be,uint64 a_event){
88  std::ostream& out = a_file.out();
89 
90  unsigned int n;
91  if(!a_be.find_entry(a_file,a_event,n)) {
92  out << "tools::rroot::find_vec : branch_element.find_entry() failed." << std::endl;
93  return 0;
94  }
95 
96  iro* o = a_be.object();
97  stl_vector<T>* od = id_cast<iro, stl_vector<T> >(*o);
98  if(!od) {
99  out << "tools::rroot::find_vec : object not a tools::rroot::stl_vector<T>." << std::endl;
100  return 0;
101  }
102 
103  return od; //WARNING : we are not owner.
104 }
105 
106 }}
107 
108 #include "leaf"
109 
110 namespace tools {
111 namespace rroot {
112 
113 template <class LEAF>
114 inline bool find_leaf(tree& a_tree,const std::string& a_name,branch*& a_branch,LEAF*& a_leaf) {
115  //used in MEMPHYS sim.
116  base_leaf* _base_leaf = a_tree.find_leaf(a_name);
117  if(!_base_leaf) {
118  a_tree.out() << "tools::rroot::find_leaf : base_leaf " << sout(a_name) << " not found." << std::endl;
119  a_branch = 0;
120  a_leaf = 0;
121  return false;
122  }
123  a_branch = a_tree.find_leaf_branch(*_base_leaf);
124  if(!a_branch) {
125  a_tree.out() << "tools::rroot::find_leaf : branch of base_leaf " << sout(a_name) << " not found." << std::endl;
126  a_branch = 0;
127  a_leaf = 0;
128  return false;
129  }
130  a_leaf = safe_cast<base_leaf,LEAF>(*_base_leaf);
131  if(!a_leaf) {
132  a_tree.out() << "tools::rroot::find_leaf : base_leaf " << sout(a_name) << " is not a LEAF." << std::endl;
133  a_branch = 0;
134  a_leaf = 0;
135  return false;
136  }
137  return true;
138 }
139 
140 inline branch_element* find_branch_element(tree& a_tree,const std::string& a_name) {
141  branch* _branch = a_tree.find_branch(a_name);
142  if(!_branch) {
143  a_tree.out() << "tools::rroot::find_branch_element : branch " << sout(a_name) << " not found." << std::endl;
144  return 0;
145  }
146  branch_element* be = safe_cast<branch,branch_element>(*_branch);
147  if(!be) {
148  a_tree.out() << "tools::rroot::find_branch_element : branch " << sout(a_name) << " not a branch_element."
149  << " It is a " << sout(_branch->s_cls()) << "."
150  << std::endl;
151  return 0;
152  }
153  return be;
154 }
155 
156 template <class TYPE>
157 inline bool read_leaf(ifile& a_file,branch& a_branch,leaf<TYPE>& a_leaf,uint64 a_event,TYPE& a_value) {
158  unsigned int n;
159  if(!a_branch.find_entry(a_file,a_event,n)) {
160  a_file.out() << "tools::rroot::read_leaf : a_branch.find_entry() failed." << std::endl;
161  a_value = TYPE(0);
162  return false;
163  }
164  return a_leaf.value(0,a_value);
165 }
166 
167 inline bool read_leaf_object(ifile& a_file,branch& a_branch,leaf_object& a_leaf,uint64 a_event) {
168  unsigned int n;
169  if(!a_branch.find_entry(a_file,a_event,n)) {
170  a_file.out() << "tools::rroot::read_leaf_object : a_branch.find_entry() failed." << std::endl;
171  return false;
172  }
173  return true;
174 }
175 
176 template <class TYPE>
177 inline stl_vector<TYPE>* read_std_vec(ifile& a_file,branch_element& a_branch,uint64 a_event) {
178  unsigned int n;
179  if(!a_branch.find_entry(a_file,a_event,n)) {
180  a_file.out() << "tools::rroot::read_std_vec : a_branch.find_entry() failed." << std::endl;
181  return 0;
182  }
183  iro* obj = a_branch.object(); //Not owner.
184  if(!obj) return 0;
185  return id_cast<iro, stl_vector<TYPE> >(*obj);
186 }
187 
188 }}
189 
190 #endif
tools::rroot::file::dir
directory & dir()
Definition: file:187
tools::rroot::find_vec
stl_vector< T > * find_vec(ifile &a_file, branch_element &a_be, uint64 a_event)
Definition: tree_manip:87
tools::uint64
unsigned long long uint64
Definition: typedefs:72
tools::rroot::tree::find_branch
branch * find_branch(const std::string &a_name, bool a_recursive=false) const
Definition: tree:94
tools::rroot::key::object_name
const std::string & object_name() const
Definition: key:182
tools::rroot::branch
Definition: branch:23
tools::rroot::ifile::out
virtual std::ostream & out() const =0
tools::rroot::leaf_object
Definition: leaf:378
tools::rroot::TTree_cls
const std::string & TTree_cls()
Definition: tree:15
tools::rroot::find_be
branch_element * find_be(tree &a_tree, const std::string &a_name)
Definition: tree_manip:64
tools::rroot::branch_element::find_entry
virtual bool find_entry(ifile &a_file, uint64 a_entry, uint32 &a_nbytes)
Definition: branch_element:586
tools::rroot::find_branch_element
branch_element * find_branch_element(tree &a_tree, const std::string &a_name)
Definition: tree_manip:140
tools::rroot::tree::find_leaf_branch
branch * find_leaf_branch(const base_leaf &a_leaf) const
Definition: tree:118
tools::rroot::file::byte_swap
virtual bool byte_swap() const
Definition: file:46
tools::rroot::tree::stream
virtual bool stream(buffer &a_buffer)
Definition: tree:130
tree
tools::rroot::directory::find_key
key * find_key(const std::string &a_name)
Definition: directory:57
tools::rroot::tree
Definition: tree:20
tools::rroot::key::get_object_buffer
char * get_object_buffer(ifile &a_file, uint32 &a_size)
Definition: key:267
tools::rroot::base_leaf
Definition: base_leaf:16
tools::sout
Definition: sout:17
tools::rroot::file
Definition: file:31
stl_vector
tools::rroot::branch_element::object
iro * object()
Definition: branch_element:647
tools::rroot::branch::find_entry
virtual bool find_entry(ifile &a_file, uint64 a_entry, uint32 &a_nbytes)
Definition: branch:537
tools::rroot::leaf::value
bool value(uint32 a_index, T &a_value) const
Definition: leaf:186
tools::rroot::tree::out
std::ostream & out() const
Definition: tree:63
tools::rroot::buffer::set_map_objs
void set_map_objs(bool a_value)
Definition: buffer:123
tools::rroot::read_std_vec
stl_vector< TYPE > * read_std_vec(ifile &a_file, branch_element &a_branch, uint64 a_event)
Definition: tree_manip:177
tools::rroot::ifac
Definition: ifac:19
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::rroot::ifile
Definition: ifile:16
tools::rroot::file::out
virtual std::ostream & out() const
Definition: file:44
tools::rroot::find_leaf
bool find_leaf(tree &a_tree, const std::string &a_name, branch *&a_branch, LEAF *&a_leaf)
Definition: tree_manip:114
file
tools::rroot::buffer
Definition: buffer:43
tools::rroot::tree::find_leaf
base_leaf * find_leaf(const std::string &a_name, bool a_recursive=false) const
Definition: tree:104
tools::rroot::key::key_length
uint32 key_length() const
Definition: key:204
tools::rroot::tree::file
ifile & file()
Definition: tree:65
tools::rroot::read_leaf_object
bool read_leaf_object(ifile &a_file, branch &a_branch, leaf_object &a_leaf, uint64 a_event)
Definition: tree_manip:167
tools::rroot::key
Definition: key:37
tools::rroot::branch::s_cls
virtual const std::string & s_cls() const
Definition: branch:35
tools::rroot::iro
Definition: iro:19
tools::rroot::find_tree
tree * find_tree(file &a_file, ifac &a_fac, const std::string &a_name, bool a_map_objs=false)
Definition: tree_manip:14
tools::rroot::key::object_class
const std::string & object_class() const
Definition: key:184
tools::rroot::leaf
Definition: leaf:57
tools::rroot::stl_vector
Definition: stl_vector:18
tools::rroot::read_leaf
bool read_leaf(ifile &a_file, branch &a_branch, leaf< TYPE > &a_leaf, uint64 a_event, TYPE &a_value)
Definition: tree_manip:157
tools::rroot::branch_element
Definition: branch_element:18
leaf