1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
4 // common columns code for ntuple and base_pntuple.
7 class column_ref : public virtual icol {
9 static const std::string& s_class() {
10 static const std::string s_v("tools::wroot::ntuple::column_ref<"+stype(T())+">");
15 static cid id_class() {
16 static const T s_v = T(); //do that for T = std::string.
17 return _cid(s_v)+10000;
19 virtual void* cast(cid a_class) const {
20 if(void* p = cmp_cast<column_ref>(this,a_class)) {return p;}
23 virtual cid id_cls() const {return id_class();}
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;}
32 column_ref(branch& a_branch,const std::string& a_name,const T& a_ref)
33 :m_branch(a_branch),m_leaf(0)
36 mem::increment(s_class().c_str());
38 m_leaf = m_branch.create_leaf_ref<T>(a_name,a_ref);
40 virtual ~column_ref(){
42 mem::decrement(s_class().c_str());
46 column_ref(const column_ref& a_from)
48 ,m_branch(a_from.m_branch)
51 column_ref& operator=(const column_ref& a_from){
52 if(&a_from==this) return *this;
57 const T& variable() const {return m_leaf->variable();}
58 T& variable() {return m_leaf->variable();}
65 class column : public column_ref<T> {
66 typedef column_ref<T> parent;
68 static const std::string& s_class() {
69 static const std::string s_v("tools::wroot::ntuple::column<"+stype(T())+">");
74 static cid id_class() {
75 static const T s_v = T(); //do that for T = std::string.
78 virtual void* cast(cid a_class) const {
79 if(void* p = cmp_cast<column>(this,a_class)) {return p;}
82 virtual cid id_cls() const {return id_class();}
84 virtual void set_def() {m_value = m_def;}
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)
91 mem::increment(s_class().c_str());
96 mem::decrement(s_class().c_str());
100 column(const column& a_from)
104 ,m_value(a_from.m_value)
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;
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;}
121 class column_string_ref : public virtual icol {
123 static const std::string& s_class() {
124 static const std::string s_v("tools::wroot::ntuple::column_string_ref");
129 static cid id_class() {
130 static const std::string s_v;
131 return _cid(s_v)+10000;
133 virtual void* cast(cid a_class) const {
134 if(void* p = cmp_cast<column_string_ref>(this,a_class)) {return p;}
137 virtual cid id_cls() const {return id_class();}
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;}
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)
150 mem::increment(s_class().c_str());
152 m_leaf = m_branch.create_leaf_string_ref(a_name,a_ref);
154 virtual ~column_string_ref(){
156 mem::decrement(s_class().c_str());
160 column_string_ref(const column_string_ref& a_from)
162 ,m_branch(a_from.m_branch)
165 column_string_ref& operator=(const column_string_ref& a_from){
166 if(&a_from==this) return *this;
171 const std::string& variable() const {return m_leaf->variable();}
172 std::string& variable() {return m_leaf->variable();}
175 leaf_string_ref* m_leaf;
178 class column_string : public column_string_ref {
179 typedef column_string_ref parent;
181 static const std::string& s_class() {
182 static const std::string s_v("tools::wroot::ntuple::column_string");
187 static cid id_class() {
188 static const std::string s_v;
191 virtual void* cast(cid a_class) const {
192 if(void* p = cmp_cast<column_string>(this,a_class)) {return p;}
195 virtual cid id_cls() const {return id_class();}
197 virtual void set_def() {m_value = m_def;}
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)
204 mem::increment(s_class().c_str());
207 virtual ~column_string(){
209 mem::decrement(s_class().c_str());
213 column_string(const column_string& a_from)
217 ,m_value(a_from.m_value)
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;
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;}
234 class column_vector_string_ref : public column_string_ref {
235 typedef column_string_ref parent;
237 static const std::string& s_class() {
238 static const std::string s_v("tools::wroot::ntuple::column_vector_string_ref");
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;}
248 virtual cid id_cls() const {return id_class();}
252 tools_vforcit(std::string,m_ref,it) {
253 if(it!=m_ref.begin()) m_string += m_sep;
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)
265 mem::increment(s_class().c_str());
268 virtual ~column_vector_string_ref(){
270 mem::decrement(s_class().c_str());
274 column_vector_string_ref(const column_vector_string_ref& a_from)
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;
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);}
289 const std::vector<std::string>& m_ref;
291 std::string m_string;
294 class column_vector_string : public column_vector_string_ref {
295 typedef column_vector_string_ref parent;
297 static const std::string& s_class() {
298 static const std::string s_v("tools::wroot::ntuple::column_vector_string");
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;}
308 virtual cid id_cls() const {return id_class();}
310 virtual void set_def() {m_value = m_def;}
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)
317 mem::increment(s_class().c_str());
320 virtual ~column_vector_string(){
322 mem::decrement(s_class().c_str());
326 column_vector_string(const column_vector_string& a_from)
330 ,m_value(a_from.m_value)
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;
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;}
343 std::vector<std::string> m_def;
344 std::vector<std::string> m_value;
348 class std_vector_column_ref : public virtual icol {
350 static const std::string& s_class() {
351 static const std::string s_v("tools::wroot::ntuple::std_vector_column_ref<"+stype(T())+">");
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;}
361 virtual cid id_cls() const {return id_class();}
364 if(m_leaf_count) m_leaf_count->fill((int)m_ref.size()); //row_wise
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;}
372 std_vector_column_ref(branch& a_branch,const std::string& a_name,const std::vector<T>& a_ref)
376 ,m_leaf_count(0) //row_wise
379 mem::increment(s_class().c_str());
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.
385 m_leaf = m_branch.create_leaf_element(a_name,id,type);
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.
394 virtual ~std_vector_column_ref(){
396 mem::decrement(s_class().c_str());
400 std_vector_column_ref(const std_vector_column_ref& a_from)
402 ,m_branch(a_from.m_barnch)
407 std_vector_column_ref& operator=(const std_vector_column_ref& a_from){
408 if(&a_from==this) return *this;
414 const std::vector<T>& variable() const {return m_ref;}
415 std::vector<T>& variable() {return const_cast< std::vector<T>& >(m_ref);}
418 const std::vector<T>& m_ref;
420 leaf<int>* m_leaf_count; //row_wise.
424 class std_vector_column : public std_vector_column_ref<T> {
425 typedef std_vector_column_ref<T> parent;
427 static const std::string& s_class() {
428 static const std::string s_v("tools::wroot::ntuple::std_vector_column<"+stype(T())+">");
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;}
438 virtual cid id_cls() const {return id_class();}
440 virtual void set_def() {m_value = m_def;}
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)
447 mem::increment(s_class().c_str());
450 virtual ~std_vector_column(){
452 mem::decrement(s_class().c_str());
456 std_vector_column(const std_vector_column& a_from)
460 ,m_value(a_from.m_value)
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;
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;}
473 std::vector<T> m_def;
474 std::vector<T> m_value;