inlib  1.2.0
/Users/barrand/private/dev/softinex/old/inexlib-1.2/inlib/inlib/wroot/tree
Go to the documentation of this file.
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
00002 // See the file inlib.license for terms.
00003 
00004 #ifndef inlib_wroot_tree
00005 #define inlib_wroot_tree
00006 
00007 #include "itree"
00008 #include "iobject"
00009 
00010 #include "branch"
00011 
00012 namespace inlib {
00013 namespace wroot {
00014 
00015 class tree : public virtual iobject, public virtual itree {
00016 #ifdef INLIB_MEM
00017   static const std::string& s_class() {
00018     static const std::string s_v("inlib::wroot::tree");
00019     return s_v;
00020   }
00021 #endif
00022 public: //iobject
00023   virtual const std::string& name() const {return m_name;}
00024   virtual const std::string& title() const {return m_title;}
00025   virtual const std::string& store_class_name() const {
00026     static const std::string s_v("TTree");
00027     return s_v;
00028   }
00029   virtual bool stream(buffer& a_buffer) const {
00030     unsigned int c;
00031     if(!a_buffer.write_version(5,c)) return false;
00032 
00033     if(!Named_stream(a_buffer,m_name,m_title)) return false;
00034 
00035     // Beurk.
00036     if(!AttLine_stream(a_buffer)) return false;
00037     if(!AttFill_stream(a_buffer)) return false;
00038     if(!AttMarker_stream(a_buffer)) return false;
00039 
00040     double fEntries = (double)m_entries;
00041     if(!a_buffer.write(fEntries)) return false;
00042 
00043     double fTotBytes = (double)m_tot_bytes;
00044     double fZipBytes = (double)m_zip_bytes;
00045     if(!a_buffer.write(fTotBytes)) return false;
00046     if(!a_buffer.write(fZipBytes)) return false;
00047     if(!a_buffer.write((double)0)) return false; //fSavedBytes
00048     if(!a_buffer.write((int)0)) return false;    //fTimerInterval
00049     if(!a_buffer.write((int)25)) return false;   //fScanField (25)
00050     if(!a_buffer.write((int)0)) return false;    //fUpdate
00051     if(!a_buffer.write((int)1000000000)) return false; //fMaxEntryLoop
00052     int fMaxVirtualSize = 0;
00053     int fAutoSave = 100000000;
00054     if(!a_buffer.write(fMaxVirtualSize)) return false;
00055     if(!a_buffer.write(fAutoSave)) return false;
00056     if(!a_buffer.write((int)1000000)) return false;    //fEstimate;
00057 
00058     if(!m_branches.stream(a_buffer)) return false;
00059 
00060    {ObjArray<base_leaf> m_leaves;
00061     std::vector<branch*>::const_iterator itb;
00062     for(itb=m_branches.begin();itb!=m_branches.end();++itb) {
00063       const std::vector<base_leaf*>& leaves = (*itb)->leaves();
00064       std::vector<base_leaf*>::const_iterator itl;
00065       for(itl=leaves.begin();itl!=leaves.end();++itl) {
00066         m_leaves.push_back(*itl); //WARNING : ownership touchy.
00067       }
00068     }
00069     if(!m_leaves.stream(a_buffer)) return false;
00070     m_leaves.clear();} //WARNING : important.
00071 
00072     // fIndexValues (TArrayD).
00073     if(!Array_stream(a_buffer,std::vector<double>())) return false; //TArrayD
00074     // fIndex (TArrayI).
00075     if(!Array_stream(a_buffer,std::vector<int>())) return false; //TArrayI
00076 
00077     if(!a_buffer.set_byte_count(c)) return false;
00078     return true;
00079   }    
00080 public: //itree
00081   virtual void add_tot_bytes(uint32 a_n) {m_tot_bytes += a_n;}
00082   virtual void add_zip_bytes(uint32 a_n) {m_zip_bytes += a_n;}
00083   virtual idir& dir() {return m_dir;}
00084   virtual const idir& dir() const {return m_dir;}
00085 public:
00086   tree(idir& a_dir, //for branch/baskets(which are keys)
00087               const std::string& a_name,
00088               const std::string& a_title)
00089   :m_dir(a_dir)
00090   ,m_out(a_dir.file().out())
00091   ,m_name(a_name)
00092   ,m_title(a_title)
00093   ,m_entries(0)
00094   ,m_tot_bytes(0)
00095   ,m_zip_bytes(0)
00096   {
00097 #ifdef INLIB_MEM
00098     mem::increment(s_class().c_str());
00099 #endif
00100     a_dir.append_object(this); //a_dir takes ownership of tree.
00101   }
00102   virtual ~tree(){
00103 #ifdef INLIB_MEM
00104     mem::decrement(s_class().c_str());
00105 #endif
00106   }
00107 protected:
00108   tree(const tree& a_from)
00109   : iobject(a_from),itree(a_from)
00110   ,m_dir(a_from.m_dir)
00111   ,m_out(a_from.m_out)
00112   {}
00113   tree& operator=(const tree&){return *this;}
00114 public:
00115   //const std::vector<branch*>& branches() const {return m_branches;}
00116 
00117   branch* create_branch(const std::string& a_name){
00118     branch* br = new branch(*this,a_name,m_name);
00119     m_branches.push_back(br);
00120     return br;
00121   }
00122 
00123   bool fill(uint32& a_nbytes) {
00124     // Fill all branches of a Tree :
00125     //   This function loops on all the branches of this tree.
00126     //   For each branch, it copies to the branch buffer (basket) the current
00127     //   values of the leaves data types.
00128     //   If a leaf is a simple data type, a simple conversion to a machine
00129     //   independent format has to be done.
00130     a_nbytes = 0;
00131     std::vector<branch*>::const_iterator it;
00132     for(it=m_branches.begin();it!=m_branches.end();++it) {
00133       //FIXME if ((*it)->testBit(kDoNotProcess)) continue;
00134       uint32 n;
00135       if(!(*it)->fill(n)) {a_nbytes = 0;return false;}
00136       a_nbytes += n;
00137     } 
00138 
00139     m_entries++;
00140 
00141     //if (fTotBytes - fSavedBytes > fAutoSave) {
00142     //  if(!autoSave()) return false;
00143     //}
00144 
00145     return true;
00146   }
00147 
00148 protected:
00149   idir& m_dir;
00150   std::ostream& m_out;
00151   //Named
00152   std::string m_name;
00153   std::string m_title;
00154 
00155   ObjArray<branch> m_branches;
00156   uint64 m_entries;   // Number of entries
00157   uint64 m_tot_bytes; // Total number of bytes in branches before compression
00158   uint64 m_zip_bytes; // Total number of bytes in branches after compression
00159 };
00160 
00161 }}
00162 
00163 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines