inlib
1.2.0
|
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved. 00002 // See the file inlib.license for terms. 00003 00004 #ifndef inlib_wroot_ntuple 00005 #define inlib_wroot_ntuple 00006 00007 // An ntuple class to write at the ROOT format. 00008 // It inherits wroot::tree and each column is mapped 00009 // on a wroot::branch. Each add_row() does a tree::fill(). 00010 00011 #include "tree" 00012 00013 #include "../vfind" 00014 #include "../vmanip" 00015 00016 namespace inlib { 00017 namespace wroot { 00018 00019 class ntuple : public tree { 00020 protected: 00021 class icol { 00022 public: 00023 virtual ~icol(){} 00024 public: 00025 virtual void add() = 0; 00026 virtual const std::string& name() const = 0; 00027 virtual void set_basket_size(uint32) = 0; 00028 }; 00029 00030 public: 00031 template <class T> 00032 class column : public virtual icol { 00033 public: //icol 00034 virtual void add() {m_leaf->fill(m_tmp);m_tmp = m_def;} 00035 virtual const std::string& name() const {return m_leaf->name();} 00036 virtual void set_basket_size(uint32 a_size) { 00037 m_leaf->branch().set_basket_size(a_size); 00038 } 00039 public: 00040 column(tree& a_tree, 00041 const std::string& a_name, 00042 const T& a_def) 00043 :m_tree(a_tree),m_leaf(0) 00044 ,m_def(a_def),m_tmp(a_def) 00045 { 00046 branch* br = m_tree.create_branch(a_name); 00047 m_leaf = br->create_leaf<T>(a_name,a_name); 00048 } 00049 virtual ~column(){} 00050 protected: 00051 column(const column& a_from) 00052 :icol(a_from) 00053 ,m_tree(a_from.m_tree) 00054 ,m_leaf(0) 00055 ,m_def(a_from.m_def) 00056 ,m_tmp(a_from.m_tmp) 00057 {} 00058 column& operator=(const column& a_from){ 00059 m_leaf = 0; 00060 m_def = a_from.m_def; 00061 m_tmp = a_from.m_tmp; 00062 return *this; 00063 } 00064 public: 00065 bool fill(const T& a_value) {m_tmp = a_value;return true;} 00066 protected: 00067 tree& m_tree; 00068 leaf<T>* m_leaf; 00069 T m_def; 00070 T m_tmp; 00071 }; 00072 00073 public: 00074 ntuple(idir& a_dir, 00075 const std::string& a_name,const std::string& a_title) 00076 : tree(a_dir,a_name,a_title){} 00077 virtual ~ntuple() {} 00078 protected: 00079 ntuple(const ntuple& a_from) 00080 :iobject(a_from),itree(a_from),tree(a_from) 00081 {} 00082 ntuple& operator=(const ntuple&){return *this;} 00083 public: 00084 const std::vector<icol*>& columns() const {return m_cols;} 00085 00086 template <class T> 00087 column<T>* create_column(const std::string& a_name, 00088 const T& a_def = T()) { 00089 if(find_named<icol>(m_cols,a_name)) return 0; 00090 column<T>* col = new column<T>(*this,a_name,a_def); 00091 if(!col) return 0; 00092 m_cols.push_back(col); 00093 return col; 00094 } 00095 00096 bool add_row() { 00097 if(m_cols.empty()) return false; 00098 {std::vector<icol*>::iterator it; 00099 for(it=m_cols.begin();it!=m_cols.end();++it) (*it)->add();} 00100 uint32 n; 00101 return tree::fill(n); 00102 } 00103 void set_basket_size(uint32 a_size) { 00104 std::vector<icol*>::iterator it; 00105 for(it=m_cols.begin();it!=m_cols.end();++it) { 00106 (*it)->set_basket_size(a_size); 00107 } 00108 } 00109 protected: 00110 std::vector<icol*> m_cols; 00111 }; 00112 00113 }} 00114 00115 #endif