4 #ifndef tools_rcsv_ntuple
5 #define tools_rcsv_ntuple
54 if(!
m_sep)
return false;
78 return find_named<read::icol>(
m_cols,a_name);
81 virtual const std::vector<read::icol*>&
columns()
const {
return m_cols;}
86 if(!
m_sep) {a_value = 0;
return false;}
91 while(
self.
next()) {
self.m_rows++;}
102 static const T s_v = T();
103 return 200+
_cid(s_v);
107 if(
void* p = cmp_cast<column>(
this,a_class)) {
return p;}
123 column(
const std::string& a_name,T* a_user_var = 0)
154 static const std::string&
s_class() {
155 static const std::string s_v(
"tools::rcsv::ntuple");
170 mem::increment(
s_class().c_str());
174 safe_clear<read::icol>(
m_cols);
176 mem::decrement(
s_class().c_str());
191 mem::increment(
s_class().c_str());
238 std::istream& a_reader,
bool a_hippo,
245 std::streampos sz = a_reader.tellg();
246 a_reader.seekg(0,std::ios::beg);
248 a_out <<
"tools::rcsv::ntuple::find_sep :"
249 <<
" stream is empty."
254 if(a_verbose) a_out <<
"file size " << sz << std::endl;
257 if(!
skip_line(a_reader,sz)) {a_sep = 0;
return false;}
258 if(!
skip_line(a_reader,sz)) {a_sep = 0;
return false;}
262 if(a_reader.tellg()>=sz) {a_sep=0;
return false;}
268 if(a_reader.tellg()>=sz)
break;
270 if((c==
CR())||(c==
LF()))
break;
274 a_out <<
"tools::rcsv::ntuple::find_set :"
275 <<
" first datat line is empty."
280 if(a_verbose) a_out <<
"first data line \"" << sfirst <<
"\"" << std::endl;
283 std::istringstream strm(sfirst.c_str());
286 std::streampos pos = strm.tellg();
287 if(pos==std::streampos(-1)) {
288 a_out <<
"tools::rcsv::ntuple::find_sep :"
289 <<
" first line does not start with a number."
294 if(a_verbose) a_out <<
"first number " << d
295 <<
" ending at pos " << pos << std::endl;
296 if(pos>=(std::streampos)sfirst.size()) {
297 a_out <<
"tools::rcsv::ntuple::find_sep :"
298 <<
" no separator found in first line."
300 <<
" sfirst.size() " << sfirst.size()
314 const std::string& a_suffix =
"x",
315 bool a_verbose =
false) {
316 safe_clear<read::icol>(
m_cols);
321 if(a_suffix.empty()) {
322 a_out <<
"tools::rcsv::ntuple::initialize : expect a column suffix." << std::endl;
331 a_out <<
"tools::rcsv::ntuple::initialize :"
332 <<
" stream is empty."
336 if(a_verbose) a_out <<
"file size " <<
m_sz << std::endl;
338 std::vector<std::string> labels;
342 a_out <<
"tools::rcsv::ntuple::initialize : read_line() failed." << std::endl;
349 a_out <<
"tools::rcsv::ntuple::initialize : (2) read_line() failed." << std::endl;
354 words(s,
"\t",
false,labels);
359 a_out <<
"tools::rcsv::ntuple::initialize : tellg() >= sz." << std::endl;
371 if((c==
CR())||(c==
LF()))
break;
375 a_out <<
"tools::rcsv::ntuple::initialize :"
376 <<
" first datat line is empty."
382 if(a_verbose) a_out <<
"first data line \"" << sfirst <<
"\"" << std::endl;
388 std::istringstream strm(sfirst.c_str());
391 std::streampos pos = strm.tellg();
392 if(pos==std::streampos(-1)) {
393 a_out <<
"tools::rcsv::ntuple::initialize :"
394 <<
" first line does not start with a number."
400 if(a_verbose) a_out <<
"first number " << d <<
" ending at pos " << pos << std::endl;
401 if(pos>=(std::streampos)sfirst.size()) {
402 a_out <<
"tools::rcsv::ntuple::initialize :"
403 <<
" no separator found in first line."
411 if(a_verbose) a_out <<
"sep " << (int)
m_sep << std::endl;
417 std::vector<std::string> ws;
423 if(a_verbose) a_out <<
"words " << ws.size() << std::endl;
424 unsigned int index = 0;
425 std::vector<std::string>::iterator it;
426 for(it=ws.begin();it!=ws.end();++it,index++) {
427 if(a_verbose) a_out <<
"word " <<
sout(*it) <<
"" << std::endl;
448 std::string name = a_suffix;
449 if(!numas<uint64>(
m_cols.size(),name)){}
451 if(index>=labels.size()) {
452 a_out <<
"tools::rcsv::ntuple::initialize :"
453 <<
" warning : not enough labels."
456 name = labels[index];
460 if(to<double>(*it,d)) {
461 if(a_verbose) a_out <<
"number " << d << std::endl;
462 create_column<double>(name);
466 create_column<csv_time>(name);
468 std::vector<double> v;
469 std::string vec_sep;vec_sep +=
m_vec_sep;
470 if(snums<double>(*it,vec_sep,v)&&v.size()) {
471 create_column< std::vector<double> >(name);
473 create_column<std::string>(name);
478 size_t num =
m_cols.size();
480 a_out <<
"tools::rcsv::ntuple::initialize :"
494 #define TOOLS_RCSV_NTUPLE_IF_CID(a__name,a__type) \
495 if(a_id==column<a__type>::id_class()) {\
496 static const std::string s_v(#a__name);\
500 #define TOOLS_RCSV_NTUPLE_IF_VEC_CID(a__name,a__type) \
501 if(a_id==column< std::vector<a__type> >::id_class()) {\
502 static const std::string s_v(#a__name+std::string("[]"));\
521 static const std::string s_v(
"string");
526 static const std::string s_v(
"time");
544 else if(a_id==
column< std::vector<std::string> >::id_class()) {
545 static const std::string s_v(
"string[]");
549 #undef TOOLS_RCSV_NTUPLE_IF_CID
550 #undef TOOLS_RCSV_NTUPLE_IF_VEC_CID
553 static const std::string s_v(
"unknown");
560 a_out <<
"separator is '" <<
m_sep <<
"'" << std::endl;
562 a_out <<
"separator is " << (
unsigned int)
m_sep << std::endl;
564 a_out <<
"number of columns " <<
m_cols.size() << std::endl;
565 std::vector<read::icol*>::const_iterator it;
567 a_out <<
sout((*it)->name())
568 <<
" " <<
s_cid((*it)->id_cls())
573 typedef std::pair<std::string,std::string>
col_desc;
578 safe_clear<read::icol>(
m_cols);
589 a_out <<
"tools::rcsv::ntuple::initialize(booking) :"
590 <<
" stream is empty."
598 std::vector<col_desc> _cols;
605 const std::string& type = (*it).first;
606 const std::string& name = (*it).second;
608 #define TOOLS_RCSV_NTUPLE_CREATE_VEC_COL(a__name,a__type) \
609 if(type==(std::string(#a__name)+"[]")) {\
610 create_column< std::vector<a__type> >(name,a_bd.find_variable< std::vector<a__type> >(name));\
615 if(type==
"char") create_column<char>(name,a_bd.find_variable<
char>(name));
616 else if(type==
"short") create_column<short>(name,a_bd.find_variable<
short>(name));
617 else if(type==
"int") create_column<int>(name,a_bd.find_variable<
int>(name));
618 else if(type==
"float") create_column<float>(name,a_bd.find_variable<
float>(name));
619 else if(type==
"double") create_column<double>(name,a_bd.find_variable<
double>(name));
620 else if(type==
"string") create_column<std::string>(name,a_bd.find_variable<std::string>(name));
622 else if(type==
"uchar") create_column<unsigned char>(name,a_bd.find_variable<
unsigned char>(name));
623 else if(type==
"ushort") create_column<unsigned short>(name,a_bd.find_variable<
unsigned short>(name));
624 else if(type==
"uint") create_column<uint32>(name,a_bd.find_variable<
uint32>(name));
625 else if(type==
"bool") create_column<bool>(name,a_bd.find_variable<
bool>(name));
626 else if(type==
"int64") create_column<int64>(name,a_bd.find_variable<
int64>(name));
627 else if(type==
"uint64") create_column<uint64>(name,a_bd.find_variable<
uint64>(name));
635 else if(type==
"string[]") create_column< std::vector<std::string> >(name,a_bd.find_variable< std::vector<std::string> >(name));
645 a_out <<
"tools::rcsv::ntuple::initialize(booking) :"
646 <<
" unhandled column type " <<
sout(type)
648 safe_clear<read::icol>(
m_cols);
656 #undef TOOLS_RCSV_NTUPLE_CREATE_VEC_COL
660 size_t num =
m_cols.size();
662 a_out <<
"tools::rcsv::ntuple::initialize(booking) :"
678 std::vector<col_desc> _cols;
688 if(!(*it)->fetch_entry()) status =
false;
695 std::string& a_title,
char& a_sep,
char& a_vec_sep,std::vector<col_desc>& a_cols) {
703 std::streampos sz = a_reader.tellg();
704 a_reader.seekg(0,std::ios::beg);
706 a_out <<
"tools::rcsv::ntuple::read_commented_header :"
707 <<
" stream is empty."
716 if(a_reader.tellg()>=sz)
break;
725 std::vector<std::string> _words;
728 a_out <<
"tools::rcsv::ntuple::read_commented_header :"
729 <<
" syntax error : empty header line."
733 if((_words[0]==
"#class")) {
734 if(_words.size()!=2) {
735 a_out <<
"tools::rcsv::ntuple::read_commented_header :"
736 <<
" syntax error in " <<
sout(
line)
741 }
else if(_words[0]==
"#title") {
742 if(_words.size()<1) {
743 a_out <<
"tools::rcsv::ntuple::read_commented_header :"
744 <<
" syntax error in " <<
sout(
line)
748 if(_words.size()==1) {
751 std::string::size_type pos =
line.find(_words[0]);
752 pos += _words[0].size()+1;
753 a_title =
line.substr(pos,
line.size()-pos);
755 }
else if((_words[0]==
"#separator")) {
756 if(_words.size()!=2) {
757 a_out <<
"tools::rcsv::ntuple::read_commented_header :"
758 <<
" syntax error in " <<
sout(
line)
763 if(!
to(_words[1],uisep)) {
764 a_out <<
"tools::rcsv::ntuple::read_commented_header :"
765 <<
" syntax error in " <<
sout(
line)
770 }
else if((_words[0]==
"#vector_separator")) {
771 if(_words.size()!=2) {
772 a_out <<
"tools::rcsv::ntuple::read_commented_header :"
773 <<
" syntax error in " <<
sout(
line)
778 if(!
to(_words[1],uisep)) {
779 a_out <<
"tools::rcsv::ntuple::read_commented_header :"
780 <<
" syntax error in " <<
sout(
line)
784 a_vec_sep = (char)uisep;
785 }
else if((_words[0]==
"#column")) {
786 if(_words.size()<2) {
787 a_out <<
"tools::rcsv::ntuple::read_commented_header :"
788 <<
" syntax error in " <<
sout(
line)
792 std::string
stype = _words[1];
794 if(_words.size()==2) {
797 std::string::size_type pos =
line.find(_words[1]);
798 pos += _words[1].size()+1;
799 label =
line.substr(pos,
line.size()-pos);
804 a_out <<
"tools::rcsv::ntuple::read_commented_header :"
805 <<
" syntax error in " <<
sout(
line)
806 <<
", unknown keyword " <<
sout(_words[0])
824 if(find_named<read::icol>(
m_cols,a_name))
return 0;
832 static bool read_line(std::istream& a_reader,std::streampos a_sz,std::string& a_s){
836 if(a_reader.tellg()>=a_sz) {a_s.clear();
return false;}
838 if(c==
CR())
continue;
845 static bool skip_line(std::istream& a_reader,std::streampos a_sz){
848 if(a_reader.tellg()>=a_sz)
return false;
857 if(a_reader.tellg()>=a_sz)
return false;
871 static bool _read(std::istream& a_reader,std::streampos,
char,T& a_v) {
873 if(a_reader.tellg()==std::streampos(-1)) {a_v = 0;
return false;}
877 static bool _read_time(std::istream& a_reader,std::streampos a_sz,
char a_sep,time_t& a_v) {
881 if(a_reader.tellg()>=a_sz)
break;
883 if((c==a_sep)||(c==
CR())||(c==
LF())) {
889 if(!
s2time(s,a_v))
return false;
892 static bool _read(std::istream& a_reader,std::streampos a_sz,
char a_sep,std::string& a_v) {
896 if(a_reader.tellg()>=a_sz)
break;
898 if((c==a_sep)||(c==
CR())||(c==
LF())) {
907 static bool _vec_read(std::istream& a_reader,std::streampos a_sz,
908 std::istringstream&,std::vector<std::string>&,
909 char a_sep,
const std::string& a_vec_sep,
910 std::vector<std::string>& a_v) {
912 if(!
_read(a_reader,a_sz,a_sep,_s))
return false;
914 words(_s,a_vec_sep,
true,a_v);
920 static bool _vec_read(std::istream& a_reader,std::streampos a_sz,
921 std::istringstream& a_iss,std::vector<std::string>& a_tmp,
922 char a_sep,
const std::string& a_vec_sep,
923 std::vector<T>& a_v) {
925 if(!
_read(a_reader,a_sz,a_sep,_s))
return false;
926 if(!snums<T>(_s,a_iss,a_tmp,a_vec_sep,a_v))
return false;
940 typedef std::string string_t;
966 std::string vec_sep;vec_sep +=
m_vec_sep;
967 std::istringstream iss;
968 std::vector<std::string> tmp;
971 size_t num =
m_cols.size();
972 std::vector<icol_t*>::const_iterator it;
975 #define TOOLS_RCSV_NTUPLE_IF_COL(a__type) \
976 if(col_##a__type* _col_##a__type = id_cast<icol_t,col_##a__type>(*(*it))) {\
978 if(!_read(m_reader,m_sz,m_sep,v)) return false;\
979 _col_##a__type->set_value(v);\
982 #define TOOLS_RCSV_NTUPLE_IF_VEC_COL(a__type) \
983 if(col_vec_##a__type* _col_vec_##a__type = id_cast<icol_t,col_vec_##a__type>(*(*it))) {\
984 std::vector<a__type> v;\
985 if(!_vec_read(m_reader,m_sz,iss,tmp,m_sep,vec_sep,v)) return false;\
986 _col_vec_##a__type->set_value(v);\
1003 else if(col_time* _col_time = id_cast<icol_t,col_time>(*(*it))) {
1007 _col_time->set_value(ct);
1024 #undef TOOLS_RCSV_NTUPLE_IF_COL
1025 #undef TOOLS_RCSV_NTUPLE_IF_VEC_COL
1032 if(index==(num-1)) {
1069 static const std::string s_v(
"tools::rcsv::fntuple");
1080 :
read::intuple(a_from)
1096 const std::string& a_suffix =
"x",
1097 bool a_verbose =
false) {
1101 a_out <<
"tools::rcsv::fntuple::initialize :"
1102 <<
" can't open " <<
m_file <<
"."