4 #ifndef tools_hdf5_ntuple
5 #define tools_hdf5_ntuple
38 virtual bool add() = 0;
40 virtual const std::string&
name()
const = 0;
48 static const std::string& s_class() {
49 static const std::string s_v(
"tools::hdf5::ntuple::column_ref<"+
tools::stype(T())+
">");
55 static const T s_v = T();
59 if(
void* p = tools::cmp_cast<column_ref>(
this,a_class)) {
return p;}
68 m_store.
out() <<
"tools::hdf5::ntuple::column_ref::add : write_page() failed." << std::endl;
89 m_store.
out() <<
"tools::hdf5::ntuple::column_ref:fetch_entry : no more data." << std::endl;
104 m_store.
out() <<
"tools::hdf5::ntuple::column_ref:fetch_entry : read_page() failed." << std::endl;
119 if(!a_size)
return false;
137 tools::mem::increment(s_class().c_str());
143 size_t n = tools::mn<size_t>((
size_t)_entries,
m_basket_size);
146 m_store.
out() <<
"tools::hdf5::ntuple::column_ref:column_ref : read_page() failed." << std::endl;
159 m_store.
out() <<
"tools::hdf5::ntuple::column_ref::~column_ref : write_page() failed." << std::endl;
164 tools::mem::decrement(s_class().c_str());
182 if(&a_from==
this)
return *
this;
207 static const std::string& s_class() {
208 static const std::string s_v(
"tools::hdf5::ntuple::column<"+
tools::stype(T())+
">");
214 static const T s_v = T();
218 if(
void* p = tools::cmp_cast<column>(
this,a_class)) {
return p;}
223 column(
store& a_store,
pages& a_pages,
bool a_write,
const std::string& a_name,
size_t a_basket_size)
224 :
parent(a_store,a_pages,a_write,a_name,a_basket_size,
m_tmp)
228 tools::mem::increment(s_class().c_str());
233 tools::mem::decrement(s_class().c_str());
243 if(&a_from==
this)
return *
this;
249 bool fill(
const T& a_value) {
m_tmp = a_value;
return true;}
270 #define TOOLS_HDF5_NTUPLE_VLEN
272 #define TOOLS_HDF5_NTUPLE_STRING
274 #ifdef TOOLS_HDF5_NTUPLE_VLEN
278 static const std::string& s_class() {
279 static const std::string s_v(
"tools::hdf5::ntuple::std_vector_column_ref<"+
tools::stype(T())+
">");
286 if(
void* p = tools::cmp_cast<std_vector_column_ref>(
this,a_class)) {
return p;}
301 m_store.
out() <<
"tools::hdf5::ntuple::std_vector_column_ref:fetch_entry : read_page() failed." << std::endl;
307 for(
size_t index=0;index<n;index++,pos++,dpos++) *pos = *dpos;
317 std::vector<T>& a_ref)
325 tools::mem::increment(s_class().c_str());
330 tools::mem::decrement(s_class().c_str());
343 if(&a_from==
this)
return *
this;
359 static const std::string& s_class() {
360 static const std::string s_v(
"tools::hdf5::ntuple::std_vector_column_ref<"+
tools::stype(T())+
">");
367 if(
void* p = tools::cmp_cast<std_vector_column_ref>(
this,a_class)) {
return p;}
372 virtual void reset() {
374 m_data_pages.reset_pos();
380 if(!m_data_pages.write_page<T>(
m_ref.size(),_data))
return false;
382 if(!parent::fill((
unsigned int)
m_ref.size()))
return false;
388 if(!parent::fetch_entry())
return false;
390 if(!parent::get_entry(sz))
return false;
391 m_ref.resize(sz,T());
394 if(!m_data_pages.read_page<T>(sz,_data)) {
m_ref.clear();
return false;}
398 virtual const std::string&
name()
const {
return m_name;}
405 const std::string& a_name,
size_t a_basket_size,
406 std::vector<T>& a_ref)
407 :parent(a_store,a_pages,a_write,a_name,a_basket_size)
412 tools::mem::increment(s_class().c_str());
414 if(!m_data_pages.is_valid()) {
415 m_store.
out() <<
"tools::hdf5::std_vector_column_ref::std_vector_column_ref :"
416 <<
" can't create data pages." << std::endl;
421 tools::mem::decrement(s_class().c_str());
433 if(&a_from==
this)
return *
this;
440 std::vector<T>&
m_ref;
443 #endif //TOOLS_HDF5_NTUPLE_VLEN
449 static const std::string& s_class() {
450 static const std::string s_v(
"tools::hdf5::ntuple::std_vector_column<"+
tools::stype(T())+
">");
457 if(
void* p = tools::cmp_cast<std_vector_column>(
this,a_class)) {
return p;}
463 :
parent(a_store,a_pages,a_write,a_name,a_basket_size,
m_tmp)
485 #ifdef TOOLS_HDF5_NTUPLE_STRING
492 static const std::string s_v;
496 if(
void* p = tools::cmp_cast<column_string_ref>(
this,a_class)) {
return p;}
508 m_store.
out() <<
"tools::hdf5::ntuple::column_string_ref:fetch_entry : read_page() failed." << std::endl;
527 tools::mem::increment(s_class().c_str());
532 tools::mem::decrement(s_class().c_str());
545 if(&a_from==
this)
return *
this;
557 class column_string_ref : public column<unsigned int> {
564 static const std::string s_v;
567 virtual void* cast(
tools::cid a_class)
const {
568 if(
void* p = tools::cmp_cast<column_string_ref>(
this,a_class)) {
return p;}
571 virtual tools::cid id_cls()
const {
return id_class();}
573 virtual void reset() {
574 m_branch.reset_pos();
575 m_data_pages.reset_pos();
580 const char* _data = m_ref.c_str();
581 if(!m_data_pages.write_page<
char>(m_ref.size(),_data))
return false;
583 if(!parent::fill((
unsigned int)m_ref.size()))
return false;
587 virtual bool fetch_entry() {
589 if(!parent::fetch_entry())
return false;
591 if(!parent::get_entry(sz))
return false;
594 char* _data = (
char*)m_ref.c_str();
595 if(!m_data_pages.read_page<
char>(sz,_data)) {m_ref.clear();
return false;}
599 virtual const std::string& name()
const {
return m_name;}
605 column_string_ref(
store& a_store,pages& a_pages,
bool a_write,
606 const std::string& a_name,
size_t a_basket_size,
608 :parent(a_store,a_pages,a_write,a_name,a_basket_size)
613 tools::mem::increment(s_class().c_str());
615 if(!m_data_pages.is_valid()) {
616 m_store.out() <<
"tools::hdf5::column_string_ref::column_string_ref :"
617 <<
" can't create data pages." << std::endl;
620 virtual ~column_string_ref(){
622 tools::mem::decrement(s_class().c_str());
626 column_string_ref(
const column_string_ref& a_from)
630 ,m_data_pages(a_from.m_store.
out(),a_from.m_store.
group(),a_from.
m_name+
"_data",
633 column_string_ref&
operator=(
const column_string_ref& a_from){
634 if(&a_from==
this)
return *
this;
644 #endif //TOOLS_HDF5_NTUPLE_STRING
653 static const std::string s_v;
657 if(
void* p = tools::cmp_cast<column_string>(
this,a_class)) {
return p;}
663 :
parent(a_store,a_pages,a_write,a_name,a_basket_size,m_tmp)
666 tools::mem::increment(s_class().c_str());
671 tools::mem::decrement(s_class().c_str());
680 if(&a_from==
this)
return *
this;
681 parent::operator=(a_from);
685 bool fill(
const std::string& a_value) {m_tmp = a_value;
return true;}
686 bool get_entry(std::string& a_value)
const {a_value = m_tmp;
return true;}
692 ntuple(std::ostream& a_out,hid_t a_group,
const std::string& a_name,
693 unsigned int a_compress,
size_t )
694 :
store(a_out,a_group,a_name,true,a_compress)
697 ntuple(std::ostream& a_out,hid_t a_group,
const std::string& a_name)
698 :
store(a_out,a_group,a_name,false,0)
702 #define TOOLS_HDF5_NTUPLE_READ_CREATE_COL(a__type) \
703 if((*it)->form()==tools::stype(a__type())) {\
704 column<a__type>* col = new column<a__type>(*this,*(*it),false,(*it)->name(),0);\
705 if(!col) {tools::safe_clear<icol>(m_cols);return;}\
706 m_cols.push_back(col);\
709 #define TOOLS_HDF5_NTUPLE_READ_CREATE_VEC_COL(a__vec_type,a__type) \
710 if((*it)->form()==s_vector_##a__vec_type()) {\
711 std_vector_column<a__type>* col = new std_vector_column<a__type>(*this,*(*it),false,(*it)->name(),0);\
712 if(!col) {tools::safe_clear<icol>(m_cols);return;}\
713 m_cols.push_back(col);\
732 if((*it)->form()==s_string()) {
734 if(!col) {tools::safe_clear<icol>(m_cols);
return;}
735 m_cols.push_back(col);
755 m_out <<
"tools::hdf5::ntuple::ntuple(read) :"
757 <<
", type " <<
tools::sout((*it)->form()) <<
" not yet handled."
760 tools::safe_clear<icol>(m_cols);
764 #undef TOOLS_HDF5_NTUPLE_READ_CREATE_VEC_COL
765 #undef TOOLS_HDF5_NTUPLE_READ_CREATE_COL
769 :
store(a_out,a_group,a_name,false,0)
771 if(!initialize(a_out,a_bd)) {}
775 unsigned int a_compress,
size_t a_basket_size)
776 :
store(a_out,a_group,a_bkg.name(),true,a_compress)
777 ,m_title(a_bkg.title())
779 const std::vector<tools::column_booking>& cols = a_bkg.
columns();
782 #define TOOLS_HDF5_NTUPLE_CREATE_COL(a__type) \
783 if((*it).cls_id()==tools::_cid(a__type())) {\
784 a__type* user = (a__type*)(*it).user_obj();\
786 if(!create_column_ref<a__type>((*it).name(),a_basket_size,*user)) {\
787 m_out << "tools::hdf5::ntuple :"\
788 << " can't create column_ref " << tools::sout((*it).name()) << "."\
790 tools::safe_clear<icol>(m_cols);\
794 if(!create_column<a__type>((*it).name(),a_basket_size)) {\
795 m_out << "tools::hdf5::ntuple :"\
796 << " can't create column " << tools::sout((*it).name()) << "."\
798 tools::safe_clear<icol>(m_cols);\
804 #define TOOLS_HDF5_NTUPLE_CREATE_VEC_COL(a__type) \
805 if((*it).cls_id()==tools::_cid_std_vector<a__type>()) {\
806 std::vector<a__type>* vec = (std::vector<a__type>*)(*it).user_obj();\
808 m_out << "tools::hdf5::ntuple :"\
809 << " for std::vector column " << tools::sout((*it).name())\
810 << ", the user vector pointer is null."\
812 tools::safe_clear<icol>(m_cols);\
815 if(!create_std_column_ref<a__type>((*it).name(),a_basket_size,*vec)) {\
816 m_out << "tools::hdf5::ntuple :"\
817 << " can't create std::vector column " << tools::sout((*it).name()) << "."\
819 tools::safe_clear<icol>(m_cols);\
839 else if((*it).cls_id()==
tools::_cid(std::string())) {
840 if(!create_column_string((*it).name(),a_basket_size)) {
841 m_out <<
"tools::hdf5::ntuple :"
842 <<
" can't create string column " <<
tools::sout((*it).name()) <<
"."
844 tools::safe_clear<icol>(m_cols);
865 m_out <<
"tools::hdf5::ntuple :"
867 <<
", type with cid " << (*it).cls_id() <<
" not yet handled."
869 tools::safe_clear<icol>(m_cols);
873 #undef TOOLS_HDF5_NTUPLE_CREATE_COL
874 #undef TOOLS_HDF5_NTUPLE_CREATE_VEC_COL
879 tools::safe_clear<icol>(m_cols);
886 tools::safe_clear<icol>(m_cols);
891 #define TOOLS_HDF5_NTUPLE_READ_BINDING_CREATE_COL(a__type) \
892 if((*it)->form()==tools::stype(a__type())) {\
893 a__type* user_var = a_bd.find_variable<a__type>((*it)->name());\
895 column_ref<a__type>* col = new column_ref<a__type>(*this,*(*it),false,(*it)->name(),0,*user_var);\
896 if(!col) {tools::safe_clear<icol>(m_cols);return false;}\
897 m_cols.push_back(col);\
899 column<a__type>* col = new column<a__type>(*this,*(*it),false,(*it)->name(),0);\
900 if(!col) {tools::safe_clear<icol>(m_cols);return false;}\
901 m_cols.push_back(col);\
905 #define TOOLS_HDF5_NTUPLE_READ_BINDING_CREATE_VEC_COL(a__vec_type,a__type) \
906 if((*it)->form()==s_vector_##a__vec_type()) {\
907 std::vector<a__type>* user_var = a_bd.find_variable< std::vector<a__type> >((*it)->name());\
909 std_vector_column_ref<a__type>* col = \
910 new std_vector_column_ref<a__type>(*this,*(*it),false,(*it)->name(),0,*user_var);\
911 if(!col) {tools::safe_clear<icol>(m_cols);return false;}\
912 m_cols.push_back(col);\
914 std_vector_column<a__type>* col = new std_vector_column<a__type>(*this,*(*it),false,(*it)->name(),0);\
915 if(!col) {tools::safe_clear<icol>(m_cols);return false;}\
916 m_cols.push_back(col);\
936 if((*it)->form()==s_string()) {
937 std::string* user_var = a_bd.find_variable<std::string>((*it)->name());
940 if(!col) {tools::safe_clear<icol>(m_cols);
return false;}
941 m_cols.push_back(col);
944 if(!col) {tools::safe_clear<icol>(m_cols);
return false;}
945 m_cols.push_back(col);
966 a_out <<
"tools::hdf5::ntuple::ntuple(read with binding) :"
968 <<
", type " <<
tools::sout((*it)->form()) <<
" not yet handled."
970 tools::safe_clear<icol>(m_cols);
974 #undef TOOLS_HDF5_NTUPLE_READ_BINDING_CREATE_COL
975 #undef TOOLS_HDF5_NTUPLE_READ_BINDING_CREATE_VEC_COL
979 const std::string&
title()
const {
return m_title;}
981 const std::vector<icol*>&
columns()
const {
return m_cols;}
982 std::vector<icol*>&
columns() {
return m_cols;}
986 if(tools::find_named<icol>(m_cols,a_name))
return 0;
988 if(!_pages)
return 0;
991 m_cols.push_back(col);
997 if(tools::find_named<icol>(m_cols,a_name))
return 0;
999 if(!_pages)
return 0;
1002 m_cols.push_back(col);
1007 if(tools::find_named<icol>(m_cols,a_name))
return 0;
1008 pages* _pages = create_pages(a_name,s_string());
1009 if(!_pages)
return 0;
1012 m_cols.push_back(col);
1019 if(tools::find_named<icol>(m_cols,a_name))
return 0;
1021 if(!_pages)
return 0;
1024 m_cols.push_back(col);
1030 icol* col = tools::find_named<icol>(m_cols,a_name);
1032 return tools::id_cast<icol, column_ref<T> >(*col);
1036 icol* col = tools::find_named<icol>(m_cols,a_name);
1038 return tools::id_cast<icol, column<T> >(*col);
1043 icol* col = tools::find_named<icol>(m_cols,a_name);
1045 return tools::id_cast<icol, std_vector_column_ref<T> >(*col);
1049 icol* col = tools::find_named<icol>(m_cols,a_name);
1051 return tools::id_cast<icol, std_vector_column<T> >(*col);
1055 icol* col = tools::find_named<icol>(m_cols,a_name);
1057 return tools::id_cast<icol, column_string >(*col);
1061 if(m_cols.empty())
return false;
1073 if(!(*it)->fetch_entry()) status =
false;
1079 tools_vforit(
icol*,m_cols,it) {
if(!(*it)->set_basket_size(a_size))
return false;}