fast pQCD calculations for hadron-induced processes
read_steer.h
1 // Author: Daniel Britzger
2 // DESY, 17/07/2012
3 #ifndef __READ_STEER_INC__
4 #define __READ_STEER_INC__
5 
343 #include <stdio.h>
344 #include <stdlib.h>
345 #include <string>
346 #include <cstring>
347 #include <fstream>
348 #include <iostream>
349 #include <map>
350 #include <set>
351 //update to unordered_map in C++11 in gcc4.7
352 #include <vector>
353 #include <sstream>
354 // use the pre-processor for accessing values
355 // in 'single-file' mode
356 #define READ(X) read_steer::readfile(X)
357 
358 #define BOOL(X) read_steer::getbool(#X)
359 #define INT(X) read_steer::getint(#X)
360 #define DOUBLE(X) read_steer::getdouble(#X)
361 #define CHAR(X) read_steer::getstring(#X).c_str()
362 #define STRING(X) read_steer::getstring(#X)
363 
364 #define BOOL_ARR(X) read_steer::getboolarray(#X)
365 #define INT_ARR(X) read_steer::getintarray(#X)
366 #define DOUBLE_ARR(X) read_steer::getdoublearray(#X)
367 #define STRING_ARR(X) read_steer::getstringarray(#X)
368 
369 #define BOOL_COL(X,Y) read_steer::getboolcolumn(#X,#Y)
370 #define INT_COL(X,Y) read_steer::getintcolumn(#X,#Y)
371 #define DOUBLE_COL(X,Y) read_steer::getdoublecolumn(#X,#Y)
372 #define STRING_COL(X,Y) read_steer::getstringcolumn(#X,#Y)
373 #define TABLEHEADER(X) read_steer::gettableheader(#X)
374 
375 #define INT_TAB(X) read_steer::getinttable(#X)
376 #define DOUBLE_TAB(X) read_steer::getdoubletable(#X)
377 #define STRING_TAB(X) read_steer::getstringtable(#X)
378 
379 #define ADD(X,Y) read_steer::addvalue(X,Y)
380 #define ADDARRAY(X,Y) read_steer::addarray(X,Y)
381 #define ADDTABLE(X,Y,Z) read_steer::addtable(X,Y,Z)
382 
383 // use the pre-processor for accessing values
384 // in 'multi-file' mode
385 // NS specifies the steerID-namespace
386 #define READ_NS(X,NS) read_steer::readfile(X,NS)
387 
388 #define BOOL_NS(X,NS) read_steer::getbool(#X,NS)
389 #define INT_NS(X,NS) read_steer::getint(#X,NS)
390 #define DOUBLE_NS(X,NS) read_steer::getdouble(#X,NS)
391 #define CHAR_NS(X,NS) read_steer::getstring(#X,NS).c_str()
392 #define STRING_NS(X,NS) read_steer::getstring(#X,NS)
393 
394 #define BOOL_ARR_NS(X,NS) read_steer::getboolarray(#X,NS)
395 #define INT_ARR_NS(X,NS) read_steer::getintarray(#X,NS)
396 #define DOUBLE_ARR_NS(X,NS) read_steer::getdoublearray(#X,NS)
397 #define STRING_ARR_NS(X,NS) read_steer::getstringarray(#X,NS)
398 
399 #define BOOL_COL_NS(X,Y,NS) read_steer::getboolcolumn(#X,#Y,NS)
400 #define INT_COL_NS(X,Y,NS) read_steer::getintcolumn(#X,#Y,NS)
401 #define DOUBLE_COL_NS(X,Y,NS) read_steer::getdoublecolumn(#X,#Y,NS)
402 #define STRING_COL_NS(X,Y,NS) read_steer::getstringcolumn(#X,#Y,NS)
403 #define TABLEHEADER_NS(X,NS) read_steer::gettableheader(#X,NS)
404 
405 #define INT_TAB_NS(X,NS) read_steer::getinttable(#X,NS)
406 #define DOUBLE_TAB_NS(X,NS) read_steer::getdoubletable(#X,NS)
407 #define STRING_TAB_NS(X,NS) read_steer::getstringtable(#X,NS)
408 
409 #define ADD_NS(X,Y,NS) read_steer::addvalue(X,Y,NS)
410 #define ADDARRAY_NS(X,Y,NS) read_steer::addarray(X,Y,NS)
411 #define ADDTABLE_NS(X,Y,Z,NS) read_steer::addtable(X,Y,Z,NS)
412 
413 // shortcuts for parsing command line and printing
414 #define PARSE(X,Y) read_steer::parsecommandline(X,Y)
415 #define PARSEV(X) read_steer::parsecommandline(X)
416 #define PRINTALL() read_steer::printall()
417 
418 // check if key exists
419 #define EXIST(X) read_steer::getexist(#X)
420 #define EXIST_NS(X,NS) read_steer::getexist(#X,NS)
421 
422 // check if array or array element exists
423 #define EXISTARRAY_NS(X,NS) read_steer::getarrayexist(#X, NS)
424 #define CONTAINKEYARRAY_NS(X,A,NS) read_steer::getarraycontainkey(#X, #A, NS)
425 #define PUSHBACKARRAY_NS(X,A,NS) read_steer::arraypushback_steer(X, #A, NS)
426 
427 class read_steer {
428 
429 private:
430  static std::string stdID;
431 
432 public:
433  ~read_steer() {;};
434  static read_steer* Steering(std::string steerID=read_steer::stdID); // get an object!
435  static void destroy(); // destroy all instances
436 
437 public:
438  // getters for single instance
439  // values
440  bool getb(const std::string& label);
441  int geti(const std::string& label);
442  double getd(const std::string& label);
443  std::string gets(const std::string& label);
444  // arrays
445  std::vector<bool> getbf(const std::string& label);
446  std::vector<int> getif(const std::string& label);
447  std::vector<double> getdf(const std::string& label);
448  std::vector<std::string> getsf(const std::string& label);
449  // tables/matrices
450  std::vector<std::string> getsthead(const std::string& label);
451  std::vector<std::vector<int> > getit(const std::string& label);
452  std::vector<std::vector<double> > getdt(const std::string& label);
453  std::vector<std::vector<std::string> > getst(const std::string& label);
454  std::vector<bool> getbtcol(const std::string& label,const std::string& col);
455  std::vector<int> getitcol(const std::string& label,const std::string& col);
456  std::vector<double> getdtcol(const std::string& label,const std::string& col);
457  std::vector<std::string> getstcol(const std::string& label,const std::string& col);
458 
459  // check if key exists
460  bool exist(const std::string& label) {
461  bool ret = ( fstrings.count(label) > 0 || ffields.count(label) > 0 || ftables.count(label) > 0 );
462  return ret;}
463  bool arrayexist(const std::string& label) {
464  return ffields.count(label) > 0;}
465  bool arraycontainkey(const std::string& key, const std::string& label) {
466  if (ffields.count(label) == 0) return false;
467  for (size_t i = 0; i < ffields[label].size(); ++i) {
468  if (ffields[label][i].find(key+"=") == std::string::npos) {
469  return true;
470  }
471  }
472  return false;}
473  void arraypushback(const std::string& value, const std::string& label) {
474  ffields[label].push_back(value);}
475  // setter
476  void AddLabel(const std::string& key, const std::string& value);
477  template <typename T> void AddLabel ( const std::string& key, T value);
478  void AddArray(const std::string& key, const std::vector<std::string>& values);
479  template <typename T> void AddArray ( const std::string& key, const std::vector<T>& values);
480  void AddTable(const std::string& key, const std::vector<std::string>& header, const std::vector<std::vector<std::string> >& values);
481  template <typename T> void AddTable ( const std::string& key, const std::vector<std::string>& header, const std::vector<std::vector<T> >& values);
482  void AppendToArray(const std::string& key, const std::string& entry);
483  template <typename T> void AppendToArray ( const std::string& key, const T& entry);
484  void AppendToTable(const std::string& key, const std::vector<std::string>& entry);
485  template <typename T> void AppendToTable ( const std::string& key, const std::vector<T>& entry);
486  // controls
487  // report return code instead of void function
488  int inits(std::string filename);
489  int initnmspc(std::ifstream& strm, std::string filename);
490  void prt();
491  static void initnamespace(std::ifstream& strm,std::string filename, std::string steerID=read_steer::stdID) { // set the steer-filename
492  read_steer::Steering(steerID)->initnmspc(strm,filename); }
493 
494  // get labels
495  std::set<std::string> GetAvailableLabels() const;
496  std::set<std::string> GetAvailableArrrays() const;
497  std::set<std::string> GetAvailableTables() const;
498 
499  static bool CheckNumber(const std::string& str);
500  static bool CheckInt(const std::string& str);
501  static int separatetag(std::string& vallhs, std::string& valrhs, const std::string& sep);
502 
503  static const std::string& GetDefaultNamespace(){ return stdID;}
504  static void SetDefaultNamespace(const std::string& nspc){ stdID = nspc;}
505 
506 private:
507  read_steer();
508  read_steer(const read_steer& ) {;};
509  static std::map<std::string,read_steer*>* instances;
510 
511  int read_stdin(const std::string& filename);
512  int readstrm(std::ifstream& strm,unsigned int lstart=0,unsigned int lend=0, bool incfile=false);
513  bool ParseString(std::string value);
514  bool ParseFindString(const std::string& str, const std::string& tag) const;
515  std::string ParseEnclosedString(const std::string&) const;
516  bool EnclosedStringToOneEntity(std::string&) const;
517  int ReplaceVariables(std::string& value);
518  bool StringToBool(const std::string& str, const std::string& label="") const;
519  static int cmdlinetag(const char* arg, std::string& label, std::string& value);
520 
521  std::map<std::string,std::string> fstrings;
522  std::map<std::string,std::vector<std::string> > ffields;
523  std::map<std::string,std::vector<std::vector<std::string> > > ftables;
524  std::map<std::string,std::vector<std::string> > ftableheaders;
525  bool fParseFieldMode;
526  int fParseTableMode;
527  std::string ffieldlabel;
528  std::vector<std::string> ffieldvalues;
529  std::vector<std::vector<std::string> > ftablevalues;
530  std::string ffilename;
531  std::string fcurrentfilename;
532  std::ifstream ffile;
533 
534  const std::string str_sep;
535  const std::string str_cmt;
536  const std::string str_arrbeg;
537  const std::string str_arrend;
538  const std::string str_tabbeg;
539  const std::string str_tabend;
540  const std::string str_nmspcbeg;
541  const std::string str_nmspcend;
542  const std::string str_inc;
543  int fParseIncMode;
544  const std::string oW;
545  const std::string oI;
546  const std::string oE;
547 
548 public:
549  // static member function; report return code instead of void function
550  static int readfile(std::string filename,std::string steerID=read_steer::stdID) { // set the steer-filename
551  return read_steer::Steering(steerID)->inits(filename); }
552  // setters
553  // verbosity
554  static int fVerbosity;
555  static void setVerbosity(int iVerbosity) {
556  read_steer::fVerbosity = iVerbosity; }
557  // getters
558  // verbosity
559  static int getVerbosity() {
560  return read_steer::fVerbosity; }
561  // values
562  static bool getbool(std::string label,std::string steerID=read_steer::stdID) {
563  return read_steer::Steering(steerID)->getb(label); }
564  static int getint(std::string label,std::string steerID=read_steer::stdID) {
565  return read_steer::Steering(steerID)->geti(label); }
566  static double getdouble(std::string label,std::string steerID=read_steer::stdID) {
567  return read_steer::Steering(steerID)->getd(label); }
568  static std::string getstring(std::string label,std::string steerID=read_steer::stdID) {
569  return read_steer::Steering(steerID)->gets(label); }
570  // arrays
571  static std::vector<bool> getboolarray(std::string label,std::string steerID=read_steer::stdID) {
572  return read_steer::Steering(steerID)->getbf(label); }
573  static std::vector<int> getintarray(std::string label,std::string steerID=read_steer::stdID) {
574  return read_steer::Steering(steerID)->getif(label); }
575  static std::vector<double> getdoublearray(std::string label,std::string steerID=read_steer::stdID) {
576  return read_steer::Steering(steerID)->getdf(label); }
577  static std::vector<std::string> getstringarray(std::string label,std::string steerID=read_steer::stdID) {
578  return read_steer::Steering(steerID)->getsf(label); }
579  // tables header
580  static std::vector<std::string> gettableheader(std::string label,std::string steerID=read_steer::stdID) {
581  return read_steer::Steering(steerID)->getsthead(label); }
582  // tables/matrices
583  static std::vector<std::vector<int> > getinttable(std::string label,std::string steerID=read_steer::stdID) {
584  return read_steer::Steering(steerID)->getit(label); }
585  static std::vector<std::vector<double> > getdoubletable(std::string label,std::string steerID=read_steer::stdID) {
586  return read_steer::Steering(steerID)->getdt(label); }
587  static std::vector<std::vector<std::string> > getstringtable(std::string label,std::string steerID=read_steer::stdID) {
588  return read_steer::Steering(steerID)->getst(label); }
589  // table columns
590  static std::vector<bool> getboolcolumn(std::string label,std::string column,std::string steerID=read_steer::stdID) {
591  return read_steer::Steering(steerID)->getbtcol(label,column); }
592  static std::vector<int> getintcolumn(std::string label,std::string column,std::string steerID=read_steer::stdID) {
593  return read_steer::Steering(steerID)->getitcol(label,column); }
594  static std::vector<double> getdoublecolumn(std::string label,std::string column,std::string steerID=read_steer::stdID) {
595  return read_steer::Steering(steerID)->getdtcol(label,column); }
596  static std::vector<std::string> getstringcolumn(std::string label,std::string column,std::string steerID=read_steer::stdID) {
597  return read_steer::Steering(steerID)->getstcol(label,column); }
598  // check existence of value
599  static bool getexist(const std::string& label, std::string steerID=read_steer::stdID ){
600  if ( read_steer::instances->count(steerID) == 0 ) return false;
601  return read_steer::Steering(steerID)->exist(label);}
602  static bool getarrayexist(const std::string& label, std::string steerID=read_steer::stdID ){
603  if ( read_steer::instances->count(steerID) == 0 ) return false;
604  return read_steer::Steering(steerID)->arrayexist(label);}
605  static bool getarraycontainkey(const std::string& key, const std::string& label, std::string steerID=read_steer::stdID ){
606  if ( read_steer::instances->count(steerID) == 0 ) return false;
607  return read_steer::Steering(steerID)->arraycontainkey(key, label);}
608  static void arraypushback_steer(const std::string& value, const std::string& label, std::string steerID=read_steer::stdID ){
609  if ( read_steer::instances->count(steerID) == 0 ) return;
610  read_steer::Steering(steerID)->arraypushback(value, label);}
611  // add values
612  template <typename T>
613  static void addvalue(const std::string& key,const T& val,std::string steerID=read_steer::stdID) {
614  read_steer::Steering(steerID)->AddLabel(key,val); }
615  template <typename T>
616  static void addarray(const std::string& key,const std::vector<T>& val,std::string steerID=read_steer::stdID) {
617  read_steer::Steering(steerID)->AddArray(key,val); }
618  template <typename T>
619  static void addtable(const std::string& key, const std::vector<std::string>& header, const std::vector<std::vector<T> >& values,std::string steerID=read_steer::stdID) {
620  read_steer::Steering(steerID)->AddTable(key,header,values); }
621  template <typename T>
622  static void appendtoarray(const std::string& key, const T& entry, std::string steerID=read_steer::stdID) {
623  read_steer::Steering(steerID)->AppendToArray(key,entry); }
624  template <typename T>
625  static void appendtotable(const std::string& key, const std::vector<T>& entry, std::string steerID=read_steer::stdID) {
626  read_steer::Steering(steerID)->AppendToTable(key,entry); }
627 
628  static void printall(); // print values of all files
629  static void print(std::string steerID=read_steer::stdID); // print values
630  static bool parsecommandline(int argc,char** argv);
631  static bool parsecommandline(std::vector<std::string> argv);
632 
633 };
634 
635 
636 template <typename T>
637 void read_steer::AddLabel ( const std::string& key, T val) {
638  std::stringstream ss;
639  ss << val;
640  AddLabel(key,ss.str());
641 }
642 
643 template <typename T>
644 void read_steer::AddArray ( const std::string& key, const std::vector<T>& val) {
645  std::vector<std::string> str(val.size());
646  for ( unsigned int i = 0 ; i<val.size() ; i++ ) {
647  std::stringstream ss;
648  ss << val[i];
649  str[i] = ss.str();
650  }
651  AddArray(key,str);
652 }
653 
654 template <typename T>
655 void read_steer::AddTable ( const std::string& key, const std::vector<std::string>& header, const std::vector<std::vector<T> >& values) {
656  std::vector<std::vector<std::string> > str(values.size());
657  for ( unsigned int i = 0 ; i<values.size() ; i++ ) {
658  str[i].resize(values[i].size());
659  for ( unsigned int j = 0 ; j<values[i].size() ; j++ ) {
660  std::stringstream ss;
661  ss << values[i][j];
662  str[i][j]=ss.str();
663  }
664  }
665  AddTable(key,header,str);
666 }
667 
668 template <typename T>
669 void read_steer::AppendToArray ( const std::string& key, const T& entry ) {
670  std::stringstream ss;
671  ss << entry;
672  std::string str=ss.str();
673  AppendToArray(key,entry);
674 }
675 
676 template <typename T>
677 void read_steer::AppendToTable ( const std::string& key, const std::vector<T>& entry ) {
678  std::vector<std::string > str(entry.size());
679  for ( unsigned int j = 0 ; j<entry.size() ; j++ ) {
680  std::stringstream ss;
681  ss << entry[j];
682  str[j]=ss.str();
683  }
684  AppendToTable(key,entry);
685 }
686 
687 #endif
Definition: read_steer.h:427
static bool parsecommandline(int argc, char **argv)
Definition: read_steer.cc:782
void AppendToTable(const std::string &key, const std::vector< std::string > &entry)
Definition: read_steer.cc:424
static bool CheckInt(const std::string &str)
Definition: read_steer.cc:441
static bool CheckNumber(const std::string &str)
Definition: read_steer.cc:435