g4tools  5.4.0
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
tools::rcsv::ntuple Class Reference
Inheritance diagram for tools::rcsv::ntuple:
Inheritance graph
[legend]
Collaboration diagram for tools::rcsv::ntuple:
Collaboration graph
[legend]

Classes

class  column
 

Public Types

typedef std::pair< std::string, std::string > col_desc
 

Public Member Functions

virtual void start ()
 
virtual bool next ()
 
virtual read::icolfind_icol (const std::string &a_name)
 
virtual const std::vector< read::icol * > & columns () const
 
virtual const std::string & title () const
 
virtual bool number_of_entries (tools::uint64 &a_value) const
 
 ntuple (std::istream &a_reader)
 
virtual ~ntuple ()
 
void set_vec_sep (char a_c)
 
void set_sep (char a_c)
 
void set_hippo (bool a_hippo)
 
std::istream & istrm ()
 
bool initialize (std::ostream &a_out, char a_sep=0, const std::string &a_suffix="x", bool a_verbose=false)
 
void dump_columns (std::ostream &a_out) const
 
bool initialize (std::ostream &a_out, const ntuple_binding &a_bd=ntuple_binding())
 
bool initialize_from_commented_header (std::ostream &a_out)
 
bool get_row () const
 
- Public Member Functions inherited from tools::read::intuple
virtual ~intuple ()
 
virtual void stop ()
 
size_t number_of_columns () const
 
void column_names (std::vector< std::string > &a_names) const
 
icolfind_icol_case_insensitive (const std::string &a_name)
 
template<class T >
icolumn< T > * find_column (const std::string &a_name)
 
template<class T >
bool find_column (const std::string &a_name, icolumn< T > *&a_col, bool a_case_sensitive=true)
 
template<class T >
bool column_is_of_type (const std::string &a_name, bool &a_is, bool a_case_sensitive=true)
 
template<class T >
bool column_min (unsigned int a_col, T &a_value)
 
template<class T >
bool column_max (unsigned int a_col, T &a_value)
 

Static Public Member Functions

static bool find_sep (std::ostream &a_out, std::istream &a_reader, bool a_hippo, bool a_verbose, char &a_sep)
 
static const std::string & s_cid (cid a_id)
 
- Static Public Member Functions inherited from tools::read::intuple
static const std::string & s_class ()
 

Protected Member Functions

 ntuple (const ntuple &a_from)
 
ntupleoperator= (const ntuple &a_from)
 
bool read_commented_header (std::ostream &a_out, std::istream &a_reader, std::string &a_title, char &a_sep, char &a_vec_sep, std::vector< col_desc > &a_cols)
 
template<class T >
column< T > * create_column (const std::string &a_name, T *a_user_var=0)
 
bool _read_line ()
 

Static Protected Member Functions

static bool read_line (std::istream &a_reader, std::streampos a_sz, std::string &a_s)
 
static bool skip_line (std::istream &a_reader, std::streampos a_sz)
 
static bool skip_comment (std::istream &a_reader, std::streampos a_sz)
 
template<class T >
static bool _read (std::istream &a_reader, std::streampos, char, T &a_v)
 
static bool _read_time (std::istream &a_reader, std::streampos a_sz, char a_sep, time_t &a_v)
 
static bool _read (std::istream &a_reader, std::streampos a_sz, char a_sep, std::string &a_v)
 
static bool _vec_read (std::istream &a_reader, std::streampos a_sz, std::istringstream &, std::vector< std::string > &, char a_sep, const std::string &a_vec_sep, std::vector< std::string > &a_v)
 
template<class T >
static bool _vec_read (std::istream &a_reader, std::streampos a_sz, std::istringstream &a_iss, std::vector< std::string > &a_tmp, char a_sep, const std::string &a_vec_sep, std::vector< T > &a_v)
 

Protected Attributes

std::istream & m_reader
 
std::string m_title
 
char m_sep
 
char m_vec_sep
 
std::vector< read::icol * > m_cols
 
std::streampos m_sz
 
int m_rows
 
bool m_hippo
 

Detailed Description

Definition at line 42 of file rcsv_ntuple.

Member Typedef Documentation

◆ col_desc

typedef std::pair<std::string,std::string> tools::rcsv::ntuple::col_desc

Definition at line 573 of file rcsv_ntuple.

Constructor & Destructor Documentation

◆ ntuple() [1/2]

tools::rcsv::ntuple::ntuple ( std::istream &  a_reader)
inline

Definition at line 160 of file rcsv_ntuple.

161  :m_reader(a_reader)
162  ,m_title()
163  ,m_sep(0)
164  ,m_vec_sep(';')
165  ,m_sz(0)
166  ,m_rows(-1)
167  ,m_hippo(false)
168  {
169 #ifdef TOOLS_MEM
170  mem::increment(s_class().c_str());
171 #endif
172  }

◆ ~ntuple()

virtual tools::rcsv::ntuple::~ntuple ( )
inlinevirtual

Definition at line 173 of file rcsv_ntuple.

173  {
174  safe_clear<read::icol>(m_cols);
175 #ifdef TOOLS_MEM
176  mem::decrement(s_class().c_str());
177 #endif
178  }

◆ ntuple() [2/2]

tools::rcsv::ntuple::ntuple ( const ntuple a_from)
inlineprotected

Definition at line 180 of file rcsv_ntuple.

