Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/tools/wroot/tree is written in an unsupported language. File is not indexed.

0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003 
0004 #ifndef tools_wroot_tree
0005 #define tools_wroot_tree
0006 
0007 #include "itree"
0008 #include "iobject"
0009 #include "idir"
0010 
0011 #include "branch_element"
0012 #include "branch_object"
0013 
0014 namespace tools {
0015 namespace wroot {
0016 
0017 class tree : public virtual iobject, public virtual itree {
0018 public: //iobject
0019   virtual const std::string& name() const {return m_name;}
0020   virtual const std::string& title() const {return m_title;}
0021   virtual const std::string& store_class_name() const {
0022     static const std::string s_v("TTree");
0023     return s_v;
0024   }
0025   virtual bool stream(buffer& a_buffer) const {
0026     unsigned int c;
0027     if(!a_buffer.write_version(5,c)) return false;
0028 
0029     if(!Named_stream(a_buffer,m_name,m_title)) return false;
0030 
0031     // Beurk.
0032     if(!AttLine_stream(a_buffer)) return false;
0033     if(!AttFill_stream(a_buffer)) return false;
0034     if(!AttMarker_stream(a_buffer)) return false;
0035 
0036     double fEntries = (double)m_entries;
0037     if(!a_buffer.write(fEntries)) return false;
0038 
0039     double fTotBytes = (double)m_tot_bytes;
0040     double fZipBytes = (double)m_zip_bytes;
0041     if(!a_buffer.write(fTotBytes)) return false;
0042     if(!a_buffer.write(fZipBytes)) return false;
0043     if(!a_buffer.write((double)0)) return false; //fSavedBytes
0044     if(!a_buffer.write((int)0)) return false;    //fTimerInterval
0045     if(!a_buffer.write((int)25)) return false;   //fScanField (25)
0046     if(!a_buffer.write((int)0)) return false;    //fUpdate
0047     if(!a_buffer.write((int)1000000000)) return false; //fMaxEntryLoop
0048     int fMaxVirtualSize = 0;
0049     int fAutoSave = 100000000;
0050     if(!a_buffer.write(fMaxVirtualSize)) return false;
0051     if(!a_buffer.write(fAutoSave)) return false;
0052     if(!a_buffer.write((int)1000000)) return false;    //fEstimate;
0053 
0054     if(!m_branches.stream(a_buffer)) return false;
0055 
0056    {obj_array<base_leaf> m_leaves;
0057     tools_vforcit(branch*,m_branches,itb) {
0058       const std::vector<base_leaf*>& leaves = (*itb)->leaves();
0059       tools_vforcit(base_leaf*,leaves,itl) {
0060         m_leaves.push_back(*itl); //WARNING : ownership touchy.
0061       }
0062     }
0063     if(!m_leaves.stream(a_buffer)) return false;
0064     m_leaves.clear();} //WARNING : important.
0065 
0066     // fIndexValues (TArrayD).
0067     if(!a_buffer.write_array(std::vector<double>())) return false; //TArrayD
0068     // fIndex (TArrayI).
0069     if(!a_buffer.write_array(std::vector<int>())) return false; //TArrayI
0070 
0071     if(!a_buffer.set_byte_count(c)) return false;
0072     return true;
0073   }
0074 public: //itree
0075   virtual idir& dir() {return m_dir;}
0076   virtual const idir& dir() const {return m_dir;}
0077 public:
0078   tree(idir& a_dir,const std::string& a_name,const std::string& a_title,bool a_managed = true)
0079   :m_dir(a_dir)
0080   ,m_out(a_dir.file().out())
0081   ,m_name(a_name)
0082   ,m_title(a_title)
0083   ,m_entries(0)
0084   ,m_tot_bytes(0)
0085   ,m_zip_bytes(0)
0086   {
0087     if(a_managed) a_dir.append_object(this); //a_dir takes ownership of tree.
0088   }
0089   virtual ~tree(){
0090   }
0091 protected:
0092   tree(const tree& a_from)
0093   :iobject(a_from),itree(a_from)
0094   ,m_dir(a_from.m_dir)
0095   ,m_out(a_from.m_out)
0096   {}
0097   tree& operator=(const tree&){return *this;}
0098 public:
0099   std::ostream& out() const {return m_out;}
0100   const std::vector<branch*>& branches() const {return m_branches;}
0101   uint64 entries() const {return m_entries;}
0102 
0103   branch* create_branch(const std::string& a_name){
0104     const ifile& _file = m_dir.file();
0105     branch* br = new branch(m_out,_file.byte_swap(),_file.compression(),
0106                             m_dir.seek_directory(),a_name,m_name,_file.verbose());
0107     if(!br) return 0;
0108     m_branches.push_back(br);
0109     return br;
0110   }
0111 
0112   ////////////////////////////////////////////////
0113   /// ref : //////////////////////////////////////
0114   ////////////////////////////////////////////////
0115   template <class TYPE>
0116   leaf_ref<TYPE>* create_leaf_ref(const std::string& a_name,const TYPE& a_ref){
0117     branch* br = create_branch(a_name);
0118     if(!br) return 0;
0119     return br->create_leaf_ref<TYPE>(a_name,a_ref);
0120   }
0121 
0122   leaf_string_ref* create_leaf_string_ref(const std::string& a_name,const std::string& a_ref){
0123     branch* br = create_branch(a_name);
0124     if(!br) return 0;
0125     return br->create_leaf_string_ref(a_name,a_ref);
0126   }
0127 
0128   template <class T>
0129   leaf_element* create_std_vector_leaf_ref(const std::string& a_name,const std::vector<T>& a_ref){
0130     const ifile& _file = m_dir.file();
0131     std_vector_be_ref<T>* br = new std_vector_be_ref<T>(m_out,_file.byte_swap(),_file.compression(),
0132                                                         m_dir.seek_directory(),a_name,m_name,a_ref,_file.verbose());
0133     leaf_element* le = br->create_leaf_element(a_name);
0134     m_branches.push_back(br);
0135     return le;
0136   }
0137   ////////////////////////////////////////////////
0138   ////////////////////////////////////////////////
0139   ////////////////////////////////////////////////
0140   template <class T>
0141   std_vector_be_ref<T>* create_std_vector_be_ref(const std::string& a_name,const std::vector<T>& a_ref){
0142     const ifile& _file = m_dir.file();
0143     std_vector_be_ref<T>* br = new std_vector_be_ref<T>(m_out,_file.byte_swap(),_file.compression(),
0144                                                         m_dir.seek_directory(),a_name,m_name,a_ref,_file.verbose());
0145     m_branches.push_back(br);
0146     return br;
0147   }
0148 
0149   template <class T>
0150   std_vector_be<T>* create_std_vector_be(const std::string& a_name,const std::vector<T>& a_def = std::vector<T>()){
0151     const ifile& _file = m_dir.file();
0152     std_vector_be<T>* br = new std_vector_be<T>(m_out,_file.byte_swap(),_file.compression(),
0153                                                 m_dir.seek_directory(),a_name,m_name,a_def,_file.verbose());
0154     m_branches.push_back(br);
0155     return br;
0156   }
0157 
0158   template <class T>
0159   std_vector_be_pointer<T>* create_std_vector_be_pointer(const std::string& a_name,std::vector<T>* a_pointer){
0160     const ifile& _file = m_dir.file();
0161     std_vector_be_pointer<T>* br = new std_vector_be_pointer<T>(m_out,_file.byte_swap(),_file.compression(),
0162                                                                 m_dir.seek_directory(),a_name,m_name,a_pointer,_file.verbose());
0163     m_branches.push_back(br);
0164     return br;
0165   }
0166 
0167   template <class TYPE>
0168   leaf<TYPE>* create_leaf(const std::string& a_name){
0169     branch* br = create_branch(a_name);
0170     if(!br) return 0;
0171     return br->create_leaf<TYPE>(a_name);
0172   }
0173 
0174   leaf_object* create_leaf(const std::string& a_name,const iobject& a_obj){
0175     const ifile& _file = m_dir.file();
0176     branch_object* br = new branch_object(m_out,_file.byte_swap(),_file.compression(),
0177                                           m_dir.seek_directory(),a_name,m_name,_file.verbose());
0178     m_branches.push_back(br);
0179     return br->create_leaf(a_name,a_obj);
0180   }
0181 
0182   bool fill(uint32& a_nbytes) {
0183     // Fill all branches of a Tree :
0184     //   This function loops on all the branches of this tree.
0185     //   For each branch, it copies to the branch buffer (basket) the current
0186     //   values of the leaves data types.
0187     //   If a leaf is a simple data type, a simple conversion to a machine
0188     //   independent format has to be done.
0189     a_nbytes = 0;
0190     tools_vforcit(branch*,m_branches,it) {
0191       //FIXME if ((*it)->testBit(kDoNotProcess)) continue;
0192       uint32 n,add_bytes,nout;
0193       if(!(*it)->fill(m_dir.file(),n,add_bytes,nout)) {a_nbytes = 0;return false;}
0194       a_nbytes += n;
0195       m_tot_bytes += add_bytes;
0196       m_zip_bytes += nout;
0197     }
0198 
0199     m_entries++;
0200 
0201     return true;
0202   }
0203 
0204   void reset() {
0205     // Reset buffers and entries count in all branches/leaves
0206     m_entries       = 0;
0207     m_tot_bytes     = 0;
0208     m_zip_bytes     = 0;
0209    {tools_vforcit(branch*,m_branches,it) (*it)->reset();}
0210   }
0211 protected:
0212   idir& m_dir;
0213   std::ostream& m_out;
0214   //Named
0215   std::string m_name;
0216   std::string m_title;
0217 
0218   obj_array<branch> m_branches;
0219   uint64 m_entries;   // Number of entries
0220   uint64 m_tot_bytes; // Total number of bytes in branches before compression
0221   uint64 m_zip_bytes; // Total number of bytes in branches after compression
0222 };
0223 
0224 }}
0225 
0226 #endif