g4tools  5.4.0
columns.icc
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 // common columns code for ntuple and base_pntuple.
5 
6  template <class T>
7  class column_ref : public virtual icol {
8 #ifdef TOOLS_MEM
9  static const std::string& s_class() {
10  static const std::string s_v("tools::wroot::ntuple::column_ref<"+stype(T())+">");
11  return s_v;
12  }
13 #endif
14  public:
15  static cid id_class() {
16  static const T s_v = T(); //do that for T = std::string.
17  return _cid(s_v)+10000;
18  }
19  virtual void* cast(cid a_class) const {
20  if(void* p = cmp_cast<column_ref>(this,a_class)) {return p;}
21  else return 0;
22  }
23  virtual cid id_cls() const {return id_class();}
24  public: //icol
25  virtual void add() {}
26  virtual void set_def() {}
27  virtual const std::string& name() const {return m_leaf->name();}
28  virtual void set_basket_size(uint32 a_size) {m_branch.set_basket_size(a_size);}
29  virtual branch& get_branch() const {return m_branch;}
30  virtual base_leaf* get_leaf() const {return m_leaf;}
31  public:
32  column_ref(branch& a_branch,const std::string& a_name,const T& a_ref)
33  :m_branch(a_branch),m_leaf(0)
34  {
35 #ifdef TOOLS_MEM
36  mem::increment(s_class().c_str());
37 #endif
38  m_leaf = m_branch.create_leaf_ref<T>(a_name,a_ref);
39  }
40  virtual ~column_ref(){
41 #ifdef TOOLS_MEM
42  mem::decrement(s_class().c_str());
43 #endif
44  }
45  protected:
46  column_ref(const column_ref& a_from)
47  :icol(a_from)
48  ,m_branch(a_from.m_branch)
49  ,m_leaf(0)
50  {}
51  column_ref& operator=(const column_ref& a_from){
52  if(&a_from==this) return *this;
53  m_leaf = 0;
54  return *this;
55  }
56  public:
57  const T& variable() const {return m_leaf->variable();}
58  T& variable() {return m_leaf->variable();}
59  protected:
60  branch& m_branch;
61  leaf_ref<T>* m_leaf;
62  };
63 
64  template <class T>
65  class column : public column_ref<T> {
66  typedef column_ref<T> parent;
67 #ifdef TOOLS_MEM
68  static const std::string& s_class() {
69  static const std::string s_v("tools::wroot::ntuple::column<"+stype(T())+">");
70  return s_v;
71  }
72 #endif
73  public:
74  static cid id_class() {
75  static const T s_v = T(); //do that for T = std::string.
76  return _cid(s_v);
77  }
78  virtual void* cast(cid a_class) const {
79  if(void* p = cmp_cast<column>(this,a_class)) {return p;}
80  else return 0;
81  }
82  virtual cid id_cls() const {return id_class();}
83  public: //icol
84  virtual void set_def() {m_value = m_def;}
85  public:
86  column(branch& a_branch,const std::string& a_name,const T& a_def)
87  :parent(a_branch,a_name,m_value)
88  ,m_def(a_def),m_value(a_def)
89  {
90 #ifdef TOOLS_MEM
91  mem::increment(s_class().c_str());
92 #endif
93  }
94  virtual ~column(){
95 #ifdef TOOLS_MEM
96  mem::decrement(s_class().c_str());
97 #endif
98  }
99  protected:
100  column(const column& a_from)
101  :icol(a_from)
102  ,parent(a_from)
103  ,m_def(a_from.m_def)
104  ,m_value(a_from.m_value)
105  {}
106  column& operator=(const column& a_from){
107  if(&a_from==this) return *this;
108  parent::operator=(a_from);
109  m_def = a_from.m_def;
110  m_value = a_from.m_value;
111  return *this;
112  }
113  public:
114  column& operator=(const T& a_value){m_value = a_value;return *this;}
115  bool fill(const T& a_value) {m_value = a_value;return true;}
116  protected:
117  T m_def;
118  T m_value;
119  };
120 
121  class column_string_ref : public virtual icol {
122 #ifdef TOOLS_MEM
123  static const std::string& s_class() {
124  static const std::string s_v("tools::wroot::ntuple::column_string_ref");
125  return s_v;
126  }
127 #endif
128  public:
129  static cid id_class() {
130  static const std::string s_v;
131  return _cid(s_v)+10000;
132  }
133  virtual void* cast(cid a_class) const {
134  if(void* p = cmp_cast<column_string_ref>(this,a_class)) {return p;}
135  else return 0;
136  }
137  virtual cid id_cls() const {return id_class();}
138  public: //icol
139  virtual void add() {}
140  virtual void set_def() {}
141  virtual const std::string& name() const {return m_leaf->name();}
142  virtual void set_basket_size(uint32 a_size) {m_branch.set_basket_size(a_size);}
143  virtual branch& get_branch() const {return m_branch;}
144  virtual base_leaf* get_leaf() const {return m_leaf;}
145  public:
146  column_string_ref(branch& a_branch,const std::string& a_name,const std::string& a_ref)
147  :m_branch(a_branch),m_leaf(0)
148  {
149 #ifdef TOOLS_MEM
150  mem::increment(s_class().c_str());
151 #endif
152  m_leaf = m_branch.create_leaf_string_ref(a_name,a_ref);
153  }
154  virtual ~column_string_ref(){
155 #ifdef TOOLS_MEM
156  mem::decrement(s_class().c_str());
157 #endif
158  }
159  protected:
160  column_string_ref(const column_string_ref& a_from)
161  :icol(a_from)
162  ,m_branch(a_from.m_branch)
163  ,m_leaf(0)
164  {}
165  column_string_ref& operator=(const column_string_ref& a_from){
166  if(&a_from==this) return *this;
167  m_leaf = 0;
168  return *this;
169  }
170  public:
171  const std::string& variable() const {return m_leaf->variable();}
172  std::string& variable() {return m_leaf->variable();}
173  protected:
174  branch& m_branch;
175  leaf_string_ref* m_leaf;
176  };
177 
178  class column_string : public column_string_ref {
179  typedef column_string_ref parent;
180 #ifdef TOOLS_MEM
181  static const std::string& s_class() {
182  static const std::string s_v("tools::wroot::ntuple::column_string");
183  return s_v;
184  }
185 #endif
186  public:
187  static cid id_class() {
188  static const std::string s_v;
189  return _cid(s_v);
190  }
191  virtual void* cast(cid a_class) const {
192  if(void* p = cmp_cast<column_string>(this,a_class)) {return p;}
193  else return 0;
194  }
195  virtual cid id_cls() const {return id_class();}
196  public: //icol
197  virtual void set_def() {m_value = m_def;}
198  public:
199  column_string(branch& a_branch,const std::string& a_name,const std::string& a_def)
200  :parent(a_branch,a_name,m_value)
201  ,m_def(a_def),m_value(a_def)
202  {
203 #ifdef TOOLS_MEM
204  mem::increment(s_class().c_str());
205 #endif
206  }
207  virtual ~column_string(){
208 #ifdef TOOLS_MEM
209  mem::decrement(s_class().c_str());
210 #endif
211  }
212  protected:
213  column_string(const column_string& a_from)
214  :icol(a_from)
215  ,parent(a_from)
216  ,m_def(a_from.m_def)
217  ,m_value(a_from.m_value)
218  {}
219  column_string& operator=(const column_string& a_from){
220  if(&a_from==this) return *this;
221  parent::operator=(a_from);
222  m_def = a_from.m_def;
223  m_value = a_from.m_value;
224  return *this;
225  }
226  public:
227  column_string& operator=(const std::string& a_value){m_value = a_value;return *this;}
228  bool fill(const std::string& a_value) {m_value = a_value;return true;}
229  protected:
230  std::string m_def;
231  std::string m_value;
232  };
233 
234  class column_vector_string_ref : public column_string_ref {
235  typedef column_string_ref parent;
236 #ifdef TOOLS_MEM
237  static const std::string& s_class() {
238  static const std::string s_v("tools::wroot::ntuple::column_vector_string_ref");
239  return s_v;
240  }
241 #endif
242  public:
243  static cid id_class() {return _cid_std_vector<std::string>()+10000;}
244  virtual void* cast(cid a_class) const {
245  if(void* p = cmp_cast<column_vector_string_ref>(this,a_class)) {return p;}
246  else return 0;
247  }
248  virtual cid id_cls() const {return id_class();}
249  public: //icol
250  virtual void add() {
251  m_string.clear();
252  tools_vforcit(std::string,m_ref,it) {
253  if(it!=m_ref.begin()) m_string += m_sep;
254  m_string += *it;
255  }
256  parent::add();
257  }
258  public:
259  column_vector_string_ref(branch& a_branch,const std::string& a_name,const std::vector<std::string>& a_ref,char a_sep)
260  :parent(a_branch,a_name,m_string)
261  ,m_ref(a_ref)
262  ,m_sep(a_sep)
263  {
264 #ifdef TOOLS_MEM
265  mem::increment(s_class().c_str());
266 #endif
267  }
268  virtual ~column_vector_string_ref(){
269 #ifdef TOOLS_MEM
270  mem::decrement(s_class().c_str());
271 #endif
272  }
273  protected:
274  column_vector_string_ref(const column_vector_string_ref& a_from)
275  :icol(a_from)
276  ,parent(a_from)
277  ,m_ref(a_from.m_ref)
278  ,m_sep(a_from.m_sep)
279  {}
280  column_vector_string_ref& operator=(const column_vector_string_ref& a_from){
281  if(&a_from==this) return *this;
282  m_sep = a_from.m_sep;
283  return *this;
284  }
285  public:
286  const std::vector<std::string>& variable() const {return m_ref;}
287  std::vector<std::string>& variable() {return const_cast< std::vector<std::string>& >(m_ref);}
288  protected:
289  const std::vector<std::string>& m_ref;
290  char m_sep;
291  std::string m_string;
292  };
293 
294  class column_vector_string : public column_vector_string_ref {
295  typedef column_vector_string_ref parent;
296 #ifdef TOOLS_MEM
297  static const std::string& s_class() {
298  static const std::string s_v("tools::wroot::ntuple::column_vector_string");
299  return s_v;
300  }
301 #endif
302  public:
303  static cid id_class() {return _cid_std_vector<std::string>();}
304  virtual void* cast(cid a_class) const {
305  if(void* p = cmp_cast<column_vector_string>(this,a_class)) {return p;}
306  else return 0;
307  }
308  virtual cid id_cls() const {return id_class();}
309  public: //icol
310  virtual void set_def() {m_value = m_def;}
311  public:
312  column_vector_string(branch& a_branch,const std::string& a_name,const std::vector<std::string>& a_def,char a_sep)
313  :parent(a_branch,a_name,m_value,a_sep)
314  ,m_def(a_def),m_value(a_def)
315  {
316 #ifdef TOOLS_MEM
317  mem::increment(s_class().c_str());
318 #endif
319  }
320  virtual ~column_vector_string(){
321 #ifdef TOOLS_MEM
322  mem::decrement(s_class().c_str());
323 #endif
324  }
325  protected:
326  column_vector_string(const column_vector_string& a_from)
327  :icol(a_from)
328  ,parent(a_from)
329  ,m_def(a_from.m_def)
330  ,m_value(a_from.m_value)
331  {}
332  column_vector_string& operator=(const column_vector_string& a_from){
333  if(&a_from==this) return *this;
334  parent::operator=(a_from);
335  m_def = a_from.m_def;
336  m_value = a_from.m_value;
337  return *this;
338  }
339  public:
340  column_vector_string& operator=(const std::vector<std::string>& a_value){m_value = a_value;return *this;}
341  bool fill(const std::vector<std::string>& a_value) {m_value = a_value;return true;}
342  protected:
343  std::vector<std::string> m_def;
344  std::vector<std::string> m_value;
345  };
346 
347  template <class T>
348  class std_vector_column_ref : public virtual icol {
349 #ifdef TOOLS_MEM
350  static const std::string& s_class() {
351  static const std::string s_v("tools::wroot::ntuple::std_vector_column_ref<"+stype(T())+">");
352  return s_v;
353  }
354 #endif
355  public:
356  static cid id_class() {return _cid_std_vector<T>()+10000;}
357  virtual void* cast(cid a_class) const {
358  if(void* p = cmp_cast<std_vector_column_ref>(this,a_class)) {return p;}
359  else return 0;
360  }
361  virtual cid id_cls() const {return id_class();}
362  public: //icol
363  virtual void add() {
364  if(m_leaf_count) m_leaf_count->fill((int)m_ref.size()); //row_wise
365  }
366  virtual void set_def() {}
367  virtual const std::string& name() const {return m_leaf->name();}
368  virtual void set_basket_size(uint32 a_size) {m_branch.set_basket_size(a_size);}
369  virtual branch& get_branch() const {return m_branch;}
370  virtual base_leaf* get_leaf() const {return m_leaf;}
371  public:
372  std_vector_column_ref(branch& a_branch,const std::string& a_name,const std::vector<T>& a_ref)
373  :m_branch(a_branch)
374  ,m_ref(a_ref)
375  ,m_leaf(0)
376  ,m_leaf_count(0) //row_wise
377  {
378 #ifdef TOOLS_MEM
379  mem::increment(s_class().c_str());
380 #endif
381  if(a_branch.store_cls()==branch_element_store_class()) {
382  //::printf("debug : std_vector_column_ref : column_wise\n");
383  int id = -1; //same as in std_vector_be.
384  int type = 0;
385  m_leaf = m_branch.create_leaf_element(a_name,id,type);
386  } else { //row_wise.
387  //::printf("debug : std_vector_column_ref : row_wise\n");
388  std::string leaf_count_name = a_name+"_count";
389  m_leaf_count = m_branch.create_leaf<int>(leaf_count_name);
390  m_leaf = m_branch.create_leaf_std_vector_ref<T>(a_name,*m_leaf_count,a_ref);
391  m_leaf->set_title(a_name+"["+leaf_count_name+"]"); //for TTreeFormula::RegisterDimensions.
392  }
393  }
394  virtual ~std_vector_column_ref(){
395 #ifdef TOOLS_MEM
396  mem::decrement(s_class().c_str());
397 #endif
398  }
399  protected:
400  std_vector_column_ref(const std_vector_column_ref& a_from)
401  :icol(a_from)
402  ,m_branch(a_from.m_barnch)
403  ,m_ref(a_from.m_ref)
404  ,m_leaf(0)
405  ,m_leaf_count(0)
406  {}
407  std_vector_column_ref& operator=(const std_vector_column_ref& a_from){
408  if(&a_from==this) return *this;
409  m_leaf = 0;
410  m_leaf_count = 0;
411  return *this;
412  }
413  public:
414  const std::vector<T>& variable() const {return m_ref;}
415  std::vector<T>& variable() {return const_cast< std::vector<T>& >(m_ref);}
416  protected:
417  branch& m_branch;
418  const std::vector<T>& m_ref;
419  base_leaf* m_leaf;
420  leaf<int>* m_leaf_count; //row_wise.
421  };
422 
423  template <class T>
424  class std_vector_column : public std_vector_column_ref<T> {
425  typedef std_vector_column_ref<T> parent;
426 #ifdef TOOLS_MEM
427  static const std::string& s_class() {
428  static const std::string s_v("tools::wroot::ntuple::std_vector_column<"+stype(T())+">");
429  return s_v;
430  }
431 #endif
432  public:
433  static cid id_class() {return _cid_std_vector<T>();}
434  virtual void* cast(cid a_class) const {
435  if(void* p = cmp_cast<std_vector_column>(this,a_class)) {return p;}
436  else return 0;
437  }
438  virtual cid id_cls() const {return id_class();}
439  public: //icol
440  virtual void set_def() {m_value = m_def;}
441  public:
442  std_vector_column(branch& a_branch,const std::string& a_name,const std::vector<T>& a_def)
443  :parent(a_branch,a_name,m_value)
444  ,m_def(a_def),m_value(a_def)
445  {
446 #ifdef TOOLS_MEM
447  mem::increment(s_class().c_str());
448 #endif
449  }
450  virtual ~std_vector_column(){
451 #ifdef TOOLS_MEM
452  mem::decrement(s_class().c_str());
453 #endif
454  }
455  protected:
456  std_vector_column(const std_vector_column& a_from)
457  :icol(a_from)
458  ,parent(a_from)
459  ,m_def(a_from.m_def)
460  ,m_value(a_from.m_value)
461  {}
462  std_vector_column& operator=(const std_vector_column& a_from){
463  if(&a_from==this) return *this;
464  parent::operator=(a_from);
465  m_def = a_from.m_def;
466  m_value = a_from.m_value;
467  return *this;
468  }
469  public:
470  std_vector_column& operator=(const std::vector<T>& a_value){m_value = a_value;return *this;}
471  bool fill(const std::vector<T>& a_value) {m_value = a_value;return true;}
472  protected:
473  std::vector<T> m_def;
474  std::vector<T> m_value;
475  };