181  :parent(a_from)
182  ,m_reader(a_from.m_reader)
183  ,m_title(a_from.m_title)
184  ,m_sep(a_from.m_sep)
185  ,m_vec_sep(a_from.m_vec_sep)
186  ,m_sz(a_from.m_sz)
187  ,m_rows(-1)
188  ,m_hippo(a_from.m_hippo)
189  {
190 #ifdef TOOLS_MEM
191  mem::increment(s_class().c_str());
192 #endif
193  }

Member Function Documentation

◆ _read() [1/2]

static bool tools::rcsv::ntuple::_read ( std::istream &  a_reader,
std::streampos  a_sz,
char  a_sep,
std::string &  a_v 
)
inlinestaticprotected

Definition at line 892 of file rcsv_ntuple.

892  {
893  a_v.clear();
894  char c;
895  while(true){
896  if(a_reader.tellg()>=a_sz) break;
897  a_reader.get(c);
898  if((c==a_sep)||(c==CR())||(c==LF())) {
899  a_reader.putback(c);
900  break;
901  }
902  a_v += c;
903  }
904  return true;
905  }

◆ _read() [2/2]

template<class T >
static bool tools::rcsv::ntuple::_read ( std::istream &  a_reader,
std::streampos  ,
char  ,
T &  a_v 
)
inlinestaticprotected

Definition at line 871 of file rcsv_ntuple.

871  {
872  a_reader >> a_v;
873  if(a_reader.tellg()==std::streampos(-1)) {a_v = 0;return false;}
874  //std::cout << "debug : _read(double) " << a_v << std::endl;
875  return true;
876  }

◆ _read_line()

bool tools::rcsv::ntuple::_read_line ( )
inlineprotected

Definition at line 931 of file rcsv_ntuple.

931  {
932  // have to loop on all columns !
933  typedef read::icol icol_t;
934 
935  typedef ntuple::column<char> col_char;
936  typedef ntuple::column<short> col_short;
937  typedef ntuple::column<int> col_int;
938  typedef ntuple::column<float> col_float;
939  typedef ntuple::column<double> col_double;
940  typedef std::string string_t;
941  typedef ntuple::column<string_t> col_string_t;
942 
943  typedef ntuple::column<uchar> col_uchar;
944  typedef ntuple::column<ushort> col_ushort;
945  typedef ntuple::column<uint32> col_uint32;
946  typedef ntuple::column<bool> col_bool;
947  typedef ntuple::column<int64> col_int64;
948  typedef ntuple::column<uint64> col_uint64;
949 
950  typedef ntuple::column<csv_time> col_time;
951 
952  typedef ntuple::column< std::vector<char> > col_vec_char;
953  typedef ntuple::column< std::vector<short> > col_vec_short;
954  typedef ntuple::column< std::vector<int32> > col_vec_int;
955  typedef ntuple::column< std::vector<float> > col_vec_float;
956  typedef ntuple::column< std::vector<double> > col_vec_double;
957  typedef ntuple::column< std::vector<std::string> > col_vec_string_t;
958 
959  typedef ntuple::column< std::vector<uchar> > col_vec_uchar;
960  typedef ntuple::column< std::vector<ushort> > col_vec_ushort;
961  typedef ntuple::column< std::vector<uint32> > col_vec_uint32;
962  typedef ntuple::column< std::vector<bool> > col_vec_bool;
963  typedef ntuple::column< std::vector<int64> > col_vec_int64;
964  typedef ntuple::column< std::vector<uint64> > col_vec_uint64;
965 
966  std::string vec_sep;vec_sep += m_vec_sep;
967  std::istringstream iss;
968  std::vector<std::string> tmp;
969 
970  size_t index = 0;
971  size_t num = m_cols.size();
972  std::vector<icol_t*>::const_iterator it;
973  for(it=m_cols.begin();it!=m_cols.end();++it,index++) {
974 
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))) {\
977  a__type v;\
978  if(!_read(m_reader,m_sz,m_sep,v)) return false;\
979  _col_##a__type->set_value(v);\
980  }
981 
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);\
987  }
988 
990  else TOOLS_RCSV_NTUPLE_IF_COL(short)
991  else TOOLS_RCSV_NTUPLE_IF_COL(int)
992  else TOOLS_RCSV_NTUPLE_IF_COL(float)
993  else TOOLS_RCSV_NTUPLE_IF_COL(double)
994  else TOOLS_RCSV_NTUPLE_IF_COL(string_t)
995 
999  else TOOLS_RCSV_NTUPLE_IF_COL(bool)
1002 
1003  else if(col_time* _col_time = id_cast<icol_t,col_time>(*(*it))) {
1004  time_t v;
1005  if(!_read_time(m_reader,m_sz,m_sep,v)) return false;
1006  csv_time ct;ct.m_l = long(v);
1007  _col_time->set_value(ct);
1008  }
1009 
1010  else TOOLS_RCSV_NTUPLE_IF_VEC_COL(char)
1011  else TOOLS_RCSV_NTUPLE_IF_VEC_COL(short)
1013  else TOOLS_RCSV_NTUPLE_IF_VEC_COL(float)
1014  else TOOLS_RCSV_NTUPLE_IF_VEC_COL(double)
1015  else TOOLS_RCSV_NTUPLE_IF_VEC_COL(string_t)
1016 
1020  else TOOLS_RCSV_NTUPLE_IF_VEC_COL(bool)
1023 
1024 #undef TOOLS_RCSV_NTUPLE_IF_COL
1025 #undef TOOLS_RCSV_NTUPLE_IF_VEC_COL
1026 
1027  else {
1028  //std::cout << "column cast failed." << std::endl;
1029  return false;
1030  }
1031 
1032  if(index==(num-1)) { //read up to LF()
1033  char c;
1034  while(true){
1035  if(m_reader.tellg()>=m_sz) break;
1036  m_reader.get(c);
1037  if(c==LF()) break;
1038  }
1039  } else { //read sep :
1040  char sep;
1041  m_reader.get(sep);
1042  }
1043  }
1044  return true;
1045  }

