ATLAS Offline Software
Loading...
Searching...
No Matches
TableUtils.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4#ifndef TABLE_UTILS_H
5#define TABLE_UTILS_H
6
7#include <array>
8#include <string>
9#include <cassert>
10#include <iomanip>
11#include <ostream>
12#include <stdexcept>
13#include <vector>
14#include <utility>
15#include <limits>
16
17class MsgStream;
18
19// Utility class to wrap a constant variable length array interface over static sized arrays
20// This avoid duplication of compiled code for arrays which only differ
21// by the number of elements.
22// The interface is sufficient to allow using the wrapped arrays in range based for loops,
23// and for random access.
24namespace TableUtils {
25 template <typename T>
26 struct Range {
27 const T *m_ptr = nullptr;
28 std::size_t m_size = 0u;
29 std::size_t m_offset = 1u;
30
32 const_iterator &operator++() { m_ptr += m_offset; return *this; }
33 const T &operator*() const { return *m_ptr; }
34 bool operator!=(const const_iterator &other) const { return m_ptr != other.m_ptr; }
35 const T *m_ptr;
36 std::size_t m_offset;
37 };
39 return const_iterator{ m_ptr, m_offset };
40 }
42 assert( (m_size % m_offset) == 0 );
44 }
45 const T &operator[](std::size_t index) const { assert(index<m_size && m_ptr); return m_ptr[index]; }
46
47 std::size_t size() const { return m_size; }
48
49 operator bool() const { return m_ptr != nullptr && m_size>0; }
50 template <typename T_Other>
51 bool equalSize(const T_Other &other_range) {
52 return m_size == other_range.m_size;
53 }
54 };
55
56 // Utility class to wrap a constant two dimensional variable length array interface over static sized arrays
57 // This avoid duplication of compiled code for arrays which only differ
58 // by the number of elements in the two dimensions.
59 // The interface is sufficient to allow using the wrapped arrays in range based for loops,
60 // and for random access.
61 template <typename T>
62 struct Range2D {
63 const T *m_ptr = nullptr;
64 std::size_t m_rows = 0u;
65 std::size_t m_columns = 0u;
66 std::size_t m_columnOffset = 0u;
67 std::size_t m_firstColumnIndex = 0u;
68 std::size_t m_offset = 1u;
69
73 bool operator!=(const const_iterator &other) const { return m_ptr != other.m_ptr; }
74 const T *m_ptr;
75 std::size_t m_columns;
76 std::size_t m_columnOffset;
77 std::size_t m_firstColumnIndex;
78 std::size_t m_offset;
79 };
80
87 Range<T> operator[](std::size_t index) const {
88 assert(index<m_rows && m_ptr);
90 }
91
92 std::size_t nColumns() const { return m_columns; }
93 std::size_t nRows() const { return m_rows; }
94
95 operator bool() { return m_ptr != nullptr && m_columns>0; }
96 template <typename T_Other>
97 bool equalSize(const T_Other &other_range) {
98 return m_rows == other_range.m_size;
99 }
100 };
101
102 // Helper method to print wrapped static arrays in table form to an output stream
103 // The table will be composed of one column for row labels and one column for the data
104 template <class T_Stream, typename T_Counter>
105 T_Stream &dumpTable(T_Stream &out,
106 Range<T_Counter> counter,
108 const std::string &label_prefix,
109 const std::size_t column_width,
110 const std::size_t min_label_width,
111 const bool dump_footer,
112 const bool separate_last_row,
113 const unsigned int precision) {
114 if (counter && label && counter.equalSize(label)) {
115 std::size_t max_size =min_label_width;
116 for (const std::string &name : label ) {
117 max_size = std::max(max_size, name.size());
118 }
119 const std::size_t total_size =max_size+3+2*2+column_width;
120 std::string line(total_size, '-');
121 std::array<std::size_t,3> vertical_line_pos{0u, max_size+3, line.size()-1};
122 for (std::size_t pos : vertical_line_pos) {
123 line[pos]='|';
124 }
125 out << line << std::endl;
126 std::size_t idx=0;
127 std::string empty;
128 auto default_precision = out.precision();
129 for (const T_Counter &a : counter) {
130 if (separate_last_row && idx+1 == label.size()) {
131 out << line << std::endl;
132 }
133 assert( idx < label.size());
134 out << "| " << (label_prefix.empty() ? std::left : std::right)
135 << std::setw(label_prefix.size()) << ( idx==0 ? label_prefix : empty)
136 << std::setw(max_size-label_prefix.size()) << label[idx] << std::right
137 << std::setprecision( precision != std::numeric_limits<unsigned int>::max() ? precision : default_precision)
138 << " | " << std::setw(column_width) << a << " |" << std::endl;
139 ++idx;
140 }
141 out << std::setprecision(default_precision);
142 if (dump_footer) {
143 out << line << std::endl;
144 }
145 }
146 return out;
147 }
148
149 // Helper method to print wrapped static two dimensional arrays in table form to an output stream
150 // The table will be composed of a header showing column labels, one column for row labels and
151 // then column for each data item. The array is expected in row major order.
152 template <class T_Stream, typename T_Counter>
153 T_Stream &dumpTable(T_Stream &out,
154 Range2D<T_Counter> counter,
155 const Range<std::string>& row_label,
156 const Range<std::string>& column_label,
157 const std::string &top_left_label,
158 const std::string &label_prefix,
159 const std::size_t column_width,
160 const std::size_t min_label_width,
161 const bool dump_header,
162 const bool dump_footer,
163 const bool separate_last_row,
164 const std::vector<unsigned int> &precision) {
165 if (counter && row_label && column_label
166 && counter.equalSize(row_label)
167 && counter.nColumns() == column_label.size()) {
168 std::size_t max_size =std::max(top_left_label.size(), static_cast<std::size_t>(min_label_width));
169 for (const std::string &name : row_label ) {
170 max_size = std::max(max_size, name.size() + label_prefix.size());
171 }
172 std::size_t the_width = column_width;
173 for (const std::string &name : column_label ) {
174 the_width = std::max(the_width, name.size());
175 }
176 std::size_t total_size =max_size+2*2;
177 for (std::size_t column_i=0; column_i<column_label.size(); ++column_i) {
178 total_size += the_width + 3;
179 }
180 std::string line(total_size, '-');
181 std::size_t pos=0;
182 line[pos]='|';
183 pos += max_size+3;
184 for (std::size_t column_i=0; column_i<column_label.size(); ++column_i) {
185 line[pos]='|';
186 pos += the_width + 3;
187 }
188 line[line.size()-1]='|';
189 if (dump_header) {
190 out << line << std::endl << "| " << std::setw(max_size) << top_left_label << " |" << std::left;
191 for (const std::string &header : column_label ) {
192 out << " " << std::setw(the_width) << header << " |";
193 }
194 out << std::right << std::endl;
195 }
196 out << line << std::endl;
197 std::size_t idx=0;
198 std::string empty;
199 auto default_precision = out.precision();
200 for (const Range<T_Counter> &a_row : counter) {
201 if (separate_last_row && idx+1 == row_label.size()) {
202 out << line << std::endl;
203 }
204 assert( idx < row_label.size());
205 out << "| " << (label_prefix.empty() ? std::left : std::right)
206 << std::setw(label_prefix.size()) << ( idx==0 ? label_prefix : empty)
207 << std::setw(max_size-label_prefix.size()) << row_label[idx] << std::right << " |";
208 unsigned int col_i=0;
209 for (const T_Counter &a : a_row) {
210 out << " "
211 << std::setprecision( (col_i < precision.size()
212 && precision[col_i] != std::numeric_limits<unsigned int>::max())
213 ? precision[col_i]
214 : default_precision)
215 << std::setw(the_width) << a << " |";
216 ++col_i;
217 }
218 out << std::endl;
219 ++idx;
220 }
221 out << std::setprecision(default_precision);
222 if (dump_footer) {
223 out << line;
224 }
225 }
226 return out;
227 }
228
229 // Helper struct to wrap data that should be dumped in table from to an output stream
230 template <typename T>
231 struct StatTable {
234 StatTable &columnWidth(std::size_t value) { m_columnWidth=value; return *this;}
235 StatTable &minLabelWidth(std::size_t value) { m_minLabelWidth=value; return *this;}
236 StatTable &dumpHeader(bool value=true) { m_dumpHeader=value; return *this;}
237 StatTable &dumpFooter(bool value=true) { m_dumpFooter=value; return *this;}
238 StatTable &separateLastRow(bool value=true) { m_separateLastRow=value; return *this;}
239 StatTable &labelPrefix(const std::string& value) { m_labelPrefix=value; return *this;}
240 StatTable &precision(unsigned int precision) { m_precision=precision; return *this;}
241
242 std::string m_labelPrefix {};
243 std::size_t m_columnWidth=12;
244 std::size_t m_minLabelWidth=0;
245 unsigned int m_precision = std::numeric_limits<unsigned int>::max();
246 bool m_dumpHeader=true;
247 bool m_dumpFooter=true;
249 };
250 // Helper struct to wrap two dimensional data that should be dumped in table from to an output stream
251 template <typename T>
256 std::string m_topLeftLable;
257 MultiColumnTable &columnWidth(std::size_t value) { m_columnWidth=value; return *this;}
258 MultiColumnTable &minLabelWidth(std::size_t value) { m_minLabelWidth=value; return *this;}
259 MultiColumnTable &dumpHeader(bool value=true) { m_dumpHeader=value; return *this;}
260 MultiColumnTable &dumpFooter(bool value=true) { m_dumpFooter=value; return *this;}
261 MultiColumnTable &separateLastRow(bool value=true) { m_separateLastRow=value; return *this;}
262 MultiColumnTable &labelPrefix(const std::string& value) { m_labelPrefix=value; return *this;}
263 MultiColumnTable &precision(std::vector<unsigned int> &&precision) { m_precision=std::move(precision); return *this;}
264
265 std::string m_labelPrefix {};
266 std::size_t m_columnWidth=12;
267 std::size_t m_minLabelWidth=0;
268 std::vector<unsigned int> m_precision{};
269 bool m_dumpHeader=true;
270 bool m_dumpFooter=true;
272
273 // convenience method to dump wrapped two dimensional arrays in table form to a std output stream
274 // Usage: out << makeTable( array2d, row_labels, column_labels);
275
276 friend inline std::ostream &operator<<(std::ostream &out,
277 const MultiColumnTable &stat) {
278 return dumpTable(out,
279 stat.m_counter,
280 stat.m_rowLabel,
281 stat.m_columnLabel,
282 stat.m_topLeftLable,
283 stat.m_labelPrefix,
284 stat.m_columnWidth,
285 stat.m_minLabelWidth,
286 stat.m_dumpHeader,
287 stat.m_dumpFooter,
288 stat.m_separateLastRow,
289 stat.m_precision);
290 }
291
292 };
293
294 template <typename T_index, class T_string>
295 std::vector<std::string> makeLabelVector(T_index n_entries,
296 std::initializer_list<std::pair<T_index, T_string> > a_list)
297 {
298 std::vector<std::string> labels;
299 labels.resize( n_entries );
300 if (a_list.size() != n_entries) {
301 throw std::logic_error("Expected number of entries and elements in the initializer lists do not match.");
302 }
303 for ( auto elm : a_list) {
304 labels.at(elm.first) = std::move(elm.second);
305 }
306 return labels;
307 }
308
309 template<class T_Collection>
310 std::size_t maxLabelWidth( const T_Collection &col) {
311 std::size_t max_width=0u;
312 for (const auto &elm : col ) {
313 max_width = std::max( max_width, elm.size());
314 }
315 return max_width;
316 }
317
318
319 constexpr inline std::size_t categoryStride([[maybe_unused]] const std::size_t categories,
320 [[maybe_unused]] const std::size_t sub_categories,
321 [[maybe_unused]] const std::size_t n_counter) {
322 return 1;
323 }
324 constexpr inline std::size_t subCategoryStride([[maybe_unused]] const std::size_t categories,
325 [[maybe_unused]] const std::size_t sub_categories,
326 [[maybe_unused]] const std::size_t n_counter) {
327 return (categories+1) * n_counter;
328 }
329 constexpr inline std::size_t counterStride([[maybe_unused]] const std::size_t categories,
330 [[maybe_unused]] const std::size_t sub_categories,
331 [[maybe_unused]] const std::size_t n_counter) {
332 return (categories+1);
333 }
334
335
336 // change order of input statistics counter and compute projections
337 // - order: change from array[category*category_stride+sub_category][counter]
338 // to array[sub_category*sub_category_stride+counter*counter_stride+category]
339 // - projections: sum numbers in direction of category and sub_category direction, respectively.
340 // - dimension of the resulting vector: (categories+1) * (sub_categories+1) * N
341 template< typename T_Output, typename T_Input, const std::size_t N>
342 std::vector<T_Output> createCounterArrayWithProjections( const std::size_t categories,
343 const std::size_t sub_categories,
344 const std::vector< std::array<T_Input, N> > &input_counts) {
345 if (categories*sub_categories!= input_counts.size()) {
346 std::stringstream msg;
347 msg << "Category dimensions (" << categories << " * " << sub_categories << "="
348 << (categories * sub_categories) << ") and input counter container size "
349 << input_counts.size() << " do not match.";
350 throw std::logic_error(msg.str());
351 }
352 std::vector<T_Output> output_counts;
353 output_counts.resize((categories+1) * (sub_categories+1) * N);
354 const std::size_t sub_category_stride = subCategoryStride(categories, sub_categories, N);
355 const std::size_t counter_stride = counterStride(categories, sub_categories, N);
356 const std::size_t category_stride = categoryStride(categories, sub_categories, N);
357 // project seeds
358 for (std::size_t sub_category_i=0;
359 sub_category_i < sub_categories;
360 ++sub_category_i) {
361 for (std::size_t category_i=0; category_i<categories; ++category_i) {
362 std::size_t src_idx = category_i * sub_categories + sub_category_i;
363 std::size_t dest_idx_base = sub_category_i * sub_category_stride + 0 * counter_stride;
364 std::size_t dest_idx_project_categories_base = dest_idx_base;
365 dest_idx_base += category_i * category_stride;
366 dest_idx_project_categories_base += categories * category_stride;
367
368 for (std::size_t counter_i=0; counter_i<N; ++counter_i) {
369 std::size_t dest_idx=dest_idx_base + counter_i * counter_stride;
370 assert( src_idx < input_counts.size() && counter_i < input_counts[src_idx].size());
371 assert( dest_idx < output_counts.size());
372 output_counts[dest_idx] = input_counts[src_idx][counter_i];
373 assert( dest_idx_project_categories_base + counter_i * counter_stride < output_counts.size());
374 output_counts[dest_idx_project_categories_base + counter_i * counter_stride] += output_counts[dest_idx];
375 }
376 }
377 }
378 // project eta bins
379 for (std::size_t category_i=0; category_i<=categories; ++category_i) {
380 for (std::size_t counter_i=0; counter_i<N; ++counter_i) {
381 std::size_t dest_idx_base = 0 * sub_category_stride + counter_i * counter_stride + category_i;
382 std::size_t dest_idx_project_sub_categories = sub_categories * sub_category_stride + dest_idx_base;
383 assert( dest_idx_project_sub_categories < output_counts.size() );
384 for (std::size_t sub_category_i=0;
385 sub_category_i<sub_categories;
386 ++sub_category_i) {
387 std::size_t sub_category_idx = dest_idx_base + sub_category_i * sub_category_stride;
388 assert( sub_category_idx < output_counts.size() );
389 output_counts[dest_idx_project_sub_categories] += output_counts[sub_category_idx];
390 }
391 }
392 }
393 return output_counts;
394 }
395
396 using SummandDefinition = std::pair<std::size_t, int>;
397 using RatioDefinition = std::pair< std::vector< SummandDefinition >,
398 std::vector< SummandDefinition > >;
399
400 // helper to create the sum definitions for the ratio of single counters
401 // this will lead to the computation of the simple ratio defined by counter[numerator] / counter[denominatpr]
402 template <typename T>
403 inline RatioDefinition
404 defineSimpleRatio(T numerator, T denominator) {
405 return std::make_pair( std::vector< SummandDefinition > { std::make_pair(static_cast<std::size_t>(numerator),1)},
406 std::vector< SummandDefinition > { std::make_pair(static_cast<std::size_t>(denominator),1)});
407 }
408
409 // helper to create a named ratio definition for a ratio of two single counters
410 template <typename T>
411 inline std::tuple< std::string, RatioDefinition >
412 defineSimpleRatio(std::string &&name, T numerator, T denominator) {
413 return std::make_pair( std::move(name), defineSimpleRatio(numerator, denominator) );
414 }
415
416
417 // helper to create a single summand definition to be used in a ratio definition
418 // @param counter_idx the index of a counter
419 // @param multiplier a multiplier to be applied to the value of the referenced counter e.g. +1 or -1.
420 // @return the definition of a single summand to be appended to a vector
421 template <typename T>
422 inline SummandDefinition defineSummand(T counter_idx, int multiplier) {
423 return std::make_pair(static_cast<std::size_t>(counter_idx), multiplier) ;
424 }
425
426 // compute the sum : sum_j stat[ eta_bin][stat_j][seed] * multiplier_j
427 // where the index stat_j is the first element of the pair, and the second element is multiplier_j
428 std::size_t computeSum( const std::vector< SummandDefinition > &sum_def,
429 std::size_t eta_offset,
430 std::size_t row_stride,
431 std::size_t seed_i,
432 const std::vector<std::size_t> &stat);
433
434 inline float computeRatio(std::size_t numerator, std::size_t denominator) {
435 return denominator!=0 ? static_cast<float>(numerator/static_cast<double>(denominator)) : 0.f;
436 }
437
438 // compute the ratio of two sums;
439 // @param ratio_def vectors which define summands for the numerator and denominator
440 // The first element of the pair defines the numberator, which is computed by summing the referenced
441 // counters (first element of the inner pair is the counter index) after multiplying them by the
442 // multiplier (second element of the inner pair). The second elment defines the denominator of
443 // the ratio.
444 inline float computeRatio( const RatioDefinition &ratio_def,
445 std::size_t eta_offset,
446 std::size_t row_stride,
447 std::size_t seed_i,
448 const std::vector<std::size_t> &stat) {
449 std::size_t numerator=computeSum(ratio_def.first, eta_offset, row_stride, seed_i, stat);
450 std::size_t denominator=!ratio_def.second.empty()
451 ? computeSum(ratio_def.second, eta_offset, row_stride, seed_i, stat)
452 : 1;
453 return computeRatio(numerator,denominator);
454 }
455
456
457 // helper function to define a named ratio
458 inline std::tuple< std::string, RatioDefinition> makeRatioDefinition(std::string &&name,
459 std::vector< SummandDefinition >&&numerator,
460 std::vector< SummandDefinition >&&denominator) {
461 return std::make_tuple(std::move(name),
462 std::make_pair(std::move(numerator),
463 std::move(denominator)));
464 }
465
466 // helper function to split a list of named ratio definitions into a vector of labels and ratio definitions
467 inline std::tuple<std::vector<std::string>, std::vector<RatioDefinition> >
468 splitRatioDefinitionsAndLabels(std::initializer_list<std::tuple<std::string, RatioDefinition> > a_ratio_list)
469 {
470 std::tuple< std::vector<std::string>, std::vector<RatioDefinition> > splitted;
471 std::get<0>(splitted).reserve( a_ratio_list.size() );
472 for ( auto a_ratio : a_ratio_list) {
473 std::get<0>(splitted).emplace_back( std::move(std::get<0>(a_ratio)) );
474 }
475 std::get<1>(splitted).reserve( a_ratio_list.size() );
476 for ( auto a_ratio : a_ratio_list) {
477 std::get<1>(splitted).emplace_back( std::move(std::get<1>(a_ratio)) );
478 }
479 return splitted;
480 }
481
482
483 constexpr inline std::size_t categoryStride([[maybe_unused]] const std::size_t categories,
484 [[maybe_unused]] const std::size_t sub_categories,
485 [[maybe_unused]] const std::vector<RatioDefinition> &ratio_def) {
486 return 1;
487 }
488 inline std::size_t subCategoryStride([[maybe_unused]] const std::size_t categories,
489 [[maybe_unused]] const std::size_t sub_categories,
490 [[maybe_unused]] const std::vector<RatioDefinition> &ratio_def) {
491 return (categories) * ratio_def.size();
492 }
493 constexpr inline std::size_t ratioStride([[maybe_unused]] const std::size_t categories,
494 [[maybe_unused]] const std::size_t sub_categories,
495 [[maybe_unused]] const std::vector<RatioDefinition> &ratio_def) {
496 return (categories);
497 }
498
499
500 std::vector<float> computeRatios(const std::vector<RatioDefinition> &ratio_def,
501 const std::size_t categories,
502 const std::size_t sub_categories,
503 const std::vector< std::size_t> &counter);
504
505 inline std::string makeBinLabel(const std::string &variable_name,
506 const std::vector<float> &bins,
507 std::size_t bin_i,
508 bool abs_value=false,
509 int precision=1) {
510 std::stringstream range_label;
511 range_label << std::fixed << std::setprecision(precision);
512 if (bin_i==bins.size()+1) {
513 range_label << " All " << variable_name;
514 }
515 else {
516 if (bin_i==0) {
517 std::stringstream value_str;
518 value_str << std::fixed << std::setprecision(precision) << 0.;
519 range_label << std::setw(4) << (abs_value ? value_str.str().c_str() : "-inf") << "-";
520 }
521 else {
522 range_label << std::setw(4) << bins.at(bin_i-1) <<"-";
523 }
524 if (bin_i>=bins.size()) {
525 range_label << std::setw(4) << "+inf";
526 }
527 else {
528 range_label << std::setw(4) << bins.at(bin_i);
529 }
530 }
531 return range_label.str();
532 }
533
534 inline std::string makeEtaBinLabel(const std::vector<float> &eta_bins,
535 std::size_t eta_bin_i,
536 bool abs_eta=false) {
537 return TableUtils::makeBinLabel("eta",eta_bins, eta_bin_i,abs_eta, 1);
538 }
539}
540// Helper method to wrap data that should be dumped in table form to an output stream
541// Usage: out << makeTable( array, labels);
542template <typename T, std::size_t N>
543TableUtils::StatTable<T> makeTable(const std::array<T, N> &counter,
544 const std::array<std::string, N> &label) {
546 TableUtils::Range<T> {counter.data(), counter.size()},
548 };
549}
550
551// Helper method to wrap two dimensional data that should be dumped in table form to an output stream
552template <typename T, std::size_t Nrows, std::size_t Ncolumns>
553TableUtils::MultiColumnTable<T> makeTable(const std::array<std::array<T, Ncolumns>, Nrows> &counter,
554 const std::array<std::string, Nrows> &row_label,
555 const std::array<std::string, Ncolumns> &column_label,
556 const std::string &top_left_label="") {
558 TableUtils::Range2D<T> {!counter.empty() ? counter[0].data() : nullptr,
559 counter.size(), column_label.size(),
560 !counter.empty() ? static_cast<std::size_t>(&counter[1][0] - &counter[0][0]) : 0u,
561 0u, // index of first column
562 1u}, // offset between columns
563 TableUtils::Range<std::string> {row_label.data(), row_label.size(), 1u},
564 TableUtils::Range<std::string> {column_label.data(), column_label.size(),1u},
565 top_left_label
566 };
567}
568
569// Helper method to wrap two dimensional data that should be dumped in table form to an output stream
570template <typename T>
571TableUtils::MultiColumnTable<T> makeTable(const std::vector<T> &counter,
572 std::size_t start_idx,
573 std::size_t row_stride,
574 const std::vector<std::string> &row_label,
575 const std::vector<std::string> &column_label,
576 const std::string &top_left_label="") {
577 if (start_idx + (row_label.size()-1) * row_stride >= counter.size() || row_stride < column_label.size()) {
578 std::stringstream msg;
579 msg << "Counter dimension and label dimensions (" << row_label.size() << " * " << column_label.size()
580 << ") do not match: [" << start_idx << ", "
581 << start_idx << " + " << (row_label.size()-1) << " * " << row_stride << " = "
582 << (start_idx + (row_label.size()-1) * row_stride)
583 << " !< " << counter.size();
584 msg << " [row_labels:";
585 for (const std::string &label : row_label) {
586 msg << " " << label;
587 }
588 msg << "; column_labels:";
589 for (const std::string &label : column_label) {
590 msg << " " << label;
591 }
592 msg << "]";
593 throw std::logic_error(msg.str());
594 }
596 TableUtils::Range2D<T> {!counter.empty() ? &counter[start_idx] : nullptr,
597 row_label.size(), // n-rows
598 column_label.size(), // n-columns
599 row_stride, // offset between rows
600 0u, // first column index
601 1u}, // offset between columns
602 TableUtils::Range<std::string> {row_label.data(), row_label.size() },
603 TableUtils::Range<std::string> {column_label.data(), column_label.size()},
604 top_left_label
605 };
606}
607
608template <typename T, std::size_t N>
609TableUtils::MultiColumnTable<T> makeTable(const std::vector<std::array<T,N> > &counter,
610 std::size_t start_row_idx,
611 std::size_t row_stride,
612 std::size_t start_column_idx,
613 std::size_t column_stride,
614 const std::vector<std::string> &row_label,
615 const std::vector<std::string> &column_label,
616 const std::string &top_left_label="") {
617 if (start_row_idx + (row_label.size()-1) * row_stride >= counter.size()*N
618 || start_column_idx + (column_label.size()-1) * column_stride >= counter.size()*N
619 || (row_stride*row_label.size()>column_stride && column_stride*column_label.size()>row_stride) ) {
620 std::stringstream msg;
621 msg << "Counter dimension and label dimensions (" << row_label.size() << " * " << column_label.size()
622 << ") do not match: [" << start_row_idx << ", "
623 << start_row_idx << " + " << (row_label.size()-1) << " * " << row_stride << " = "
624 << (start_row_idx + (row_label.size()-1) * row_stride)
625 << " or "
626 << start_column_idx << " + " << (column_label.size()-1) << " * " << column_stride << " = "
627 << (start_column_idx + (column_label.size()-1) * column_stride)
628 << " !< " << counter.size()
629 << std::endl
630 << (start_row_idx + (row_label.size()-1) * row_stride) << " >= " << (counter.size()*N)
631 << " || " << (start_column_idx + (column_label.size()-1) * column_stride) << " >= " << (counter.size()*N)
632 << " || ( " << (row_stride*row_label.size()) << " > " << column_stride << " && " << (column_stride*column_label.size()) << " > " << (row_stride)
633 << ")";
634 msg << " [row_labels:";
635 for (const std::string &label : row_label) {
636 msg << " " << label;
637 }
638 msg << "; column_labels:";
639 for (const std::string &label : column_label) {
640 msg << " " << label;
641 }
642 msg << "]";
643 throw std::logic_error(msg.str());
644 }
646 TableUtils::Range2D<T> {!counter.empty() && N>0 ? counter[start_row_idx].data() : nullptr,
647 row_label.size(), // n-rows
648 column_label.size(), // n-columns
649 row_stride, // offset between rows
650 start_column_idx, // first column index
651 column_stride}, // offset between columns
652 TableUtils::Range<std::string> {row_label.data(), row_label.size() },
653 TableUtils::Range<std::string> {column_label.data(), column_label.size()},
654 top_left_label};
655}
656
657
658// Helper method to wrap two dimensional data that should be dumped in table form to an output stream
659template <typename T>
660TableUtils::MultiColumnTable<T> makeTable(const std::vector<T> &counter,
661 const std::vector<std::string> &row_label,
662 const std::vector<std::string> &column_label,
663 const std::string &top_left_label="") {
664 return makeTable(counter, 0u, column_label.size(), row_label, column_label, top_left_label);
665}
666
667
668// convenience method to dump wrapped arrays in table form to a MsgStream
669// Usage: msg(MSG::INFO) << makeTable( array, labels);
670// ATH_MSG_INFO( makeTable( array, labels) );
671template <typename T>
672inline MsgStream &operator<<(MsgStream &out,
673 const TableUtils::StatTable<T> &stat)
674{
675 return dumpTable(out,
676 stat.m_counter,
677 stat.m_label,
678 stat.m_labelPrefix,
679 stat.m_columnWidth,
680 stat.m_minLabelWidth,
681 stat.m_dumpFooter,
682 stat.m_separateLastRow,
683 stat.m_precision);
684}
685
686// convenience method to dump wrapped two dimensional arrays in table form to a MsgStream
687// Usage: msg(MSG::INFO) << makeTable( array2d, row_labels, column_labels);
688// ATH_MSG_INFO( makeTable( array2d, row_labels, column_labels) );
689template <typename T>
690inline MsgStream &operator<<(MsgStream &out,
692{
693 return dumpTable(out,
694 stat.m_counter,
695 stat.m_rowLabel,
696 stat.m_columnLabel,
697 stat.m_topLeftLable,
698 stat.m_labelPrefix,
699 stat.m_columnWidth,
700 stat.m_minLabelWidth,
701 stat.m_dumpHeader,
702 stat.m_dumpFooter,
703 stat.m_separateLastRow,
704 stat.m_precision);
705}
706
707
708// convenience method to dump wrapped two dimensional arrays in table form to a std output stream
709// Usage: out << makeTable( array2d, row_labels, column_labels);
710template <typename T>
711inline std::ostream &operator<<(std::ostream &out,
712 const TableUtils::StatTable<T> &stat)
713{
714 return dumpTable(out,
715 stat.m_counter,
716 stat.m_label,
717 stat.m_labelPrefix,
718 stat.m_columnWidth,
719 stat.m_minLabelWidth,
720 stat.m_dumpFooter,
721 stat.m_separateLastRow,
722 stat.m_precision);
723}
724
725
726
727#endif
static Double_t a
static const std::vector< std::string > bins
MsgStream & operator<<(MsgStream &out, const TableUtils::StatTable< T > &stat)
Definition TableUtils.h:672
TableUtils::StatTable< T > makeTable(const std::array< T, N > &counter, const std::array< std::string, N > &label)
Definition TableUtils.h:543
static const Attributes_t empty
std::string label(const std::string &format, int i)
Definition label.h:19
std::tuple< std::vector< std::string >, std::vector< RatioDefinition > > splitRatioDefinitionsAndLabels(std::initializer_list< std::tuple< std::string, RatioDefinition > > a_ratio_list)
Definition TableUtils.h:468
SummandDefinition defineSummand(T counter_idx, int multiplier)
Definition TableUtils.h:422
constexpr std::size_t categoryStride(const std::size_t categories, const std::size_t sub_categories, const std::size_t n_counter)
Definition TableUtils.h:319
float computeRatio(std::size_t numerator, std::size_t denominator)
Definition TableUtils.h:434
T_Stream & dumpTable(T_Stream &out, Range< T_Counter > counter, const Range< std::string > &label, const std::string &label_prefix, const std::size_t column_width, const std::size_t min_label_width, const bool dump_footer, const bool separate_last_row, const unsigned int precision)
Definition TableUtils.h:105
std::size_t maxLabelWidth(const T_Collection &col)
Definition TableUtils.h:310
RatioDefinition defineSimpleRatio(T numerator, T denominator)
Definition TableUtils.h:404
std::size_t computeSum(const std::vector< SummandDefinition > &sum_def, std::size_t eta_offset, std::size_t row_stride, std::size_t seed_i, const std::vector< std::size_t > &stat)
Definition TableUtils.cxx:8
std::vector< std::string > makeLabelVector(T_index n_entries, std::initializer_list< std::pair< T_index, T_string > > a_list)
Definition TableUtils.h:295
constexpr std::size_t subCategoryStride(const std::size_t categories, const std::size_t sub_categories, const std::size_t n_counter)
Definition TableUtils.h:324
constexpr std::size_t counterStride(const std::size_t categories, const std::size_t sub_categories, const std::size_t n_counter)
Definition TableUtils.h:329
std::string makeEtaBinLabel(const std::vector< float > &eta_bins, std::size_t eta_bin_i, bool abs_eta=false)
Definition TableUtils.h:534
std::tuple< std::string, RatioDefinition > makeRatioDefinition(std::string &&name, std::vector< SummandDefinition > &&numerator, std::vector< SummandDefinition > &&denominator)
Definition TableUtils.h:458
std::pair< std::vector< SummandDefinition >, std::vector< SummandDefinition > > RatioDefinition
Definition TableUtils.h:397
std::string makeBinLabel(const std::string &variable_name, const std::vector< float > &bins, std::size_t bin_i, bool abs_value=false, int precision=1)
Definition TableUtils.h:505
std::vector< float > computeRatios(const std::vector< RatioDefinition > &ratio_def, const std::size_t categories, const std::size_t sub_categories, const std::vector< std::size_t > &counter)
constexpr std::size_t ratioStride(const std::size_t categories, const std::size_t sub_categories, const std::vector< RatioDefinition > &ratio_def)
Definition TableUtils.h:493
std::vector< T_Output > createCounterArrayWithProjections(const std::size_t categories, const std::size_t sub_categories, const std::vector< std::array< T_Input, N > > &input_counts)
Definition TableUtils.h:342
std::pair< std::size_t, int > SummandDefinition
Definition TableUtils.h:396
Definition index.py:1
MultiColumnTable & dumpFooter(bool value=true)
Definition TableUtils.h:260
Range< std::string > m_columnLabel
Definition TableUtils.h:255
friend std::ostream & operator<<(std::ostream &out, const MultiColumnTable &stat)
Definition TableUtils.h:276
MultiColumnTable & separateLastRow(bool value=true)
Definition TableUtils.h:261
MultiColumnTable & columnWidth(std::size_t value)
Definition TableUtils.h:257
MultiColumnTable & minLabelWidth(std::size_t value)
Definition TableUtils.h:258
Range< std::string > m_rowLabel
Definition TableUtils.h:254
std::vector< unsigned int > m_precision
Definition TableUtils.h:268
MultiColumnTable & precision(std::vector< unsigned int > &&precision)
Definition TableUtils.h:263
MultiColumnTable & dumpHeader(bool value=true)
Definition TableUtils.h:259
MultiColumnTable & labelPrefix(const std::string &value)
Definition TableUtils.h:262
bool operator!=(const const_iterator &other) const
Definition TableUtils.h:73
std::size_t m_offset
Definition TableUtils.h:68
const_iterator begin() const
Definition TableUtils.h:81
std::size_t m_columns
Definition TableUtils.h:65
const_iterator end() const
Definition TableUtils.h:84
std::size_t m_rows
Definition TableUtils.h:64
bool equalSize(const T_Other &other_range)
Definition TableUtils.h:97
std::size_t nRows() const
Definition TableUtils.h:93
std::size_t m_columnOffset
Definition TableUtils.h:66
std::size_t nColumns() const
Definition TableUtils.h:92
std::size_t m_firstColumnIndex
Definition TableUtils.h:67
Range< T > operator[](std::size_t index) const
Definition TableUtils.h:87
const_iterator & operator++()
Definition TableUtils.h:32
bool operator!=(const const_iterator &other) const
Definition TableUtils.h:34
bool equalSize(const T_Other &other_range)
Definition TableUtils.h:51
std::size_t size() const
Definition TableUtils.h:47
const_iterator begin() const
Definition TableUtils.h:38
const T & operator[](std::size_t index) const
Definition TableUtils.h:45
const_iterator end() const
Definition TableUtils.h:41
const T * m_ptr
Definition TableUtils.h:27
std::size_t m_offset
Definition TableUtils.h:29
std::size_t m_size
Definition TableUtils.h:28
StatTable & precision(unsigned int precision)
Definition TableUtils.h:240
Range< std::string > m_label
Definition TableUtils.h:233
unsigned int m_precision
Definition TableUtils.h:245
StatTable & minLabelWidth(std::size_t value)
Definition TableUtils.h:235
StatTable & separateLastRow(bool value=true)
Definition TableUtils.h:238
StatTable & dumpFooter(bool value=true)
Definition TableUtils.h:237
StatTable & columnWidth(std::size_t value)
Definition TableUtils.h:234
StatTable & labelPrefix(const std::string &value)
Definition TableUtils.h:239
std::size_t m_columnWidth
Definition TableUtils.h:243
StatTable & dumpHeader(bool value=true)
Definition TableUtils.h:236
std::size_t m_minLabelWidth
Definition TableUtils.h:244
std::string m_labelPrefix
Definition TableUtils.h:242
MsgStream & msg
Definition testRead.cxx:32