g4tools  5.4.0
base_pntuple_column_wise
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_wroot_base_pntuple_column_wise
5 #define tools_wroot_base_pntuple_column_wise
6 
7 // pntuple = for parallel ntupling.
8 
9 #include "base_pntuple"
10 
11 #include "../ntuple_booking"
12 
13 #ifdef TOOLS_MEM
14 #include "../mem"
15 #endif
16 
17 namespace tools {
18 namespace wroot {
19 
21  typedef base_pntuple parent;
22 public:
23  class file {
24  public:
25  file(std::ostream& a_out,bool a_byte_swap,uint32 a_compression,bool a_verbose)
26  :m_out(a_out)
27  ,m_byte_swap(a_byte_swap),m_compression(a_compression),m_verbose(a_verbose)
28  {}
29  virtual ~file(){}
30  public:
31  file(const file& a_from)
32  :m_out(a_from.m_out)
34  {}
35  file& operator=(const file& a_from) {
36  m_byte_swap = a_from.m_byte_swap;
37  m_verbose = a_from.m_verbose;
38  return *this;
39  }
40  public:
41  bool verbose() const {return m_verbose;}
42  std::ostream& out() const {return m_out;}
43  bool byte_swap() const {return m_byte_swap;}
44  uint32 compression() const {return m_compression;}
45  protected:
46  std::ostream& m_out;
49  bool m_verbose;
50  };
51 
52 public:
53  base_pntuple_column_wise(std::ostream& a_out,bool a_byte_swap,uint32 a_compression,seek a_seek_directory,
54  const std::string& a_name,const std::string& a_title,bool a_verbose)
55  :parent(a_out,a_seek_directory,a_name,a_title)
56  ,m_file(a_out,a_byte_swap,a_compression,a_verbose)
57  {}
58  base_pntuple_column_wise(std::ostream& a_out,bool a_byte_swap,uint32 a_compression,seek a_seek_directory,
59  const std::vector<uint32>& a_basket_sizes,
60  const ntuple_booking& a_bkg,bool a_verbose)
61  :parent(a_out,a_seek_directory,a_bkg.name(),a_bkg.title())
62  ,m_file(a_out,a_byte_swap,a_compression,a_verbose)
63  {
64  const std::vector<column_booking>& cols = a_bkg.columns();
65 
66  if(a_basket_sizes.size()!=cols.size()) {
67  a_out << "tools::wroot::base_pntuple_column_wise :"
68  << " a_basket_sizes.size() (" << a_basket_sizes.size() << ") != "
69  << "a_bkg.columns().size() (" << a_bkg.columns().size() << ")."
70  << std::endl;
71  return;
72  }
73  std::vector<uint32>::const_iterator itb = a_basket_sizes.begin();
74 
76 
77 #define TOOLS_WROOT_PNTUPLE_CREATE_COL(a__type) \
78  if((*it).cls_id()==_cid(a__type())) {\
79  a__type* user = (a__type*)(*it).user_obj();\
80  if(user) {\
81  if(!create_column_ref<a__type>(*itb,(*it).name(),*user)) {\
82  a_out << "tools::wroot::base_pntuple_column_wise : create_column_ref(" << (*it).name() << ") failed." << std::endl;\
83  safe_clear<icol>(m_cols);\
84  safe_clear<branch>(m_branches);\
85  return;\
86  }\
87  itb++;\
88  } else {\
89  if(!create_column<a__type>(*itb,(*it).name())) {\
90  a_out << "tools::wroot::base_pntuple_column_wise : create_column(" << (*it).name() << ") failed." << std::endl;\
91  safe_clear<icol>(m_cols);\
92  safe_clear<branch>(m_branches);\
93  return;\
94  }\
95  itb++;\
96  }\
97  }
98 
99 #define TOOLS_WROOT_PNTUPLE_CREATE_VEC_COL(a__type) \
100  if((*it).cls_id()==_cid_std_vector<a__type>()) {\
101  std::vector<a__type>* vec = (std::vector<a__type>*)(*it).user_obj();\
102  if(vec) {\
103  if(!create_column_vector_ref<a__type>(*itb,(*it).name(),*vec)) {\
104  a_out << "tools::wroot::base_pntuple_column_wise :"\
105  << " create_column failed for std::vector column_ref " << sout((*it).name()) << "."\
106  << std::endl;\
107  safe_clear<icol>(m_cols);\
108  safe_clear<branch>(m_branches);\
109  return;\
110  }\
111  itb++;\
112  } else {\
113  if(!create_column_vector<a__type>(*itb,(*it).name())) {\
114  a_out << "tools::wroot::base_pntuple_column_wise :"\
115  << " create_column failed for std::vector column " << sout((*it).name()) << "."\
116  << std::endl;\
117  safe_clear<icol>(m_cols);\
118  safe_clear<branch>(m_branches);\
119  return;\
120  }\
121  itb++;\
122  }\
123  }
124 
129  else TOOLS_WROOT_PNTUPLE_CREATE_COL(double)
130 
131  else if((*it).cls_id()==_cid(std::string())) {
132  std::string* user = (std::string*)(*it).user_obj();
133  if(user) {
134  if(!create_column_string_ref(*itb,(*it).name(),*user)) {
135  a_out << "tools::wroot::base_pntuple_column_wise : create_column_string_ref(" << (*it).name() << ") failed."
136  << std::endl;
137  safe_clear<icol>(m_cols);
138  safe_clear<branch>(m_branches);
139  return;
140  }
141  itb++;
142  } else {
143  if(!create_column_string(*itb,(*it).name())) {
144  a_out << "tools::wroot::base_pntuple_column_wise : create_column_string(" << (*it).name() << ") failed." << std::endl;
145  safe_clear<icol>(m_cols);
146  safe_clear<branch>(m_branches);
147  return;
148  }
149  itb++;
150  }
151  }
152 
158 
159  else if((*it).cls_id()==_cid_std_vector<std::string>()) {\
160  std::vector<std::string>* user = (std::vector<std::string>*)(*it).user_obj();
161  char sep = '\n';
162  if(user) {
163  if(!create_column_vector_string_ref(*itb,(*it).name(),*user,sep)) {
164  a_out << "tools::wroot::base_pntuple_column_wise :"
165  << " create_column_vector_string_ref(" << (*it).name() << ") failed." << std::endl;
166  safe_clear<icol>(m_cols);
167  safe_clear<branch>(m_branches);
168  return;
169  }
170  itb++;
171  } else {
172  if(!create_column_vector_string(*itb,(*it).name(),std::vector<std::string>(),sep)) {
173  a_out << "tools::wroot::base_pntuple_column_wise :"
174  << " create_column_vector_string(" << (*it).name() << ") failed." << std::endl;
175  safe_clear<icol>(m_cols);
176  safe_clear<branch>(m_branches);
177  return;
178  }
179  itb++;
180  }
181  }
182 
183  // no leaf_store_class() defined for the other types.
184 
185  else {
186  a_out << "tools::wroot::base_pntuple_column_wise :"
187  << " for column " << sout((*it).name())
188  << ", type with cid " << (*it).cls_id() << " not yet handled."
189  << std::endl;
190  //throw
191  safe_clear<icol>(m_cols);
192  safe_clear<branch>(m_branches);
193  return;
194  }
195 
196 #undef TOOLS_WROOT_PNTUPLE_CREATE_COL
197 #undef TOOLS_WROOT_PNTUPLE_CREATE_VEC_COL
198 
199  }
200  }
201 
202  virtual ~base_pntuple_column_wise() {safe_clear<branch>(m_branches);}
203 protected:
206 public:
207  template <class T>
208  column_ref<T>* create_column_ref(uint32 a_basket_size,const std::string& a_name,const T& a_ref) {
209  if(find_named<icol>(m_cols,a_name)) return 0;
210  branch* _branch = new branch(m_file.out(),m_file.byte_swap(),m_file.compression(),
212  _branch->set_basket_size(a_basket_size);
213  column_ref<T>* col = new column_ref<T>(*_branch,a_name,a_ref);
214  if(!col) {delete _branch;return 0;}
215  m_branches.push_back(_branch);
216  m_cols.push_back(col);
217  return col;
218  }
219 
220  template <class T>
221  column<T>* create_column(uint32 a_basket_size,const std::string& a_name,const T& a_def = T()) {
222  if(find_named<icol>(m_cols,a_name)) return 0;
223  branch* _branch = new branch(m_file.out(),m_file.byte_swap(),m_file.compression(),
225  _branch->set_basket_size(a_basket_size);
226  column<T>* col = new column<T>(*_branch,a_name,a_def);
227  if(!col) {delete _branch;return 0;}
228  m_branches.push_back(_branch);
229  m_cols.push_back(col);
230  return col;
231  }
232 
233  column_string_ref* create_column_string_ref(uint32 a_basket_size,const std::string& a_name,const std::string& a_ref) {
234  if(find_named<icol>(m_cols,a_name)) return 0;
235  branch* _branch = new branch(m_file.out(),m_file.byte_swap(),m_file.compression(),
237  _branch->set_basket_size(a_basket_size);
238  column_string_ref* col = new column_string_ref(*_branch,a_name,a_ref);
239  if(!col) {delete _branch;return 0;}
240  m_branches.push_back(_branch);
241  m_cols.push_back(col);
242  return col;
243  }
244 
245  column_string* create_column_string(uint32 a_basket_size,
246  const std::string& a_name,
247  const std::string& a_def = std::string()) {
248  if(find_named<icol>(m_cols,a_name)) return 0;
249  branch* _branch = new branch(m_file.out(),m_file.byte_swap(),m_file.compression(),
251  _branch->set_basket_size(a_basket_size);
252  column_string* col = new column_string(*_branch,a_name,a_def);
253  if(!col) {delete _branch;return 0;}
254  m_branches.push_back(_branch);
255  m_cols.push_back(col);
256  return col;
257  }
258 
259  column_vector_string_ref* create_column_vector_string_ref(uint32 a_basket_size,const std::string& a_name,
260  const std::vector<std::string>& a_ref,char a_sep) {
261  if(find_named<icol>(m_cols,a_name)) return 0;
262  branch* _branch = new branch(m_file.out(),m_file.byte_swap(),m_file.compression(),
264  _branch->set_basket_size(a_basket_size);
265  column_vector_string_ref* col = new column_vector_string_ref(*_branch,a_name,a_ref,a_sep);
266  if(!col) {delete _branch;return 0;}
267  m_branches.push_back(_branch);
268  m_cols.push_back(col);
269  return col;
270  }
271 
272  column_vector_string* create_column_vector_string(uint32 a_basket_size,
273  const std::string& a_name,
274  const std::vector<std::string>& a_def,char a_sep) {
275  if(find_named<icol>(m_cols,a_name)) return 0;
276  branch* _branch = new branch(m_file.out(),m_file.byte_swap(),m_file.compression(),
278  _branch->set_basket_size(a_basket_size);
279  column_vector_string* col = new column_vector_string(*_branch,a_name,a_def,a_sep);
280  if(!col) {delete _branch;return 0;}
281  m_branches.push_back(_branch);
282  m_cols.push_back(col);
283  return col;
284  }
285 
286  template <class T>
287  std_vector_column_ref<T>* create_column_vector_ref(uint32 a_basket_size,const std::string& a_name,const std::vector<T>& a_ref) {
288  if(find_named<icol>(m_cols,a_name)) return 0;
290  m_seek_directory,a_name,m_name,a_ref,m_file.verbose());
291  _branch->set_basket_size(a_basket_size);
292  std_vector_column_ref<T>* col = new std_vector_column_ref<T>(*_branch,a_name,a_ref);
293  if(!col) {delete _branch;return 0;}
294  m_branches.push_back(_branch);
295  m_cols.push_back(col);
296  return col;
297  }
298 
299  template <class T>
300  std_vector_column<T>* create_column_vector(uint32 a_basket_size,const std::string& a_name,const std::vector<T>& a_def = std::vector<T>()) {
301  if(find_named<icol>(m_cols,a_name)) return 0;
304  _branch->set_basket_size(a_basket_size);
305  std_vector_column<T>* col = new std_vector_column<T>(*_branch,a_name,a_def);
306  if(!col) {delete _branch;return 0;}
307  _branch->set_pointer(&(col->variable()));
308  m_branches.push_back(_branch);
309  m_cols.push_back(col);
310  return col;
311  }
312 protected:
314  std::vector<branch*> m_branches;
315 };
316 
317 }}
318 
319 #endif
tools::wroot::seek
int64 seek
Definition: seek:16
tools::wroot::base_pntuple_column_wise::create_column
column< T > * create_column(uint32 a_basket_size, const std::string &a_name, const T &a_def=T())
Definition: base_pntuple_column_wise:221
tools::wroot::base_pntuple_column_wise::file::file
file(std::ostream &a_out, bool a_byte_swap, uint32 a_compression, bool a_verbose)
Definition: base_pntuple_column_wise:25
tools::wroot::base_pntuple_column_wise::base_pntuple_column_wise
base_pntuple_column_wise(std::ostream &a_out, bool a_byte_swap, uint32 a_compression, seek a_seek_directory, const std::vector< uint32 > &a_basket_sizes, const ntuple_booking &a_bkg, bool a_verbose)
Definition: base_pntuple_column_wise:58
tools::_cid
cid _cid(byte)
Definition: cids:14
tools::wroot::base_pntuple_column_wise::file::m_verbose
bool m_verbose
Definition: base_pntuple_column_wise:49
tools::column_booking
Definition: ntuple_booking:17
TOOLS_WROOT_PNTUPLE_CREATE_VEC_COL
#define TOOLS_WROOT_PNTUPLE_CREATE_VEC_COL(a__type)
tools::wroot::branch
Definition: branch:16
tools::wroot::base_pntuple_column_wise::create_column_vector
std_vector_column< T > * create_column_vector(uint32 a_basket_size, const std::string &a_name, const std::vector< T > &a_def=std::vector< T >())
Definition: base_pntuple_column_wise:300
tools::wroot::base_pntuple::m_name
std::string m_name
Definition: base_pntuple:124
TOOLS_WROOT_PNTUPLE_CREATE_COL
#define TOOLS_WROOT_PNTUPLE_CREATE_COL(a__type)
tools::wroot::base_pntuple_column_wise::file::~file
virtual ~file()
Definition: base_pntuple_column_wise:29
tools::wroot::base_pntuple_column_wise::create_column_vector_string_ref
column_vector_string_ref * create_column_vector_string_ref(uint32 a_basket_size, const std::string &a_name, const std::vector< std::string > &a_ref, char a_sep)
Definition: base_pntuple_column_wise:259
tools::wroot::base_pntuple_column_wise::file::m_compression
uint32 m_compression
Definition: base_pntuple_column_wise:48
tools::wroot::base_pntuple_column_wise::file::file
file(const file &a_from)
Definition: base_pntuple_column_wise:31
tools::wroot::base_pntuple_column_wise::m_file
file m_file
Definition: base_pntuple_column_wise:313
tools::wroot::base_pntuple_column_wise::file::m_out
std::ostream & m_out
Definition: base_pntuple_column_wise:46
tools::wroot::base_pntuple_column_wise::base_pntuple_column_wise
base_pntuple_column_wise(std::ostream &a_out, bool a_byte_swap, uint32 a_compression, seek a_seek_directory, const std::string &a_name, const std::string &a_title, bool a_verbose)
Definition: base_pntuple_column_wise:53
tools::wroot::base_pntuple_column_wise::file
Definition: base_pntuple_column_wise:23
tools::wroot::std_vector_be_ref
Definition: branch_element:80
tools::wroot::base_pntuple_column_wise::operator=
base_pntuple_column_wise & operator=(const base_pntuple_column_wise &)
Definition: base_pntuple_column_wise:205
base_pntuple
tools::wroot::base_pntuple_column_wise::base_pntuple_column_wise
base_pntuple_column_wise(const base_pntuple_column_wise &a_from)
Definition: base_pntuple_column_wise:204
tools::wroot::base_pntuple_column_wise::~base_pntuple_column_wise
virtual ~base_pntuple_column_wise()
Definition: base_pntuple_column_wise:202
tools::wroot::base_pntuple_column_wise::file::compression
uint32 compression() const
Definition: base_pntuple_column_wise:44
tools::sout
Definition: sout:17
tools::wroot::std_vector_be_pointer
Definition: branch_element:183
tools::wroot::base_pntuple_column_wise::file::operator=
file & operator=(const file &a_from)
Definition: base_pntuple_column_wise:35
tools::wroot::base_pntuple_column_wise
Definition: base_pntuple_column_wise:20
tools::ntuple_booking::columns
const std::vector< column_booking > & columns() const
Definition: ntuple_booking:93
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::wroot::base_pntuple
Definition: base_pntuple:21
tools::wroot::base_pntuple_column_wise::file::byte_swap
bool byte_swap() const
Definition: base_pntuple_column_wise:43
tools::wroot::branch::set_basket_size
void set_basket_size(uint32 a_size)
Definition: branch:193
tools::wroot::base_pntuple_column_wise::m_branches
std::vector< branch * > m_branches
Definition: base_pntuple_column_wise:314
tools::wroot::base_pntuple::m_seek_directory
seek m_seek_directory
Definition: base_pntuple:123
tools::wroot::base_pntuple_column_wise::create_column_string
column_string * create_column_string(uint32 a_basket_size, const std::string &a_name, const std::string &a_def=std::string())
Definition: base_pntuple_column_wise:245
tools::wroot::base_pntuple_column_wise::create_column_vector_ref
std_vector_column_ref< T > * create_column_vector_ref(uint32 a_basket_size, const std::string &a_name, const std::vector< T > &a_ref)
Definition: base_pntuple_column_wise:287
tools::wroot::base_pntuple_column_wise::create_column_string_ref
column_string_ref * create_column_string_ref(uint32 a_basket_size, const std::string &a_name, const std::string &a_ref)
Definition: base_pntuple_column_wise:233
tools::wroot::base_pntuple::m_cols
std::vector< icol * > m_cols
Definition: base_pntuple:126
tools::wroot::base_pntuple_column_wise::create_column_vector_string
column_vector_string * create_column_vector_string(uint32 a_basket_size, const std::string &a_name, const std::vector< std::string > &a_def, char a_sep)
Definition: base_pntuple_column_wise:272
tools::wroot::std_vector_be_pointer::set_pointer
void set_pointer(std::vector< T > *a_pointer)
Definition: branch_element:237
tools::sep
const std::string & sep()
Definition: sep:11
tools_vforcit
#define tools_vforcit(a__T, a__v, a__it)
Definition: forit:7
tools::wroot::base_pntuple_column_wise::file::m_byte_swap
bool m_byte_swap
Definition: base_pntuple_column_wise:47
tools::wroot::base_pntuple_column_wise::file::verbose
bool verbose() const
Definition: base_pntuple_column_wise:41
tools::uint32
unsigned int uint32
Definition: typedefs:71
tools::ntuple_booking
Definition: ntuple_booking:49
tools::wroot::base_pntuple_column_wise::create_column_ref
column_ref< T > * create_column_ref(uint32 a_basket_size, const std::string &a_name, const T &a_ref)
Definition: base_pntuple_column_wise:208
tools::wroot::base_pntuple_column_wise::file::out
std::ostream & out() const
Definition: base_pntuple_column_wise:42