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