◆ _read_time()

static bool tools::rcsv::ntuple::_read_time ( std::istream &  a_reader,
std::streampos  a_sz,
char  a_sep,
time_t &  a_v 
)
inlinestaticprotected

Definition at line 877 of file rcsv_ntuple.

877  {
878  std::string s;
879  char c;
880  while(true){
881  if(a_reader.tellg()>=a_sz) break;
882  a_reader.get(c);
883  if((c==a_sep)||(c==CR())||(c==LF())) {
884  a_reader.putback(c);
885  break;
886  }
887  s += c;
888  }
889  if(!s2time(s,a_v)) return false;
890  return true;
891  }

◆ _vec_read() [1/2]

static bool tools::rcsv::ntuple::_vec_read ( std::istream &  a_reader,
std::streampos  a_sz,
std::istringstream &  ,
std::vector< std::string > &  ,
char  a_sep,
const std::string &  a_vec_sep,
std::vector< std::string > &  a_v 
)
inlinestaticprotected

Definition at line 907 of file rcsv_ntuple.

910  {
911  std::string _s;
912  if(!_read(a_reader,a_sz,a_sep,_s)) return false;
913  //replace(_s,"\\"+a_vec_sep,"@@");
914  words(_s,a_vec_sep,true,a_v);
915  //tools_vforit(std::string,a_v,it) replace(*it,"@@",a_vec_sep);
916  return true;
917  }

◆ _vec_read() [2/2]

template<class T >
static bool tools::rcsv::ntuple::_vec_read ( std::istream &  a_reader,
std::streampos  a_sz,
std::istringstream &  a_iss,
std::vector< std::string > &  a_tmp,
char  a_sep,
const std::string &  a_vec_sep,
std::vector< T > &  a_v 
)
inlinestaticprotected

Definition at line 920 of file rcsv_ntuple.

923  {
924  std::string _s;
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;
927  return true;
928  }

◆ columns()

virtual const std::vector<read::icol*>& tools::rcsv::ntuple::columns ( ) const
inlinevirtual

Implements tools::read::intuple.

Definition at line 81 of file rcsv_ntuple.

81 {return m_cols;}

◆ create_column()

template<class T >
column<T>* tools::rcsv::ntuple::create_column ( const std::string &  a_name,
T *  a_user_var = 0 
)
inlineprotected

Definition at line 823 of file rcsv_ntuple.

823  {
824  if(find_named<read::icol>(m_cols,a_name)) return 0;
825  column<T>* col = new column<T>(a_name,a_user_var);
826  if(!col) return 0;
827  m_cols.push_back(col);
828  return col;
829  }

◆ dump_columns()

void tools::rcsv::ntuple::dump_columns ( std::ostream &  a_out) const
inline

Definition at line 558 of file rcsv_ntuple.

558  {
559  if((m_sep>=32)&&(m_sep<=126)) { //printable
560  a_out << "separator is '" << m_sep << "'" << std::endl;
561  } else {
562  a_out << "separator is " << (unsigned int)m_sep << std::endl;
563  }
564  a_out << "number of columns " << m_cols.size() << std::endl;
565  std::vector<read::icol*>::const_iterator it;
566  for(it=m_cols.begin();it!=m_cols.end();++it) {
567  a_out << sout((*it)->name())
568  << " " << s_cid((*it)->id_cls())
569  << std::endl;
570  }
571  }

◆ find_icol()

virtual read::icol* tools::rcsv::ntuple::find_icol ( const std::string &  a_name)
inlinevirtual

Implements tools::read::intuple.

Definition at line 77 of file rcsv_ntuple.

77  {
78  return find_named<read::icol>(m_cols,a_name);
79  }

◆ find_sep()

static bool tools::rcsv::ntuple::find_sep ( std::ostream &  a_out,
std::istream &  a_reader,
bool  a_hippo,
bool  a_verbose,
char &  a_sep 
)
inlinestatic

Definition at line 237 of file rcsv_ntuple.

