g4tools  5.4.0
ntuple_binding
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_ntuple_binding
5 #define tools_ntuple_binding
6 
7 // a little class to capture column binding parameters
8 // when reading an ntuple.
9 
10 #include <string>
11 #include <vector>
12 #include "forit"
13 #include "cids"
14 
15 namespace tools {
16 
18 public:
19  column_binding(const std::string& a_name,cid a_cid,void* a_user_obj)
20  :m_name(a_name)
21  ,m_cid(a_cid)
22  ,m_user_obj(a_user_obj) //WARNING : not owner.
23  {}
24  virtual ~column_binding() {}
25 public:
27  :m_name(a_from.m_name)
28  ,m_cid(a_from.m_cid)
29  ,m_user_obj(a_from.m_user_obj)
30  {}
32  if(&a_from==this) return *this;
33  m_name = a_from.m_name;
34  m_cid = a_from.m_cid;
35  m_user_obj = a_from.m_user_obj;
36  return *this;
37  }
38 public:
39  const std::string& name() const {return m_name;}
40  cid get_cid() const {return m_cid;}
41  void* user_obj() const {return m_user_obj;}
42 protected:
43  std::string m_name;
45  void* m_user_obj;
46 };
47 
49 public:
51  {}
52  virtual ~ntuple_binding(){}
53 public:
55  :m_columns(a_from.m_columns)
56  {}
58  m_columns = a_from.m_columns;
59  return *this;
60  }
61 public:
62  template <class T>
63  void add_column(const std::string& a_name,T& a_user_var) {
64  m_columns.push_back(column_binding(a_name,_cid(T()),(void*)&a_user_var));
65  }
66 
67  template <class T>
68  void add_column(const std::string& a_name,std::vector<T>& a_user_var) {
69  m_columns.push_back(column_binding(a_name,_cid_std_vector<T>(),(void*)&a_user_var));
70  }
71 
72  // to have consistent naming than in ntuple_booking :
73  template <class T>
74  void add_column_vec(const std::string& a_name,std::vector<T>& a_user_var) {
75  m_columns.push_back(column_binding(a_name,_cid_std_vector<T>(),(void*)&a_user_var));
76  }
77 
78  template <class T>
79  void add_column_cid(const std::string& a_name,T& a_user_var) {
80  m_columns.push_back(column_binding(a_name,T::id_class(),(void*)&a_user_var));
81  }
82 
83  void add_column_no_var(const std::string& a_name) { //used in rcsv_ntuple.
84  m_columns.push_back(column_binding(a_name,0,0));
85  }
86 
87  //void add_column(const std::string& a_name,cid a_id,void* a_user_var) {
88  // m_columns.push_back(column_binding(a_name,a_id,a_user_var));
89  //}
90 
91  const std::vector<column_binding>& columns() const {return m_columns;}
92 
93  bool find_user_obj(const std::string& a_name,cid& a_cid,void*& a_obj) const {
95  if((*it).name()==a_name) {
96  a_cid = (*it).get_cid();
97  a_obj = (*it).user_obj();
98  return true;
99  }
100  }
101  a_cid = 0;
102  a_obj = 0;
103  return false;
104  }
105 
106  template <class T>
107  T* find_variable(const std::string& a_name) const {
109  if((*it).name()==a_name) {
110  // we should check cid. If so, take care of T=aida::ntuple.
111  return (T*)((*it).user_obj());
112  }
113  }
114  return 0;
115  }
116 
117  template <class T>
118  std::vector<T>* find_vector_variable(const std::string& a_name) const {
120  if((*it).name()==a_name) {
121  if((*it).get_cid()!=_cid_std_vector<T>()) return 0;
122  return (std::vector<T>*)((*it).user_obj());
123  }
124  }
125  return 0;
126  }
127 protected:
128  std::vector<column_binding> m_columns;
129 };
130 
131 }
132 
133 #endif
tools::_cid
cid _cid(byte)
Definition: cids:14
tools::ntuple_binding::add_column_no_var
void add_column_no_var(const std::string &a_name)
Definition: ntuple_binding:83
tools::ntuple_binding
Definition: ntuple_binding:48
tools::ntuple_binding::find_vector_variable
std::vector< T > * find_vector_variable(const std::string &a_name) const
Definition: ntuple_binding:118
tools::ntuple_binding::ntuple_binding
ntuple_binding(const ntuple_binding &a_from)
Definition: ntuple_binding:54
tools::ntuple_binding::m_columns
std::vector< column_binding > m_columns
Definition: ntuple_binding:128
tools::ntuple_binding::add_column
void add_column(const std::string &a_name, T &a_user_var)
Definition: ntuple_binding:63
tools::column_binding
Definition: ntuple_binding:17
tools::column_binding::get_cid
cid get_cid() const
Definition: ntuple_binding:40
tools::column_binding::operator=
column_binding & operator=(const column_binding &a_from)
Definition: ntuple_binding:31
tools::column_binding::column_binding
column_binding(const column_binding &a_from)
Definition: ntuple_binding:26
tools::ntuple_binding::operator=
ntuple_binding & operator=(const ntuple_binding &a_from)
Definition: ntuple_binding:57
tools::ntuple_binding::columns
const std::vector< column_binding > & columns() const
Definition: ntuple_binding:91
tools::column_binding::m_name
std::string m_name
Definition: ntuple_binding:43
tools::column_binding::m_cid
cid m_cid
Definition: ntuple_binding:44
tools::column_binding::~column_binding
virtual ~column_binding()
Definition: ntuple_binding:24
tools::column_binding::user_obj
void * user_obj() const
Definition: ntuple_binding:41
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::ntuple_binding::add_column_vec
void add_column_vec(const std::string &a_name, std::vector< T > &a_user_var)
Definition: ntuple_binding:74
tools::column_binding::m_user_obj
void * m_user_obj
Definition: ntuple_binding:45
tools::column_binding::name
const std::string & name() const
Definition: ntuple_binding:39
forit
cids
tools_vforcit
#define tools_vforcit(a__T, a__v, a__it)
Definition: forit:7
tools::ntuple_binding::ntuple_binding
ntuple_binding()
Definition: ntuple_binding:50
tools::ntuple_binding::add_column_cid
void add_column_cid(const std::string &a_name, T &a_user_var)
Definition: ntuple_binding:79
tools::ntuple_binding::~ntuple_binding
virtual ~ntuple_binding()
Definition: ntuple_binding:52
tools::ntuple_binding::find_variable
T * find_variable(const std::string &a_name) const
Definition: ntuple_binding:107
tools::column_binding::column_binding
column_binding(const std::string &a_name, cid a_cid, void *a_user_obj)
Definition: ntuple_binding:19
tools::cid
unsigned short cid
Definition: cid:9
tools::ntuple_binding::find_user_obj
bool find_user_obj(const std::string &a_name, cid &a_cid, void *&a_obj) const
Definition: ntuple_binding:93
tools::ntuple_binding::add_column
void add_column(const std::string &a_name, std::vector< T > &a_user_var)
Definition: ntuple_binding:68