Warning, /include/Geant4/tools/ntuple_binding is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef tools_ntuple_binding
0005 #define tools_ntuple_binding
0006
0007 // a little class to capture column binding parameters
0008 // when reading an ntuple.
0009
0010 #include <string>
0011 #include <vector>
0012 #include "forit"
0013 #include "cids"
0014
0015 namespace tools {
0016
0017 class column_binding {
0018 public:
0019 column_binding(const std::string& a_name,cid a_cid,void* a_user_obj)
0020 :m_name(a_name)
0021 ,m_cid(a_cid)
0022 ,m_user_obj(a_user_obj) //WARNING : not owner.
0023 {}
0024 virtual ~column_binding() {}
0025 public:
0026 column_binding(const column_binding& a_from)
0027 :m_name(a_from.m_name)
0028 ,m_cid(a_from.m_cid)
0029 ,m_user_obj(a_from.m_user_obj)
0030 {}
0031 column_binding& operator=(const column_binding& a_from) {
0032 if(&a_from==this) return *this;
0033 m_name = a_from.m_name;
0034 m_cid = a_from.m_cid;
0035 m_user_obj = a_from.m_user_obj;
0036 return *this;
0037 }
0038 public:
0039 const std::string& name() const {return m_name;}
0040 cid get_cid() const {return m_cid;}
0041 void* user_obj() const {return m_user_obj;}
0042 protected:
0043 std::string m_name;
0044 cid m_cid;
0045 void* m_user_obj;
0046 };
0047
0048 class ntuple_binding {
0049 public:
0050 ntuple_binding()
0051 {}
0052 virtual ~ntuple_binding(){}
0053 public:
0054 ntuple_binding(const ntuple_binding& a_from)
0055 :m_columns(a_from.m_columns)
0056 {}
0057 ntuple_binding& operator=(const ntuple_binding& a_from){
0058 m_columns = a_from.m_columns;
0059 return *this;
0060 }
0061 public:
0062 template <class T>
0063 void add_column(const std::string& a_name,T& a_user_var) {
0064 m_columns.push_back(column_binding(a_name,_cid(T()),(void*)&a_user_var));
0065 }
0066
0067 template <class T>
0068 void add_column(const std::string& a_name,std::vector<T>& a_user_var) {
0069 m_columns.push_back(column_binding(a_name,_cid_std_vector<T>(),(void*)&a_user_var));
0070 }
0071
0072 // to have consistent naming than in ntuple_booking :
0073 template <class T>
0074 void add_column_vec(const std::string& a_name,std::vector<T>& a_user_var) {
0075 m_columns.push_back(column_binding(a_name,_cid_std_vector<T>(),(void*)&a_user_var));
0076 }
0077
0078 template <class T>
0079 void add_column_cid(const std::string& a_name,T& a_user_var) {
0080 m_columns.push_back(column_binding(a_name,T::id_class(),(void*)&a_user_var));
0081 }
0082
0083 void add_column_no_var(const std::string& a_name) { //used in rcsv_ntuple.
0084 m_columns.push_back(column_binding(a_name,0,0));
0085 }
0086
0087 //void add_column(const std::string& a_name,cid a_id,void* a_user_var) {
0088 // m_columns.push_back(column_binding(a_name,a_id,a_user_var));
0089 //}
0090
0091 const std::vector<column_binding>& columns() const {return m_columns;}
0092
0093 bool find_user_obj(const std::string& a_name,cid& a_cid,void*& a_obj) const {
0094 tools_vforcit(column_binding,m_columns,it) {
0095 if((*it).name()==a_name) {
0096 a_cid = (*it).get_cid();
0097 a_obj = (*it).user_obj();
0098 return true;
0099 }
0100 }
0101 a_cid = 0;
0102 a_obj = 0;
0103 return false;
0104 }
0105
0106 template <class T>
0107 T* find_variable(const std::string& a_name) const {
0108 tools_vforcit(column_binding,m_columns,it) {
0109 if((*it).name()==a_name) {
0110 // we should check cid. If so, take care of T=aida::ntuple.
0111 return (T*)((*it).user_obj());
0112 }
0113 }
0114 return 0;
0115 }
0116
0117 template <class T>
0118 std::vector<T>* find_vector_variable(const std::string& a_name) const {
0119 tools_vforcit(column_binding,m_columns,it) {
0120 if((*it).name()==a_name) {
0121 if((*it).get_cid()!=_cid_std_vector<T>()) return 0;
0122 return (std::vector<T>*)((*it).user_obj());
0123 }
0124 }
0125 return 0;
0126 }
0127 protected:
0128 std::vector<column_binding> m_columns;
0129 };
0130
0131 }
0132
0133 #endif