240  {
241  // analyse first data line to find the char separator.
242 
243  a_reader.clear();
244  a_reader.seekg(0,std::ios::end);
245  std::streampos sz = a_reader.tellg();
246  a_reader.seekg(0,std::ios::beg);
247  if(!sz) {
248  a_out << "tools::rcsv::ntuple::find_sep :"
249  << " stream is empty."
250  << std::endl;
251  a_sep = 0;
252  return false;
253  } //file empty.
254  if(a_verbose) a_out << "file size " << sz << std::endl;
255 
256  if(a_hippo) { //skip first two lines :
257  if(!skip_line(a_reader,sz)) {a_sep = 0;return false;}
258  if(!skip_line(a_reader,sz)) {a_sep = 0;return false;}
259  } else {
260  while(skip_comment(a_reader,sz)){}
261  }
262  if(a_reader.tellg()>=sz) {a_sep=0;return false;} //no data line.
263 
264  // get first data line :
265  std::string sfirst;
266  {char c;
267  while(true) {
268  if(a_reader.tellg()>=sz) break;
269  a_reader.get(c);
270  if((c==CR())||(c==LF())) break;
271  sfirst += c;
272  }}
273  if(sfirst.empty()) {
274  a_out << "tools::rcsv::ntuple::find_set :"
275  << " first datat line is empty."
276  << std::endl;
277  a_sep = 0;
278  return false;
279  }
280  if(a_verbose) a_out << "first data line \"" << sfirst << "\"" << std::endl;
281 
282  //guess sep from first data line :
283  std::istringstream strm(sfirst.c_str());
284  double d;
285  strm >> d;
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."
290  << std::endl;
291  a_sep = 0;
292  return false;
293  } //not 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."
299  << " pos " << pos
300  << " sfirst.size() " << sfirst.size()
301  << std::endl;
302  a_sep = 0;
303  return false;
304  } //no sep.
305 
306  strm.get(a_sep);
307 
308  return true;
309  }

◆ get_row()

bool tools::rcsv::ntuple::get_row ( ) const
inline

Definition at line 685 of file rcsv_ntuple.

685  {
686  bool status = true;
687  tools_vforcit(read::icol*,m_cols,it) {
688  if(!(*it)->fetch_entry()) status = false;
689  }
690  return status;
691  }

◆ initialize() [1/2]

bool tools::rcsv::ntuple::initialize ( std::ostream &  a_out,
char  a_sep = 0,
const std::string &  a_suffix = "x",
bool  a_verbose = false 
)
inline

Definition at line 312 of file rcsv_ntuple.

315  {
316  safe_clear<read::icol>(m_cols);
317  m_sep = 0;
318  m_sz = 0;
319  m_rows = -1;
320 
321  if(a_suffix.empty()) {
322  a_out << "tools::rcsv::ntuple::initialize : expect a column suffix." << std::endl;
323  return false;
324  }
325 
326  m_reader.clear();
327  m_reader.seekg(0,std::ios::end);
328  m_sz = m_reader.tellg();
329  m_reader.seekg(0,std::ios::beg);
330  if(!m_sz) {
331  a_out << "tools::rcsv::ntuple::initialize :"
332  << " stream is empty."
333  << std::endl;
334  return false; //file empty.
335  }
336  if(a_verbose) a_out << "file size " << m_sz << std::endl;
337 
338  std::vector<std::string> labels;
339  if(m_hippo) { //skip first two lines :
340  std::string _title;
341  if(!read_line(m_reader,m_sz,_title)) {
342  a_out << "tools::rcsv::ntuple::initialize : read_line() failed." << std::endl;
343  m_sz = 0;
344  m_rows = -1;
345  return false;
346  }
347  std::string s;
348  if(!read_line(m_reader,m_sz,s)) {
349  a_out << "tools::rcsv::ntuple::initialize : (2) read_line() failed." << std::endl;
350  m_sz = 0;
351  m_rows = -1;
352  return false;
353  }
354  words(s,"\t",false,labels); //false for glast.tnt that has a trailing \t.
355  } else {
356  while(skip_comment(m_reader,m_sz)){}
357  }
358  if(m_reader.tellg()>=m_sz) {
359  a_out << "tools::rcsv::ntuple::initialize : tellg() >= sz." << std::endl;
360  m_sz = 0;
361  m_rows = -1;
362  return false;
363  }
364 
365  // get first data line :
366  std::string sfirst;
367  {{char c;
368  while(true) {
369  if(m_reader.tellg()>=m_sz) break;
370  m_reader.get(c);
371  if((c==CR())||(c==LF())) break;
372  sfirst += c;
373  }}
374  if(sfirst.empty()) {
375  a_out << "tools::rcsv::ntuple::initialize :"
376  << " first datat line is empty."
377  << std::endl;
378  m_sz = 0;
379  m_rows = -1;
380  return false;
381  }}
382  if(a_verbose) a_out << "first data line \"" << sfirst << "\"" << std::endl;
383 
384  if(a_sep) {
385  m_sep = a_sep;
386  } else {
387  //guess sep from first data line :
388  std::istringstream strm(sfirst.c_str());
389  double d;
390  strm >> d;
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."
395  << std::endl;
396  m_sz = 0;
397  m_rows = -1;
398  return false;
399  }
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."
404  << std::endl;
405  m_sz = 0;
406  m_rows = -1;
407  return false;
408  }
409  strm.get(m_sep);
410  }
411  if(a_verbose) a_out << "sep " << (int)m_sep << std::endl;
412 
413  // in case sep is ' ', there is an ambiguity with some leading
414  // space in front of first number.
415  if(m_sep==' ') strip(sfirst,leading,' ');
416 
417  std::vector<std::string> ws;
418  {std::string sep;
419  sep += m_sep;
420  words(sfirst,sep,m_hippo?false:true,ws);}
421 
422  // look if words are numbers :
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;
428 /* with glast.tnt there is trailing \t that will induce an extra empty column.
429  if((*it).empty()) {
430  // do not accept :
431  // <num><sep><num><sep><sep><num>...
432  // but accept a trailing <sep> (glast.tnt) :
433  // <num><sep><num>....<sep><num><sep>
434  if(index==(ws.size()-1)) {
435  break;
436  } else {
437  a_out << "tools::rcsv::ntuple::initialize :"
438  << " empty word."
439  << std::endl;
440  safe_clear<read::icol>(m_cols);
441  m_sep = 0;
442  m_sz = 0;
443  m_rows = -1;
444  return false;
445  }
446  }
447 */
448  std::string name = a_suffix;
449  if(!numas<uint64>(m_cols.size(),name)){}
450  if(m_hippo) {
451  if(index>=labels.size()) {
452  a_out << "tools::rcsv::ntuple::initialize :"
453  << " warning : not enough labels."
454  << std::endl;
455  } else {
456  name = labels[index];
457  }
458  }
459  double d;
460  if(to<double>(*it,d)) {
461  if(a_verbose) a_out << "number " << d << std::endl;
462  create_column<double>(name);
463  } else {
464  time_t time;
465  if(s2time(*it,time)) {
466  create_column<csv_time>(name);
467  } else {
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);
472  } else {
473  create_column<std::string>(name);
474  }
475  }
476  }
477  }
478  size_t num = m_cols.size();
479  if(!num) {
480  a_out << "tools::rcsv::ntuple::initialize :"
481  << " zero columns."
482  << std::endl;
483  m_sep = 0;
484  m_sz = 0;
485  m_rows = -1;
486  return false;
487  }
488 
489  return true;
490  }

