g4tools  5.4.0
tree
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_wroot_tree
5 #define tools_wroot_tree
6 
7 #include "itree"
8 #include "iobject"
9 #include "idir"
10 
11 #include "branch_element"
12 #include "branch_object"
13 
14 namespace tools {
15 namespace wroot {
16 
17 class tree : public virtual iobject, public virtual itree {
18 #ifdef TOOLS_MEM
19  static const std::string& s_class() {
20  static const std::string s_v("tools::wroot::tree");
21  return s_v;
22  }
23 #endif
24 public: //iobject
25  virtual const std::string& name() const {return m_name;}
26  virtual const std::string& title() const {return m_title;}
27  virtual const std::string& store_class_name() const {
28  static const std::string s_v("TTree");
29  return s_v;
30  }
31  virtual bool stream(buffer& a_buffer) const {
32  unsigned int c;
33  if(!a_buffer.write_version(5,c)) return false;
34 
35  if(!Named_stream(a_buffer,m_name,m_title)) return false;
36 
37  // Beurk.
38  if(!AttLine_stream(a_buffer)) return false;
39  if(!AttFill_stream(a_buffer)) return false;
40  if(!AttMarker_stream(a_buffer)) return false;
41 
42  double fEntries = (double)m_entries;
43  if(!a_buffer.write(fEntries)) return false;
44 
45  double fTotBytes = (double)m_tot_bytes;
46  double fZipBytes = (double)m_zip_bytes;
47  if(!a_buffer.write(fTotBytes)) return false;
48  if(!a_buffer.write(fZipBytes)) return false;
49  if(!a_buffer.write((double)0)) return false; //fSavedBytes
50  if(!a_buffer.write((int)0)) return false; //fTimerInterval
51  if(!a_buffer.write((int)25)) return false; //fScanField (25)
52  if(!a_buffer.write((int)0)) return false; //fUpdate
53  if(!a_buffer.write((int)1000000000)) return false; //fMaxEntryLoop
54  int fMaxVirtualSize = 0;
55  int fAutoSave = 100000000;
56  if(!a_buffer.write(fMaxVirtualSize)) return false;
57  if(!a_buffer.write(fAutoSave)) return false;
58  if(!a_buffer.write((int)1000000)) return false; //fEstimate;
59 
60  if(!m_branches.stream(a_buffer)) return false;
61 
62  {obj_array<base_leaf> m_leaves;
64  const std::vector<base_leaf*>& leaves = (*itb)->leaves();
65  tools_vforcit(base_leaf*,leaves,itl) {
66  m_leaves.push_back(*itl); //WARNING : ownership touchy.
67  }
68  }
69  if(!m_leaves.stream(a_buffer)) return false;
70  m_leaves.clear();} //WARNING : important.
71 
72  // fIndexValues (TArrayD).
73  if(!a_buffer.write_array(std::vector<double>())) return false; //TArrayD
74  // fIndex (TArrayI).
75  if(!a_buffer.write_array(std::vector<int>())) return false; //TArrayI
76 
77  if(!a_buffer.set_byte_count(c)) return false;
78  return true;
79  }
80 public: //itree
81  //virtual void add_tot_bytes(uint32 a_n) {m_tot_bytes += a_n;}
82  //virtual void add_zip_bytes(uint32 a_n) {m_zip_bytes += a_n;}
83  virtual idir& dir() {return m_dir;}
84  virtual const idir& dir() const {return m_dir;}
85 public:
86  tree(idir& a_dir,const std::string& a_name,const std::string& a_title,bool a_managed = true)
87  :m_dir(a_dir)
88  ,m_out(a_dir.file().out())
89  ,m_name(a_name)
90  ,m_title(a_title)
91  ,m_entries(0)
92  ,m_tot_bytes(0)
93  ,m_zip_bytes(0)
94  {
95 #ifdef TOOLS_MEM
96  mem::increment(s_class().c_str());
97 #endif
98  if(a_managed) a_dir.append_object(this); //a_dir takes ownership of tree.
99  }
100  virtual ~tree(){
101 #ifdef TOOLS_MEM
102  mem::decrement(s_class().c_str());
103 #endif
104  }
105 protected:
106  tree(const tree& a_from)
107  :iobject(a_from),itree(a_from)
108  ,m_dir(a_from.m_dir)
109  ,m_out(a_from.m_out)
110  {}
111  tree& operator=(const tree&){return *this;}
112 public:
113  std::ostream& out() const {return m_out;}
114  const std::vector<branch*>& branches() const {return m_branches;}
115  //uint64 tot_bytes() const {return m_tot_bytes;}
116  //uint64 zip_bytes() const {return m_zip_bytes;}
117  uint64 entries() const {return m_entries;}
118 
119  branch* create_branch(const std::string& a_name){
120  const ifile& _file = m_dir.file();
121  branch* br = new branch(m_out,_file.byte_swap(),_file.compression(),
122  m_dir.seek_directory(),a_name,m_name,_file.verbose());
123  if(!br) return 0;
124  m_branches.push_back(br);
125  return br;
126  }
127 
131  template <class TYPE>
132  leaf_ref<TYPE>* create_leaf_ref(const std::string& a_name,const TYPE& a_ref){
133  branch* br = create_branch(a_name);
134  if(!br) return 0;
135  return br->create_leaf_ref<TYPE>(a_name,a_ref);
136  }
137 
138  leaf_string_ref* create_leaf_string_ref(const std::string& a_name,const std::string& a_ref){
139  branch* br = create_branch(a_name);
140  if(!br) return 0;
141  return br->create_leaf_string_ref(a_name,a_ref);
142  }
143 
144  template <class T>
145  leaf_element* create_std_vector_leaf_ref(const std::string& a_name,const std::vector<T>& a_ref){
146  const ifile& _file = m_dir.file();
148  m_dir.seek_directory(),a_name,m_name,a_ref,_file.verbose());
149  leaf_element* le = br->create_leaf_element(a_name);
150  m_branches.push_back(br);
151  return le;
152  }
156  template <class T>
157  std_vector_be_ref<T>* create_std_vector_be_ref(const std::string& a_name,const std::vector<T>& a_ref){
158  const ifile& _file = m_dir.file();
160  m_dir.seek_directory(),a_name,m_name,a_ref,_file.verbose());
161  m_branches.push_back(br);
162  return br;
163  }
164 
165  template <class T>
166  std_vector_be<T>* create_std_vector_be(const std::string& a_name,const std::vector<T>& a_def = std::vector<T>()){
167  const ifile& _file = m_dir.file();
168  std_vector_be<T>* br = new std_vector_be<T>(m_out,_file.byte_swap(),_file.compression(),
169  m_dir.seek_directory(),a_name,m_name,a_def,_file.verbose());
170  m_branches.push_back(br);
171  return br;
172  }
173 
174  template <class T>
175  std_vector_be_pointer<T>* create_std_vector_be_pointer(const std::string& a_name,std::vector<T>* a_pointer){
176  const ifile& _file = m_dir.file();
178  m_dir.seek_directory(),a_name,m_name,a_pointer,_file.verbose());
179  m_branches.push_back(br);
180  return br;
181  }
182 
183  template <class TYPE>
184  leaf<TYPE>* create_leaf(const std::string& a_name){
185  branch* br = create_branch(a_name);
186  if(!br) return 0;
187  return br->create_leaf<TYPE>(a_name);
188  }
189 
190  leaf_object* create_leaf(const std::string& a_name,const iobject& a_obj){
191  const ifile& _file = m_dir.file();
192  branch_object* br = new branch_object(m_out,_file.byte_swap(),_file.compression(),
193  m_dir.seek_directory(),a_name,m_name,_file.verbose());
194  m_branches.push_back(br);
195  return br->create_leaf(a_name,a_obj);
196  }
197 
198  bool fill(uint32& a_nbytes) {
199  // Fill all branches of a Tree :
200  // This function loops on all the branches of this tree.
201  // For each branch, it copies to the branch buffer (basket) the current
202  // values of the leaves data types.
203  // If a leaf is a simple data type, a simple conversion to a machine
204  // independent format has to be done.
205  a_nbytes = 0;
207  //FIXME if ((*it)->testBit(kDoNotProcess)) continue;
208  uint32 n,add_bytes,nout;
209  if(!(*it)->fill(m_dir.file(),n,add_bytes,nout)) {a_nbytes = 0;return false;}
210  a_nbytes += n;
211  m_tot_bytes += add_bytes;
212  m_zip_bytes += nout;
213  }
214 
215  m_entries++;
216 
217  //if (fTotBytes - fSavedBytes > fAutoSave) {
218  // if(!autoSave()) return false;
219  //}
220 
221  return true;
222  }
223 
224  void reset() {
225  // Reset buffers and entries count in all branches/leaves
226  m_entries = 0;
227  m_tot_bytes = 0;
228  m_zip_bytes = 0;
229  //fSavedBytes = 0;
230  //fTotalBuffers = 0;
231  //fChainOffset = 0;
232  {tools_vforcit(branch*,m_branches,it) (*it)->reset();}
233  }
234 protected:
236  std::ostream& m_out;
237  //Named
238  std::string m_name;
239  std::string m_title;
240 
242  uint64 m_entries; // Number of entries
243  uint64 m_tot_bytes; // Total number of bytes in branches before compression
244  uint64 m_zip_bytes; // Total number of bytes in branches after compression
245 };
246 
247 }}
248 
249 #endif
tools::wroot::tree::m_title
std::string m_title
Definition: tree:239
tools::wroot::branch::create_leaf_ref
leaf_ref< T > * create_leaf_ref(const std::string &a_name, const T &a_ref)
Definition: branch:200
tools::wroot::tree::m_dir
idir & m_dir
Definition: tree:235
tools::uint64
unsigned long long uint64
Definition: typedefs:72
tools::wroot::tree::operator=
tree & operator=(const tree &)
Definition: tree:111
tools::wroot::tree::reset
void reset()
Definition: tree:224
tools::wroot::ifile
Definition: ifile:18
tools::wroot::leaf_object
Definition: leaf:318
tools::wroot::tree::create_branch
branch * create_branch(const std::string &a_name)
Definition: tree:119
tools::wroot::iobject
Definition: iobject:17
tools::wroot::tree::create_leaf
leaf_object * create_leaf(const std::string &a_name, const iobject &a_obj)
Definition: tree:190
tools::wroot::branch
Definition: branch:16
tools::wroot::idir
Definition: idir:18
tools::wroot::tree::create_std_vector_be_pointer
std_vector_be_pointer< T > * create_std_vector_be_pointer(const std::string &a_name, std::vector< T > *a_pointer)
Definition: tree:175
tools::wroot::tree::m_tot_bytes
uint64 m_tot_bytes
Definition: tree:243
tools::wroot::idir::file
virtual ifile & file()=0
tools::wroot::tree::dir
virtual const idir & dir() const
Definition: tree:84
tools::wroot::tree::m_out
std::ostream & m_out
Definition: tree:236
branch_element
iobject
tools::wroot::tree::~tree
virtual ~tree()
Definition: tree:100
tools::wroot::tree::store_class_name
virtual const std::string & store_class_name() const
Definition: tree:27
tools::wroot::buffer::set_byte_count
bool set_byte_count(uint32 a_pos)
Definition: buffer:199
tools::wroot::obj_array
Definition: named:34
tools::wroot::tree::create_std_vector_be
std_vector_be< T > * create_std_vector_be(const std::string &a_name, const std::vector< T > &a_def=std::vector< T >())
Definition: tree:166
tools::wroot::tree::tree
tree(idir &a_dir, const std::string &a_name, const std::string &a_title, bool a_managed=true)
Definition: tree:86
idir
tools::wroot::std_vector_be_ref
Definition: branch_element:80
tools::wroot::tree::create_leaf
leaf< TYPE > * create_leaf(const std::string &a_name)
Definition: tree:184
tools::wroot::Named_stream
bool Named_stream(buffer &a_buffer, const std::string &a_name, const std::string &a_title)
Definition: named:23
tools::wroot::tree::create_leaf_string_ref
leaf_string_ref * create_leaf_string_ref(const std::string &a_name, const std::string &a_ref)
Definition: tree:138
tools::wroot::tree::branches
const std::vector< branch * > & branches() const
Definition: tree:114
tools::wroot::obj_array::stream
virtual bool stream(buffer &a_buffer) const
Definition: named:41
tools::wroot::idir::seek_directory
virtual seek seek_directory() const =0
tools::wroot::branch_element::create_leaf_element
leaf_element * create_leaf_element(const std::string &a_name)
Definition: branch_element:64
tools::wroot::tree::m_zip_bytes
uint64 m_zip_bytes
Definition: tree:244
tools::wroot::base_leaf
Definition: base_leaf:19
tools::wroot::buffer::write_version
bool write_version(short a_version)
Definition: buffer:169
tools::wroot::tree::create_leaf_ref
leaf_ref< TYPE > * create_leaf_ref(const std::string &a_name, const TYPE &a_ref)
ref : //////////////////////////////////////
Definition: tree:132
tools::wroot::buffer::write_array
bool write_array(const T *a_a, uint32 a_n)
Definition: buffer:148
branch_object
tools::wroot::std_vector_be_pointer
Definition: branch_element:183
itree
tools::wroot::buffer::write
bool write(T x)
Definition: buffer:97
tools::wroot::tree::out
std::ostream & out() const
Definition: tree:113
tools::wroot::tree::m_name
std::string m_name
Definition: tree:238
tools::wroot::tree::m_entries
uint64 m_entries
Definition: tree:242
tools::wroot::tree
Definition: tree:17
tools::wroot::idir::append_object
virtual void append_object(iobject *)=0
tools::wroot::AttFill_stream
bool AttFill_stream(buffer &a_buffer)
Definition: named:137
tools::wroot::ifile::compression
virtual uint32 compression() const =0
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::wroot::std_vector_be
Definition: branch_element:140
tools::wroot::branch_object::create_leaf
leaf_object * create_leaf(const std::string &a_name, const iobject &a_obj)
Definition: branch_object:58
tools::wroot::tree::entries
uint64 entries() const
Definition: tree:117
tools::wroot::itree
Definition: itree:17
tools::wroot::ifile::byte_swap
virtual bool byte_swap() const =0
tools::wroot::leaf
Definition: leaf:105
tools::wroot::buffer
Definition: buffer:28
tools::wroot::file
Definition: file:34
tools::wroot::AttLine_stream
bool AttLine_stream(buffer &a_buffer)
Definition: named:124
tools::wroot::tree::dir
virtual idir & dir()
Definition: tree:83
tools::wroot::branch::create_leaf
leaf< T > * create_leaf(const std::string &a_name)
Definition: branch:216
tools::wroot::leaf_element
Definition: leaf:266
tools::wroot::leaf_string_ref
Definition: leaf:129
tools::wroot::AttMarker_stream
bool AttMarker_stream(buffer &a_buffer)
Definition: named:148
tools::wroot::tree::create_std_vector_be_ref
std_vector_be_ref< T > * create_std_vector_be_ref(const std::string &a_name, const std::vector< T > &a_ref)
Definition: tree:157
tools::wroot::tree::name
virtual const std::string & name() const
Definition: tree:25
tools::wroot::tree::title
virtual const std::string & title() const
Definition: tree:26
tools::wroot::tree::fill
bool fill(uint32 &a_nbytes)
Definition: tree:198
tools_vforcit
#define tools_vforcit(a__T, a__v, a__it)
Definition: forit:7
tools::wroot::ifile::verbose
virtual bool verbose() const =0
tools::wroot::tree::tree
tree(const tree &a_from)
Definition: tree:106
tools::uint32
unsigned int uint32
Definition: typedefs:71
tools::wroot::branch_object
Definition: branch_object:12
tools::wroot::branch::create_leaf_string_ref
leaf_string_ref * create_leaf_string_ref(const std::string &a_name, const std::string &a_ref)
Definition: branch:206
tools::wroot::tree::create_std_vector_leaf_ref
leaf_element * create_std_vector_leaf_ref(const std::string &a_name, const std::vector< T > &a_ref)
Definition: tree:145
tools::wroot::tree::stream
virtual bool stream(buffer &a_buffer) const
Definition: tree:31
tools::wroot::tree::m_branches
obj_array< branch > m_branches
Definition: tree:241
tools::wroot::leaf_ref
Definition: leaf:49