◆ initialize() [2/2]

bool tools::rcsv::ntuple::initialize ( std::ostream &  a_out,
const ntuple_binding a_bd = ntuple_binding() 
)
inline

Definition at line 575 of file rcsv_ntuple.

575  {
576  // it assumes a "commented header".
577 
578  safe_clear<read::icol>(m_cols);
579  m_sep = 0;
580  m_sz = 0;
581  m_rows = -1;
582  m_hippo = false;
583 
584  m_reader.clear();
585  m_reader.seekg(0,std::ios::end);
586  m_sz = m_reader.tellg();
587  m_reader.seekg(0,std::ios::beg);
588  if(!m_sz) {
589  a_out << "tools::rcsv::ntuple::initialize(booking) :"
590  << " stream is empty."
591  << std::endl;
592  return false; //file empty.
593  }
594  //if(a_verbose) a_out << "file size " << m_sz << std::endl;
595 
596  std::string _title;
597  char _sep,_vec_sep;
598  std::vector<col_desc> _cols;
599  if(!read_commented_header(a_out,m_reader,_title,_sep,_vec_sep,_cols)) return false;
600 
601  m_sep = _sep;
602  m_title = _title;
603 
604  tools_vforcit(col_desc,_cols,it) {
605  const std::string& type = (*it).first;
606  const std::string& name = (*it).second;
607 
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));\
611  }
612 
613  // see cid2s() for string types.
614 
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));
621 
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)); //WARNING
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));
628 
629  else TOOLS_RCSV_NTUPLE_CREATE_VEC_COL(char,char)
630  else TOOLS_RCSV_NTUPLE_CREATE_VEC_COL(short,short)
632  else TOOLS_RCSV_NTUPLE_CREATE_VEC_COL(float,float)
633  else TOOLS_RCSV_NTUPLE_CREATE_VEC_COL(double,double)
634 
635  else if(type=="string[]") create_column< std::vector<std::string> >(name,a_bd.find_variable< std::vector<std::string> >(name));
636 
639  else TOOLS_RCSV_NTUPLE_CREATE_VEC_COL(uint,uint32) //WARNING
640  else TOOLS_RCSV_NTUPLE_CREATE_VEC_COL(bool,bool)
643 
644  else {
645  a_out << "tools::rcsv::ntuple::initialize(booking) :"
646  << " unhandled column type " << sout(type)
647  << std::endl;
648  safe_clear<read::icol>(m_cols);
649  m_sep = 0;
650  m_sz = 0;
651  m_rows = -1;
652  m_hippo = false;
653  return false;
654  }
655 
656 #undef TOOLS_RCSV_NTUPLE_CREATE_VEC_COL
657 
658  }
659 
660  size_t num = m_cols.size();
661  if(!num) {
662  a_out << "tools::rcsv::ntuple::initialize(booking) :"
663  << " zero columns."
664  << std::endl;
665  return false;
666  }
667 
668  //a_out << "tools::rroot::ntuple::initialize :"
669  // << " number of columns " << num << "."
670  // << std::endl;
671 
672  return true;
673  }

◆ initialize_from_commented_header()

bool tools::rcsv::ntuple::initialize_from_commented_header ( std::ostream &  a_out)
inline

Definition at line 675 of file rcsv_ntuple.

675  { // it assumes a "commented header".
676  std::string _title;
677  char _sep,_vec_sep;
678  std::vector<col_desc> _cols;
679  if(!read_commented_header(a_out,m_reader,_title,_sep,_vec_sep,_cols)) return false;
680  ntuple_binding nbd;
681  {tools_vforcit(col_desc,_cols,it) nbd.add_column_no_var((*it).second);} //user_var is 0.
682  return initialize(a_out,nbd);
683  }

◆ istrm()

std::istream& tools::rcsv::ntuple::istrm ( )
inline

Definition at line 207 of file rcsv_ntuple.

207 {return m_reader;}

◆ next()

virtual bool tools::rcsv::ntuple::next ( )
inlinevirtual

Implements tools::read::intuple.

Definition at line 53 of file rcsv_ntuple.

53  {
54  if(!m_sep) return false; //not inited.
55  if(m_reader.tellg()>=m_sz) return false;
56  // first time we are at bol but else we are at eol.
57  char c;
58  m_reader.get(c);
59  if(c==LF()){
60  if(m_reader.tellg()>=m_sz) {
61  //eof. Tell caller to stop looping on ntuple rows.
62  return false;
63  }
64  //eol. Next char read is going to be at bol.
65  } else {
66  m_reader.putback(c);
67  //bol
68  }
69  // ready for a new row :
70 
71  while(skip_comment(m_reader,m_sz)){}
72  if(m_reader.tellg()>=m_sz) return false;
73 
74  return _read_line();
75  }

◆ number_of_entries()

virtual bool tools::rcsv::ntuple::number_of_entries ( tools::uint64 a_value) const
inlinevirtual

Implements tools::read::intuple.

Definition at line 85 of file rcsv_ntuple.

85  {
86  if(!m_sep) {a_value = 0;return false;} //not inited.
87  ntuple& self = const_cast<ntuple&>(*this);
88  if(m_rows==(-1)) {
89  self.m_rows = 0;
90  self.start();
91  while(self.next()) {self.m_rows++;}
92  }
93  a_value = (uint64)m_rows;
94  return true;
95  }

◆ operator=()

ntuple& tools::rcsv::ntuple::operator= ( const ntuple a_from)
inlineprotected

Definition at line 194 of file rcsv_ntuple.

194  {
195  m_title = a_from.m_title;
196  m_sep = a_from.m_sep;
197  m_vec_sep = a_from.m_vec_sep;
198  m_hippo = a_from.m_hippo;
199  m_rows = a_from.m_rows;
200  return *this;
201  }

◆ read_commented_header()

bool tools::rcsv::ntuple::read_commented_header ( std::ostream &  a_out,
std::istream &  a_reader,
std::string &  a_title,
char &  a_sep,
char &  a_vec_sep,
std::vector< col_desc > &  a_cols 
)
inlineprotected

Definition at line 694 of file rcsv_ntuple.

695  {
696  // analyse first lines starting with '#'.
697  a_title.clear();
698  a_sep = 0;
699  a_cols.clear();
700 
701  a_reader.clear();
702  a_reader.seekg(0,std::ios::end);
703  std::streampos sz = a_reader.tellg();
704  a_reader.seekg(0,std::ios::beg);
705  if(!sz) {
706  a_out << "tools::rcsv::ntuple::read_commented_header :"
707  << " stream is empty."
708  << std::endl;
709  return false;
710  } //file empty.
711 
712 
713  std::string _class;
714 
715  while(true) {
716  if(a_reader.tellg()>=sz) break;
717  //we should be at bol :
718  char c;
719  a_reader.get(c);
720  a_reader.putback(c);
721  if(c!='#') break; //finished, probably a data line now.
722  std::string line;
723  if(!read_line(a_reader,sz,line)) break; //or return false ?
724 
725  std::vector<std::string> _words;
726  words(line," ",false,_words);
727  if(!_words.size()) {
728  a_out << "tools::rcsv::ntuple::read_commented_header :"
729  << " syntax error : empty header line."
730  << std::endl;
731  return false;
732  }
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)
737  << std::endl;
738  return false;
739  }
740  _class = _words[1];
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)
745  << std::endl;
746  return false;
747  }
748  if(_words.size()==1) {
749  a_title.clear();
750  } else {
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);
754  }
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)
759  << std::endl;
760  return false;
761  }
762  unsigned int uisep;
763  if(!to(_words[1],uisep)) {
764  a_out << "tools::rcsv::ntuple::read_commented_header :"
765  << " syntax error in " << sout(line)
766  << std::endl;
767  return false;
768  }
769  a_sep = (char)uisep;
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)
774  << std::endl;
775  return false;
776  }
777  unsigned int uisep;
778  if(!to(_words[1],uisep)) {
779  a_out << "tools::rcsv::ntuple::read_commented_header :"
780  << " syntax error in " << sout(line)
781  << std::endl;
782  return false;
783  }
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)
789  << std::endl;
790  return false;
791  }
792  std::string stype = _words[1];
793  std::string label;
794  if(_words.size()==2) {
795  label.clear();
796  } else {
797  std::string::size_type pos = line.find(_words[1]);
798  pos += _words[1].size()+1;
799  label = line.substr(pos,line.size()-pos);
800  }
801  //a_out << "column " << stype << " " << sout(label) << std::endl;
802  a_cols.push_back(col_desc(stype,label));
803  } else {
804  a_out << "tools::rcsv::ntuple::read_commented_header :"
805  << " syntax error in " << sout(line)
806  << ", unknown keyword " << sout(_words[0])
807  << std::endl;
808  //return false;
809  }
810  }
811 
812 /*
813  a_out << "class " << _class << std::endl;
814  a_out << "title " << _title << std::endl;
815  a_out << "separator " << _separator << std::endl;
816 */
817 
818  return true;
819  }

◆ read_line()

static bool tools::rcsv::ntuple::read_line ( std::istream &  a_reader,
std::streampos  a_sz,
std::string &  a_s 
)
inlinestaticprotected

Definition at line 832 of file rcsv_ntuple.

832  {
833  a_s.clear();
834  char c;
835  while(true) {
836  if(a_reader.tellg()>=a_sz) {a_s.clear();return false;}
837  a_reader.get(c);
838  if(c==CR()) continue;
839  if(c==LF()) break; //eol.
840  a_s += c;
841  }
842  return true;
843  }

◆ s_cid()

static const std::string& tools::rcsv::ntuple::s_cid ( cid  a_id)
inlinestatic

Definition at line 492 of file rcsv_ntuple.

492  {
493 
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);\
497  return s_v;\
498  }
499 
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("[]"));\
503  return s_v;\
504  }
505 
506  TOOLS_RCSV_NTUPLE_IF_CID(char,char)
507  else TOOLS_RCSV_NTUPLE_IF_CID(short,short)
508  else TOOLS_RCSV_NTUPLE_IF_CID(int,int)
510 
511  else TOOLS_RCSV_NTUPLE_IF_CID(float,float)
512  else TOOLS_RCSV_NTUPLE_IF_CID(double,double)
513 
516  else TOOLS_RCSV_NTUPLE_IF_CID(uint,uint32) //WARNING
518 
519  else TOOLS_RCSV_NTUPLE_IF_CID(bool,bool)
520  else if(a_id==column<std::string>::id_class()) {
521  static const std::string s_v("string");
522  return s_v;
523  }
524 
525  else if(a_id==column<csv_time>::id_class()) {
526  static const std::string s_v("time");
527  return s_v;
528  }
529 
530  else TOOLS_RCSV_NTUPLE_IF_VEC_CID(char,char)
531  else TOOLS_RCSV_NTUPLE_IF_VEC_CID(short,short)
532  else TOOLS_RCSV_NTUPLE_IF_VEC_CID(int,int)
534 
535  else TOOLS_RCSV_NTUPLE_IF_VEC_CID(float,float)
536  else TOOLS_RCSV_NTUPLE_IF_VEC_CID(double,double)
537 
540  else TOOLS_RCSV_NTUPLE_IF_VEC_CID(uint,uint32) //WARNING
542 
543  else TOOLS_RCSV_NTUPLE_IF_VEC_CID(bool,bool)
544  else if(a_id==column< std::vector<std::string> >::id_class()) {
545  static const std::string s_v("string[]");
546  return s_v;
547  }
548 
549 #undef TOOLS_RCSV_NTUPLE_IF_CID
550 #undef TOOLS_RCSV_NTUPLE_IF_VEC_CID
551 
552  else {
553  static const std::string s_v("unknown");
554  return s_v;
555  }
556  }

◆ set_hippo()

void tools::rcsv::ntuple::set_hippo ( bool  a_hippo)
inline

Definition at line 205 of file rcsv_ntuple.

205 {m_hippo = a_hippo;}

◆ set_sep()

void tools::rcsv::ntuple::set_sep ( char  a_c)
inline

Definition at line 204 of file rcsv_ntuple.

204 {m_sep = a_c;}

◆ set_vec_sep()

void tools::rcsv::ntuple::set_vec_sep ( char  a_c)
inline

Definition at line 203 of file rcsv_ntuple.

203 {m_vec_sep = a_c;}

◆ skip_comment()

static bool tools::rcsv::ntuple::skip_comment ( std::istream &  a_reader,
std::streampos  a_sz 
)
inlinestaticprotected

Definition at line 855 of file rcsv_ntuple.

855  {
856  //ret true = we had a commented line, false : a data line or nothing.
857  if(a_reader.tellg()>=a_sz) return false;
858  //we should be at bol :
859  char c;
860  a_reader.get(c);
861  if(c=='#') {
862  return skip_line(a_reader,a_sz);
863  //eol. Next char should be bol.
864  } else {
865  a_reader.putback(c);
866  return false;
867  }
868  }

◆ skip_line()

static bool tools::rcsv::ntuple::skip_line ( std::istream &  a_reader,
std::streampos  a_sz 
)
inlinestaticprotected

Definition at line 845 of file rcsv_ntuple.

845  {
846  char c;
847  while(true) {
848  if(a_reader.tellg()>=a_sz) return false;
849  a_reader.get(c);
850  if(c==LF()) break;
851  }
852  return true;
853  }

◆ start()

virtual void tools::rcsv::ntuple::start ( )
inlinevirtual

Implements tools::read::intuple.

Definition at line 45 of file rcsv_ntuple.

45  {
46  m_reader.clear();
47  m_reader.seekg(0,std::ios::beg);
48  if(m_hippo) {
51  }
52  }

◆ title()

virtual const std::string& tools::rcsv::ntuple::title ( ) const
inlinevirtual

Implements tools::read::intuple.

Definition at line 83 of file rcsv_ntuple.

83 {return m_title;}

Member Data Documentation

◆ m_cols

std::vector<read::icol*> tools::rcsv::ntuple::m_cols
protected

Definition at line 1051 of file rcsv_ntuple.

◆ m_hippo

bool tools::rcsv::ntuple::m_hippo
protected

Definition at line 1054 of file rcsv_ntuple.

◆ m_reader

std::istream& tools::rcsv::ntuple::m_reader
protected

Definition at line 1047 of file rcsv_ntuple.

◆ m_rows

int tools::rcsv::ntuple::m_rows
protected

Definition at line 1053 of file rcsv_ntuple.

◆ m_sep

char tools::rcsv::ntuple::m_sep
protected

Definition at line 1049 of file rcsv_ntuple.

◆ m_sz

std::streampos tools::rcsv::ntuple::m_sz
protected

Definition at line 1052 of file rcsv_ntuple.

◆ m_title

std::string tools::rcsv::ntuple::m_title
protected

Definition at line 1048 of file rcsv_ntuple.

◆ m_vec_sep

char tools::rcsv::ntuple::m_vec_sep
protected

Definition at line 1050 of file rcsv_ntuple.


The documentation for this class was generated from the following file:
TOOLS_RCSV_NTUPLE_IF_VEC_COL
#define TOOLS_RCSV_NTUPLE_IF_VEC_COL(a__type)
tools::read::intuple::s_class
static const std::string & s_class()
Definition: rntuple:57
tools::rcsv::ntuple::m_cols
std::vector< read::icol * > m_cols
Definition: rcsv_ntuple:1051
tools::uint64
unsigned long long uint64
Definition: typedefs:72
tools::int64
long long int64
Definition: typedefs:67
tools::rcsv::ntuple::m_rows
int m_rows
Definition: rcsv_ntuple:1053
tools::leading
@ leading
Definition: strip:12
tools::rcsv::ntuple::m_hippo
bool m_hippo
Definition: rcsv_ntuple:1054
tools::rcsv::ntuple::next
virtual bool next()
Definition: rcsv_ntuple:53
tools::rcsv::ntuple::col_desc
std::pair< std::string, std::string > col_desc
Definition: rcsv_ntuple:573
tools::rcsv::ntuple::m_sep
char m_sep
Definition: rcsv_ntuple:1049
tools::rcsv::ntuple::_read_line
bool _read_line()
Definition: rcsv_ntuple:931
TOOLS_RCSV_NTUPLE_IF_COL
#define TOOLS_RCSV_NTUPLE_IF_COL(a__type)
tools::rcsv::ntuple::ntuple
ntuple(std::istream &a_reader)
Definition: rcsv_ntuple:160
TOOLS_RCSV_NTUPLE_CREATE_VEC_COL
#define TOOLS_RCSV_NTUPLE_CREATE_VEC_COL(a__name, a__type)
tools::rcsv::ntuple::_read_time
static bool _read_time(std::istream &a_reader, std::streampos a_sz, char a_sep, time_t &a_v)
Definition: rcsv_ntuple:877
tools::rcsv::ntuple::s_cid
static const std::string & s_cid(cid a_id)
Definition: rcsv_ntuple:492
tools::rcsv::ntuple::m_vec_sep
char m_vec_sep
Definition: rcsv_ntuple:1050
tools::uchar
unsigned char uchar
Definition: typedefs:99
TOOLS_RCSV_NTUPLE_IF_VEC_CID
#define TOOLS_RCSV_NTUPLE_IF_VEC_CID(a__name, a__type)
tools::rcsv::ntuple::initialize
bool initialize(std::ostream &a_out, char a_sep=0, const std::string &a_suffix="x", bool a_verbose=false)
Definition: rcsv_ntuple:312
tools::to
std::vector< std::string > to(int a_argc, char **a_argv)
Definition: args:507
tools::CR
char CR()
Definition: chars:17
tools::rcsv::ntuple::skip_comment
static bool skip_comment(std::istream &a_reader, std::streampos a_sz)
Definition: rcsv_ntuple:855
tools::rcsv::ntuple::m_sz
std::streampos m_sz
Definition: rcsv_ntuple:1052
tools::words
void words(const std::string &a_string, const std::string &a_sep, bool a_take_empty, std::vector< std::string > &a_words, bool a_clear=true)
Definition: words:12
tools::rcsv::ntuple::read_commented_header
bool read_commented_header(std::ostream &a_out, std::istream &a_reader, std::string &a_title, char &a_sep, char &a_vec_sep, std::vector< col_desc > &a_cols)
Definition: rcsv_ntuple:694
tools::waxml::end
void end(std::ostream &a_writer)
Definition: begend:31
tools::s2time
bool s2time(const std::string &a_string, time_t &a_time)
Definition: s2time:13
tools::rcsv::ntuple::column::id_class
static cid id_class()
Definition: rcsv_ntuple:101
tools::rcsv::ntuple::m_title
std::string m_title
Definition: rcsv_ntuple:1048
tools::rcsv::ntuple::skip_line
static bool skip_line(std::istream &a_reader, std::streampos a_sz)
Definition: rcsv_ntuple:845
tools::strip
bool strip(std::string &a_string, what a_type=both, char a_char=' ')
Definition: strip:14
TOOLS_RCSV_NTUPLE_IF_CID
#define TOOLS_RCSV_NTUPLE_IF_CID(a__name, a__type)
tools::sep
const std::string & sep()
Definition: sep:11
tools::rcsv::ntuple::read_line
static bool read_line(std::istream &a_reader, std::streampos a_sz, std::string &a_s)
Definition: rcsv_ntuple:832
tools_vforcit
#define tools_vforcit(a__T, a__v, a__it)
Definition: forit:7
tools::stype
const std::string & stype(const mat4f &)
Definition: mat4f:73
tools::ushort
unsigned short ushort
Definition: typedefs:101
tools::LF
char LF()
Definition: chars:16
tools::uint32
unsigned int uint32
Definition: typedefs:71
tools::rcsv::ntuple::_read
static bool _read(std::istream &a_reader, std::streampos, char, T &a_v)
Definition: rcsv_ntuple:871
tools::rcsv::ntuple::m_reader
std::istream & m_reader
Definition: rcsv_ntuple:1047